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.QuestionCategoryMapper; import com.farriver.bwf.data.master.model.QuestionCategory; import com.farriver.bwf.data.master.model.QuestionCategoryExample; import com.farriver.bwf.data.transferobject.queryobject.question.QuestionCategoryQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.question.QuestionCategoryViewModel; 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_QuestionCategory_cache") public class QuestionCategoryService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(QuestionCategoryService.class); @Resource QuestionCategoryMapper mapper; private String message; public Map GetList(QuestionCategoryQueryObject queryObject) { if (queryObject == null) return null; QuestionCategoryExample example = new QuestionCategoryExample(); QuestionCategoryExample.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 QuestionCategory GetDetail(QuestionCategoryQueryObject 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(QuestionCategoryViewModel model) { QuestionCategory 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(QuestionCategoryViewModel model) { QuestionCategory 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(QuestionCategoryQueryObject 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 QuestionCategoryViewModel GetViewDetail(QuestionCategoryQueryObject queryObject) { QuestionCategory entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public ApiPageData GetViewPageList(QuestionCategoryQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (QuestionCategory entity : list) { QuestionCategoryViewModel 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 getQuestionCategoryTree(QuestionCategoryQueryObject queryObject) { List reusltQuestionCategorys = new ArrayList<>(); List roots = new ArrayList<>(); //系统所有菜单 List QuestionCategorys = (List) GetViewPageList(queryObject).getData(); for (QuestionCategoryViewModel QuestionCategory : QuestionCategorys) { if (QuestionCategory.getParentid() == null || QuestionCategory.getParentid().isEmpty()) { roots.add(QuestionCategory); } } for (QuestionCategoryViewModel root : roots) { root = BuildTree(root, QuestionCategorys); reusltQuestionCategorys.add(root); } Collections.sort(reusltQuestionCategorys, (QuestionCategoryViewModel o1, QuestionCategoryViewModel 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 reusltQuestionCategorys; } @Cacheable public ApiPageData GetViewPageListWithChildren(QuestionCategoryQueryObject queryObject) { if (queryObject == null) return null; //目的获取全部数据 QuestionCategoryQueryObject queryObject2 = new QuestionCategoryQueryObject(); 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 (QuestionCategory entity : list) { QuestionCategoryViewModel viewModel = BuildViewModel(entity); viewModels.add(viewModel); } } viewModels = FormateQuestionCategoryParent(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 (QuestionCategoryViewModel model : viewModels) { if (model != null) { List parents = GetParent(model, temps, roots1); for (QuestionCategoryViewModel parent : parents) { if (viewModels.stream().filter(e -> e.getId().equals(parent.getId())).count() <= 0) { temps2.add(parent); } } } } //将父级元素组添加到结果集合中 for (QuestionCategoryViewModel model : temps2) { if (viewModels.stream().filter(e -> e.getId().equals(model.getId())).count() <= 0) { viewModels.add(model); } } //通过递归构造树形结构 List roots = new ArrayList<>(); for (QuestionCategoryViewModel 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 QuestionCategoryViewModel BuildViewModel(QuestionCategory entity) { if (entity == null) return null; QuestionCategoryViewModel model = new QuestionCategoryViewModel(); 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 QuestionCategory Check(ActionType actionType, QuestionCategoryViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } QuestionCategory entity = null; if (ActionType.CREATE == actionType) { if (model.getName() != null && !model.getName().isEmpty()) { QuestionCategoryQueryObject queryObject2 = new QuestionCategoryQueryObject(); 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 QuestionCategory(); entity.setId(UUID.randomUUID().toString()); entity.setCreatetime(new Date()); entity.setIsdeleted(false); } else { QuestionCategoryQueryObject queryObject = new QuestionCategoryQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); entity.setUpdatetime(new Date()); if (model.getName() != null && !model.getName().isEmpty()) { QuestionCategoryQueryObject queryObject2 = new QuestionCategoryQueryObject(); 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(QuestionCategoryViewModel 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()); QuestionCategoryViewModel 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 QuestionCategoryViewModel BuildTree(QuestionCategoryViewModel root, List QuestionCategorys) { if (QuestionCategorys == null || QuestionCategorys.size() <= 0) { return root; } List children = new ArrayList<>(); for (QuestionCategoryViewModel QuestionCategory : QuestionCategorys) { if (QuestionCategory != null && QuestionCategory.getParentid() != null && root.getId().equals(QuestionCategory.getParentid())) { children.add(QuestionCategory); } } Collections.sort(children, (QuestionCategoryViewModel o1, QuestionCategoryViewModel 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 (QuestionCategoryViewModel child : children) { BuildTree(child, QuestionCategorys); } } return root; } private List FormateQuestionCategoryParent(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 (QuestionCategoryViewModel hasparentnode : hasparentnodes) { List parents = models.stream() .filter(m -> m.getId().equals(hasparentnode.getParentid())) .collect(Collectors.toList()); if (parents != null && parents.size() > 0) { QuestionCategoryViewModel model = new QuestionCategoryViewModel(); QuestionCategoryViewModel 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 FormateQuestionCategoryChildren(List models) { List list = new ArrayList<>(); List roots = models.stream() .filter(m -> m.getParentid() == null || m.getParentid().isEmpty()) .collect(Collectors.toList()); for (QuestionCategoryViewModel 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; } }