ycl-pojo/src/main/java/com/ycl/platform/domain/entity/CalculateRule.java
@@ -6,8 +6,12 @@ import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum; import com.alibaba.excel.enums.poi.VerticalAlignmentEnum; import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableName; import com.ycl.system.domain.TreeEntity; import com.ycl.system.domain.TreeNode; import lombok.Data; import java.util.Date; /** * 分数核算规则对象 t_default_rule @@ -18,7 +22,8 @@ @Data @ExcelIgnoreUnannotated @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER) public class CalculateRule extends TreeEntity { @TableName("t_calculate_rule") public class CalculateRule extends TreeNode { private static final long serialVersionUID = 1L; /** @@ -28,9 +33,9 @@ private Long id; /** * 单位id * 合同id */ private Integer unitId; private Integer contractId; /** * 规则名称 @@ -73,7 +78,10 @@ @ExcelProperty("除以数量") private Integer calcUnit; private Date createTime; private Date updateTime; @TableLogic private String deleted; private Integer deleted; } ycl-pojo/src/main/java/com/ycl/platform/domain/entity/TContract.java
@@ -5,9 +5,10 @@ import com.alibaba.excel.annotation.write.style.ContentStyle; import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum; import com.alibaba.excel.enums.poi.VerticalAlignmentEnum; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableLogic; import com.fasterxml.jackson.annotation.JsonFormat; import com.ycl.system.entity.BaseEntity; import lombok.Data; import java.util.Date; @@ -21,19 +22,23 @@ @Data @ExcelIgnoreUnannotated @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER) public class TContract extends BaseEntity { public class TContract { private static final long serialVersionUID = 1L; /** * 主键 */ @ExcelProperty("序号") @TableId private Long id; /** * 单位id */ private Integer unitId; @TableField(exist = false) private String unitName; /** * 合同名称 @@ -43,15 +48,19 @@ /** * 开始时间 */ @JsonFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = "yyyy-MM-dd hh-mm-ss") private Date startTime; /** * 结束时间 */ @JsonFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = "yyyy-MM-dd hh-mm-ss") private Date endTime; private Date createTime; private Date updateTime; @TableLogic private String deleted; ycl-pojo/src/main/java/com/ycl/system/domain/TreeNode.java
New file @@ -0,0 +1,55 @@ package com.ycl.system.domain; import com.baomidou.mybatisplus.annotation.TableField; import java.io.Serializable; import java.util.ArrayList; import java.util.List; /** * 树节点,所有需要实现树节点的,都需要继承该类 * * @author Mark sunlightcs@gmail.com * @since 1.0.0 */ public class TreeNode<T> implements Serializable { private static final long serialVersionUID = 1L; /** * 主键 */ private Long id; /** * 上级ID */ private Long parentId; /** * 子节点列表 */ @TableField(exist = false) private List<T> children = new ArrayList<>(); public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Long getParentId() { return parentId; } public void setParentId(Long pid) { this.parentId = pid; } public List<T> getChildren() { return children; } public void setChildren(List<T> children) { this.children = children; } } ycl-pojo/src/main/java/com/ycl/system/domain/TreeUtils.java
New file @@ -0,0 +1,75 @@ package com.ycl.system.domain; import org.apache.commons.lang3.ObjectUtils; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; /** * 树形结构工具类,如:菜单、部门等 * * @author Mark sunlightcs@gmail.com * @since 1.0.0 */ public class TreeUtils { /** * 根据pid,构建树节点 */ public static <T extends TreeNode> List<T> build(List<T> treeNodes, Long pid) { //pid不能为空 if(ObjectUtils.isEmpty(pid)) { return null; } List<T> treeList = new ArrayList<>(); for (T treeNode : treeNodes) { if (pid.equals(treeNode.getParentId())) { treeList.add(findChildren(treeNodes, treeNode)); } } return treeList; } /** * 查找子节点 */ private static <T extends TreeNode> T findChildren(List<T> treeNodes, T rootNode) { for (T treeNode : treeNodes) { if (rootNode.getId().equals(treeNode.getParentId())) { rootNode.getChildren().add(findChildren(treeNodes, treeNode)); } } return rootNode; } /** * 构建树节点 */ public static <T extends TreeNode> List<T> build(List<T> treeNodes) { List<T> result = new ArrayList<>(); //list转map Map<Long, T> nodeMap = new LinkedHashMap<>(treeNodes.size()); for (T treeNode : treeNodes) { nodeMap.put(treeNode.getId(), treeNode); } for (T node : nodeMap.values()) { T parent = nodeMap.get(node.getParentId()); if (parent != null && !(node.getId().equals(parent.getId()))) { parent.getChildren().add(node); continue; } result.add(node); } return result; } } ycl-server/src/main/java/com/ycl/platform/controller/CalculateRuleController.java
@@ -39,6 +39,16 @@ } /** * 根据合同id查询违约规则列表 */ @PreAuthorize("@ss.hasPermi('system:rule:query')") @GetMapping("/getRuleListByContractId") public AjaxResult getRuleListByContractId(Integer contractId) { return success(defaultRuleService.getRuleListByContractId(contractId)); } /** * 导出违约规则列表 */ @PreAuthorize("@ss.hasPermi('system:rule:export')") ycl-server/src/main/java/com/ycl/platform/controller/TContractController.java
@@ -1,17 +1,25 @@ package com.ycl.platform.controller; import annotation.Log; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ycl.platform.domain.entity.TContract; import com.ycl.platform.domain.entity.TMonitor; import com.ycl.platform.domain.query.YwUnitQuery; import com.ycl.platform.domain.vo.TMonitorVO; import com.ycl.platform.service.ITContractService; import com.ycl.system.AjaxResult; import com.ycl.system.Result; import com.ycl.system.controller.BaseController; import com.ycl.utils.poi.ExcelUtil; import com.ycl.system.page.TableDataInfo; import enumeration.BusinessType; import io.swagger.annotations.ApiOperation; import jakarta.servlet.http.HttpServletResponse; import lombok.AllArgsConstructor; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.util.List; /** * 【请填写功能名称】Controller @@ -26,6 +34,11 @@ private final ITContractService tContractService; @PreAuthorize("@ss.hasPermi('system:contract:list')") @GetMapping("/list") public List<TContract> list() { return tContractService.selectAll(); } /** * 合同导入模板 @@ -40,17 +53,13 @@ /** * 合同导入 * * @param file 导入文件 * @param unitId 运维单位 * @param startTime 开始时间 * @param endTime 结束时间 * @return 导入结果 */ @Log(title = "合同导入", businessType = BusinessType.IMPORT) @PreAuthorize("@ss.hasPermi('system:user:import')") @PostMapping("/importData") public AjaxResult importData(MultipartFile file, String unitId, String startTime, String endTime) { ExcelUtil<TContract> util = new ExcelUtil<>(TContract.class); public AjaxResult importData(MultipartFile file, TContract tContract) { tContractService.importData(file, tContract); return success(); } ycl-server/src/main/java/com/ycl/platform/service/ICalculateRuleService.java
@@ -1,6 +1,9 @@ package com.ycl.platform.service; import com.baomidou.mybatisplus.extension.service.IService; import com.ycl.platform.domain.entity.CalculateRule; import com.ycl.platform.domain.entity.TContract; import org.springframework.web.multipart.MultipartFile; import java.util.List; @@ -10,8 +13,7 @@ * @author ruoyi * @date 2024-04-01 */ public interface ICalculateRuleService { public interface ICalculateRuleService extends IService<CalculateRule> { /** * 查询违约规则 * @@ -59,4 +61,13 @@ * @return 结果 */ public int deleteDefaultRuleById(Long id); /** * 读取Excel数据 * * @param file 文件 */ List<CalculateRule> readExcel(MultipartFile file); List<CalculateRule> getRuleListByContractId(Integer contractId); } ycl-server/src/main/java/com/ycl/platform/service/ITContractService.java
@@ -3,6 +3,9 @@ import com.baomidou.mybatisplus.extension.service.IService; import com.ycl.platform.domain.entity.TContract; import jakarta.servlet.http.HttpServletResponse; import org.springframework.web.multipart.MultipartFile; import java.util.List; /** * 【请填写功能名称】Service接口 @@ -17,4 +20,14 @@ * @param response 结果 */ void importTemplate(HttpServletResponse response); /** * 导入合同数据 * @param file 文件 * @param tContract 合同信息 * @return 导入结果 */ void importData(MultipartFile file, TContract tContract); List<TContract> selectAll(); } ycl-server/src/main/java/com/ycl/platform/service/impl/CalculateRuleServiceImpl.java
@@ -1,13 +1,26 @@ package com.ycl.platform.service.impl; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.enums.CellExtraTypeEnum; import com.alibaba.excel.metadata.CellExtra; import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.fastjson2.JSON; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ycl.platform.domain.entity.CalculateRule; import com.ycl.platform.domain.entity.TContract; import com.ycl.platform.mapper.CalculateRuleMapper; import com.ycl.platform.service.ICalculateRuleService; import com.ycl.system.domain.TreeNode; import com.ycl.system.domain.TreeUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import utils.DateUtils; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** @@ -17,8 +30,7 @@ * @date 2024-04-01 */ @Service public class CalculateRuleServiceImpl extends ServiceImpl<CalculateRuleMapper, CalculateRule> implements ICalculateRuleService { public class CalculateRuleServiceImpl extends ServiceImpl<CalculateRuleMapper, CalculateRule> implements ICalculateRuleService { @Autowired private CalculateRuleMapper calculateRuleMapper; @@ -29,8 +41,7 @@ * @return 违约规则 */ @Override public CalculateRule selectDefaultRuleById(Long id) { public CalculateRule selectDefaultRuleById(Long id) { return calculateRuleMapper.selectDefaultRuleById(id); } @@ -41,8 +52,7 @@ * @return 违约规则 */ @Override public List<CalculateRule> selectDefaultRuleList(CalculateRule calculateRule) { public List<CalculateRule> selectDefaultRuleList(CalculateRule calculateRule) { return calculateRuleMapper.selectDefaultRuleList(calculateRule); } @@ -53,8 +63,7 @@ * @return 结果 */ @Override public int insertDefaultRule(CalculateRule calculateRule) { public int insertDefaultRule(CalculateRule calculateRule) { calculateRule.setCreateTime(DateUtils.getNowDate()); return calculateRuleMapper.insertDefaultRule(calculateRule); } @@ -66,8 +75,7 @@ * @return 结果 */ @Override public int updateDefaultRule(CalculateRule calculateRule) { public int updateDefaultRule(CalculateRule calculateRule) { calculateRule.setUpdateTime(DateUtils.getNowDate()); return calculateRuleMapper.updateDefaultRule(calculateRule); } @@ -79,8 +87,7 @@ * @return 结果 */ @Override public int deleteDefaultRuleByIds(Long[] ids) { public int deleteDefaultRuleByIds(Long[] ids) { return calculateRuleMapper.deleteDefaultRuleByIds(ids); } @@ -91,8 +98,47 @@ * @return 结果 */ @Override public int deleteDefaultRuleById(Long id) { public int deleteDefaultRuleById(Long id) { return calculateRuleMapper.deleteDefaultRuleById(id); } /** * 获取合同Excel规则 * * @param file 合同Excel文件 * @return 规则 */ @Override public List<CalculateRule> readExcel(MultipartFile file) { List<CalculateRule> calculateRuleList = new ArrayList<>(); try { EasyExcel.read(file.getInputStream(), CalculateRule.class, new ReadListener<CalculateRule>() { @Override public void invoke(CalculateRule calculateRule, AnalysisContext analysisContext) { calculateRule.setId(null); calculateRuleList.add(calculateRule); } @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { } }) .head(CalculateRule.class) .sheet() .doRead(); } catch (IOException e) { throw new RuntimeException(e); } return calculateRuleList; } @Override public List<CalculateRule> getRuleListByContractId(Integer contractId) { List<CalculateRule> list = new LambdaQueryChainWrapper<>(calculateRuleMapper) .eq(CalculateRule::getContractId, contractId) .list(); return TreeUtils.build(list, 0L); } } ycl-server/src/main/java/com/ycl/platform/service/impl/TContractServiceImpl.java
@@ -3,19 +3,28 @@ import com.alibaba.excel.EasyExcel; import com.alibaba.excel.write.merge.LoopMergeStrategy; import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ycl.handler.CommentWriteHandler; import com.ycl.handler.CustomSheetWriteHandler; import com.ycl.platform.domain.entity.CalculateRule; import com.ycl.platform.domain.entity.TContract; import com.ycl.platform.mapper.TContractMapper; import com.ycl.platform.service.ICalculateRuleService; import com.ycl.platform.service.ITContractService; import com.ycl.utils.DateUtils; import com.ycl.utils.StringUtils; import jakarta.servlet.http.HttpServletResponse; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Date; import java.util.List; /** @@ -25,7 +34,12 @@ * @date 2024-03-12 */ @Service @AllArgsConstructor public class TContractServiceImpl extends ServiceImpl<TContractMapper, TContract> implements ITContractService { private final ICalculateRuleService calculateRuleService; private final YwUnitServiceImpl ywUnitService; @Override public void importTemplate(HttpServletResponse response) { try { @@ -87,12 +101,10 @@ calculateRule4.setCalcFraction(0.1); list.add(calculateRule4); CalculateRule calculateRule7 = new CalculateRule(); calculateRule7.setRuleName("…………"); calculateRule7.setRuleDesc("…………"); calculateRule7.setRuleCondition("…………"); list.add(calculateRule7); CalculateRule calculateRule6 = new CalculateRule(); calculateRule6.setRuleName("…………"); calculateRule6.setRuleDesc("…………"); calculateRule6.setRuleCondition("…………"); list.add(calculateRule6); @@ -107,4 +119,45 @@ list.add(calculateRule5); return list; } @Override @Transactional public void importData(MultipartFile file, TContract tContract) { tContract.setCreateTime(DateUtils.getNowDate()); tContract.setDeleted("0"); save(tContract); List<CalculateRule> list = calculateRuleService.readExcel(file); // 遍历父子关系 List<CalculateRule> calculateRulesToSave = new ArrayList<>(); CalculateRule temp = new CalculateRule(); for (CalculateRule calculateRule : list) { // 父规则 if (StringUtils.isNotBlank(calculateRule.getRuleName())) { CalculateRule fu = new CalculateRule(); fu.setContractId(tContract.getId().intValue()); fu.setRuleName(calculateRule.getRuleName()); fu.setCreateTime(DateUtils.getNowDate()); fu.setDeleted(0); calculateRuleService.save(fu); temp = fu; } calculateRule.setContractId(tContract.getId().intValue()); calculateRule.setCreateTime(DateUtils.getNowDate()); calculateRule.setDeleted(0); calculateRule.setParentId(temp.getId()); calculateRulesToSave.add(calculateRule); } // 批量保存规则 calculateRuleService.saveBatch(calculateRulesToSave); } @Override public List<TContract> selectAll() { return list(new LambdaQueryWrapper<TContract>() .orderByDesc(TContract::getCreateTime)) .stream().peek( tContract -> tContract.setUnitName(ywUnitService.getById(tContract.getUnitId()).getUnitName()) ).toList(); } }