Administrator
2024-05-29 2b3defc58765bdd126901b809448868a5f8feb3c
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
package com.dy.pipIrrRemote.valve;
 
import com.dy.common.aop.SsoAop;
import com.dy.common.mw.protocol.Command;
import com.dy.common.mw.protocol.Data;
import com.dy.common.mw.protocol.p206V202404.CodeV202404;
import com.dy.common.mw.protocol.p206V202404.downVos.ComCd92_A2Vo;
import com.dy.common.mw.protocol.p206V202404.downVos.ComCd93_A3Vo;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.pipIrrGlobal.daoPr.PrWaterPriceMapper;
import com.dy.pipIrrGlobal.daoRm.RmCommandHistoryMapper;
import com.dy.pipIrrGlobal.daoSe.SeVirtualCardMapper;
import com.dy.pipIrrGlobal.pojoRm.RmCommandHistory;
import com.dy.pipIrrGlobal.pojoSe.SeVirtualCard;
import com.dy.pipIrrGlobal.voRm.VoUnclosedValve;
import com.dy.pipIrrGlobal.voSe.VoVirtualCard;
import com.dy.pipIrrRemote.common.ComSupport;
import com.dy.pipIrrRemote.common.CommandSv;
import com.dy.pipIrrRemote.common.dto.ValveClose;
import com.dy.pipIrrRemote.common.dto.ValveOpen;
import com.dy.pipIrrRemote.result.RemoteResultCode;
import com.dy.pipIrrRemote.utils.RestTemplateUtils;
import com.dy.pipIrrSell.virtualCard.enums.LastOperateENUM;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
 
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Random;
 
/**
 * @author ZhuBaoMin
 * @date 2024-05-07 14:59
 * @LastEditTime 2024-05-07 14:59
 * @Description
 */
 
@Slf4j
@Tag(name = "分水房管理", description = "分水房操作")
@RestController
@RequestMapping(path="valve")
@RequiredArgsConstructor
public class ValveCtrl extends ComSupport {
    private final RestTemplateUtils restTemplateUtils;
    private final CommandSv commandSv;
    private final SeVirtualCardMapper seVirtualCardMapper;
    private final PrWaterPriceMapper prWaterPriceMapper;
    private final RmCommandHistoryMapper rmCommandHistoryMapper;
 
    /**
     * 远程开阀(平台、APP)
     * @param valve
     * @param bindingResult
     * @return
     */
    @PostMapping(path = "open", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    @SsoAop()
    public BaseResponse<Boolean> open(@RequestBody @Valid ValveOpen valve, BindingResult bindingResult) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyMMddHHmmss");
        if(bindingResult != null && bindingResult.hasErrors()){
            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
 
        Long intakeId = valve.getIntakeId();
        Long vcId = valve.getVcId();
        Integer userType = valve.getUserType();
        Long operator = valve.getOperator();
        String comId = null;
 
        // 获取系统参数
        if(!setuped) {
            setUp();
        }
 
        // 虚拟卡ID换虚拟卡对象
        VoVirtualCard vc = seVirtualCardMapper.getVcById(vcId);
        // 获取水价
        Double waterPrice = prWaterPriceMapper.getPrice();
 
        // 取水口ID换阀控器地址
        String rtuAddr = commandSv.getRtuAddrByIntakeId(intakeId);
        if(rtuAddr == null || rtuAddr.length() == 0) {
            return BaseResponseUtils.buildError(RemoteResultCode.RTU_ADDR_CANNOT_BE_NULL.getMessage());
        }
 
        // 生成订单号
        LocalDateTime dateTime = LocalDateTime.now();
        Random random = new Random();
        String CHARACTERS = "0123456789";
        StringBuilder sb = new StringBuilder(4);
        for (int i = 0; i < 4; i++) {
            int index = random.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(index));
        }
        String orderNo = dtf.format(dateTime) + sb.toString();
 
        String commandCode = null;
        if(protocolName.equals("p206V202404")) {
            // 获取功能码
            if(userType == 1) {
                // 平台开阀
                commandCode = CodeV202404.cd_92;
            }else {
                // APP开阀
                commandCode = CodeV202404.cd_A2;
            }
 
            // 创建视图
            ComCd92_A2Vo param = new ComCd92_A2Vo();
            param.controllerType = controllerType;
            param.projectNo = projectNo;
            param.icCardNo = vc.getVcNum();
            param.waterRemain = 0.0;
            param.moneyRemain = vc.getMoney();
            param.waterPrice = waterPrice;
            param.elePrice = 0.0;
            param.orderNo = orderNo;
 
            // 创建命令日志对象并添加到数据库中
            RmCommandHistory rmCommandHistory = getComHistory(commandCode, rtuAddr, param, operator);
            comId = commandSv.insert(rmCommandHistory);
 
            // 构造并发送命令
            Command com = command(comId, commandCode, rtuAddr, param);
            sendCom2Mw(com);
        } else {
            // 获取功能码
            if(userType == 1) {
                // 平台开阀
                commandCode = "92";
            }else {
                // APP开阀
                commandCode = "97";
            }
 
            // 创建视图
            ComCd92_A2Vo param = new ComCd92_A2Vo();
 
            // 创建命令日志对象并添加到数据库中
            RmCommandHistory rmCommandHistory = getComHistory(commandCode, rtuAddr, param, operator);
            comId = commandSv.insert(rmCommandHistory);
 
            // 构造并发送命令
            Command com = command(comId, commandCode, rtuAddr, param);
            sendCom2Mw(com);
        }
 
        // 处理回调
        BaseResponse response = dealWithCallBack(comId);
        if(!response.getCode().equals("0001")) {
            return BaseResponseUtils.buildError(RemoteResultCode.GET_RESULT_ERROR.getMessage());
        }
 
        // 更改虚拟卡状态:是否使用中、最后操作、最后操作时间
        SeVirtualCard virtualCard = new SeVirtualCard();
        virtualCard.setId(vcId);
        virtualCard.setInUse((byte) 1);
        virtualCard.setLastOperate(LastOperateENUM.OPEN_VALVE.getCode());
        virtualCard.setLastOperateTime(new Date());
        seVirtualCardMapper.updateByPrimaryKeySelective(virtualCard);
 
        Data myData = (Data)response.getContent();
        return BaseResponseUtils.buildSuccess(myData) ;
    }
 
