liurunyu
2024-01-29 c568a328a8c273531773710fe814ac429bafc48f
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
package com.dy.pipIrrProject.intakeController;
 
import com.dy.common.aop.SsoAop;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.common.webUtil.QueryResultVo;
import com.dy.common.webUtil.ResultCodeMsg;
import com.dy.pipIrrGlobal.pojoBa.BaClient;
import com.dy.pipIrrGlobal.pojoPr.PrController;
import com.dy.pipIrrGlobal.pojoPr.PrControllerTramp;
import com.dy.pipIrrGlobal.pojoPr.PrIntakeController;
import com.dy.pipIrrProject.controller.ControllerSv;
import com.dy.pipIrrProject.intake.IntakeSv;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * @author ZhuBaoMin
 * @date 2024-01-02 9:18
 * @LastEditTime 2024-01-02 9:18
 * @Description
 */
 
@Slf4j
@Tag(name = "取水口/控制器关联管理", description = "取水口/控制器关联操作")
@RestController
@RequestMapping(path="intake_controller")
@RequiredArgsConstructor
public class IntakeControllerCtrl {
    private final IntakeControllerSv intakeControllerSv;
    private final IntakeSv intakeSv;
    private final ControllerSv controllerSv;
 
    /**
     * 取水口绑定控制器
     * 1. 接收取水口ID及流浪控制器ID,并验证取水口和控制器是否存在
     * 2. 判断该控制器是否存在未解绑记录,如果存在提示用户该控制器存在未解绑记录
     * 3. 组装控制器对象插入控制器表中
     * 4. 添加绑定记录
     * 5. 删除流浪控制器(物理删除)
     * @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 DtoIntakeController po, BindingResult bindingResult) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(bindingResult != null && bindingResult.hasErrors()){
            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        // 接收参数
        Long intakeId = po.getIntakeId();
        Long controllerId = po.getControllerId();
        String remarks = po.getRemarks();
        Long operator = po.getOperator();
 
        // 根据编号分别获取取水口记录数、流浪控制器信息,判断取水口及控制器是否存在
        Integer recIntke = Optional.ofNullable(intakeSv.getRecordCountOfIntake(intakeId)).orElse(0);
        PrControllerTramp prControllerTramp = intakeControllerSv.getTrampControllerInfo(controllerId);
        //Map map_TrampController = Optional.ofNullable(intakeControllerSv.getTrampControllerInfo(controllerId)).orElse(new HashMap());
        if(recIntke == 0 || prControllerTramp == null) {
            return BaseResponseUtils.buildFail(ProjectResultCode.INTAKE_OR_CONTROLLER_NO_EXIST.getMessage());
        }
 
        // 根据控制器编号判断该控制器是否存在未解绑记录
        Integer recBinded = Optional.ofNullable(controllerSv.getBindedCount(controllerId)).orElse(0);
        if(recBinded > 0) {
            return BaseResponseUtils.buildFail(ProjectResultCode.CONTROLLER_BINDED.getMessage());
        }
 
        // 添加控制器记录
        PrController prController = new PrController();
        prController.setIntakeId(intakeId);
        prController.setRtuAddr(prControllerTramp.getRtuAddr());
        prController.setProtocol(prControllerTramp.getProtocol());
        prController.setFindDt(prControllerTramp.getFindDt());
        prController.setAddWays((byte)1);
        prController.setOperator(operator);
        Date operateTime = new Date();
        prController.setOperateDt(operateTime);
        prController.setDeleted((byte)0);
        Integer rec_addController = Optional.ofNullable(controllerSv.addController(prController)).orElse(0);
        if(rec_addController == 0) {
            return BaseResponseUtils.buildFail(ProjectResultCode.BIND_FAIL.getMessage());
        }
 
        // 添加绑定记录
        PrIntakeController prIntakeController = DtoToPojo.INSTANCT.po2vo(po);
        prIntakeController.setOperatedt(operateTime);
        prIntakeController.setOperatetype((byte)1);
        Integer rec = Optional.ofNullable(intakeControllerSv.addRecord(prIntakeController)).orElse(0);
        if(rec == 0) {
            return BaseResponseUtils.buildFail(ProjectResultCode.BIND_FAIL.getMessage());
        }
 
        // 根据流浪控制器编号删除流浪控制器记录
        intakeControllerSv.deleteTrampController(controllerId);
 
        return BaseResponseUtils.buildSuccess(true) ;
    }
 
    /**
     * 取水口解绑控制器
     * 1. 接收取水口ID及流浪控制器ID,并验证取水口和控制器是否存在
     * 2. 添加解绑记录
     * 3. 删除控制器外键
     * @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 = "unbind", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    @SsoAop()
    public BaseResponse<Boolean> unbind(@RequestBody @Valid DtoIntakeController po, BindingResult bindingResult){
        if(bindingResult != null && bindingResult.hasErrors()){
            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
 
        // 根据编号分别获取取水口、控制器未删除记录数
        Integer recIntke = Optional.ofNullable(intakeSv.getRecordCountOfIntake(po.getIntakeId())).orElse(0);
        Integer recController = Optional.ofNullable(controllerSv.getRecordCountOfController(po.getControllerId())).orElse(0);
        if(recIntke == 0 || recController == 0) {
            return BaseResponseUtils.buildFail(ProjectResultCode.INTAKE_OR_CONTROLLER_NO_EXIST.getMessage());
        }
 
        // 判断最后操作记录是否为解绑记录
        //Integer recordCount = Optional.ofNullable(intakeControllerSv.getBindRecordCount(po.getIntakeId(), po.getControllerId(), (byte)2)).orElse(0);
        //if(recordCount > 0) {
        //    return BaseResponseUtils.buildFail(ProjectResultCode.INTAKE_CONTROLLER_HAS_UNBOUND.getMessage());
        //}
 
        // 添加解绑记录
        PrIntakeController prIntakeController = DtoToPojo.INSTANCT.po2vo(po);
        Date operateTime = new Date();
        prIntakeController.setOperatedt(operateTime);
        prIntakeController.setOperatetype((byte)2);
        Integer rec = Optional.ofNullable(intakeControllerSv.addRecord(prIntakeController)).orElse(0);
        if(rec == 0) {
            return BaseResponseUtils.buildFail(ProjectResultCode.BIND_FAIL.getMessage());
        }
 
        // 删除控制器外键
        Integer rec_deleteIntakeId = Optional.ofNullable(controllerSv.deleteIntakeId(po.getControllerId())).orElse(0);
        if(rec_deleteIntakeId == 0) {
            return BaseResponseUtils.buildFail(ProjectResultCode.BIND_FAIL.getMessage());
        }
 
        return BaseResponseUtils.buildSuccess(true) ;
    }
 
    /**
     * 根据取水口编号获取绑定记录列表
     * @param intakeId 取水口编号
     * @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 = BaClient.class))}
            )
    })
    @GetMapping(path = "intake_binds")
    @SsoAop()
    public BaseResponse<QueryResultVo<List<Map<String, Object>>>> getBindsByIntakeId(Long intakeId){
        try {
            List<Map<String, Object>> list = Optional.ofNullable(intakeControllerSv.getBindsByIntakeId(intakeId)).orElse(new ArrayList<>());
            if(list.size() <= 0) {
                return BaseResponseUtils.buildFail(ProjectResultCode.INTAKE_NO_RECORDS.getMessage());
            }
            return BaseResponseUtils.buildSuccess(list);
        } catch (Exception e) {
            log.error("查询农户异常", e);
            return BaseResponseUtils.buildException(e.getMessage()) ;
        }
    }
 
    /**
     * 根据控制器编号获取绑定记录列表
     * @param controllerId 控制器编号
     * @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 = BaClient.class))}
            )
    })
    @GetMapping(path = "controller_binds")
    @SsoAop()
    public BaseResponse<QueryResultVo<List<Map<String, Object>>>> getBindsByControllerId(Long controllerId){
        try {
            List<Map<String, Object>> list = Optional.ofNullable(intakeControllerSv.getBindsByControllerId(controllerId)).orElse(new ArrayList<>());
            if(list.size() <= 0) {
                return BaseResponseUtils.buildFail(ProjectResultCode.CONTROLLER_NO_RECORDS.getMessage());
            }
            return BaseResponseUtils.buildSuccess(list);
        } catch (Exception e) {
            log.error("查询农户异常", e);
            return BaseResponseUtils.buildException(e.getMessage()) ;
        }
    }
}