|  |  | 
 |  |  | package doumeemes.service.business.impl; | 
 |  |  |  | 
 |  |  | import com.baomidou.mybatisplus.extension.api.R; | 
 |  |  | import doumeemes.biz.system.SystemDictBiz; | 
 |  |  | import doumeemes.core.constants.ResponseStatus; | 
 |  |  | import doumeemes.core.exception.BusinessException; | 
 |  |  | import doumeemes.core.model.ApiResponse; | 
 |  |  | 
 |  |  | import doumeemes.core.utils.Utils; | 
 |  |  | import doumeemes.core.utils.excel.EasyExcelUtil; | 
 |  |  | import doumeemes.core.utils.redis.RedisUtil; | 
 |  |  | import doumeemes.dao.business.DepartmentMapper; | 
 |  |  | import doumeemes.dao.business.DeviceMapper; | 
 |  |  | import doumeemes.dao.business.UserDeviceMapper; | 
 |  |  | import doumeemes.dao.business.model.CompanyUser; | 
 |  |  | import doumeemes.dao.business.model.Department; | 
 |  |  | import doumeemes.dao.business.model.Device; | 
 |  |  | import doumeemes.dao.business.model.UserDevice; | 
 |  |  | import doumeemes.dao.business.*; | 
 |  |  | import doumeemes.dao.business.model.*; | 
 |  |  | import doumeemes.dao.ext.dto.DeviceImportDTO; | 
 |  |  | import doumeemes.dao.ext.dto.QueryDeviceExtDTO; | 
 |  |  | import doumeemes.dao.ext.vo.DeviceExtListVO; | 
 |  |  | import doumeemes.dao.system.SystemDictDataMapper; | 
 |  |  | import doumeemes.dao.system.model.SystemDictData; | 
 |  |  | import doumeemes.service.business.DeviceService; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
 |  |  | 
 |  |  | import org.springframework.util.CollectionUtils; | 
 |  |  | import org.springframework.web.multipart.MultipartFile; | 
 |  |  |  | 
 |  |  | import java.util.Date; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.*; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * 生产设备信息表Service实现 | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private DepartmentMapper departmentMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private   ProceduresMapper proceduresMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private WarehouseLocationMapper  warehouseLocationMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private WarehouseMapper  warehouseMapper; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private SystemDictDataMapper systemDictDataMapper; | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public Integer create(Device device) { | 
 |  |  | 
 |  |  |     @Override | 
 |  |  |     public Device findOne(Device device) { | 
 |  |  |         QueryWrapper<Device> wrapper = new QueryWrapper<>(device); | 
 |  |  |         return deviceMapper.selectOne(wrapper); | 
 |  |  |         return deviceMapper.selectOne(wrapper.last("limit 1")); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |         } | 
 |  |  |         QueryDeviceExtDTO queryDeviceExtDTO=new QueryDeviceExtDTO(); | 
 |  |  |         Device queryDeviceExtDTO=new Device(); | 
 |  |  |         queryDeviceExtDTO.setDeleted(Constants.ZERO); | 
 |  |  |         queryDeviceExtDTO.setCode(device.getCode()); | 
 |  |  |         queryDeviceExtDTO.setRootDepartId(user.getRootDepartment().getId()); | 
 |  |  |         List<DeviceExtListVO> list= deviceExtService.getListByCondition(queryDeviceExtDTO); | 
 |  |  |         List<Device> list= this.findList(queryDeviceExtDTO); | 
 |  |  |  | 
 |  |  |        /* QueryDeviceExtDTO queryDeviceExtDTO1=new QueryDeviceExtDTO(); | 
 |  |  |         queryDeviceExtDTO1.setDeleted(Constants.ZERO); | 
 |  |  | 
 |  |  |         queryDeviceExtDTO1.setRootDepartId(user.getRootDepartment().getId()); | 
 |  |  |         List<DeviceExtListVO> list1= deviceExtService.getListByCondition(queryDeviceExtDTO1);*/ | 
 |  |  |         if(list.size()>0){ | 
 |  |  |             return ApiResponse.failed("设备编码,不允许添加"); | 
 |  |  |             return ApiResponse.failed("设备编码已存在,不允许添加"); | 
 |  |  |         } | 
 |  |  |         device.setDeleted(Constants.ZERO); | 
 |  |  |         device.setCreateTime(new Date()); | 
 |  |  | 
 |  |  |             userDeviceMapper.delete(new QueryWrapper<UserDevice>().eq("DEVICE_ID",deviceId)); | 
 |  |  |         } | 
 |  |  |         if(StringUtils.isNotEmpty(userIds)){ | 
 |  |  |             String [] ids=userIds.split(","); | 
 |  |  |             String [] ids=userIds.replace(" ","").split("[.,;,]"); | 
 |  |  |             for (String id : ids) { | 
 |  |  |                 CompanyUser companyUser = companyUserService.findById(Integer.valueOf(id)); | 
 |  |  |                 UserDevice userDevice = new UserDevice(); | 
 |  |  | 
 |  |  |         if(plansList == null || plansList.size()==0){ | 
 |  |  |             throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "导入数据内容有误!"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         plansList.forEach(s->{ | 
 |  |  |  | 
 |  |  |         List<String> departName = new ArrayList<>(); | 
 |  |  |         List<String> procedureName = new ArrayList<>(); | 
 |  |  |         int num =0; | 
 |  |  |         for(DeviceImportDTO s : plansList){ | 
 |  |  |             if (StringUtils.isBlank(s.getDeviceName()) | 
 |  |  |                     || s.getDeviceName().length() > 50 | 
 |  |  |                     || StringUtils.isBlank(s.getDepartName()) | 
 |  |  | 
 |  |  |                     || StringUtils.isBlank(s.getFinishWarehouseLocationName())){ | 
 |  |  |                 throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"导入数据内容有误"); | 
 |  |  |             } | 
 |  |  |         }); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         QueryWrapper<Department> wrapper = new QueryWrapper<>(); | 
 |  |  |         wrapper.lambda().eq(Department::getRootId,user.getRootDepartment().getId()); | 
 |  |  |         departmentMapper.selectList(wrapper); | 
 |  |  |             if(isRepeatCode(s,num,plansList)){ | 
 |  |  |                 throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"文档中设备号【"+s.getDeviceCode()+"】重复!"); | 
 |  |  |             } | 
 |  |  |             departName.add(s.getDepartName()); | 
 |  |  |             procedureName.add(s.getProcedureName()); | 
 |  |  |             num++; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         plansList.forEach(s->{ | 
 |  |  |  | 
 |  |  |             QueryWrapper<Department> wrapper = new QueryWrapper<>(); | 
 |  |  |             wrapper.lambda() | 
 |  |  |                     .eq(Department::getRootId,user.getRootDepartment().getId()) | 
 |  |  |                     .eq(Department::getName,s.getDepartName()) | 
 |  |  |                     .last("limit 1"); | 
 |  |  |             Department department = departmentMapper.selectOne(wrapper); | 
 |  |  |             if(Objects.isNull(department)){ | 
 |  |  |                 throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"工厂名称不存在"); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             QueryWrapper<Procedures> proceduresQuery = new QueryWrapper<>(); | 
 |  |  |             proceduresQuery.lambda() | 
 |  |  |                     .eq(Procedures::getRootDepartId,user.getRootDepartment().getId()) | 
 |  |  |                     .eq(Procedures::getOrgId,department.getId()) | 
 |  |  |                     .eq(Procedures::getName,s.getProcedureName()); | 
 |  |  |             Procedures procedures = proceduresMapper.selectOne(proceduresQuery); | 
 |  |  |             if(Objects.isNull(procedures)){ | 
 |  |  |                 throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"工序名称不存在"); | 
 |  |  |             } | 
 |  |  | //            QueryDeviceExtDTO queryDeviceExtDTO=new QueryDeviceExtDTO(); | 
 |  |  | //            queryDeviceExtDTO.setDeleted(Constants.ZERO); | 
 |  |  | //            queryDeviceExtDTO.setCode(s.getDeviceCode()); | 
 |  |  | //            queryDeviceExtDTO.setRootDepartId(user.getRootDepartment().getId()); | 
 |  |  | //            List<DeviceExtListVO> list= deviceExtService.getListByCondition(queryDeviceExtDTO); | 
 |  |  | //            if(list.size() > 0){ | 
 |  |  | //                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"设备编码不存在"); | 
 |  |  | //            } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |             QueryWrapper<WarehouseLocation> warehouseLocationQuery = new QueryWrapper<>(); | 
 |  |  |             warehouseLocationQuery.lambda() | 
 |  |  |                                     .eq(WarehouseLocation::getRootDepartId,department.getRootId()) | 
 |  |  |                                     .eq(WarehouseLocation::getUnionName | 
 |  |  |                                             ,s.getProduceWarehouseLocationName()).last("limit 1"); | 
 |  |  |  | 
 |  |  |             WarehouseLocation warehouseLocation = warehouseLocationMapper.selectOne(warehouseLocationQuery); | 
 |  |  |  | 
 |  |  |             if (Objects.isNull(warehouseLocation)){ | 
 |  |  |                 throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"待生产货位不存在"); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             QueryWrapper<SystemDictData> systemDictDataQuery = new QueryWrapper<>(); | 
 |  |  |             systemDictDataQuery.lambda() | 
 |  |  |                     .eq(SystemDictData::getDictId,3) | 
 |  |  |                     .eq(SystemDictData::getCode,"混合"); | 
 |  |  |             SystemDictData systemDictData = systemDictDataMapper.selectOne(systemDictDataQuery); | 
 |  |  |  | 
 |  |  |             warehouseLocationQuery.clear(); | 
 |  |  |             warehouseLocationQuery.lambda() | 
 |  |  |                                     .eq(WarehouseLocation::getRootDepartId,department.getRootId()) | 
 |  |  |                                     .eq(WarehouseLocation::getSystemDicDataId,systemDictData.getId()) | 
 |  |  |                                     .eq(WarehouseLocation::getUnionName,s.getFinishWarehouseLocationName()).last("limit 1"); | 
 |  |  |  | 
 |  |  |             WarehouseLocation finishWarehouseLocations = warehouseLocationMapper.selectOne(warehouseLocationQuery); | 
 |  |  |  | 
 |  |  |             if (Objects.isNull(finishWarehouseLocations)){ | 
 |  |  |                 throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"已完工货位不是混合属性"); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             Map<String, WarehouseLocation> collect =new HashMap<>(); | 
 |  |  |             collect.put(warehouseLocation.getUnionName(),warehouseLocation); | 
 |  |  |             collect.put(finishWarehouseLocations.getUnionName(),finishWarehouseLocations); | 
 |  |  |  | 
 |  |  |             Device device = new Device(); | 
 |  |  |             device.setDeleted(Constants.ZERO); | 
 |  |  | 
 |  |  |             device.setUpdateUser(user.getId()); | 
 |  |  |             device.setUpdateTime(new Date()); | 
 |  |  | //            device.setRemark(); | 
 |  |  | //            device.setRootDepartId(); | 
 |  |  | //            device.setDepartId(); | 
 |  |  | //            device.setProcedureId(); | 
 |  |  | //            device.setCode(); | 
 |  |  | //            device.setType(); | 
 |  |  | //            device.setName(); | 
 |  |  | //            device.setModel(); | 
 |  |  | //            device.setStatus(); | 
 |  |  | //            device.setSupplier(); | 
 |  |  | //            device.setStationCode(); | 
 |  |  | //            device.setGroupId(); | 
 |  |  | //            device.setSerialNum(); | 
 |  |  | //            device.setProduceWarehouseLocationId(); | 
 |  |  | //            device.setProduceWarehouseId(); | 
 |  |  | //            device.setFinishWarehouseLocationId(); | 
 |  |  | //            device.setFinishWarehouseId(); | 
 |  |  | //            device.setUserIds(); | 
 |  |  |             device.setRootDepartId(user.getRootDepartment().getId()); | 
 |  |  |             device.setDepartId(user.getCurComDepartment().getId()); | 
 |  |  |             device.setProcedureId(procedures.getId()); | 
 |  |  |             device.setCode(StringUtils.isNotBlank(s.getDeviceCode()) ? s.getDeviceCode() : this.getNextCode(user.getCompany().getId())); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |             Device ttt=new Device(); | 
 |  |  |             ttt.setDeleted(Constants.ZERO); | 
 |  |  |             ttt.setCode(device.getCode()); | 
 |  |  |             ttt.setRootDepartId(user.getRootDepartment().getId()); | 
 |  |  |             if(findOne(ttt) !=null){ | 
 |  |  |                 throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"设备编码【"+device.getCode()+"重复】,不允许添加"); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |             if(StringUtils.isNotBlank(s.getType()) && StringUtils.equals("设备组",s.getType())){ | 
 |  |  |                 device.setType(0); | 
 |  |  |             }else{ | 
 |  |  |                 device.setType(1); | 
 |  |  |             } | 
 |  |  | //            device.setType(StringUtils.isNotBlank(device.getCode()) ? device.getCode().trim().equals("设备") ? 1 : 0 : 1); | 
 |  |  |             device.setName(s.getDeviceName()); | 
 |  |  |             device.setModel(s.getModel()); | 
 |  |  |             device.setStatus(Constants.ZERO); | 
 |  |  |             device.setStationCode(s.getStationCode()); | 
 |  |  |             device.setProduceWarehouseLocationId(Optional.ofNullable(collect.get(s.getProduceWarehouseLocationName())).map(m->m.getId()).orElseThrow(null)); | 
 |  |  |             device.setFinishWarehouseLocationId(Optional.ofNullable(collect.get(s.getFinishWarehouseLocationName())).map(m->m.getId()).orElseThrow(null)); | 
 |  |  |             deviceMapper.insert(device); | 
 |  |  |             if(StringUtils.isNotBlank(s.getUserIds())){ | 
 |  |  |  | 
 |  |  |                 QueryWrapper<CompanyUser>  companyUserQuery = new QueryWrapper<>(); | 
 |  |  |  | 
 |  |  |                 companyUserQuery.lambda() | 
 |  |  |                                 .eq(CompanyUser::getComDepartId,user.getComDepartment().getId()) | 
 |  |  |                                 .in(CompanyUser::getPhone,Arrays.asList(s.getUserIds().split("[.,;,]"))); | 
 |  |  |                 List<CompanyUser> list1 = companyUserService.findList(companyUserQuery); | 
 |  |  |                 if (!CollectionUtils.isEmpty(list1)){ | 
 |  |  |                     List<String> collect1 = list1.stream().map(c -> c.getId().toString()).collect(Collectors.toList()); | 
 |  |  |                     this.dealDeviceUserId(Constants.ZERO,StringUtils.join(collect1,","),device.getId(),user); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |         }); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private boolean isRepeatCode(DeviceImportDTO s,int num,List<DeviceImportDTO> plansList) { | 
 |  |  |         if(StringUtils.isNotBlank(s.getDeviceCode())){ | 
 |  |  |             int tNum = 0; | 
 |  |  |             for(DeviceImportDTO d :plansList){ | 
 |  |  |                 if(num != tNum && StringUtils.equals(s.getDeviceCode(), d.getDeviceCode())){ | 
 |  |  |                     return  true; | 
 |  |  |                 } | 
 |  |  |                 tNum++; | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return  false; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     public synchronized String  getNextCode(Integer comId ){ | 
 |  |  |         String prefix =  "S-" + DateUtil.getDate(new Date(),"yyyyMMdd") +"-"; |