Sfoglia il codice sorgente

plc策略列表、删除plc策略

zhj 2 anni fa
parent
commit
890f6b4d22

+ 88 - 1
src/main/java/com/welampiot/controller/PlcPolicyController.java

@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.RestController;
 
 import java.lang.reflect.Field;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -54,7 +55,7 @@ public class PlcPolicyController {
         Integer version = policyVO.getVersion();
         Integer id = policyVO.getId();
         String name = policyVO.getName();
-        if (name == null || name.length() == 0)
+        if (name.isEmpty())
             return toolUtils.response(InterfaceResultEnum.LACK_NAME_ERROR,version);
         Integer policyType = policyVO.getPolicyType();
         if (policyType == null)
@@ -94,6 +95,7 @@ public class PlcPolicyController {
             }
         }
 
+        PlcPolicyVO plcPolicyVO = new PlcPolicyVO();
         PlcPolicyDTO plcPolicyDTO = new PlcPolicyDTO();
         plcPolicyDTO.setName(name);
         plcPolicyDTO.setPolicyType(policyType);
@@ -110,6 +112,7 @@ public class PlcPolicyController {
             plcPolicyDTO.setCreateTime(format);
             plcPolicyService.addPlcPolicyData(plcPolicyDTO);
             Integer id1 = plcPolicyDTO.getId();
+            plcPolicyVO.setId(id1);
             // 添加策略内容
             for (PlcPolicyContentDTO p : dataList) {
                 Integer weekly = p.getWeekly();
@@ -174,6 +177,7 @@ public class PlcPolicyController {
                 }
             }
         } else  { // 编辑
+            plcPolicyVO.setId(id);
             plcPolicyDTO.setId(id);
             plcPolicyService.updatePlcPolicyData(plcPolicyDTO);
             plcPolicyCmdService.deletePlcPolicyCmdByPlcPolicyId(id);
@@ -241,6 +245,89 @@ public class PlcPolicyController {
                 }
             }
         }
