diff --git a/iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/PageRequest.java b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/PageRequest.java index 9242d037..3a7b69cb 100644 --- a/iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/PageRequest.java +++ b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/PageRequest.java @@ -1,5 +1,10 @@ package cc.iotkit.common.api; +import cc.iotkit.common.utils.SnowflakeIdGeneratorUtil; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotNull; +import java.util.Map; import lombok.Data; import lombok.EqualsAndHashCode; @@ -18,22 +23,24 @@ public class PageRequest extends Request implements Serializable { /** * 分页大小 */ + @Min(1) + @NotNull private Integer pageSize; /** * 当前页数 */ + @Min(1) + @Max(100) + @NotNull private Integer pageNum; /** - * 排序列 + * 排序 key为排序字段名 value为排序方向方向desc或者asc */ - private String orderByColumn; + private Map sortMap; + - /** - * 排序的方向desc或者asc - */ - private String isAsc; /** * 当前记录起始索引 默认值 @@ -41,8 +48,38 @@ public class PageRequest extends Request implements Serializable { public static final int DEFAULT_PAGE_NUM = 1; /** - * 每页显示记录数 默认值 默认查全部 + * 每页显示记录数 默认值 */ - public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE; + public static final int DEFAULT_PAGE_SIZE = 20; + + + public static PageRequest of(T data) { + PageRequest pageRequest = new PageRequest<>(); + pageRequest.setPageSize(DEFAULT_PAGE_SIZE); + pageRequest.setPageNum(DEFAULT_PAGE_NUM); + pageRequest.setData(data); + pageRequest.setRequestId(String.valueOf(SnowflakeIdGeneratorUtil.getInstanceSnowflake().nextId())); + return pageRequest; + } + + public static PageRequest request2PageRequest(Request request) { + PageRequest pageRequest = new PageRequest<>(); + pageRequest.setData(request.getData()); + pageRequest.setPageNum(DEFAULT_PAGE_NUM); + pageRequest.setPageSize(DEFAULT_PAGE_SIZE); + pageRequest.setRequestId(request.getRequestId()); + return pageRequest; + } + + public static PageRequest copyPageRequest(PageRequest query,DTO data) { + PageRequest pageRequest = new PageRequest<>(); + pageRequest.setData(data); + pageRequest.setPageNum(query.getPageNum()); + pageRequest.setPageSize(query.getPageSize()); + pageRequest.setRequestId(query.getRequestId()); + pageRequest.setSortMap(query.getSortMap()); + return pageRequest; + } + } diff --git a/iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/PageRequestEmpty.java b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/PageRequestEmpty.java index c2ce71b2..44aaf6e4 100644 --- a/iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/PageRequestEmpty.java +++ b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/PageRequestEmpty.java @@ -10,6 +10,6 @@ import lombok.Data; */ @Data public class PageRequestEmpty { - private Integer page = 1; - private Integer size = 20; + private Integer pageNum = 1; + private Integer pageSize = 20; } diff --git a/iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/Request.java b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/Request.java index db50282d..0f55b3b2 100644 --- a/iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/Request.java +++ b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/Request.java @@ -1,5 +1,8 @@ package cc.iotkit.common.api; +import cc.iotkit.common.utils.SnowflakeIdGeneratorUtil; +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; import lombok.Data; import java.io.Serializable; @@ -13,12 +16,15 @@ import java.util.UUID; */ @Data public class Request extends RequestEmpty implements Serializable { - private T data; + + @Valid + @NotNull + private T data; public static Request of(T data) { - Request request = new Request(); + Request request = new Request<>(); request.setData(data); - request.setRequestId(UUID.randomUUID().toString()); + request.setRequestId(String.valueOf(SnowflakeIdGeneratorUtil.getInstanceSnowflake().nextId())); return request; } } diff --git a/iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/RequestEmpty.java b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/RequestEmpty.java index 3a0c167f..a7fb7d7d 100644 --- a/iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/RequestEmpty.java +++ b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/RequestEmpty.java @@ -1,5 +1,8 @@ package cc.iotkit.common.api; +import cc.iotkit.common.utils.SnowflakeIdGeneratorUtil; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; import lombok.Data; import java.io.Serializable; @@ -14,11 +17,12 @@ import java.util.UUID; @Data public class RequestEmpty implements Serializable { + @NotBlank private String requestId; public static RequestEmpty of() { RequestEmpty request = new RequestEmpty(); - request.setRequestId(UUID.randomUUID().toString()); + request.setRequestId(String.valueOf(SnowflakeIdGeneratorUtil.getInstanceSnowflake().nextId())); return request; } diff --git a/iot-common/iot-common-core/src/main/java/cc/iotkit/common/enums/ErrCode.java b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/enums/ErrCode.java index f5ff907d..9671aedd 100644 --- a/iot-common/iot-common-core/src/main/java/cc/iotkit/common/enums/ErrCode.java +++ b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/enums/ErrCode.java @@ -16,6 +16,8 @@ public enum ErrCode implements IEnum { NOT_FOUND(10000006, "请求资源不存在"), FORBIDDEN(10000007, "请求被拒绝"), UNAUTHORIZED_EXCEPTION(10000008, "未授权访问"), + UNSUPPORTED_OPERATION_EXCEPTION(10000009, "方法未实现"), + DATA_NOT_EXIST(10000010, "数据不存在"), /** * 组件通用异常段 diff --git a/iot-common/iot-common-core/src/main/java/cc/iotkit/common/exception/BizException.java b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/exception/BizException.java index bcd34e9d..a917a79d 100644 --- a/iot-common/iot-common-core/src/main/java/cc/iotkit/common/exception/BizException.java +++ b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/exception/BizException.java @@ -29,7 +29,7 @@ public class BizException extends RuntimeException { /** * 错误码 */ - private String code; + private Integer code; /** * 错误消息 @@ -38,7 +38,7 @@ public class BizException extends RuntimeException { public BizException(String message) { super(message); - this.code = ErrCode.SYSTEM_EXCEPTION.getValue(); + this.code = ErrCode.SYSTEM_EXCEPTION.getKey(); } /** @@ -48,6 +48,7 @@ public class BizException extends RuntimeException { */ public BizException(ErrCode errCode) { this.message = errCode.getValue(); + this.code = errCode.getKey(); } public BizException(ErrCode errCode, Throwable cause) { @@ -57,7 +58,7 @@ public class BizException extends RuntimeException { public BizException(ErrCode errCode, String message) { this.message = message; - this.code = errCode.getValue(); + this.code = errCode.getKey(); } public BizException(String message, Throwable cause) { diff --git a/iot-common/iot-common-core/src/main/java/cc/iotkit/common/validate/DeleteGroup.java b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/validate/DeleteGroup.java new file mode 100644 index 00000000..9f4b9c45 --- /dev/null +++ b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/validate/DeleteGroup.java @@ -0,0 +1,9 @@ +package cc.iotkit.common.validate; + +/** + * 校验分组 delete + * + * @author Lion Li + */ +public interface DeleteGroup { +} diff --git a/iot-common/iot-common-dao/iot-data-service/src/main/java/cc/iotkit/data/ICommonData.java b/iot-common/iot-common-dao/iot-data-service/src/main/java/cc/iotkit/data/ICommonData.java index f31e5e75..5454a9bc 100644 --- a/iot-common/iot-common-dao/iot-data-service/src/main/java/cc/iotkit/data/ICommonData.java +++ b/iot-common/iot-common-dao/iot-data-service/src/main/java/cc/iotkit/data/ICommonData.java @@ -9,9 +9,11 @@ */ package cc.iotkit.data; +import cc.iotkit.common.api.PageRequest; import cc.iotkit.model.Id; import cc.iotkit.common.api.Paging; +import java.util.Collection; import java.util.List; /** @@ -24,15 +26,22 @@ public interface ICommonData, ID> { */ T findById(ID id); + /** + * 通过ID取数据 + */ + List findByIds(Collection id); + + /** * 保存数据,id不为空更新,否则添加 */ T save(T data); + /** - * 添加数据 + * 批量保存数据 */ - T add(T data); + void batchSave(List data); /** * 按id删除 @@ -42,7 +51,7 @@ public interface ICommonData, ID> { /** * 按id批量删除 */ - void deleteByIds(ID[] ids); + void deleteByIds(Collection ids); /** * 总数统计 @@ -56,9 +65,17 @@ public interface ICommonData, ID> { /** * 分页获取所有信息 - * - * @param page 页码,从0开始 - * @param size 分页大小 */ - Paging findAll(int page, int size); + Paging findAll(PageRequest pageRequest); + + /** + * 按条件查询多个结果 + */ + List findAllByCondition(T data); + + /** + * 按条件查询单个结果 + */ + T findOneByCondition(T data); + } diff --git a/iot-common/iot-common-dao/iot-data-service/src/main/java/cc/iotkit/data/system/ISysConfigData.java b/iot-common/iot-common-dao/iot-data-service/src/main/java/cc/iotkit/data/system/ISysConfigData.java index ca722a3d..8b64a7d1 100644 --- a/iot-common/iot-common-dao/iot-data-service/src/main/java/cc/iotkit/data/system/ISysConfigData.java +++ b/iot-common/iot-common-dao/iot-data-service/src/main/java/cc/iotkit/data/system/ISysConfigData.java @@ -1,5 +1,7 @@ package cc.iotkit.data.system; +import cc.iotkit.common.api.Paging; +import cc.iotkit.common.api.PageRequest; import cc.iotkit.common.api.Paging; import cc.iotkit.data.ICommonData; import cc.iotkit.model.system.SysConfig; @@ -11,8 +13,4 @@ import cc.iotkit.model.system.SysConfig; */ public interface ISysConfigData extends ICommonData { - Paging findConfigs(SysConfig query); - - SysConfig findByConfigKey(String configKey); - } diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-cache/src/main/java/cc/iotkit/data/service/CategoryDataCache.java b/iot-common/iot-common-dao/iot-data-serviceImpl-cache/src/main/java/cc/iotkit/data/service/CategoryDataCache.java index 56ccc3d1..8632175f 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-cache/src/main/java/cc/iotkit/data/service/CategoryDataCache.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-cache/src/main/java/cc/iotkit/data/service/CategoryDataCache.java @@ -34,11 +34,6 @@ public class CategoryDataCache implements ICategoryData { return data; } - @Override - public Category add(Category data) { - return categoryData.add(data); - } - @Override public void deleteById(String s) { categoryData.deleteById(s); diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/AlertConfigDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/AlertConfigDataImpl.java index c116c8cc..870c36c4 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/AlertConfigDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/AlertConfigDataImpl.java @@ -36,10 +36,6 @@ public class AlertConfigDataImpl implements IAlertConfigData { return data; } - @Override - public AlertConfig add(AlertConfig data) { - return null; - } @Override public void deleteById(String s) { diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/AlertRecordDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/AlertRecordDataImpl.java index 3ca88192..64d80ca9 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/AlertRecordDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/AlertRecordDataImpl.java @@ -34,10 +34,6 @@ public class AlertRecordDataImpl implements IAlertRecordData { return null; } - @Override - public AlertRecord add(AlertRecord data) { - return null; - } @Override public void deleteById(String s) { diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/CategoryDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/CategoryDataImpl.java index 7adabe8d..91695038 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/CategoryDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/CategoryDataImpl.java @@ -42,13 +42,6 @@ public class CategoryDataImpl implements ICategoryData { return data; } - @Override - public Category add(Category data) { - TbCategory tb = categoryRepository.save(MapstructUtils.convert(data, TbCategory.class)); - data.setId(tb.getId()); - return data; - } - @Override public void deleteById(String s) { categoryRepository.deleteById(s); diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ChannelConfigDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ChannelConfigDataImpl.java index ab844531..08fd4b45 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ChannelConfigDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ChannelConfigDataImpl.java @@ -42,12 +42,6 @@ public class ChannelConfigDataImpl implements IChannelConfigData { return data; } - @Override - public ChannelConfig add(ChannelConfig data) { - data.setCreateAt(System.currentTimeMillis()); - return save(data); - } - @Override public void deleteById(String id) { channelConfigRepository.deleteById(id); diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ChannelDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ChannelDataImpl.java index 30ce9c4c..fb86fb02 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ChannelDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ChannelDataImpl.java @@ -44,11 +44,6 @@ public class ChannelDataImpl implements IChannelData { return data; } - @Override - public Channel add(Channel data) { - data.setCreateAt(System.currentTimeMillis()); - return save(data); - } @Override public void deleteById(String id) { diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ChannelTemplateDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ChannelTemplateDataImpl.java index 7b30a2d5..cd1f0dac 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ChannelTemplateDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ChannelTemplateDataImpl.java @@ -43,11 +43,6 @@ public class ChannelTemplateDataImpl implements IChannelTemplateData { return data; } - @Override - public ChannelTemplate add(ChannelTemplate data) { - data.setCreateAt(System.currentTimeMillis()); - return save(data); - } @Override public void deleteById(String id) { diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/CommonService.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/CommonService.java deleted file mode 100644 index 807de04f..00000000 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/CommonService.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * +---------------------------------------------------------------------- - * | Copyright (c) 奇特物联 2021-2022 All rights reserved. - * +---------------------------------------------------------------------- - * | Licensed 未经许可不能去掉「奇特物联」相关版权 - * +---------------------------------------------------------------------- - * | Author: xw2sy@163.com - * +---------------------------------------------------------------------- - */ -package cc.iotkit.data.service; - -import cc.iotkit.data.ICommonData; -import cc.iotkit.model.Id; -import cc.iotkit.common.api.Paging; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.jpa.repository.JpaRepository; - -import java.util.List; - -public class CommonService, T> implements ICommonData { - - private final JpaRepository repository; - - public CommonService(JpaRepository repository) { - this.repository = repository; - } - - @Override - public VO findById(T id) { - return repository.findById(id).orElse(null); - } - - @Override - public VO save(VO data) { - return repository.save(data); - } - - @Override - public VO add(VO data) { - return repository.save(data); - } - - @Override - public void deleteById(T id) { - repository.deleteById(id); - } - - @Override - public void deleteByIds(T[] ts) { - - } - - @Override - public long count() { - return repository.count(); - } - - @Override - public List findAll() { - return repository.findAll(); - } - - @Override - public Paging findAll(int page, int size) { - Page rst = repository.findAll(Pageable.ofSize(size).withPage(page - 1)); - return new Paging<>(rst.getTotalElements(), rst.getContent()); - } -} diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/DeviceConfigDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/DeviceConfigDataImpl.java index a8e9bfdb..b3a693e7 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/DeviceConfigDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/DeviceConfigDataImpl.java @@ -56,10 +56,6 @@ public class DeviceConfigDataImpl implements IDeviceConfigData { return data; } - @Override - public DeviceConfig add(DeviceConfig data) { - return save(data); - } @Override public void deleteById(String s) { diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/DeviceGroupDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/DeviceGroupDataImpl.java index be8fc392..9c06303e 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/DeviceGroupDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/DeviceGroupDataImpl.java @@ -45,11 +45,6 @@ public class DeviceGroupDataImpl implements IDeviceGroupData { return data; } - @Override - public DeviceGroup add(DeviceGroup data) { - data.setCreateAt(System.currentTimeMillis()); - return save(data); - } @Override public void deleteById(String s) { diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/DeviceInfoDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/DeviceInfoDataImpl.java index c1ff427f..7c4d87d1 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/DeviceInfoDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/DeviceInfoDataImpl.java @@ -463,12 +463,6 @@ public class DeviceInfoDataImpl implements IDeviceInfoData { return data; } - @Override - public DeviceInfo add(DeviceInfo data) { - data.setCreateAt(System.currentTimeMillis()); - return save(data); - } - @Override public void deleteById(String s) { deviceInfoRepository.deleteById(s); diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/HomeDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/HomeDataImpl.java index ea9fb4e7..c6cd3dfc 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/HomeDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/HomeDataImpl.java @@ -60,10 +60,6 @@ public class HomeDataImpl implements IHomeData { return data; } - @Override - public Home add(Home data) { - return save(data); - } @Override public void deleteById(String s) { diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/OauthClientDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/OauthClientDataImpl.java index 5a9345d5..f44b6701 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/OauthClientDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/OauthClientDataImpl.java @@ -51,11 +51,6 @@ public class OauthClientDataImpl implements IOauthClientData { return data; } - @Override - public OauthClient add(OauthClient data) { - return save(data); - } - @Override public void deleteById(String s) { oauthClientRepository.deleteById(s); diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProductDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProductDataImpl.java index 3928cd13..6a1536f5 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProductDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProductDataImpl.java @@ -56,11 +56,6 @@ public class ProductDataImpl implements IProductData { return data; } - @Override - public Product add(Product data) { - data.setCreateAt(System.currentTimeMillis()); - return save(data); - } @Override public void deleteById(String s) { diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProductModelDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProductModelDataImpl.java index 759fa66a..73ccc40d 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProductModelDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProductModelDataImpl.java @@ -47,10 +47,6 @@ public class ProductModelDataImpl implements IProductModelData { return null; } - @Override - public ProductModel add(ProductModel data) { - return save(data); - } @Override public void deleteById(String s) { diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProtocolComponentDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProtocolComponentDataImpl.java index a2da3a7d..47708f7f 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProtocolComponentDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProtocolComponentDataImpl.java @@ -66,10 +66,6 @@ public class ProtocolComponentDataImpl implements IProtocolComponentData { return data; } - @Override - public ProtocolComponent add(ProtocolComponent data) { - return save(data); - } @Override public void deleteById(String s) { diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProtocolConverterDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProtocolConverterDataImpl.java index 410ad815..1805b737 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProtocolConverterDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProtocolConverterDataImpl.java @@ -66,10 +66,6 @@ public class ProtocolConverterDataImpl implements IProtocolConverterData { return data; } - @Override - public ProtocolConverter add(ProtocolConverter data) { - return save(data); - } @Override public void deleteById(String s) { diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/RuleInfoDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/RuleInfoDataImpl.java index 384bee27..a99e899e 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/RuleInfoDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/RuleInfoDataImpl.java @@ -86,11 +86,6 @@ public class RuleInfoDataImpl implements IRuleInfoData { return data; } - @Override - public RuleInfo add(RuleInfo data) { - return save(data); - } - @Override public void deleteById(String s) { ruleInfoRepository.deleteById(s); diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/SpaceDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/SpaceDataImpl.java index 6c3f6bc8..b157cc92 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/SpaceDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/SpaceDataImpl.java @@ -70,11 +70,6 @@ public class SpaceDataImpl implements ISpaceData { return data; } - @Override - public Space add(Space data) { - return save(data); - } - @Override public void deleteById(String s) { spaceRepository.deleteById(s); diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/SpaceDeviceDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/SpaceDeviceDataImpl.java index c9407ecc..9b98bb95 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/SpaceDeviceDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/SpaceDeviceDataImpl.java @@ -95,11 +95,6 @@ public class SpaceDeviceDataImpl implements ISpaceDeviceData { return data; } - @Override - public SpaceDevice add(SpaceDevice data) { - return save(data); - } - @Override public void deleteById(String s) { spaceDeviceRepository.deleteById(s); diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/SysConfigDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/SysConfigDataImpl.java index a648fb2f..c5e0aa2e 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/SysConfigDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/SysConfigDataImpl.java @@ -1,15 +1,31 @@ package cc.iotkit.data.service; +import static cc.iotkit.data.model.QTbSysConfig.tbSysConfig; + +import cc.iotkit.common.api.PageRequest; +import cc.iotkit.common.api.Paging; +import cc.iotkit.common.enums.ErrCode; +import cc.iotkit.common.exception.BizException; import cc.iotkit.common.utils.MapstructUtils; +import cc.iotkit.common.utils.StringUtils; import cc.iotkit.data.dao.SysConfigRepository; import cc.iotkit.data.model.TbSysConfig; import cc.iotkit.data.system.ISysConfigData; -import cc.iotkit.common.api.Paging; +import cc.iotkit.data.util.PageBuilder; +import cc.iotkit.data.util.PredicateBuilder; import cc.iotkit.model.system.SysConfig; +import cn.hutool.core.collection.CollUtil; +import com.querydsl.core.types.Predicate; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; - +import java.util.Map; +import java.util.Objects; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Primary; +import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Sort.Direction; +import org.springframework.data.domain.Sort.Order; import org.springframework.stereotype.Service; @Primary @@ -22,7 +38,8 @@ public class SysConfigDataImpl implements ISysConfigData { @Override public SysConfig findById(Long id) { - TbSysConfig tbSysConfig = alertConfigRepository.findById(id).orElseThrow(); + TbSysConfig tbSysConfig = alertConfigRepository.findById(id).orElseThrow(() -> + new BizException(ErrCode.DATA_NOT_EXIST)); return MapstructUtils.convert(tbSysConfig,SysConfig.class); } @@ -33,43 +50,67 @@ public class SysConfigDataImpl implements ISysConfigData { } @Override - public SysConfig add(SysConfig data) { - return null; + public List findByIds(Collection id) { + throw new BizException(ErrCode.UNSUPPORTED_OPERATION_EXCEPTION); } @Override - public void deleteById(Long id) { - + public void batchSave(List data) { + throw new BizException(ErrCode.UNSUPPORTED_OPERATION_EXCEPTION); } @Override - public void deleteByIds(Long[] longs) { + public void deleteById(Long aLong) { + throw new BizException(ErrCode.UNSUPPORTED_OPERATION_EXCEPTION); + } + @Override + public void deleteByIds(Collection longs) { + throw new BizException(ErrCode.UNSUPPORTED_OPERATION_EXCEPTION); } @Override public long count() { - return 0; + throw new BizException(ErrCode.UNSUPPORTED_OPERATION_EXCEPTION); } @Override public List findAll() { - return null; + throw new BizException(ErrCode.UNSUPPORTED_OPERATION_EXCEPTION); } @Override - public Paging findAll(int page, int size) { - return null; - } + public Paging findAll(PageRequest pageRequest) { + SysConfig query = pageRequest.getData(); + Predicate predicate = PredicateBuilder.instance(tbSysConfig.configId.isNotNull()) + .and(StringUtils.isNotEmpty(query.getConfigKey()),() -> tbSysConfig.configKey.eq(query.getConfigKey())) + + .build(); + + List orders = new ArrayList<>(); + Map sortMap = pageRequest.getSortMap(); + if (CollUtil.isNotEmpty(sortMap)){ + sortMap.forEach((k,v) -> { + orders.add(new Order(Direction.ASC, k)); + }); + } + // TODO: 2023/5/26 抽成通用工具类方法 - @Override - public Paging findConfigs(SysConfig query) { - return null; + alertConfigRepository.findAll(predicate,PageBuilder.toPageable(pageRequest, Sort.by(orders))); + + + + throw new BizException(ErrCode.UNSUPPORTED_OPERATION_EXCEPTION); } @Override - public SysConfig findByConfigKey(String configKey) { - return null; + public List findAllByCondition(SysConfig data) { + throw new BizException(ErrCode.UNSUPPORTED_OPERATION_EXCEPTION); + } + + @Override + public SysConfig findOneByCondition(SysConfig data) { + throw new BizException(ErrCode.UNSUPPORTED_OPERATION_EXCEPTION); } } diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/TaskInfoDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/TaskInfoDataImpl.java index 2af7e1af..c9a81684 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/TaskInfoDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/TaskInfoDataImpl.java @@ -65,11 +65,6 @@ public class TaskInfoDataImpl implements ITaskInfoData { return data; } - @Override - public TaskInfo add(TaskInfo data) { - return save(data); - } - @Override public void deleteById(String s) { taskInfoRepository.deleteById(s); diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ThingModelDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ThingModelDataImpl.java index fd648322..db5965a5 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ThingModelDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ThingModelDataImpl.java @@ -43,11 +43,6 @@ public class ThingModelDataImpl implements IThingModelData { return data; } - @Override - public ThingModel add(ThingModel data) { - return save(data); - } - @Override public void deleteById(String s) { thingModelRepository.deleteById(s); diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/UserInfoDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/UserInfoDataImpl.java index fa2ab46b..a63739bf 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/UserInfoDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/UserInfoDataImpl.java @@ -62,11 +62,6 @@ public class UserInfoDataImpl implements IUserInfoData { return data; } - @Override - public UserInfo add(UserInfo data) { - return save(data); - } - @Override public void deleteById(String s) { userInfoRepository.deleteById(s); diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/VirtualDeviceDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/VirtualDeviceDataImpl.java index ed609620..5cc89dd1 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/VirtualDeviceDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/VirtualDeviceDataImpl.java @@ -105,11 +105,6 @@ public class VirtualDeviceDataImpl implements IVirtualDeviceData { return data; } - @Override - public VirtualDevice add(VirtualDevice data) { - return save(data); - } - @Override @Transactional public void deleteById(String s) { diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/util/PageBuilder.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/util/PageBuilder.java new file mode 100644 index 00000000..1c5f2988 --- /dev/null +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/util/PageBuilder.java @@ -0,0 +1,37 @@ +package cc.iotkit.data.util; + +import cc.iotkit.common.api.PageRequest; +import cc.iotkit.common.api.PageRequestEmpty; +import cc.iotkit.model.system.SysConfig; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; + +/** + * @author: Longjun.Tu + * @description: + * @date:created in 2023/5/26 17:14 + * @modificed by: + */ +public class PageBuilder { + + public static Pageable toPageable(PageRequest request, Sort.Direction direction, String... properties) { + return (Pageable)(request.getPageSize() <= 0 ? Pageable.unpaged() : org.springframework.data.domain.PageRequest.of(request.getPageNum() - 1, request.getPageSize(), direction, properties)); + } + + public static Pageable toPageable(PageRequest request) { + return (Pageable)(request.getPageSize() <= 0 ? Pageable.unpaged() : org.springframework.data.domain.PageRequest.of(request.getPageNum() - 1, request.getPageSize())); + } + + public static Pageable toPageable(PageRequestEmpty request) { + return (Pageable)(request.getPageNum() <= 0 ? Pageable.unpaged() : org.springframework.data.domain.PageRequest.of(request.getPageNum() - 1, request.getPageSize())); + } + + public static Pageable toPageable(PageRequestEmpty request, Sort.Direction direction, String... properties) { + return (Pageable)(request.getPageNum() <= 0 ? Pageable.unpaged() : org.springframework.data.domain.PageRequest.of(request.getPageNum() - 1, request.getPageSize(), direction, properties)); + } + + public static Pageable toPageable(PageRequest request, Sort sort) { + return (Pageable)(request.getPageSize() <= 0 ? Pageable.unpaged() : org.springframework.data.domain.PageRequest.of(request.getPageNum() - 1, request.getPageSize(), sort)); + } + +} diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/util/PredicateBuilder.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/util/PredicateBuilder.java new file mode 100644 index 00000000..74dc3e10 --- /dev/null +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/util/PredicateBuilder.java @@ -0,0 +1,116 @@ +package cc.iotkit.data.util; + +import com.querydsl.core.types.Predicate; +import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.core.types.dsl.Expressions; +import java.util.function.Supplier; + +/** + * @author: Longjun.Tu + * @description: + * @date:created in 2023/5/26 17:02 + * @modificed by: + */ +public class PredicateBuilder { + /** + * 1 = 1, 永远为真. + */ + private static final BooleanExpression ALWAYS_TRUE = Expressions.ONE.eq(Expressions.ONE); + + private BooleanExpression expression; + + private PredicateBuilder(BooleanExpression expression) { + this.expression = expression; + } + + /** + * 获取{@link PredicateBuilder}实例. + * + * @return PredicateBuilder + */ + public static PredicateBuilder instance() { + return instance(null); + } + + /** + * 获取{@link PredicateBuilder}实例. + * + * @param init 初始条件 + * @return PredicateBuilder + */ + public static PredicateBuilder instance(BooleanExpression init) { + return new PredicateBuilder(init); + } + + /** + * 使用 'and' 对条件进行拼接 + * + * @param expr Boolean expressions + * @return PredicateBuilder + */ + public PredicateBuilder and(BooleanExpression expr) { + return and(true, () -> expr); + } + + /** + * 如果条件为true, 则使用 'and' 对条件进行拼接 + * + * @param condition 执行条件, 如果为false, 则不会拼接该条件. + * @param exprSupplier expression supplier. + * @return PredicateBuilder + */ + public PredicateBuilder and(boolean condition, Supplier exprSupplier) { + if (condition) { + if (exprIsNull()) { + expression = exprSupplier.get(); + return this; + } + expression = expression.and(exprSupplier.get()); + } + return this; + } + + /** + * 使用 'or' 对条件进行拼接 + * + * @param expr Boolean expressions + * @return PredicateBuilder + */ + public PredicateBuilder or(BooleanExpression expr) { + return or(true, () -> expr); + } + + /** + * 如果条件为true, 则使用 'or' 对条件进行拼接 + * + * @param condition 执行条件, 如果为false, 则不会拼接该条件. + * @param exprSupplier expression supplier. + * @return PredicateBuilder + */ + public PredicateBuilder or(boolean condition, Supplier exprSupplier) { + if (condition) { + if (exprIsNull()) { + expression = exprSupplier.get(); + return this; + } + expression = expression.or(exprSupplier.get()); + } + return this; + } + + /** + * 获取最终的{@link BooleanExpression}表达式 + * + * @return Predicate, 如果没有拼接条件, 则默认返回 1 = 1的表达式. + */ + public Predicate build() { + if (exprIsNull()) { + return ALWAYS_TRUE; + } + return expression; + } + + private boolean exprIsNull() { + return null == expression; + } +} diff --git a/iot-common/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/helper/TenantHelper.java b/iot-common/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/helper/TenantHelper.java index b3521ce8..86231d77 100644 --- a/iot-common/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/helper/TenantHelper.java +++ b/iot-common/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/helper/TenantHelper.java @@ -1,138 +1,96 @@ -//package cc.iotkit.common.tenant.helper; -// -//import cc.iotkit.common.constant.GlobalConstants; -//import cc.iotkit.common.redis.utils.RedisUtils; -//import cc.iotkit.common.satoken.utils.LoginHelper; -//import cc.iotkit.common.utils.SpringUtils; -//import cc.iotkit.common.utils.StringUtils; -//import cn.dev33.satoken.context.SaHolder; -//import cn.dev33.satoken.spring.SpringMVCUtil; -//import cn.hutool.core.convert.Convert; -//import com.alibaba.ttl.TransmittableThreadLocal; -//import lombok.AccessLevel; -//import lombok.NoArgsConstructor; -//import lombok.extern.slf4j.Slf4j; -// -//import java.util.function.Supplier; -// -///** -// * 租户助手 -// * -// * @author Lion Li -// */ -//@Slf4j -//@NoArgsConstructor(access = AccessLevel.PRIVATE) -//public class TenantHelper { -// -// private static final String DYNAMIC_TENANT_KEY = GlobalConstants.GLOBAL_REDIS_KEY + "dynamicTenant"; -// -// private static final ThreadLocal TEMP_DYNAMIC_TENANT = new TransmittableThreadLocal<>(); -// -// /** -// * 租户功能是否启用 -// */ -// public static boolean isEnable() { -// return Convert.toBool(SpringUtils.getProperty("tenant.enable"), false); -// } -// -// /** -// * 开启忽略租户(开启后需手动调用 {@link #disableIgnore()} 关闭) -// */ -// public static void enableIgnore() { -// InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build()); -// } -// -// /** -// * 关闭忽略租户 -// */ -// public static void disableIgnore() { -// InterceptorIgnoreHelper.clearIgnoreStrategy(); -// } -// -// /** -// * 在忽略租户中执行 -// * -// * @param handle 处理执行方法 -// */ -// public static void ignore(Runnable handle) { -// enableIgnore(); -// try { -// handle.run(); -// } finally { -// disableIgnore(); -// } -// } -// -// /** -// * 在忽略租户中执行 -// * -// * @param handle 处理执行方法 -// */ -// public static T ignore(Supplier handle) { -// enableIgnore(); -// try { -// return handle.get(); -// } finally { -// disableIgnore(); -// } -// } -// -// /** -// * 设置动态租户(一直有效 需要手动清理) -// *

-// * 如果为非web环境 那么只在当前线程内生效 -// */ -// public static void setDynamic(String tenantId) { -// if (!SpringMVCUtil.isWeb()) { -// TEMP_DYNAMIC_TENANT.set(tenantId); -// return; -// } -// String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId(); -// RedisUtils.setCacheObject(cacheKey, tenantId); -// SaHolder.getStorage().set(cacheKey, tenantId); -// } -// -// /** -// * 获取动态租户(一直有效 需要手动清理) -// *

-// * 如果为非web环境 那么只在当前线程内生效 -// */ -// public static String getDynamic() { -// if (!SpringMVCUtil.isWeb()) { -// return TEMP_DYNAMIC_TENANT.get(); -// } -// String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId(); -// String tenantId = (String) SaHolder.getStorage().get(cacheKey); -// if (StringUtils.isNotBlank(tenantId)) { -// return tenantId; -// } -// tenantId = RedisUtils.getCacheObject(cacheKey); -// SaHolder.getStorage().set(cacheKey, tenantId); -// return tenantId; -// } -// -// /** -// * 清除动态租户 -// */ -// public static void clearDynamic() { -// if (!SpringMVCUtil.isWeb()) { -// TEMP_DYNAMIC_TENANT.remove(); -// return; -// } -// String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId(); -// RedisUtils.deleteObject(cacheKey); -// SaHolder.getStorage().delete(cacheKey); -// } -// -// /** -// * 获取当前租户id(动态租户优先) -// */ -// public static String getTenantId() { -// String tenantId = TenantHelper.getDynamic(); -// if (StringUtils.isBlank(tenantId)) { -// tenantId = LoginHelper.getTenantId(); -// } -// return tenantId; -// } -// -//} +package cc.iotkit.common.tenant.helper; + +import cc.iotkit.common.constant.GlobalConstants; +import cc.iotkit.common.redis.utils.RedisUtils; +import cc.iotkit.common.satoken.utils.LoginHelper; +import cc.iotkit.common.utils.SpringUtils; +import cc.iotkit.common.utils.StringUtils; +import cn.dev33.satoken.context.SaHolder; +import cn.dev33.satoken.spring.SpringMVCUtil; +import cn.hutool.core.convert.Convert; +import com.alibaba.ttl.TransmittableThreadLocal; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import java.util.function.Supplier; + +/** + * 租户助手 + * + * @author Lion Li + */ +@Slf4j +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class TenantHelper { + + private static final String DYNAMIC_TENANT_KEY = GlobalConstants.GLOBAL_REDIS_KEY + "dynamicTenant"; + + private static final ThreadLocal TEMP_DYNAMIC_TENANT = new TransmittableThreadLocal<>(); + + /** + * 租户功能是否启用 + */ + public static boolean isEnable() { + return Convert.toBool(SpringUtils.getProperty("tenant.enable"), false); + } + + /** + * 设置动态租户(一直有效 需要手动清理) + *

+ * 如果为非web环境 那么只在当前线程内生效 + */ + public static void setDynamic(String tenantId) { + if (!SpringMVCUtil.isWeb()) { + TEMP_DYNAMIC_TENANT.set(tenantId); + return; + } + String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId(); + RedisUtils.setCacheObject(cacheKey, tenantId); + SaHolder.getStorage().set(cacheKey, tenantId); + } + + /** + * 获取动态租户(一直有效 需要手动清理) + *

+ * 如果为非web环境 那么只在当前线程内生效 + */ + public static String getDynamic() { + if (!SpringMVCUtil.isWeb()) { + return TEMP_DYNAMIC_TENANT.get(); + } + String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId(); + String tenantId = (String) SaHolder.getStorage().get(cacheKey); + if (StringUtils.isNotBlank(tenantId)) { + return tenantId; + } + tenantId = RedisUtils.getCacheObject(cacheKey); + SaHolder.getStorage().set(cacheKey, tenantId); + return tenantId; + } + + /** + * 清除动态租户 + */ + public static void clearDynamic() { + if (!SpringMVCUtil.isWeb()) { + TEMP_DYNAMIC_TENANT.remove(); + return; + } + String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId(); + RedisUtils.deleteObject(cacheKey); + SaHolder.getStorage().delete(cacheKey); + } + + /** + * 获取当前租户id(动态租户优先) + */ + public static String getTenantId() { + String tenantId = TenantHelper.getDynamic(); + if (StringUtils.isBlank(tenantId)) { + tenantId = LoginHelper.getTenantId(); + } + return tenantId; + } + +} diff --git a/iot-module/iot-system/pom.xml b/iot-module/iot-system/pom.xml index 85417a1a..7358b8b9 100644 --- a/iot-module/iot-system/pom.xml +++ b/iot-module/iot-system/pom.xml @@ -53,6 +53,17 @@ iot-data-serviceImpl-rdb + + cc.iotkit + iot-common-doc + + + + cc.iotkit + iot-common-oss + + + diff --git a/iot-module/iot-system/src/main/java/cc/iotkit/system/controller/SysConfigController.java b/iot-module/iot-system/src/main/java/cc/iotkit/system/controller/SysConfigController.java index 80e67513..dfdce951 100644 --- a/iot-module/iot-system/src/main/java/cc/iotkit/system/controller/SysConfigController.java +++ b/iot-module/iot-system/src/main/java/cc/iotkit/system/controller/SysConfigController.java @@ -2,14 +2,19 @@ package cc.iotkit.system.controller; import cc.iotkit.common.api.PageRequest; import cc.iotkit.common.api.Paging; +import cc.iotkit.common.api.Request; import cc.iotkit.common.log.annotation.Log; import cc.iotkit.common.log.enums.BusinessType; import cc.iotkit.common.excel.utils.ExcelUtil; +import cc.iotkit.common.validate.EditGroup; +import cc.iotkit.common.validate.QueryGroup; import cc.iotkit.common.web.core.BaseController; import cc.iotkit.system.dto.bo.SysConfigBo; import cc.iotkit.system.dto.vo.SysConfigVo; import cn.dev33.satoken.annotation.SaCheckPermission; import cc.iotkit.system.service.ISysConfigService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -26,106 +31,89 @@ import java.util.List; @RequiredArgsConstructor @RestController @RequestMapping("/system/config") +@Api(tags = "参数配置 信息操作处理") public class SysConfigController extends BaseController { - private final ISysConfigService configService; + private final ISysConfigService configService; - /** - * 获取参数配置列表 - */ - @SaCheckPermission("system:config:list") - @PostMapping("/list") - public Paging list(@RequestBody @Validated PageRequest query) { - return configService.selectPageConfigList(query); - } - /** - * 导出参数配置列表 - */ - @Log(title = "参数管理", businessType = BusinessType.EXPORT) - @SaCheckPermission("system:config:export") - @PostMapping("/export") - public void export(SysConfigBo config, HttpServletResponse response) { - List list = configService.selectConfigList(config); - ExcelUtil.exportExcel(list, "参数数据", SysConfigVo.class, response); - } + @ApiOperation("获取参数配置列表") + @SaCheckPermission("system:config:list") + @PostMapping("/list") + public Paging list( + @RequestBody @Validated(QueryGroup.class) PageRequest query) { + return configService.selectPageConfigList(query); + } - /** - * 根据参数编号获取详细信息 - * - * @param configId 参数ID - */ - @SaCheckPermission("system:config:query") - @GetMapping(value = "/{configId}") - public SysConfigVo getInfo(@PathVariable Long configId) { - return configService.selectConfigById(configId); - } + @ApiOperation("导出参数配置列表") + @Log(title = "参数管理", businessType = BusinessType.EXPORT) + @SaCheckPermission("system:config:export") + @PostMapping("/export") + public void export(@RequestBody @Validated(QueryGroup.class) SysConfigBo config, + HttpServletResponse response) { + List list = configService.selectConfigList(config); + ExcelUtil.exportExcel(list, "参数数据", SysConfigVo.class, response); + } - /** - * 根据参数键名查询参数值 - * - * @param configKey 参数Key - */ - @GetMapping(value = "/configKey/{configKey}") - public void getConfigKey(@PathVariable String configKey) { - configService.selectConfigByKey(configKey); - } - /** - * 新增参数配置 - */ - @SaCheckPermission("system:config:add") - @Log(title = "参数管理", businessType = BusinessType.INSERT) - @PostMapping - public void add(@Validated @RequestBody SysConfigBo config) { - if (!configService.checkConfigKeyUnique(config)) { - fail("新增参数'" + config.getConfigName() + "'失败,参数键名已存在"); - } - configService.insertConfig(config); - } + @ApiOperation("根据参数编号获取详细信息") + @SaCheckPermission("system:config:query") + @PostMapping(value = "/getInfo") + public SysConfigVo getInfo(@RequestBody @Validated Request request) { + return configService.selectConfigById(request.getData()); + } - /** - * 修改参数配置 - */ - @SaCheckPermission("system:config:edit") - @Log(title = "参数管理", businessType = BusinessType.UPDATE) - @PutMapping - public void edit(@Validated @RequestBody SysConfigBo config) { - if (!configService.checkConfigKeyUnique(config)) { - fail("修改参数'" + config.getConfigName() + "'失败,参数键名已存在"); - } - configService.updateConfig(config); - } - /** - * 根据参数键名修改参数配置 - */ - @SaCheckPermission("system:config:edit") - @Log(title = "参数管理", businessType = BusinessType.UPDATE) - @PutMapping("/updateByKey") - public void updateByKey(@RequestBody SysConfigBo config) { - configService.updateConfig(config); - } + @ApiOperation("根据参数键名查询参数值") + @PostMapping(value = "/getConfigKey") + public void getConfigKey(@RequestBody @Validated Request request) { + configService.selectConfigByKey(request.getData()); + } - /** - * 删除参数配置 - * - * @param configIds 参数ID串 - */ - @SaCheckPermission("system:config:remove") - @Log(title = "参数管理", businessType = BusinessType.DELETE) - @DeleteMapping("/{configIds}") - public void remove(@PathVariable Long[] configIds) { - configService.deleteConfigByIds(configIds); - } - /** - * 刷新参数缓存 - */ - @SaCheckPermission("system:config:remove") - @Log(title = "参数管理", businessType = BusinessType.CLEAN) - @DeleteMapping("/refreshCache") - public void refreshCache() { - configService.resetConfigCache(); + @ApiOperation("新增参数配置") + @SaCheckPermission("system:config:add") + @Log(title = "参数管理", businessType = BusinessType.INSERT) + @PostMapping(value = "/add") + public void add(@RequestBody @Validated(EditGroup.class) Request request) { + if (!configService.checkConfigKeyUnique(request.getData())) { + fail("新增参数'" + request.getData().getConfigName() + "'失败,参数键名已存在"); } + configService.insertConfig(request.getData()); + } + + @ApiOperation("修改参数配置") + @SaCheckPermission("system:config:edit") + @Log(title = "参数管理", businessType = BusinessType.UPDATE) + @PostMapping(value = "/edit") + public void edit(@RequestBody @Validated(EditGroup.class) Request request) { + if (!configService.checkConfigKeyUnique(request.getData())) { + fail("修改参数'" + request.getData().getConfigName() + "'失败,参数键名已存在"); + } + configService.updateConfig(request.getData()); + } + + @ApiOperation("根据参数键名修改参数配置") + @SaCheckPermission("system:config:edit") + @Log(title = "参数管理", businessType = BusinessType.UPDATE) + @PostMapping("/updateByKey") + public void updateByKey(@RequestBody @Validated(EditGroup.class) Request request) { + configService.updateConfig(request.getData()); + } + + @ApiOperation("删除参数配置") + @SaCheckPermission("system:config:remove") + @Log(title = "参数管理", businessType = BusinessType.DELETE) + @PostMapping("/remove") + public void remove(@RequestBody @Validated Request> request) { + configService.deleteConfigByIds(request.getData()); + } + + @ApiOperation("刷新参数缓存") + @SaCheckPermission("system:config:remove") + @Log(title = "参数管理", businessType = BusinessType.CLEAN) + @PostMapping("/refreshCache") + public void refreshCache() { + configService.resetConfigCache(); + } } diff --git a/iot-module/iot-system/src/main/java/cc/iotkit/system/controller/SysTenantController.java b/iot-module/iot-system/src/main/java/cc/iotkit/system/controller/SysTenantController.java index 1344ca62..b02f172e 100644 --- a/iot-module/iot-system/src/main/java/cc/iotkit/system/controller/SysTenantController.java +++ b/iot-module/iot-system/src/main/java/cc/iotkit/system/controller/SysTenantController.java @@ -6,6 +6,7 @@ import cc.iotkit.common.api.Paging; import cc.iotkit.common.excel.utils.ExcelUtil; import cc.iotkit.common.log.annotation.Log; import cc.iotkit.common.log.enums.BusinessType; +import cc.iotkit.common.tenant.helper.TenantHelper; import cc.iotkit.common.validate.AddGroup; import cc.iotkit.common.validate.EditGroup; import cc.iotkit.common.web.core.BaseController; @@ -92,7 +93,7 @@ public class SysTenantController extends BaseController { if (!tenantService.checkCompanyNameUnique(bo)) { fail("新增租户'" + bo.getCompanyName() + "'失败,企业名称已存在"); } - TenantHelper.ignore(() -> tenantService.insertByBo(bo)); + //TenantHelper.ignore(() -> tenantService.insertByBo(bo)); } /** @@ -168,7 +169,7 @@ public class SysTenantController extends BaseController { @Log(title = "租户", businessType = BusinessType.UPDATE) @GetMapping("/syncTenantPackage") public void syncTenantPackage(@NotBlank(message = "租户ID不能为空") String tenantId, @NotBlank(message = "套餐ID不能为空") String packageId) { - TenantHelper.ignore(() -> tenantService.syncTenantPackage(tenantId, packageId)); + //TenantHelper.ignore(() -> tenantService.syncTenantPackage(tenantId, packageId)); } } diff --git a/iot-module/iot-system/src/main/java/cc/iotkit/system/service/ISysConfigService.java b/iot-module/iot-system/src/main/java/cc/iotkit/system/service/ISysConfigService.java index 238233fa..470b4147 100644 --- a/iot-module/iot-system/src/main/java/cc/iotkit/system/service/ISysConfigService.java +++ b/iot-module/iot-system/src/main/java/cc/iotkit/system/service/ISysConfigService.java @@ -69,7 +69,7 @@ public interface ISysConfigService { * * @param configIds 需要删除的参数ID */ - void deleteConfigByIds(Long[] configIds); + void deleteConfigByIds(List configIds); /** * 重置参数缓存数据 diff --git a/iot-module/iot-system/src/main/java/cc/iotkit/system/service/impl/SysConfigServiceImpl.java b/iot-module/iot-system/src/main/java/cc/iotkit/system/service/impl/SysConfigServiceImpl.java index 76dede98..6d8163b2 100644 --- a/iot-module/iot-system/src/main/java/cc/iotkit/system/service/impl/SysConfigServiceImpl.java +++ b/iot-module/iot-system/src/main/java/cc/iotkit/system/service/impl/SysConfigServiceImpl.java @@ -1,8 +1,8 @@ package cc.iotkit.system.service.impl; import cc.iotkit.common.api.PageRequest; +import cc.iotkit.common.api.Paging; import cc.iotkit.common.constant.CacheNames; - import cc.iotkit.common.constant.UserConstants; import cc.iotkit.common.exception.BizException; import cc.iotkit.common.redis.utils.CacheUtils; @@ -14,16 +14,13 @@ import cc.iotkit.data.system.ISysConfigData; import cc.iotkit.model.system.SysConfig; import cc.iotkit.system.dto.bo.SysConfigBo; import cc.iotkit.system.dto.vo.SysConfigVo; - -import cn.hutool.core.util.ObjectUtil; import cc.iotkit.system.service.ISysConfigService; +import cn.hutool.core.util.ObjectUtil; +import java.util.ArrayList; +import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import cc.iotkit.common.api.Paging; - -import java.util.ArrayList; -import java.util.List; /** * 参数配置 服务层实现 @@ -39,8 +36,8 @@ public class SysConfigServiceImpl implements ISysConfigService, ConfigService { @Override public Paging selectPageConfigList(PageRequest query) { - SysConfig sysConfig = MapstructUtils.convert(query.getData(), SysConfig.class); - return MapstructUtils.convert(sysConfigData.findConfigs(sysConfig), SysConfigVo.class); + PageRequest pageRequest = PageRequest.copyPageRequest(query,MapstructUtils.convert(query.getData(), SysConfig.class)); + return MapstructUtils.convert(sysConfigData.findAll(pageRequest), SysConfigVo.class); } /** @@ -62,7 +59,9 @@ public class SysConfigServiceImpl implements ISysConfigService, ConfigService { */ @Override public String selectConfigByKey(String configKey) { - SysConfig sysConfig = sysConfigData.findByConfigKey(configKey); + SysConfig data = new SysConfig(); + data.setConfigKey(configKey); + SysConfig sysConfig = sysConfigData.findOneByCondition(data); if (ObjectUtil.isNotNull(sysConfig)) { return sysConfig.getConfigValue(); } @@ -171,13 +170,13 @@ public class SysConfigServiceImpl implements ISysConfigService, ConfigService { * @param configIds 需要删除的参数ID */ @Override - public void deleteConfigByIds(Long[] configIds) { + public void deleteConfigByIds(List configIds) { for (Long configId : configIds) { SysConfig old = sysConfigData.findById(configId); if (StringUtils.equals(UserConstants.YES, old.getConfigType())) { throw new BizException(String.format("内置参数【%1$s】不能删除 ", old.getConfigKey())); } -// CacheUtils.evict(CacheNames.SYS_CONFIG, config.getConfigKey()); + CacheUtils.evict(CacheNames.SYS_CONFIG, old.getConfigKey()); } sysConfigData.deleteByIds(configIds); } diff --git a/iot-module/iot-system/src/main/java/cc/iotkit/system/service/impl/SysOssServiceImpl.java b/iot-module/iot-system/src/main/java/cc/iotkit/system/service/impl/SysOssServiceImpl.java index 0bd0d297..c10bfe19 100644 --- a/iot-module/iot-system/src/main/java/cc/iotkit/system/service/impl/SysOssServiceImpl.java +++ b/iot-module/iot-system/src/main/java/cc/iotkit/system/service/impl/SysOssServiceImpl.java @@ -4,7 +4,12 @@ import cc.iotkit.common.api.PageRequest; import cc.iotkit.common.api.Paging; import cc.iotkit.common.constant.CacheNames; import cc.iotkit.common.exception.BizException; +import cc.iotkit.common.oss.core.OssClient; +import cc.iotkit.common.oss.entity.UploadResult; +import cc.iotkit.common.oss.enumd.AccessPolicyType; +import cc.iotkit.common.oss.factory.OssFactory; import cc.iotkit.common.service.OssService; +import cc.iotkit.common.utils.MapstructUtils; import cc.iotkit.common.utils.SpringUtils; import cc.iotkit.common.utils.StreamUtils; import cc.iotkit.common.utils.StringUtils; diff --git a/pom.xml b/pom.xml index e56e456b..3bbd85ad 100755 --- a/pom.xml +++ b/pom.xml @@ -335,6 +335,12 @@ ${project.version} + + cc.iotkit + iot-common-oss + ${project.version} + + cc.iotkit iot-common-excel