CZT
2023-11-27 c206acfaedc69c390fb67daa81bc686f58a212ef
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
package com.ld.igds.three.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.ld.igds.common.CoreThreeService;
import com.ld.igds.gas.dto.GasData;
import com.ld.igds.models.ThreeConf;
import com.ld.igds.three.ThreeCodeEnum;
import com.ld.igds.three.ThreeConstant;
import com.ld.igds.three.data.Data2003;
import com.ld.igds.three.data.ThreeResponse;
import com.ld.igds.three.mapper.ThreeMapper;
import com.ld.igds.three.param.ThreeCommonParam;
import com.ld.igds.three.param.ThreeRequest;
import com.ld.igds.three.service.ThreeService;
import com.ld.igds.three.util.ThreeRespUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 2003-气体信息接口实现
 *
 * @author chen
 */
@Service
public class ThreeServiceImpl2003 implements ThreeService {
 
    @Autowired
    private CoreThreeService commonService;
    @Autowired
    private ThreeMapper threeMapper;
 
    @Override
    public String getInterfaceId() {
        return ThreeConstant.API_THREE_2003;
    }
 
    @SuppressWarnings("unchecked")
    @Override
    public ThreeResponse<Object> execute(ThreeRequest<JSONObject> req){
        //转化请求参数
        ThreeCommonParam param = JSONObject.parseObject(req.getData().toString(), ThreeCommonParam.class);
 
        //判断uid
        if(StringUtils.isEmpty(param.getUid())){
            return ThreeRespUtil.error(ThreeCodeEnum.CODE_1005, req);
        }
 
        //返回数据定义
        List<Data2003> list = new ArrayList<>();
        Data2003 data2003;
 
        //获取所有仓库气体信息(每个仓库取最新的一条)
        Map<String, GasData> gasMap = getGasAll(req.getCompanyId());
        if(gasMap.isEmpty()){
            return ThreeRespUtil.success(list, req);
        }
 
        //根据uid判断查询所有仓库气体还是单个仓库气体
        if(ThreeConstant.REQUEST_UID.equals(param.getUid())){
            //获取全部三维配置信息
            List<ThreeConf> threeList = commonService.getCacheThreeAll(req.getCompanyId());
            if(threeList == null || threeList.isEmpty()){
                return ThreeRespUtil.error(ThreeCodeEnum.CODE_1111, "未查询相关三维配置信息,请联系管理员!", req);
            }
 
            //返回数据处理
            for (ThreeConf threeConf : threeList) {
                if(StringUtils.isEmpty(threeConf.getDeviceId()) &&
                        StringUtils.isNotEmpty(threeConf.getDepotId())){
 
                    GasData gasData = gasMap.get(threeConf.getDepotId());
                    if(gasData != null){
                        data2003 = getData(gasData, threeConf.getUid());
                        list.add(data2003);
                    }
                }
            }
 
 
        }else {
            //根据uid获取三维配置信息
            ThreeConf threeConf = commonService.getCacheThreeConfById(req.getCompanyId(), param.getUid());
            if(threeConf == null){
                return ThreeRespUtil.error(ThreeCodeEnum.CODE_1111, "未查询到此uid的配置信息,请联系管理员!", req);
            }
 
            GasData gasData = gasMap.get(threeConf.getDepotId());
            if(gasData != null){
                data2003 = getData(gasData, threeConf.getUid());
                list.add(data2003);
            }
        }
 
        return ThreeRespUtil.success(list, req);
    }
 
    /**
     * 数据封装
     * @param gasData
     * @param uid
     * @return
     */
    private Data2003 getData(GasData gasData, String uid){
 
        Data2003 data = new Data2003();
 
        data.setUid(uid);
        data.setTime(gasData.getReceiveDate());
        data.setCo2(gasData.getPerCo2());
        data.setO2(gasData.getPerO2());
        data.setPh3(gasData.getPerPh3());
        data.setN2(gasData.getPerN2());
        data.setMaxCo2(gasData.getPerCo2Max());
        data.setMinCo2(gasData.getPerCo2Min());
        data.setMaxO2(gasData.getPerO2Max());
        data.setMinO2(gasData.getPerO2Min());
        data.setMaxPh3(gasData.getPerPh3Max());
        data.setMinPh3(gasData.getPerPh3Min());
        data.setMaxN2(gasData.getPerN2Max());
        data.setMinN2(gasData.getPerN2Min());
 
        return data;
    }
 
 
    /**
     * 从数据库查询气体,并转为map集合
     * @param companyId
     * @return
     */
    private Map<String, GasData> getGasAll(String companyId){
 
        List<GasData> grainAll = threeMapper.getGasAll(companyId);
 
        Map<String, GasData> map = new HashMap<>();
 
        if(grainAll != null && grainAll.size() > 0){
            for (GasData gasData : grainAll) {
                map.put(gasData.getDepotId(),gasData);
            }
        }
        return map;
    }
}