+        return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,plcPolicyVO);
+    }
+
+    /**
+     * plc策略列表
+     * @param vo PlcPolicyVO
+     * @return plc策略列表
+     */
+    @RequestMapping(value = "/getList", method = RequestMethod.POST)
+    public BaseResult<?> getList(PlcPolicyVO vo) throws NoSuchFieldException, IllegalAccessException {
+        PlcPolicyVO policyVO = PlcPolicyVO.getDefaultPlcPolicyVO(vo);
+        Integer version = policyVO.getVersion();
+        String username = policyVO.getUsername();
+        if (username == null || username.isEmpty())
+            return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version);
+        UserDTO userDTO = userService.queryUserIdByUsername(username);
+        if (userDTO == null) return toolUtils.response(InterfaceResultEnum.PARAM_FAIL,version);
+        policyVO.setUserid(userDTO.getId());
+        List<PlcPolicyDTO> policyList;
+        Integer total;
+        if (userDTO.getRole() == 1) { // 超管获取全部的策略列表
+            policyList = plcPolicyService.getAllPlcPolicyList(policyVO);
+            total = plcPolicyService.getAllPlcPolicyTotal();
+        } else {
+            policyList = plcPolicyService.getPlcPolicyList(policyVO);
+            total = plcPolicyService.getPlcPolicyTotal(userDTO.getId());
+            if (policyList == null || policyList.isEmpty()) {
+                PlcPolicyVO plcPolicyVO = new PlcPolicyVO();
+                plcPolicyVO.setList(policyList);
+                plcPolicyVO.setTotal(total);
+                return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,plcPolicyVO);
+            }
+        }
+        List<String> ids = new ArrayList<>();
+        policyList.forEach(dto -> ids.add(String.valueOf(dto.getId())));
+        List<PlcPolicyCmdDTO> cmdList = plcPolicyCmdService.getPlcPolicyCmdListByPolicyId(ids);
+        for (PlcPolicyDTO dto : policyList) {
+            List<PlcPolicyContentDTO> contentList = new ArrayList<>();
+            for (PlcPolicyCmdDTO p : cmdList) {
+                if (dto.getId().equals(p.getPlcPolicyId())) {
+                    dto.setLighterMask(p.getLighterMask());
+                    Integer baseTimeType = p.getBaseTimeType();
+                    PlcPolicyContentDTO contentDTO = new PlcPolicyContentDTO();
+                    contentDTO.setBaseTimeType(baseTimeType);
+                    List<String> value;
+                    if (dto.getPolicyType() == 0) { // 日常
+                        value = ToolUtils.getPlcPolicyValue(p);
+                    } else if (dto.getPolicyType() == 1) { // 周期
+                        Integer weekly = p.getWeekly();
+                        value = ToolUtils.getPlcPolicyValue(p);
+                        contentDTO.setWeekly(weekly);
+                    } else { // 假期
+                        String holidayDate = p.getHolidayDate();
+                        Integer holidayDay = p.getHolidayDay();
+                        value = ToolUtils.getPlcPolicyValue(p);
+                        contentDTO.setHolidayDate(holidayDate);
+                        contentDTO.setHolidayDay(holidayDay);
+                    }
+                    contentDTO.setValue(value);
+                    contentList.add(contentDTO);
+                }
+            }
+            dto.setContent(contentList);
+        }
+        PlcPolicyVO plcPolicyVO = new PlcPolicyVO();
+        plcPolicyVO.setTotal(total);
+        plcPolicyVO.setList(policyList);
+        return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,plcPolicyVO);
+    }
+
+    /**
+     * 删除plc策略
+     * @param vo plc策略id
+     * @return 删除plc策略
+     */
+    @RequestMapping(value = "/del", method = RequestMethod.POST)
+    public BaseResult<?> del(PlcPolicyVO vo) throws NoSuchFieldException, IllegalAccessException {
+        PlcPolicyVO policyVO = PlcPolicyVO.getDefaultPlcPolicyVO(vo);
+        Integer version = policyVO.getVersion();
+        Integer id = policyVO.getId();
+        if (id == null) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version);
+        plcPolicyService.deletePlcPolicyDataById(id);
+        plcPolicyCmdService.deletePlcPolicyCmdByPlcPolicyId(id);
         return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
     }
 }

+ 4 - 0
src/main/java/com/welampiot/dao/PlcPolicyCmdDao.java

@@ -3,6 +3,8 @@ package com.welampiot.dao;
 import com.welampiot.dto.PlcPolicyCmdDTO;
 import org.apache.ibatis.annotations.Param;
 
