package com.iailab.module.model.mcs.pre.service.impl; import com.baomidou.dynamic.datasource.annotation.DSTransactional; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.iailab.framework.common.pojo.PageResult; import com.iailab.framework.common.util.object.BeanUtils; import com.iailab.module.model.mcs.pre.dao.MmPredictItemDao; import com.iailab.module.model.mcs.pre.dto.MmPredictItemDTO; import com.iailab.module.model.mcs.pre.entity.*; import com.iailab.module.model.mcs.pre.enums.ItemIncreaseCodeEnum; import com.iailab.module.model.mcs.pre.enums.ItemTypeEnum; import com.iailab.module.model.mcs.pre.service.MmPredictItemService; import com.iailab.module.model.mcs.pre.service.MmSequenceNumService; import com.iailab.module.model.mcs.pre.vo.MmPredictItemPageReqVO; import com.iailab.module.model.mcs.pre.vo.MmPredictItemRespVO; import com.iailab.module.model.mdk.vo.ItemVO; import com.iailab.module.model.mdk.vo.MergeItemVO; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.util.*; /** * @author PanZhibao * @date 2021年04月26日 14:03 */ @Service public class MmPredictItemServiceImpl extends ServiceImpl implements MmPredictItemService { @Autowired private DmModuleItemServiceImpl dmModuleItemService; @Autowired private MmItemOutputServiceImpl mmItemOutputService; @Autowired private MmPredictModelServiceImpl mmPredictModelService; @Autowired private MmModelArithSettingsServiceImpl mmModelArithSettingsService; @Autowired private MmModelParamServiceImpl mmModelParamService; @Autowired private MmPredictMergeItemServiceImpl mmPredictMergeItemService; @Autowired private MmItemTypeImpl mmItemTypeImpl; @Autowired private MmSequenceNumService sequenceNumService; @Autowired private MmPredictItemDao mmPredictItemDao; private static HashMap itemVOHashMap = new HashMap<>(); @Override public PageResult queryPage(MmPredictItemPageReqVO reqVO) { IPage page = mmPredictItemDao.selectPage(reqVO); return new PageResult<>(page.getRecords(), page.getTotal()); } @Override public List list(Map params) { return mmPredictItemDao.queryList(params); } @Override public MmPredictItemEntity getById(String id) { return mmPredictItemDao.selectById(id); } @DSTransactional(rollbackFor = Exception.class) @Override public void add(MmPredictItemDTO mmPredictItemDto) { MmPredictItemEntity predictItem = mmPredictItemDto.getMmPredictItem(); predictItem.setId(UUID.randomUUID().toString()); predictItem.setCreateTime(new Date()); predictItem.setUpdateTime(new Date()); MmItemTypeEntity itemType = mmItemTypeImpl.getById(predictItem.getItemtypeid()); if (itemType != null && ItemTypeEnum.NORMAL_ITEM.getName().equals(itemType.getItemtypename())) { MmSequenceNumEntity sequenceNumEntity = sequenceNumService.getAndIncreaseByCode(ItemIncreaseCodeEnum.IM.toString()); String str = String.format("%010d", sequenceNumEntity.getSequenceNum()); predictItem.setItemno(sequenceNumEntity.getPrefix() + str); MmPredictModelEntity mmPredictModel = mmPredictItemDto.getMmPredictModel(); MmSequenceNumEntity preMdlSequenceNumEntity = sequenceNumService.getAndIncreaseByCode(ItemIncreaseCodeEnum.PRE_MDL.toString()); String preMdlStr = String.format("%04d", preMdlSequenceNumEntity.getSequenceNum()); mmPredictModel.setModelno(preMdlSequenceNumEntity.getPrefix() + preMdlStr); mmPredictModel.setItemid(predictItem.getId()); mmPredictModelService.savePredictModel(mmPredictModel); List mmModelArithSettingsList = mmPredictItemDto.getMmModelArithSettingsList(); mmModelArithSettingsList.forEach(item -> { item.setModelid(mmPredictModel.getId()); }); mmModelArithSettingsService.saveList(mmModelArithSettingsList); List mmModelParamList = mmPredictItemDto.getMmModelParamList(); mmModelParamList.forEach(item -> { item.setModelid(mmPredictModel.getId()); }); mmModelParamService.saveList(mmModelParamList); } else if (itemType != null && ItemTypeEnum.MERGE_ITEM.getName().equals(itemType.getItemtypename())) { MmSequenceNumEntity sequenceNumEntity = sequenceNumService.getAndIncreaseByCode(ItemIncreaseCodeEnum.IC.toString()); String str = String.format("%010d", sequenceNumEntity.getSequenceNum()); predictItem.setItemno(sequenceNumEntity.getPrefix() + str); MmPredictMergeItemEntity mMmPredictMergeItem = mmPredictItemDto.getMmPredictMergeItem(); mMmPredictMergeItem.setItemid(predictItem.getId()); mmPredictMergeItemService.savePredictMergeItem(mMmPredictMergeItem); } mmPredictItemDao.insert(predictItem); DmModuleItemEntity dmModuleItem = mmPredictItemDto.getDmModuleItem(); dmModuleItem.setItemid(predictItem.getId()); dmModuleItemService.saveModuleItem(dmModuleItem); List mmItemOutput = mmPredictItemDto.getMmItemOutputList(); mmItemOutput.forEach(e -> { e.setId(UUID.randomUUID().toString()); e.setItemid(predictItem.getId()); }); mmItemOutputService.saveMmItemOutput(mmItemOutput); // 清空缓存 clearCatch(); } @DSTransactional(rollbackFor = Exception.class) @Override public void update(MmPredictItemDTO mmPredictItemDto) { MmPredictItemEntity predictItem = mmPredictItemDto.getMmPredictItem(); predictItem.setUpdateTime(new Date()); mmPredictItemDao.updateById(predictItem); MmItemTypeEntity itemType = mmItemTypeImpl.getById(predictItem.getItemtypeid()); if (itemType != null && ItemTypeEnum.NORMAL_ITEM.getName().equals(itemType.getItemtypename())) { MmPredictModelEntity mmPredictModel = mmPredictItemDto.getMmPredictModel(); mmPredictModelService.update(mmPredictModel); List mmModelArithSettingsList = mmPredictItemDto.getMmModelArithSettingsList(); mmModelArithSettingsList.forEach(item -> { item.setModelid(mmPredictModel.getId()); }); mmModelArithSettingsService.saveList(mmModelArithSettingsList); List mmModelParamList = mmPredictItemDto.getMmModelParamList(); mmModelParamList.forEach(item -> { item.setModelid(mmPredictModel.getId()); }); mmModelParamService.saveList(mmModelParamList); } else if (itemType != null && ItemTypeEnum.MERGE_ITEM.getName().equals(itemType.getItemtypename())) { MmPredictMergeItemEntity mMmPredictMergeItem = mmPredictItemDto.getMmPredictMergeItem(); mmPredictMergeItemService.update(mMmPredictMergeItem); } DmModuleItemEntity dmModuleItem = mmPredictItemDto.getDmModuleItem(); if (!"".equals(dmModuleItem.getId()) && dmModuleItem.getId() != null) { dmModuleItemService.update(dmModuleItem); } mmItemOutputService.deleteByItemId(predictItem.getId()); List mmItemOutput = mmPredictItemDto.getMmItemOutputList(); if (!CollectionUtils.isEmpty(mmItemOutput)) { mmItemOutput.forEach(e -> { // 尽量保存原有id if (StringUtils.isBlank(e.getId())) { e.setId(UUID.randomUUID().toString()); } e.setItemid(predictItem.getId()); }); mmItemOutputService.saveMmItemOutput(mmItemOutput); } // 清空缓存 clearCatch(); } @DSTransactional(rollbackFor = Exception.class) @Override public void deleteBatch(String[] itemIds) { mmPredictItemDao.deleteBatchIds(Arrays.asList(itemIds)); mmPredictMergeItemService.deleteBatch(itemIds); mmPredictModelService.deleteBatch(itemIds); mmItemOutputService.deleteBatch(itemIds); // 清空缓存 clearCatch(); } @Override public MmPredictItemDTO info(String id) { MmPredictItemEntity predictItem = mmPredictItemDao.selectById(id); MmPredictItemDTO mmPredictItemDto = BeanUtils.toBean(predictItem, MmPredictItemDTO.class); mmPredictItemDto.setMmPredictItem(predictItem); mmPredictItemDto.setDmModuleItem(dmModuleItemService.getByItemid(id)); mmPredictItemDto.setMmItemOutputList(mmItemOutputService.getByItemid(id)); MmItemTypeEntity itemType = mmItemTypeImpl.getById(predictItem.getItemtypeid()); if (itemType != null && ItemTypeEnum.NORMAL_ITEM.getName().equals(itemType.getItemtypename())) { MmPredictModelEntity mmPredictModel = mmPredictModelService.getByItemid(id); mmPredictItemDto.setMmPredictModel(mmPredictModel); mmPredictItemDto.setMmModelArithSettingsList(mmModelArithSettingsService.getByModelId(mmPredictModel.getId())); mmPredictItemDto.setMmModelParamList(mmModelParamService.getByModelid(mmPredictModel.getId())); mmPredictItemDto.setMmPredictMergeItem(new MmPredictMergeItemEntity()); } else if (itemType != null && ItemTypeEnum.MERGE_ITEM.getName().equals(itemType.getItemtypename())) { mmPredictItemDto.setMmPredictModel(new MmPredictModelEntity()); mmPredictItemDto.setMmModelArithSettingsList(new ArrayList<>()); mmPredictItemDto.setMmModelParamList(new ArrayList<>()); mmPredictItemDto.setMmPredictMergeItem(new MmPredictMergeItemEntity()); mmPredictItemDto.setMmPredictMergeItem(mmPredictMergeItemService.getByItemid(id)); } return mmPredictItemDto; } @Override public int check(MmPredictItemEntity mmPredictItem) { String id = mmPredictItem.getId(); String itemname = mmPredictItem.getItemname(); String itemno = mmPredictItem.getItemno(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.ne(StringUtils.isNotBlank(id), "id", id); queryWrapper.and(wrapper -> wrapper.eq("itemname", itemname).or().eq("itemno", itemno)); List list = mmPredictItemDao.selectList(queryWrapper); return list.size(); } @Override public List getByModuleId(String moduleId) { Map params = new HashMap<>(); params.put("MODULEID",moduleId); return mmPredictItemDao.getByModuleId(params); } @Override public ItemVO getItemByItemNo(String itemNo) { if (StringUtils.isBlank(itemNo)) { return null; } Map params = new HashMap(1); params.put("ITEMNO", itemNo); List list = mmPredictItemDao.getItem(params); if (CollectionUtils.isEmpty(list)) { return null; } return list.get(0); } @Override public void clearCatch() { itemVOHashMap.clear(); } @Override public ItemVO getItemById(String itemId) { if (StringUtils.isBlank(itemId)) { return null; } if (!itemVOHashMap.containsKey(itemId)) { Map params = new HashMap<>(1); params.put("ITEMID", itemId); List list = mmPredictItemDao.getItem(params); if (CollectionUtils.isEmpty(list)) { return null; } itemVOHashMap.put(itemId, list.get(0)); } return itemVOHashMap.get(itemId); } @Override public ItemVO getItemByOutPutId(String outPutId) { if (StringUtils.isBlank(outPutId)) { return null; } MmItemOutputEntity outPutById = mmItemOutputService.getOutPutById(outPutId); if (ObjectUtils.isEmpty(outPutById)) { return null; } String itemId = outPutById.getItemid(); return getItemById(itemId); } @Override public MergeItemVO getMergeItemByItemId(String itemId) { if (StringUtils.isBlank(itemId)) { return null; } Map params = new HashMap(1); params.put("ITEMID", itemId); List list = mmPredictItemDao.getMergeItemByItemId(params); if (CollectionUtils.isEmpty(list)) { return null; } return list.get(0); } }