| package doumeemes.service.ext.impl; | 
|   | 
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
| import com.github.pagehelper.PageHelper; | 
| import com.github.pagehelper.PageInfo; | 
| import doumeemes.core.constants.ResponseStatus; | 
| import doumeemes.core.exception.BusinessException; | 
| import doumeemes.core.model.LoginUserInfo; | 
| import doumeemes.core.model.PageData; | 
| import doumeemes.core.model.PageWrap; | 
| import doumeemes.core.utils.Constants; | 
| import doumeemes.core.utils.redis.RedisUtil; | 
| import doumeemes.dao.business.BomMapper; | 
| import doumeemes.dao.business.ProceduresMapper; | 
| import doumeemes.dao.business.model.Bom; | 
| import doumeemes.dao.business.model.Procedures; | 
| import doumeemes.dao.ext.CompanyExtMapper; | 
| import doumeemes.dao.ext.DepartmentExtMapper; | 
| import doumeemes.dao.ext.ProceduresExtMapper; | 
| import doumeemes.dao.ext.dto.QueryCompanyExtDTO; | 
| import doumeemes.dao.ext.dto.QueryProceduresExtDTO; | 
| import doumeemes.dao.ext.vo.CompanyExtListVO; | 
| import doumeemes.dao.ext.vo.ProceduresExtListVO; | 
| import doumeemes.service.ext.ProceduresExtService; | 
| import io.swagger.models.Contact; | 
| import io.swagger.models.auth.In; | 
| import org.apache.commons.lang3.StringUtils; | 
| 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 java.util.ArrayList; | 
| import java.util.List; | 
|   | 
| /** | 
|  * 工序信息Service实现 | 
|  * @author 江蹄蹄 | 
|  * @date 2022/04/20 10:57 | 
|  */ | 
| @Service | 
| public class ProceduresExtServiceImpl implements ProceduresExtService { | 
|   | 
|     @Autowired | 
|     private ProceduresExtMapper proceduresExtMapper; | 
|   | 
|     @Autowired | 
|     private ProceduresMapper proceduresMapper; | 
|     @Autowired | 
|     private DepartmentExtMapper departmentExtMapper; | 
|   | 
|     @Autowired | 
|     private RedisTemplate<String, Object> redisTemplate; | 
|     @Autowired | 
|     private CompanyExtMapper companyExtMapper; | 
|   | 
|     @Autowired | 
|     private BomMapper bomMapper; | 
|     @Override | 
|     public PageData<ProceduresExtListVO> findPage(PageWrap<QueryProceduresExtDTO> pageWrap) { | 
|         PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity()); | 
|         List<ProceduresExtListVO> result = proceduresExtMapper.selectList(pageWrap.getModel()); | 
|         return PageData.from(new PageInfo<>(result)); | 
|     } | 
|     @Override | 
|     public  void loadCom(CompanyExtListVO com ) { | 
|         QueryProceduresExtDTO d = new QueryProceduresExtDTO(); | 
|         d.setDeleted(Constants.ZERO); | 
|         d.setDmodelCompanyId(com.getId()); | 
|         //查询全部企业部门信息 | 
|         List<ProceduresExtListVO> clist =    proceduresExtMapper.selectList(d); | 
|         //加入redis缓存,以企业id为编码,缓存企业二维码 | 
|         RedisUtil.addObject(redisTemplate,Constants.RedisKeys.COM_PROCEDURE_KEY+com.getId(),clist); | 
|     } | 
|   | 
|     @Override | 
|     public List<ProceduresExtListVO> getListByCondition(QueryProceduresExtDTO dto) { | 
|         LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
|         if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){ | 
|             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!"); | 
|         } | 
|        /* //获取权限范围内的工序 | 
|         if(user.getProcedureIds()!=null){ | 
|             dto.setIds(user.getProcedureIds()); | 
|         }else{ | 
|             dto.setUserId(user.getCompanyUser().getId()); | 
|         }*/ | 
|   | 
|         List<ProceduresExtListVO> result = proceduresExtMapper.selectList(dto); | 
|         return result; | 
|     } | 
|   | 
|     @Override | 
|     public  void loadAll() { | 
|         QueryCompanyExtDTO dto = new QueryCompanyExtDTO(); | 
|         dto.setDeleted(Constants.ZERO); | 
|         //查询全部企业信息 | 
|         List<CompanyExtListVO> list =    companyExtMapper.selectList(dto); | 
|         if(list != null && list.size()>0){ | 
|             QueryProceduresExtDTO d = new QueryProceduresExtDTO(); | 
|             d.setDeleted(Constants.ZERO); | 
|             //查询全部企业工序信息 | 
|             List<ProceduresExtListVO> codeList =    proceduresExtMapper.selectList(d); | 
|             for(CompanyExtListVO com : list){ | 
|                 //当前企业的全部工序信息 | 
|                 List<ProceduresExtListVO> clist = getAllByComId(com.getId(),codeList); | 
|                 //加入redis缓存,以企业id为编码,缓存企业工序 | 
|                 RedisUtil.addObject(redisTemplate,Constants.RedisKeys.COM_PROCEDURE_KEY+com.getId(),clist); | 
|             } | 
|         } | 
|     } | 
|     /** | 
|      * 根据企业编码获取工序 | 
|      * @param id | 
|      * @param alllist | 
|      * @return | 
|      */ | 
|     private  List<ProceduresExtListVO>  getAllByComId(Integer id, List<ProceduresExtListVO> alllist) { | 
|         List<ProceduresExtListVO> list = null; | 
|         if(alllist!= null){ | 
|             for(ProceduresExtListVO d :alllist){ | 
|                 if(d.getDmodel()!=null && Constants.equalsInteger(id,d.getDmodel().getCompanyId())){ | 
|                     if(list == null){ | 
|                         list = new ArrayList<>(); | 
|                     } | 
|                     list.add(d); | 
|                 } | 
|             } | 
|         } | 
|         return  list; | 
|     } | 
|   | 
|     @Override | 
|     public List<ProceduresExtListVO> getGYListByCondition(Integer routeId) { | 
|         List<ProceduresExtListVO> result = proceduresExtMapper.getGYListByCondition(routeId); | 
|         return result; | 
|     } | 
|     @Override | 
|     public List<ProceduresExtListVO>  getSelfList(QueryProceduresExtDTO queryProceduresExtDTO){ | 
|         LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
|         if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){ | 
|             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!"); | 
|         } | 
|         List<Integer> ids =user.getProcedureIds(); | 
|         List<ProceduresExtListVO> result = new ArrayList<>(); | 
|         if(ids!=null && ids.size()>0){ | 
|             //查询缓存数据 | 
|             List<ProceduresExtListVO> clist = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_PROCEDURE_KEY+user.getCompany().getId(),ArrayList.class); | 
|             if(clist!=null){ | 
|                 for(ProceduresExtListVO p : clist){ | 
|                     if(Constants.equalsInteger(p.getDepartId(),user.getCurComDepartment().getId()) && exsistsInList(ids,p.getId())){ | 
|                         result.add(p); | 
|                     } | 
|                 } | 
|             } | 
|         } | 
|         return result; | 
|     } | 
|   | 
|     private boolean exsistsInList(List<Integer> ids, Integer id) { | 
|         for(Integer idt : ids){ | 
|             if(Constants.equalsInteger(idt,id)){ | 
|                 return true; | 
|             } | 
|         } | 
|         return false; | 
|     } | 
|   | 
|     @Override | 
|     public  ProceduresExtListVO getById(Integer comId, Integer id){ | 
|         if(comId==null || id == null){ | 
|             return null; | 
|         } | 
|         //查询缓存数据 | 
|         List<ProceduresExtListVO> clist = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_PROCEDURE_KEY+comId,ArrayList.class); | 
|        return getModelFromList(clist,id); | 
|     } | 
|   | 
|     private ProceduresExtListVO getModelFromList(List<ProceduresExtListVO> clist, Integer id) { | 
|         if(clist!=null){ | 
|             for(ProceduresExtListVO model:clist){ | 
|                 if(Constants.equalsInteger(id,model.getId())){ | 
|                     return model ; | 
|                 } | 
|             } | 
|         } | 
|         return null; | 
|     } | 
|   | 
|     @Override | 
|     public  String getNamesByIds(Integer comId, List<Integer> ids){ | 
|         if(comId==null || ids == null){ | 
|             return null; | 
|         } | 
|         String temp = ""; | 
|         //查询缓存数据 | 
|         List<ProceduresExtListVO> clist = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_PROCEDURE_KEY+comId,ArrayList.class); | 
|         for(Integer id:ids){ | 
|             ProceduresExtListVO model =  getModelFromList(clist,id); | 
|             if(model !=null && StringUtils.isNotBlank(model.getName())){ | 
|                 if(StringUtils.isNotBlank(temp)){ | 
|                     temp+=","; | 
|                 } | 
|                 temp+=StringUtils.defaultString(model.getName(),""); | 
|             } | 
|         } | 
|         return temp; | 
|   | 
|     } | 
|     @Override | 
|     public  String getNamesByIdStr(Integer comId,String idStr){ | 
|         if(comId==null || idStr == null){ | 
|             return null; | 
|         } | 
|         String[] ids = idStr.split(","); | 
|         List<Integer> idList = new ArrayList<>(); | 
|         for(String s : ids){ | 
|             idList.add(Integer.parseInt(s)); | 
|         } | 
|         return getNamesByIds(comId,idList); | 
|     } | 
|   | 
|     @Override | 
|     public List<Procedures> getListByMaterialId(Integer materialId){ | 
|         //获取物料BOM信息 | 
|         List<Bom> bomList = bomMapper.selectList(new QueryWrapper<Bom>().eq("MATERIAL_ID",materialId).eq("STATUS",1)); | 
|         if(bomList.size()>Constants.ONE){ | 
|             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,当前物料对应BOM数据异常,存在多条启用BOM!"); | 
|         }else if(bomList.size()== Constants.ZERO){ | 
|             return new ArrayList<>(); | 
|         } | 
|         Bom bom = bomList.get(Constants.ZERO); | 
|        List<Procedures> proceduresList = proceduresMapper.selectList( | 
|                 new QueryWrapper<Procedures>().apply(" id in ( select r.PROCEDURE_ID  from route_procedure r where r.ROUTE_ID = '"+bom.getRouteId()+"' )  ") | 
|                         .orderByAsc( " SORTNUM  ") | 
|         ); | 
|        return proceduresList; | 
|     } | 
|   | 
|   | 
| } |