|
@@ -0,0 +1,280 @@
|
|
|
+package com.welampiot.controller;
|
|
|
+
|
|
|
+import com.welampiot.common.BaseResult;
|
|
|
+import com.welampiot.common.InterfaceResultEnum;
|
|
|
+import com.welampiot.dto.*;
|
|
|
+import com.welampiot.service.LoopPolicyCmdService;
|
|
|
+import com.welampiot.service.LoopPolicyService;
|
|
|
+import com.welampiot.service.UserService;
|
|
|
+import com.welampiot.utils.ToolUtils;
|
|
|
+import com.welampiot.vo.LoopPolicyDetailVO;
|
|
|
+import com.welampiot.vo.LoopPolicyVO;
|
|
|
+import org.json.JSONArray;
|
|
|
+import org.json.JSONObject;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * ClassName: LoopPolicyController
|
|
|
+ * Package: com.welampiot.controller
|
|
|
+ * Description:
|
|
|
+ *
|
|
|
+ * @Author: zhj_Start
|
|
|
+ * @Create: 2023/7/31 - 15:56
|
|
|
+ * @Version: v1.0
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@CrossOrigin
|
|
|
+@RequestMapping("/loopPolicy")
|
|
|
+public class LoopPolicyController {
|
|
|
+ @Autowired
|
|
|
+ private ToolUtils toolUtils;
|
|
|
+ @Autowired
|
|
|
+ private LoopPolicyService loopPolicyService;
|
|
|
+ @Autowired
|
|
|
+ private LoopPolicyCmdService loopPolicyCmdService;
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 回路策略列表
|
|
|
+ * @param request username
|
|
|
+ * @return list
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/getList", method = RequestMethod.POST)
|
|
|
+ public BaseResult<?> getList(HttpServletRequest request) throws NoSuchFieldException, IllegalAccessException {
|
|
|
+ Integer version = (Integer) toolUtils.getRequestContent(request,"version",1);
|
|
|
+ String username = (String) toolUtils.getRequestContent(request,"username",2);
|
|
|
+ String keyword = (String) toolUtils.getRequestContent(request,"keyword",2);
|
|
|
+ int page = request.getParameter("page") == null ? 1 : Integer.parseInt(request.getParameter("page"));
|
|
|
+ int count = request.getParameter("count") == null ? 16 : Integer.parseInt(request.getParameter("count"));
|
|
|
+ UserDTO userDTO = userService.queryUserIdByUsername(username);
|
|
|
+ if (userDTO == null) return toolUtils.response(InterfaceResultEnum.PARAM_FAIL,version);
|
|
|
+ LoopPolicyVO vo = new LoopPolicyVO();
|
|
|
+ vo.setCount(count);
|
|
|
+ vo.setPage(count * (page - 1));
|
|
|
+ vo.setKeyword(keyword);
|
|
|
+ vo.setUserid(userDTO.getId());
|
|
|
+ List<LoopPolicyDTO> list = new ArrayList<>();
|
|
|
+ List<LoopPolicyDTO> policyList;
|
|
|
+ if (userDTO.getRole() == 1) { // 超级管理员获取所有的策略
|
|
|
+ policyList = loopPolicyService.getAllLoopPolicyListByDTO(vo);
|
|
|
+ } else {
|
|
|
+ policyList = loopPolicyService.getLoopPolicyListByDTO(vo);
|
|
|
+ }
|
|
|
+ for (LoopPolicyDTO dto : policyList) {
|
|
|
+ List<LoopPolicyCmdDTO> policyCmdList = loopPolicyCmdService.getLoopPolicyCmdListByPolicyId(dto.getId());
|
|
|
+ List<LoopPolicyContentDTO> contentList = new ArrayList<>();
|
|
|
+ for (LoopPolicyCmdDTO cmdDTO : policyCmdList) {
|
|
|
+ LoopPolicyContentDTO loopPolicyContentDTO = new LoopPolicyContentDTO();
|
|
|
+ loopPolicyContentDTO.setDate(cmdDTO.getStartTime());
|
|
|
+ List<String> value = new ArrayList<>();
|
|
|
+ for (int i = 1; i <= 10; i++) {
|
|
|
+ Field timeField = cmdDTO.getClass().getDeclaredField("time" + i); // 获取time属性的值
|
|
|
+ if (i < 10) {
|
|
|
+ Field timeField1 = cmdDTO.getClass().getDeclaredField("time" + (i + 1));
|
|
|
+ timeField.setAccessible(true); // 设置可以访问
|
|
|
+ timeField1.setAccessible(true);
|
|
|
+ String timeValue = (String) timeField.get(cmdDTO);
|
|
|
+ String timeValue1 = (String) timeField1.get(cmdDTO);
|
|
|
+ if (timeValue != null && !timeValue.equals("") && timeValue1 != null && !timeValue1.equals("")) {
|
|
|
+ Field valueField = cmdDTO.getClass().getDeclaredField("value" + i); // 获取value属性的值
|
|
|
+ valueField.setAccessible(true); // 设置可以访问
|
|
|
+ int valueValue = (int) valueField.get(cmdDTO);
|
|
|
+ String newString;
|
|
|
+ if (valueValue == 1) {
|
|
|
+ newString = timeValue + "-" + timeValue1 + " 分闸";
|
|
|
+ } else {
|
|
|
+ newString = timeValue + "-" + timeValue1 + " 合闸";
|
|
|
+ }
|
|
|
+ value.add(newString);
|
|
|
+ } else if (timeValue != null && !timeValue.equals("")) {
|
|
|
+ Field valueField = cmdDTO.getClass().getDeclaredField("value" + i);
|
|
|
+ valueField.setAccessible(true); // 设置可以访问
|
|
|
+ int valueValue = (int) valueField.get(cmdDTO);
|
|
|
+ String newString;
|
|
|
+ if (valueValue == 1) {
|
|
|
+ newString = timeValue + "-" + "以后" + " 分闸";
|
|
|
+ } else {
|
|
|
+ newString = timeValue + "-" + "以后" + " 合闸";
|
|
|
+ }
|
|
|
+ value.add(newString);
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ timeField.setAccessible(true);
|
|
|
+ String timeValue = (String) timeField.get(cmdDTO);
|
|
|
+ if (timeValue != null && !timeValue.equals("")) {
|
|
|
+ Field valueField = cmdDTO.getClass().getDeclaredField("value" + i);
|
|
|
+ valueField.setAccessible(true);
|
|
|
+ int valueValue = (int) valueField.get(cmdDTO);
|
|
|
+ String newString;
|
|
|
+ if (valueValue == 1) {
|
|
|
+ newString = timeValue + "-" + "以后" + " 分闸";
|
|
|
+ } else {
|
|
|
+ newString = timeValue + "-" + "以后" + " 合闸";
|
|
|
+ }
|
|
|
+ value.add(newString);
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ loopPolicyContentDTO.setValue(value);
|
|
|
+ contentList.add(loopPolicyContentDTO);
|
|
|
+ }
|
|
|
+ dto.setContent(contentList);
|
|
|
+ list.add(dto);
|
|
|
+ }
|
|
|
+ LoopPolicyVO loopPolicyVO = new LoopPolicyVO();
|
|
|
+ loopPolicyVO.setList(list);
|
|
|
+ return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,loopPolicyVO);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加编辑策略
|
|
|
+ * @param request 策略属性
|
|
|
+ * @return 添加编辑策略
|
|
|
+ */
|
|
|
+ @PostMapping("/save")
|
|
|
+ private BaseResult<?> save(HttpServletRequest request) throws IllegalAccessException, NoSuchFieldException {
|
|
|
+ Integer version = (Integer) toolUtils.getRequestContent(request,"version",1);
|
|
|
+ Integer id = (Integer) toolUtils.getRequestContent(request,"id",1);
|
|
|
+ String username = (String) toolUtils.getRequestContent(request,"username",2);
|
|
|
+ String name = (String) toolUtils.getRequestContent(request,"name",2);
|
|
|
+ String dataList = (String) toolUtils.getRequestContent(request,"dataList",2);
|
|
|
+ if (username.length() == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version);
|
|
|
+ if (name.length() == 0 || dataList.length() == 0)
|
|
|
+ return toolUtils.response(InterfaceResultEnum.LACK_NEED_PARAM,version);
|
|
|
+ JSONArray jsonArray = new JSONArray(dataList);
|
|
|
+ LoopPolicyDTO loopPolicyDTO = new LoopPolicyDTO();
|
|
|
+ loopPolicyDTO.setName(name);
|
|
|
+ LoopPolicyVO loopPolicyVO = new LoopPolicyVO();
|
|
|
+ if (id == 0) { // 添加
|
|
|
+ UserDTO userDTO = userService.queryUserIdByUsername(username);
|
|
|
+ loopPolicyDTO.setUserid(userDTO.getId());
|
|
|
+ long l = System.currentTimeMillis();
|
|
|
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String format = simpleDateFormat.format(l);
|
|
|
+ loopPolicyDTO.setCreateTime(format);
|
|
|
+ loopPolicyService.addLoopPolicyData(loopPolicyDTO);
|
|
|
+ Integer policyId = loopPolicyDTO.getId();
|
|
|
+ loopPolicyVO.setId(policyId);
|
|
|
+ for (int i = 0; i < jsonArray.length(); i ++) {
|
|
|
+ JSONObject dataObject = jsonArray.getJSONObject(i);
|
|
|
+ String date = dataObject.getString("date");
|
|
|
+ JSONArray timeArray = dataObject.getJSONArray("timeList");
|
|
|
+ LoopPolicyCmdDTO cmdDTO = new LoopPolicyCmdDTO();
|
|
|
+ cmdDTO.setStartTime(date);
|
|
|
+ cmdDTO.setPolicyId(policyId);
|
|
|
+ for (int j = 0; j < timeArray.length(); j ++) {
|
|
|
+ JSONObject jsonObject = timeArray.getJSONObject(j);
|
|
|
+ String time = jsonObject.getString("time");
|
|
|
+ String status = jsonObject.getString("status");
|
|
|
+ String timeName = "time" + (j + 1);
|
|
|
+ String statusName = "value" + (j + 1);
|
|
|
+ Field fieldTime = cmdDTO.getClass().getDeclaredField(timeName);
|
|
|
+ Field fieldValue = cmdDTO.getClass().getDeclaredField(statusName);
|
|
|
+ fieldTime.setAccessible(true);
|
|
|
+ fieldValue.setAccessible(true);
|
|
|
+ fieldTime.set(cmdDTO,time);
|
|
|
+ fieldValue.set(cmdDTO,Integer.valueOf(status));
|
|
|
+ }
|
|
|
+ loopPolicyCmdService.addLoopPolicyCmdData(cmdDTO);
|
|
|
+ }
|
|
|
+ } else { // 编辑
|
|
|
+ loopPolicyVO.setId(id);
|
|
|
+ loopPolicyDTO.setId(id);
|
|
|
+ loopPolicyService.updateLoopPolicyData(loopPolicyDTO);
|
|
|
+ // 删除之前绑定的策略内容
|
|
|
+ loopPolicyCmdService.deleteLoopPolicyDataByPolicyId(id);
|
|
|
+ for (int i = 0; i < jsonArray.length(); i ++) {
|
|
|
+ JSONObject dataObject = jsonArray.getJSONObject(i);
|
|
|
+ String date = dataObject.getString("date");
|
|
|
+ JSONArray timeArray = dataObject.getJSONArray("timeList");
|
|
|
+ LoopPolicyCmdDTO cmdDTO = new LoopPolicyCmdDTO();
|
|
|
+ cmdDTO.setStartTime(date);
|
|
|
+ cmdDTO.setPolicyId(id);
|
|
|
+ for (int j = 0; j < timeArray.length(); j ++) {
|
|
|
+ JSONObject jsonObject = timeArray.getJSONObject(j);
|
|
|
+ String time = jsonObject.getString("time");
|
|
|
+ String status = jsonObject.getString("status");
|
|
|
+ String timeName = "time" + (j + 1);
|
|
|
+ String statusName = "value" + (j + 1);
|
|
|
+ Field fieldTime = cmdDTO.getClass().getDeclaredField(timeName);
|
|
|
+ Field fieldValue = cmdDTO.getClass().getDeclaredField(statusName);
|
|
|
+ fieldTime.setAccessible(true);
|
|
|
+ fieldValue.setAccessible(true);
|
|
|
+ fieldTime.set(cmdDTO,time);
|
|
|
+ fieldValue.set(cmdDTO,Integer.valueOf(status));
|
|
|
+ }
|
|
|
+ loopPolicyCmdService.addLoopPolicyCmdData(cmdDTO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,loopPolicyVO);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 策略详情
|
|
|
+ * @param request 策略id
|
|
|
+ * @return 策略详情
|
|
|
+ */
|
|
|
+ @PostMapping("/details")
|
|
|
+ private BaseResult<?> details(HttpServletRequest request) throws NoSuchFieldException, IllegalAccessException {
|
|
|
+ Integer version = (Integer) toolUtils.getRequestContent(request,"version",1);
|
|
|
+ Integer policyId = (Integer) toolUtils.getRequestContent(request,"policyId",1);
|
|
|
+ if (policyId == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version);
|
|
|
+ LoopPolicyDTO loopPolicyDTO = loopPolicyService.getLoopPolicyDetailsById(policyId);
|
|
|
+ if (loopPolicyDTO == null) return toolUtils.response(InterfaceResultEnum.PARAM_FAIL,version);
|
|
|
+ List<LoopPolicyContentDTO> contentList = new ArrayList<>();
|
|
|
+ List<LoopPolicyCmdDTO> list = loopPolicyCmdService.getLoopPolicyCmdListByPolicyId(loopPolicyDTO.getId());
|
|
|
+ for (LoopPolicyCmdDTO l : list) {
|
|
|
+ LoopPolicyContentDTO contentDTO = new LoopPolicyContentDTO();
|
|
|
+ contentDTO.setDate(l.getStartTime());
|
|
|
+ List<LoopPolicyTimeValueDTO> timeList = new ArrayList<>();
|
|
|
+ for (int i = 1; i <= 10; i ++) {
|
|
|
+ Field timeField = l.getClass().getDeclaredField("time" + i);
|
|
|
+ timeField.setAccessible(true);
|
|
|
+ String time = (String) timeField.get(l);
|
|
|
+ if (time != null && !time.equals("")) {
|
|
|
+ Field valueField = l.getClass().getDeclaredField("value" + i);
|
|
|
+ valueField.setAccessible(true);
|
|
|
+ int status = (int) valueField.get(l);
|
|
|
+ LoopPolicyTimeValueDTO valueDTO = new LoopPolicyTimeValueDTO();
|
|
|
+ valueDTO.setTime(time);
|
|
|
+ valueDTO.setStatus(status);
|
|
|
+ timeList.add(valueDTO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ contentDTO.setTimeList(timeList);
|
|
|
+ contentList.add(contentDTO);
|
|
|
+ }
|
|
|
+ loopPolicyDTO.setContent(contentList);
|
|
|
+ LoopPolicyDetailVO loopPolicyDetailVO = new LoopPolicyDetailVO();
|
|
|
+ BeanUtils.copyProperties(loopPolicyDTO,loopPolicyDetailVO);
|
|
|
+ return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,loopPolicyDetailVO);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除策略
|
|
|
+ * @param request 策略id
|
|
|
+ * @return 删除策略
|
|
|
+ */
|
|
|
+ @PostMapping("/del")
|
|
|
+ private BaseResult<?> del(HttpServletRequest request) {
|
|
|
+ Integer version = (Integer) toolUtils.getRequestContent(request,"version",1);
|
|
|
+ Integer policyId = (Integer) toolUtils.getRequestContent(request,"policyId",1);
|
|
|
+ if (policyId == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version);
|
|
|
+ loopPolicyService.deleteLoopPolicyDataById(policyId);
|
|
|
+ loopPolicyCmdService.deleteLoopPolicyDataByPolicyId(policyId);
|
|
|
+ return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
|
|
|
+ }
|
|
|
+}
|