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
package com.iailab.module.prod.service.impl;
 
import com.alibaba.fastjson.JSON;
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.enums.CommonConstant;
import com.iailab.framework.common.page.PageData;
import com.iailab.framework.common.service.impl.BaseServiceImpl;
import com.iailab.common.utils.CommonUtils;
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.prod.dao.PrdCurrentPerformanceDao;
import com.iailab.module.prod.dto.PrdCurrentPerformanceDTO;
import com.iailab.module.prod.entity.PrdCurrentPerformanceEntity;
import com.iailab.module.prod.service.PrdCurrentPerformanceService;
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 java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
 
/**
 * @author PanZhibao
 * @Description
 * @createTime 2024年05月14日
 */
@Slf4j
@Service
public class PrdCurrentPerformanceServiceImpl extends BaseServiceImpl<PrdCurrentPerformanceDao, PrdCurrentPerformanceEntity> implements PrdCurrentPerformanceService {
 
    private String HTTP_API_CODE = "Prd.CurrentPerformance";
 
    @Resource
    private IFeignDataApi feignDataApi;
 
    @Resource
    private HttpsRequest httpsRequest;
 
    @Override
    public PageData<PrdCurrentPerformanceDTO> page(Map<String, Object> params) {
        IPage<PrdCurrentPerformanceEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );
        return getPageData(page, PrdCurrentPerformanceDTO.class);
    }
 
    private QueryWrapper<PrdCurrentPerformanceEntity> getWrapper(Map<String, Object> params){
        String rq = (String)params.get("rq");
 
        QueryWrapper<PrdCurrentPerformanceEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(rq), "rq", rq)
                .orderByAsc("rq");
 
        return wrapper;
    }
 
    @Override
    public PrdCurrentPerformanceDTO get(String id) {
        PrdCurrentPerformanceEntity entity = baseDao.selectById(id);
 
        return ConvertUtils.sourceToTarget(entity, PrdCurrentPerformanceDTO.class);
    }
 
    @Override
    public void save(PrdCurrentPerformanceDTO dto) {
        PrdCurrentPerformanceEntity entity = ConvertUtils.sourceToTarget(dto, PrdCurrentPerformanceEntity.class);
 
        insert(entity);
    }
 
    @Override
    public void update(PrdCurrentPerformanceDTO dto) {
        PrdCurrentPerformanceEntity entity = ConvertUtils.sourceToTarget(dto, PrdCurrentPerformanceEntity.class);
 
        updateById(entity);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String[] ids) {
        baseDao.deleteBatchIds(Arrays.asList(ids));
    }
 
    @Override
    public BarLineDTO barLine(String length) {
        return null;
    }
 
    @Override
    public BigDecimal currentValue() {
        return null;
    }
 
    public List<String> getLastMonthDates() {
        List<String> dateList = new ArrayList<>();
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (int i = 29; i >= 0; i--) {
            dateList.add(currentDate.minusDays(i).format(formatter));
        }
 
        return dateList;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncData() {
        FeignHttpApiDTO apiDTO = feignDataApi.getHttpApi(HTTP_API_CODE);
        Map<String, String> params = new HashMap<>();
        String responseStr = httpsRequest.doGet(apiDTO.getUrl(), params, "utf-8", "");
        JSONObject responseObj = JSON.parseObject(responseStr);
        if (!CommonConstant.STA_TRUE.equals(responseObj.get("sta").toString())) {
            log.info("接口异常");
        }
        JSONObject res = responseObj.getJSONObject(CommonConstant.ZX_RES);
        JSONObject yesterday = res.getJSONObject("yesterday");
        if (yesterday == null) {
            return;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        String rq = DateUtils.format(calendar.getTime());
        this.deleteByRq(rq);
        PrdCurrentPerformanceEntity entity = new PrdCurrentPerformanceEntity();
        entity.setId(UUID.randomUUID().toString());
        entity.setRq(rq);
        entity.setXxPerformance(CommonUtils.getJSONValue(yesterday.get("xxPerformance")));
        entity.setZqPerformance(CommonUtils.getJSONValue(yesterday.get("zqPerformance")));
        entity.setCreateDate(new Date());
        entity.setUpdateDate(new Date());
        insert(entity);
 
    }
 
    private void deleteByRq(String rq) {
        QueryWrapper<PrdCurrentPerformanceEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(rq), "rq", rq);
        baseDao.delete(wrapper);
    }
}