|  |  | 
 |  |  | 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 org.springframework.validation.BindingResult; | 
 |  |  | import org.springframework.web.bind.annotation.*; | 
 |  |  |  | 
 |  |  | import java.text.ParseException; | 
 |  |  | import java.text.SimpleDateFormat; | 
 |  |  | import java.util.*; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  | 
 |  |  | @Slf4j | 
 |  |  | @Tag(name = "取水口/控制器关联管理", description = "取水口/控制器关联操作") | 
 |  |  | @RestController | 
 |  |  | @RequestMapping(path="intake_controller") | 
 |  |  | @RequestMapping(path = "intake_controller") | 
 |  |  | @RequiredArgsConstructor | 
 |  |  | public class IntakeControllerCtrl { | 
 |  |  |     private final IntakeControllerSv intakeControllerSv; | 
 |  |  | 
 |  |  |     private final ControllerSv controllerSv; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 添加取水口/控制器捆绑记录 | 
 |  |  |      *      若取水口或控制器不存在需提示用户 | 
 |  |  |      *      若取水口已经与控制器绑定需提示用户 | 
 |  |  |      * 取水口绑定控制器 | 
 |  |  |      * 1. 接收取水口ID及流浪控制器ID,并验证取水口和控制器是否存在 | 
 |  |  |      * 2. 判断该控制器是否存在未解绑记录,如果存在提示用户该控制器存在未解绑记录 | 
 |  |  |      * 3. 组装控制器对象插入控制器表中(及添加绑定记录) | 
 |  |  |      * 4. 修改绑定记录设置备注 | 
 |  |  |      * 5. 删除流浪控制器(物理删除) | 
 |  |  |      * | 
 |  |  |      * @param po | 
 |  |  |      * @param bindingResult | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @Operation(summary = "添加绑定记录", description = "添加绑定记录") | 
 |  |  |     @ApiResponses(value = { | 
 |  |  | 
 |  |  |     @PostMapping(path = "bind", consumes = MediaType.APPLICATION_JSON_VALUE) | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     @SsoAop() | 
 |  |  |     public BaseResponse<Boolean> bind(@RequestBody @Valid DtoIntakeController po, BindingResult bindingResult){ | 
 |  |  |         if(bindingResult != null && bindingResult.hasErrors()){ | 
 |  |  |     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()); | 
 |  |  |         } | 
 |  |  |         // 根据编号分别获取取水口、控制器未删除记录数 | 
 |  |  |         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) { | 
 |  |  |         // 接收参数 | 
 |  |  |         Long intakeId = po.getIntakeId(); | 
 |  |  |         Long trampControllerId = po.getControllerId(); | 
 |  |  |         String remarks = po.getRemarks(); | 
 |  |  |         Long operator = po.getOperator(); | 
 |  |  |  | 
 |  |  |         // 根据编号分别获取取水口记录数、流浪控制器信息,判断取水口及控制器是否存在 | 
 |  |  |         Integer recIntke = Optional.ofNullable(intakeSv.getRecordCountOfIntake(intakeId)).orElse(0); | 
 |  |  |         PrControllerTramp prControllerTramp = intakeControllerSv.getTrampControllerInfo(trampControllerId); | 
 |  |  |         //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 recordCount = Optional.ofNullable(intakeControllerSv.getBindRecordCount(po.getIntakeId(), po.getControllerId(), (byte)1)).orElse(0); | 
 |  |  |         if(recordCount > 0) { | 
 |  |  |             return BaseResponseUtils.buildFail(ProjectResultCode.INTAKE_HAS_BINDED_CONTROLLER.getMessage()); | 
 |  |  |         // 根据控制器编号判断该控制器是否存在未解绑记录 | 
 |  |  |         Integer recBinded = Optional.ofNullable(controllerSv.getBindedCount(trampControllerId)).orElse(0); | 
 |  |  |         if (recBinded > 0) { | 
 |  |  |             return BaseResponseUtils.buildFail(ProjectResultCode.CONTROLLER_BINDED.getMessage()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         PrIntakeController prIntakeController = DtoToPojo.INSTANCT.po2vo(po); | 
 |  |  |         // 添加控制器记录 | 
 |  |  |         PrController prController = new PrController(); | 
 |  |  |         prController.setIntakeId(intakeId); | 
 |  |  |         prController.setRtuAddr(prControllerTramp.getRtuAddr()); | 
 |  |  |         prController.setProtocol(prControllerTramp.getProtocol()); | 
 |  |  |         prController.setProtocolVersion(prControllerTramp.getProtocolVersion()); | 
 |  |  |         prController.setFindDt(prControllerTramp.getFindDt()); | 
 |  |  |         prController.setOrgTag(prControllerTramp.getOrgTag()); | 
 |  |  |         prController.setAddWays((byte) 1); | 
 |  |  |         prController.setOperator(operator); | 
 |  |  |         Date operateTime = new Date(); | 
 |  |  |         prIntakeController.setOperatedt(operateTime); | 
 |  |  |         prIntakeController.setOperatetype((byte)1); | 
 |  |  |  | 
 |  |  |         Integer rec = Optional.ofNullable(intakeControllerSv.addRecord(prIntakeController)).orElse(0); | 
 |  |  |         if(rec == 0) { | 
 |  |  |         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()); | 
 |  |  |         } | 
 |  |  |         return BaseResponseUtils.buildSuccess(true) ; | 
 |  |  | //        if (remarks != null || remarks !=""){ | 
 |  |  | //            // 修改绑定记录 | 
 |  |  | //            PrIntakeController prIntakeController = new PrIntakeController(); | 
 |  |  | //            prIntakeController.setRemarks(remarks); | 
 |  |  | //            prIntakeController.setIntakeid(intakeId); | 
 |  |  | // | 
 |  |  | //            Integer rec = Optional.ofNullable(intakeControllerSv.updateBindRecord(prIntakeController)).orElse(0); | 
 |  |  | //            if (rec == 0) { | 
 |  |  | //                return BaseResponseUtils.buildFail(ProjectResultCode.BIND_FAIL.getMessage()); | 
 |  |  | //            } | 
 |  |  | //        } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         // 根据流浪控制器编号删除流浪控制器记录 | 
 |  |  |         intakeControllerSv.deleteTrampController(trampControllerId); | 
 |  |  |  | 
 |  |  |         return BaseResponseUtils.buildSuccess(true); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 添加取水口/控制器解绑记录 | 
 |  |  |      * 1. 判断取水口是否存在 | 
 |  |  |      * 2. 判断控制器是否存在 | 
 |  |  |      * 3. 判断取水口、控制器解绑关系是否已存在 | 
 |  |  |      * 取水口解绑控制器 | 
 |  |  |      * 1. 接收取水口ID及流浪控制器ID,并验证取水口和控制器是否存在 | 
 |  |  |      * 2. 添加解绑记录 | 
 |  |  |      * 3. 删除控制器外键 | 
 |  |  |      * | 
 |  |  |      * @param po | 
 |  |  |      * @param bindingResult | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @Operation(summary = "添加解绑记录", description = "添加解绑记录") | 
 |  |  |     @ApiResponses(value = { | 
 |  |  | 
 |  |  |     @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()){ | 
 |  |  |     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) { | 
 |  |  |         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()); | 
 |  |  |         } | 
 |  |  |         // 判断最后操作记录是否为解绑记录 | 
 |  |  |         //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); | 
 |  |  |  | 
 |  |  |         prIntakeController.setOperatetype((byte) 2); | 
 |  |  |         prIntakeController.setRemarks("解绑"); | 
 |  |  |         Integer rec = Optional.ofNullable(intakeControllerSv.addRecord(prIntakeController)).orElse(0); | 
 |  |  |         if(rec == 0) { | 
 |  |  |         if (rec == 0) { | 
 |  |  |             return BaseResponseUtils.buildFail(ProjectResultCode.BIND_FAIL.getMessage()); | 
 |  |  |         } | 
 |  |  |         return BaseResponseUtils.buildSuccess(true) ; | 
 |  |  |         //添加到流浪控制器 | 
 |  |  |         PrController ControllerList = controllerSv.getByControllerId(po.getControllerId()); | 
 |  |  |         PrControllerTramp prControllerTramp = new PrControllerTramp(); | 
 |  |  |         prControllerTramp.setRtuAddr(ControllerList.getRtuAddr()); | 
 |  |  |         prControllerTramp.setProtocol(ControllerList.getProtocol()); | 
 |  |  |         prControllerTramp.setFindDt(new Date()); | 
 |  |  |         prControllerTramp.setOrgTag(ControllerList.getOrgTag()); | 
 |  |  |         Integer rec_addControllerTramp = Optional.ofNullable(intakeControllerSv.addTrampController(prControllerTramp)).orElse(0); | 
 |  |  |         if (rec_addControllerTramp == 0) { | 
 |  |  |             return BaseResponseUtils.buildFail(ProjectResultCode.ADD_TRAMP_CONTROLLER_FAIL.getMessage()); | 
 |  |  |         } | 
 |  |  |         // 删除控制器 | 
 |  |  |         Integer rec_deleteIntakeId = Optional.ofNullable(controllerSv.deleteControllerByIdTwo(po.getControllerId())).orElse(0); | 
 |  |  |         if (rec_deleteIntakeId == 0) { | 
 |  |  |             return BaseResponseUtils.buildFail(ProjectResultCode.BIND_FAIL.getMessage()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         return BaseResponseUtils.buildSuccess(true); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 根据取水口编号获取绑定记录列表 | 
 |  |  |      * | 
 |  |  |      * @param intakeId 取水口编号 | 
 |  |  |      * @return 取水口下控制器的绑定、解绑记录 | 
 |  |  |      */ | 
 |  |  | 
 |  |  |     }) | 
 |  |  |     @GetMapping(path = "intake_binds") | 
 |  |  |     @SsoAop() | 
 |  |  |     public BaseResponse<QueryResultVo<List<Map<String, Object>>>> getBindsByIntakeId(Long intakeId){ | 
 |  |  |     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()); | 
 |  |  |             } | 
 |  |  |             if (list.size() <= 0) { | 
 |  |  |                 //无绑定记录也返回成功 但数据为空 | 
 |  |  |                 return BaseResponseUtils.buildSuccess(list); | 
 |  |  |             }//成功返回绑定记录数据 | 
 |  |  |             return BaseResponseUtils.buildSuccess(list); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error("查询农户异常", e); | 
 |  |  |             return BaseResponseUtils.buildException(e.getMessage()) ; | 
 |  |  |             log.error("查询异常", e); | 
 |  |  |             return BaseResponseUtils.buildException(e.getMessage()); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 根据控制器编号获取绑定记录列表 | 
 |  |  |      * | 
 |  |  |      * @param controllerId 控制器编号 | 
 |  |  |      * @return 与该控制器相关的绑定、解绑记录 | 
 |  |  |      */ | 
 |  |  | 
 |  |  |     }) | 
 |  |  |     @GetMapping(path = "controller_binds") | 
 |  |  |     @SsoAop() | 
 |  |  |     public BaseResponse<QueryResultVo<List<Map<String, Object>>>> getBindsByControllerId(Long controllerId){ | 
 |  |  |     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()); | 
 |  |  |             if (list.size() <= 0) { | 
 |  |  |                 //无绑定记录也返回成功 但数据为空 | 
 |  |  |                 return BaseResponseUtils.buildSuccess(list); | 
 |  |  |             } | 
 |  |  |             return BaseResponseUtils.buildSuccess(list); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error("查询农户异常", e); | 
 |  |  |             return BaseResponseUtils.buildException(e.getMessage()) ; | 
 |  |  |             log.error("查询异常", e); | 
 |  |  |             return BaseResponseUtils.buildException(e.getMessage()); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 根据指定条件获取流浪控制器列表 | 
 |  |  |      * | 
 |  |  |      * @param vo 查询条件 | 
 |  |  |      * @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 = PrControllerTramp.class))} | 
 |  |  |             ) | 
 |  |  |     }) | 
 |  |  |     @GetMapping(path = "/getTrampControllers") | 
 |  |  |     @SsoAop() | 
 |  |  |     public BaseResponse<QueryResultVo<List<PrControllerTramp>>> getTrampControllers(TrampControllerQueryVo vo) { | 
 |  |  |         try { | 
 |  |  |             //QueryResultVo<List<PrControllerTramp>> res = intakeControllerSv.getTrampControllers(vo); | 
 |  |  |             //if (res.itemTotal != null && res.itemTotal > 0) { | 
 |  |  |             //    return BaseResponseUtils.buildSuccess(res); | 
 |  |  |             //} else { | 
 |  |  |             //    //请求成功但数据为空 | 
 |  |  |             //    return BaseResponseUtils.buildSuccess(res); | 
 |  |  |             //} | 
 |  |  |             return BaseResponseUtils.buildSuccess(intakeControllerSv.getTrampControllers(vo)); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error("获取流浪控制器记录异常", e); | 
 |  |  |             return BaseResponseUtils.buildException(e.getMessage()); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 添加流浪控制器 | 
 |  |  |      * | 
 |  |  |      * @param pct | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @PostMapping("addTrampController") | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     @SsoAop() | 
 |  |  |     public BaseResponse<Boolean> addTrampController(@RequestBody PrControllerTramp pct) { | 
 |  |  |         pct.setFindDt(new Date()); | 
 |  |  |         Integer rows = intakeControllerSv.addTrampController(pct); | 
 |  |  |         if (rows == 0) { | 
 |  |  |             return BaseResponseUtils.buildErrorMsg(ProjectResultCode.ADD_TRAMP_CONTROLLER_FAIL.getMessage()); | 
 |  |  |         } | 
 |  |  |         return BaseResponseUtils.buildSuccess(true); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 删除流浪控制器 | 
 |  |  |      * | 
 |  |  |      * @param id | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @GetMapping("deleteTrampController") | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     @SsoAop() | 
 |  |  |     public BaseResponse<Boolean> deleteTrampController(Long id) { | 
 |  |  |         PrControllerTramp controllerInfo = intakeControllerSv.getTrampControllerInfo(id); | 
 |  |  |         if (controllerInfo == null) { | 
 |  |  |             return BaseResponseUtils.buildErrorMsg(ProjectResultCode.NO_EXIST_TRAMP_CONTROLLER.getMessage()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         Integer rows = intakeControllerSv.deleteTrampController(id); | 
 |  |  | //        if (rows == 0) { | 
 |  |  | //            return BaseResponseUtils.buildErrorMsg(ProjectResultCode.DELETE_TRAMP_CONTROLLER_FAIL.getMessage()); | 
 |  |  | //        } | 
 |  |  |         return BaseResponseUtils.buildSuccess(true); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 清空流浪控制器 | 
 |  |  |      * | 
 |  |  |      * @param | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @PostMapping("emptyTrampController") | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     @SsoAop() | 
 |  |  |     public BaseResponse<Boolean> emptyTrampController() { | 
 |  |  |  | 
 |  |  |         Integer rows = intakeControllerSv.emptyTrampController(); | 
 |  |  |         if (rows == 0) { | 
 |  |  |             return BaseResponseUtils.buildErrorMsg(ProjectResultCode.DELETE_TRAMP_CONTROLLER_FAIL.getMessage()); | 
 |  |  |         } | 
 |  |  |         return BaseResponseUtils.buildSuccess(true); | 
 |  |  |     } | 
 |  |  | } |