package com.aps.core.service.impl; import cn.hutool.core.util.IdUtil; import com.aps.common.core.utils.DateUtils; import com.aps.common.core.utils.poi.ExcelUtil; import com.aps.common.security.utils.DictUtils; import com.aps.common.security.utils.SecurityUtils; import com.aps.core.domain.ApsGasPipelinePrediction; import com.aps.core.mapper.ApsGasPipelinePredictionMapper; import com.aps.core.service.IApsGasPipelinePredictionService; import lombok.SneakyThrows; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.sql.Timestamp; import java.util.*; import java.util.stream.Collectors; /** * 管路手工气体预测数据Service业务层处理 * * @author ruoyi * @date 2025-05-19 */ @Service public class ApsGasPipelinePredictionServiceImpl implements IApsGasPipelinePredictionService { @Autowired private ApsGasPipelinePredictionMapper apsGasPipelinePredictionMapper; /** * 查询管路手工气体预测数据 * * @param id 管路手工气体预测数据主键 * @return 管路手工气体预测数据 */ @Override public ApsGasPipelinePrediction selectApsGasPipelinePredictionById(Long id) { return apsGasPipelinePredictionMapper.selectApsGasPipelinePredictionById(id); } /** * 查询管路手工气体预测数据列表 * * @param apsGasPipelinePrediction 管路手工气体预测数据 * @return 管路手工气体预测数据 */ @Override public List selectApsGasPipelinePredictionList(ApsGasPipelinePrediction apsGasPipelinePrediction) { return apsGasPipelinePredictionMapper.selectApsGasPipelinePredictionList(apsGasPipelinePrediction); } /** * 新增管路手工气体预测数据 * * @param apsGasPipelinePrediction 管路手工气体预测数据 * @return 结果 */ @Override public int insertApsGasPipelinePrediction(ApsGasPipelinePrediction apsGasPipelinePrediction) { apsGasPipelinePrediction.setCreateTime(DateUtils.getNowDate()); return apsGasPipelinePredictionMapper.insertApsGasPipelinePrediction(apsGasPipelinePrediction); } /** * 修改管路手工气体预测数据 * * @param apsGasPipelinePrediction 管路手工气体预测数据 * @return 结果 */ @Override public int updateApsGasPipelinePrediction(ApsGasPipelinePrediction apsGasPipelinePrediction) { apsGasPipelinePrediction.setUpdateTime(DateUtils.getNowDate()); return apsGasPipelinePredictionMapper.updateApsGasPipelinePrediction(apsGasPipelinePrediction); } /** * 批量删除管路手工气体预测数据 * * @param ids 需要删除的管路手工气体预测数据主键 * @return 结果 */ @Override public int deleteApsGasPipelinePredictionByIds(Long[] ids) { return apsGasPipelinePredictionMapper.deleteApsGasPipelinePredictionByIds(ids); } /** * 删除管路手工气体预测数据信息 * * @param id 管路手工气体预测数据主键 * @return 结果 */ @Override public int deleteApsGasPipelinePredictionById(Long id) { return apsGasPipelinePredictionMapper.deleteApsGasPipelinePredictionById(id); } @SneakyThrows @Override public int batchInsertApsGasPipelinePrediction(MultipartFile file) { ExcelUtil util = new ExcelUtil<>(ApsGasPipelinePrediction.class); List list = util.importExcel(file.getInputStream()); DictUtils.CacheValue cacheValue = DictUtils.getCacheValue("aps_factory"); Set keys = new HashSet<>(); list.forEach(item -> { item.setId(IdUtil.getSnowflakeNextId()); item.setFactory(cacheValue.get(item.getFactory())); item.setCreateBy(SecurityUtils.getUsername()); item.setCreateTime(DateUtils.getNowDate()); }); Collections.reverse(list); list = list.stream().filter(item -> { item.setKey(null); if (!keys.contains(item.getKey())) { keys.add(item.getKey()); return true; } return false; }).collect(Collectors.toList()); if (!list.isEmpty()) { List> facCodeKey = apsGasPipelinePredictionMapper.selectByFacOrMaterial(keys); if (!facCodeKey.isEmpty()) { Iterator it = list.iterator(); while (it.hasNext()) { ApsGasPipelinePrediction item = it.next(); int count = 0; for (Map map : facCodeKey) { if (map.get("key").equals(item.getKey())) { item.setId((Long) map.get("id")); item.setCreateBy(null); item.setCreateTime(null); item.setUpdateBy(SecurityUtils.getUsername()); item.setUpdateTime(new Timestamp(System.currentTimeMillis())); apsGasPipelinePredictionMapper.updateById(item); count++; } } if (count > 0) { it.remove(); } } } if (!list.isEmpty()) { apsGasPipelinePredictionMapper.insert(list); } } return list.size(); } }