diff --git a/shuili-system/pom.xml b/shuili-system/pom.xml index d9be2550..7e2d2cd4 100644 --- a/shuili-system/pom.xml +++ b/shuili-system/pom.xml @@ -1,84 +1,91 @@ - - - slSoft - com.slsoft - 0.0.1-SNAPSHOT - - 4.0.0 - - shuili-system - - shuili-system - - http://www.example.com - - 4.5.16 - - - - - - - org.springframework.boot - spring-boot-starter-web - 2.6.6 - - - - - mysql - mysql-connector-java - - - - org.apache.ant - ant - 1.10.10 - - - - - com.itextpdf - itextpdf - 5.4.3 - - - - - com.slsoft - shuili-generator - 0.0.1-SNAPSHOT - - - - com.slsoft - shuili-common - 0.0.1-SNAPSHOT - - - - - org.apache.httpcomponents - httpclient - 4.5.13 - - - - - net.sourceforge.htmlcleaner - htmlcleaner - 2.25 - - - - org.jsoup - jsoup - 1.15.3 - - - + + + slSoft + com.slsoft + 0.0.1-SNAPSHOT + + 4.0.0 + + shuili-system + + shuili-system + + http://www.example.com + + 4.5.16 + + + + + + + org.springframework.boot + spring-boot-starter-web + 2.6.6 + + + + + mysql + mysql-connector-java + + + + org.apache.ant + ant + 1.10.10 + + + + + com.itextpdf + itextpdf + 5.4.3 + + + + + com.slsoft + shuili-generator + 0.0.1-SNAPSHOT + + + + com.slsoft + shuili-common + 0.0.1-SNAPSHOT + + + + + org.apache.httpcomponents + httpclient + 4.5.13 + + + + + net.sourceforge.htmlcleaner + htmlcleaner + 2.25 + + + + org.jsoup + jsoup + 1.15.3 + + + + cn.hutool + hutool-core + 5.8.5 + + + diff --git a/shuili-system/src/main/java/com/kms/yxgh/base/DfException.java b/shuili-system/src/main/java/com/kms/yxgh/base/DfException.java new file mode 100644 index 00000000..5719b899 --- /dev/null +++ b/shuili-system/src/main/java/com/kms/yxgh/base/DfException.java @@ -0,0 +1,17 @@ +package com.kms.yxgh.base; + +import com.shuili.common.exception.BaseException; + +/** + * 用户信息异常类 + * + * @author shuili + */ +public class DfException extends BaseException { + + private static final long serialVersionUID = 1L; + + public DfException(String message) { + super("Df", "500", null, message); + } +} diff --git a/shuili-system/src/main/java/com/kms/yxgh/df/controller/DfCheckingController.java b/shuili-system/src/main/java/com/kms/yxgh/df/controller/DfCheckingController.java index 4b54f460..deefdcb6 100644 --- a/shuili-system/src/main/java/com/kms/yxgh/df/controller/DfCheckingController.java +++ b/shuili-system/src/main/java/com/kms/yxgh/df/controller/DfCheckingController.java @@ -6,6 +6,7 @@ import com.kms.yxgh.df.domain.dto.DfCheckingDetailDto; import com.kms.yxgh.df.domain.dto.SimpleDfCheckingDto; import com.kms.yxgh.df.domain.entity.DfChecking; import com.kms.yxgh.df.service.DfCheckingService; +import com.kms.yxgh.util.BeanCopyUtils; import com.shuili.common.annotation.Log; import com.shuili.common.core.domain.SearchParam; import com.shuili.common.enums.BusinessType; @@ -49,8 +50,9 @@ public class DfCheckingController { @PostMapping("/list-simple") @ApiOperation("堤防巡视检查列表-简略信息") - public Response> listSimple(@RequestBody SearchParam sp) { - return Response.ok(); + public Response> listSimple(SearchParam sp) { + return Response.ok( + BeanCopyUtils.copyList(dfCheckingService.selectSimple(sp), SimpleDfCheckingDto.class)); } @@ -59,16 +61,15 @@ public class DfCheckingController { */ @ApiOperation(" 堤防巡视检查详情") @GetMapping(value = "/{id}") - public Response getInfo(@PathVariable("id") Long id) { - // return Response.ok(dfCheckingService.getById(id)); - return Response.ok(); + public Response getInfo(@PathVariable("id") String id) { + return Response.ok(dfCheckingService.getDetailById(id)); } + @ApiOperation("堤防巡视检查新增") @Log(title = "堤防巡视检查新增", businessType = BusinessType.INSERT) @PostMapping public Response add(@RequestBody DfCheckingDetailDto dfChecking) { - // dfCheckingService.updateById(dfChecking); - return Response.ok(); + return Response.ok(dfCheckingService.add(dfChecking)); } /** @@ -78,8 +79,7 @@ public class DfCheckingController { @Log(title = "堤防巡视检查修改", businessType = BusinessType.UPDATE) @PutMapping public Response edit(@RequestBody DfCheckingDetailDto dfChecking) { - // dfCheckingService.updateById(dfChecking); - return Response.ok(); + return Response.ok(dfCheckingService.update(dfChecking)); } /** @@ -87,10 +87,9 @@ public class DfCheckingController { */ @ApiOperation("堤防巡视检查删除") @Log(title = "堤防巡视检查删除", businessType = BusinessType.DELETE) - @DeleteMapping("/{ids}") - public Response remove(@PathVariable Long[] ids) { - // dfCheckingService.removeByIds(Arrays.asList(ids)); - return Response.ok(); + @DeleteMapping("/{id}") + public Response remove(@PathVariable("id") String id) { + return Response.ok(dfCheckingService.deleteById(id)); } diff --git a/shuili-system/src/main/java/com/kms/yxgh/df/controller/DfPlanController.java b/shuili-system/src/main/java/com/kms/yxgh/df/controller/DfPlanController.java index c975724b..409b7352 100644 --- a/shuili-system/src/main/java/com/kms/yxgh/df/controller/DfPlanController.java +++ b/shuili-system/src/main/java/com/kms/yxgh/df/controller/DfPlanController.java @@ -3,6 +3,7 @@ package com.kms.yxgh.df.controller; import com.baomidou.mybatisplus.core.metadata.IPage; import com.kms.yxgh.base.Response; import com.kms.yxgh.df.domain.dto.DfPlanDetailDto; +import com.kms.yxgh.df.domain.dto.DfPointDto; import com.kms.yxgh.df.domain.entity.DfPlan; import com.kms.yxgh.df.service.DfPlanService; import com.shuili.common.annotation.Log; @@ -10,6 +11,7 @@ import com.shuili.common.core.domain.SearchParam; import com.shuili.common.enums.BusinessType; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import java.util.List; import lombok.AllArgsConstructor; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -84,4 +86,20 @@ public class DfPlanController { public Response remove(@PathVariable Long[] ids) { return Response.ok(); } + + @ApiOperation("堤防巡视检查计划巡查点设置") + @Log(title = "堤防巡视检查计划巡查点设置", businessType = BusinessType.UPDATE) + @PostMapping("/points/{id}") + public Response addPoints(@PathVariable("id") String id, + @RequestBody List points) { + return Response.ok(); + } + + + @ApiOperation("堤防巡视检查计划巡查点位数据") + @Log(title = "堤防巡视检查计划巡查点位数据", businessType = BusinessType.SEARCH) + @GetMapping("/points/{id}") + public Response addPoints(@PathVariable("id") String id) { + return Response.ok(); + } } diff --git a/shuili-system/src/main/java/com/kms/yxgh/df/domain/dto/DfCheckingDetailDto.java b/shuili-system/src/main/java/com/kms/yxgh/df/domain/dto/DfCheckingDetailDto.java index 077e4295..f7f0e6da 100644 --- a/shuili-system/src/main/java/com/kms/yxgh/df/domain/dto/DfCheckingDetailDto.java +++ b/shuili-system/src/main/java/com/kms/yxgh/df/domain/dto/DfCheckingDetailDto.java @@ -26,11 +26,11 @@ public class DfCheckingDetailDto { private Long type; @ApiModelProperty("检查项") - private List items; + private List items; @Data - public static class DfCheckingItem { + public static class DfCheckingItemDto { @ApiModelProperty("主键") private String id; diff --git a/shuili-system/src/main/java/com/kms/yxgh/df/domain/dto/DfPlanDetailDto.java b/shuili-system/src/main/java/com/kms/yxgh/df/domain/dto/DfPlanDetailDto.java index 32de4e0a..626508d7 100644 --- a/shuili-system/src/main/java/com/kms/yxgh/df/domain/dto/DfPlanDetailDto.java +++ b/shuili-system/src/main/java/com/kms/yxgh/df/domain/dto/DfPlanDetailDto.java @@ -23,7 +23,7 @@ public class DfPlanDetailDto { private String id; @ApiModelProperty("巡查ID") - private Long xcId; + private String xcId; @ApiModelProperty("巡查类型") private Long type; diff --git a/shuili-system/src/main/java/com/kms/yxgh/df/domain/dto/DfPointDto.java b/shuili-system/src/main/java/com/kms/yxgh/df/domain/dto/DfPointDto.java new file mode 100644 index 00000000..77abe92c --- /dev/null +++ b/shuili-system/src/main/java/com/kms/yxgh/df/domain/dto/DfPointDto.java @@ -0,0 +1,29 @@ +package com.kms.yxgh.df.domain.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + + +/** + * 堤防巡视检查计划点位 + * + * @author sy + * @date 2023-11-09 + */ +@Data +@ApiModel("堤防巡视检查计划点位") +public class DfPointDto { + + @ApiModelProperty("主键") + private String id; + + @ApiModelProperty("经度") + private Double longitude; + + @ApiModelProperty("纬度") + private Double latitude; + + @ApiModelProperty("海拔") + private Double altitude; +} diff --git a/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfCheckingItem.java b/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfCheckingItem.java index e102b514..ea475553 100644 --- a/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfCheckingItem.java +++ b/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfCheckingItem.java @@ -28,7 +28,7 @@ public class DfCheckingItem extends BaseEntity /** 巡查ID */ @Excel(name = "巡查ID") @ApiModelProperty("巡查ID") - private Long xcId; + private String xcId; /** 检查部位 */ @Excel(name = "检查部位") diff --git a/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfPlan.java b/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfPlan.java index 6320b37c..2b804445 100644 --- a/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfPlan.java +++ b/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfPlan.java @@ -28,7 +28,7 @@ public class DfPlan extends BaseEntity /** 巡查ID */ @Excel(name = "巡查ID") @ApiModelProperty("巡查ID") - private Long xcId; + private String xcId; /** 巡查类型 */ @Excel(name = "巡查类型") diff --git a/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfPlanOperator.java b/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfPlanOperator.java index ab0052e0..532acab1 100644 --- a/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfPlanOperator.java +++ b/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfPlanOperator.java @@ -28,7 +28,7 @@ public class DfPlanOperator extends BaseEntity /** 巡查计划ID */ @Excel(name = "巡查计划ID") @ApiModelProperty("巡查计划ID") - private Long planId; + private String planId; /** 执行人 */ @Excel(name = "执行人") diff --git a/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfPoint.java b/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfPoint.java index f40db412..e38e7d9c 100644 --- a/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfPoint.java +++ b/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfPoint.java @@ -28,7 +28,7 @@ public class DfPoint extends BaseEntity /** 巡查计划ID */ @Excel(name = "巡查计划ID") @ApiModelProperty("巡查计划ID") - private Long planId; + private String planId; /** 经度 */ @Excel(name = "经度") diff --git a/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfRecord.java b/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfRecord.java index 95624b5a..8cc9ce49 100644 --- a/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfRecord.java +++ b/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfRecord.java @@ -28,7 +28,7 @@ public class DfRecord extends BaseEntity /** 巡查计划ID */ @Excel(name = "巡查计划ID") @ApiModelProperty("巡查计划ID") - private Long planId; + private String planId; /** 巡查记录名称 */ @Excel(name = "巡查记录名称") diff --git a/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfRecordItem.java b/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfRecordItem.java index 2a86ba69..30f2fff3 100644 --- a/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfRecordItem.java +++ b/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfRecordItem.java @@ -28,7 +28,7 @@ public class DfRecordItem extends BaseEntity /** 巡查记录ID */ @Excel(name = "巡查记录ID") @ApiModelProperty("巡查记录ID") - private Long recordId; + private String recordId; /** 检查部位 */ @Excel(name = "检查部位") diff --git a/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfTrace.java b/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfTrace.java index a7531399..b10b78cb 100644 --- a/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfTrace.java +++ b/shuili-system/src/main/java/com/kms/yxgh/df/domain/entity/DfTrace.java @@ -1,15 +1,13 @@ package com.kms.yxgh.df.domain.entity; -import com.shuili.common.core.domain.BaseEntity; -import java.util.Date; +import com.baomidou.mybatisplus.annotation.TableName; import com.fasterxml.jackson.annotation.JsonFormat; +import com.shuili.common.annotation.Excel; +import com.shuili.common.core.domain.BaseEntity; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; +import java.util.Date; import lombok.Data; -import com.shuili.common.annotation.Excel; - -import com.baomidou.mybatisplus.annotation.TableName; - /** @@ -21,39 +19,51 @@ import com.baomidou.mybatisplus.annotation.TableName; @TableName("bs_sgc_df_xsjhjgj") @Data @ApiModel("堤防巡视检查检查记录轨迹") -public class DfTrace extends BaseEntity -{ - private static final long serialVersionUID = 1L; - - /** 巡查记录ID */ - @Excel(name = "巡查记录ID") - @ApiModelProperty("巡查记录ID") - private Long recordId; - - /** 经度 */ - @Excel(name = "经度") - @ApiModelProperty("经度") - private Long longitude; - - /** 纬度 */ - @Excel(name = "纬度") - @ApiModelProperty("纬度") - private Long latitude; - - /** 海拔 */ - @Excel(name = "海拔") - @ApiModelProperty("海拔") - private Long altitude; - - /** 创建人 */ - @Excel(name = "创建人") - @ApiModelProperty("创建人") - private String createUid; - - /** 创建时间 */ - @JsonFormat(pattern = "yyyy-MM-dd") - @Excel(name = "创建时间", width = 30, dateFormat = "yyyy-MM-dd") - @ApiModelProperty("创建时间") - private Date createDate; +public class DfTrace extends BaseEntity { + + private static final long serialVersionUID = 1L; + + /** + * 巡查记录ID + */ + @Excel(name = "巡查记录ID") + @ApiModelProperty("巡查记录ID") + private String recordId; + + /** + * 经度 + */ + @Excel(name = "经度") + @ApiModelProperty("经度") + private Long longitude; + + /** + * 纬度 + */ + @Excel(name = "纬度") + @ApiModelProperty("纬度") + private Long latitude; + + /** + * 海拔 + */ + @Excel(name = "海拔") + @ApiModelProperty("海拔") + private Long altitude; + + /** + * 创建人 + */ + @Excel(name = "创建人") + @ApiModelProperty("创建人") + private String createUid; + + /** + * 创建时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd") + @Excel(name = "创建时间", width = 30, dateFormat = "yyyy-MM-dd") + @ApiModelProperty("创建时间") + private Date createDate; } diff --git a/shuili-system/src/main/java/com/kms/yxgh/df/service/DfCheckingService.java b/shuili-system/src/main/java/com/kms/yxgh/df/service/DfCheckingService.java index e71b3588..fa286f03 100644 --- a/shuili-system/src/main/java/com/kms/yxgh/df/service/DfCheckingService.java +++ b/shuili-system/src/main/java/com/kms/yxgh/df/service/DfCheckingService.java @@ -1,9 +1,29 @@ package com.kms.yxgh.df.service; -import com.shuili.common.core.service.BaseService; -import com.kms.yxgh.df.mapper.DfCheckingMapper; +import cn.hutool.core.collection.CollectionUtil; +import com.baomidou.mybatisplus.core.conditions.Wrapper; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.kms.common.utils.BaseEntityUtils; +import com.kms.yxgh.base.DfException; +import com.kms.yxgh.df.domain.dto.DfCheckingDetailDto; +import com.kms.yxgh.df.domain.dto.DfCheckingDetailDto.DfCheckingItemDto; import com.kms.yxgh.df.domain.entity.DfChecking; +import com.kms.yxgh.df.domain.entity.DfCheckingItem; +import com.kms.yxgh.df.mapper.DfCheckingItemMapper; +import com.kms.yxgh.df.mapper.DfCheckingMapper; +import com.kms.yxgh.util.BeanCopyUtils; +import com.kms.yxgh.util.StreamUtils; +import com.shuili.common.core.domain.SearchParam; +import com.shuili.common.core.service.BaseService; +import com.shuili.common.exception.BaseException; +import com.shuili.common.utils.StringUtils; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; +import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; /** * 堤防巡视检查Service接口 @@ -12,6 +32,124 @@ import org.springframework.stereotype.Service; * @date 2023-11-09 */ @Service -public class DfCheckingService extends BaseService{ +@AllArgsConstructor +public class DfCheckingService extends BaseService { + + private final DfCheckingItemMapper dfCheckingItemMapper; + private final static String PART_SEPARATOR = ":"; + + public List selectSimple(SearchParam sp) { + Wrapper wp = Wrappers.lambdaQuery() + .select(DfChecking::getId, DfChecking::getName) + .eq(DfChecking::getType, sp.getData().getType()); + return getBaseMapper().selectList(wp); + } + + + public DfCheckingDetailDto getDetailById(String id) { + DfChecking checking = this.getById(id); + DfCheckingDetailDto dto = BeanCopyUtils.copy(checking, DfCheckingDetailDto.class); + if (dto != null) { + Wrapper wp = Wrappers.lambdaQuery() + .select(DfCheckingItem::getId, DfCheckingItem::getParts, DfCheckingItem::getContent) + .eq(DfCheckingItem::getXcId, id); + dto.setItems(StreamUtils.toList(dfCheckingItemMapper.selectList(wp), (o) -> { + DfCheckingItemDto itemDto = new DfCheckingItemDto(); + itemDto.setContent(o.getContent()); + itemDto.setId(o.getId()); + itemDto.setParts(Arrays.stream(StringUtils.split(o.getParts(), ":")) + .sorted(Comparator.comparingInt(String::length)).collect(Collectors.toList())); + return itemDto; + })); + } + return dto; + } + + + @Transactional(rollbackFor = Exception.class) + public DfCheckingDetailDto add(DfCheckingDetailDto dto) { + DfChecking checking = BeanCopyUtils.copy(dto, DfChecking.class); + if (checking != null) { + if (checkNameDistinct(checking.getId(), checking.getName())) { + BaseEntityUtils.preInsert(checking); + getBaseMapper().insert(checking); + String id = checking.getId(); + if (CollectionUtil.isNotEmpty(dto.getItems())) { + dto.getItems().forEach((o) -> { + DfCheckingItem item = new DfCheckingItem(); + item.setXcId(id); + item.setContent(o.getContent()); + item.setParts(String.join(PART_SEPARATOR, o.getParts())); + BaseEntityUtils.preInsert(item); + dfCheckingItemMapper.insert(item); + }); + } + return this.getDetailById(id); + } else { + throw new DfException("存在相同的名称"); + } + + } + return null; + } + + @Transactional(rollbackFor = Exception.class) + public DfCheckingDetailDto update(DfCheckingDetailDto dto) { + if (exist(dto.getId())) { + DfChecking checking = BeanCopyUtils.copy(dto, DfChecking.class); + if (checking != null) { + if (checkNameDistinct(checking.getId(), checking.getName())) { + BaseEntityUtils.preUpdate(checking); + getBaseMapper().updateById(checking); + String id = checking.getId(); + deleteItems(id); + if (CollectionUtil.isNotEmpty(dto.getItems())) { + dto.getItems().forEach((o) -> { + DfCheckingItem item = new DfCheckingItem(); + item.setXcId(id); + item.setContent(o.getContent()); + item.setParts(String.join(PART_SEPARATOR, o.getParts())); + BaseEntityUtils.preInsert(item); + dfCheckingItemMapper.insert(item); + }); + } + return this.getDetailById(id); + } else { + throw new DfException("存在相同的名称"); + } + } + } + throw new BaseException("源数据不存在,请确认id值是否正确"); + + } + + + @Transactional(rollbackFor = Exception.class) + public Boolean deleteById(String id) { + boolean rt = removeById(id); + if (rt) { + deleteItems(id); + } + return rt; + } + + private void deleteItems(String id) { + Wrapper wp = Wrappers.lambdaQuery() + .eq(DfCheckingItem::getXcId, id); + dfCheckingItemMapper.delete(wp); + } + + private boolean checkNameDistinct(String id, String name) { + Wrapper wp = Wrappers.lambdaQuery() + .eq(DfChecking::getName, name) + .ne(StringUtils.isNotEmpty(id), DfChecking::getId, id); + return this.getBaseMapper().selectCount(wp) <= 0; + } + + private boolean exist(String id) { + Wrapper wp = Wrappers.lambdaQuery() + .eq(DfChecking::getId, id); + return this.getBaseMapper().selectCount(wp) > 0; + } } diff --git a/shuili-system/src/main/java/com/kms/yxgh/df/service/DfPlanService.java b/shuili-system/src/main/java/com/kms/yxgh/df/service/DfPlanService.java index 0f9256a1..1dca5394 100644 --- a/shuili-system/src/main/java/com/kms/yxgh/df/service/DfPlanService.java +++ b/shuili-system/src/main/java/com/kms/yxgh/df/service/DfPlanService.java @@ -1,8 +1,14 @@ package com.kms.yxgh.df.service; -import com.shuili.common.core.service.BaseService; -import com.kms.yxgh.df.mapper.DfPlanMapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.kms.yxgh.df.domain.entity.DfPlan; +import com.kms.yxgh.df.mapper.DfPlanMapper; +import com.shuili.common.core.domain.SearchParam; +import com.shuili.common.core.service.BaseService; +import com.shuili.common.mybaitsplus.BeanToWrapper; +import lombok.AllArgsConstructor; +import org.apache.poi.ss.formula.functions.T; import org.springframework.stereotype.Service; /** @@ -12,6 +18,7 @@ import org.springframework.stereotype.Service; * @date 2023-11-09 */ @Service -public class DfPlanService extends BaseService{ +@AllArgsConstructor +public class DfPlanService extends BaseService { } diff --git a/shuili-system/src/main/java/com/kms/yxgh/util/BeanCopyUtils.java b/shuili-system/src/main/java/com/kms/yxgh/util/BeanCopyUtils.java new file mode 100644 index 00000000..0e45aef9 --- /dev/null +++ b/shuili-system/src/main/java/com/kms/yxgh/util/BeanCopyUtils.java @@ -0,0 +1,178 @@ +package com.kms.yxgh.util; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.lang.SimpleCache; +import cn.hutool.core.map.MapUtil; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.ReflectUtil; +import cn.hutool.core.util.StrUtil; +import java.util.List; +import java.util.Map; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.springframework.cglib.beans.BeanCopier; +import org.springframework.cglib.beans.BeanMap; +import org.springframework.cglib.core.Converter; + +/** + * bean深拷贝工具(基于 cglib 性能优异) + * + * @author sy + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class BeanCopyUtils { + + /** + * 单对象基于class创建拷贝 + * + * @param source 数据来源实体 + * @param desc 描述对象 转换后的对象 + * @return desc + */ + public static V copy(T source, Class desc) { + if (ObjectUtil.isNull(source)) { + return null; + } + if (ObjectUtil.isNull(desc)) { + return null; + } + final V target = ReflectUtil.newInstanceIfPossible(desc); + return copy(source, target); + } + + /** + * 单对象基于对象创建拷贝 + * + * @param source 数据来源实体 + * @param desc 转换后的对象 + * @return desc + */ + public static V copy(T source, V desc) { + if (ObjectUtil.isNull(source)) { + return null; + } + if (ObjectUtil.isNull(desc)) { + return null; + } + BeanCopier beanCopier = BeanCopierCache.INSTANCE.get(source.getClass(), desc.getClass(), null); + beanCopier.copy(source, desc, null); + return desc; + } + + /** + * 列表对象基于class创建拷贝 + * + * @param sourceList 数据来源实体列表 + * @param desc 描述对象 转换后的对象 + * @return desc + */ + public static List copyList(List sourceList, Class desc) { + if (ObjectUtil.isNull(sourceList)) { + return null; + } + if (CollUtil.isEmpty(sourceList)) { + return CollUtil.newArrayList(); + } + return StreamUtils.toList(sourceList, source -> { + V target = ReflectUtil.newInstanceIfPossible(desc); + copy(source, target); + return target; + }); + } + + /** + * bean拷贝到map + * + * @param bean 数据来源实体 + * @return map对象 + */ + @SuppressWarnings("unchecked") + public static Map copyToMap(T bean) { + if (ObjectUtil.isNull(bean)) { + return null; + } + return BeanMap.create(bean); + } + + /** + * map拷贝到bean + * + * @param map 数据来源 + * @param beanClass bean类 + * @return bean对象 + */ + public static T mapToBean(Map map, Class beanClass) { + if (MapUtil.isEmpty(map)) { + return null; + } + if (ObjectUtil.isNull(beanClass)) { + return null; + } + T bean = ReflectUtil.newInstanceIfPossible(beanClass); + return mapToBean(map, bean); + } + + /** + * map拷贝到bean + * + * @param map 数据来源 + * @param bean bean对象 + * @return bean对象 + */ + public static T mapToBean(Map map, T bean) { + if (MapUtil.isEmpty(map)) { + return null; + } + if (ObjectUtil.isNull(bean)) { + return null; + } + BeanMap.create(bean).putAll(map); + return bean; + } + + /** + * BeanCopier属性缓存
缓存用于防止多次反射造成的性能问题 + * + * @author Looly + * @since 5.4.1 + */ + public enum BeanCopierCache { + /** + * BeanCopier属性缓存单例 + */ + INSTANCE; + + private final SimpleCache cache = new SimpleCache<>(); + + /** + * 获得类与转换器生成的key在{@link BeanCopier}的Map中对应的元素 + * + * @param srcClass 源Bean的类 + * @param targetClass 目标Bean的类 + * @param converter 转换器 + * @return Map中对应的BeanCopier + */ + public BeanCopier get(Class srcClass, Class targetClass, Converter converter) { + final String key = genKey(srcClass, targetClass, converter); + return cache.get(key, () -> BeanCopier.create(srcClass, targetClass, converter != null)); + } + + /** + * 获得类与转换器生成的key + * + * @param srcClass 源Bean的类 + * @param targetClass 目标Bean的类 + * @param converter 转换器 + * @return 属性名和Map映射的key + */ + private String genKey(Class srcClass, Class targetClass, Converter converter) { + final StringBuilder key = StrUtil.builder() + .append(srcClass.getName()).append('#').append(targetClass.getName()); + if (null != converter) { + key.append('#').append(converter.getClass().getName()); + } + return key.toString(); + } + } + +} diff --git a/shuili-system/src/main/java/com/kms/yxgh/util/StreamUtils.java b/shuili-system/src/main/java/com/kms/yxgh/util/StreamUtils.java new file mode 100644 index 00000000..3fbfc020 --- /dev/null +++ b/shuili-system/src/main/java/com/kms/yxgh/util/StreamUtils.java @@ -0,0 +1,259 @@ +package com.kms.yxgh.util; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.map.MapUtil; +import cn.hutool.core.util.StrUtil; +import java.util.Collection; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +/** + * stream 流工具类 + * + * @author Lion Li + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class StreamUtils { + + /** + * 将collection过滤 + * + * @param collection 需要转化的集合 + * @param function 过滤方法 + * @return 过滤后的list + */ + public static List filter(Collection collection, Predicate function) { + if (CollUtil.isEmpty(collection)) { + return CollUtil.newArrayList(); + } + return collection.stream().filter(function).collect(Collectors.toList()); + } + + /** + * 将collection拼接 + * + * @param collection 需要转化的集合 + * @param function 拼接方法 + * @return 拼接后的list + */ + public static String join(Collection collection, Function function) { + return join(collection, function, ","); + } + + /** + * 将collection拼接 + * + * @param collection 需要转化的集合 + * @param function 拼接方法 + * @param delimiter 拼接符 + * @return 拼接后的list + */ + public static String join(Collection collection, Function function, CharSequence delimiter) { + if (CollUtil.isEmpty(collection)) { + return StrUtil.EMPTY; + } + return collection.stream().map(function).filter(Objects::nonNull).collect(Collectors.joining(delimiter)); + } + + /** + * 将collection排序 + * + * @param collection 需要转化的集合 + * @param comparing 排序方法 + * @return 排序后的list + */ + public static List sorted(Collection collection, Comparator comparing) { + if (CollUtil.isEmpty(collection)) { + return CollUtil.newArrayList(); + } + return collection.stream().sorted(comparing).collect(Collectors.toList()); + } + + /** + * 将collection转化为类型不变的map
+ * {@code Collection ----> Map} + * + * @param collection 需要转化的集合 + * @param key V类型转化为K类型的lambda方法 + * @param collection中的泛型 + * @param map中的key类型 + * @return 转化后的map + */ + public static Map toIdentityMap(Collection collection, Function key) { + if (CollUtil.isEmpty(collection)) { + return MapUtil.newHashMap(); + } + return collection.stream().collect(Collectors.toMap(key, Function.identity(), (l, r) -> l)); + } + + /** + * 将Collection转化为map(value类型与collection的泛型不同)
+ * {@code Collection -----> Map } + * + * @param collection 需要转化的集合 + * @param key E类型转化为K类型的lambda方法 + * @param value E类型转化为V类型的lambda方法 + * @param collection中的泛型 + * @param map中的key类型 + * @param map中的value类型 + * @return 转化后的map + */ + public static Map toMap(Collection collection, Function key, Function value) { + if (CollUtil.isEmpty(collection)) { + return MapUtil.newHashMap(); + } + return collection.stream().collect(Collectors.toMap(key, value, (l, r) -> l)); + } + + /** + * 将collection按照规则(比如有相同的班级id)分类成map
+ * {@code Collection -------> Map> } + * + * @param collection 需要分类的集合 + * @param key 分类的规则 + * @param collection中的泛型 + * @param map中的key类型 + * @return 分类后的map + */ + public static Map> groupByKey(Collection collection, Function key) { + if (CollUtil.isEmpty(collection)) { + return MapUtil.newHashMap(); + } + return collection + .stream() + .collect(Collectors.groupingBy(key, LinkedHashMap::new, Collectors.toList())); + } + + /** + * 将collection按照两个规则(比如有相同的年级id,班级id)分类成双层map
+ * {@code Collection ---> Map>> } + * + * @param collection 需要分类的集合 + * @param key1 第一个分类的规则 + * @param key2 第二个分类的规则 + * @param 集合元素类型 + * @param 第一个map中的key类型 + * @param 第二个map中的key类型 + * @return 分类后的map + */ + public static Map>> groupBy2Key(Collection collection, Function key1, Function key2) { + if (CollUtil.isEmpty(collection)) { + return MapUtil.newHashMap(); + } + return collection + .stream() + .collect(Collectors.groupingBy(key1, LinkedHashMap::new, Collectors.groupingBy(key2, LinkedHashMap::new, Collectors.toList()))); + } + + /** + * 将collection按照两个规则(比如有相同的年级id,班级id)分类成双层map
+ * {@code Collection ---> Map> } + * + * @param collection 需要分类的集合 + * @param key1 第一个分类的规则 + * @param key2 第二个分类的规则 + * @param 第一个map中的key类型 + * @param 第二个map中的key类型 + * @param collection中的泛型 + * @return 分类后的map + */ + public static Map> group2Map(Collection collection, Function key1, Function key2) { + if (CollUtil.isEmpty(collection) || key1 == null || key2 == null) { + return MapUtil.newHashMap(); + } + return collection + .stream() + .collect(Collectors.groupingBy(key1, LinkedHashMap::new, Collectors.toMap(key2, Function.identity(), (l, r) -> l))); + } + + /** + * 将collection转化为List集合,但是两者的泛型不同
+ * {@code Collection ------> List } + * + * @param collection 需要转化的集合 + * @param function collection中的泛型转化为list泛型的lambda表达式 + * @param collection中的泛型 + * @param List中的泛型 + * @return 转化后的list + */ + public static List toList(Collection collection, Function function) { + if (CollUtil.isEmpty(collection)) { + return CollUtil.newArrayList(); + } + return collection + .stream() + .map(function) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + + /** + * 将collection转化为Set集合,但是两者的泛型不同
+ * {@code Collection ------> Set } + * + * @param collection 需要转化的集合 + * @param function collection中的泛型转化为set泛型的lambda表达式 + * @param collection中的泛型 + * @param Set中的泛型 + * @return 转化后的Set + */ + public static Set toSet(Collection collection, Function function) { + if (CollUtil.isEmpty(collection) || function == null) { + return CollUtil.newHashSet(); + } + return collection + .stream() + .map(function) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); + } + + + /** + * 合并两个相同key类型的map + * + * @param map1 第一个需要合并的 map + * @param map2 第二个需要合并的 map + * @param merge 合并的lambda,将key value1 value2合并成最终的类型,注意value可能为空的情况 + * @param map中的key类型 + * @param 第一个 map的value类型 + * @param 第二个 map的value类型 + * @param 最终map的value类型 + * @return 合并后的map + */ + public static Map merge(Map map1, Map map2, BiFunction merge) { + if (MapUtil.isEmpty(map1) && MapUtil.isEmpty(map2)) { + return MapUtil.newHashMap(); + } else if (MapUtil.isEmpty(map1)) { + map1 = MapUtil.newHashMap(); + } else if (MapUtil.isEmpty(map2)) { + map2 = MapUtil.newHashMap(); + } + Set key = new HashSet<>(); + key.addAll(map1.keySet()); + key.addAll(map2.keySet()); + Map map = new HashMap<>(); + for (K t : key) { + X x = map1.get(t); + Y y = map2.get(t); + V z = merge.apply(x, y); + if (z != null) { + map.put(t, z); + } + } + return map; + } + +}