+import java.util.List;
+
 /**
  * ClassName: PlcPolicyCmdDao
  * Package: com.welampiot.dao
@@ -18,4 +20,6 @@ public interface PlcPolicyCmdDao {
     void addPlcPolicyCmdSunType(PlcPolicyCmdDTO dto);
 
     void deletePlcPolicyCmdByPlcPolicyId(@Param("plcPolicyId") Integer plcPolicyId);
+
+    List<PlcPolicyCmdDTO> getPlcPolicyCmdListByPolicyId(@Param("policyIds") List<String> policyIds);
 }

+ 14 - 0
src/main/java/com/welampiot/dao/PlcPolicyDao.java

@@ -1,6 +1,10 @@
 package com.welampiot.dao;
 
 import com.welampiot.dto.PlcPolicyDTO;
+import com.welampiot.vo.PlcPolicyVO;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
 
 /**
  * ClassName: PlcPolicyDao
@@ -15,4 +19,14 @@ public interface PlcPolicyDao {
     void addPlcPolicyData(PlcPolicyDTO dto);
 
     void updatePlcPolicyData(PlcPolicyDTO dto);
+
+    List<PlcPolicyDTO> getPlcPolicyList(PlcPolicyVO vo);
+
+    List<PlcPolicyDTO> getAllPlcPolicyList(PlcPolicyVO vo);
+
+    Integer getPlcPolicyTotal(@Param("userid") Integer userid);
+
+    Integer getAllPlcPolicyTotal();
+
+    void deletePlcPolicyDataById(@Param("id") Integer id);
 }

+ 5 - 0
src/main/java/com/welampiot/dto/PlcPolicyContentDTO.java

@@ -20,5 +20,10 @@ public class PlcPolicyContentDTO implements Serializable {
 
     private Integer weekly;
 
+    private List<String> value;
+
+    private String holidayDate;
+    private Integer holidayDay;
+
     private List<PlcPolicyValueDTO> timeList;
 }

+ 7 - 0
src/main/java/com/welampiot/dto/PlcPolicyDTO.java

@@ -3,6 +3,7 @@ package com.welampiot.dto;
 import lombok.Data;
 
 import java.io.Serializable;
+import java.util.List;
 
 /**
  * ClassName: PlcPolicyDTO
@@ -26,4 +27,10 @@ public class PlcPolicyDTO implements Serializable {
     private Integer policyType; // 策略类型(0 日常,1 周期,2 假期)
 
     private Integer operateType; // 策略的使用方式(0 分组,1 广播)
+
+    private Integer lighterMask;
+
+    private Integer baseTimeType;
+
+    private List<PlcPolicyContentDTO> content;
 }

+ 4 - 0
src/main/java/com/welampiot/service/PlcPolicyCmdService.java

@@ -2,6 +2,8 @@ package com.welampiot.service;
 
 import com.welampiot.dto.PlcPolicyCmdDTO;
 
+import java.util.List;
+
 /**
  * ClassName: PlcPolicyCmdService
  * Package: com.welampiot.service
@@ -17,4 +19,6 @@ public interface PlcPolicyCmdService {
     void addPlcPolicyCmdSunType(PlcPolicyCmdDTO dto);
 
     void deletePlcPolicyCmdByPlcPolicyId(Integer plcPolicyId);
+
+    List<PlcPolicyCmdDTO> getPlcPolicyCmdListByPolicyId(List<String> policyIds);
 }

+ 14 - 0
src/main/java/com/welampiot/service/PlcPolicyService.java

@@ -1,6 +1,10 @@
 package com.welampiot.service;
 
 import com.welampiot.dto.PlcPolicyDTO;
+import com.welampiot.vo.PlcPolicyVO;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
 
 /**
  * ClassName: PlcPolicyService
@@ -15,4 +19,14 @@ public interface PlcPolicyService {
     void addPlcPolicyData(PlcPolicyDTO dto);
 
     void updatePlcPolicyData(PlcPolicyDTO dto);
+
+    List<PlcPolicyDTO> getPlcPolicyList(PlcPolicyVO vo);
+
+    List<PlcPolicyDTO> getAllPlcPolicyList(PlcPolicyVO vo);
+
+    Integer getPlcPolicyTotal(@Param("userid") Integer userid);
+
+    Integer getAllPlcPolicyTotal();
+
+    void deletePlcPolicyDataById(Integer id);
 }

+ 7 - 0
src/main/java/com/welampiot/service/impl/PlcPolicyCmdServiceImpl.java

@@ -6,6 +6,8 @@ import com.welampiot.service.PlcPolicyCmdService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.List;
+
 /**
  * ClassName: PlcPolicyCmdServiceImpl
  * Package: com.welampiot.service.impl
@@ -34,4 +36,9 @@ public class PlcPolicyCmdServiceImpl implements PlcPolicyCmdService {
     public void deletePlcPolicyCmdByPlcPolicyId(Integer plcPolicyId) {
         plcPolicyCmdDao.deletePlcPolicyCmdByPlcPolicyId(plcPolicyId);
     }
+
+    @Override
+    public List<PlcPolicyCmdDTO> getPlcPolicyCmdListByPolicyId(List<String> policyIds) {
+        return plcPolicyCmdDao.getPlcPolicyCmdListByPolicyId(policyIds);
+    }
 }

+ 28 - 0
src/main/java/com/welampiot/service/impl/PlcPolicyServiceImpl.java

@@ -3,9 +3,12 @@ package com.welampiot.service.impl;
 import com.welampiot.dao.PlcPolicyDao;
 import com.welampiot.dto.PlcPolicyDTO;
 import com.welampiot.service.PlcPolicyService;
+import com.welampiot.vo.PlcPolicyVO;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.List;
+
 /**
  * ClassName: PlcPolicyServiceImpl
  * Package: com.welampiot.service.impl
@@ -29,4 +32,29 @@ public class PlcPolicyServiceImpl implements PlcPolicyService {
     public void updatePlcPolicyData(PlcPolicyDTO dto) {
         plcPolicyDao.updatePlcPolicyData(dto);
     }
+
+    @Override
+    public List<PlcPolicyDTO> getPlcPolicyList(PlcPolicyVO vo) {
+        return plcPolicyDao.getPlcPolicyList(vo);
+    }
+
+    @Override
+    public List<PlcPolicyDTO> getAllPlcPolicyList(PlcPolicyVO vo) {
+        return plcPolicyDao.getAllPlcPolicyList(vo);
+    }
+
+    @Override
+    public Integer getPlcPolicyTotal(Integer userid) {
+        return plcPolicyDao.getPlcPolicyTotal(userid);
+    }
+
+    @Override
+    public Integer getAllPlcPolicyTotal() {
+        return plcPolicyDao.getAllPlcPolicyTotal();
+    }
+
+    @Override
+    public void deletePlcPolicyDataById(Integer id) {
+        plcPolicyDao.deletePlcPolicyDataById(id);
+    }
 }

+ 85 - 5
src/main/java/com/welampiot/utils/ToolUtils.java

@@ -6,10 +6,7 @@ import com.welampiot.common.DevInfoEnum;
 import com.welampiot.common.InterfaceResultEnum;
 import com.welampiot.common.LampControlTypeEnum;
 import com.welampiot.configuration.MqttConfig;
-import com.welampiot.dto.GlobalLocationDTO;
-import com.welampiot.dto.LampPoleDTO;
-import com.welampiot.dto.SectionDTO;
-import com.welampiot.dto.UserDTO;
+import com.welampiot.dto.*;
 import com.welampiot.handle.MqttHandler;
 import com.welampiot.service.GlobalLocationService;
 import com.welampiot.service.LampPoleService;
@@ -33,6 +30,7 @@ import org.springframework.stereotype.Component;
 
 import javax.servlet.http.HttpServletRequest;
 import java.io.IOException;
+import java.lang.reflect.Field;
 import java.net.URL;
 import java.net.URLConnection;
 import java.text.DecimalFormat;
@@ -454,7 +452,7 @@ public class ToolUtils {
             return keyId;
         }else if (type == 2){
             String keyStr = request.getParameter(key) == null ? "" : request.getParameter(key).toString();
-            return keyStr;
+            return keyStr.trim();
         }
         return request.getParameter(key);
     }
@@ -764,4 +762,86 @@ public class ToolUtils {
         }
         return content;
     }
+
+    /**
+     * 获取plc策略内容
+     * @param p plc策略内容对象
+     * @return plc策略内容
+     * @throws NoSuchFieldException 对象中没有该属性抛出的异常
+     * @throws IllegalAccessException 类型非法转换抛出的异常
+     */
+    public static List<String> getPlcPolicyValue(PlcPolicyCmdDTO p) throws NoSuchFieldException, IllegalAccessException {
+        List<String> value = new ArrayList<>();
+        if (p.getBaseTimeType() == 0) { // 零点
+            for (int i = 1; i <= 6; i ++) {
+                Field fieldStartTime = p.getClass().getDeclaredField("startTime" + i);
+                fieldStartTime.setAccessible(true);
+                String startTime = (String) fieldStartTime.get(p);
+                if (startTime == null || startTime.isEmpty()) break;
+                Field fieldEndTime = p.getClass().getDeclaredField("endTime" + i);
+                fieldEndTime.setAccessible(true);
+                String endTime = (String) fieldEndTime.get(p);
+                // 亮度
+                Field fieldStartBright = p.getClass().getDeclaredField("bright" + i);
+                fieldStartBright.setAccessible(true);
+                Integer startBright = (Integer) fieldStartBright.get(p);
+                // 色温
+                Field fieldStartColor = p.getClass().getDeclaredField("color" + i);
+                fieldStartColor.setAccessible(true);
+                Integer startColor = (Integer) fieldStartColor.get(p);
+                if (endTime != null && !endTime.isEmpty()) {
+                    String tips = startTime + " - " + endTime + " 亮度" + startBright + "% 色温" + startColor + "k";
+                    value.add(tips);
+                    Field fieldStartTime2 = p.getClass().getDeclaredField("startTime" + (i + 1));
+                    fieldStartTime2.setAccessible(true);
+                    String startTime2 = (String) fieldStartTime2.get(p);
+                    // 亮度
+                    Field fieldEndBright = p.getClass().getDeclaredField("endBright" + i);
+                    fieldEndBright.setAccessible(true);
+                    Integer endBright = (Integer) fieldEndBright.get(p);
+                    // 色温
+                    Field fieldEndColor = p.getClass().getDeclaredField("endColor" + i);
+                    fieldEndColor.setAccessible(true);
+                    Integer endColor = (Integer) fieldEndColor.get(p);
+                    if (startTime2 != null && !startTime2.isEmpty()) {
+                        String tips1 = endTime + " - " + startTime2 + " 亮度" + endBright + "% 色温" + endColor + "k";
+                        value.add(tips1);
+                    } else {
+                        String tips1 = endTime + " - " + " 亮度" + endBright + "% 色温" + endColor + "k";
+                        value.add(tips1);
+                        break;
+                    }
+                } else {
+                    String tips = startTime + " - " + " 亮度" + startBright + "% 色温" + startColor + "k";
+                    value.add(tips);
+                    break;
+                }
+            }
+        } else { // 日出日落
+            String startDelayTime = p.getStartDelayTime();
+            Integer startBright = p.getStartBright();
+            Integer startColor = p.getStartColor();
+            String tips1,tips2;
+            int startTime = Integer.parseInt(startDelayTime);
+            if (startTime >= 0) {
+                tips1 = "日出推迟" + startTime + "分钟 亮度" + startBright + "% 色温" + startColor + "k";
+            } else {
+                startTime = -startTime;
+                tips1 = "日出提前" + startTime + "分钟 亮度" + startBright + "% 色温" + startColor + "k";
+            }
+            String endDelayTime = p.getEndDelayTime();
+            Integer endBright = p.getEndBright();
+            Integer endColor = p.getEndColor();
+            int endTime = Integer.parseInt(endDelayTime);
+            if (endTime >= 0) {
+                tips2 = "日落推迟" + endTime + "分钟 亮度" + endBright + "% 色温" + endColor + "k";
+            } else {
+                endTime = -endTime;
+                tips2 = "日落提前" + endTime + "分钟 亮度" + endBright + "% 色温" + endColor + "k";
+            }
+            value.add(tips1);
+            value.add(tips2);
+        }
+        return value;
+    }
 }

