package com.iailab.module.ansteel.power.service.impl;
|
|
import com.alibaba.fastjson.JSONArray;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.iailab.framework.common.util.date.DateUtils;
|
import com.iailab.framework.common.util.object.ConvertUtils;
|
import com.iailab.framework.security.core.util.SecurityFrameworkUtils;
|
import com.iailab.module.ansteel.api.dto.PowerPriceDetDTO;
|
import com.iailab.module.ansteel.api.dto.PowerPriceMainDTO;
|
import com.iailab.module.ansteel.api.dto.PowerPriceRespVO;
|
import com.iailab.module.ansteel.common.constant.CommonConstant;
|
import com.iailab.module.ansteel.power.dao.PowerPriceDetDao;
|
import com.iailab.module.ansteel.power.dao.PowerPriceMainDao;
|
import com.iailab.module.ansteel.power.entity.PowerPriceDetEntity;
|
import com.iailab.module.ansteel.power.entity.PowerPriceMainEntity;
|
import com.iailab.module.ansteel.power.service.PowerPriceDetService;
|
import com.iailab.module.ansteel.power.service.PowerPriceMainService;
|
import com.iailab.module.model.api.mcs.McsApi;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.ObjectUtils;
|
|
import javax.annotation.Resource;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.stream.Collectors;
|
|
/**
|
* @author lirm
|
* @since 1.0.0 2025-05-21
|
*/
|
@Slf4j
|
@Service
|
public class PowerPriceMainServiceImpl implements PowerPriceMainService {
|
|
@Resource
|
private PowerPriceMainDao powerPriceMainDao;
|
|
@Resource
|
private PowerPriceDetDao powerPriceDetDao;
|
|
@Resource
|
private PowerPriceDetService powerPriceDetService;
|
@Autowired
|
private McsApi mcsApi;
|
|
private String peakKey = "peak_periods_json";//峰
|
private String valleyKey = "valley_periods_json";//谷
|
private String flatKey = "flat_periods_json";//平
|
|
@Override
|
public List<PowerPriceMainDTO> list(Map<String, Object> params) {
|
PowerPriceMainEntity powerPriceMainEntity = new PowerPriceMainEntity();
|
List<PowerPriceMainEntity> entitylist = powerPriceMainDao.selectList(new QueryWrapper<>(powerPriceMainEntity).eq("status", CommonConstant.IS_ENABLE));
|
List<PowerPriceMainDTO> dtolist = ConvertUtils.sourceToTarget(entitylist, PowerPriceMainDTO.class);
|
dtolist.stream().map(item -> {
|
List<PowerPriceDetDTO> detList = powerPriceDetService.getByMainId(item.getId());
|
item.setDetList(detList);
|
return item;
|
}).collect(Collectors.toList());
|
return dtolist;
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Boolean save(PowerPriceMainDTO mainDTO) {
|
if (ObjectUtils.isEmpty(mainDTO)) {
|
return null;
|
}
|
powerPriceMainDao.updateStatus();
|
|
if (!CollectionUtils.isEmpty(mainDTO.getDetList())) {
|
PowerPriceMainEntity powerPriceMainEntity = ConvertUtils.sourceToTarget(mainDTO, PowerPriceMainEntity.class);
|
powerPriceMainEntity.setCode(DateUtils.format(new Date(), "yyyyMMddHHmmss"));
|
powerPriceMainEntity.setStatus(CommonConstant.IS_ENABLE);
|
powerPriceMainEntity.setCreator(SecurityFrameworkUtils.getLoginUser().getId());
|
powerPriceMainEntity.setCreateDate(new Date());
|
powerPriceMainDao.insert(powerPriceMainEntity);
|
|
List<PowerPriceRespVO> list = new ArrayList<>();
|
for (int i = 0; i < mainDTO.getDetList().size(); i++) {
|
PowerPriceDetEntity powerPriceDetEntity = ConvertUtils.sourceToTarget(mainDTO.getDetList().get(i), PowerPriceDetEntity.class);
|
powerPriceDetEntity.setMainId(powerPriceMainEntity.getId());
|
powerPriceDetEntity.setSort(i + 1);
|
powerPriceDetDao.insert(powerPriceDetEntity);
|
|
ArrayList arrayList = new ArrayList();
|
arrayList.add(powerPriceDetEntity.getStart());
|
arrayList.add(powerPriceDetEntity.getEnd());
|
|
PowerPriceRespVO powerPriceRespVO = new PowerPriceRespVO();
|
if ("峰".equals(powerPriceDetEntity.getName())) {
|
powerPriceRespVO.setType("峰");
|
} else if ("谷".equals(powerPriceDetEntity.getName())) {
|
powerPriceRespVO.setType("谷");
|
} else if ("平".equals(powerPriceDetEntity.getName())) {
|
powerPriceRespVO.setType("平");
|
}
|
|
powerPriceRespVO.setValue(arrayList.stream().toArray(String[]::new));
|
list.add(powerPriceRespVO);
|
}
|
|
Object[] peakArray = list.stream().filter(e -> e.getType().equals("峰")).collect(Collectors.toList()).stream().map(e -> e.getValue()).collect(Collectors.toList()).stream().toArray(Object[]::new);
|
Object[] valleyArray = list.stream().filter(e -> e.getType().equals("谷")).collect(Collectors.toList()).stream().map(e -> e.getValue()).collect(Collectors.toList()).stream().toArray(Object[]::new);
|
Object[] flatArray = list.stream().filter(e -> e.getType().equals("平")).collect(Collectors.toList()).stream().map(e -> e.getValue()).collect(Collectors.toList()).stream().toArray(Object[]::new);
|
|
mcsApi.updateScheduleModelSetting(CommonConstant.COAL_MODEL_CODE,peakKey,JSONArray.toJSONString(peakArray));
|
mcsApi.updateScheduleModelSetting(CommonConstant.COAL_MODEL_CODE,valleyKey,JSONArray.toJSONString(valleyArray));
|
mcsApi.updateScheduleModelSetting(CommonConstant.COAL_MODEL_CODE,flatKey,JSONArray.toJSONString(flatArray));
|
}
|
return true;
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Boolean update(PowerPriceMainDTO mainDTO) {
|
if (!CollectionUtils.isEmpty(mainDTO.getDetList())) {
|
PowerPriceMainEntity powerPriceMainEntity = powerPriceMainDao.selectById(mainDTO.getId());
|
powerPriceMainEntity.setCreator(SecurityFrameworkUtils.getLoginUser().getId());
|
powerPriceMainEntity.setCreateDate(new Date());
|
powerPriceMainDao.updateById(powerPriceMainEntity);
|
|
QueryWrapper queryWrapper = new QueryWrapper<>();
|
queryWrapper.eq("main_id", powerPriceMainEntity.getId());
|
powerPriceDetDao.delete(queryWrapper);
|
|
List<PowerPriceRespVO> list = new ArrayList<>();
|
for (int i = 0; i < mainDTO.getDetList().size(); i++) {
|
PowerPriceDetEntity powerPriceDetEntity = ConvertUtils.sourceToTarget(mainDTO.getDetList().get(i), PowerPriceDetEntity.class);
|
powerPriceDetEntity.setMainId(powerPriceMainEntity.getId());
|
powerPriceDetEntity.setSort(i + 1);
|
powerPriceDetDao.insert(powerPriceDetEntity);
|
|
ArrayList arrayList = new ArrayList();
|
arrayList.add(powerPriceDetEntity.getStart());
|
arrayList.add(powerPriceDetEntity.getEnd());
|
|
PowerPriceRespVO powerPriceRespVO = new PowerPriceRespVO();
|
if ("峰".equals(powerPriceDetEntity.getName())) {
|
powerPriceRespVO.setType("峰");
|
} else if ("谷".equals(powerPriceDetEntity.getName())) {
|
powerPriceRespVO.setType("谷");
|
} else if ("平".equals(powerPriceDetEntity.getName())) {
|
powerPriceRespVO.setType("平");
|
}
|
|
powerPriceRespVO.setValue(arrayList.stream().toArray(String[]::new));
|
list.add(powerPriceRespVO);
|
}
|
|
Object[] peakArray = list.stream().filter(e -> e.getType().equals("峰")).collect(Collectors.toList()).stream().map(e -> e.getValue()).collect(Collectors.toList()).stream().toArray(Object[]::new);
|
Object[] valleyArray = list.stream().filter(e -> e.getType().equals("谷")).collect(Collectors.toList()).stream().map(e -> e.getValue()).collect(Collectors.toList()).stream().toArray(Object[]::new);
|
Object[] flatArray = list.stream().filter(e -> e.getType().equals("平")).collect(Collectors.toList()).stream().map(e -> e.getValue()).collect(Collectors.toList()).stream().toArray(Object[]::new);
|
|
mcsApi.updateScheduleModelSetting(CommonConstant.COAL_MODEL_CODE,peakKey,JSONArray.toJSONString(peakArray));
|
mcsApi.updateScheduleModelSetting(CommonConstant.COAL_MODEL_CODE,valleyKey,JSONArray.toJSONString(valleyArray));
|
mcsApi.updateScheduleModelSetting(CommonConstant.COAL_MODEL_CODE,flatKey,JSONArray.toJSONString(flatArray));
|
}
|
return true;
|
}
|
}
|