package com.ycl.controller.depart;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ycl.annotation.LogSave;
import com.ycl.api.BasePageDTO;
import com.ycl.api.BasePageVO;
import com.ycl.api.CommonResult;
import com.ycl.controller.BaseController;
import com.ycl.dto.UmsDepartDto;
import com.ycl.entity.depart.UmsDepart;
import com.ycl.entity.dict.DataDictionary;
import com.ycl.mapper.depart.UmsDepartMapper;
import com.ycl.mapper.dict.DataDictionary2Mapper;
import com.ycl.service.depart.UmsDepartService;
import com.ycl.service.user.UmsDepartManageService;
import com.ycl.utils.EasyExcelUtils;
import com.ycl.utils.auth.UserAuthUtil;
import com.ycl.vo.depart.DepartVO;
import com.ycl.vo.depart.UmsDepartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
/**
*
* 部门表 前端控制器
*
*
* @author lyq
* @since 2022-09-07
*/
@RestController
@Api(tags = "部门管理模块")
@RequestMapping("/depart")
public class DepartController extends BaseController {
@Autowired
private UmsDepartService departService;
@Resource
private UmsDepartManageService umsAdminDepartService;
@Resource
private UserAuthUtil userAuthUtil;
@ApiOperation("添加部门")
@PostMapping(value = "/create")
@LogSave(operationType = "部门管理", contain = "添加部门")
public CommonResult create(@Validated @RequestBody DepartVO.AddDepartVO addDepartVO) {
departService.create(addDepartVO);
return CommonResult.success(null);
}
@ApiOperation("编辑部门")
@PostMapping(value = "/update")
@LogSave(operationType = "部门管理", contain = "编辑部门")
public CommonResult create(@Validated @RequestBody DepartVO.UpdateDepartVO params) {
departService.update(params);
return CommonResult.success(null);
}
@ApiOperation("删除")
@PostMapping(value = "/delete")
@LogSave(operationType = "部门管理", contain = "删除部门")
public CommonResult delete(@Validated @RequestBody DepartVO.IdDepartVO params) {
departService.delete(params.getId());
return CommonResult.success(null);
}
/**
* @return com.ycl.api.CommonResult
* @Description batch deletion departments
* @Param [ids]
**/
@ApiOperation("批处理-删除")
@PostMapping(value = "/batch_deletion")
@LogSave(operationType = "部门管理", contain = "批量删除部门")
public CommonResult delete(@RequestParam List ids) {
if (ids.isEmpty()) {
return CommonResult.failed("bad request parameter");
}
return CommonResult.success(departService.removeBatchByIds(ids));
}
@ApiOperation("批处理-开启或者禁用部门")
@PutMapping(value = "/batch_status")
@LogSave(operationType = "部门管理", contain = "批量开启或者禁用部门")
public CommonResult updateBatch(@RequestBody List umsDepartList) {
if (umsDepartList.isEmpty()) {
return CommonResult.failed("bad request parameter");
}
return CommonResult.success(departService.updateBatchById(umsDepartList));
}
@ApiOperation("详情")
@PostMapping(value = "/detail")
@LogSave(operationType = "部门管理", contain = "查看部门")
public CommonResult detail(@Validated @RequestBody DepartVO.IdDepartVO params) {
UmsDepart sccgDepart = departService.loadDepartById(params.getId());
return CommonResult.success(sccgDepart);
}
@ApiOperation("树结构")
@GetMapping(value = "/tree")
public CommonResult> tree() {
List tree = departService.tree();
return CommonResult.success(tree);
}
@ApiOperation("修改部门状态")
@PostMapping(value = "/status")
public CommonResult status(@Validated @RequestBody DepartVO.StatusDepartVO params) {
departService.updateStatus(params);
return CommonResult.success(null);
}
@ApiOperation("查询全部部门")
@GetMapping(value = "/page")
public CommonResult> page(@RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize,
@RequestParam(value = "departName", required = false) String departName) {
IPage page = new Page<>(currentPage, pageSize);
departService.pageDepart(departName, page);
return CommonResult.success(page);
}
@ApiOperation("部门导出")
@PostMapping("/export")
public void exportDpt(HttpServletResponse response) {
List umsDepartDtos = departService.departExp();
String sheetName = "部门导出";
EasyExcelUtils.export1(response, sheetName, UmsDepartDto.class, umsDepartDtos, "部门导出");
}
@Resource
private UmsDepartMapper sccgDepartMapper;
@Resource
private DataDictionary2Mapper dataDictionary2Mapper;
@ApiOperation("部门导入")
@PostMapping("/import")
public CommonResult importDpt(MultipartFile file) throws IOException {
EasyExcel.read(file.getInputStream())
.head(UmsDepartDto.class)
.registerReadListener(new ReadListener() {
@Override
public void invoke(UmsDepartDto umsDepartDto, AnalysisContext analysisContext) {
// 不能重复插入
String departTypeName = umsDepartDto.getDepartTypeName();
UmsDepart one = sccgDepartMapper.selectOne(new LambdaQueryWrapper().eq(UmsDepart::getDepartName, departTypeName));
if (Objects.nonNull(one)){
throw new RuntimeException("该部门已经存在");
}
UmsDepart umsDepart = new UmsDepart();
// 设置部门名字
umsDepart.setDepartName(umsDepartDto.getDepartTypeName());
// 设置部门描述
umsDepart.setDepartDes(umsDepartDto.getDepartDes());
// 设置部门类型
DataDictionary dataDictionary = dataDictionary2Mapper.selectOne(new LambdaQueryWrapper().eq(DataDictionary::getName, umsDepartDto.getDepartType()));
if (Objects.isNull(dataDictionary)){
throw new RuntimeException("部门类型不存在");
}else {
umsDepart.setDepartType(new Long(dataDictionary.getId()).intValue());
}
// 设置父id
UmsDepart two = sccgDepartMapper.selectOne(new LambdaQueryWrapper().eq(UmsDepart::getDepartName, umsDepartDto.getParentDepartName()));
if (Objects.nonNull(two)){
umsDepart.setParentId(two.getParentId());
}
int insert = sccgDepartMapper.insert(umsDepart);
if (insert < 1){
throw new RuntimeException("插入失败");
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}).sheet().doRead();
return CommonResult.success(true, "导入成功");
}
@ApiOperation("查询我的部门")
@GetMapping(value = "/belongDepart")
public CommonResult belongDepart(BasePageVO params) {
BasePageDTO basePageDTO = umsAdminDepartService.belongDepart(fetchOperator(request).getOperatorId(), params.getCurrent(), params.getPageSize());
return CommonResult.success(basePageDTO);
}
@ApiOperation("用户修改角色")
@PutMapping(value = "/user_update_Role")
public CommonResult userUpdateRole(@RequestParam Long id,
@RequestParam List roles) {
umsAdminDepartService.userUpdateRoles(id, roles);
return CommonResult.success("update success");
}
@ApiOperation("用户修改部门")
@PutMapping(value = "/user_update_depart")
public CommonResult userUpdateDepart(@RequestParam Long userId,
@RequestParam Long departId) {
umsAdminDepartService.userUpdateDepart(userId, departId);
return CommonResult.success("update success");
}
@ApiOperation("查询不在部门的剩余人员")
@GetMapping("/query_surplus_user")
public CommonResult searchSurplusUser() {
return CommonResult.success(departService.selectSurplusUser());
}
@ApiOperation("查询不在部门的剩余人员和已在部门的人")
@GetMapping("/query_surplus_exist_user")
public CommonResult searchSurplusUser(@RequestParam Long id) {
return CommonResult.success(departService.selectSurplusUserAndExistUser(id));
}
@ApiOperation("查询顶级部门")
@GetMapping(value = "/query_father")
public CommonResult search() {
Integer fatherId = 0;
return CommonResult.success(departService
.list(new LambdaQueryWrapper()
.eq(UmsDepart::getParentId, fatherId)));
}
@ApiOperation("查询部门二级")
@GetMapping(value = "/query_father_children")
public CommonResult search(@RequestParam Integer fatherId) {
return CommonResult.success(departService
.list(new LambdaQueryWrapper()
.eq(UmsDepart::getParentId, fatherId)));
}
}