package com.jcdm.main.da.paramCollection.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.db.Db; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.jcdm.common.utils.DateUtils; import com.jcdm.main.bs.formula.service.IBsFormulaInfoService; import com.jcdm.main.bs.formula.service.impl.BsFormulaInfoServiceImpl; import com.jcdm.main.bs.formulaChild.domain.BsFormulaChildInfo; import com.jcdm.main.bs.formulaChild.mapper.BsFormulaChildInfoMapper; import com.jcdm.main.bs.formulaChild.service.IBsFormulaChildInfoService; import com.jcdm.main.constant.Constants; import com.jcdm.main.da.collectionParamConf.domain.DaCollectionParamConf; import com.jcdm.main.da.collectionParamConf.mapper.DaCollectionParamConfMapper; import com.jcdm.main.da.collectionParamConf.service.IDaCollectionParamConfService; import com.jcdm.main.da.paramCollection.domain.DaParamCollection; import com.jcdm.main.da.paramCollection.mapper.DaParamCollectionMapper; import com.jcdm.main.da.paramCollection.service.IDaParamCollectionService; import com.jcdm.main.da.passingStationCollection.domain.DaPassingStationCollection; import com.jcdm.main.da.passingStationCollection.mapper.DaPassingStationCollectionMapper; import com.jcdm.main.da.passingStationCollection.service.IDaPassingStationCollectionService; import com.jcdm.main.plcserver.sub.OPCUaSubscription; import com.jcdm.main.restful.qingYan.doman.ChildVO; import com.jcdm.main.restful.qingYan.doman.ParentVO; import com.kangaroohy.milo.model.ReadWriteEntity; import com.kangaroohy.milo.service.MiloService; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.parameters.P; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.text.SimpleDateFormat; import java.time.Instant; import java.util.*; import java.util.stream.Collectors; /** * 设备产品过程参数采集Service业务层处理 * * @author yyt * @date 2023-12-13 */ @Service public class DaParamCollectionServiceImpl extends ServiceImpl implements IDaParamCollectionService { @Autowired private DaParamCollectionMapper daParamCollectionMapper; @Autowired private DaCollectionParamConfMapper daCollectionParamConfMapper; @Autowired private BsFormulaChildInfoMapper bsFormulaChildInfoMapper; @Autowired private IBsFormulaChildInfoService bsFormulaChildInfoService; @Autowired private MiloService miloService; @Autowired private DaPassingStationCollectionMapper daPassingStationCollectionMapper; @Resource private SqlSessionFactory sqlSessionFactory; @Autowired private IDaParamCollectionService daParamCollectionService; @Autowired private IDaCollectionParamConfService daCollectionParamConfService; /** * 查询设备产品过程参数采集 * * @param id 设备产品过程参数采集主键 * @return 设备产品过程参数采集 */ @Override public DaParamCollection selectDaParamCollectionById(Long id) { return daParamCollectionMapper.selectDaParamCollectionById(id); } /** * 查询设备产品过程参数采集列表 * * @param daParamCollection 设备产品过程参数采集 * @return 设备产品过程参数采集 */ @Override public List selectDaParamCollectionList(DaParamCollection daParamCollection) { return daParamCollectionMapper.selectDaParamCollectionList(daParamCollection); } @Override public void saveBeachDaParamCollection(List list) { // ExecutorType.SIMPLE: 这个执行器类型不做特殊的事情。它为每个语句的执行创建一个新的预处理语句。 // ExecutorType.REUSE: 这个执行器类型会复用预处理语句。 // ExecutorType.BATCH: 这个执行器会批量执行所有更新语句,如果 SELECT 在它们中间执行还会标定它们是 必须的,来保证一个简单并易于理解的行为。 // 关闭session的自动提交 SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false); try { DaParamCollectionMapper userMapper = sqlSession.getMapper(DaParamCollectionMapper.class); list.stream().forEach(DaParamCollection -> userMapper.insertDaParamCollection(DaParamCollection)); // 提交数据 sqlSession.commit(); sqlSession.rollback(); } catch (Exception e) { sqlSession.rollback(); } finally { sqlSession.close(); } } /** * 新增设备产品过程参数采集 * * @param daParamCollection 设备产品过程参数采集 * @return 结果 */ @Override public int insertDaParamCollection(DaParamCollection daParamCollection) { daParamCollection.setCreateTime(DateUtils.getNowDate()); return daParamCollectionMapper.insertDaParamCollection(daParamCollection); } /** * 修改设备产品过程参数采集 * * @param daParamCollection 设备产品过程参数采集 * @return 结果 */ @Override public int updateDaParamCollection(DaParamCollection daParamCollection) { daParamCollection.setUpdateTime(DateUtils.getNowDate()); return daParamCollectionMapper.updateDaParamCollection(daParamCollection); } /** * 批量删除设备产品过程参数采集 * * @param ids 需要删除的设备产品过程参数采集主键 * @return 结果 */ @Override public int deleteDaParamCollectionByIds(Long[] ids) { return daParamCollectionMapper.deleteDaParamCollectionByIds(ids); } /** * 删除设备产品过程参数采集信息 * * @param id 设备产品过程参数采集主键 * @return 结果 */ @Override public int deleteDaParamCollectionById(Long id) { return daParamCollectionMapper.deleteDaParamCollectionById(id); } @Override public void addBasicParameters(DaParamCollection daParamCollection) { // 假设这是从数据库或其他地方获取的参数数据 Map map = new HashMap<>(); map.put("GC", "南浔工厂"); map.put("CXBH", "Pack线"); map.put("SBBH", "设备001"); map.put("YGBH", "员工001"); map.put("GDBH", daParamCollection.getWorkOrderNo()); map.put("CPXH", daParamCollection.getModel()); map.put("INT", DateUtil.formatDateTime(new Date())); map.forEach((key, value) -> { DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf(); daCollectionParamConf.setProcessesCode(daParamCollection.getLocationCode()); daCollectionParamConf.setCollectParameterId(key); List daCollectionParamConfs = daCollectionParamConfMapper.selectDaCollectionParamConfList(daCollectionParamConf); DaParamCollection saveData = new DaParamCollection(); saveData.setWorkOrderNo(daParamCollection.getWorkOrderNo()); saveData.setProductCode(daParamCollection.getProductCode()); saveData.setLocationCode(daParamCollection.getLocationCode()); saveData.setSfcCode(daParamCollection.getProductBarcode()); saveData.setParamCode(daCollectionParamConfs.get(0).getCollectParameterId()); saveData.setParamName(daCollectionParamConfs.get(0).getCollectParameterName()); saveData.setCollectionTime(new Date()); saveData.setParamValue(value); daParamCollectionMapper.insertDaParamCollection(saveData); }); } @Override public void addTighteningParameters(DaParamCollection daParamCollection) { String tightenData = daParamCollection.getTightenTheArray(); tightenData = tightenData.replace("[", "").replace("]", "").replace(" ", ""); String[] tightenDataParts = tightenData.split(","); String paramCode = daParamCollection.getParamCode(); String[] paramCodeParts = paramCode.split(","); for (int i = 0; i < paramCodeParts.length; i++) { daParamCollection.setParamValue(tightenDataParts[i]); daParamCollection.setParamCode(paramCodeParts[i]); daParamCollection.setCollectionTime(new Date()); daParamCollection.setSfcCode(daParamCollection.getProductBarcode()); daParamCollectionMapper.insertDaParamCollection(daParamCollection); } } @Override public void saveCampaignTimeParameters(DaParamCollection daParamCollection) { List list = bsFormulaChildInfoService.list(new LambdaQueryWrapper() .eq(BsFormulaChildInfo::getProcessesCode, daParamCollection.getLocationCode()) .eq(BsFormulaChildInfo::getSpareField4, "1") ); if(!list.get(0).getResults().equals("OK")){ try { miloService.writeToOpcShort(ReadWriteEntity.builder().identifier("PACK."+daParamCollection.getLocationCode()+".RecordDataDone").value(22).build()); } catch (Exception e) { throw new RuntimeException(e); } return; } BsFormulaChildInfo bsFormulaChildInfo = new BsFormulaChildInfo(); bsFormulaChildInfo.setProcessesCode(daParamCollection.getLocationCode()); bsFormulaChildInfo.setProductCode(daParamCollection.getProductCode()); bsFormulaChildInfo.setSpareField4("1"); List bsFormulaChildInfos = bsFormulaChildInfoMapper.selectBsFormulaChildInfoList(bsFormulaChildInfo); String result = bsFormulaChildInfos.get(0).getResults(); if(result != null && !result.isEmpty()){ try { //过站参数采集记录出站时间 DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf(); daCollectionParamConf.setProcessesCode(daParamCollection.getLocationCode()); daCollectionParamConf.setCollectParameterId("OUTT"); List daCollectionParamConfs = daCollectionParamConfMapper.selectDaCollectionParamConfList(daCollectionParamConf); DaParamCollection saveData = new DaParamCollection(); saveData.setWorkOrderNo(daParamCollection.getWorkOrderNo()); saveData.setProductCode(daParamCollection.getProductCode()); saveData.setLocationCode(daParamCollection.getLocationCode()); saveData.setSfcCode(daParamCollection.getProductBarcode()); saveData.setParamCode(daCollectionParamConfs.get(0).getCollectParameterId()); saveData.setParamName(daCollectionParamConfs.get(0).getCollectParameterName()); saveData.setCollectionTime(new Date()); saveData.setParamValue(DateUtil.formatDateTime(new Date())); daParamCollectionMapper.insertDaParamCollection(saveData); //更新过站记录表出站时间 DaPassingStationCollection daPassingStationCollection = new DaPassingStationCollection(); daPassingStationCollection.setWorkOrderNo(daParamCollection.getWorkOrderNo()); daPassingStationCollection.setLocationCode(daParamCollection.getLocationCode()); List daPassingStationCollections = daPassingStationCollectionMapper.selectDaPassingStationCollectionList(daPassingStationCollection); daPassingStationCollections.get(0).setOutboundTime(new Date()); int i = daPassingStationCollectionMapper.updateDaPassingStationCollection(daPassingStationCollections.get(0)); // OPCUaSubscription.SaveParamData(daParamCollection.getProductBarcode(),"OP",daParamCollection.getLocationCode(),daParamCollection.getWorkOrderNo(),daParamCollection.getProductCode()); //给opc发21 miloService.writeToOpcShort(ReadWriteEntity.builder().identifier("PACK."+daParamCollection.getLocationCode()+".RecordDataDone").value(21).build()); } catch (Exception e) { throw new RuntimeException(e); } } } @Override public void insertBatch(List confList){ try{ this.saveBatch(confList); }catch (Exception e){ return; } } @Override public void replaceAssemblyCode(DaParamCollection daParamCollection) { List list = daParamCollectionService.list(new LambdaQueryWrapper() .eq(DaParamCollection::getSfcCode, daParamCollection.getYzSfcCode()) ); if(list.size() > 0){ for (DaParamCollection paramCollection : list) { paramCollection.setSfcCode(daParamCollection.getSfcCode()); daParamCollectionService.saveOrUpdate(paramCollection); } } } @Override public void yzAddBasicParameters(DaParamCollection daParamCollection) { // 假设这是从数据库或其他地方获取的参数数据 Map map = new HashMap<>(); // map.put("GC", "南浔工厂"); // map.put("CXBH", "Pack线"); // map.put("SBBH", "设备001"); // map.put("YGBH", "员工001"); // map.put("GDBH", daParamCollection.getWorkOrderNo()); // map.put("CPXH", daParamCollection.getModel()); map.put("INT", DateUtil.formatDateTime(new Date())); map.forEach((key, value) -> { DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf(); daCollectionParamConf.setProcessesCode(daParamCollection.getLocationCode()); daCollectionParamConf.setCollectParameterId(key); List daCollectionParamConfs = daCollectionParamConfMapper.selectDaCollectionParamConfList(daCollectionParamConf); DaParamCollection saveData = new DaParamCollection(); saveData.setWorkOrderNo(daParamCollection.getWorkOrderNo()); saveData.setProductCode(daParamCollection.getProductCode()); saveData.setLocationCode(daParamCollection.getLocationCode()); saveData.setSfcCode(daParamCollection.getProductBarcode()); saveData.setParamCode(daCollectionParamConfs.get(0).getCollectParameterId()); saveData.setParamName(daCollectionParamConfs.get(0).getCollectParameterName()); saveData.setCollectionTime(new Date()); saveData.setParamValue(value); daParamCollectionMapper.insertDaParamCollection(saveData); }); } @Override public void jrmSaveCampaignTimeParameters(DaParamCollection daParamCollection) { DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf(); daCollectionParamConf.setProcessesCode(daParamCollection.getLocationCode()); daCollectionParamConf.setCollectParameterId("OUTT"); List daCollectionParamConfs = daCollectionParamConfMapper.selectDaCollectionParamConfList(daCollectionParamConf); DaParamCollection saveData = new DaParamCollection(); saveData.setWorkOrderNo(daParamCollection.getWorkOrderNo()); saveData.setProductCode(daParamCollection.getProductCode()); saveData.setLocationCode(daParamCollection.getLocationCode()); saveData.setSfcCode(daParamCollection.getProductBarcode()); saveData.setParamCode(daCollectionParamConfs.get(0).getCollectParameterId()); saveData.setParamName(daCollectionParamConfs.get(0).getCollectParameterName()); saveData.setCollectionTime(new Date()); saveData.setParamValue(DateUtil.formatDateTime(new Date())); daParamCollectionMapper.insertDaParamCollection(saveData); //更新过站记录表出站时间 DaPassingStationCollection daPassingStationCollection = new DaPassingStationCollection(); daPassingStationCollection.setWorkOrderNo(daParamCollection.getWorkOrderNo()); daPassingStationCollection.setLocationCode(daParamCollection.getLocationCode()); List daPassingStationCollections = daPassingStationCollectionMapper.selectDaPassingStationCollectionList(daPassingStationCollection); daPassingStationCollections.get(0).setOutboundTime(new Date()); int i = daPassingStationCollectionMapper.updateDaPassingStationCollection(daPassingStationCollections.get(0)); // OPCUaSubscription.SaveParamData(daParamCollection.getProductBarcode(),"OP",daParamCollection.getLocationCode(),daParamCollection.getWorkOrderNo(),daParamCollection.getProductCode()); //给opc发21 try { // miloService.writeToOpcShort(ReadWriteEntity.builder().identifier("PACK."+daParamCollection.getLocationCode()+".RecordDataDone").value(21).build()); String strA = daParamCollection.getLocationCode() + "A"; String strB = daParamCollection.getLocationCode() + "B"; miloService.writeToOpcShort(ReadWriteEntity.builder().identifier("PACK."+strA+".RecordDataDone").value(21).build()); miloService.writeToOpcShort(ReadWriteEntity.builder().identifier("PACK."+strB+".RecordDataDone").value(21).build()); } catch (Exception e) { throw new RuntimeException(e); } } public void sendToFactoryMes(String stationCode,String productNum ){ String url = "https://imes-uat-group.geelycv-test.com/api/mom-open/restful/aMesSysIntegration/deviceResultFeedback"; ParentVO vo =new ParentVO(); List checkList = new ArrayList<>(); List list = this.list(new LambdaQueryWrapper() .eq(DaParamCollection::getSfcCode, productNum) .eq(DaParamCollection::getLocationCode, stationCode)); vo.setTotalResult("1"); String string = new Random(10).toString(); vo.setRecordId(string); vo.setProductNum(productNum); vo.setSiteCode("3983"); vo.setStationCode(stationCode); if (CollUtil.isNotEmpty(list)){ List collect = list.stream().filter(x -> Constants.NG.equals(x.getParamValue())).collect(Collectors.toList()); if (CollUtil.isNotEmpty(collect)){ vo.setTotalResult("0"); } List bsFormulaChildInfoList = bsFormulaChildInfoService.list(); for (DaParamCollection daParamCollection : list) { ChildVO childVO = new ChildVO(); childVO.setItemCode(daParamCollection.getParamCode()); String time = daParamCollection.getCollectionTime() != null ? daParamCollection.getCollectionTime().toString() : ""; childVO.setCheckResult("1"); if (StrUtil.isNotBlank(daParamCollection.getParamValue())){ if (Constants.NG.equals(daParamCollection.getParamValue())){ childVO.setCheckResult("0"); } } childVO.setCheckTime(time); childVO.setItemValue(daParamCollection.getParamValue()); childVO.setItemType("3"); childVO.setItemText(daParamCollection.getParamName()); if (StrUtil.isNotBlank(daParamCollection.getParamCode())){ List collect1 = bsFormulaChildInfoList.stream().filter(x -> daParamCollection.getParamCode().equals(x.getParamCode())).collect(Collectors.toList()); if (CollUtil.isNotEmpty(collect1)){ BsFormulaChildInfo bsFormulaChildInfo = collect1.get(0); childVO.setItemType(bsFormulaChildInfo.getOperationType()); } } checkList.add(childVO); } } vo.setCheckList(checkList); HttpResponse execute = HttpRequest.post(url).body(JSONUtil.toJsonStr(vo)).execute(); } @Override public void pushGeelycvMesFeedback(String packID, String stationCode) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String url = "https://imes-uat-group.geelycv-test.com/api/mom-open/restful/aMesSysIntegration/deviceResultFeedback"; String totalResult = "0"; List paramList = daParamCollectionService.list(new LambdaQueryWrapper().eq(DaParamCollection::getSfcCode, packID).eq(DaParamCollection::getLocationCode, stationCode)); if(paramList.size() > 0){ ParentVO parentVO = new ParentVO(); parentVO.setSiteCode("3983"); parentVO.setRecordId(String.valueOf(Instant.now().toEpochMilli())); parentVO.setStationCode(stationCode); parentVO.setProductNum(packID); parentVO.setTotalResult("1"); List listChildVo = new ArrayList<>(); for (DaParamCollection daParamCollection : paramList) { ChildVO childVO = new ChildVO(); childVO.setItemCode(daParamCollection.getParamCode()); BsFormulaChildInfo childOne = bsFormulaChildInfoService.getOne(new LambdaQueryWrapper().eq(BsFormulaChildInfo::getParamCode, daParamCollection.getParamCode())); DaCollectionParamConf paramConfOne = daCollectionParamConfService.getOne(new LambdaQueryWrapper().eq(DaCollectionParamConf::getCollectParameterId, daParamCollection.getParamCode())); if(childOne!=null){ String operationType = childOne.getOperationType(); if(operationType.equals("1")){ childVO.setItemType("2"); }else if(operationType.equals("2")){ childVO.setItemType("1"); } }else { childVO.setItemType("3"); } if(paramConfOne!=null){ childVO.setItemText(paramConfOne.getCollectParameterName()); }else { childVO.setItemText(""); } childVO.setItemValue(daParamCollection.getParamValue()); if(daParamCollection.getParamValue().equals("1")){ childVO.setCheckResult("1"); }else if(daParamCollection.getParamValue().equals("2")){ childVO.setCheckResult("0"); parentVO.setTotalResult("0"); totalResult = "1"; }else { childVO.setCheckResult("1"); } childVO.setCheckTime(format.format(daParamCollection.getCollectionTime())); listChildVo.add(childVO); } parentVO.setCheckList(listChildVo); HttpResponse execute = HttpRequest.post(url).body(JSONUtil.toJsonStr(parentVO)).execute(); System.out.println(execute.body()); System.out.println("-----------------------"+totalResult); } } }