houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
 
package com.iailab.module.quality.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iailab.framework.common.constant.Constant;
import com.iailab.common.dto.echarts.BarLineDTO;
import com.iailab.common.dto.echarts.SeriesItem;
import com.iailab.common.enums.CommonConstant;
import com.iailab.framework.common.page.PageData;
import com.iailab.framework.common.service.impl.BaseServiceImpl;
import com.iailab.framework.common.util.object.ConvertUtils;
import com.iailab.common.utils.DateUtils;
import com.iailab.common.utils.HttpsRequest;
import com.iailab.module.data.dto.FeignHttpApiDTO;
import com.iailab.module.data.api.IFeignDataApi;
import com.iailab.module.quality.dao.QualityQualityTrendDayDao;
import com.iailab.module.quality.dto.QualityQualityTrendDayDTO;
import com.iailab.module.quality.entity.QualityQualityTrendDayEntity;
import com.iailab.module.quality.service.QualityQualityTrendDayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
 
/**
 * @author DongYukun
 * @Description
 * @createTime 2024年05月20日
 */
@Slf4j
@Service
public class QualityQualityTrendDayServiceImpl extends BaseServiceImpl<QualityQualityTrendDayDao, QualityQualityTrendDayEntity> implements QualityQualityTrendDayService {
 
    private String HTTP_API_CODE = "Quality.QualityTrendDay";
 
    @Resource
    private IFeignDataApi feignDataApi;
 
    @Resource
    private HttpsRequest httpsRequest;
 
