|  |  | 
 |  |  | import com.dy.common.mw.protocol.p206V202404.downVos.ComCd93_A3Vo; | 
 |  |  | import com.dy.common.util.IDLongGenerator; | 
 |  |  | import com.dy.common.webUtil.BaseResponse; | 
 |  |  | import com.dy.common.webUtil.BaseResponseUtils; | 
 |  |  | import com.dy.common.webUtil.QueryResultVo; | 
 |  |  | import com.dy.pipIrrGlobal.command.ComSupport; | 
 |  |  | import com.dy.pipIrrGlobal.command.dto.Param; | 
 |  |  | import com.dy.pipIrrGlobal.daoIr.IrIntakeOperateMapper; | 
 |  |  | import com.dy.pipIrrGlobal.daoPr.PrIntakeControllerMapper; | 
 |  |  | import com.dy.pipIrrGlobal.daoPr.PrIntakeMapper; | 
 |  |  | import com.dy.pipIrrGlobal.daoPr.PrIntakeVcMapper; | 
 |  |  | import com.dy.pipIrrGlobal.daoPr.PrWaterPriceMapper; | 
 |  |  | 
 |  |  | import com.dy.pipIrrWechat.command.dto.AutomaticClose; | 
 |  |  | import com.dy.pipIrrWechat.command.dto.ValveClose; | 
 |  |  | import com.dy.pipIrrWechat.command.dto.ValveOpen; | 
 |  |  | import com.dy.pipIrrWechat.irrigation.IrrigationSv; | 
 |  |  | import com.dy.pipIrrWechat.result.WechatResultCode; | 
 |  |  | import lombok.RequiredArgsConstructor; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | 
 |  |  | import org.springframework.beans.factory.annotation.Value; | 
 |  |  | import org.springframework.core.env.Environment; | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  | import org.springframework.transaction.annotation.Transactional; | 
 |  |  |  | 
 |  |  | import java.util.*; | 
 |  |  |  | 
 |  |  | 
 |  |  |     private final RmOpenCloseValveLastMapper rmOpenCloseValveLastMapper; | 
 |  |  |     //private final PrCommonIntakesMapper prCommonIntakesMapper; | 
 |  |  |     private final IrIntakeOperateMapper irIntakeOperateMapper; | 
 |  |  |     private final IrrigationSv irrigationSv; | 
 |  |  |     private final PrIntakeControllerMapper prIntakeControllerMapper; | 
 |  |  |  | 
 |  |  |     @Value("${mw.rtuCallbackUrl_wx}") | 
 |  |  |     private String rtuCallbackUrl_wx; | 
 |  |  | 
 |  |  |     private String pro_mw = "mw"; | 
 |  |  |     private String key_mw = "comSendUrl"; | 
 |  |  |     @Autowired | 
 |  |  |     public CommandSv(RmCommandHistoryMapper rmCommandHistoryMapper,RmOpenCloseValveLastMapper rmOpenCloseValveLastMapper, SeVirtualCardMapper seVirtualCardMapper, PrIntakeMapper prIntakeMapper, PrWaterPriceMapper prWaterPriceMapper, PrIntakeVcMapper prIntakeVcMapper, SeClientCardMapper seClientCardMapper, RmIrrigateProfileMapper rmIrrigateProfileMapper, Environment env, IrIntakeOperateMapper irIntakeOperateMapper) { | 
 |  |  |     public CommandSv(RmCommandHistoryMapper rmCommandHistoryMapper,RmOpenCloseValveLastMapper rmOpenCloseValveLastMapper, SeVirtualCardMapper seVirtualCardMapper, PrIntakeMapper prIntakeMapper, PrWaterPriceMapper prWaterPriceMapper, PrIntakeVcMapper prIntakeVcMapper, SeClientCardMapper seClientCardMapper, RmIrrigateProfileMapper rmIrrigateProfileMapper, Environment env, IrIntakeOperateMapper irIntakeOperateMapper, IrrigationSv irrigationSv, PrIntakeControllerMapper prIntakeControllerMapper) { | 
 |  |  |         this.rmCommandHistoryMapper = rmCommandHistoryMapper; | 
 |  |  |         this.rmOpenCloseValveLastMapper = rmOpenCloseValveLastMapper; | 
 |  |  |         //this.prCommonIntakesMapper = prCommonIntakesMapper; | 
 |  |  | 
 |  |  |         this.rmIrrigateProfileMapper = rmIrrigateProfileMapper; | 
 |  |  |         this.env = env; | 
 |  |  |         this.irIntakeOperateMapper = irIntakeOperateMapper; | 
 |  |  |         this.irrigationSv = irrigationSv; | 
 |  |  |         this.prIntakeControllerMapper = prIntakeControllerMapper; | 
 |  |  |     } | 
 |  |  |     /** | 
 |  |  |      * 虚拟卡ID换虚拟卡对象 | 
 |  |  |      * @param vcId | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |  | 
 |  |  |     public VoVirtualCard getVcById(Long vcId) { | 
 |  |  |         return seVirtualCardMapper.getVcById(vcId); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取水价 | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |  | 
 |  |  |     public Double getPrice() { | 
 |  |  |         return prWaterPriceMapper.getPrice(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 根据操作员ID获取未关阀记录(包含在线情况) | 
 |  |  |      * @param operator | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public List<VoUnclosedValve> getUnclosedValves(Long operator) { | 
 |  |  |         Command com = new Command() ; | 
 |  |  |         com.id = Command.defaultId; | 
 |  |  | 
 |  |  |         com.type = "innerCommand"; | 
 |  |  |         comSendUrl = env.getProperty(pro_mw + "." + DataSourceContext.get() + "." + key_mw); | 
 |  |  |         JSONObject response = (JSONObject) JSON.toJSON(sendCom2Mw(com)); | 
 |  |  |         String aa = response.toJSONString(); | 
 |  |  |  | 
 |  |  |         JSONArray jsonArray = new JSONArray(); | 
 |  |  |         if(response != null && response.getString("code").equals("0001")) { | 
 |  |  | 
 |  |  |             //System.out.println(jsonArray.toJSONString()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         /** | 
 |  |  |          * 获取未关阀的RTU地址和虚拟卡编号对数组 | 
 |  |  |          * 遍历数组获取最后命令日志ID集合 | 
 |  |  |          */ | 
 |  |  |         List<VoRtuAndVc> rtus = rmCommandHistoryMapper.getUnclosedRtus(operator); | 
 |  |  |         String comIds = ""; | 
 |  |  |         if(rtus != null && rtus.size() > 0) { | 
 |  |  | 
 |  |  |         return new ArrayList<>(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 为指定(阀控器地址、水卡编号、无关阀记录)开关阀最新记录添加关阀时间,使之不在未关阀记录中显示 | 
 |  |  |      * @param rtuAddr | 
 |  |  |      * @param icCardNo | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public Integer addClostTime(String rtuAddr, String icCardNo) { | 
 |  |  |         return rmOpenCloseValveLastMapper.addCloseTime(rtuAddr, icCardNo); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 根据取水口ID获取与之绑定虚拟卡ID | 
 |  |  |      * @param intakeId | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public Long getVcIdByIntakeId(Long intakeId) { | 
 |  |  |         return prIntakeVcMapper.getVcIdByIntakeId(intakeId); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 根据水卡编号获取水卡对象,远程充值使用 | 
 |  |  |      * @param cardId | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public SeClientCard geClientCardByCardId(Long cardId) { | 
 |  |  |         return seClientCardMapper.selectByPrimaryKey(cardId); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 添加命令日志 | 
 |  |  |      * @param po 命令日志对象 | 
 |  |  |      * @return 字符串类型的主键 | 
 |  |  |      */ | 
 |  |  |     public Long insert(RmCommandHistory po) { | 
 |  |  |         rmCommandHistoryMapper.insert(po); | 
 |  |  |         return po.getComId(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 修改命令日志信息 | 
 |  |  |      * @param po 命令日志对象 | 
 |  |  |      * @return 影响记录数量 | 
 |  |  |      */ | 
 |  |  |     public Integer update(RmCommandHistory po) { | 
 |  |  |         return rmCommandHistoryMapper.updateByPrimaryKeySelective(po); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 取水口名称换取水口ID,扫码开阀使用 | 
 |  |  |      * @param intakeName | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public Long getIntakeIdByName(String intakeName) { | 
 |  |  |         return prIntakeMapper.getIntakeIdByName(intakeName); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 根据配置类型获取配置值 | 
 |  |  |      * @param type | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     QueryResultVo<List<VoIrrigaterProfile>> getIrrPro(Integer type) { | 
 |  |  |         QueryResultVo<List<VoIrrigaterProfile>> rsVo = new QueryResultVo<>() ; | 
 |  |  |         rsVo.obj = rmIrrigateProfileMapper.getIrrPro(type); | 
 |  |  |         return rsVo ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 添加取水口操作记录,轮灌过程命令下发时使用 | 
 |  |  |      * @param po | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public Long addIntakeOperate(IrIntakeOperate po) { | 
 |  |  |         irIntakeOperateMapper.insert(po); | 
 |  |  |         return po.getId(); | 
 |  |  | 
 |  |  |     //    return irIntakeOperateMapper.updateByCommandId(commandId, commandResult); | 
 |  |  |     //} | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 定时关阀式计划开阀 | 
 |  |  |      * @param automaticClose | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     public Map planedOpenTimedClose(AutomaticClose automaticClose, Long planId, Byte operatetype, Date startTime, Integer duration) { | 
 |  |  |         Long intakeId = automaticClose.getIntakeId(); | 
 |  |  |         Long vcId = automaticClose.getVcId(); | 
 |  |  | 
 |  |  |         Long operator = automaticClose.getOperator(); | 
 |  |  |         Byte openType = automaticClose.getOpenType(); | 
 |  |  |         Long comId = new IDLongGenerator().generate(); | 
 |  |  |         String intakeName = Optional.ofNullable(prIntakeControllerMapper.getIntakeNameById(intakeId)).orElse(""); | 
 |  |  |  | 
 |  |  |         // 如果是轮灌 | 
 |  |  |         Long operateId = null; | 
 |  |  |         if(openType != null && openType == 1) { | 
 |  |  |             IrIntakeOperate irIntakeOperate = new IrIntakeOperate(); | 
 |  |  |             irIntakeOperate.setPlanId(planId); | 
 |  |  | 
 |  |  |             irIntakeOperate.setStartTime(startTime); | 
 |  |  |             irIntakeOperate.setDuration(duration); | 
 |  |  |  | 
 |  |  |             Long id = addIntakeOperate(irIntakeOperate); | 
 |  |  |             if(id == null) { | 
 |  |  |             operateId = addIntakeOperate(irIntakeOperate); | 
 |  |  |             if(operateId == null) { | 
 |  |  |                 Map map = new HashMap<>(); | 
 |  |  |                 map.put("success", false); | 
 |  |  |                 map.put("msg", "添加取水口操作记录失败"); | 
 |  |  | 
 |  |  |         Integer hour = Integer.parseInt(String.format("%tH", plannedOpenTime)); | 
 |  |  |         Integer minute = Integer.parseInt(String.format("%tM", plannedOpenTime)); | 
 |  |  |  | 
 |  |  |         /** | 
 |  |  |          * 如果农户选择了虚拟卡,则使用该虚拟卡 | 
 |  |  |          * 如果农户未选择虚拟卡,则根据取水口ID获取与之绑定的虚拟卡 | 
 |  |  |          * 如果取水口没有与之绑定的虚拟卡,则提示农户选择一张虚拟卡 | 
 |  |  |          */ | 
 |  |  |         if(vcId == null) { | 
 |  |  |             vcId = getVcIdByIntakeId(intakeId); | 
 |  |  |             if(vcId == null) { | 
 |  |  | 
 |  |  |  | 
 |  |  |                 Map map = new HashMap<>(); | 
 |  |  |                 map.put("success", false); | 
 |  |  |                 map.put("msg", WechatResultCode.PLEASE_SELECT_A_VC.getMessage() + "(" + intakeId + ")"); | 
 |  |  |                 map.put("msg", WechatResultCode.PLEASE_SELECT_A_VC.getMessage() + "(" + intakeName + ")"); | 
 |  |  |                 map.put("content", null); | 
 |  |  |                 return map; | 
 |  |  |             } | 
 |  |  | 
 |  |  |  | 
 |  |  |             Map map = new HashMap<>(); | 
 |  |  |             map.put("success", false); | 
 |  |  |             map.put("msg", WechatResultCode.PLEASE_SELECT_A_VC.getMessage() + "(" + intakeId + ")"); | 
 |  |  |             map.put("msg", WechatResultCode.PLEASE_SELECT_A_VC.getMessage() + "(" + intakeName + ")"); | 
 |  |  |             map.put("content", null); | 
 |  |  |             return map; | 
 |  |  |         } | 
 |  |  |         String vcNum = vc.getVcNum().toString(); | 
 |  |  |         Double moneyRemain = vc.getMoney(); | 
 |  |  |         if(moneyRemain < 100) { | 
 |  |  |             irIntakeOperateMapper.updateByCommandId(comId, (byte)2, "虚拟卡余额不足,请充值" + "(" + intakeId + ")"); | 
 |  |  |             irIntakeOperateMapper.updateByCommandId(comId, (byte)2, "虚拟卡余额不足,请充值" + "(" + intakeName + ")"); | 
 |  |  |             Map map = new HashMap<>(); | 
 |  |  |             map.put("success", false); | 
 |  |  |             map.put("msg", "虚拟卡余额不足,请充值"); | 
 |  |  | 
 |  |  |         // 获取水价 | 
 |  |  |         Double waterPrice = getPrice(); | 
 |  |  |         if(waterPrice == null) { | 
 |  |  |             irIntakeOperateMapper.updateByCommandId(comId, (byte)2, WechatResultCode.NO_WATER_PRICE.getMessage() + "(" + intakeName + ")"); | 
 |  |  |             Map map = new HashMap<>(); | 
 |  |  |             map.put("success", false); | 
 |  |  |             map.put("msg", WechatResultCode.NO_WATER_PRICE.getMessage()); | 
 |  |  | 
 |  |  |         // 取水口ID换阀控器地址及通讯协议 | 
 |  |  |         JSONObject job_rtu = getRtu(intakeId, null); | 
 |  |  |         if(job_rtu == null) { | 
 |  |  |             irIntakeOperateMapper.updateByCommandId(comId, (byte)2, WechatResultCode.RTU_NOT_EXIST.getMessage() + "(" + intakeId + ")"); | 
 |  |  |             irIntakeOperateMapper.updateByCommandId(comId, (byte)2, WechatResultCode.RTU_NOT_EXIST.getMessage() + "(" + intakeName + ")"); | 
 |  |  |  | 
 |  |  |             Map map = new HashMap<>(); | 
 |  |  |             map.put("success", false); | 
 |  |  | 
 |  |  |         // 生成订单号 | 
 |  |  |         String orderNo = generateOrderNo(); | 
 |  |  |         if(orderNo == null) { | 
 |  |  |             irIntakeOperateMapper.updateByCommandId(comId, (byte)2, WechatResultCode.NO_ORDER_NUMBER.getMessage() + "(" + intakeId + ")"); | 
 |  |  |             irIntakeOperateMapper.updateByCommandId(comId, (byte)2, WechatResultCode.NO_ORDER_NUMBER.getMessage() + "(" + intakeName + ")"); | 
 |  |  |  | 
 |  |  |             Map map = new HashMap<>(); | 
 |  |  |             map.put("success", false); | 
 |  |  |             map.put("msg", WechatResultCode.NO_ORDER_NUMBER.getMessage()); | 
 |  |  |             map.put("content", null); | 
 |  |  |             return map; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 判断阀控器是否已经开阀 | 
 |  |  |         Boolean isOpen = irrigationSv.getOpenCloseStatus(rtuAddr); | 
 |  |  |         if(isOpen) { | 
 |  |  |             irIntakeOperateMapper.updateByCommandId(comId, (byte)2, intakeName + " 阀门为打开状态"); | 
 |  |  |             Map map = new HashMap<>(); | 
 |  |  |             map.put("success", false); | 
 |  |  |             map.put("msg", intakeName + " 阀门为打开状态"); | 
 |  |  |             map.put("content", null); | 
 |  |  |             return map; | 
 |  |  |         } | 
 |  |  | 
 |  |  |             param.setMinute(minute); | 
 |  |  |             param.setOrderNo(orderNo); | 
 |  |  |  | 
 |  |  |             /** | 
 |  |  |              * 构造命令、发送命令并处理请求结果及执行结果 | 
 |  |  |              * 1 准备参数 | 
 |  |  |              * 2 调用公共方法 | 
 |  |  |              */ | 
 |  |  |             Param myParam = new Param(); | 
 |  |  |             myParam.setComId(comId); | 
 |  |  |             myParam.setComType((byte)1); | 
 |  |  | 
 |  |  |             BaseResponse<Boolean> result = dealWithCommandResult(myParam); | 
 |  |  |             // 如果命令执行失败,则再次执行命令 | 
 |  |  |             if(!result.isSuccess()) { | 
 |  |  |                 myParam.setComId(new IDLongGenerator().generate()); | 
 |  |  |                 Long newComId = new IDLongGenerator().generate(); | 
 |  |  |                 myParam.setComId(newComId); | 
 |  |  |                 result = dealWithCommandResult(myParam); | 
 |  |  |  | 
 |  |  |                 // 根据operateId修改取水口操作记录中的commandId | 
 |  |  |                 irIntakeOperateMapper.updateCommandId(newComId, operateId); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             Map map = new HashMap<>(); | 
 |  |  | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 小程序远程开阀 | 
 |  |  |      * @param valve | 
 |  |  |      * @param planId | 
 |  |  |      * @param operatetype | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public Map openWx(ValveOpen valve, Long planId, Byte operatetype) { | 
 |  |  |         Long intakeId = valve.getIntakeId(); | 
 |  |  |         String intakeName = valve.getIntakeName(); | 
 |  |  | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         /** | 
 |  |  |          * 如果农户选择了虚拟卡,则使用该虚拟卡 | 
 |  |  |          * 如果农户未选择虚拟卡,则根据取水口ID获取与之绑定的虚拟卡 | 
 |  |  |          * 如果取水口没有与之绑定的虚拟卡,则提示农户选择一张虚拟卡 | 
 |  |  |          */ | 
 |  |  |         if (vcId == null) { | 
 |  |  |             vcId = getVcIdByIntakeId(intakeId); | 
 |  |  |             if (vcId == null) { | 
 |  |  | 
 |  |  |             param.elePrice = 0.0; | 
 |  |  |             param.orderNo = orderNo; | 
 |  |  |  | 
 |  |  |             /** | 
 |  |  |              * 构造命令、发送命令并处理请求结果及执行结果 | 
 |  |  |              * 1 准备参数 | 
 |  |  |              * 2 调用公共方法 | 
 |  |  |              */ | 
 |  |  |             Param myParam = new Param(); | 
 |  |  |             myParam.setComId(comId); | 
 |  |  |             myParam.setComType((byte)1); | 
 |  |  | 
 |  |  |             param.waterPrice = waterPrice; | 
 |  |  |             param.orderNo = orderNo; | 
 |  |  |  | 
 |  |  |             /** | 
 |  |  |              * 构造命令、发送命令并处理请求结果及执行结果 | 
 |  |  |              * 1 准备参数 | 
 |  |  |              * 2 调用公共方法 | 
 |  |  |              */ | 
 |  |  |             Param myParam = new Param(); | 
 |  |  |             myParam.setComId(comId); | 
 |  |  |             myParam.setComType((byte)1); | 
 |  |  | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 小程序远程关阀 | 
 |  |  |      * @param valve | 
 |  |  |      * @param planId | 
 |  |  |      * @param operatetype | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     public Map closeWx(ValveClose valve, Long planId, Byte operatetype) { | 
 |  |  |         String rtuAddr = valve.getRtuAddr(); | 
 |  |  |         String vcNum = valve.getVcNum(); | 
 |  |  | 
 |  |  |             irIntakeOperate.setOperateType(operatetype); | 
 |  |  |             irIntakeOperate.setCommandId(comId); | 
 |  |  |             irIntakeOperate.setIntakeId(intakeId); | 
 |  |  |             irIntakeOperate.setCommandResult((byte)1); | 
 |  |  |  | 
 |  |  |             Long id = addIntakeOperate(irIntakeOperate); | 
 |  |  |             if(id == null) { | 
 |  |  | 
 |  |  |             param.icCardNo = vcNum; | 
 |  |  |             param.orderNo = orderNo; | 
 |  |  |  | 
 |  |  |             /** | 
 |  |  |              * 构造命令、发送命令并处理请求结果及执行结果 | 
 |  |  |              * 1 准备参数 | 
 |  |  |              * 2 调用公共方法 | 
 |  |  |              */ | 
 |  |  |             Param myParam = new Param(); | 
 |  |  |             myParam.setComId(comId); | 
 |  |  |             myParam.setComType((byte)2); | 
 |  |  | 
 |  |  |             param.icCardNo = vcNum; | 
 |  |  |             param.orderNo = orderNo; | 
 |  |  |  | 
 |  |  |             /** | 
 |  |  |              * 构造命令、发送命令并处理请求结果及执行结果 | 
 |  |  |              * 1 准备参数 | 
 |  |  |              * 2 调用公共方法 | 
 |  |  |              */ | 
 |  |  |             Param myParam = new Param(); | 
 |  |  |             myParam.setComId(comId); | 
 |  |  |             myParam.setComType((byte)2); |