| | |
| | | import cn.afterturn.easypoi.excel.ExcelImportUtil; |
| | | import cn.afterturn.easypoi.excel.entity.ImportParams; |
| | | import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult; |
| | | 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.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.basic.PageVO; |
| | | import com.ruoyi.common.basic.PageDTO; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.BeanUtils; |
| | | import com.ruoyi.common.utils.CollUtils; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | | import com.ruoyi.system.domain.TbDept; |
| | | import com.ruoyi.system.dto.update.DeptFocusDTO; |
| | | import com.ruoyi.system.handler.DeptVerifyHandler; |
| | | import com.ruoyi.system.importExcel.DeptExcel; |
| | | import com.ruoyi.system.mapper.TbDeptMapper; |
| | | import com.ruoyi.system.query.DeptQuery; |
| | | import com.ruoyi.system.service.TbDeptService; |
| | |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.InputStream; |
| | | import java.util.*; |
| | | |
| | |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | public class TbDeptServiceImpl extends ServiceImpl<TbDeptMapper, TbDept> implements TbDeptService { |
| | | |
| | | private final DeptVerifyHandler deptVerifyHandler; |
| | | private final HttpServletResponse response; |
| | | |
| | | @Override |
| | | public PageVO<DeptVO> queryPage(DeptQuery query) { |
| | | public PageDTO<DeptVO> queryPage(DeptQuery query) { |
| | | Page<TbDept> page = new Page<>(query.getPageNum(), query.getPageSize()); |
| | | LambdaQueryWrapper<TbDept> queryWrapper = new LambdaQueryWrapper<TbDept>() |
| | | .like(StringUtils.isNotEmpty(query.getAreaName()), TbDept::getAreaName, query.getAreaName()) |
| | | Page<TbDept> tbDeptPage = this.lambdaQuery().like(StringUtils.isNotEmpty(query.getAreaName()), TbDept::getAreaName, query.getAreaName()) |
| | | .like(StringUtils.isNotEmpty(query.getAccount()), TbDept::getAccount, query.getAccount()) |
| | | .like(StringUtils.isNotEmpty(query.getPhone()), TbDept::getPhone, query.getPhone()) |
| | | .orderByDesc(TbDept::getCreateTime); |
| | | Page<TbDept> deptPage = this.baseMapper.selectPage(page, queryWrapper); |
| | | PageVO<DeptVO> pageVO = new PageVO<>(); |
| | | pageVO.setPageNo(deptPage.getPages()); |
| | | pageVO.setPageSize(deptPage.getSize()); |
| | | pageVO.setTotalPages(deptPage.getTotal()); |
| | | pageVO.setTotalCount(deptPage.getTotal()); |
| | | IPage<DeptVO> convert = deptPage.convert(result -> { |
| | | DeptVO deptVO = new DeptVO(); |
| | | BeanUtils.copyBeanProp(deptVO, result); |
| | | return deptVO; |
| | | }); |
| | | pageVO.setRecords(convert.getRecords()); |
| | | return pageVO; |
| | | .orderByDesc(TbDept::getCreateTime) |
| | | .page(page); |
| | | if (CollUtils.isEmpty(tbDeptPage.getRecords())) { |
| | | return PageDTO.empty(page); |
| | | } |
| | | return PageDTO.of(tbDeptPage, DeptVO.class); |
| | | } |
| | | |
| | | @Override |
| | |
| | | //使用自定义校验规则 |
| | | importParams.setVerifyHandler(deptVerifyHandler); |
| | | InputStream inputStream = file.getInputStream(); |
| | | ExcelImportResult<TbDept> result = ExcelImportUtil.importExcelMore(inputStream, TbDept.class, importParams); |
| | | ExcelImportResult<DeptExcel> result = ExcelImportUtil.importExcelMore(inputStream, DeptExcel.class, importParams); |
| | | inputStream.close(); |
| | | List<TbDept> list = result.getList(); |
| | | if (Objects.requireNonNull(result).isVerfiyFail() || CollectionUtils.isEmpty(list)) { |
| | | throw new RuntimeException("文件校验失败,请检查数据填写是否完整"); |
| | | List<DeptExcel> list = result.getList(); |
| | | |
| | | if (result.isVerifyFail() || CollectionUtils.isEmpty(list)) { |
| | | throw new ServiceException("文件校验失败,请检查数据填写是否完整"); |
| | | } |
| | | List<String> strings = hasDuplicateAreaCode(list); |
| | | if (!CollectionUtils.isEmpty(strings)) { |
| | | throw new RuntimeException(String.format("区划代码%s重复,请修改后重新导入", String.join(",", strings))); |
| | | throw new ServiceException(String.format("区划代码%s重复,请修改后重新导入", String.join(",", strings))); |
| | | }else { |
| | | List<TbDept> tbDeptList = BeanUtils.copyList(list, TbDept.class); |
| | | this.remove(null); |
| | | this.saveBatch(list); |
| | | tbDeptList.forEach(dept->{ |
| | | dept.setPassword(SecurityUtils.encryptPassword(dept.getPassword())); |
| | | }); |
| | | this.saveBatch(tbDeptList); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 校验区划代码是否重复 |
| | | * @param deptList |
| | | * @return |
| | | * @param deptList 部门列表 |
| | | * @return 重复的区划代码 |
| | | */ |
| | | public List<String> hasDuplicateAreaCode(List<TbDept> deptList) { |
| | | public List<String> hasDuplicateAreaCode(List<DeptExcel> deptList) { |
| | | Set<String> areaCodes = new HashSet<>(); |
| | | List<String> result = new ArrayList<>(); |
| | | for (TbDept dept : deptList) { |
| | | for (DeptExcel dept : deptList) { |
| | | if (!areaCodes.add(dept.getAreaCode())) { |
| | | result.add(dept.getAreaCode()); |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public void focus(DeptFocusDTO dto) { |
| | | TbDept dept = this.getById(dto.getId()); |
| | | if (Objects.isNull(dept)) { |
| | | throw new ServiceException("非法参数"); |
| | | } |
| | | this.lambdaUpdate() |
| | | .eq(TbDept::getId, dto.getId()) |
| | | .set(TbDept::getFocussed, dto.getFocussed()) |
| | | .update(); |
| | | } |
| | | } |