浏览代码

基础配置

Newspaper 1 年之前
父节点
当前提交
14ae875952

+ 11 - 0
snowy-plugin/snowy-plugin-biz/src/main/java/vip/xiaonuo/biz/modular/user/entity/BizUser.java

@@ -22,6 +22,7 @@ import com.fhs.core.trans.vo.TransPojo;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Getter;
 import lombok.Setter;
+import vip.xiaonuo.biz.modular.department.entity.Department;
 import vip.xiaonuo.biz.modular.org.entity.BizOrg;
 import vip.xiaonuo.biz.modular.position.entity.BizPosition;
 import vip.xiaonuo.common.handler.CommonSm4CbcTypeHandler;
@@ -199,6 +200,12 @@ public class BizUser extends CommonEntity implements TransPojo {
     @Trans(type = TransType.SIMPLE, target = BizOrg.class, fields = "name", alias = "org", ref = "orgName")
     private String orgId;
 
+    /** 机构id */
+    @ApiModelProperty(value = "机构id", position = 32)
+    @TableField(insertStrategy = FieldStrategy.IGNORED, updateStrategy = FieldStrategy.IGNORED)
+    @Trans(type = TransType.SIMPLE, target = Department.class, fields = "departmentName", alias = "department", ref = "departmentName")
+    private String departmentId;
+
     /** 岗位id */
     @ApiModelProperty(value = "岗位id", position = 33)
     @TableField(insertStrategy = FieldStrategy.IGNORED, updateStrategy = FieldStrategy.IGNORED)
@@ -270,6 +277,10 @@ public class BizUser extends CommonEntity implements TransPojo {
     @TableField(exist = false)
     private String orgName;
 
+    @ApiModelProperty(value = "部门名称", position = 48)
+    @TableField(exist = false)
+    private String departmentName;
+
     @ApiModelProperty(value = "岗位名称", position = 49)
     @TableField(exist = false)
     private String positionName;

+ 48 - 71
snowy-web-app/src/main/java/vip/xiaonuo/modular/energy/energythreshold/controller/EnergyThresholdController.java

@@ -10,37 +10,37 @@
  * 5.不可二次分发开源参与同类竞品,如有想法可联系团队xiaonuobase@qq.com商议合作。
  * 6.若您的项目无法满足以上几点,需要更多功能代码,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
  */
-package vip.xiaonuo.modular.energy.energythreshold.controller;
+package vip.xiaonuo.modular.energy.energyThreshold.controller;
 
-import cn.dev33.satoken.annotation.SaCheckPermission;
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
 import com.github.xiaoymin.knife4j.annotations.ApiSupport;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RestController;
-import vip.xiaonuo.common.annotation.CommonLog;
 import vip.xiaonuo.common.pojo.CommonResult;
-import vip.xiaonuo.common.pojo.CommonValidList;
-import vip.xiaonuo.modular.energy.energythreshold.entity.EnergyThreshold;
-import vip.xiaonuo.modular.energy.energythreshold.param.*;
-import vip.xiaonuo.modular.energy.energythreshold.service.EnergyThresholdService;
+import vip.xiaonuo.modular.energy.energyThreshold.entity.EnergyThreshold;
+import vip.xiaonuo.modular.energy.energyThreshold.param.EnergyThresholdModifyParam;
+import vip.xiaonuo.modular.energy.energyThreshold.param.EnergyThresholdSaveParam;
+import vip.xiaonuo.modular.energy.energyThreshold.param.EnergyThresholdSyncParam;
+import vip.xiaonuo.modular.energy.energyThreshold.param.EnergyThresholdUnitIdParam;
+import vip.xiaonuo.modular.energy.energyThreshold.service.EnergyThresholdService;
+import vip.xiaonuo.modular.env.envThreshold.entity.EnvThreshold;
+import vip.xiaonuo.modular.env.envThreshold.param.EnvThresholdModifyParam;
+import vip.xiaonuo.modular.env.envThreshold.param.EnvThresholdSaveParam;
+import vip.xiaonuo.modular.env.envThreshold.param.EnvThresholdSyncParam;
+import vip.xiaonuo.modular.env.envThreshold.param.EnvThresholdUnitIdParam;
 
 import javax.annotation.Resource;
-import javax.validation.Valid;
-import javax.validation.constraints.NotEmpty;
 
 /**
- * 阈值设置控制器
+ * 能耗阈值控制器
  *
- * @author wang
- * @date  2023/12/25 09:43
+ * @author newspaper
+ * @date  2023/12/26 09:32
  */
-@Api(tags = "阈值设置控制器")
+@Api(tags = "能耗阈值控制器")
 @ApiSupport(author = "SNOWY_TEAM", order = 1)
 @RestController
 @Validated
@@ -50,81 +50,58 @@ public class EnergyThresholdController {
     private EnergyThresholdService energyThresholdService;
 
     /**
-     * 获取阈值设置分页
+     * 根据单元ID获取阈值
      *
-     * @author wang
-     * @date  2023/12/25 09:43
+     * @author newspaper
+     * @date  2023/12/26 10:20
      */
     @ApiOperationSupport(order = 1)
-    @ApiOperation("获取阈值设置分页")
-    @GetMapping("/energ/energythreshold/page")
-    public CommonResult<Page<EnergyThreshold>> page(EnergyThresholdPageParam energyThresholdPageParam) {
-        return CommonResult.data(energyThresholdService.page(energyThresholdPageParam));
+    @ApiOperation("根据单元ID获取能耗阈值")
+    @GetMapping("/energy/energyThreshold/getThresholdByUnitId")
+    public CommonResult<EnergyThreshold> getThresholdByUnitId(EnergyThresholdUnitIdParam energyThresholdUnitIdParam) {
+        return CommonResult.data(energyThresholdService.getThresholdByUnitId(energyThresholdUnitIdParam));
     }
 
     /**
-     * 添加阈值设置
+     * 编辑能耗阈值
      *
-     * @author wang
-     * @date  2023/12/25 09:43
+     * @author newspaper
+     * @date  2023/12/26 10:20
      */
-    @ApiOperationSupport(order = 2)
-    @ApiOperation("添加阈值设置")
-    @CommonLog("添加阈值设置")
-    @PostMapping("/energ/energythreshold/add")
-    public CommonResult<String> add(@RequestBody @Valid EnergyThresholdAddParam energyThresholdAddParam) {
-        energyThresholdService.add(energyThresholdAddParam);
+    @ApiOperationSupport(order = 1)
+    @ApiOperation("编辑能耗阈值")
+    @GetMapping("/energy/energyThreshold/saveThreshold")
+    public CommonResult<String> saveThreshold(EnergyThresholdSaveParam energyThresholdSaveParam) {
+        energyThresholdService.saveThreshold(energyThresholdSaveParam);
         return CommonResult.ok();
     }
 
-    /**
-     * 编辑阈值设置
-     *
-     * @author wang
-     * @date  2023/12/25 09:43
-     */
-    @ApiOperationSupport(order = 3)
-    @ApiOperation("编辑阈值设置")
-    @CommonLog("编辑阈值设置")
-    @PostMapping("/energ/energythreshold/edit")
-    public CommonResult<String> edit(@RequestBody @Valid EnergyThresholdEditParam energyThresholdEditParam) {
-        energyThresholdService.edit(energyThresholdEditParam);
-        return CommonResult.ok();
-    }
 
     /**
-     * 删除阈值设置
+     * 修改能耗阈值状态
      *
-     * @author wang
-     * @date  2023/12/25 09:43
+     * @author newspaper
+     * @date  2023/12/26 10:20
      */
-    @ApiOperationSupport(order = 4)
-    @ApiOperation("删除阈值设置")
-    @CommonLog("删除阈值设置")
-    @PostMapping("/energ/energythreshold/delete")
-    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
-                                                   CommonValidList<EnergyThresholdIdParam> energyThresholdIdParamList) {
-        energyThresholdService.delete(energyThresholdIdParamList);
+    @ApiOperationSupport(order = 2)
+    @ApiOperation("修改能耗阈值状态")
+    @GetMapping("/energy/energyThreshold/modifyThresholdStatus")
+    public CommonResult<String> modifyThresholdStatus(EnergyThresholdModifyParam energyThresholdModifyParam) {
+        energyThresholdService.modifyThresholdStatus(energyThresholdModifyParam);
         return CommonResult.ok();
     }
 
     /**
-     * 获取阈值设置详情
+     * 同步能耗阈值
      *
-     * @author wang
-     * @date  2023/12/25 09:43
+     * @author newspaper
+     * @date  2023/12/26 10:20
      */
-    @ApiOperationSupport(order = 5)
-    @ApiOperation("获取阈值设置详情")
-    @GetMapping("/energ/energythreshold/detail")
-    public CommonResult<EnergyThreshold> detail(@Valid EnergyThresholdIdParam energyThresholdIdParam) {
-        return CommonResult.data(energyThresholdService.detail(energyThresholdIdParam));
-    }
-
-    @ApiOperationSupport(order = 5)
-    @ApiOperation("获取阈值设置详情")
-    @GetMapping("/energ/energythreshold/detail")
-    public CommonResult<String> batchThreshold(@Valid EnergyThresholdBatchParam energyThresholdIdParam) {
-        return (energyThresholdService.batchThreshold(energyThresholdIdParam));
+    @ApiOperationSupport(order = 2)
+    @ApiOperation("同步能耗阈值")
+    @GetMapping("/energy/energyThreshold/syncThreshold")
+    public CommonResult<String> syncThreshold(EnergyThresholdSyncParam energyThresholdSyncParam) {
+        energyThresholdService.syncThreshold(energyThresholdSyncParam);
+        return CommonResult.ok();
     }
 }

+ 60 - 34
snowy-web-app/src/main/java/vip/xiaonuo/modular/energy/energythreshold/entity/EnergyThreshold.java

@@ -10,7 +10,7 @@
  * 5.不可二次分发开源参与同类竞品,如有想法可联系团队xiaonuobase@qq.com商议合作。
  * 6.若您的项目无法满足以上几点,需要更多功能代码,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
  */
-package vip.xiaonuo.modular.energy.energythreshold.entity;
+package vip.xiaonuo.modular.energy.energyThreshold.entity;
 
 import com.baomidou.mybatisplus.annotation.*;
 import io.swagger.annotations.ApiModelProperty;
@@ -20,62 +20,88 @@ import java.math.BigDecimal;
 import java.util.Date;
 
 /**
- * 阈值设置实体
+ * 能耗阈值实体
  *
- * @author wang
- * @date  2023/12/25 09:43
+ * @author newspaper
+ * @date  2023/12/26 09:32
  **/
 @Getter
 @Setter
 @TableName("energy_threshold")
 public class EnergyThreshold {
 
-    /** ID */
+    /** id */
     @TableId
-    @ApiModelProperty(value = "ID", position = 1)
-    private Integer id;
+    @ApiModelProperty(value = "id", position = 1)
+    private String id;
 
     /** 阈值对象 */
     @ApiModelProperty(value = "阈值对象", position = 2)
     private String unitId;
 
-    /** 用水下值 */
-    @ApiModelProperty(value = "用水下值", position = 3)
-    private String waterDown;
+    /** 0为不启用 1为启用 */
+    @ApiModelProperty(value = "0为不启用 1为启用", position = 3)
+    private Integer isEnable;
 
-    /** 用水上值 */
-    @ApiModelProperty(value = "用水上值", position = 4)
-    private String waterUpper;
+    /** 用水下限 */
+    @ApiModelProperty(value = "用水下限", position = 4)
+    private BigDecimal waterLower;
 
-    /** 用电下值 */
-    @ApiModelProperty(value = "用电下值", position = 5)
-    private String electricityDown;
+    /** 用水上限 */
+    @ApiModelProperty(value = "用水上限", position = 5)
+    private BigDecimal waterUpper;
 
-    /** 用电上值 */
-    @ApiModelProperty(value = "用电上值", position = 6)
-    private String electricityUpper;
+    /** 用电下限 */
+    @ApiModelProperty(value = "用电下限", position = 6)
+    private BigDecimal electricityLower;
 
-    /** 七日用水下值 */
-    @ApiModelProperty(value = "七日用水下值", position = 7)
-    private String waterWeekDown;
+    /** 用电上限 */
+    @ApiModelProperty(value = "用电上限", position = 7)
+    private BigDecimal electricityUpper;
 
-    /** 七日用水上值 */
-    @ApiModelProperty(value = "七日用水上值", position = 8)
-    private String waterWeekUpper;
+    /** 七日用水下限 */
+    @ApiModelProperty(value = "七日用水下限", position = 8)
+    private BigDecimal sevenDayWaterLower;
 
-    /** 七日用电上值 */
-    @ApiModelProperty(value = "七日用电上值", position = 9)
-    private String electricityWeekDown;
+    /** 七日用水上限 */
+    @ApiModelProperty(value = "七日用水上限", position = 9)
+    private BigDecimal sevenDayWaterUpper;
 
-    /** 七日用电下值 */
-    @ApiModelProperty(value = "七日用电下值", position = 10)
-    private String electricityWeekUpper;
+    /** 七日用电上限 */
+    @ApiModelProperty(value = "七日用电上限", position = 10)
+    private BigDecimal sevenDayElectricityLower;
 
-    /** 0为不启用 1为启用 */
-    @ApiModelProperty(value = "0为不启用 1为启用", position = 11)
-    private Integer isEnable;
+    /** 七日用电下限 */
+    @ApiModelProperty(value = "七日用电下限", position = 11)
+    private BigDecimal sevenDayElectricityUpper;
 
     /** 组织id */
     @ApiModelProperty(value = "组织id", position = 12)
     private String orgId;
+
+    /** 删除标志 */
+    @ApiModelProperty(value = "删除标志", position = 13)
+    @TableLogic
+    @TableField(fill = FieldFill.INSERT)
+    private String deleteFlag;
+
+    /** 创建时间 */
+    @ApiModelProperty(value = "创建时间", position = 14)
+    @TableField(fill = FieldFill.INSERT)
+    private Date createTime;
+
+    /** 创建用户 */
+    @ApiModelProperty(value = "创建用户", position = 15)
+    @TableField(fill = FieldFill.INSERT)
+    private String createUser;
+
+    /** 修改时间 */
+    @ApiModelProperty(value = "修改时间", position = 16)
+    @TableField(fill = FieldFill.UPDATE)
+    private Date updateTime;
+
+    /** 修改用户 */
+    @ApiModelProperty(value = "修改用户", position = 17)
+    @TableField(fill = FieldFill.UPDATE)
+    private String updateUser;
 }

+ 4 - 4
snowy-web-app/src/main/java/vip/xiaonuo/modular/energy/energythreshold/enums/EnergyThresholdEnum.java

@@ -10,15 +10,15 @@
  * 5.不可二次分发开源参与同类竞品,如有想法可联系团队xiaonuobase@qq.com商议合作。
  * 6.若您的项目无法满足以上几点,需要更多功能代码,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
  */
-package vip.xiaonuo.modular.energy.energythreshold.enums;
+package vip.xiaonuo.modular.energy.energyThreshold.enums;
 
 import lombok.Getter;
 
 /**
- * 阈值设置枚举
+ * 能耗阈值枚举
  *
- * @author wang
- * @date  2023/12/25 09:43
+ * @author newspaper
+ * @date  2023/12/26 09:32
  **/
 @Getter
 public enum EnergyThresholdEnum {

+ 5 - 5
snowy-web-app/src/main/java/vip/xiaonuo/modular/energy/energythreshold/mapper/EnergyThresholdMapper.java

@@ -10,16 +10,16 @@
  * 5.不可二次分发开源参与同类竞品,如有想法可联系团队xiaonuobase@qq.com商议合作。
  * 6.若您的项目无法满足以上几点,需要更多功能代码,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
  */
-package vip.xiaonuo.modular.energy.energythreshold.mapper;
+package vip.xiaonuo.modular.energy.energyThreshold.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import vip.xiaonuo.modular.energy.energythreshold.entity.EnergyThreshold;
+import vip.xiaonuo.modular.energy.energyThreshold.entity.EnergyThreshold;
 
 /**
- * 阈值设置Mapper接口
+ * 能耗阈值Mapper接口
  *
- * @author wang
- * @date  2023/12/25 09:43
+ * @author newspaper
+ * @date  2023/12/26 09:32
  **/
 public interface EnergyThresholdMapper extends BaseMapper<EnergyThreshold> {
 }

+ 1 - 1
snowy-web-app/src/main/java/vip/xiaonuo/modular/energy/energythreshold/mapper/mapping/EnergyThresholdMapper.xml

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-<mapper namespace="vip.xiaonuo.modular.energy.energythreshold.mapper.EnergyThresholdMapper">
+<mapper namespace="vip.xiaonuo.modular.energy.energyThreshold.mapper.EnergyThresholdMapper">
 
 </mapper>

+ 25 - 49
snowy-web-app/src/main/java/vip/xiaonuo/modular/energy/energythreshold/service/EnergyThresholdService.java

@@ -10,76 +10,52 @@
  * 5.不可二次分发开源参与同类竞品,如有想法可联系团队xiaonuobase@qq.com商议合作。
  * 6.若您的项目无法满足以上几点,需要更多功能代码,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
  */
-package vip.xiaonuo.modular.energy.energythreshold.service;
+package vip.xiaonuo.modular.energy.energyThreshold.service;
 
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.IService;
-import vip.xiaonuo.common.pojo.CommonResult;
-import vip.xiaonuo.modular.energy.energythreshold.entity.EnergyThreshold;
-import vip.xiaonuo.modular.energy.energythreshold.param.*;
-
-import java.util.List;
+import vip.xiaonuo.modular.energy.energyThreshold.entity.EnergyThreshold;
+import vip.xiaonuo.modular.energy.energyThreshold.param.EnergyThresholdModifyParam;
+import vip.xiaonuo.modular.energy.energyThreshold.param.EnergyThresholdSaveParam;
+import vip.xiaonuo.modular.energy.energyThreshold.param.EnergyThresholdSyncParam;
+import vip.xiaonuo.modular.energy.energyThreshold.param.EnergyThresholdUnitIdParam;
 
 /**
- * 阈值设置Service接口
+ * 能耗阈值Service接口
  *
- * @author wang
- * @date  2023/12/25 09:43
+ * @author newspaper
+ * @date  2023/12/26 09:32
  **/
 public interface EnergyThresholdService extends IService<EnergyThreshold> {
 
     /**
-     * 获取阈值设置分页
+     * 根据单元ID获取阈值
      *
-     * @author wang
-     * @date  2023/12/25 09:43
+     * @author newspaper
+     * @date  2023/12/26 10:20
      */
-    Page<EnergyThreshold> page(EnergyThresholdPageParam energyThresholdPageParam);
+    EnergyThreshold getThresholdByUnitId(EnergyThresholdUnitIdParam energyThresholdUnitIdParam);
 
     /**
-     * 添加阈值设置
+     * 编辑能耗阈值
      *
-     * @author wang
-     * @date  2023/12/25 09:43
+     * @author newspaper
+     * @date  2023/12/26 10:20
      */
-    CommonResult add(EnergyThresholdAddParam energyThresholdAddParam);
+    void saveThreshold(EnergyThresholdSaveParam energyThresholdSaveParam);
 
     /**
-     * 编辑阈值设置
+     * 修改能耗阈值状态
      *
-     * @author wang
-     * @date  2023/12/25 09:43
+     * @author newspaper
+     * @date  2023/12/26 10:20
      */
-    void edit(EnergyThresholdEditParam energyThresholdEditParam);
+    void modifyThresholdStatus(EnergyThresholdModifyParam energyThresholdModifyParam);
 
     /**
-     * 删除阈值设置
+     * 同步能耗阈值
      *
-     * @author wang
-     * @date  2023/12/25 09:43
+     * @author newspaper
+     * @date  2023/12/26 10:20
      */
-    void delete(List<EnergyThresholdIdParam> energyThresholdIdParamList);
-
-    /**
-     * 获取阈值设置详情
-     *
-     * @author wang
-     * @date  2023/12/25 09:43
-     */
-    EnergyThreshold detail(EnergyThresholdIdParam energyThresholdIdParam);
-
-    /**
-     * 获取阈值设置详情
-     *
-     * @author wang
-     * @date  2023/12/25 09:43
-     **/
-    EnergyThreshold queryEntity(String id);
-
-
-    /**
-     * 同步阈值
-     */
-    CommonResult batchThreshold(EnergyThresholdBatchParam energyThresholdBatchParam);
-
+    void syncThreshold(EnergyThresholdSyncParam energyThresholdSyncParam);
 }

+ 50 - 73
snowy-web-app/src/main/java/vip/xiaonuo/modular/energy/energythreshold/service/impl/EnergyThresholdServiceImpl.java

@@ -10,113 +10,90 @@
  * 5.不可二次分发开源参与同类竞品,如有想法可联系团队xiaonuobase@qq.com商议合作。
  * 6.若您的项目无法满足以上几点,需要更多功能代码,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
  */
-package vip.xiaonuo.modular.energy.energythreshold.service.impl;
+package vip.xiaonuo.modular.energy.energyThreshold.service.impl;
 
 import cn.hutool.core.bean.BeanUtil;
-import cn.hutool.core.collection.CollStreamUtil;
 import cn.hutool.core.util.ObjectUtil;
-import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import org.checkerframework.checker.units.qual.A;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
-import vip.xiaonuo.common.enums.CommonSortOrderEnum;
-import vip.xiaonuo.common.exception.CommonException;
-import vip.xiaonuo.common.page.CommonPageRequest;
-import vip.xiaonuo.common.pojo.CommonResult;
+import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
 import vip.xiaonuo.modular.base.pigpen.entity.BasePigpen;
 import vip.xiaonuo.modular.base.pigpen.mapper.BasePigpenMapper;
-import vip.xiaonuo.modular.energy.energythreshold.entity.EnergyThreshold;
-import vip.xiaonuo.modular.energy.energythreshold.mapper.EnergyThresholdMapper;
-import vip.xiaonuo.modular.energy.energythreshold.param.*;
-import vip.xiaonuo.modular.energy.energythreshold.service.EnergyThresholdService;
+import vip.xiaonuo.modular.energy.energyThreshold.entity.EnergyThreshold;
+import vip.xiaonuo.modular.energy.energyThreshold.mapper.EnergyThresholdMapper;
+import vip.xiaonuo.modular.energy.energyThreshold.param.EnergyThresholdModifyParam;
+import vip.xiaonuo.modular.energy.energyThreshold.param.EnergyThresholdSaveParam;
+import vip.xiaonuo.modular.energy.energyThreshold.param.EnergyThresholdSyncParam;
+import vip.xiaonuo.modular.energy.energyThreshold.param.EnergyThresholdUnitIdParam;
+import vip.xiaonuo.modular.energy.energyThreshold.service.EnergyThresholdService;
+import vip.xiaonuo.modular.env.envThreshold.entity.EnvThreshold;
 
+import java.util.ArrayList;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
- * 阈值设置Service接口实现类
+ * 能耗阈值Service接口实现类
  *
- * @author wang
- * @date  2023/12/25 09:43
+ * @author newspaper
+ * @date  2023/12/26 09:32
  **/
 @Service
 public class EnergyThresholdServiceImpl extends ServiceImpl<EnergyThresholdMapper, EnergyThreshold> implements EnergyThresholdService {
 
     @Autowired
-    private EnergyThresholdMapper thresholdMapper;
-    @Autowired
     private BasePigpenMapper basePigpenMapper;
-    @Override
-    public Page<EnergyThreshold> page(EnergyThresholdPageParam energyThresholdPageParam) {
-        QueryWrapper<EnergyThreshold> queryWrapper = new QueryWrapper<>();
-        if(ObjectUtil.isAllNotEmpty(energyThresholdPageParam.getSortField(), energyThresholdPageParam.getSortOrder())) {
-            CommonSortOrderEnum.validate(energyThresholdPageParam.getSortOrder());
-            queryWrapper.orderBy(true, energyThresholdPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
-                    StrUtil.toUnderlineCase(energyThresholdPageParam.getSortField()));
-        } else {
-            queryWrapper.lambda().orderByAsc(EnergyThreshold::getId);
-        }
-        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
-    }
 
-    @Transactional(rollbackFor = Exception.class)
     @Override
-    public CommonResult add(EnergyThresholdAddParam energyThresholdAddParam) {
-        EnergyThreshold energyThreshold = BeanUtil.toBean(energyThresholdAddParam, EnergyThreshold.class);
-        QueryWrapper<EnergyThreshold> queryWrapper = new QueryWrapper<>();
-        queryWrapper.eq("org_id", energyThreshold.getOrgId()).eq("unit_id", energyThreshold.getUnitId());
-        if (ObjectUtil.isEmpty(thresholdMapper.selectOne(queryWrapper))) {
+    public EnergyThreshold getThresholdByUnitId(EnergyThresholdUnitIdParam energyThresholdUnitIdParam) {
+        String orgId = StpLoginUserUtil.getLoginUser().getOrgId();
+        String unitId = energyThresholdUnitIdParam.getUnitId();
+        EnergyThreshold energyThreshold = this.getOne(new QueryWrapper<EnergyThreshold>().lambda().eq(EnergyThreshold::getOrgId, orgId).eq(EnergyThreshold::getUnitId, unitId));
+        if (ObjectUtil.isEmpty(energyThreshold)){
+            energyThreshold = new EnergyThreshold();
+            energyThreshold.setOrgId(orgId);
+            energyThreshold.setUnitId(unitId);
             this.save(energyThreshold);
-            return CommonResult.ok();
-        } else {
-            return new CommonResult(500, "阈值已存在,无需重复添加!", false);
         }
-
+        return energyThreshold;
     }
 
-    @Transactional(rollbackFor = Exception.class)
     @Override
-    public void edit(EnergyThresholdEditParam energyThresholdEditParam) {
-        EnergyThreshold energyThreshold = this.queryEntity(energyThresholdEditParam.getId().toString());
-        BeanUtil.copyProperties(energyThresholdEditParam, energyThreshold);
+    public void saveThreshold(EnergyThresholdSaveParam energyThresholdSaveParam) {
+        String orgId = StpLoginUserUtil.getLoginUser().getOrgId();
+        EnergyThreshold energyThreshold = this.getOne(new QueryWrapper<EnergyThreshold>().lambda().eq(EnergyThreshold::getOrgId, orgId).eq(EnergyThreshold::getUnitId, energyThresholdSaveParam.getUnitId()));
+        BeanUtil.copyProperties(energyThresholdSaveParam,energyThreshold);
         this.updateById(energyThreshold);
     }
 
-    @Transactional(rollbackFor = Exception.class)
-    @Override
-    public void delete(List<EnergyThresholdIdParam> energyThresholdIdParamList) {
-        // 执行删除
-        this.removeByIds(CollStreamUtil.toList(energyThresholdIdParamList, EnergyThresholdIdParam::getId));
-    }
-
     @Override
-    public EnergyThreshold detail(EnergyThresholdIdParam energyThresholdIdParam) {
-        return this.queryEntity(energyThresholdIdParam.getId().toString());
-    }
-
-    @Override
-    public EnergyThreshold queryEntity(String id) {
-        EnergyThreshold energyThreshold = this.getById(id);
-        if(ObjectUtil.isEmpty(energyThreshold)) {
-            throw new CommonException("阈值设置不存在,id值为:{}", id);
-        }
-        return energyThreshold;
+    public void modifyThresholdStatus(EnergyThresholdModifyParam energyThresholdModifyParam) {
+        String unitId = energyThresholdModifyParam.getUnitId();
+        List<BasePigpen> basePigpens = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().lambda().like(BasePigpen::getPids, unitId).eq(BasePigpen::getType, 2));
+        List<String> unitIdList = basePigpens.stream().map(basePigpen -> basePigpen.getId()).collect(Collectors.toList());
+        this.update(new UpdateWrapper<EnergyThreshold>().lambda().set(EnergyThreshold::getIsEnable,energyThresholdModifyParam.getIsEnable()).in(EnergyThreshold::getUnitId,unitIdList));
     }
 
     @Override
-    public CommonResult batchThreshold(EnergyThresholdBatchParam energyThresholdBatchParam) {
-        EnergyThreshold energyThreshold = BeanUtil.toBean(energyThresholdBatchParam, EnergyThreshold.class);
-        QueryWrapper<BasePigpen> queryWrapper = new QueryWrapper<>();
-        queryWrapper.eq("org_id", energyThresholdBatchParam.getOrgId())
-                .like("pids", energyThresholdBatchParam.getUnitId());
-        List<BasePigpen> basePigpens = basePigpenMapper.selectList(queryWrapper);
-        for (BasePigpen basePigpen : basePigpens) {
-            energyThreshold.setUnitId(basePigpen.getId());
-            thresholdMapper.insert(energyThreshold);
+    public void syncThreshold(EnergyThresholdSyncParam energyThresholdSyncParam) {
+        String orgId = StpLoginUserUtil.getLoginUser().getOrgId();
+        List<BasePigpen> basePigpens = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().lambda()
+                .like(BasePigpen::getPids, energyThresholdSyncParam.getUnitId())
+                .eq(BasePigpen::getOrgId, orgId)
+                .eq(BasePigpen::getType,2));
+        List<String> unitList = basePigpens.stream().map(basePigpen -> basePigpen.getId()).collect(Collectors.toList());
+        this.remove(new QueryWrapper<EnergyThreshold>().lambda().in(EnergyThreshold::getUnitId,unitList));
+        List<EnergyThreshold> energyThresholds = new ArrayList<>();
+        for (String s : unitList) {
+            EnergyThreshold energyThreshold = new EnergyThreshold();
+            BeanUtil.copyProperties(energyThresholdSyncParam,energyThreshold);
+            energyThreshold.setUnitId(s);
+            energyThreshold.setOrgId(orgId);
+            energyThresholds.add(energyThreshold);
         }
-        return CommonResult.ok();
+        this.saveBatch(energyThresholds);
     }
 }