package com.gxwebsoft.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.security.JwtUtil;
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.CompanyUser;
import com.gxwebsoft.oa.param.CompanyUserParam;
import com.gxwebsoft.oa.service.CompanyUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 成员管理控制器
 *
 * @author 科技小王子
 * @since 2024-02-04 14:44:42
 */
@Api(tags = "成员管理管理")
@RestController
@RequestMapping("/api/oa/company-user")
public class CompanyUserController extends BaseController {
    @Resource
    private CompanyUserService companyUserService;

    @ApiOperation("分页查询成员管理")
    @GetMapping("/page")
    public ApiResult<PageResult<CompanyUser>> page(CompanyUserParam param, HttpServletRequest request) {
      // 使用关联查询
      param.setToken(JwtUtil.getAccessToken(request));
      param.setTenantId(getTenantId());
      return success(companyUserService.pageRel(param));
    }

    @ApiOperation("查询全部成员管理")
    @GetMapping()
    public ApiResult<List<CompanyUser>> list(CompanyUserParam param) {
        // 使用关联查询
        if (param.getCompanyId() == null) {
          return fail("该企业不存在",null);
        }
        // 使用关联查询
        return success(companyUserService.listRel(param));
    }

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

    @ApiOperation("添加成员管理")
    @PostMapping()
    public ApiResult<?> save(@RequestBody CompanyUser companyUser) {
        if (companyUserService.count(new LambdaQueryWrapper<CompanyUser>()
          .eq(CompanyUser::getUserId, companyUser.getUserId()).eq(CompanyUser::getCompanyId,companyUser.getCompanyId())) > 0) {
          return fail("该成员已存在");
        }
        if (companyUserService.save(companyUser)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改成员管理")
    @PutMapping()
    public ApiResult<?> update(@RequestBody CompanyUser companyUser) {
        if (companyUserService.updateById(companyUser)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
