Browse Source

添加编辑策略

zhj 2 years ago
parent
commit
dae29d2b00

+ 103 - 0
src/main/java/com/welampiot/controller/PolicyController.java

@@ -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);
+    }
 }

+ 3 - 0
src/main/java/com/welampiot/dao/PolicyCmdDao.java

@@ -8,4 +8,7 @@ import java.util.List;
 public interface PolicyCmdDao {
     List<PolicyCmdDTO> getListByPolicyId(@Param("policyId")Integer policyId);
     List<PolicyCmdDTO> getPolicyCmdListByDTO(PolicyCmdDTO dto);
+    void addPolicyCmdData(PolicyCmdDTO dto);
+    void updatePolicyCmdData(PolicyCmdDTO dto);
+    void deletePolicyCmdDataByPolicyId(@Param("policyId") Integer policyId);
 }

+ 2 - 0
src/main/java/com/welampiot/dao/PolicyDao.java

@@ -13,4 +13,6 @@ public interface PolicyDao {
     List<PolicyDTO> getAllPolicyListByDTO(PolicyDTO dto);
     Integer getPolicyCountByUserid(@Param("userId") Integer userId);
     Integer getAllPolicyCount();
+    void addPolicyData(PolicyDTO dto);
+    void updatePolicyData(PolicyDTO dto);
 }

+ 6 - 0
src/main/java/com/welampiot/dto/PolicyTimeValueDTO.java

@@ -19,4 +19,10 @@ import lombok.NoArgsConstructor;
 public class PolicyTimeValueDTO {
     private String time;
     private Integer value;
+    private Integer color;
+
+    public PolicyTimeValueDTO(String time, Integer value) {
+        this.time = time;
+        this.value = value;
+    }
 }

+ 3 - 0
src/main/java/com/welampiot/service/PolicyCmdService.java

@@ -8,4 +8,7 @@ import java.util.List;
 public interface PolicyCmdService {
     List<PolicyCmdDTO> getListByPolicyId(@Param("policyId")Integer policyId);
     List<PolicyCmdDTO> getPolicyCmdListByDTO(PolicyCmdDTO dto);
+    void addPolicyCmdData(PolicyCmdDTO dto);
+    void updatePolicyCmdData(PolicyCmdDTO dto);
+    void deletePolicyCmdDataByPolicyId(Integer policyId);
 }

+ 2 - 0
src/main/java/com/welampiot/service/PolicyService.java

@@ -13,4 +13,6 @@ public interface PolicyService {
     List<PolicyDTO> getAllPolicyListByDTO(PolicyDTO dto);
     Integer getPolicyCountByUserid(Integer userId);
     Integer getAllPolicyCount();
+    void addPolicyData(PolicyDTO dto);
+    void updatePolicyData(PolicyDTO dto);
 }

+ 15 - 0
src/main/java/com/welampiot/service/impl/PolicyCmdServiceImpl.java

@@ -22,4 +22,19 @@ public class PolicyCmdServiceImpl implements PolicyCmdService {
     public List<PolicyCmdDTO> getPolicyCmdListByDTO(PolicyCmdDTO dto) {
         return policyCmdDao.getPolicyCmdListByDTO(dto);
     }
+
+    @Override
+    public void addPolicyCmdData(PolicyCmdDTO dto) {
+        policyCmdDao.addPolicyCmdData(dto);
+    }
+
+    @Override
+    public void updatePolicyCmdData(PolicyCmdDTO dto) {
+        policyCmdDao.updatePolicyCmdData(dto);
+    }
+
+    @Override
+    public void deletePolicyCmdDataByPolicyId(Integer policyId) {
+        policyCmdDao.deletePolicyCmdDataByPolicyId(policyId);
+    }
 }

+ 10 - 0
src/main/java/com/welampiot/service/impl/PolicyServiceImpl.java

@@ -139,4 +139,14 @@ public class PolicyServiceImpl implements PolicyService {
     public Integer getAllPolicyCount() {
         return policyDao.getAllPolicyCount();
     }
+
+    @Override
+    public void addPolicyData(PolicyDTO dto) {
+        policyDao.addPolicyData(dto);
+    }
+
+    @Override
+    public void updatePolicyData(PolicyDTO dto) {
+        policyDao.updatePolicyData(dto);
+    }
 }

+ 114 - 0
src/main/resources/mapper/PolicyCmdMapper.xml

@@ -81,6 +81,73 @@
         delete from `group` where id=#{id};
     </delete>
 