+ 27 - 0
src/main/java/com/welampiot/vo/PlcPolicyVO.java

@@ -1,6 +1,7 @@
 package com.welampiot.vo;
 
 import com.welampiot.dto.PlcPolicyContentDTO;
+import com.welampiot.dto.PlcPolicyDTO;
 import lombok.Data;
 import org.jetbrains.annotations.Contract;
 import org.jetbrains.annotations.NotNull;
@@ -37,7 +38,14 @@ public class PlcPolicyVO implements Serializable {
 
     private Integer baseTimeType; // 基准时间(0 零点,1 日出与日落)
 
+    private Integer total;
     private List<PlcPolicyContentDTO> dataList;
+    private List<PlcPolicyDTO> list;
+
+    private Integer page;
+    private Integer count;
+    private String keyword;
+    private Integer userid;
 
     @NotNull
     @Contract("_ -> param1")
@@ -48,6 +56,25 @@ public class PlcPolicyVO implements Serializable {
         if (vo.getId() == null) {
             vo.setId(0);
         }
+        if (vo.getName() == null) {
+            vo.setName("");
+        }
+        vo.setName(vo.getName().trim());
+        return vo;
+    }
+
+    @NotNull
+    @Contract("_ -> param1")
+    public static PlcPolicyVO getDefaultPlcPolicyVO(@NotNull PlcPolicyVO vo) {
+        if (vo.getVersion() == null) {
+            vo.setVersion(0);
+        }
+        if (vo.getPage() == null) {
+            vo.setPage(1);
+        }
+        if (vo.getCount() == null) {
+            vo.setCount(16);
+        }
         return vo;
     }
 }

