|
@@ -3,15 +3,19 @@ package com.welampiot.controller;
|
|
|
import com.welampiot.common.BaseResult;
|
|
|
import com.welampiot.common.InterfaceResultEnum;
|
|
|
import com.welampiot.dto.GroupDTO;
|
|
|
+import com.welampiot.dto.PolicyCmdDTO;
|
|
|
import com.welampiot.dto.PolicyDTO;
|
|
|
import com.welampiot.dto.UserDTO;
|
|
|
import com.welampiot.service.GroupService;
|
|
|
+import com.welampiot.service.PolicyCmdService;
|
|
|
import com.welampiot.service.PolicyService;
|
|
|
import com.welampiot.service.UserService;
|
|
|
import com.welampiot.utils.ToolUtils;
|
|
|
import com.welampiot.vo.GroupVO;
|
|
|
import com.welampiot.vo.ListResponseVO;
|
|
|
import com.welampiot.vo.PolicyVO;
|
|
|
+import org.json.JSONArray;
|
|
|
+import org.json.JSONObject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
@@ -19,6 +23,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
@@ -37,6 +43,9 @@ public class PolicyController{
|
|
|
@Autowired
|
|
|
private PolicyService policyService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private PolicyCmdService policyCmdService;
|
|
|
+
|
|
|
@Autowired
|
|
|
private GroupService groupService;
|
|
|
//
|
|
@@ -150,4 +159,98 @@ public class PolicyController{
|
|
|
listResponseVO.setList(list);
|
|
|
return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,listResponseVO);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加编辑策略
|
|
|
+ * @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);
|
|
|
+ Integer policyType = (Integer) toolUtils.getRequestContent(request,"policyType",1);
|
|
|
+ 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);
|
|
|
+ PolicyDTO policyDTO = new PolicyDTO();
|
|
|
+ policyDTO.setPolicyType(policyType);
|
|
|
+ policyDTO.setName(name);
|
|
|
+ if (id == 0) { // 添加
|
|
|
+ UserDTO userDTO = userService.queryUserIdByUsername(username);
|
|
|
+ policyDTO.setUserId(userDTO.getId());
|
|
|
+ long l = System.currentTimeMillis();
|
|
|
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String format = simpleDateFormat.format(l);
|
|
|
+ policyDTO.setCreateTime(format);
|
|
|
+ policyService.addPolicyData(policyDTO);
|
|
|
+ Integer policyId = policyDTO.getId();
|
|
|
+ for (int i = 0; i < jsonArray.length(); i ++) {
|
|
|
+ JSONObject dataObject = jsonArray.getJSONObject(i);
|
|
|
+ String date = dataObject.getString("date");
|
|
|
+ JSONArray timeArray = dataObject.getJSONArray("timeList");
|
|
|
+ PolicyCmdDTO cmdDTO = new PolicyCmdDTO();
|
|
|
+ cmdDTO.setStartTime(date);
|
|
|
+ cmdDTO.setPolicyId(policyId);
|
|
|
+ cmdDTO.setPolicyType(policyType);
|
|
|
+ for (int j = 0; j < timeArray.length(); j ++) {
|
|
|
+ JSONObject jsonObject = timeArray.getJSONObject(j);
|
|
|
+ String time = jsonObject.getString("time");
|
|
|
+ String color = jsonObject.getString("color");
|
|
|
+ String light = jsonObject.getString("light");
|
|
|
+ String timeName = "time" + (j + 1);
|
|
|
+ String colorName = "color" + (j + 1);
|
|
|
+ String valueName = "value" + (j + 1);
|
|
|
+ Field fieldTime = cmdDTO.getClass().getDeclaredField(timeName);
|
|
|
+ Field fieldColor = cmdDTO.getClass().getDeclaredField(colorName);
|
|
|
+ Field fieldValue = cmdDTO.getClass().getDeclaredField(valueName);
|
|
|
+ fieldTime.setAccessible(true);
|
|
|
+ fieldColor.setAccessible(true);
|
|
|
+ fieldValue.setAccessible(true);
|
|
|
+ fieldTime.set(cmdDTO,time);
|
|
|
+ fieldColor.set(cmdDTO,Integer.valueOf(color));
|
|
|
+ fieldValue.set(cmdDTO,Integer.valueOf(light));
|
|
|
+ }
|
|
|
+ policyCmdService.addPolicyCmdData(cmdDTO);
|
|
|
+ }
|
|
|
+ } else { // 编辑
|
|
|
+ policyDTO.setId(id);
|
|
|
+ policyService.updatePolicyData(policyDTO);
|
|
|
+ // 删除之前绑定的策略内容
|
|
|
+ policyCmdService.deletePolicyCmdDataByPolicyId(id);
|
|
|
+ for (int i = 0; i < jsonArray.length(); i ++) {
|
|
|
+ JSONObject dataObject = jsonArray.getJSONObject(i);
|
|
|
+ String date = dataObject.getString("date");
|
|
|
+ JSONArray timeArray = dataObject.getJSONArray("timeList");
|
|
|
+ PolicyCmdDTO cmdDTO = new PolicyCmdDTO();
|
|
|
+ cmdDTO.setStartTime(date);
|
|
|
+ cmdDTO.setPolicyId(id);
|
|
|
+ cmdDTO.setPolicyType(policyType);
|
|
|
+ for (int j = 0; j < timeArray.length(); j ++) {
|
|
|
+ JSONObject jsonObject = timeArray.getJSONObject(j);
|
|
|
+ String time = jsonObject.getString("time");
|
|
|
+ String color = jsonObject.getString("color");
|
|
|
+ String light = jsonObject.getString("light");
|
|
|
+ String timeName = "time" + (j + 1);
|
|
|
+ String colorName = "color" + (j + 1);
|
|
|
+ String valueName = "value" + (j + 1);
|
|
|
+ Field fieldTime = cmdDTO.getClass().getDeclaredField(timeName);
|
|
|
+ Field fieldColor = cmdDTO.getClass().getDeclaredField(colorName);
|
|
|
+ Field fieldValue = cmdDTO.getClass().getDeclaredField(valueName);
|
|
|
+ fieldTime.setAccessible(true);
|
|
|
+ fieldColor.setAccessible(true);
|
|
|
+ fieldValue.setAccessible(true);
|
|
|
+ fieldTime.set(cmdDTO,time);
|
|
|
+ fieldColor.set(cmdDTO,Integer.valueOf(color));
|
|
|
+ fieldValue.set(cmdDTO,Integer.valueOf(light));
|
|
|
+ }
|
|
|
+ policyCmdService.addPolicyCmdData(cmdDTO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
|
|
|
+ }
|
|
|
}
|