@ -36,10 +36,7 @@ import org.springframework.util.StringUtils;
import javax.annotation.Resource ;
import javax.persistence.EntityManager ;
import javax.persistence.Query ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.Map ;
import java.util.StringJoiner ;
import java.util.* ;
import java.util.stream.Collectors ;
@Slf4j
@ -52,17 +49,17 @@ public class MesEquipmentLogService implements IMesEquipmentLogService {
private MesEquipmentVariableRepository equipmentVariableRepository ;
@Autowired
private MesEquipmentLogExtService mesEquipmentLogExtService ;
@Autowired
private MesEquipmentExtService mesEquipmentExtService ;
@Autowired
private MesEquipmentLogDetailRepository mesEquipmentLogDetailRepository ;
private MesEquipmentLogExtService equipmentLogExtService ;
@Autowired
private MesEquipmentLogRepository mesEquipmentLogRepository ;
private MesEquipmentExtService equipmentExtService ;
@Autowired
private MesEquipmentRecordRepository mesEquipmentRecordRepository ;
@Autowired
private SnowflakeIdMaker snowflakeIdMaker ;
@Resource ( name = "redisMesPcn" )
private ImppRedis redisMesPcn ;
@ -75,7 +72,7 @@ public class MesEquipmentLogService implements IMesEquipmentLogService {
@Override
public void saveEquipmentLogToRedis ( ActorMessage actorMessage ) {
// 获取设备变量属性
List < MesEquipmentVariable > equipmentVariableList = m esE quipmentExtService. getEquipmentVariableList ( actorMessage . getOrganizeCode ( ) , actorMessage . getEquipmentCode ( ) , MesExtEnumUtil . EQUIP_VARIABLE_TYPE . PROCESS_FINISH . getValue ( ) ) ;
List < MesEquipmentVariable > equipmentVariableList = equipmentExtService. getEquipmentVariableList ( actorMessage . getOrganizeCode ( ) , actorMessage . getEquipmentCode ( ) , MesExtEnumUtil . EQUIP_VARIABLE_TYPE . PROCESS_FINISH . getValue ( ) ) ;
// 获取变量ID集合
Map < Long , MesEquipmentVariable > equipVariableMap = CollectionUtils . isEmpty ( equipmentVariableList ) ? null : equipmentVariableList . stream ( ) . filter ( o - > null ! = o ) . collect ( Collectors . toMap ( MesEquipmentVariable : : getId , o - > o ) ) ;
if ( CollectionUtils . isEmpty ( equipVariableMap ) ) {
@ -84,7 +81,7 @@ public class MesEquipmentLogService implements IMesEquipmentLogService {
}
// 获取设备数据采集信息
List < MesEquipVariableCollectContext > equipmentLogList = m esE quipmentLogExtService. getEquipmentLogCollectList ( actorMessage . getOrganizeCode ( ) , actorMessage . getEquipId ( ) , MesExtEnumUtil . EQUIP_VARIABLE_NEED_NEW_VALUE . FALSE . getValue ( ) , MesExtEnumUtil . EQUIP_VARIABLE_TYPE . PROCESS_FINISH . getValue ( ) ) ;
List < MesEquipVariableCollectContext > equipmentLogList = equipmentLogExtService. getEquipmentLogCollectList ( actorMessage . getOrganizeCode ( ) , actorMessage . getEquipId ( ) , MesExtEnumUtil . EQUIP_VARIABLE_NEED_NEW_VALUE . FALSE . getValue ( ) , MesExtEnumUtil . EQUIP_VARIABLE_TYPE . PROCESS_FINISH . getValue ( ) ) ;
// 筛选出该设备中设备变量属性对应的数据采集信息
equipmentLogList = CollectionUtils . isEmpty ( equipmentLogList ) ? null : equipmentLogList . stream ( ) . filter ( o - > equipVariableMap . containsKey ( o . getEquipVariableId ( ) ) ) . collect ( Collectors . toList ( ) ) ;
if ( CollectionUtils . isEmpty ( equipmentLogList ) ) {
@ -119,37 +116,44 @@ public class MesEquipmentLogService implements IMesEquipmentLogService {
}
@Override
public void saveEquipmentLogDetails ( String organizeCode ) {
public void saveEquipmentLogDetails ( String organizeCode , String userInfo , Integer qty ) {
DdlPackBean ddlPackBean = DdlPackBean . getDdlPackBean ( organizeCode ) ;
DdlPreparedPack . getNumEqualPack ( MesExtEnumUtil . RECORD_STATUS . RECORD_STATUS_10 . getValue ( ) , "recordStatus" , ddlPackBean ) ;
DdlPreparedPack . getOrderByPack ( new Object [ ] { CommonEnumUtil . ASC_OR_DESC . ASC . getValue ( ) } , new String [ ] { "createDatetime" } , ddlPackBean ) ;
DdlPreparedPack . getNumEqualPack ( MesExtEnumUtil . RECORD_STATUS . RECORD_STATUS_10 . getValue ( ) , MesPcnExtConstWords . RECORD_STATUS , ddlPackBean ) ;
DdlPreparedPack . getOrderByPack ( new Object [ ] { CommonEnumUtil . ASC_OR_DESC . ASC . getValue ( ) } , new String [ ] { MesPcnExtConstWords . CREATE_DATE_TIME } , ddlPackBean ) ;
List < MesEquipmentRecord > mesEquipmentRecords = mesEquipmentRecordRepository . findByHqlTopWhere ( ddlPackBean , 10 ) ;
LOGGER . info ( "保存工艺参数 job --- 读取到设备记录信息[{}]条 ---", CollectionUtils . isEmpty ( mesEquipmentRecords ) ? MesPcnExtConstWords . ZERO : mesEquipmentRecords . size ( ) ) ;
LOGGER . info ( "保存工艺参数 JOB --- 读取到设备记录信息[{}]条 ---", CollectionUtils . isEmpty ( mesEquipmentRecords ) ? MesPcnExtConstWords . ZERO : mesEquipmentRecords . size ( ) ) ;
if ( CollectionUtils . isEmpty ( mesEquipmentRecords ) ) return ;
for ( MesEquipmentRecord mesEquipmentRecord : mesEquipmentRecords ) {
if ( null = = mesEquipmentRecord ) continue ;
saveEquipmentLogDetail ( mesEquipmentRecord) ;
saveEquipmentLogDetail ( organizeCode, userInfo , qty , mesEquipmentRecord) ;
}
}
private void saveEquipmentLogDetail ( MesEquipmentRecord mesEquipmentRecord ) {
private void saveEquipmentLogDetail ( String organizeCode , String userInfo , Integer qty , MesEquipmentRecord mesEquipmentRecord ) {
String key = getKey ( mesEquipmentRecord ) ;
List < MesEquipmentLog > equipmentLogList = ( List < MesEquipmentLog > ) redisMesPcn . getList ( key , 0 , - 1 ) ;
if ( CollectionUtils . isEmpty ( equipmentLogList ) ) {
// 修改状态
mesEquipmentRecord . setRecordStatus ( MesExtEnumUtil . RECORD_STATUS . RECORD_STATUS_20 . getValue ( ) ) ;
mesEquipmentRecordRepository . save ( mesEquipmentRecord ) ;
// 删除key
redisMesPcn . deleteKey ( key ) ;
return ;
}
List < MesEquipmentLogDetail > mesEquipmentLogDetails = new ArrayList < > ( ) ;
// 修改状态
mesEquipmentRecord . setRecordStatus ( CollectionUtils . isEmpty ( equipmentLogList ) ? MesExtEnumUtil . RECORD_STATUS . RECORD_STATUS_20 . getValue ( ) : MesExtEnumUtil . RECORD_STATUS . RECORD_STATUS_30 . getValue ( ) ) ;
ConvertBean . serviceModelUpdate ( mesEquipmentRecord , userInfo ) ;
mesEquipmentRecordRepository . save ( mesEquipmentRecord ) ;
// 删除key
redisMesPcn . deleteKey ( key ) ;
if ( CollectionUtils . isEmpty ( equipmentLogList ) ) return ;
Optional < MesEquipmentLog > optional = equipmentLogList . stream ( ) . filter ( o - > ( null ! = o & & ! StringUtils . isEmpty ( o . getEquipId ( ) ) ) ) . findFirst ( ) ;
if ( null = = optional | | ! optional . isPresent ( ) ) return ;
insertBatchEquipmentLogDetail ( organizeCode , optional . get ( ) . getEquipId ( ) , userInfo , qty , mesEquipmentRecord , equipmentLogList ) ;
for ( MesEquipmentLog mesEquipmentLog : equipmentLogList ) {
MesEquipmentLogDetail mesEquipmentLogDetail = new MesEquipmentLogDetail ( ) ;
@ -163,20 +167,29 @@ public class MesEquipmentLogService implements IMesEquipmentLogService {
mesEquipmentLogDetail . setCreateUser ( null ) ;
mesEquipmentLogDetail . setOrgEquipId ( mesEquipmentLogDetail . getOrganizeCode ( ) + "_" + mesEquipmentLogDetail . getEquipId ( ) ) ;
String jsonStr = mesEquipmentRecord . getProduceSnJson ( ) ;
if ( ! StringUtils . isEmpty ( jsonStr ) ) {
if ( ! StringUtils . isEmpty ( mesEquipmentRecord . getProduceSnJson ( ) ) ) mesEquipmentLogDetail . setExtend ( mesEquipmentRecord . getProduceSnJson ( ) ) ;
mesEquipmentLogDetail . setExtend ( mesEquipmentRecord . getProduceSnJson ( ) ) ;
}
ConvertBean . serviceModelInitialize ( mesEquipmentLogDetail , "job" ) ;
mesEquipmentLogDetails . add ( mesEquipmentLogDetail ) ;
}
mesEquipmentLogDetailRepository . saveAll ( mesEquipmentLogDetails ) ;
// 修改状态
mesEquipmentRecord . setRecordStatus ( MesExtEnumUtil . RECORD_STATUS . RECORD_STATUS_20 . getValue ( ) ) ;
mesEquipmentRecordRepository . save ( mesEquipmentRecord ) ;
// 删除key
redisMesPcn . deleteKey ( key ) ;
}
private void insertBatchEquipmentLogDetail ( String organizeCode , Integer equipId , String userInfo , Integer qty , MesEquipmentRecord mesEquipmentRecord , List < MesEquipmentLog > equipmentLogList ) {
if ( CollectionUtils . isEmpty ( equipmentLogList ) ) return ;
if ( qty . compareTo ( equipmentLogList . size ( ) ) > = 0 ) {
equipmentLogExtService . insertBatchEquipmentLogDetail ( organizeCode , equipId , userInfo , mesEquipmentRecord , equipmentLogList ) ;
equipmentLogList = null ;
} else {
List < MesEquipmentLog > handleList = equipmentLogList . subList ( 0 , qty ) ;
equipmentLogExtService . insertBatchEquipmentLogDetail ( organizeCode , equipId , userInfo , mesEquipmentRecord , handleList ) ;
equipmentLogList = equipmentLogList . subList ( qty , equipmentLogList . size ( ) ) ;
}
insertBatchEquipmentLogDetail ( organizeCode , equipId , userInfo , qty , mesEquipmentRecord , equipmentLogList ) ;
}
@Override
@ -191,12 +204,12 @@ public class MesEquipmentLogService implements IMesEquipmentLogService {
if ( mesEquipmentVariable = = null ) {
return ;
}
// 获取设备点位数据
// 获取设备点位数据 //TODO 暂不使用此代码块 使用需要调整代码 此表已不支持shardingsphere配置
MesEquipmentLog equipmentLog = getMesEquipmentLog ( equipLogMqttMsg , mesEquipmentVariable ) ;
if ( equipmentLog = = null ) {
return ;
}
// 更新设备点位数据
// 更新设备点位数据 //TODO 暂不使用此代码块 使用需要调整代码 此表已不支持shardingsphere配置
updateEquipmentLog ( equipLogMqttMsg , mesEquipmentVariable , equipmentLog ) ;
@ -213,12 +226,12 @@ public class MesEquipmentLogService implements IMesEquipmentLogService {
}
private void updateEquipmentLog ( EquipLogMqttMsg equipLogMqttMsg , MesEquipmentVariable mesEquipmentVariable , MesEquipmentLog equipmentLog ) {
DdlPackBean packBean = DdlPackBean . getDdlPackBean ( mesEquipmentVariable . getOrganizeCode ( ) ) ;
DdlPreparedPack . getNumEqualPack ( equipmentLog . getEquipId ( ) , MesPcnExtConstWords . EQUIP_ID , packBean ) ;
DdlPreparedPack . getNumEqualPack ( equipmentLog . getEquipVariableId ( ) , MesPcnExtConstWords . EQUIP_VARIABLE_ID , packBean ) ;
log . info ( "== MesEquipmentLog修改开始" ) ;
mesEquipmentLogRepository . updateByProperties ( new String [ ] { MesPcnExtConstWords . EQUIP_VARIABLE_STATUS , "equipVariableValue" } , new Object [ ] { MesExtEnumUtil . EQUIP_VARIABLE_NEED_NEW_VALUE . TRUE . getValue ( ) , equipLogMqttMsg . getValue ( ) } , packBean ) ;
log . info ( "== MesEquipmentLog修改结束" ) ;
// DdlPackBean packBean = DdlPackBean.getDdlPackBean(mesEquipmentVariable.getOrganizeCode());
// DdlPreparedPack.getNumEqualPack(equipmentLog.getEquipId(), MesPcnExtConstWords.EQUIP_ID, packBean);
// DdlPreparedPack.getNumEqualPack(equipmentLog.getEquipVariableId(), MesPcnExtConstWords.EQUIP_VARIABLE_ID, packBean);
// log.info("== MesEquipmentLog修改开始");
// mesEquipmentLogRepository.updateByProperties(new String[]{MesPcnExtConstWords.EQUIP_VARIABLE_STATUS,"equipVariableValue"}, new Object[]{MesExtEnumUtil.EQUIP_VARIABLE_NEED_NEW_VALUE.TRUE.getValue(), equipLogMqttMsg.getValue() }, packBean);
// log.info("== MesEquipmentLog修改结束");
}
private MesEquipmentVariable getMesEquipmentVariable ( EquipLogMqttMsg equipLogMqttMsg ) {
@ -245,22 +258,23 @@ public class MesEquipmentLogService implements IMesEquipmentLogService {
}
private MesEquipmentLog getMesEquipmentLog ( EquipLogMqttMsg equipLogMqttMsg , MesEquipmentVariable mesEquipmentVariable ) {
String variableKey = getVariableKey ( equipLogMqttMsg . getPTCode ( ) , "MES_EQUIPMENT_LOG" ) ;
Object obj = redisMesPcn . getHash ( variableKey , equipLogMqttMsg . getKey ( ) ) ;
MesEquipmentLog equipmentLog = new MesEquipmentLog ( ) ;
if ( obj = = null ) {
log . info ( "== MesEquipmentLog查询开始" ) ;
equipmentLog = mesEquipmentLogExtService . queryMesEquipmentLog ( mesEquipmentVariable . getOrganizeCode ( ) , Integer . valueOf ( equipLogMqttMsg . getPTCode ( ) ) , mesEquipmentVariable . getId ( ) ) ;
log . info ( "== MesEquipmentLog查询结束" ) ;
if ( equipmentLog = = null ) {
LOGGER . info ( "log信息不存在 不存在【{}】" , equipLogMqttMsg . getTagAddress ( ) ) ;
return null ;
}
redisMesPcn . putHash ( variableKey , equipLogMqttMsg . getKey ( ) , equipmentLog ) ;
} else {
equipmentLog = ( MesEquipmentLog ) obj ;
}
return equipmentLog ;
// String variableKey = getVariableKey(equipLogMqttMsg.getPTCode(), "MES_EQUIPMENT_LOG");
// Object obj = redisMesPcn.getHash(variableKey, equipLogMqttMsg.getKey());
// MesEquipmentLog equipmentLog = new MesEquipmentLog();
// if (obj == null) {
// log.info("== MesEquipmentLog查询开始");
// equipmentLog = mesEquipmentLogExtService.queryMesEquipmentLog(mesEquipmentVariable.getOrganizeCode(), Integer.valueOf(equipLogMqttMsg.getPTCode()), mesEquipmentVariable.getId());
// log.info("== MesEquipmentLog查询结束");
// if (equipmentLog == null) {
// LOGGER.info("log信息不存在 不存在【{}】", equipLogMqttMsg.getTagAddress());
// return null;
// }
// redisMesPcn.putHash(variableKey, equipLogMqttMsg.getKey(), equipmentLog);
// } else {
// equipmentLog = (MesEquipmentLog) obj;
// }
// return equipmentLog;
return null ;
}
public void saveLogDetail ( MesEquipmentLogDetail mesEquipmentLogDetail ) {