package com.gxwebsoft.open.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.core.web.*;
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.web.bind.annotation.*;

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

/**
 * 应用成员控制器
 *
 * @author 科技小王子
 * @since 2023-02-17 13:22:20
 */
@Api(tags = "应用成员管理")
@RestController
@RequestMapping("/api/open/app-user")
public class OpenAppUserController extends BaseController {
    @Resource
    private AppUserService appUserService;

    @OperationLog
    @ApiOperation("分页查询应用成员")
    @GetMapping("/page")
    public ApiResult<PageResult<AppUser>> page(AppUserParam param) {
        // 验证签名
        isCheckSign();
        PageParam<AppUser, AppUserParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(appUserService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(appUserService.pageRel(param));
    }

    @OperationLog
    @ApiOperation("查询全部应用成员")
    @GetMapping()
    public ApiResult<List<AppUser>> list(AppUserParam param) {
      // 验证签名
      isCheckSign();
        PageParam<AppUser, AppUserParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(appUserService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(appUserService.listRel(param));
    }

    @ApiOperation("根据id查询应用成员")
    @GetMapping("/{id}")
    public ApiResult<AppUser> get(@PathVariable("id") Integer id) {
      // 验证签名
      isCheckSign();
        return success(appUserService.getById(id));
        // 使用关联查询
        //return success(appUserService.getByIdRel(id));
    }

    @OperationLog
    @ApiOperation("添加应用成员")
    @PostMapping()
    public ApiResult<?> save(@RequestBody AppUser appUser) {
        // 验证签名
        isCheckSign();
        String username = appUser.getUsername();
        Integer appId = appUser.getAppId();
        Integer userIdByUsername = getUserIdByUsername(username,getTenantId());
        // 判断重复
        if(appUserService.count(new LambdaQueryWrapper<AppUser>().eq(AppUser::getUserId,userIdByUsername).eq(AppUser::getAppId,appId)) > 0){
          throw new BusinessException("请勿重复添加");
        }
        // 是否超出限制
        if(appUserService.count(new LambdaQueryWrapper<AppUser>().eq(AppUser::getAppId,appUser.getAppId())) > 50){
          throw new BusinessException("最多只能添加50个");
        }
        appUser.setUserId(userIdByUsername);
        appUser.setStatus(0);
        if (appUserService.save(appUser)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @OperationLog
    @ApiOperation("修改应用成员")
    @PutMapping()
    public ApiResult<?> update(@RequestBody AppUser appUser) {
      // 验证签名
      isCheckSign();
        if (appUserService.updateById(appUser)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @OperationLog
    @ApiOperation("删除应用成员")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
      // 验证签名
      isCheckSign();
      AppUser appUser = appUserService.getById(id);
      if(appUser.getRole().equals(30)){
        return fail("管理员不能删除");
      }
      if (appUserService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @OperationLog
    @ApiOperation("批量添加应用成员")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<AppUser> list) {
      // 验证签名
      isCheckSign();
        if (appUserService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @OperationLog
    @ApiOperation("批量修改应用成员")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<AppUser> batchParam) {
      // 验证签名
      isCheckSign();
        if (batchParam.update(appUserService, "app_user_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @OperationLog
    @ApiOperation("批量删除应用成员")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
      // 验证签名
      isCheckSign();
        if (appUserService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
