package com.gxwebsoft.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.oa.entity.AppUser;
import com.gxwebsoft.oa.param.AppUserParam;
import com.gxwebsoft.oa.service.AppUserService;
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-05-31 13:18:55
 */
@Api(tags = "应用成员管理")
@RestController
@RequestMapping("/api/oa/app-user")
public class AppUserController extends BaseController {
    @Resource
    private AppUserService appUserService;

    @PreAuthorize("hasAuthority('oa:appUser:list')")
    @OperationLog
    @ApiOperation("分页查询应用成员")
    @GetMapping("/page")
    public ApiResult<PageResult<AppUser>> page(AppUserParam param) {
        // 使用关联查询
        return success(appUserService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('oa:appUser:list')")
    @OperationLog
    @ApiOperation("查询全部应用成员")
    @GetMapping()
    public ApiResult<List<AppUser>> list(AppUserParam param) {
        // 使用关联查询
        if (param.getAppId() == null) {
          return fail("AppId不存在",null);
        }
        return success(appUserService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:appUser:list')")
    @OperationLog
    @ApiOperation("根据id查询应用成员")
    @GetMapping("/{id}")
    public ApiResult<AppUser> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(appUserService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('oa:appUser:save')")
    @OperationLog
    @ApiOperation("添加应用成员")
    @PostMapping()
    public ApiResult<?> save(@RequestBody AppUser appUser) {
        if (appUserService.count(new LambdaQueryWrapper<AppUser>()
          .eq(AppUser::getUserId, appUser.getUserId()).eq(AppUser::getAppId,appUser.getAppId())) > 0) {
          return fail("该成员已存在");
        }
        if (appUserService.save(appUser)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:appUser:update')")
    @OperationLog
    @ApiOperation("修改应用成员")
    @PutMapping()
    public ApiResult<?> update(@RequestBody AppUser appUser) {
        if (appUserService.updateById(appUser)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:appUser:remove')")
    @OperationLog
    @ApiOperation("删除应用成员")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (appUserService.removeById(id)) {
            return success("移除成功");
        }
        return fail("移除失败");
    }

    @PreAuthorize("hasAuthority('oa:appUser:save')")
    @OperationLog
    @ApiOperation("批量添加应用成员")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<AppUser> list) {
        if (appUserService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:appUser:update')")
    @OperationLog
    @ApiOperation("批量修改应用成员")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<AppUser> batchParam) {
        if (batchParam.update(appUserService, "app_user_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:appUser:remove')")
    @OperationLog
    @ApiOperation("批量删除应用成员")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (appUserService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
