zhubaomin
2024-10-12 2a959177f1adcd36f790e14718bbd116752e5c64
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
package com.dy.pipIrrProject.flowMonitoring;
 
import com.dy.common.aop.SsoAop;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.common.webUtil.ResultCodeMsg;
import com.dy.pipIrrGlobal.pojoPr.PrFlowMonitoring;
import com.dy.pipIrrGlobal.pojoPr.PrFlowmeter;
import com.dy.pipIrrGlobal.pojoPr.PrMonitoringFlowmeter;
import com.dy.pipIrrProject.result.ProjectResultCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
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.util.*;
 
/**
 * @author ZhuBaoMin
 * @date 2024-01-05 10:26
 * @LastEditTime 2024-01-05 10:26
 * @Description 管网流量监测站、流量计关联控制类
 */
 
@Slf4j
@Tag(name = "流量监测站、流量计关联管理", description = "流量监测站、流量计关联操作")
@RestController
@RequestMapping(path = "moni_flow")
@RequiredArgsConstructor
public class MoniFlowCtrl {
    private final MoniFlowSv moniFlowSv;
 
    private final FlowMonitoringSv flowMonitoringSv;
    private final FlowmeterSv flowmeterSv;
 
    /**
     * 流量监测站绑定流量计
     * 若流量监测站或流量计不存在需提示用户
     * 若流量监测站已经与流量计绑定需提示用户
     *
     * @param po            绑定关系对象
     * @param bindingResult
     * @return
     */
    @Operation(summary = "添加绑定记录", description = "添加绑定记录")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = Boolean.class))}
            )
    })
    @PostMapping(path = "bind", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    @SsoAop()
    public BaseResponse<Boolean> bind(@RequestBody @Valid DtoMoniFlow po, BindingResult bindingResult) {
        if (bindingResult != null && bindingResult.hasErrors()) {
            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
 
        // 根据编号分别获取流量监测站、流量计未删除记录数
        Integer recFlowMonitoring = Optional.ofNullable(flowMonitoringSv.getRecordCountOfFlowMonitoringByFlowMonitoringId(po.getMonitoringId())).orElse(0);
        Integer recFlowmeter = Optional.ofNullable(flowmeterSv.getRecordCountOfFlowmeterByCode(po.getFlowmeterId())).orElse(0);
        if (recFlowMonitoring == 0 || recFlowmeter == 0) {
            return BaseResponseUtils.buildFail(ProjectResultCode.STATION_OR_FLOWMETER_NO_EXIST.getMessage());
        }
 
        // 根据编号获取已绑定数量
        Integer recordCount = Optional.ofNullable(moniFlowSv.getBindRecordCount(po.getMonitoringId(), po.getFlowmeterId(), (byte) 1)).orElse(0);
        if (recordCount > 0) {
            return BaseResponseUtils.buildFail(ProjectResultCode.MONITORING_HAS_BINDED_FLOWMETER.getMessage());
        }
 
        PrMonitoringFlowmeter prMonitoringFlowmeter = DtoToMoniFlowPojo.INSTANCT.po2vo(po);
        Date operateTime = new Date();
        prMonitoringFlowmeter.setOperatedt(operateTime);
        prMonitoringFlowmeter.setOperatetype((byte) 1);
 
        PrFlowmeter flowmeter = new PrFlowmeter();
        flowmeter.setMonitoringId(po.getMonitoringId().toString());
        flowmeter.setId(po.getFlowmeterId());
        flowmeter.setOperator(po.getOperator());
        flowmeter.setOperateDt(operateTime);
        Integer shu = flowmeterSv.flowmeterAddMonId(flowmeter);
 
        Integer rec = Optional.ofNullable(moniFlowSv.addRecord(prMonitoringFlowmeter)).orElse(0);
        if (rec == 0 || shu == 0) {
            return BaseResponseUtils.buildFail(ProjectResultCode.MONITORING_FLOWMETER_BIND_FAIL.getMessage());
        }
        return BaseResponseUtils.buildSuccess(true);
    }
 
    @Operation(summary = "添加解绑记录", description = "添加解绑记录")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = Boolean.class))}
            )
    })
    @PostMapping(path = "unbind", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    @SsoAop()
    public BaseResponse<Boolean> unbind(@RequestBody @Valid DtoMoniFlow po, BindingResult bindingResult) {
        if (bindingResult != null && bindingResult.hasErrors()) {
            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
 
        // 根据编号分别获取流量监测站、流量计未删除记录数
        Integer recFlowMonitoring = Optional.ofNullable(flowMonitoringSv.getRecordCountOfFlowMonitoringByFlowMonitoringId(po.getMonitoringId())).orElse(0);
        Integer recFlowmeter = Optional.ofNullable(flowmeterSv.getRecordCountOfFlowmeterByCode(po.getFlowmeterId())).orElse(0);
        if (recFlowMonitoring == 0 || recFlowmeter == 0) {
            return BaseResponseUtils.buildFail(ProjectResultCode.STATION_OR_FLOWMETER_NO_EXIST.getMessage());
        }
 
        // 根据编号获取已绑定数量
        Integer recordCount = Optional.ofNullable(moniFlowSv.getBindRecordCount(po.getMonitoringId(), po.getFlowmeterId(), (byte) 2)).orElse(0);
        if (recordCount > 0) {
            return BaseResponseUtils.buildFail(ProjectResultCode.STATION_FLOWMETER_HAS_UNBOUND.getMessage());
        }
 
        PrMonitoringFlowmeter prMonitoringFlowmeter = DtoToMoniFlowPojo.INSTANCT.po2vo(po);
        Date operateTime = new Date();
        prMonitoringFlowmeter.setOperatedt(operateTime);
        prMonitoringFlowmeter.setOperatetype((byte) 2);
 
        PrFlowmeter flowmeter = new PrFlowmeter();
        flowmeter.setMonitoringId(null);
        flowmeter.setId(po.getFlowmeterId());
        flowmeter.setOperator(po.getOperator());
        flowmeter.setOperateDt(operateTime);
        Integer shu = flowmeterSv.flowmeterAddMonId(flowmeter);
 
        Integer rec = Optional.ofNullable(moniFlowSv.addRecord(prMonitoringFlowmeter)).orElse(0);
        if (rec == 0 || shu == 0) {
            return BaseResponseUtils.buildFail(ProjectResultCode.MONITORING_FLOWMETER_BIND_FAIL.getMessage());
        }
        return BaseResponseUtils.buildSuccess(true);
    }
 
    /**
     * 根据监测站编号获取绑定记录 按操作时间降序
     *
     * @param monitoringId 监测站编号
     * @return 绑定记录
     */
    @Operation(summary = "获取绑定记录", description = "返回获取绑定记录")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "返回绑定记录(BaseResponse.content:QueryResultVo[{}])",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = PrMonitoringFlowmeter.class))}
            )
    })
    @GetMapping(path = "bingRecords/{monitoringId}")
    @SsoAop()
    public BaseResponse<List<Map<String, Object>>> getBingRecordsByMonitoringId(@PathVariable("monitoringId") String monitoringId) {
        try {
            List<Map<String, Object>> list = Optional.ofNullable(moniFlowSv.getBingRecordsByMonitoringId(monitoringId)).orElse(new ArrayList<>());
            if (list.size() <= 0) {
                return BaseResponseUtils.buildFail(ProjectResultCode.NO_MONITORING_FLOWMETERS.getMessage());
            }
            return BaseResponseUtils.buildSuccess(list);
        } catch (Exception e) {
            log.error("查询绑定记录异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
    }
}