sfd
2025-05-26 2a64b537e8e3bce9ce030585a3da17d48379c0ad
aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsGasPipelinePredictionServiceImpl.java
@@ -2,151 +2,151 @@
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 io.micrometer.common.util.StringUtils;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
 * 管路手工气体预测数据Service业务层处理
 *
 *
 * @author ruoyi
 * @date 2025-05-19
 */
@Service
public class ApsGasPipelinePredictionServiceImpl implements IApsGasPipelinePredictionService
{
public class ApsGasPipelinePredictionServiceImpl implements IApsGasPipelinePredictionService {
    @Autowired
    private ApsGasPipelinePredictionMapper apsGasPipelinePredictionMapper;
    /**
     * 查询管路手工气体预测数据
     *
     *
     * @param id 管路手工气体预测数据主键
     * @return 管路手工气体预测数据
     */
    @Override
    public ApsGasPipelinePrediction selectApsGasPipelinePredictionById(Long id)
    {
    public ApsGasPipelinePrediction selectApsGasPipelinePredictionById(Long id) {
        return apsGasPipelinePredictionMapper.selectApsGasPipelinePredictionById(id);
    }
    /**
     * 查询管路手工气体预测数据列表
     *
     *
     * @param apsGasPipelinePrediction 管路手工气体预测数据
     * @return 管路手工气体预测数据
     */
    @Override
    public List<ApsGasPipelinePrediction> selectApsGasPipelinePredictionList(ApsGasPipelinePrediction apsGasPipelinePrediction)
    {
    public List<ApsGasPipelinePrediction> selectApsGasPipelinePredictionList(ApsGasPipelinePrediction apsGasPipelinePrediction) {
        return apsGasPipelinePredictionMapper.selectApsGasPipelinePredictionList(apsGasPipelinePrediction);
    }
    /**
     * 新增管路手工气体预测数据
     *
     *
     * @param apsGasPipelinePrediction 管路手工气体预测数据
     * @return 结果
     */
    @Override
    public int insertApsGasPipelinePrediction(ApsGasPipelinePrediction apsGasPipelinePrediction)
    {
    public int insertApsGasPipelinePrediction(ApsGasPipelinePrediction apsGasPipelinePrediction) {
        apsGasPipelinePrediction.setCreateTime(DateUtils.getNowDate());
        return apsGasPipelinePredictionMapper.insertApsGasPipelinePrediction(apsGasPipelinePrediction);
    }
    /**
     * 修改管路手工气体预测数据
     *
     *
     * @param apsGasPipelinePrediction 管路手工气体预测数据
     * @return 结果
     */
    @Override
    public int updateApsGasPipelinePrediction(ApsGasPipelinePrediction apsGasPipelinePrediction)
    {
    public int updateApsGasPipelinePrediction(ApsGasPipelinePrediction apsGasPipelinePrediction) {
        apsGasPipelinePrediction.setUpdateTime(DateUtils.getNowDate());
        return apsGasPipelinePredictionMapper.updateApsGasPipelinePrediction(apsGasPipelinePrediction);
    }
    /**
     * 批量删除管路手工气体预测数据
     *
     *
     * @param ids 需要删除的管路手工气体预测数据主键
     * @return 结果
     */
    @Override
    public int deleteApsGasPipelinePredictionByIds(Long[] ids)
    {
    public int deleteApsGasPipelinePredictionByIds(Long[] ids) {
        return apsGasPipelinePredictionMapper.deleteApsGasPipelinePredictionByIds(ids);
    }
    /**
     * 删除管路手工气体预测数据信息
     *
     *
     * @param id 管路手工气体预测数据主键
     * @return 结果
     */
    @Override
    public int deleteApsGasPipelinePredictionById(Long id)
    {
    public int deleteApsGasPipelinePredictionById(Long id) {
        return apsGasPipelinePredictionMapper.deleteApsGasPipelinePredictionById(id);
    }
    @SneakyThrows
    @Override
    public int batchInsertApsGasPipelinePrediction(MultipartFile file) {
        Workbook workbook = WorkbookFactory.create(file.getInputStream());
        Sheet sheet = workbook.getSheetAt(0);
        int rows = sheet.getLastRowNum();
        if (rows > 0) {
            List<ApsGasPipelinePrediction> list = new ArrayList<>();
        ExcelUtil<ApsGasPipelinePrediction> util = new ExcelUtil<>(ApsGasPipelinePrediction.class);
        List<ApsGasPipelinePrediction> list = util.importExcel(file.getInputStream());
        DictUtils.CacheValue cacheValue = DictUtils.getCacheValue("aps_factory");
        Set<String> keys = new HashSet<>();
        list.forEach(item -> {
            item.setId(IdUtil.getSnowflakeNextId());
            item.setFactory(cacheValue.get(item.getFactory()));
            item.setCreateBy(SecurityUtils.getUsername());
            item.setCreateTime(DateUtils.getNowDate());
        });
            /*数据列从1开始*/
            for (int i = 1; i <= rows; i++) {
                Row row = sheet.getRow(i);
                String materialCode = row.getCell(0).getStringCellValue();
                if (StringUtils.isEmpty(materialCode)){
                    continue;
                }
                String factory = row.getCell(1).getStringCellValue();
                double quantity = row.getCell(2).getNumericCellValue();
                Date date = row.getCell(3).getDateCellValue();
                if (StringUtils.isNotBlank(factory) &&
                    date != null) {
                    ApsGasPipelinePrediction item = new ApsGasPipelinePrediction();
                    item.setId(IdUtil.getSnowflakeNextId());
                    item.setFactory(factory);
                    item.setMaterialCode(materialCode);
                    item.setCreateBy(SecurityUtils.getUsername());
                    item.setCreateTime(DateUtils.getNowDate());
                    item.setPredictQuantity(new BigDecimal(quantity));
                    item.setPredictDate(new Timestamp(date.getTime()));
                    list.add(item);
        for (int i = list.size() - 1; i >= 0; --i) {
            list.get(i).setKey(null);
            if (!keys.contains(list.get(i).getKey())) {
                keys.add(list.get(i).getKey());
            } else {
                list.remove(i);
            }
        }
        if (!list.isEmpty()) {
            List<Map<String, Object>> facCodeKey = apsGasPipelinePredictionMapper.selectByFacOrMaterial(keys);
            if (!facCodeKey.isEmpty()) {
                Iterator<ApsGasPipelinePrediction> it = list.iterator();
                while (it.hasNext()) {
                    ApsGasPipelinePrediction item = it.next();
                    int count = 0;
                    for (Map<String, Object> 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.deleteAll();
                apsGasPipelinePredictionMapper.insert(list);
            }
            return list.size();
        }
        return 0;
        return list.size();
    }
}