    @Override
    public PageData<QualityQualityTrendDayDTO> page(Map<String, Object> params) {
        IPage<QualityQualityTrendDayEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );
        return getPageData(page, QualityQualityTrendDayDTO.class);
    }
 
    private QueryWrapper<QualityQualityTrendDayEntity> getWrapper(Map<String, Object> params){
        String rq = (String)params.get("rq");
 
        QueryWrapper<QualityQualityTrendDayEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(rq), "rq", rq)
                .orderByAsc("rq");
 
        return wrapper;
    }
 
    @Override
    public QualityQualityTrendDayDTO get(String id) {
        QualityQualityTrendDayEntity entity = baseDao.selectById(id);
 
        return ConvertUtils.sourceToTarget(entity, QualityQualityTrendDayDTO.class);
    }
 
    @Override
    public void save(QualityQualityTrendDayDTO dto) {
        QualityQualityTrendDayEntity entity = ConvertUtils.sourceToTarget(dto, QualityQualityTrendDayEntity.class);
 
        insert(entity);
    }
 
    @Override
    public void update(QualityQualityTrendDayDTO dto) {
        QualityQualityTrendDayEntity entity = ConvertUtils.sourceToTarget(dto, QualityQualityTrendDayEntity.class);
 
        updateById(entity);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String[] ids) {
        baseDao.deleteBatchIds(Arrays.asList(ids));
    }
 
    @Override
    public BarLineDTO barLine(String length,String type,String coal) {
 
        BarLineDTO barLineDTO = new BarLineDTO();
        List<SeriesItem> seriesItemList = new ArrayList<>();
        List<Object[]> data = new ArrayList();
        switch (type) {
            case "sf":
                //水分
                this.list(coal).stream().forEach(
                        item->{
                            Object[] dataItem = new Object[2];
                            dataItem[0] = item.getRq();
                            dataItem[1] = item.getMt();
                            if(item.getMt()!=null){
                                data.add(dataItem);
                            }
                            barLineDTO.setValueName(coal+"水分");
                        }
                );
                break;
            case "hf":
                //灰分
                this.list(coal).stream().forEach(
                        item->{
                            Object[] dataItem = new Object[2];
                            dataItem[0] = item.getRq();
                            dataItem[1] = item.getAd();
                            if(item.getAd()!=null){
                                data.add(dataItem);
                            }
                            barLineDTO.setValueName(coal+"灰分");
                        }
                );
                break;
            case "lf":
                //硫分
                this.list(coal).stream().forEach(
                        item->{
                            Object[] dataItem = new Object[2];
                            dataItem[0] = item.getRq();
                            dataItem[1] = item.getSt();
                            if(item.getSt()!=null){
                                data.add(dataItem);
                            }
                            barLineDTO.setValueName(coal+"硫分");
                        }
                );
                break;
            case "rz":
                //热值
                this.list(coal).stream().forEach(
                        item->{
                            Object[] dataItem = new Object[2];
                            dataItem[0] = item.getRq();
                            dataItem[1] = item.getQnet();
                            if(item.getQnet()!=null){
                                data.add(dataItem);
                            }
                            barLineDTO.setValueName(coal+"热值");
                        }
                );
                break;
            default:
                break;
        }
 
        SeriesItem seriesItem = new SeriesItem();
        seriesItem.setData(data);
        seriesItemList.add(seriesItem);
        barLineDTO.setCategories(this.getLastMonthDates(Integer.parseInt(length)));
        barLineDTO.setSeries(seriesItemList);
        return barLineDTO;
    }
 
    @Override
    public BigDecimal currentValue(String coal,String type) {
        switch (type) {
            case "sf":
                //水分
                QualityQualityTrendDayEntity sf = baseDao.selectOne(
                        new QueryWrapper<QualityQualityTrendDayEntity>()
                                .eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                                .eq("coal",coal)
                );
                if(sf != null){
                    return sf.getMt();
                }
                return null;
            case "hf":
                //灰分
                QualityQualityTrendDayEntity hf = baseDao.selectOne(
                        new QueryWrapper<QualityQualityTrendDayEntity>()
                                .eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                                .eq("coal",coal)
                );
                if(hf != null){
                    return hf.getAd();
                }
                return null;
            case "lf":
                //硫分
                QualityQualityTrendDayEntity lf = baseDao.selectOne(
                        new QueryWrapper<QualityQualityTrendDayEntity>()
                                .eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                                .eq("coal",coal)
                );
                if(lf != null){
                    return lf.getSt();
                }
                return null;
            case "rz":
                //热值
                QualityQualityTrendDayEntity rz = baseDao.selectOne(
                        new QueryWrapper<QualityQualityTrendDayEntity>()
                                .eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                                .eq("coal",coal)
                );
                if(rz != null){
                    return rz.getQnet();
                }
                return null;
            default:
                break;
        }
        return null;
    }
 
    public List<String> getLastMonthDates(int length) {
        List<String> dateList = new ArrayList<>();
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (int i = length-1; i >= 0; i--) {
            dateList.add(currentDate.minusDays(i).format(formatter));
        }
 
        return dateList;
    }
 
    public List<QualityQualityTrendDayEntity> list(String coal){
        List<QualityQualityTrendDayEntity> prdDayProdList = baseDao.selectList(
                new QueryWrapper<QualityQualityTrendDayEntity>()
                        .le("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                        .ge("rq",LocalDate.now().minusDays(29).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")))
                        .eq("coal",coal)
                        .orderByAsc("rq")
        );
        return prdDayProdList;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncData() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        String endDate = DateUtils.format(calendar.getTime());
        calendar.add(Calendar.DAY_OF_YEAR, -10);
        String startDate = DateUtils.format(calendar.getTime());
 
        FeignHttpApiDTO apiDTO = feignDataApi.getHttpApi(HTTP_API_CODE);
        Map<String, String> params = new HashMap<>();
        String responseStr = httpsRequest.doGet(apiDTO.getUrl() + "&startDate=" + startDate + "&endDate=" + endDate, params, "utf-8", "");
        JSONObject responseObj = JSON.parseObject(responseStr);
        if (!CommonConstant.STA_TRUE.equals(responseObj.get("sta").toString())) {
            log.info("接口异常");
        }
        JSONArray dataArray = responseObj.getJSONArray(CommonConstant.ZX_RES);
        if (CollectionUtils.isEmpty(dataArray)) {
            return;
        }
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject dataItem = dataArray.getJSONObject(i);
            String date = dataItem.getString("date");
            JSONArray coalList = dataItem.getJSONArray("coalList");
            this.deleteByDate(date);
            List<QualityQualityTrendDayEntity> entities = coalList.toJavaList(QualityQualityTrendDayEntity.class);
            entities.forEach(entity -> {
                entity.setId(UUID.randomUUID().toString());
                entity.setRq(date);
                entity.setCreateDate(new Date());
                entity.setUpdateDate(new Date());
                insert(entity);
            });
        }
    }
 
    private void deleteByDate(String date) {
        QueryWrapper<QualityQualityTrendDayEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(date), "rq", date);
        baseDao.delete(wrapper);
    }
}