package org.jeecg.modules.system.service.impl; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.jeecg.common.constant.CommonConstant; import org.jeecg.common.constant.FillRuleConstant; import org.jeecg.common.system.vo.LoginUser; import org.jeecg.common.util.FillRuleUtil; import org.jeecg.common.util.oConvertUtils; import org.jeecg.modules.system.entity.*; import org.jeecg.modules.system.mapper.*; import org.jeecg.modules.system.model.MdcProductionTreeModel; import org.jeecg.modules.system.model.ProductionIdModel; import org.jeecg.modules.system.service.IMdcProductionService; import org.jeecg.modules.system.util.FindsProductionsChildrenUtil; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.*; import java.util.stream.Collectors; /** * @Description: 产线表 * @Author: liuS * @Date: 2023-03-23 * @Version: V1.0 */ @Service public class MdcProductionServiceImpl extends ServiceImpl implements IMdcProductionService { @Resource private SysUserMapper sysUserMapper; @Resource private MdcUserProductionMapper userProductionMapper; @Resource private MdcProductionEquipmentMapper productionEquipmentMapper; /** * queryTreeList 对应 queryTreeList 查询所有的产线数据,以树结构形式响应给前端 */ @Override @Cacheable(value = "mdc:cache:production:alldata") public List queryTreeList() { LambdaQueryWrapper query = new LambdaQueryWrapper(); query.eq(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_0.toString()); query.orderByAsc(MdcProduction::getProductionOrder); List list = this.list(query); //设置用户id,让前台显示 this.setUserIdsByProList(list); //调用wrapTreeDataToTreeList方法生成树状数据 return FindsProductionsChildrenUtil.wrapTreeDataToTreeList(list); } /** * queryTreeList 根据产线id查询,前端回显调用 */ @Override public List queryTreeList(String ids) { List listResult = new ArrayList<>(); LambdaQueryWrapper query = new LambdaQueryWrapper(); query.eq(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_0.toString()); if (oConvertUtils.isNotEmpty(ids)) { query.in(true, MdcProduction::getId, ids.split(",")); } query.orderByAsc(MdcProduction::getProductionOrder); List list = this.list(query); for (MdcProduction production : list) { if (production.getDescription().isEmpty()){ production.setDescription(""); } listResult.add(new MdcProductionTreeModel(production)); } return listResult; } @Override @Cacheable(value = "mdc:cache:production:allids") public List queryProductionIdTreeList() { LambdaQueryWrapper query = new LambdaQueryWrapper<>(); query.eq(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_0.toString()); query.orderByAsc(MdcProduction::getProductionOrder); List list = this.list(query); //调用wrapTreeDataToTreeList方法生成树状数据 return FindsProductionsChildrenUtil.wrapTreeDataToProductionIdTreeList(list); } /** * 根据关键字搜索相关的部门数据 */ @Override public List searchByKeyWord(String keyWord) { LambdaQueryWrapper query = new LambdaQueryWrapper<>(); List newList = new ArrayList<>(); query.like(MdcProduction::getProductionName, keyWord); MdcProductionTreeModel model = new MdcProductionTreeModel(); List productionList = this.list(query); if (!productionList.isEmpty()) { for (MdcProduction mdcProduction : productionList) { model = new MdcProductionTreeModel(mdcProduction); model.setChildren(null); newList.add(model); } return newList; } return Collections.emptyList(); } /** * saveProductionData 对应 add 保存用户在页面添加的新的产线对象数据 */ @Override @Transactional(rollbackFor = Exception.class) public void saveProductionData(MdcProduction mdcProduction) { if (mdcProduction != null) { if (mdcProduction.getParentId() == null) { mdcProduction.setParentId(""); } mdcProduction.setId(IdWorker.getIdStr(mdcProduction)); // 先判断该对象有无父级ID,有则意味着不是最高级,否则意味着是最高级 // 获取父级ID String parentId = mdcProduction.getParentId(); JSONObject formData = new JSONObject(); formData.put("parentId",parentId); String[] codeArray = (String[]) FillRuleUtil.executeRule(FillRuleConstant.PRODUCTION,formData); mdcProduction.setOrgCode(codeArray[0]); String orgType = codeArray[1]; mdcProduction.setOrgType(String.valueOf(orgType)); mdcProduction.setDelFlag(CommonConstant.DEL_FLAG_0.toString()); this.save(mdcProduction); } } /** * updateProductionDataById 对应 edit 根据产线主键来更新对应的产线数据 */ @Override @Transactional(rollbackFor = Exception.class) public boolean updateProductionDataById(MdcProduction mdcProduction) { if (mdcProduction != null) { this.updateById(mdcProduction); return true; } return false; } /** * 根据产线id删除并删除其可能存在的子级产线 */ @Override @Transactional(rollbackFor = Exception.class) public boolean delete(String id) { List idList = new ArrayList<>(); idList.add(id); this.checkChildrenExists(id, idList); boolean result = this.removeByIds(idList); //根据产线id删除用户与产线关系 userProductionMapper.delete(new LambdaQueryWrapper().in(MdcUserProduction::getProId, idList)); //根据产线id删除产线与设备关系 productionEquipmentMapper.delete(new LambdaQueryWrapper().in(MdcProductionEquipment::getProductionId, idList)); return result; } /** * 根据产线id批量删除并删除其可能存在的子级产线 */ @Override @Transactional(rollbackFor = Exception.class) public void deleteBatchWithChildren(List ids) { List idList = new ArrayList<>(); for (String id : ids) { idList.add(id); this.checkChildrenExists(id, idList); } this.removeByIds(idList); //根据产线id删除用户与产线关系 userProductionMapper.delete(new LambdaQueryWrapper().in(MdcUserProduction::getProId, idList)); //根据产线id删除产线与设备关系 productionEquipmentMapper.delete(new LambdaQueryWrapper().in(MdcProductionEquipment::getProductionId, idList)); } /** * 根据id查询下级产线 */ @Override public List queryProdByPid(String pid) { return this.baseMapper.queryProdByPid(pid); } /** * 递归查询所有子节点 */ @Override public List recursionChildrenByPid(String pid){ List ids=this.recursionChildren(pid); return super.list(new LambdaQueryWrapper().eq(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_0.toString()).in(MdcProduction::getId, ids)); } /** * 根据用户id获取产线下拉树选项 */ @Override public List loadProductionTreeOptions(String userId) { //获取所有产线数据 List productionList = this.baseMapper.selectList(new LambdaQueryWrapper().eq(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_0.toString()).orderByAsc(MdcProduction::getProductionOrder)); //根据用户id获取拥有的产线信息集合 List productionIds = userProductionMapper.queryProductionIdsByUserId(userId); List allProductionIds = new ArrayList<>(); //找到所有产线id的上级id if (productionIds != null && !productionIds.isEmpty()) { for (String productionId : productionIds) { this.getAllProductionIds(productionList, productionId, allProductionIds); } } //过滤产线数据 List list = productionList.stream().filter((MdcProduction mdcProduction) -> allProductionIds.contains(mdcProduction.getId())).collect(Collectors.toList()); return FindsProductionsChildrenUtil.wrapTreeDataToProductionIdTreeList(list); } /** * 递归查询所有子节点id */ @Override public List recursionChildren(String productionId) { return this.baseMapper.recursionChildren(productionId); } /** * 根据用户id和车间id获取用户拥有的车间id * @param userId * @param productionId * @return */ @Override public String findFirstProduction(String userId, String productionId) { return this.baseMapper.findFirstProduction(userId, productionId); } /** * 根据用户id查询用户工段权限 */ @Override public String findThreeProductionId(String userId) { return this.baseMapper.findThreeProductionId(userId); } /** * delete 方法调用 递归查找子集id */ private void checkChildrenExists(String id, List idList) { LambdaQueryWrapper query = new LambdaQueryWrapper<>(); query.eq(MdcProduction::getParentId, id); List productionList = this.list(query); if (productionList != null && !productionList.isEmpty()) { for (MdcProduction production : productionList) { idList.add(production.getId()); this.checkChildrenExists(production.getId(), idList); } } } /** * 获取所有的产线id(包含所有上级) */ private void getAllProductionIds(List productionList, String productionId, List allProductionIds) { if (!allProductionIds.contains(productionId)) { allProductionIds.add(productionId); } for (MdcProduction mdcProduction : productionList) { if (StringUtils.isEmpty(mdcProduction.getParentId())) { continue; } if (productionId.equals(mdcProduction.getId())) { if (!allProductionIds.contains(mdcProduction.getParentId())) { allProductionIds.add(mdcProduction.getParentId()); getAllProductionIds(productionList, mdcProduction.getParentId(), allProductionIds); } } } } /** * 通过产线集合为产线设置用户id,用于前台展示 */ private void setUserIdsByProList(List productionList) { //查询负责部门不为空的情况 LambdaQueryWrapper query = new LambdaQueryWrapper<>(); query.isNotNull(SysUser::getDepartIds); List users = sysUserMapper.selectList(query); Map map = new HashMap(5); //先循环一遍找到不同的负责产线id for (SysUser user : users) { String productionIds = user.getProductionIds(); if (StringUtils.isNotBlank(productionIds)) { String[] productionIdArray = productionIds.split(","); for (String productionId : productionIdArray) { if (map.containsKey(productionId)) { String userIds = map.get(productionId) + "," + user.getId(); map.put(productionId, userIds); } else { map.put(productionId, user.getId()); } } } } //循环产线集合找到产线id对应的负责用户 for (MdcProduction mdcProduction : productionList) { if (map.containsKey(mdcProduction.getId())) { mdcProduction.setDirectorUserIds(map.get(mdcProduction.getId()).toString()); } } } /** * 查询所有父节点和本节点名称 * @param id * @return */ @Override public List findListParentTreeAll(String id){ MdcProductionEquipment mdcProductionEquipment=productionEquipmentMapper.selectOne(new QueryWrapper().eq("equipment_id",id)); if (mdcProductionEquipment==null) { return null; } List strings = new ArrayList<>(); MdcProduction en=super.getById(mdcProductionEquipment.getProductionId()); if (en == null) { return null; } strings.add(en.getProductionName()); if (StringUtils.isEmpty(en.getParentId())) { return strings; } else { return findListParentTree(en.getParentId(),strings); } } // 查询所以父节点 @Override public List findListParentTree(String parentId,List stringList){ if (StringUtils.isEmpty(parentId)) { return null; } if (stringList == null || stringList.isEmpty()) { stringList = new ArrayList<>(); } boolean p = true; if (p) { MdcProduction en = super.getById(parentId); if (en != null) { stringList.add(0,en.getProductionName()); } if (StringUtils.isNotBlank(en.getParentId())) { parentId = en.getParentId(); findListParentTree(parentId,stringList); } else { p = false; return stringList; } } return stringList; } /** * 获取用户已分配的部门列表 * @param userId * @return */ @Override public Map getUserAssignedDepart(String userId){ if(StrUtil.isEmpty(userId)) return null; List userPermDepart = this.baseMapper.findAllProductionId(userId); if(userPermDepart == null || userPermDepart.isEmpty()) return null; Map map = new HashMap<>(); userPermDepart.forEach(item -> { map.put(item.getId(), item); }); return map; } /** * 通过一组id获取部门 * @param ids * @return */ @Override public List findAllProductionIds(List ids){ return this.baseMapper.recursionChildrenByList(ids); } }