package com.farriver.bwf.service.question; import com.farriver.bwf.common.model.ApiData; import com.farriver.bwf.common.model.ApiPageData; import com.farriver.bwf.common.options.ActionType; import com.farriver.bwf.common.options.MapDataType; import com.farriver.bwf.common.statics.LangConstants; import com.farriver.bwf.common.utilities.ListUtils; import com.farriver.bwf.data.master.mapper.QuestionPaperCategoryMapper; import com.farriver.bwf.data.master.model.QuestionPaperCategory; import com.farriver.bwf.data.master.model.QuestionPaperCategoryExample; import com.farriver.bwf.data.transferobject.queryobject.question.QuestionPaperCategoryQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.question.QuestionPaperCategoryViewModel; import com.farriver.bwf.service.ServiceBase; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import jakarta.annotation.Resource; import java.util.*; import java.util.stream.Collectors; @Service @CacheConfig(cacheNames = "bwf_QuestionPaperCategory_cache") public class QuestionPaperCategoryService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(QuestionPaperCategoryService.class); @Resource QuestionPaperCategoryMapper mapper; private String message; public Map GetList(QuestionPaperCategoryQueryObject queryObject) { if (queryObject == null) return null; QuestionPaperCategoryExample example = new QuestionPaperCategoryExample(); QuestionPaperCategoryExample.Criteria criteria = example.createCriteria(); //查询条件 if (queryObject.getFilter() != null && !queryObject.getFilter().isEmpty()) { criteria.andNameLike("%" + queryObject.getFilter() + "%"); } else { if (queryObject.getId() != null && !queryObject.getId().isEmpty()) { criteria.andIdEqualTo(queryObject.getId()); } if (queryObject.getName() != null && !queryObject.getName().isEmpty()) { criteria.andNameLike("%" + queryObject.getName() + "%"); } if (queryObject.getStatus() != null && queryObject.getStatus() >= 0) { criteria.andStatusEqualTo(queryObject.getStatus()); } if (queryObject.getStatuslist() != null && queryObject.getStatuslist().size() > 0) { criteria.andStatusIn(queryObject.getStatuslist()); } if (queryObject.getStartDateTime() != null && queryObject.getEndDateTime() != null) { criteria.andCreatetimeBetween(queryObject.getStartDateTime(), queryObject.getEndDateTime()); } } //查询总数 long total = mapper.countByExample(example); //分页 int pageIndex = queryObject.getPageIndex(); int pageSize = queryObject.getPageSize(); if (pageIndex > 0 && pageSize > 0) { example.setRows(pageSize); example.setOffset((pageIndex - 1) * pageSize); } List list = mapper.selectByExample(example); Map map = new HashMap<>(); map.put(MapDataType.DATA_LIST, list); map.put(MapDataType.TOTAL_COUNT, total); map.put(MapDataType.PAGE_INDEX, pageIndex); map.put(MapDataType.PAGE_SIZE, pageSize); return map; } public QuestionPaperCategory GetDetail(QuestionPaperCategoryQueryObject queryObject) { Map map = GetList(queryObject); Object object = map.get(MapDataType.DATA_LIST); if (object != null) { List list = (List) object; if (list != null && list.size() == 1) return list.get(0); } return null; } @CacheEvict(allEntries = true) public ApiData Create(QuestionPaperCategoryViewModel model) { QuestionPaperCategory entity = Check(ActionType.CREATE, model); if (entity != null) { return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, mapper.insertSelective(entity)); } else { return ApiData.error(message); } } @CacheEvict(allEntries = true) public ApiData Update(QuestionPaperCategoryViewModel model) { QuestionPaperCategory entity = Check(ActionType.UPDATE, model); if (entity != null) { return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, mapper.updateByPrimaryKeySelective(entity)); } else { return ApiData.error(message); } } @CacheEvict(allEntries = true) public ApiData Delete(String id) { return ApiData.ok("", mapper.deleteByPrimaryKey(id)); } @Transactional @CacheEvict(allEntries = true) public ApiData BatchDelete(QuestionPaperCategoryQueryObject queryObject) { Map map = GetList(queryObject); Object object = map.get(MapDataType.DATA_LIST); int count = 0; if (object != null) { List list = (List) object; if (list != null && list.size() == 1) { count += mapper.deleteByPrimaryKey(list.get(0).getId()); } } return ApiData.ok("", count); } @Cacheable public QuestionPaperCategoryViewModel GetViewDetail(QuestionPaperCategoryQueryObject queryObject) { QuestionPaperCategory entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public ApiPageData GetViewPageList(QuestionPaperCategoryQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (QuestionPaperCategory entity : list) { QuestionPaperCategoryViewModel viewModel = BuildViewModel(entity); viewModels.add(viewModel); } ApiPageData bean = ApiPageData.build(); bean.setData(viewModels); bean.setTotal((Long) map.get(MapDataType.TOTAL_COUNT)); return bean; } public List getQuestionPaperCategoryTree(QuestionPaperCategoryQueryObject queryObject) { List reusltQuestionPaperCategorys = new ArrayList<>(); List roots = new ArrayList<>(); //系统所有菜单 List QuestionPaperCategorys = (List) GetViewPageList(queryObject).getData(); for (QuestionPaperCategoryViewModel QuestionPaperCategory : QuestionPaperCategorys) { if (QuestionPaperCategory.getParentid() == null || QuestionPaperCategory.getParentid().isEmpty()) { roots.add(QuestionPaperCategory); } } for (QuestionPaperCategoryViewModel root : roots) { root = BuildTree(root, QuestionPaperCategorys); reusltQuestionPaperCategorys.add(root); } Collections.sort(reusltQuestionPaperCategorys, (QuestionPaperCategoryViewModel o1, QuestionPaperCategoryViewModel o2) -> { if (o2.getStatus() == null || o1.getStatus() == null) return o2.getName().compareTo(o1.getName()); int levelC = o2.getStatus() - o1.getStatus(); if (levelC == 0) return o2.getName().compareTo(o1.getName()); else return levelC; }); return reusltQuestionPaperCategorys; } @Cacheable public ApiPageData GetViewPageListWithChildren(QuestionPaperCategoryQueryObject queryObject) { if (queryObject == null) return null; //目的获取全部数据 QuestionPaperCategoryQueryObject queryObject2 = new QuestionPaperCategoryQueryObject(); queryObject2.setPageIndex(-1); queryObject2.setPageSize(-1); //查询 Map map = GetList(queryObject2); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); if (list != null && list.size() > 0) { for (QuestionPaperCategory entity : list) { QuestionPaperCategoryViewModel viewModel = BuildViewModel(entity); viewModels.add(viewModel); } } viewModels = FormateQuestionPaperCategoryParent(viewModels); List temps = viewModels; //查询条件 if (queryObject.getId() != null && !queryObject.getId().isEmpty()) { viewModels = viewModels.stream() .filter(e -> e.getId() != null) .filter(e -> e.getId().contains(queryObject.getId())) .collect(Collectors.toList()); } if (queryObject.getParentid() != null && !queryObject.getParentid().isEmpty()) { viewModels = viewModels.stream() .filter(e -> e.getParentid() != null) .filter(e -> e.getParentid().contains(queryObject.getParentid())) .collect(Collectors.toList()); } if (queryObject.getStatus() != null && queryObject.getStatus() >= 0) { viewModels = viewModels.stream() .filter(e -> e.getStatus() != null) .filter(e -> Objects.equals(e.getStatus(), queryObject.getStatus())) .collect(Collectors.toList()); } if (queryObject.getName() != null && !queryObject.getName().isEmpty()) { viewModels = viewModels.stream() .filter(e -> e.getName() != null) .filter(e -> e.getName().contains(queryObject.getName())) .collect(Collectors.toList()); } if (queryObject.getStartDateTime() != null && queryObject.getEndDateTime() != null) { viewModels = viewModels.stream() .filter(e -> e.getCreatetime().before(queryObject.getStartDateTime()) && e.getCreatetime().after(queryObject.getEndDateTime())) .collect(Collectors.toList()); } //获取筛查后没有取到的父级元素组 List temps2 = new ArrayList<>(); List roots1 = new ArrayList<>(); for (QuestionPaperCategoryViewModel model : viewModels) { if (model != null) { List parents = GetParent(model, temps, roots1); for (QuestionPaperCategoryViewModel parent : parents) { if (viewModels.stream().filter(e -> e.getId().equals(parent.getId())).count() <= 0) { temps2.add(parent); } } } } //将父级元素组添加到结果集合中 for (QuestionPaperCategoryViewModel model : temps2) { if (viewModels.stream().filter(e -> e.getId().equals(model.getId())).count() <= 0) { viewModels.add(model); } } //通过递归构造树形结构 List roots = new ArrayList<>(); for (QuestionPaperCategoryViewModel model : viewModels) { if (model.getParentid() == null || model.getParentid().isEmpty()) { model = BuildTree(model, viewModels); String id = model.getId(); if (roots.stream().filter(e -> e.getId().equals(id)).count() <= 0) { roots.add(model); } } } List pagelist = ListUtils.pagination(roots, queryObject.getPageIndex(), queryObject.getPageSize()); //构造结果 ApiPageData bean = ApiPageData.build(); bean.setData(pagelist); bean.setTotal(roots.size()); return bean; } //private methods private static QuestionPaperCategoryViewModel BuildViewModel(QuestionPaperCategory entity) { if (entity == null) return null; QuestionPaperCategoryViewModel model = new QuestionPaperCategoryViewModel(); model.setId(entity.getId()); model.setStatus(entity.getStatus()); model.setName(entity.getName()); model.setIsdeleted(entity.getIsdeleted()); model.setRemark(entity.getRemark()); model.setParentid(entity.getParentid()); model.setCreatetime(entity.getCreatetime()); model.setUpdatetime(entity.getUpdatetime()); model.setIcon(entity.getIcon()); return model; } public QuestionPaperCategory Check(ActionType actionType, QuestionPaperCategoryViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } QuestionPaperCategory entity = null; if (ActionType.CREATE == actionType) { if (model.getName() != null && !model.getName().isEmpty()) { QuestionPaperCategoryQueryObject queryObject2 = new QuestionPaperCategoryQueryObject(); queryObject2.setName(model.getName()); Map map2 = GetList(queryObject2); Object object2 = map2.get(MapDataType.DATA_LIST); if (object2 != null) { List list = (List) object2; if (list != null && list.size() > 0) { message = LangConstants.MSG_ERROR_NAME_EXISTED; return null; } } } entity = new QuestionPaperCategory(); entity.setId(UUID.randomUUID().toString()); entity.setCreatetime(new Date()); entity.setIsdeleted(false); } else { QuestionPaperCategoryQueryObject queryObject = new QuestionPaperCategoryQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); entity.setUpdatetime(new Date()); if (model.getName() != null && !model.getName().isEmpty()) { QuestionPaperCategoryQueryObject queryObject2 = new QuestionPaperCategoryQueryObject(); queryObject2.setName(model.getName()); Map map2 = GetList(queryObject2); Object object2 = map2.get(MapDataType.DATA_LIST); if (object2 != null) { List list = (List) object2; if (list != null && list.size() > 0) { List target = list.stream() .filter(m -> m.getName() != null && m.getName().equals(model.getName()) && !m.getId().equals(model.getId())) .collect(Collectors.toList()); if (target != null && target.size() > 0) { message = LangConstants.MSG_ERROR_CODE_EXISTED; return null; } } } } } entity.setStatus(model.getStatus()); entity.setName(model.getName()); entity.setRemark(model.getRemark()); entity.setParentid(model.getParentid()); entity.setIcon(model.getIcon()); return entity; } public List GetParent(QuestionPaperCategoryViewModel temp, List temps, List roots) { String parentid = temp.getParentid(); if (parentid != null && !parentid.isEmpty()) { List temps2 = temps.stream().filter(e -> e.getId().equals(parentid)).collect(Collectors.toList()); QuestionPaperCategoryViewModel root = temps2 != null && temps2.size() > 0 ? temps2.get(0) : null; if (root != null) { String rootparentid = root.getParentid(); if (rootparentid != null && !rootparentid.isEmpty()) { roots = GetParent(root, temps, roots); } else { if (roots.stream().filter(e -> e.getId().equals(root.getId())).count() <= 0) { roots.add(root); } } } } else { if (roots.stream().filter(e -> e.getId().equals(temp.getId())).count() <= 0) { roots.add(temp); } } return roots; } public QuestionPaperCategoryViewModel BuildTree(QuestionPaperCategoryViewModel root, List QuestionPaperCategorys) { if (QuestionPaperCategorys == null || QuestionPaperCategorys.size() <= 0) { return root; } List children = new ArrayList<>(); for (QuestionPaperCategoryViewModel QuestionPaperCategory : QuestionPaperCategorys) { if (QuestionPaperCategory != null && QuestionPaperCategory.getParentid() != null && root.getId().equals(QuestionPaperCategory.getParentid())) { children.add(QuestionPaperCategory); } } Collections.sort(children, (QuestionPaperCategoryViewModel o1, QuestionPaperCategoryViewModel o2) -> { if (o2.getStatus() == null || o1.getStatus() == null) return o2.getName().compareTo(o1.getName()); int levelC = o2.getStatus() - o1.getStatus(); if (levelC == 0) return o2.getName().compareTo(o1.getName()); else return levelC; }); root.setChildren(children); if (children != null && children.size() > 0) { for (QuestionPaperCategoryViewModel child : children) { BuildTree(child, QuestionPaperCategorys); } } return root; } private List FormateQuestionPaperCategoryParent(List models) { List list = new ArrayList<>(); List roots = models.stream() .filter(m -> m.getParentid() == null || m.getParentid().isEmpty()) .collect(Collectors.toList()); list.addAll(roots); List hasparentnodes = models.stream() .filter(m -> m.getParentid() != null && !m.getParentid().isEmpty()) .collect(Collectors.toList()); for (QuestionPaperCategoryViewModel hasparentnode : hasparentnodes) { List parents = models.stream() .filter(m -> m.getId().equals(hasparentnode.getParentid())) .collect(Collectors.toList()); if (parents != null && parents.size() > 0) { QuestionPaperCategoryViewModel model = new QuestionPaperCategoryViewModel(); QuestionPaperCategoryViewModel entity = parents.get(0); model.setId(entity.getId()); model.setCreatetime(entity.getCreatetime()); model.setParentid(entity.getParentid()); model.setName(entity.getName()); model.setIsdeleted(entity.getIsdeleted()); model.setRemark(entity.getRemark()); model.setIcon(entity.getIcon()); hasparentnode.setParent(model); } list.add(hasparentnode); } return list; } private List FormateQuestionPaperCategoryChildren(List models) { List list = new ArrayList<>(); List roots = models.stream() .filter(m -> m.getParentid() == null || m.getParentid().isEmpty()) .collect(Collectors.toList()); for (QuestionPaperCategoryViewModel root : roots) { List childrens = models.stream() .filter(m -> m.getParentid() != null && m.getParentid().equals(root.getId())) .collect(Collectors.toList()); if (childrens != null && childrens.size() > 0) { root.setChildren(childrens); } list.add(root); } return list; } }