jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
package com.ld.igds.sh.service;
 
import com.alibaba.fastjson.JSONObject;
import com.ld.igds.constant.RedisConst;
import com.ld.igds.log.service.InteStatusLogService;
import com.ld.igds.models.Grain;
import com.ld.igds.models.InteStatusLog;
import com.ld.igds.sh.dto.ApiResponse;
import com.ld.igds.sh.dto.Dto1401;
import com.ld.igds.sh.param.ApiRequest;
import com.ld.igds.sh.param.BaseParam;
import com.ld.igds.sh.service.impl.HApiShServiceImpl;
import com.ld.igds.sh.util.ApiShConst;
import com.ld.igds.sh.util.RespCodeEnum;
import com.ld.igds.sh.util.RespUtil;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.DateUtil;
import com.ld.igds.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * 温湿度检测接口
 *
 * @author chen
 */
@Slf4j
@Service
public class ApiShServiceImpl1401 implements ApiShService {
 
    @Autowired
    private HApiShServiceImpl hApiShServiceImpl;
    @Autowired
    private InteStatusLogService inteStatusLogService;
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public String getInterfaceId() {
        return ApiShConst.API_SH_1401;
    }
 
    @SuppressWarnings("unchecked")
    @Override
    public ApiResponse<Object> execute(ApiRequest<Object> request){
        //获取参数信息
        String deptId = request.getDeptId();
        BaseParam param = JSONObject.parseObject(JSONObject.toJSONString(request.getData()), BaseParam.class);
 
        String companyId = ContextUtil.getDefaultCompanyId();
 
        String key = RedisConst.buildKey(companyId, ApiShConst.API_SH_1401);
        Date startTime = (Date) redisUtil.get(key);
        if(null == startTime){
            startTime = DateUtils.addDays(param.getEndTime(), -30);
        }
        redisUtil.set(key, param.getEndTime());
 
        //查询温湿度信息,即粮情信息
        List<Grain> dataList = hApiShServiceImpl.listGrain(companyId,
                param.getDepotId(), startTime, param.getEndTime());
 
        //响应数据为空则直接返回响应码2000
        if (dataList == null || dataList.isEmpty()) {
            return RespUtil.error(RespCodeEnum.CODE_2000.getCode(), request);
        }
 
        List<Dto1401> list = new ArrayList<>();
        Dto1401 dto1401;
        InteStatusLog log;
        InteStatusLog addLog;
        Calendar c = Calendar.getInstance();
        //封装数据信息
        for (Grain grain : dataList) {
            dto1401 = new Dto1401();
            dto1401.setLqjcbm(grain.getDepotId() + grain.getBatchId());
            dto1401.setJcsj(DateFormatUtils.format(grain.getReceiveDate(), "yyyy-MM-dd HH:mm:ss"));
 
            dto1401.setHwbm(grain.getDepotId());
            dto1401.setCfww(String.valueOf(grain.getTempOut()));
            dto1401.setCfws(String.valueOf(grain.getHumidityOut()));
            dto1401.setCfnw(String.valueOf(grain.getTempIn()));
            dto1401.setCfns(String.valueOf(grain.getHumidityIn()));
            dto1401.setZgw(String.valueOf(grain.getTempMax()));
            dto1401.setZdw(String.valueOf(grain.getTempMin()));
            dto1401.setPjw(String.valueOf(grain.getTempAve()));
            if (StringUtils.isEmpty(grain.getCableCir())) {
                dto1401.setLswdzjh(getPointList1(grain.getPoints(), grain.getCable()));
            } else {
                dto1401.setLswdzjh(getPointList2(grain.getPoints(), grain.getCable(), grain.getCableCir()));
            }
            dto1401.setNm(grain.getDepotId().substring(0, 21));
            dto1401.setZznm(grain.getDepotId().substring(0, 21));
            dto1401.setCfnm(grain.getDepotId().substring(0, 21));
            dto1401.setDqwd(String.valueOf(grain.getTempOut()));
            dto1401.setDqsd(String.valueOf(grain.getHumidityOut()));
            dto1401.setCnwd(String.valueOf(grain.getTempIn()));
            dto1401.setCnsd(String.valueOf(grain.getHumidityIn()));
            dto1401.setZgwd(String.valueOf(grain.getTempMax()));
            dto1401.setZdwd(String.valueOf(grain.getTempMin()));
            dto1401.setPjlw(String.valueOf(grain.getTempAve()));
 
            dto1401.setCpjw(String.valueOf(grain.getTempAve()));
            dto1401.setCzgw(String.valueOf(grain.getTempAve()));
            dto1401.setCzdw(String.valueOf(grain.getTempAve()));
            dto1401.setGfwjw(String.valueOf(grain.getTempAve()));
            dto1401.setLwmx(dto1401.getLswdzjh());
            dto1401.setSf(dto1401.getLswdzjh());
            dto1401.setJwrq(DateFormatUtils.format(grain.getReceiveDate(), "yyyy-MM-dd"));
            dto1401.setJwsj(DateFormatUtils.format(grain.getReceiveDate(), "HH:mm:ss"));
            c.setTime(grain.getReceiveDate());
            dto1401.setXq((c.get(Calendar.DAY_OF_WEEK) - 1) + "");
            dto1401.setPcid(grain.getBatchId());
 
            dto1401.setZhgxsj(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            //查询日志状态表,设置操作标志
            log = inteStatusLogService.getLogByBizId(companyId, ApiShConst.API_SH_1401 + "_" + grain.getBatchId());
            if(null == log){
                dto1401.setTjbz("0"); //未提交
                addLog = new InteStatusLog();
                addLog.setBizId(ApiShConst.API_SH_1401 + "_" + grain.getBatchId());
                addLog.setCompanyId(companyId);
                addLog.setBizType(ApiShConst.API_SH_TYPE_TASK);
                addLog.setInterfaceId(ApiShConst.API_SH_1401);
                addLog.setInterfaceType(ApiShConst.API_SH_TYPE_TASK);
                addLog.setStatus(ApiShConst.API_SH_STATUS_SUCCESS);
 
                inteStatusLogService.addInteStatusLog(addLog);
            }else {
                dto1401.setTjbz("1"); //已提交
                dto1401.setCzbz(ApiShConst.API_SH_U);
            }
            list.add(dto1401);
        }
 
        //响应数据为空则直接返回响应码2000
        if (list.isEmpty()) {
            return RespUtil.error(RespCodeEnum.CODE_2000.getCode(), request);
        }
 
        return RespUtil.success(list, request);
    }
 
    /**
     * 获取平方仓的温度值集合,格式:温度值,层数,行数,列数|温度值,层数,行数,列数|温度值,层数,行数,列数
     *
     * @return
     */
    private String getPointList1(String point, String cable) {
        String[] cab = cable.split("-");
        int c = Integer.valueOf(cab[0]);
        int h = Integer.valueOf(cab[1]);
        int l = Integer.valueOf(cab[2]);
 
        String[] points = point.split(",");
        String result = "";
        for (int i = 1; i <= l; i++){
            for (int j = 1; j <= h; j++){
                for (int z = 1; z <= c; z++){
                    if((i*j-1)*c + z - 1 < points.length){
                        result += ("|" + points[(i*j-1)*c + z - 1] + "," + z + "," + j + "," + i);
                    }
                }
            }
        }
        return result.substring(1);
    }
 
    /**
     * 获取筒仓的温度值集合,格式:温度值,圈数,点数,层数|温度值,圈数,点数,层数|温度值,圈数,点数,层数
     *
     * @return
     */
    private String getPointList2(String point, String cable, String cableCir) {
        String[] cab = cable.split("-");
        String[] cabCir = cableCir.split("-");
 
        int q = cab.length;  //圈数
        int c = Integer.valueOf(cabCir[0]);  //层数
        int g = 0;  //圈根数
        int g1 = 1;  //圈起始根数
        int g2 = 0;  //圈截止根数
 
        String[] points = point.split(",");
        String result = "";
        for (int i = 1; i <= q; i++){
            g1 += g2;
            g = Integer.valueOf(cab[i-1]);
            g2 += g;
            for (int j = g1; j <= g2; j++){
                for (int z = 1; z <= c; z++){
                    if((j - 1) * c + z - 1 < points.length) {
                        result += ("|" + points[(j - 1) * c + z - 1] + "," + i + "," + j + "," + z);
                    }
                }
            }
 
        }
        return result.substring(1);
    }
}