|
@@ -0,0 +1,246 @@
|
|
|
+package com.welampiot.controller;
|
|
|
+
|
|
|
+import com.welampiot.common.BaseResult;
|
|
|
+import com.welampiot.common.InterfaceResultEnum;
|
|
|
+import com.welampiot.dto.*;
|
|
|
+import com.welampiot.service.PlcPolicyCmdService;
|
|
|
+import com.welampiot.service.PlcPolicyService;
|
|
|
+import com.welampiot.service.UserService;
|
|
|
+import com.welampiot.utils.ToolUtils;
|
|
|
+import com.welampiot.vo.PlcPolicyVO;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.CrossOrigin;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * ClassName: PlcPolicyController
|
|
|
+ * Package: com.welampiot.controller
|
|
|
+ * Description:
|
|
|
+ *
|
|
|
+ * @Author: zhj_Start
|
|
|
+ * @Create: 2023/8/17 - 11:27
|
|
|
+ * @Version: v1.0
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@CrossOrigin
|
|
|
+@RequestMapping("/plcPolicy")
|
|
|
+public class PlcPolicyController {
|
|
|
+ @Autowired
|
|
|
+ private ToolUtils toolUtils;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PlcPolicyService plcPolicyService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PlcPolicyCmdService plcPolicyCmdService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * plc添加/编辑策略
|
|
|
+ * @param vo PlcPolicyVO
|
|
|
+ * @return plc添加/编辑策略
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/save", method = RequestMethod.POST)
|
|
|
+ public BaseResult<?> save(PlcPolicyVO vo) throws NoSuchFieldException, IllegalAccessException {
|
|
|
+ PlcPolicyVO policyVO = PlcPolicyVO.getPlcPolicyVO(vo);
|
|
|
+ Integer version = policyVO.getVersion();
|
|
|
+ Integer id = policyVO.getId();
|
|
|
+ String name = policyVO.getName();
|
|
|
+ if (name == null || name.length() == 0)
|
|
|
+ return toolUtils.response(InterfaceResultEnum.LACK_NAME_ERROR,version);
|
|
|
+ Integer policyType = policyVO.getPolicyType();
|
|
|
+ if (policyType == null)
|
|
|
+ return toolUtils.response(InterfaceResultEnum.LACK_POLICY_TYPE_ERROR,version);
|
|
|
+ Integer operateType = policyVO.getOperateType();
|
|
|
+ if (operateType == null)
|
|
|
+ return toolUtils.response(InterfaceResultEnum.LACK_OPERATE_TYPE_ERROR,version);
|
|
|
+ Integer lighterMask = policyVO.getLighterMask();
|
|
|
+ if (lighterMask == null)
|
|
|
+ return toolUtils.response(InterfaceResultEnum.LACK_LIGHTER_NUMBER_ERROR,version);
|
|
|
+ List<PlcPolicyContentDTO> dataList = policyVO.getDataList();
|
|
|
+ if (dataList == null || dataList.isEmpty())
|
|
|
+ return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version);
|
|
|
+
|
|
|
+ for (PlcPolicyContentDTO p : dataList) {
|
|
|
+ Integer weekly = p.getWeekly();
|
|
|
+ if (policyType == 1) {
|
|
|
+ if (weekly == null) return toolUtils.response(InterfaceResultEnum.LACK_NEED_PARAM,version);
|
|
|
+ }
|
|
|
+ if (p.getBaseTimeType() == null) p.setBaseTimeType(0);
|
|
|
+ Integer baseTimeType = p.getBaseTimeType();
|
|
|
+ List<PlcPolicyValueDTO> timeList = p.getTimeList();
|
|
|
+ if (timeList == null || timeList.isEmpty())
|
|
|
+ return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version);
|
|
|
+ if (baseTimeType == 0) { // 零点
|
|
|
+ for (PlcPolicyValueDTO v : timeList) {
|
|
|
+ String startTime = v.getStartTime();
|
|
|
+ if (startTime == null || startTime.length() == 0)
|
|
|
+ return toolUtils.response(InterfaceResultEnum.LACK_START_TIME_ERROR,version);
|
|
|
+ }
|
|
|
+ } else { // 日出与日落
|
|
|
+ for (PlcPolicyValueDTO v : timeList) {
|
|
|
+ String time = v.getTime();
|
|
|
+ if (time == null || time.length() == 0)
|
|
|
+ return toolUtils.response(InterfaceResultEnum.LACK_START_TIME_ERROR,version);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ PlcPolicyDTO plcPolicyDTO = new PlcPolicyDTO();
|
|
|
+ plcPolicyDTO.setName(name);
|
|
|
+ plcPolicyDTO.setPolicyType(policyType);
|
|
|
+ plcPolicyDTO.setOperateType(operateType);
|
|
|
+ if (id == 0) { // 添加
|
|
|
+ String username = policyVO.getUsername();
|
|
|
+ if (username == null || username.length() == 0)
|
|
|
+ return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version);
|
|
|
+ UserDTO userDTO = userService.queryUserIdByUsername(username);
|
|
|
+ plcPolicyDTO.setUserid(userDTO.getId());
|
|
|
+ long l = System.currentTimeMillis();
|
|
|
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String format = simpleDateFormat.format(l);
|
|
|
+ plcPolicyDTO.setCreateTime(format);
|
|
|
+ plcPolicyService.addPlcPolicyData(plcPolicyDTO);
|
|
|
+ Integer id1 = plcPolicyDTO.getId();
|
|
|
+ // 添加策略内容
|
|
|
+ for (PlcPolicyContentDTO p : dataList) {
|
|
|
+ Integer weekly = p.getWeekly();
|
|
|
+ Integer baseTimeType = p.getBaseTimeType();
|
|
|
+ List<PlcPolicyValueDTO> timeList = p.getTimeList();
|
|
|
+ PlcPolicyCmdDTO cmdDTO = new PlcPolicyCmdDTO();
|
|
|
+ cmdDTO.setPlcPolicyId(id1);
|
|
|
+ cmdDTO.setBaseTimeType(baseTimeType);
|
|
|
+ cmdDTO.setWeekly(weekly);
|
|
|
+ cmdDTO.setPolicyType(policyType);
|
|
|
+ cmdDTO.setLighterMask(lighterMask);
|
|
|
+ if (baseTimeType == 0) { // 零点
|
|
|
+ for (PlcPolicyValueDTO v : timeList) {
|
|
|
+ String startTime = v.getStartTime();
|
|
|
+ Integer light = v.getLight();
|
|
|
+ Integer color = v.getColor();
|
|
|
+ int i = timeList.indexOf(v) + 1;
|
|
|
+ if (i % 2 == 0) {
|
|
|
+ i /= 2;
|
|
|
+ Field fieldEndTime = cmdDTO.getClass().getDeclaredField("endTime" + i);
|
|
|
+ fieldEndTime.setAccessible(true);
|
|
|
+ fieldEndTime.set(cmdDTO,startTime);
|
|
|
+ Field fieldEndBright = cmdDTO.getClass().getDeclaredField("endBright" + i);
|
|
|
+ fieldEndBright.setAccessible(true);
|
|
|
+ fieldEndBright.set(cmdDTO,light);
|
|
|
+ Field fieldEndColor = cmdDTO.getClass().getDeclaredField("endColor" + i);
|
|
|
+ fieldEndColor.setAccessible(true);
|
|
|
+ fieldEndColor.set(cmdDTO,color);
|
|
|
+ } else {
|
|
|
+ i = (i + 1) / 2;
|
|
|
+ Field fieldStartTime = cmdDTO.getClass().getDeclaredField("startTime" + i);
|
|
|
+ fieldStartTime.setAccessible(true);
|
|
|
+ fieldStartTime.set(cmdDTO,startTime);
|
|
|
+ Field fieldBright = cmdDTO.getClass().getDeclaredField("bright" + i);
|
|
|
+ fieldBright.setAccessible(true);
|
|
|
+ fieldBright.set(cmdDTO,light);
|
|
|
+ Field fieldColor = cmdDTO.getClass().getDeclaredField("color" + i);
|
|
|
+ fieldColor.setAccessible(true);
|
|
|
+ fieldColor.set(cmdDTO,color);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ plcPolicyCmdService.addPlcPolicyCmdZeroType(cmdDTO);
|
|
|
+ } else { // 日出与日落
|
|
|
+ for (PlcPolicyValueDTO v : timeList) {
|
|
|
+ String time = v.getTime();
|
|
|
+ Integer baseTime = v.getBaseTime();
|
|
|
+ Integer light = v.getLight();
|
|
|
+ Integer color = v.getColor();
|
|
|
+ if (baseTime == 1) { // 日出
|
|
|
+ cmdDTO.setSunrise(1);
|
|
|
+ cmdDTO.setStartDelayTime(time);
|
|
|
+ cmdDTO.setStartBright(light);
|
|
|
+ cmdDTO.setStartColor(color);
|
|
|
+ } else { // 日落
|
|
|
+ cmdDTO.setSunset(2);
|
|
|
+ cmdDTO.setEndDelayTime(time);
|
|
|
+ cmdDTO.setEndBright(light);
|
|
|
+ cmdDTO.setEndColor(color);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ plcPolicyCmdService.addPlcPolicyCmdSunType(cmdDTO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else { // 编辑
|
|
|
+ plcPolicyDTO.setId(id);
|
|
|
+ plcPolicyService.updatePlcPolicyData(plcPolicyDTO);
|
|
|
+ plcPolicyCmdService.deletePlcPolicyCmdByPlcPolicyId(id);
|
|
|
+ // 添加策略内容
|
|
|
+ for (PlcPolicyContentDTO p : dataList) {
|
|
|
+ Integer weekly = p.getWeekly();
|
|
|
+ Integer baseTimeType = p.getBaseTimeType();
|
|
|
+ List<PlcPolicyValueDTO> timeList = p.getTimeList();
|
|
|
+ PlcPolicyCmdDTO cmdDTO = new PlcPolicyCmdDTO();
|
|
|
+ cmdDTO.setPlcPolicyId(id);
|
|
|
+ cmdDTO.setBaseTimeType(baseTimeType);
|
|
|
+ cmdDTO.setWeekly(weekly);
|
|
|
+ cmdDTO.setPolicyType(policyType);
|
|
|
+ cmdDTO.setLighterMask(lighterMask);
|
|
|
+ if (baseTimeType == 0) { // 零点
|
|
|
+ for (PlcPolicyValueDTO v : timeList) {
|
|
|
+ String startTime = v.getStartTime();
|
|
|
+ Integer light = v.getLight();
|
|
|
+ Integer color = v.getColor();
|
|
|
+ int i = timeList.indexOf(v) + 1;
|
|
|
+ if (i % 2 == 0) {
|
|
|
+ i /= 2;
|
|
|
+ Field fieldEndTime = cmdDTO.getClass().getDeclaredField("endTime" + i);
|
|
|
+ fieldEndTime.setAccessible(true);
|
|
|
+ fieldEndTime.set(cmdDTO, startTime);
|
|
|
+ Field fieldEndBright = cmdDTO.getClass().getDeclaredField("endBright" + i);
|
|
|
+ fieldEndBright.setAccessible(true);
|
|
|
+ fieldEndBright.set(cmdDTO, light);
|
|
|
+ Field fieldEndColor = cmdDTO.getClass().getDeclaredField("endColor" + i);
|
|
|
+ fieldEndColor.setAccessible(true);
|
|
|
+ fieldEndColor.set(cmdDTO, color);
|
|
|
+ } else {
|
|
|
+ i = (i + 1) / 2;
|
|
|
+ Field fieldStartTime = cmdDTO.getClass().getDeclaredField("startTime" + i);
|
|
|
+ fieldStartTime.setAccessible(true);
|
|
|
+ fieldStartTime.set(cmdDTO, startTime);
|
|
|
+ Field fieldBright = cmdDTO.getClass().getDeclaredField("bright" + i);
|
|
|
+ fieldBright.setAccessible(true);
|
|
|
+ fieldBright.set(cmdDTO, light);
|
|
|
+ Field fieldColor = cmdDTO.getClass().getDeclaredField("color" + i);
|
|
|
+ fieldColor.setAccessible(true);
|
|
|
+ fieldColor.set(cmdDTO, color);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ plcPolicyCmdService.addPlcPolicyCmdZeroType(cmdDTO);
|
|
|
+ } else { // 日出与日落
|
|
|
+ for (PlcPolicyValueDTO v : timeList) {
|
|
|
+ String time = v.getTime();
|
|
|
+ Integer baseTime = v.getBaseTime();
|
|
|
+ Integer light = v.getLight();
|
|
|
+ Integer color = v.getColor();
|
|
|
+ if (baseTime == 1) { // 日出
|
|
|
+ cmdDTO.setSunrise(1);
|
|
|
+ cmdDTO.setStartDelayTime(time);
|
|
|
+ cmdDTO.setStartBright(light);
|
|
|
+ cmdDTO.setStartColor(color);
|
|
|
+ } else { // 日落
|
|
|
+ cmdDTO.setSunset(2);
|
|
|
+ cmdDTO.setEndDelayTime(time);
|
|
|
+ cmdDTO.setEndBright(light);
|
|
|
+ cmdDTO.setEndColor(color);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ plcPolicyCmdService.addPlcPolicyCmdSunType(cmdDTO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
|
|
|
+ }
|
|
|
+}
|