    /**
     * 远程关阀(平台、APP)
     * @param valve
     * @param bindingResult
     * @return
     */
    @PostMapping(path = "close", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    @SsoAop()
    public BaseResponse<Boolean> close(@RequestBody @Valid ValveClose valve, BindingResult bindingResult) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyMMddHHmmss");
        if(bindingResult != null && bindingResult.hasErrors()){
            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
 
        String rtuAddr = valve.getRtuAddr();
        String vcNum = valve.getVcNum();
        String orderNo = valve.getOrderNo();
        Integer userType = valve.getUserType();
        Long operator = valve.getOperator();
        String comId = null;
 
        // 获取系统参数
        if(!setuped) {
            setUp();
        }
 
        String commandCode = null;
        if(protocolName.equals("p206V202404")) {
            // 获取功能码
            if (userType == 1) {
                // 平台关阀
                commandCode = CodeV202404.cd_93;
            } else {
                // APP关阀
                commandCode = CodeV202404.cd_A3;
            }
 
            // 创建视图
            ComCd93_A3Vo param = new ComCd93_A3Vo();
            param.controllerType = controllerType;
            param.projectNo = projectNo;
            param.icCardNo = vcNum;
            param.orderNo = orderNo;
 
            // 创建命令日志对象并添加到数据库中
            RmCommandHistory rmCommandHistory = getComHistory(commandCode, rtuAddr, param, operator);
            comId = commandSv.insert(rmCommandHistory);
 
            // 构造并发送命令
            Command com = command(comId, commandCode, rtuAddr, param);
            sendCom2Mw(com);
        } else {
            // 获取功能码
            if (userType == 1) {
                // 平台关阀
                commandCode = "93";
            } else {
                // APP关阀
                commandCode = "98";
            }
 
            // 创建视图
            ComCd93_A3Vo param = new ComCd93_A3Vo();
            param.controllerType = controllerType;
            param.projectNo = projectNo;
            param.icCardNo = vcNum;
            param.orderNo = orderNo;
 
            // 创建命令日志对象并添加到数据库中
            RmCommandHistory rmCommandHistory = getComHistory(commandCode, rtuAddr, param, operator);
            comId = commandSv.insert(rmCommandHistory);
 
            // 构造并发送命令
            Command com = command(comId, commandCode, rtuAddr, param);
            sendCom2Mw(com);
        }
 
        // 处理回调
        BaseResponse response = dealWithCallBack(comId);
        if(!response.getCode().equals("0001")) {
            return BaseResponseUtils.buildError(RemoteResultCode.GET_RESULT_ERROR.getMessage());
        }
 
        // 更改虚拟卡状态:是否使用中、最后操作、最后操作时间
        Long vcId = seVirtualCardMapper.getVcIdByNum(vcNum);
        SeVirtualCard virtualCard = new SeVirtualCard();
        virtualCard.setId(vcId);
        virtualCard.setInUse((byte) 0);
        virtualCard.setLastOperate(LastOperateENUM.CLOSE_VALVE.getCode());
        virtualCard.setLastOperateTime(new Date());
        seVirtualCardMapper.updateByPrimaryKeySelective(virtualCard);
 
        Data myData = (Data)response.getContent();
        return BaseResponseUtils.buildSuccess(myData) ;
    }
 
    /**
     * 根据操作员ID获取未关阀记录
     * @param operator
     * @return
     */
    @GetMapping(path = "/get")
    @SsoAop()
    public BaseResponse<List<VoUnclosedValve>> getUnclosedValves(@RequestParam Long operator){
        try {
            List<VoUnclosedValve> res = commandSv.getUnclosedValves(operator);
            return BaseResponseUtils.buildSuccess(res);
        } catch (Exception e) {
            log.error("获取未关阀记录异常", e);
            return BaseResponseUtils.buildException(e.getMessage()) ;
        }
    }
}