package com.gxwebsoft.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.AssetsUserService;
import com.gxwebsoft.oa.entity.AssetsUser;
import com.gxwebsoft.oa.param.AssetsUserParam;
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.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 服务器成员管理控制器
 *
 * @author 科技小王子
 * @since 2023-10-26 21:08:27
 */
@Api(tags = "服务器成员管理管理")
@RestController
@RequestMapping("/api/oa/assets-user")
public class AssetsUserController extends BaseController {
    @Resource
    private AssetsUserService assetsUserService;

    @ApiOperation("分页查询服务器成员管理")
    @GetMapping("/page")
    public ApiResult<PageResult<AssetsUser>> page(AssetsUserParam param) {
        // 使用关联查询
        return success(assetsUserService.pageRel(param));
    }

    @ApiOperation("查询全部服务器成员管理")
    @GetMapping()
    public ApiResult<List<AssetsUser>> list(AssetsUserParam param) {
        // 使用关联查询
        return success(assetsUserService.listRel(param));
    }

    @ApiOperation("根据id查询服务器成员管理")
    @GetMapping("/{id}")
    public ApiResult<AssetsUser> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(assetsUserService.getByIdRel(id));
    }

    @ApiOperation("添加服务器成员管理")
    @PostMapping()
    public ApiResult<?> save(@RequestBody AssetsUser assetsUser) {
        if (assetsUserService.count(new LambdaQueryWrapper<AssetsUser>()
                .eq(AssetsUser::getUserId, assetsUser.getUserId()).eq(AssetsUser::getAssetsId,assetsUser.getAssetsId())) > 0) {
            return fail("该成员已存在");
        }
        if (assetsUserService.save(assetsUser)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改服务器成员管理")
    @PutMapping()
    public ApiResult<?> update(@RequestBody AssetsUser assetsUser) {
        if (assetsUserService.updateById(assetsUser)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
