| 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.biz.system.SystemDictDataBiz; | 
| 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.DateUtil; | 
| import doumeemes.core.utils.redis.RedisUtil; | 
| import doumeemes.dao.business.model.CategoryUnion; | 
| import doumeemes.dao.business.model.CompanyUser; | 
| import doumeemes.dao.business.model.WorkorderCheck; | 
| import doumeemes.dao.ext.CategoryUnionExtMapper; | 
| import doumeemes.dao.ext.CompanyUserExtMapper; | 
| import doumeemes.dao.ext.WorkorderCheckExtMapper; | 
| import doumeemes.dao.ext.dto.QueryWorkorderCheckExtDTO; | 
| import doumeemes.dao.ext.vo.CategoryExtListVO; | 
| import doumeemes.dao.ext.vo.DepartmentExtListVO; | 
| import doumeemes.dao.ext.vo.WorkorderCheckExtListVO; | 
| import doumeemes.dao.ext.vo.WorkorderCheckGroupListVO; | 
| import doumeemes.service.ext.CategoryExtService; | 
| import doumeemes.service.ext.DepartmentExtService; | 
| import doumeemes.service.ext.WorkorderCheckExtService; | 
| 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.Date; | 
| import java.util.List; | 
|   | 
| /** | 
|  * 检验信息Service实现 | 
|  * @author 江蹄蹄 | 
|  * @date 2022/04/20 10:56 | 
|  */ | 
| @Service | 
| public class WorkorderCheckExtServiceImpl implements WorkorderCheckExtService { | 
|   | 
|     @Autowired | 
|     private RedisTemplate<String, Object> redisTemplate; | 
|     @Autowired | 
|     private WorkorderCheckExtMapper workorderCheckExtMapper; | 
|     @Autowired | 
|     private CompanyUserExtMapper companyUserExtMapper; | 
|     @Autowired | 
|     private CategoryExtService  categoryExtService; | 
|   | 
|     @Autowired | 
|     private CategoryUnionExtMapper  categoryUnionExtMapper; | 
|     @Autowired | 
|     private DepartmentExtService departmentExtService; | 
|     @Autowired | 
|     private SystemDictDataBiz systemDictDataBiz; | 
|   | 
|     @Override | 
|     public PageData<WorkorderCheckExtListVO> findPage(PageWrap<QueryWorkorderCheckExtDTO> pageWrap) { | 
|         LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
|         if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){ | 
|             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!"); | 
|         } | 
|         //只能查看当前根组织的数据 | 
|         pageWrap.getModel().setDeleted(Constants.ZERO); | 
|         pageWrap.getModel().setRootDepartId(user.getRootDepartment().getId()); | 
| //        if(!Constants.equalsInteger(user.getCurComDepartment().getId(),user.getRootDepartment().getId())){ | 
|             //如果当前选择的公司级组织非根组织信息,只能查看当前选择公司级组织数据 | 
|             pageWrap.getModel().setDepartId(user.getCurComDepartment().getId()); | 
| //        } | 
|         //数据权限 | 
|         List<Integer> dataPermission = user.getDepartPermissionList(); | 
|         if(dataPermission!=null){ | 
|            // pageWrap.getModel().setPmodelUserId(user.getId()); | 
|             pageWrap.getModel().setDepartIds(dataPermission); | 
|             pageWrap.getModel().setCreateUser(user.getId()); | 
|         /*    if(dataPermission.size() == 0){ | 
|                 //只能看自己的 | 
|                 pageWrap.getModel().setPmodelUserId(user.getId()); | 
|             }else{ | 
|                 //否则走数据权限 | 
|                 pageWrap.getModel().setDepartIds(dataPermission); | 
|             }*/ | 
|         } | 
|   | 
|         List<Integer> procedureIds=user.getProcedureIds();//权限范围内工序 | 
|         if(procedureIds!=null){ | 
|             if(procedureIds.size()>0){ | 
|                 pageWrap.getModel().setProcedureIds(procedureIds); | 
|             } | 
|   | 
|         } | 
|         PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity()); | 
|         pageWrap.getModel().setDeleted(Constants.ZERO); | 
|         List<WorkorderCheckExtListVO> result = workorderCheckExtMapper.selectListNew(pageWrap.getModel()); | 
|         if(result.size()>0){ | 
|             List<DepartmentExtListVO> allDepartList = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_DEPART_LIST_KEY+user.getCompany().getId(),ArrayList.class); | 
|             for(int i=0;i<result.size();i++){ | 
|                 WorkorderCheckExtListVO data=result.get(i); | 
|                 initProuserIds(user,data,allDepartList); | 
|                 initCheckUser(user,data,allDepartList); | 
|                 initCheckInfo(data,user); | 
|             } | 
|         } | 
|         return PageData.from(new PageInfo<>(result)); | 
|     } | 
|   | 
|     private void initCheckUser(LoginUserInfo user, WorkorderCheckExtListVO data, List<DepartmentExtListVO> allDepartList) { | 
|         if(data.getCheckUserId() ==null){ | 
|             return; | 
|         } | 
|         CompanyUser query=new CompanyUser(); | 
|         query.setUserId(data.getCheckUserId()); | 
|         query.setDeleted(Constants.ZERO); | 
|         query.setRootDepartId(user.getRootDepartment().getId()); | 
|         CompanyUser comp= companyUserExtMapper.selectOne(new QueryWrapper<>(query)); | 
|         if(comp!=null){ | 
|             DepartmentExtListVO d = departmentExtService.getModelById(user.getCompany().getId(),comp.getDepartmentId(),allDepartList); | 
|             String dName =d==null?"":StringUtils.defaultString(d.getName(),""); | 
|             data.setCheckUserDepartName(dName); | 
|         } | 
|     } | 
|   | 
|     private void initProuserIds(LoginUserInfo user, WorkorderCheckExtListVO data, List<DepartmentExtListVO> allDepartList) { | 
|         if(StringUtils.isNotEmpty(data.getProUserids())){ | 
|             String[] str=data.getProUserids().split(","); | 
|             for(String id:str){ | 
|                 id = StringUtils.strip(id,"[]"); | 
|                 CompanyUser query=new CompanyUser(); | 
|                 query.setUserId(Integer.valueOf(id)); | 
|                 Integer rootDepartId=data.getRootDepartId(); | 
|                 query.setRootDepartId(rootDepartId); | 
|                 CompanyUser comp= companyUserExtMapper.selectOne(new QueryWrapper<>(query).last(" limit 1")); | 
|                 if(comp!=null){ | 
|                     DepartmentExtListVO d = departmentExtService.getModelById(user.getCompany().getId(),comp.getDepartmentId(),allDepartList); | 
|                     String dName =d==null?"":StringUtils.defaultString(d.getName(),""); | 
|                     data.setProUserids(comp.getName()+"-"+dName); | 
|                 } | 
|             } | 
|         } | 
|     } | 
|   | 
|     @Override | 
|     public  WorkorderCheckExtListVO findDetailById(Integer tid){ | 
|         LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
|         if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){ | 
|             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!"); | 
|         } | 
|         QueryWorkorderCheckExtDTO pageWrap = new QueryWorkorderCheckExtDTO(); | 
|         pageWrap.setId(tid); | 
|         //只能查看当前根组织的数据 | 
|         pageWrap.setDeleted(Constants.ZERO); | 
|         List<WorkorderCheckExtListVO> result = workorderCheckExtMapper.selectListNew(pageWrap); | 
|         WorkorderCheckExtListVO data = null; | 
|         if(result != null && result.size()>0){ | 
|             data=result.get(0); | 
|             List<DepartmentExtListVO> allDepartList = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_DEPART_LIST_KEY+user.getCompany().getId(),ArrayList.class); | 
|             initProuserIds(user,data,allDepartList); | 
|             initCheckUser(user,data,allDepartList); | 
|             initCheckInfo(data,user); | 
|         } | 
|         return data; | 
|     } | 
|     private void initCheckInfo(WorkorderCheckExtListVO data, LoginUserInfo user) { | 
|         if(StringUtils.isNotBlank(data.getCheckInfo())){ | 
|             try { | 
|                 List<CategoryExtListVO> clist = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_CATEGORY_KEY+user.getCompany().getId(),ArrayList.class); | 
|                 CategoryUnion uModel = categoryUnionExtMapper.selectById(Integer.parseInt(data.getCheckInfo())); | 
|                 if(uModel!= null ){ | 
|                     String cnamge = getCatenameById(user,uModel.getCateBigId(),clist)+ getCatenameById(user,uModel.getCateMiddleId(),clist)+ getCatenameById(user,uModel.getCateSmallId(),clist); | 
|                     data.setCheckInfo(cnamge); | 
|   | 
|                 } | 
|             }catch (Exception e){ | 
|   | 
|             } | 
|         } | 
|     } | 
|   | 
|     private String getCatenameById(LoginUserInfo user, Integer cateId, List<CategoryExtListVO> clist) { | 
|         if(cateId!=null){ | 
|             CategoryExtListVO bc = categoryExtService.getByCategoryId(user.getCompany().getId(),cateId,clist); | 
|             if(bc!=null){ | 
|                 return StringUtils.defaultString(bc.getName(),""); | 
|             } | 
|         } | 
|         return ""; | 
|     } | 
|   | 
|     @Override | 
|     public  List<WorkorderCheckGroupListVO> listGroupByAppliance(QueryWorkorderCheckExtDTO pageWrap){ | 
|         LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
|         if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){ | 
|             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!"); | 
|         } | 
|         if(pageWrap.getWorkorderId() == null){ | 
|             throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,请求参数不正确!"); | 
|         } | 
|   | 
|         WorkorderCheck cParam = new WorkorderCheck(); | 
|         cParam.setWorkorderId(pageWrap.getWorkorderId()); | 
|         cParam.setDeleted(Constants.ZERO); | 
|         //查询工单全部检验记录 | 
|         List<WorkorderCheck> checkList = workorderCheckExtMapper.selectGroupList(cParam); | 
|         List<WorkorderCheckGroupListVO> result = new ArrayList<>(); | 
|         Integer scrapId =   systemDictDataBiz.queryByCode(Constants.SYSTEM_APPLIANCE_TYPE,Constants.APPLIANCES_TYPE_LABLE.scrap).getId(); | 
|         if(checkList !=null){ | 
|             for(WorkorderCheck c : checkList){ | 
|                 if( Constants.formatIntegerNum(c.getUnqualifiedNum())>0){ | 
|                     WorkorderCheckGroupListVO tModel = new WorkorderCheckGroupListVO(); | 
|                     tModel.setId(c.getQualifiedApplianceId()); | 
|                     tModel.setCode(c.getQualifiedApplianceCode()); | 
|                     tModel.setMaterialName(c.getMaterialName()); | 
|                     tModel.setNum(c.getUnqualifiedNum()); | 
|                     if(Constants.equalsInteger(c.getDoneType(),scrapId)){ | 
|                         //如果是报废品 | 
|                         tModel.setType(Constants.ONE); | 
|                     }else { | 
|                         //如果是不良品 | 
|                         tModel.setType(Constants.ZERO); | 
|                     } | 
|                     result.add(tModel); | 
|                 } | 
|             } | 
|         } | 
|         return result; | 
|     } | 
|     @Override | 
|     public synchronized  String  getNextCode(Integer comId ){ | 
|         String prefix =   DateUtil.getDate(new Date(),"yyyyMMdd") +"-"; | 
|         Integer countNum  = RedisUtil.getObject(redisTemplate, Constants.RedisKeys.COM_CHECK_CODE_KEY+comId,Integer.class); | 
|         countNum = Constants.formatIntegerNum(countNum)+1; | 
|         //更新缓存 | 
|         RedisUtil.addObject(redisTemplate,Constants.RedisKeys.COM_CHECK_CODE_KEY+comId,countNum); | 
|         String nextIndex =Integer.toString( countNum); | 
|         return prefix + StringUtils.leftPad(nextIndex,4,"0"); | 
|     } | 
| } |