package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaAssetsUserService;
import com.gxwebsoft.oa.entity.OaAssetsUser;
import com.gxwebsoft.oa.param.OaAssetsUserParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 服务器成员管理控制器
 *
 * @author 科技小王子
 * @since 2024-09-10 20:57:41
 */
@Tag(name = "服务器成员管理管理")
@RestController
@RequestMapping("/api/oa/oa-assets-user")
public class OaAssetsUserController extends BaseController {
    @Resource
    private OaAssetsUserService oaAssetsUserService;

    @Operation(summary = "分页查询服务器成员管理")
    @GetMapping("/page")
    public ApiResult<PageResult<OaAssetsUser>> page(OaAssetsUserParam param) {
        // 使用关联查询
        return success(oaAssetsUserService.pageRel(param));
    }

    @Operation(summary = "查询全部服务器成员管理")
    @GetMapping()
    public ApiResult<List<OaAssetsUser>> list(OaAssetsUserParam param) {
        PageParam<OaAssetsUser, OaAssetsUserParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(oaAssetsUserService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(oaAssetsUserService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsUser:list')")
    @OperationLog
    @Operation(summary = "根据id查询服务器成员管理")
    @GetMapping("/{id}")
    public ApiResult<OaAssetsUser> get(@PathVariable("id") Integer id) {
        return success(oaAssetsUserService.getById(id));
        // 使用关联查询
        //return success(oaAssetsUserService.getByIdRel(id));
    }

    @Operation(summary = "添加服务器成员管理")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaAssetsUser oaAssetsUser) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          oaAssetsUser.setUserId(loginUser.getUserId());
        }
        if (oaAssetsUserService.save(oaAssetsUser)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "修改服务器成员管理")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaAssetsUser oaAssetsUser) {
        if (oaAssetsUserService.updateById(oaAssetsUser)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "删除服务器成员管理")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (oaAssetsUserService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @Operation(summary = "批量添加服务器成员管理")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OaAssetsUser> list) {
        if (oaAssetsUserService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "批量修改服务器成员管理")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaAssetsUser> batchParam) {
        if (batchParam.update(oaAssetsUserService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "批量删除服务器成员管理")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (oaAssetsUserService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
