|  |  |  | 
|---|
|  |  |  | package com.dy.pipIrrWechat.irrigation; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.dy.common.webUtil.QueryResultVo; | 
|---|
|  |  |  | import com.alibaba.fastjson2.JSON; | 
|---|
|  |  |  | import com.alibaba.fastjson2.JSONArray; | 
|---|
|  |  |  | import com.alibaba.fastjson2.JSONObject; | 
|---|
|  |  |  | import com.dy.common.multiDataSource.DataSourceContext; | 
|---|
|  |  |  | import com.dy.common.mw.protocol.Command; | 
|---|
|  |  |  | import com.dy.common.mw.protocol.CommandType; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.dy.common.webUtil.BaseResponseUtils; | 
|---|
|  |  |  | import com.dy.pipIrrGlobal.rtuMw.Web2RtuMw; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.dy.common.webUtil.BaseResponse; | 
|---|
|  |  |  | import com.dy.pipIrrGlobal.daoIr.IrIrrigateGroupMapper; | 
|---|
|  |  |  | import com.dy.pipIrrGlobal.daoIr.IrProjectMapper; | 
|---|
|  |  |  | import com.dy.pipIrrGlobal.rtuMw.CodeLocal; | 
|---|
|  |  |  | import com.dy.pipIrrGlobal.voIr.VoGroupIntakes; | 
|---|
|  |  |  | import com.dy.pipIrrGlobal.voIr.VoGroupSimple; | 
|---|
|  |  |  | import com.dy.pipIrrGlobal.voIr.VoIntake; | 
|---|
|  |  |  | import com.dy.pipIrrGlobal.voIr.VoProjectSimple; | 
|---|
|  |  |  | import com.dy.pipIrrWechat.irrigation.qo.QoGroup; | 
|---|
|  |  |  | import com.dy.pipIrrWechat.irrigation.qo.QoProject; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.apache.dubbo.common.utils.PojoUtils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.core.env.Environment; | 
|---|
|  |  |  | import org.springframework.http.HttpEntity; | 
|---|
|  |  |  | import org.springframework.http.HttpHeaders; | 
|---|
|  |  |  | import org.springframework.http.HttpMethod; | 
|---|
|  |  |  | import org.springframework.http.ResponseEntity; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.web.client.RestTemplate; | 
|---|
|  |  |  | import org.springframework.web.util.UriComponentsBuilder; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private IrIrrigateGroupMapper irIrrigateGroupMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private Environment env; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RestTemplate restTemplate; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static final String pro_mw = "mw"; | 
|---|
|  |  |  | private static final String pro_url = "url"; | 
|---|
|  |  |  | protected static final String ContextComSend = "/rtuMw/com/send"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | protected String comSendUrl; | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * pro_mw:属性 | 
|---|
|  |  |  | * tag从控制器中获取 | 
|---|
|  |  |  | * key_mw:url的key | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | //private Environment env = null; | 
|---|
|  |  |  | //private String pro_mw = "mw"; | 
|---|
|  |  |  | private String key_mw = "comSendUrl"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 根据指定条件获取项目记录(精简) | 
|---|
|  |  |  | * @param queryVo | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param queryVo | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public QueryResultVo<List<VoGroupSimple>> getSimpleGroups(QoGroup queryVo) { | 
|---|
|  |  |  | public List<VoGroupSimple> getSimpleGroups(QoGroup queryVo) { | 
|---|
|  |  |  | Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo); | 
|---|
|  |  |  | Long itemTotal = irIrrigateGroupMapper.getSimpleGroupCount(params); | 
|---|
|  |  |  | //Long itemTotal = irIrrigateGroupMapper.getSimpleGroupCount(params); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | QueryResultVo<List<VoGroupSimple>> rsVo = new QueryResultVo<>(); | 
|---|
|  |  |  | //QueryResultVo<List<VoGroupSimple>> rsVo = new QueryResultVo<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | rsVo.calculateAndSet(itemTotal, params); | 
|---|
|  |  |  | rsVo.obj = irIrrigateGroupMapper.getSimpleGroups(params); | 
|---|
|  |  |  | return rsVo; | 
|---|
|  |  |  | //rsVo.calculateAndSet(itemTotal, params); | 
|---|
|  |  |  | //rsVo.obj = irIrrigateGroupMapper.getSimpleGroups(params); | 
|---|
|  |  |  | //return rsVo; | 
|---|
|  |  |  | return irIrrigateGroupMapper.getSimpleGroups(params); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 根据轮灌组ID获取轮灌组详情 | 
|---|
|  |  |  | * @param groupId | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Map getGroupDetails(Long groupId) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | // 生成项目信息及轮灌组信息 | 
|---|
|  |  |  | VoGroupIntakes voGroupIntakes = new VoGroupIntakes(); | 
|---|
|  |  |  | String projectName = ""; | 
|---|
|  |  |  | String groupName = ""; | 
|---|
|  |  |  | List<VoGroupIntakes> groupIntakes = irIrrigateGroupMapper.getGroupIntakes(groupId); | 
|---|
|  |  |  | if (groupIntakes == null) { | 
|---|
|  |  |  | Map map = new HashMap<>(); | 
|---|
|  |  |  | map.put("success", false); | 
|---|
|  |  |  | map.put("msg", "轮灌组不存在"); | 
|---|
|  |  |  | map.put("content", null); | 
|---|
|  |  |  | return map; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for(VoGroupIntakes groupIntake : groupIntakes) { | 
|---|
|  |  |  | projectName = projectName + groupIntake.getProjectName() + "、"; | 
|---|
|  |  |  | groupName = groupIntake.getGroupName(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | voGroupIntakes.setProjectName(projectName.substring(0, projectName.length() - 1)); | 
|---|
|  |  |  | voGroupIntakes.setGroupName(groupName); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 补全取水口信息(在线情况) | 
|---|
|  |  |  | List<VoIntake> intakes = irIrrigateGroupMapper.getGroupIntakesList(groupId); | 
|---|
|  |  |  | if(intakes == null) { | 
|---|
|  |  |  | Map map = new HashMap<>(); | 
|---|
|  |  |  | map.put("success", false); | 
|---|
|  |  |  | map.put("msg", "该轮灌组未绑定取水口"); | 
|---|
|  |  |  | map.put("content", null); | 
|---|
|  |  |  | return map; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (VoIntake intake : intakes) { | 
|---|
|  |  |  | Boolean isOnLine = getOnlineStatus(intake.getRtuAddr()); | 
|---|
|  |  |  | intake.setIsOnLine(isOnLine); | 
|---|
|  |  |  | Boolean isOpen = getOpenCloseStatus(intake.getRtuAddr()); | 
|---|
|  |  |  | intake.setIsOpen(isOpen); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | voGroupIntakes.setIntakes(intakes); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Map map = new HashMap<>(); | 
|---|
|  |  |  | map.put("success", true); | 
|---|
|  |  |  | map.put("msg", "获取轮灌组详情成功"); | 
|---|
|  |  |  | map.put("content", voGroupIntakes); | 
|---|
|  |  |  | return map; | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | Map map = new HashMap<>(); | 
|---|
|  |  |  | map.put("success", false); | 
|---|
|  |  |  | map.put("msg", "获取轮灌组详情失败"); | 
|---|
|  |  |  | map.put("content", null); | 
|---|
|  |  |  | return map; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Boolean getOnlineStatus(String rtuAdd) { | 
|---|
|  |  |  | Command com = new Command(); | 
|---|
|  |  |  | com.id = Command.defaultId; | 
|---|
|  |  |  | com.code = CodeLocal.onLinePart; | 
|---|
|  |  |  | com.type = CommandType.innerCommand; | 
|---|
|  |  |  | com.setRtuAddr(rtuAdd); | 
|---|
|  |  |  | com.setParam(rtuAdd); | 
|---|
|  |  |  | JSONObject response = (JSONObject) JSON.toJSON(sendCom2Mw(com)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (response == null || !response.getString("code").equals("0001") || response.getJSONObject("content").getJSONObject("attachment").size() == 0) { | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(response.getJSONObject("content").getJSONObject("attachment").getBoolean(rtuAdd)) { | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else { | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Boolean getOpenCloseStatus(String rtuAdd) { | 
|---|
|  |  |  | Command com = new Command(); | 
|---|
|  |  |  | com.id = Command.defaultId; | 
|---|
|  |  |  | com.code = CodeLocal.oneRtuStates; | 
|---|
|  |  |  | com.type = CommandType.innerCommand; | 
|---|
|  |  |  | com.setRtuAddr(rtuAdd); | 
|---|
|  |  |  | com.setParam(rtuAdd); | 
|---|
|  |  |  | JSONObject response = (JSONObject) JSON.toJSON(sendCom2Mw(com)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (response == null || !response.getString("code").equals("0001") || response.getJSONObject("content").getJSONObject("attachment") == null) { | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // if(response.getJSONObject("content").getJSONObject("attachment").get("valveOpenTrueCloseFalse").equals("true")) { | 
|---|
|  |  |  | if(response.getJSONObject("content").getJSONObject("attachment").getBoolean("valveOpenTrueCloseFalse")) { | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else { | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 发送命令 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | protected BaseResponse sendCom2Mw(Command com) { | 
|---|
|  |  |  | String url = UriComponentsBuilder.fromUriString(env.getProperty(pro_mw + "." + DataSourceContext.get() + "." + key_mw)) | 
|---|
|  |  |  | .build() | 
|---|
|  |  |  | .toUriString(); | 
|---|
|  |  |  | HttpHeaders headers = new HttpHeaders(); | 
|---|
|  |  |  | HttpEntity<Command> httpEntity = new HttpEntity<>(com, headers); | 
|---|
|  |  |  | ResponseEntity<BaseResponse> response = null; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | // 通过Post方式调用接口 | 
|---|
|  |  |  | response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, BaseResponse.class); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(response == null) { | 
|---|
|  |  |  | return BaseResponseUtils.buildErrorMsg("中间件调用失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return response.getBody(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|