+ 20 - 0
src/main/resources/mapper/PlcPolicyCmdMapper.xml

@@ -110,4 +110,24 @@
         where plc_policy_id = #{plcPolicyId};
     </delete>
 
+    <select id="getPlcPolicyCmdListByPolicyId" resultType="PlcPolicyCmdDTO">
+        select
+            p.id, p.plc_policy_id as plcPolicyId, p.policyType, p.base_time_type as baseTimeType,
+            p.lighter_mask as lighterMask, p.starttime1 as startTime1, p.bright1, p.color1,
+            p.endtime1 as endTime1,p.end_bright1 as endBright1, p.end_color1 as endColor1,
+            p.starttime2 as startTime2, p.bright2, p.color2, p.endtime2 as endTime2, p.end_bright2 as endBright2, p.end_color2 as endColor2,
+            p.starttime3 as startTime3, p.endtime3 as endTime3, p.bright3, p.color3, p.end_bright3 as endBright3, p.end_color3 as endColor3,
+            p.starttime4 as startTime4, p.endtime4 as endTime4, p.bright4, p.color4, p.end_bright4 as endBright4, p.end_color4 as endColor4,
+            p.starttime5 as startTime5, p.endtime5 as endTime5, p.bright5, p.color5, p.end_bright5 as endBright5, p.end_color5 as endColor5,
+            p.starttime6 as startTime6, p.endtime6 as endTime6, p.bright6, p.color6, p.end_bright6 as endBright6, p.end_color6 as endColor6,
+            p.sunrise, p.start_delay_time as startDelayTime, p.start_bright as startBright, p.start_color as startColor,
+            p.sunset, p.end_delay_time as endDelayTime, p.end_bright as endBright, p.end_color as endColor,
+            p.weekly, p.holiday_date as holidayDate, p.holiday_day as holidayDay
+        from plc_policy_cmd p
+        where p.plc_policy_id in
+        <foreach collection="policyIds" item="item" open="(" separator="," close=")">
+            #{item}
+        </foreach>
+    </select>
+
 </mapper>

