Administrator
2024-01-05 8ffdb9e3170e5ca530b652cb4600046bfb08a925
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
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.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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.Date;
import java.util.Objects;
import java.util.Optional;
 
/**
 * @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.getRecordCountOfFlowMonitoring(po.getMonitoringId())).orElse(0);
        Integer recFlowmeter = Optional.ofNullable(flowmeterSv.getRecordCountOfFlowmeter(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);
 
        Integer rec = Optional.ofNullable(moniFlowSv.addRecord(prMonitoringFlowmeter)).orElse(0);
        if(rec == 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.getRecordCountOfFlowMonitoring(po.getMonitoringId())).orElse(0);
        Integer recFlowmeter = Optional.ofNullable(flowmeterSv.getRecordCountOfFlowmeter(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)1);
 
        Integer rec = Optional.ofNullable(moniFlowSv.addRecord(prMonitoringFlowmeter)).orElse(0);
        if(rec == 0) {
            return BaseResponseUtils.buildFail(ProjectResultCode.MONITORING_FLOWMETER_BIND_FAIL.getMessage());
        }
        return BaseResponseUtils.buildSuccess(true) ;
    }
}