|
@@ -0,0 +1,205 @@
|
|
|
+package com.dragoninfo.dcuc.auth.sub.business.impl;
|
|
|
+
|
|
|
+import com.dragoninfo.dcuc.auth.constance.Constant;
|
|
|
+import com.dragoninfo.dcuc.auth.sub.business.IAuthSubTaskTypeBusiness;
|
|
|
+import com.dragoninfo.dcuc.auth.sub.dto.tasktype.ExcelImpTaskType;
|
|
|
+import com.dragoninfo.dcuc.auth.sub.entity.AuthSubTaskType;
|
|
|
+import com.dragoninfo.dcuc.auth.sub.service.IAuthSubTaskTypeService;
|
|
|
+import com.dragoninfo.dcuc.auth.sub.vo.tasktype.AuthSubTaskTypeVo;
|
|
|
+import com.dragoninfo.dcuc.auth.sub.vo.tasktype.TaskTypeTreeVo;
|
|
|
+import com.dragoninfo.dcuc.auth.util.ImportUtil;
|
|
|
+import com.dragoninfo.dcuc.common.utils.DcucBeanUtil;
|
|
|
+import com.dragoninfo.dcuc.duceap.facade.IDuceapUploadFacade;
|
|
|
+import com.dragoninfo.dcuc.duceap.upload.dto.DocContentDTO;
|
|
|
+import com.dragonsoft.duceap.base.entity.http.ResponseStatus;
|
|
|
+import com.dragonsoft.duceap.base.entity.search.SearchDTO;
|
|
|
+import com.dragonsoft.duceap.commons.util.string.StringUtils;
|
|
|
+import com.dragonsoft.duceap.core.search.Searchable;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
|
+import org.apache.poi.ss.usermodel.Row;
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author mazq
|
|
|
+ * @date 2023/2/13
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class AuthSubTaskTypeBusinessImpl implements IAuthSubTaskTypeBusiness {
|
|
|
+
|
|
|
+ private final List<String> excelTitle = Arrays.asList("任务类型名称", "任务类型编码", "上级任务类型编码");
|
|
|
+
|
|
|
+ private IAuthSubTaskTypeService taskTypeService;
|
|
|
+
|
|
|
+ private IDuceapUploadFacade uploadFacade;
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setTaskTypeService(IAuthSubTaskTypeService taskTypeService) {
|
|
|
+ this.taskTypeService = taskTypeService;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setUploadFacade(IDuceapUploadFacade uploadFacade) {
|
|
|
+ this.uploadFacade = uploadFacade;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public ResponseStatus authSubTaskTypeImp(String fileId) {
|
|
|
+ DocContentDTO fileContent = uploadFacade.getFileContent(fileId);
|
|
|
+ InputStream inputStream = new ByteArrayInputStream(fileContent.getFileData());
|
|
|
+ try {
|
|
|
+ List<ExcelImpTaskType> list = loadTaskTypeFromExcel(inputStream);
|
|
|
+ //对数据进行校验
|
|
|
+ verifyTaskTypeInfo(list);
|
|
|
+ //保存数据
|
|
|
+ saveImport(list);
|
|
|
+ } catch (IOException e) {
|
|
|
+ return ResponseStatus.fail("文件格式不对");
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ return ResponseStatus.fail(e.getMessage());
|
|
|
+ }
|
|
|
+ return ResponseStatus.success("导入成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveImport(List<ExcelImpTaskType> list) {
|
|
|
+ List<AuthSubTaskType> taskTypes = DcucBeanUtil.createCopyToObjectList(list, AuthSubTaskType.class);
|
|
|
+ taskTypeService.saveByCodes(taskTypes);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void verifyTaskTypeInfo(List<ExcelImpTaskType> list) {
|
|
|
+ // 校验导入的code是否重复
|
|
|
+ Map<String, List<ExcelImpTaskType>> map = list.stream()
|
|
|
+ .collect(Collectors.groupingBy(ExcelImpTaskType::getTaskTypeCode));
|
|
|
+ for (List<ExcelImpTaskType> value : map.values()) {
|
|
|
+ if (value.size() > 1) {
|
|
|
+ ExcelImpTaskType taskType = value.get(0);
|
|
|
+ String taskTypeCode = taskType.getTaskTypeCode();
|
|
|
+ throw new NumberFormatException("导入行中任务类型编码:【" + taskTypeCode + "】存在重复数据");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验数据中code是否存在
|
|
|
+ List<AuthSubTaskType> existCodes = taskTypeService.getByCodes(map.keySet());
|
|
|
+ if (CollectionUtils.isNotEmpty(existCodes)) {
|
|
|
+ String collect = existCodes.stream()
|
|
|
+ .map(AuthSubTaskType::getTaskTypeCode)
|
|
|
+ .collect(Collectors.joining(Constant.CHINESE_COMMA));
|
|
|
+ throw new NumberFormatException("任务类型编码:【" + collect + "】已存在数据库");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<ExcelImpTaskType> loadTaskTypeFromExcel(InputStream inputStream) throws IOException {
|
|
|
+ List<ExcelImpTaskType> list = new ArrayList<>();
|
|
|
+ Workbook wb0 = new HSSFWorkbook(inputStream);
|
|
|
+ Sheet sheet = wb0.getSheetAt(0);
|
|
|
+ for (Row r : sheet) {
|
|
|
+ if (r.getRowNum() == 0) {
|
|
|
+ ImportUtil.checkTitle(r, excelTitle);
|
|
|
+ } else {
|
|
|
+ if (ImportUtil.checkIsEmpty(r, 3)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ ExcelImpTaskType orgInfo = getTaskTypeInfoFromRow(r);
|
|
|
+ list.add(orgInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ExcelImpTaskType getTaskTypeInfoFromRow(Row r) {
|
|
|
+ ExcelImpTaskType taskType = new ExcelImpTaskType();
|
|
|
+ taskType.setRowNum(r.getRowNum());
|
|
|
+
|
|
|
+ Cell cell;
|
|
|
+ cell = r.getCell(0);
|
|
|
+ // 设置任务类型名称
|
|
|
+ String taskTypeName = ImportUtil.getCellStringValue(cell);
|
|
|
+ taskType.setTaskTypeName(taskTypeName);
|
|
|
+
|
|
|
+ // 设置任务类型编码
|
|
|
+ cell = r.getCell(1);
|
|
|
+ String taskTypeCode = ImportUtil.getCellStringValue(cell);
|
|
|
+ taskType.setTaskTypeCode(taskTypeCode);
|
|
|
+
|
|
|
+ // 设置创建时间
|
|
|
+ cell = r.getCell(2);
|
|
|
+ String parentCode = ImportUtil.getCellStringValue(cell);
|
|
|
+ taskType.setParentCode(parentCode);
|
|
|
+ return taskType;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<AuthSubTaskTypeVo> pageSearch(SearchDTO searchDTO) {
|
|
|
+ Searchable searchable = Searchable.toSearchable(searchDTO);
|
|
|
+ Page<AuthSubTaskType> page = taskTypeService.pageSearch(searchable);
|
|
|
+ return DcucBeanUtil.createCopyToObjectPage(page, AuthSubTaskTypeVo.class);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<TaskTypeTreeVo> taskTypeTree() {
|
|
|
+ List<AuthSubTaskType> list = taskTypeService.findAll();
|
|
|
+ List<TaskTypeTreeVo> vos = getVoList(list);
|
|
|
+ return getTreeVos(vos);
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<TaskTypeTreeVo> getTreeVos(List<TaskTypeTreeVo> vos) {
|
|
|
+ Map<Boolean, List<TaskTypeTreeVo>> partMap = vos.stream()
|
|
|
+ .collect(Collectors.partitioningBy(e -> StringUtils.isBlank(e.getPid())));
|
|
|
+ // 最顶级的父节点
|
|
|
+ List<TaskTypeTreeVo> topNodes = partMap.get(Boolean.TRUE);
|
|
|
+ // 子节点
|
|
|
+ List<TaskTypeTreeVo> childNodes = partMap.get(Boolean.FALSE);
|
|
|
+ Map<String, List<TaskTypeTreeVo>> groupMap = childNodes.stream()
|
|
|
+ .collect(Collectors.groupingBy(TaskTypeTreeVo::getPid));
|
|
|
+
|
|
|
+ Deque<TaskTypeTreeVo> queue = new LinkedList<>(topNodes);
|
|
|
+ while (!queue.isEmpty()) {
|
|
|
+ TaskTypeTreeVo firstNode = queue.poll();
|
|
|
+ String id = firstNode.getId();
|
|
|
+ List<TaskTypeTreeVo> childVos = groupMap.get(id);
|
|
|
+ if (CollectionUtils.isNotEmpty(childVos)) {
|
|
|
+ firstNode.setChildren(childVos);
|
|
|
+ firstNode.setIsParent(Boolean.TRUE);
|
|
|
+ queue.addAll(childVos);
|
|
|
+ } else {
|
|
|
+ firstNode.setIsParent(Boolean.FALSE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return topNodes;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<TaskTypeTreeVo> getVoList(List<AuthSubTaskType> list) {
|
|
|
+ return list.stream()
|
|
|
+ .map(e -> {
|
|
|
+ TaskTypeTreeVo treeVo = new TaskTypeTreeVo();
|
|
|
+ treeVo.setId(e.getId());
|
|
|
+ treeVo.setPid(e.getParentId());
|
|
|
+ treeVo.setName(e.getTaskTypeName());
|
|
|
+ treeVo.setCode(e.getTaskTypeCode());
|
|
|
+ treeVo.setIsParent(false);
|
|
|
+ return treeVo;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|