+ 49 - 0
src/main/resources/mapper/PlcPolicyMapper.xml

@@ -17,5 +17,54 @@
         where
             p.id = #{id}
     </update>
+    
+    <select id="getPlcPolicyList" resultType="PlcPolicyDTO">
+        select
+            p.id,
+            p.name,
+            p.policyType,
+            p.operateType
+        from plc_policy p
+        where p.userid = #{userid}
+        <if test="keyword != null and keyword != ''">
+            and p.name like '%${keyword}%'
+        </if>
+        <if test="page >= 0 and count > 0">
+            limit #{page},#{count}
+        </if>
+    </select>
+
+    <select id="getAllPlcPolicyList" resultType="PlcPolicyDTO">
+        select
+        p.id,
+        p.name,
+        p.policyType,
+        p.operateType
+        from plc_policy p
+        where 1=1
+        <if test="keyword != null and keyword != ''">
+            and p.name like '%${keyword}%'
+        </if>
+        <if test="page >= 0 and count > 0">
+            limit #{page},#{count}
+        </if>
+    </select>
+
+    <select id="getPlcPolicyTotal" resultType="Integer">
+        select count(*)
+        from plc_policy p
+        where p.userid = #{userid}
+    </select>
+
+    <select id="getAllPlcPolicyTotal" resultType="Integer">
+        select count(*)
+        from plc_policy p
+    </select>
+
+    <delete id="deletePlcPolicyDataById">
+        delete
+        from plc_policy
+        where id = #{id};
+    </delete>
 
 </mapper>