+    <insert id="addPolicyCmdData" parameterType="PolicyCmdDTO" useGeneratedKeys="true" keyProperty="id">
+        insert into policy_cmd(policyid, starttime,
+        <if test="time1 != null and time1 != ''">
+            time1, value1, color1,
+        </if>
+        <if test="time2 != null and time2 != ''">
+            time2, value2, color2,
+        </if>
+        <if test="time3 != null and time3 != ''">
+            time3, value3, color3,
+        </if>
+        <if test="time4 != null and time4 != ''">
+            time4, value4, color4,
+        </if>
+        <if test="time5 != null and time5 != ''">
+            time5, value5, color5,
+        </if>
+        <if test="time6 != null and time6 != ''">
+            time6, value6, color6,
+        </if>
+        <if test="time7 != null and time7 != ''">
+            time7, value7, color7,
+        </if>
+        <if test="time8 != null and time8 != ''">
+            time8, value8, color8,
+        </if>
+        <if test="time9 != null and time9 != ''">
+            time9, value9, color9,
+        </if>
+        <if test="time10 != null and time10 != ''">
+            time10, value10, color10,
+        </if>
+        policyType)
+        values (#{policyId},#{startTime},
+        <if test="time1 != null and time1 != ''">
+            #{time1},#{value1},#{color1},
+        </if>
+        <if test="time2 != null and time2 != ''">
+            #{time2},#{value2},#{color2},
+        </if>
+        <if test="time3 != null and time3 != ''">
+            #{time3},#{value3},#{color3},
+        </if>
+        <if test="time4 != null and time4 != ''">
+            #{time4},#{value4},#{color4},
+        </if>
+        <if test="time5 != null and time5 != ''">
+            #{time5},#{value5},#{color5},
+        </if>
+        <if test="time6 != null and time6 != ''">
+            #{time6},#{value6},#{color6},
+        </if>
+        <if test="time7 != null and time7 != ''">
+            #{time7},#{value7},#{color7},
+        </if>
+        <if test="time8 != null and time8 != ''">
+            #{time8},#{value8},#{color8},
+        </if>
+        <if test="time9 != null and time9 != ''">
+            #{time9},#{value9},#{color9},
+        </if>
+        <if test="time10 != null and time10 != ''">
+            #{time10},#{value10},#{color10},
+        </if>
+        #{policyType})
+    </insert>
+
     <select id="getPolicyCmdListByDTO" resultType="com.welampiot.dto.PolicyCmdDTO">
         select p.starttime as startTime,
                time1,value1,
@@ -96,4 +163,51 @@
         from policy_cmd p
         where p.policyid = #{policyId}
     </select>
+
+    <update id="updatePolicyCmdData" parameterType="PolicyCmdDTO">
+        update
+            policy_cmd p
+        set
+            p.starttime = #{startTime},
+            <if test="time1 != null and time1 != ''">
+                p.time1 = #{time1},p.value1 = #{value1},p.color1 = #{color1},
+            </if>
+            <if test="time2 != null and time2 != ''">
+                p.time2 = #{time2},p.value2 = #{value2},p.color2 = #{color2},
+            </if>
+            <if test="time3 != null and time3 != ''">
+                p.time3 = #{time3},p.value3 = #{value3},p.color3 = #{color3},
+            </if>
+            <if test="time4 != null and time4 != ''">
+                p.time4 = #{time4},p.value4 = #{value4},p.color4 = #{color4},
+            </if>
+            <if test="time5 != null and time5 != ''">
+                p.time5 = #{time5},p.value5 = #{value5},p.color5 = #{color5},
+            </if>
+            <if test="time6 != null and time6 != ''">
+                p.time6 = #{time6},p.value6 = #{value6},p.color6 = #{color6},
+            </if>
+            <if test="time7 != null and time7 != ''">
+                p.time7 = #{time7},p.value7 = #{value7},p.color7 = #{color7},
+            </if>
+            <if test="time8 != null and time8 != ''">
+                p.time8 = #{time8},p.value8 = #{value8},p.color8 = #{color8},
+            </if>
+            <if test="time9 != null and time9 != ''">
+                p.time9 = #{time9},p.value9 = #{value9},p.color9 = #{color9},
+            </if>
+            <if test="time10 != null and time10 != ''">
+                p.time10 = #{time10},p.value10 = #{value10},p.color10 = #{color10},
+            </if>
+            p.policyType = #{policyType}
+        where
+            p.id = #{id}
+    </update>
+
+    <delete id="deletePolicyCmdDataByPolicyId">
+        delete
+        from policy_cmd
+        where policyid = #{policyId};
+    </delete>
+
 </mapper>

+ 15 - 0
src/main/resources/mapper/PolicyMapper.xml

@@ -135,5 +135,20 @@
         select count(p.id)
         from policy p
     </select>
+
+    <insert id="addPolicyData" parameterType="PolicyDTO" keyProperty="id" useGeneratedKeys="true">
+        insert into policy(`name`,policyType,createtime,userid)
+        values (#{name},#{policyType},#{createTime},#{userId})
+    </insert>
+
+    <update id="updatePolicyData" parameterType="PolicyDTO">
+        update
+            policy p
+        set
+            p.name = #{name},
+            p.policyType = #{policyType}
+        where
+            p.id = #{id}
+    </update>
     
 </mapper>