From 30e858fa504b268b9b436afca0a1259cf6e8c488 Mon Sep 17 00:00:00 2001 From: MrShi <1878285526@qq.com> Date: 星期二, 19 八月 2025 11:01:40 +0800 Subject: [PATCH] 优化 --- server/src/main/java/com/doumee/service/business/impl/CategoryServiceImpl.java | 788 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 769 insertions(+), 19 deletions(-) diff --git a/server/src/main/java/com/doumee/service/business/impl/CategoryServiceImpl.java b/server/src/main/java/com/doumee/service/business/impl/CategoryServiceImpl.java index 9dbee6a..4269564 100644 --- a/server/src/main/java/com/doumee/service/business/impl/CategoryServiceImpl.java +++ b/server/src/main/java/com/doumee/service/business/impl/CategoryServiceImpl.java @@ -1,20 +1,50 @@ package com.doumee.service.business.impl; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.doumee.core.annotation.excel.ExcelImporter; +import com.doumee.core.constants.Constants; +import com.doumee.core.constants.ResponseStatus; +import com.doumee.core.exception.BusinessException; +import com.doumee.core.model.LoginUserInfo; import com.doumee.core.model.PageData; import com.doumee.core.model.PageWrap; import com.doumee.core.utils.Utils; import com.doumee.dao.business.CategoryMapper; import com.doumee.dao.business.model.Category; +import com.doumee.dao.business.model.Company; +import com.doumee.dao.business.vo.CategoryDcaProblemDto; +import com.doumee.dao.business.vo.CompanyTree; +import com.doumee.dao.web.vo.CategoryMapTree; +import com.doumee.dao.web.vo.CategoryVO; +import com.doumee.dao.web.vo.CategoryVOTree; +import com.doumee.dao.business.vo.CategoryTree; +import com.doumee.dao.system.model.SystemUser; import com.doumee.service.business.CategoryService; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.github.yulichang.wrapper.MPJLambdaWrapper; +import io.swagger.models.auth.In; +import org.apache.commons.lang3.StringUtils; +import org.apache.poi.ss.usermodel.CellType; +import org.apache.shiro.SecurityUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; +import org.springframework.web.multipart.MultipartFile; -import java.util.List; +import javax.annotation.PostConstruct; +import javax.annotation.Resource; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.*; +import java.util.stream.Collectors; /** * 鍒嗙被淇℃伅琛⊿ervice瀹炵幇 @@ -24,18 +54,67 @@ @Service public class CategoryServiceImpl implements CategoryService { + @Resource(name="sessionRedisTemplate") + private RedisTemplate<Object, Serializable> redisTemplate; @Autowired private CategoryMapper categoryMapper; @Override public Integer create(Category category) { + if(StringUtils.isBlank(category.getName())){ + throw new BusinessException(ResponseStatus.BAD_REQUEST); + } + if(category.getType()==null){ + throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "瀵逛笉璧凤紝缁勭粐绫诲瀷涓嶆纭紝璇锋寜瑕佹眰濉啓~"); + } + /* //鏌ヨ鍚嶇О涓嶈兘閲嶅 + if(categoryMapper.selectCount(new QueryWrapper<Category>().lambda() + .eq(Category::getName,category.getName()) + .eq(Category::getIsdeleted,Constants.ZERO)) >0){ + throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "瀵逛笉璧凤紝鍚嶇О涓嶈兘閲嶅~"); + }*/ + LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); + category.setIdPath(category.getId()+"/");//鍚嶇О璺緞 + category.setNamePath(category.getName());//鍚嶇О璺緞 + String idPath = ""; + if(category.getParentId() !=null){ + Category parent = findById(category.getParentId()); + if(parent == null || Constants.equalsInteger(parent.getIsdeleted(),Constants.ONE)){ + throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "瀵逛笉璧凤紝鐖剁骇淇℃伅涓嶅瓨鍦▇"); + } + if(Constants.equalsInteger(parent.getType(),Constants.FOUR) && parent.getParentId()!=null){ + //濡傛灉鏄簩绾DA涓婚,鑷繁涓鸿瀵熼」 + category.setType(Constants.SIX); + } + idPath = parent.getIdPath(); + category.setIdPath(parent.getIdPath()+category.getId()+"/"); + category.setNamePath(parent.getNamePath()+"/"+category.getName()); + } + category.setCreateDate(new Date()); + category.setCreator(user.getId()); + category.setIsdeleted(Constants.ZERO); + category.setStatus(Constants.ZERO); + category.setEditDate(category.getCreateDate()); + category.setEditor(category.getCreator()); categoryMapper.insert(category); + + Category com = new Category(); + com.setId(category.getId()); + com.setIdPath(idPath+"/"+category.getId()+"/"); + categoryMapper.updateById(com); return category.getId(); + } @Override public void deleteById(Integer id) { - categoryMapper.deleteById(id); + Category model = new Category(); + model.setId(id); + LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); + model.setEditor(user.getId()); + model.setEditDate(new Date()); + model.setIsdeleted(Constants.ONE); + categoryMapper.updateById(model); } @Override @@ -49,12 +128,67 @@ if (CollectionUtils.isEmpty(ids)) { return; } - categoryMapper.deleteBatchIds(ids); + for(Integer id :ids){ + deleteById(id); + } } @Override public void updateById(Category category) { - categoryMapper.updateById(category); + if( category.getId() == null){ + throw new BusinessException(ResponseStatus.BAD_REQUEST); + } + if(Constants.equalsInteger(category.getId(),category.getParentId())){ + throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝鐖剁骇涓嶈兘璁剧疆涓鸿嚜韬紒"); + } + category.setType(null);//绫诲瀷涓嶆敮鎸佷慨鏀� + Category model = categoryMapper.selectById(category.getId()); + if(model ==null || Constants.equalsInteger(model.getIsdeleted(),Constants.ONE)) { + throw new BusinessException(ResponseStatus.DATA_EMPTY); + } + /* //鏌ヨ鍚嶇О涓嶈兘閲嶅 + if(categoryMapper.selectCount(new QueryWrapper<Category>().lambda() + .eq(Category::getName,category.getName()) + .eq(Category::getType,model.getType()) + .ne(Category::getId,category.getId()) + .eq(Category::getIsdeleted,Constants.ZERO)) >0){ + throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "瀵逛笉璧凤紝鍚嶇О涓嶈兘閲嶅~"); + }*/ + String oldIdPath = model.getIdPath(); + String oldNamePath = model.getNamePath(); + LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); + category.setIdPath(category.getId()+"/");//鍚嶇О璺緞 + category.setNamePath(category.getName());//鍚嶇О璺緞 + if(category.getParentId() !=null ){ + //濡傛灉鐖剁骇鍙戠敓鏀瑰彉 + Category parent = findById(category.getParentId()); + if(parent == null || Constants.equalsInteger(parent.getIsdeleted(),Constants.ONE)){ + throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "瀵逛笉璧凤紝鐖剁骇淇℃伅涓嶅瓨鍦▇"); + } + category.setIdPath(parent.getIdPath()+category.getId()+"/"); + category.setNamePath(parent.getNamePath()+"/"+category.getName()); + } + category.setEditDate(new Date()); + category.setEditor(user.getId()); + categoryMapper.update(null,new UpdateWrapper<Category>().lambda() + .eq(Category::getId,category.getId()) + .set(Category::getEditDate,category.getEditDate()) + .set(Category::getEditor,category.getEditor()) + .set(Category::getName,category.getName()) + .set(Category::getParentId,category.getParentId()) + .set(Category::getSortnum,category.getSortnum()) + .set(Category::getRemark,category.getRemark()) + .set(Category::getNamePath,category.getNamePath()) + .set(Category::getIdPath,category.getId()) + ); + //淇敼鍏ㄩ儴涓嬬骇鐨勭紪鐮佸拰鍚嶇О璺緞 + categoryMapper.update(null,new UpdateWrapper<Category>().lambda() + .likeRight(Category::getIdPath,oldIdPath) + .set(Category::getEditDate,category.getEditDate()) + .set(Category::getEditor,category.getEditor()) + .setSql("id_path=replace(id_path,'"+oldIdPath+"','"+category.getIdParentPath()+"')") + .setSql("name_path=replace(name_path,'"+oldNamePath+"/','"+category.getName()+"/')") + ); } @Override @@ -80,17 +214,49 @@ @Override public List<Category> findList(Category category) { - QueryWrapper<Category> wrapper = new QueryWrapper<>(category); - return categoryMapper.selectList(wrapper); + category.setIsdeleted(Constants.ZERO); + MPJLambdaWrapper<Category> queryWrapper = new MPJLambdaWrapper<>(); + queryWrapper.selectAll(Category.class); + queryWrapper.selectAs(SystemUser::getUsername,Category::getEditorName); + queryWrapper.select("t1.name",Category::getParentName); + queryWrapper.select("t1.id_path",Category::getIdParentPath); + queryWrapper.leftJoin(Category.class,Category::getId,Category::getParentId); + queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,Category::getEditor); + queryWrapper.eq(category.getId() != null, Category::getId, category.getId()) + .eq(category.getCreator() != null, Category::getCreator, category.getCreator()) + .ge(category.getCreateDate() != null, Category::getCreateDate, Utils.Date.getStart(category.getCreateDate())) + .le(category.getCreateDate() != null, Category::getCreateDate, Utils.Date.getEnd(category.getCreateDate())) + .eq(category.getEditor() != null, Category::getEditor, category.getEditor()) + .ge(category.getEditDate() != null, Category::getEditDate, Utils.Date.getStart(category.getEditDate())) + .le(category.getEditDate() != null, Category::getEditDate, Utils.Date.getEnd(category.getEditDate())) + .eq(category.getIsdeleted() != null, Category::getIsdeleted, category.getIsdeleted()) + .like(category.getName() != null, Category::getName, category.getName()) + .eq(category.getRemark() != null, Category::getRemark, category.getRemark()) + .eq(category.getStatus() != null, Category::getStatus, category.getStatus()) + .eq(category.getSortnum() != null, Category::getSortnum, category.getSortnum()) + .eq(category.getImgurl() != null, Category::getImgurl, category.getImgurl()) + .eq(category.getType() != null, Category::getType, category.getType()) + .eq(category.getParentId() != null, Category::getParentId, category.getParentId()) + .eq(category.getNamePath() != null, Category::getNamePath, category.getNamePath()) + .isNull(category.getIsRoot() != null &&category.getIsRoot().equals(Constants.ONE), Category::getParentId) + .eq(category.getIdPath() != null, Category::getIdPath, category.getIdPath()) + .orderByAsc( Category::getSortnum); + return categoryMapper.selectJoinList(Category.class,queryWrapper); } @Override public PageData<Category> findPage(PageWrap<Category> pageWrap) { IPage<Category> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); - QueryWrapper<Category> queryWrapper = new QueryWrapper<>(); Utils.MP.blankToNull(pageWrap.getModel()); - queryWrapper.lambda() - .eq(pageWrap.getModel().getId() != null, Category::getId, pageWrap.getModel().getId()) + pageWrap.getModel().setIsdeleted(Constants.ZERO); + MPJLambdaWrapper<Category> queryWrapper = new MPJLambdaWrapper<>(); + queryWrapper.selectAll(Category.class); + queryWrapper.selectAs(SystemUser::getUsername,Category::getEditorName); + queryWrapper.select("t1.name",Category::getParentName); + queryWrapper.select("t1.id_path",Category::getIdParentPath); + queryWrapper.leftJoin(Category.class,Category::getId,Category::getParentId); + queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,Category::getEditor); + queryWrapper.eq(pageWrap.getModel().getId() != null, Category::getId, pageWrap.getModel().getId()) .eq(pageWrap.getModel().getCreator() != null, Category::getCreator, pageWrap.getModel().getCreator()) .ge(pageWrap.getModel().getCreateDate() != null, Category::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate())) .le(pageWrap.getModel().getCreateDate() != null, Category::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate())) @@ -98,24 +264,18 @@ .ge(pageWrap.getModel().getEditDate() != null, Category::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate())) .le(pageWrap.getModel().getEditDate() != null, Category::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate())) .eq(pageWrap.getModel().getIsdeleted() != null, Category::getIsdeleted, pageWrap.getModel().getIsdeleted()) - .eq(pageWrap.getModel().getName() != null, Category::getName, pageWrap.getModel().getName()) + .like(pageWrap.getModel().getName() != null, Category::getName, pageWrap.getModel().getName()) .eq(pageWrap.getModel().getRemark() != null, Category::getRemark, pageWrap.getModel().getRemark()) .eq(pageWrap.getModel().getStatus() != null, Category::getStatus, pageWrap.getModel().getStatus()) .eq(pageWrap.getModel().getSortnum() != null, Category::getSortnum, pageWrap.getModel().getSortnum()) .eq(pageWrap.getModel().getImgurl() != null, Category::getImgurl, pageWrap.getModel().getImgurl()) .eq(pageWrap.getModel().getType() != null, Category::getType, pageWrap.getModel().getType()) .eq(pageWrap.getModel().getParentId() != null, Category::getParentId, pageWrap.getModel().getParentId()) + .isNull(pageWrap.getModel().getIsRoot() != null && pageWrap.getModel().getIsRoot().equals(Constants.ONE), Category::getParentId) .eq(pageWrap.getModel().getNamePath() != null, Category::getNamePath, pageWrap.getModel().getNamePath()) .eq(pageWrap.getModel().getIdPath() != null, Category::getIdPath, pageWrap.getModel().getIdPath()) - ; - for(PageWrap.SortData sortData: pageWrap.getSorts()) { - if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) { - queryWrapper.orderByDesc(sortData.getProperty()); - } else { - queryWrapper.orderByAsc(sortData.getProperty()); - } - } - return PageData.from(categoryMapper.selectPage(page, queryWrapper)); + .orderByAsc( Category::getSortnum); + return PageData.from(categoryMapper.selectJoinPage(page,Category.class, queryWrapper)); } @Override @@ -123,4 +283,594 @@ QueryWrapper<Category> wrapper = new QueryWrapper<>(category); return categoryMapper.selectCount(wrapper); } + + @Override + public List<Category> treeList(Category param ) { + MPJLambdaWrapper<Category> queryWrapper = new MPJLambdaWrapper<>(); + queryWrapper.selectAll(Category.class); + queryWrapper.selectAs(SystemUser::getUsername,Category::getEditorName); + queryWrapper.select("t1.name",Category::getParentName); + queryWrapper.select("t1.id_path",Category::getIdParentPath); + queryWrapper.leftJoin(Category.class,Category::getId,Category::getParentId); + queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,Category::getEditor); + queryWrapper.eq(Category::getIsdeleted, Constants.ZERO); + queryWrapper.eq(Category::getType, param.getType()); + queryWrapper.orderByAsc( Category::getSortnum); + List<Category> companyList = categoryMapper.selectJoinList(Category.class,queryWrapper); + CategoryTree treeBuild = new CategoryTree(companyList); + companyList = treeBuild.buildTree(); + return companyList; + } + + + + + + @Override + public List<CategoryVO> getCategoryVOTree(String categoryType,Integer categoryId){ + List<Category> categoryList = categoryMapper.selectList(new QueryWrapper<Category>().lambda() + .eq(Category::getIsdeleted, Constants.ZERO) + .in(Objects.nonNull(categoryType),Category::getType,categoryType.split(",")) + .apply(Objects.nonNull(categoryId)," find_in_set("+categoryId+", REPLACE(ID_PATH,'/',',') ) ") + .orderByAsc(Category::getSortnum,Category::getId) + ); + List<CategoryVO> categoryVOList = new ArrayList<>(); + if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(categoryList)){ + for (Category category:categoryList) { + CategoryVO categoryVO = new CategoryVO(); + categoryVO.setId(category.getId()); + categoryVO.setName(category.getName()); + categoryVO.setParentId(category.getParentId()); + categoryVOList.add(categoryVO); + } + CategoryVOTree treeBuild = new CategoryVOTree(categoryVOList); + categoryVOList = treeBuild.buildTree(); + } + return categoryVOList; + } + + + @Override + public List<CategoryVO> getCategoryVOForGCXTree(Integer categoryId){ + List<Category> categoryList = categoryMapper.selectList(new QueryWrapper<Category>().lambda() + .eq(Category::getIsdeleted, Constants.ZERO) + .eq(Category::getType, Constants.FOUR) + .and( ms->ms.eq(Category::getId,categoryId).or().eq(Category::getParentId,categoryId)) + .orderByAsc(Category::getSortnum,Category::getId) + ); + if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(categoryList)){ + categoryList.addAll( + categoryMapper.selectList(new QueryWrapper<Category>().lambda() + .eq(Category::getIsdeleted, Constants.ZERO) + .eq(Category::getType, Constants.SIX) + .in(Category::getParentId,categoryList.stream().map(i->i.getId()).collect(Collectors.toList())) + .orderByAsc(Category::getSortnum,Category::getId)) + ); + } + List<CategoryVO> categoryVOList = new ArrayList<>(); + if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(categoryList)){ + for (Category category:categoryList) { + CategoryVO categoryVO = new CategoryVO(); + categoryVO.setId(category.getId()); + categoryVO.setName(category.getName()); + categoryVO.setParentId(category.getParentId()); + categoryVOList.add(categoryVO); + } + CategoryVOTree treeBuild = new CategoryVOTree(categoryVOList); + categoryVOList = treeBuild.buildTree(); + } + return categoryVOList; + } + + + + + @Override + public List<CategoryVO> getCategoryVOList(Integer categoryType,Integer isRoot){ + List<Category> categoryList = categoryMapper.selectList(new QueryWrapper<Category>().lambda() + .eq(Category::getIsdeleted, Constants.ZERO) + .eq(Objects.nonNull(categoryType),Category::getType,categoryType) + .isNull(Constants.equalsInteger(isRoot,Constants.ONE),Category::getParentId) + .orderByAsc(Category::getSortnum,Category::getId) + ); + List<CategoryVO> categoryVOList = new ArrayList<>(); + if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(categoryList)){ + for (Category category:categoryList) { + CategoryVO categoryVO = new CategoryVO(); + categoryVO.setId(category.getId()); + categoryVO.setName(category.getName()); + categoryVO.setParentId(category.getParentId()); + categoryVOList.add(categoryVO); + } + } + return categoryVOList; + } + public List<Category> findAllListForDca() { + List<Category> categoryList = categoryMapper.selectList(new QueryWrapper<Category>().lambda() + .eq(Category::getIsdeleted, Constants.ZERO) + .in(Category::getType, Constants.FOUR, Constants.SIX)//涓婚鍜岃瀵熼」 + .orderByAsc(Category::getSortnum) + ); + return categoryList; + } + public List<Category> findListForDcaTree( List<Category> categoryList) { + if (com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(categoryList)) { + long index = 1; + List<Category> pList = new ArrayList<>(); + for (Category category : categoryList) { + if (category.getParentId() == null) { + category.setChildList(new ArrayList<>()); + for (Category cc : categoryList) { + if (Constants.equalsInteger(cc.getParentId(), category.getId())) { + cc.setChildList(new ArrayList<>()); + for (Category ccc : categoryList) { + if (Constants.equalsInteger(ccc.getParentId(), cc.getId())) { + cc.getChildList().add(ccc); + } + } + category.getChildList().add(cc); + } + } + pList.add(category); + } + } + return pList; + } + return null; + } + public List<Category> findImportTreeForDca ( List<CategoryDcaProblemDto> categoryList,LoginUserInfo user, Date date) { + List<Category> pList = new ArrayList<>(); + if (com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(categoryList)) { + Integer index = 1; + for (CategoryDcaProblemDto categoryDcaProblemDto : categoryList) { + if(StringUtils.isBlank(categoryDcaProblemDto.getProblem()) + &&StringUtils.isBlank(categoryDcaProblemDto.getParentName()) + &&StringUtils.isBlank(categoryDcaProblemDto.getTypeName()) ){ + //鍘荤┖琛� + continue; + } + if(StringUtils.isBlank(categoryDcaProblemDto.getParentName())){ + throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝琛ㄦ牸绗��"+(index + 1)+"銆戣涓�绾т富棰樹俊鎭笉鑳戒负绌猴紝璇疯繑鍥炴鏌ヨ〃鏍间俊鎭紒"); + } + categoryDcaProblemDto.setProblem(StringUtils.defaultString(categoryDcaProblemDto.getProblem(),"").trim()); + categoryDcaProblemDto.setTypeName(StringUtils.defaultString(categoryDcaProblemDto.getTypeName(),"").trim()); + categoryDcaProblemDto.setParentName(StringUtils.defaultString(categoryDcaProblemDto.getParentName(),"").trim()); + Category first = getNewCateFromListByName(categoryDcaProblemDto.getParentName(), pList); + if(first == null){ + first = new Category(); + first.setIsdeleted(Constants.ZERO); + first.setCreateDate(date); + first.setCreator(user.getId()); + first.setIsdeleted(Constants.ZERO); + first.setStatus(Constants.ZERO); + first.setEditDate(date); + first.setEditor(user.getId()); + first.setName(categoryDcaProblemDto.getParentName()); + first.setType(Constants.FOUR);//涓婚 + first.setSortnum(index++); + first.setChildList(new ArrayList<>()); + pList.add(first); + List<Category> childList = getSecondListFromImport(first,0,categoryList); + first.setChildList(childList); + } + } + } + return pList; + } + + private List<Category> getSecondListFromImport(Category parent,int type, List<CategoryDcaProblemDto> categoryList) { + List<Category> pList = new ArrayList<>(); + Integer index = 1; + for (CategoryDcaProblemDto categoryDcaProblemDto : categoryList) { + if(type ==0 && (StringUtils.isBlank(categoryDcaProblemDto.getTypeName()) || StringUtils.isBlank(categoryDcaProblemDto.getProblem()))){ + //鍘荤┖琛� + continue; + } + if(type ==1 &&(StringUtils.isBlank(categoryDcaProblemDto.getProblem()))){ + //鍘荤┖琛� + continue; + } + if(type ==0 && StringUtils.isBlank(categoryDcaProblemDto.getTypeName())){ + throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝琛ㄦ牸绗��"+(index + 1)+"銆戣浜岀骇涓婚淇℃伅涓嶈兘涓虹┖锛岃杩斿洖妫�鏌ヨ〃鏍间俊鎭紒"); + } + if(type == 0 && !StringUtils.equals(categoryDcaProblemDto.getParentName(),parent.getName())){ + //鍙鐞嗗瓙绾� + continue; + } + if(type == 1 && !StringUtils.equals(categoryDcaProblemDto.getTypeName(),parent.getName())){ + //鍙鐞嗗瓙绾� + continue; + } + Category model = getNewCateFromListByName(type == 0 ?categoryDcaProblemDto.getTypeName():categoryDcaProblemDto.getProblem(), pList); + if(model == null){ + model = new Category(); + model.setIsdeleted(Constants.ZERO); + model.setCreateDate(parent.getCreateDate()); + model.setCreator(parent.getCreator()); + model.setIsdeleted(Constants.ZERO); + model.setStatus(Constants.ZERO); + model.setEditDate(parent.getCreateDate()); + model.setEditor(parent.getCreator()); + model.setName(type == 0 ?categoryDcaProblemDto.getTypeName():categoryDcaProblemDto.getProblem()); + model.setType(type == 0 ? Constants.FOUR:Constants.SIX);// + model.setSortnum(index++); + model.setChildList(new ArrayList<>()); + pList.add(model); + } + if(type == 0){ + //濡傛灉鏄簩绾т富棰樺鐞嗚幏鍙栧瓙闆嗛儴闂� + List<Category> childList = getSecondListFromImport(model,1,categoryList); + model.setChildList(childList); + } + } + return pList; + } + + private Category getNewCateFromListByName(String parentName, List<Category> pList) { + if(pList ==null){ + return null; + } + for(Category cate : pList){ + if(StringUtils.equals(parentName,cate.getName())){ + return cate; + } + } + return null; + } + private Category getNewCateFromListByNameAndParent(String parentName,int type,boolean isRoot, List<Category> pList) { + for(Category cate : pList){ + if(StringUtils.equals(parentName,cate.getName()) && Constants.equalsInteger(cate.getType(),type)){ + if(isRoot && cate.getParentId()==null){ + return cate; + }else if(isRoot && cate.getParentId()==null){ + return cate; + } + } + } + return null; + } + + @Override + public List<CategoryDcaProblemDto> findListForDca(Category param){ + long index =1; + List<CategoryDcaProblemDto> categoryVOList = new ArrayList<>(); + List<Category> pList = findListForDcaTree( findAllListForDca()); + for (Category category:pList) { + if(category.getChildList().size() ==0){ + CategoryDcaProblemDto categoryVO = new CategoryDcaProblemDto(); + categoryVO.setIndex(index); + categoryVO.setParentName(category.getName()); + index++; + categoryVOList.add(categoryVO); + }else{ + for (Category cc:category.getChildList()) { + if(cc.getChildList().size() ==0){ + CategoryDcaProblemDto categoryVO = new CategoryDcaProblemDto(); + categoryVO.setIndex(index); + categoryVO.setParentName(category.getName()); + categoryVO.setTypeName(cc.getName()); + index++; + categoryVOList.add(categoryVO); + }else{ + for (Category ccc:cc.getChildList()) { + CategoryDcaProblemDto categoryVO = new CategoryDcaProblemDto(); + categoryVO.setIndex(index); + categoryVO.setParentName(category.getName()); + categoryVO.setTypeName(cc.getName()); + categoryVO.setProblem(ccc.getName()); + index++; + categoryVOList.add(categoryVO); + + } + } + } + } + } + return categoryVOList; + } + + + @Override + @PostConstruct + public String initRedis(){ + redisTemplate.delete(Constants.RedisKeys.IMPORTING_CATEGORY); + return ""; + } + @Override + @Transactional(rollbackFor = {BusinessException.class,Exception.class}) + public String importDcaBatch(MultipartFile file){ + Boolean importing = (Boolean) redisTemplate.opsForValue().get(Constants.RedisKeys.IMPORTING_CATEGORY); + if(importing!=null && importing){ + throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝宸插瓨鍦ㄥ憳宸ュ鍏ヤ换鍔℃鍦ㄦ墽琛屼腑锛岃绋嶅悗鍐嶈瘯锛�"); + } + redisTemplate.opsForValue().set(Constants.RedisKeys.IMPORTING_CATEGORY,true); + try { + ExcelImporter ie = null; + List<CategoryDcaProblemDto> dataList =null; + try { + ie = new ExcelImporter(file,0,0, CellType.STRING); // 纭繚鍗曞厓鏍肩被鍨嬩负瀛楃涓�); + dataList = ie.getDataList(CategoryDcaProblemDto.class,null); + } catch (Exception e) { + e.printStackTrace(); + } + if(dataList == null || dataList.size() ==0){ + throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝褰曞叆鏁版嵁涓虹┖锛�"); + } + Date date = new Date(); + LoginUserInfo user = (LoginUserInfo)SecurityUtils.getSubject().getPrincipal(); + List<Category> treeImportList = findImportTreeForDca(dataList,user,date);//鏌ヨ鐜版湁鐨勫叏閮ㄤ富棰樿瀵熼」鏁版嵁 + if(treeImportList == null || treeImportList.size() ==0){ + throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝褰曞叆鐨勬湁鏁堟暟鎹负绌猴紒"); + } + //鏌ヨ鐜版湁鐨勫叏閮ㄤ富棰樿瀵熼」鏁版嵁 + List<Category> list = categoryMapper.selectJoinList(Category.class,new MPJLambdaWrapper<Category>() + .selectAll(Category.class) +// .select("t1.name",Category::getParentName) +// .select("t2.name",Category::getRootName) +// .leftJoin(Category.class,Category::getId,Category::getParentId) +// .leftJoin( "category t2 on t1.parent_id=t2.id") + .eq(Category::getIsdeleted, Constants.ZERO) + .in(Category::getType, Constants.FOUR, Constants.SIX)//涓婚鍜岃瀵熼」 + .orderByAsc(Category::getSortnum) + ) ; + List<Category> treeList = findListForDcaTree(list);//鏌ヨ鐜版湁鐨勫叏閮ㄤ富棰樿瀵熼」鏁版嵁 + List<Integer> allUpdateIds = new ArrayList<>(); + dealImportNewOrUpdateBiz(treeList,treeImportList,allUpdateIds); + dealDeletedList(list,allUpdateIds,user,date);//澶勭悊鍒犻櫎鐨勬暟鎹俊鎭� + dealTreePathInfo(treeImportList); + return "瀵煎叆鎴愬姛"; + }catch (BusinessException e){ + throw e; + }catch (Exception e){ + throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"鍛樺伐淇℃伅瀵煎叆澶辫触锛岃绋嶅悗閲嶈瘯"); + }finally { + redisTemplate.delete(Constants.RedisKeys.IMPORTING_CATEGORY); + } + } + + private void dealImportNewOrUpdateBiz(List<Category> treeList, List<Category> treeImportList, List<Integer> allUpdateIds) { + List<Category> newList = new ArrayList<>(); + List<Category> updateList = new ArrayList<>(); + for(Category tmodel :treeImportList){ + + //澶勭悊涓�绾т富棰樺鏀归�昏緫 + //鏌ヨ鏄惁瀛樺湪鍚嶇О鐩稿悓鐨勪竴绾т富棰橈紝瀛樺湪鍒欐洿鏂帮紝涓嶅瓨鍦ㄥ垯鏂板 + Category first = getNewCateFromListByName(tmodel.getName(),treeList); + if(first!=null){ + tmodel.setId(first.getId()); + tmodel.setChildMatchList(first.getChildList()); + allUpdateIds.add(first.getId()); + updateList.add(tmodel); + }else{ + newList.add(tmodel); + } + } + + //鍏堟壒閲忓鐞嗕竴绾т富棰樼殑澧炴敼鏁版嵁锛屽埛鏂颁竴绾т富棰樺璞$殑缂栫爜 + if(newList.size() >0){ + categoryMapper.insert(newList); + } + if( updateList.size() >0){ + for(Category update :updateList){ + categoryMapper.updateById(update); + } + } + dealSecChildList(newList,updateList,allUpdateIds,1);//澶勭悊浜岀骇鏁版嵁 + } + + private void dealDeletedList(List<Category> list, List<Integer> allUpdateIds, LoginUserInfo user, Date date) { + List<Integer> deleteList = new ArrayList<>(); + for(Category cate : list){ + for(Integer nowId : allUpdateIds){ + if(Constants.equalsInteger(cate.getId(),nowId)){ + continue; + } + deleteList.add(cate.getId()); + } + } + if(deleteList.size() >0){ + //鍒犻櫎涓嶅瓨鍦ㄧ殑 + categoryMapper.update(null,new UpdateWrapper<Category>().lambda() + .set(Category::getIsdeleted,Constants.ONE) + .set(Category::getEditor,user.getId()) + .set(Category::getEditDate,date) + .in(Category::getId,deleteList)); + } + } + + private void dealSecChildList(List<Category> newList, List<Category> updateList,List<Integer> allUpdateIds,int level) { + List<Category> childNewList = new ArrayList<>(); + List<Category> childUpdateList = new ArrayList<>(); + if(newList.size() >0 || updateList.size()>0) { + for(Category c : newList){ + if(c.getChildList()!=null && c.getChildList().size()>0){ + for(Category sec : c.getChildList()){ + sec.setParentId(c.getId()); + } + childNewList.addAll(c.getChildList()); + } + } + for(Category c : updateList){ + if(c.getChildList()!=null && c.getChildList().size()>0){ + List<Category> tList = c.getChildMatchList(); + for(Category sec : c.getChildList()){ + sec.setParentId(c.getId()); + Category mmodel = getNewCateFromListByName(sec.getName(),tList); + if(mmodel!=null){ + sec.setId(mmodel.getId()); + sec.setChildMatchList(mmodel.getChildList()); + allUpdateIds.add(mmodel.getId()); + childUpdateList.add(sec); + }else{ + childNewList.add(sec); + } + } + } + } + } + if(childNewList.size() >0){ + categoryMapper.insert(childNewList); + } + if( childUpdateList.size() >0){ + for(Category update :childUpdateList){ + categoryMapper.updateById(update); + } + } + if(level == 1){ + dealSecChildList(childNewList,childUpdateList,allUpdateIds,2);//澶勭悊涓夌骇绾ф暟鎹� + } + } + private void dealTreePathInfo(List<Category> tree) { + if(tree!=null || tree.size()>0){ + for(Category t : tree){ + t.setIdPath(t.getId()+"/"); + t.setNamePath(t.getName()); + categoryMapper.updateById(t); + dealChildParentId(t,t.getChildList()); + } + } + } + + private void dealChildParentId(Category t, List<Category> childList) { + if(childList ==null || childList.size()==0){ + return; + } + for(Category c : childList){ + c.setParentId(t.getId()); + c.setIdPath(t.getIdPath()+c.getId()+"/"); + c.setNamePath(t.getNamePath()+"/"+c.getName()); + categoryMapper.updateById(c); + if(t.getChildList()!=null || t.getChildList().size()==0){ + //閫掑綊澶勭悊瀛愰泦 + dealChildParentId(c,c.getChildList()); + } + } + } + + + + @Override + public void dealjson(){ + String json = ""; + BufferedReader reader = null; + try { + reader = new BufferedReader(new FileReader("d://file.txt")); + String line; + while ((line = reader.readLine()) != null) { + json = json + line; + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (reader != null){ + reader.close(); + }; + } catch (IOException e) { + e.printStackTrace(); + } + } + JSONObject jsonObject = JSONObject.parseObject(json); + JSONArray jsonArray = jsonObject.getJSONArray("data"); + + List<Map<String,Object>> mapList = new ArrayList<>(); + for (int i = 0; i < jsonArray.size(); i++) { + JSONObject jsons = jsonArray.getJSONObject(i); + Map<String,Object> map = new HashMap<>(); + map.put("id",jsons.getString("id")); + map.put("level",jsons.getInteger("level")); + map.put("name",jsons.getString("name")); + map.put("parentId",jsons.getString("parentId")); + mapList.add(map); + } + CategoryMapTree categoryMapTree = new CategoryMapTree(mapList); + mapList = categoryMapTree.buildTree(); + + for (Map<String,Object> map:mapList) { + Category category = new Category(); + category.setCreateDate(new Date()); + category.setIsdeleted(Constants.ZERO); + category.setStatus(Constants.ZERO); + category.setType(Constants.SIX); + category.setName(map.get("name").toString()); + category.setNamePath(map.get("name").toString()); + categoryMapper.insert(category); + if(Objects.nonNull(map.get("childTree"))){ + List<Map<String,Object>> childOneMap = (List<Map<String, Object>>) map.get("childTree"); + for (Map<String,Object> oneMap:childOneMap) { + Category oneCategory = new Category(); + oneCategory.setCreateDate(new Date()); + oneCategory.setIsdeleted(Constants.ZERO); + oneCategory.setStatus(Constants.ZERO); + oneCategory.setType(Constants.SIX); + oneCategory.setName(oneMap.get("name").toString()); + oneCategory.setNamePath(category.getNamePath()+"/"+oneCategory.getName()); + oneCategory.setParentId(category.getId()); + categoryMapper.insert(oneCategory); + + if(Objects.nonNull(oneMap.get("childTree"))){ + List<Map<String,Object>> childTwoMap = (List<Map<String, Object>>) oneMap.get("childTree"); + for (Map<String,Object> twoMap:childTwoMap) { + Category twoCategory = new Category(); + twoCategory.setCreateDate(new Date()); + twoCategory.setIsdeleted(Constants.ZERO); + twoCategory.setStatus(Constants.ZERO); + twoCategory.setType(Constants.SIX); + twoCategory.setName(twoMap.get("name").toString()); + twoCategory.setNamePath(oneCategory.getNamePath() + "/" + twoCategory.getName()); + twoCategory.setParentId(oneCategory.getId()); + categoryMapper.insert(twoCategory); + } + } + } + + + + + + + } + + } + + + + +// for (int i = 0; i < jsonArray.size(); i++) { +// JSONObject jsonObject1 = jsonArray.getJSONObject(i); +// if(Constants.equalsInteger(jsonObject1.getInteger("level"),Constants.ONE)){ +// Boolean iHavaChild = false; +// for (int k = 0; i < jsonArray.size(); i++) { +// JSONObject jsonObject2 = jsonArray.getJSONObject(k); +// +// Boolean kHavaChild = false; +// if(Constants.equalsInteger(jsonObject2.getInteger("level"),Constants.TWO) +// &&jsonObject2.getString("parentId").equals(jsonObject1.getString("id"))){ +// iHavaChild = true; +// +// for (int g = 0; i < jsonArray.size(); i++) { +// JSONObject jsonObject3 = jsonArray.getJSONObject(g); +// if(Constants.equalsInteger(jsonObject3.getInteger("level"),Constants.THREE) +// &&jsonObject3.getString("parentId").equals(jsonObject2.getString("id"))) { +// kHavaChild = true; +// +// System.out.println(j.getString("id")+"---"+j.getString("name").replace("\n","")+"---"+j.getInteger("level")+"---"+j.getString("parentId")); +// } +// } +// +// } +// } +// if(!iHavaChild){ +// +// } +// } +// +// +//// System.out.println(j.getString("id")+"---"+j.getString("name").replace("\n","")+"---"+j.getInteger("level")+"---"+j.getString("parentId")); +// } + + + + } + } -- Gitblit v1.9.3