|  |  | 
 |  |  | package doumeemes.service.business.impl; | 
 |  |  |  | 
 |  |  | import com.baomidou.mybatisplus.extension.api.R; | 
 |  |  | 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 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; | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public Integer create(Device device) { | 
 |  |  | 
 |  |  |             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(), "导入数据内容有误!"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         List<String> departName = new ArrayList<>(); | 
 |  |  |         List<String> procedureName = new ArrayList<>(); | 
 |  |  |         plansList.forEach(s->{ | 
 |  |  |  | 
 |  |  |             if (StringUtils.isBlank(s.getDeviceName()) | 
 |  |  | 
 |  |  |                     || StringUtils.isBlank(s.getFinishWarehouseLocationName())){ | 
 |  |  |                 throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"导入数据内容有误"); | 
 |  |  |             } | 
 |  |  |             departName.add(s.getDepartName()); | 
 |  |  |             procedureName.add(s.getProcedureName()); | 
 |  |  |         }); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         QueryWrapper<Department> wrapper = new QueryWrapper<>(); | 
 |  |  |         wrapper.lambda().eq(Department::getRootId,user.getRootDepartment().getId()); | 
 |  |  |         departmentMapper.selectList(wrapper); | 
 |  |  |  | 
 |  |  |         plansList.forEach(s->{ | 
 |  |  |  | 
 |  |  |             QueryWrapper<Department> wrapper = new QueryWrapper<>(); | 
 |  |  |             wrapper.lambda() | 
 |  |  |                     .eq(Department::getRootId,user.getRootDepartment().getId()) | 
 |  |  |                     .eq(Department::getName,departName) | 
 |  |  |                     .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::getDepartId,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 | 
 |  |  |                                             ,Arrays.asList(s.getFinishWarehouseLocationName(),s.getProduceWarehouseLocationName())); | 
 |  |  |             if(Objects.equals(list.size(),2)){ | 
 |  |  |                 throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"库位信息已存在"); | 
 |  |  |             } | 
 |  |  |             List<WarehouseLocation> warehouseLocations = warehouseLocationMapper.selectList(warehouseLocationQuery); | 
 |  |  |  | 
 |  |  |             Map<String, WarehouseLocation> collect = warehouseLocations.stream().collect(Collectors.toMap(w -> w.getUnionName(), w -> w)); | 
 |  |  |  | 
 |  |  |             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.setRootDepartId(department.getRootId()); | 
 |  |  |             device.setDepartId(procedures.getId()); | 
 |  |  |             device.setProcedureId(procedures.getId()); | 
 |  |  |  | 
 |  |  |             device.setCode(StringUtils.isNotBlank(device.getCode()) ? device.getCode() : this.getNextCode(user.getCompany().getId())); | 
 |  |  |             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.setSupplier(); | 
 |  |  | //            device.setStationCode(); | 
 |  |  |             device.setStationCode(s.getStationCode()); | 
 |  |  | //            device.setGroupId(); | 
 |  |  | //            device.setSerialNum(); | 
 |  |  | //            device.setProduceWarehouseLocationId(); | 
 |  |  | //            device.setProduceWarehouseId(); | 
 |  |  | //            device.setFinishWarehouseLocationId(); | 
 |  |  | //            device.setFinishWarehouseId(); | 
 |  |  | //            device.setUserIds(); | 
 |  |  |             device.setProduceWarehouseLocationId(collect.get(s.getProduceWarehouseLocationName()).getId()); | 
 |  |  |             device.setFinishWarehouseLocationId(collect.get(s.getFinishWarehouseLocationName()).getId()); | 
 |  |  |             deviceMapper.insert(device); | 
 |  |  |             if(StringUtils.isNotBlank(s.getUserIds())){ | 
 |  |  |                 this.dealDeviceUserId(Constants.ZERO,device.getUserIds().trim(),device.getId(),user); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |         }); | 
 |  |  |     } | 
 |  |  |  |