鞍钢鲅鱼圈能源管控系统后端代码
liriming
2025-06-17 b66bb9f7e496ac2c9295dbe759befd2e276441cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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;
    }
}