diff --git a/src/main/java/com/gaotao/modules/sys/controller/SysSceneExceptionRuleController.java b/src/main/java/com/gaotao/modules/sys/controller/SysSceneExceptionRuleController.java new file mode 100644 index 0000000..099c2a8 --- /dev/null +++ b/src/main/java/com/gaotao/modules/sys/controller/SysSceneExceptionRuleController.java @@ -0,0 +1,70 @@ +package com.gaotao.modules.sys.controller; + +import com.gaotao.common.utils.PageUtils; +import com.gaotao.common.utils.R; +import com.gaotao.modules.sys.entity.SysSceneExceptionRuleConditionEntity; +import com.gaotao.modules.sys.entity.SysSceneExceptionRuleEntity; +import com.gaotao.modules.sys.service.SysSceneExceptionRuleService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("sysSceneExceptionRule") +public class SysSceneExceptionRuleController { + + @Autowired + private SysSceneExceptionRuleService sysSceneExceptionRuleService; + + @PostMapping("/searchRule") + public R searchRule(@RequestBody SysSceneExceptionRuleEntity data) { + PageUtils page = sysSceneExceptionRuleService.searchRule(data); + return R.ok().put("page", page); + } + + @PostMapping("/saveRule") + public R saveRule(@RequestBody SysSceneExceptionRuleEntity data) { + sysSceneExceptionRuleService.saveRule(data); + return R.ok().put("ruleCode", data.getRuleCode()); + } + + @PostMapping("/updateRule") + public R updateRule(@RequestBody SysSceneExceptionRuleEntity data) { + sysSceneExceptionRuleService.updateRule(data); + return R.ok(); + } + + @PostMapping("/deleteRule") + public R deleteRule(@RequestBody SysSceneExceptionRuleEntity data) { + sysSceneExceptionRuleService.deleteRule(data); + return R.ok(); + } + + @PostMapping("/getConditionList") + public R getConditionList(@RequestBody SysSceneExceptionRuleConditionEntity data) { + List list = sysSceneExceptionRuleService.getConditionList(data); + return R.ok().put("rows", list); + } + + @PostMapping("/saveCondition") + public R saveCondition(@RequestBody SysSceneExceptionRuleConditionEntity data) { + sysSceneExceptionRuleService.saveCondition(data); + return R.ok(); + } + + @PostMapping("/updateCondition") + public R updateCondition(@RequestBody SysSceneExceptionRuleConditionEntity data) { + sysSceneExceptionRuleService.updateCondition(data); + return R.ok(); + } + + @PostMapping("/deleteCondition") + public R deleteCondition(@RequestBody SysSceneExceptionRuleConditionEntity data) { + sysSceneExceptionRuleService.deleteCondition(data); + return R.ok(); + } +} diff --git a/src/main/java/com/gaotao/modules/sys/entity/SysSceneExceptionRuleConditionEntity.java b/src/main/java/com/gaotao/modules/sys/entity/SysSceneExceptionRuleConditionEntity.java new file mode 100644 index 0000000..9547d22 --- /dev/null +++ b/src/main/java/com/gaotao/modules/sys/entity/SysSceneExceptionRuleConditionEntity.java @@ -0,0 +1,47 @@ +package com.gaotao.modules.sys.entity; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.gaotao.common.utils.QueryPage; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.springframework.format.annotation.DateTimeFormat; + +import java.math.BigDecimal; +import java.util.Date; +import java.util.List; + +@Data +@EqualsAndHashCode(callSuper = true) +public class SysSceneExceptionRuleConditionEntity extends QueryPage { + + private String site; + private String buNo; + private String ruleCode; + private String conditionNo; + private String parentConditionNo; + private String relationField; + private String compareSymbol; + private BigDecimal conditionParam; + /** + * 强制 / 提示 + */ + private String forceType; + private String warningLevel; + private String animationEffect; + private String responsibilityDept; + /** + * 异常代码 / 异常单 + */ + private String triggerEvent; + private String createBy; + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date createDate; + private String updateBy; + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date updateDate; + + private List children; + private List submitList; +} diff --git a/src/main/java/com/gaotao/modules/sys/entity/SysSceneExceptionRuleEntity.java b/src/main/java/com/gaotao/modules/sys/entity/SysSceneExceptionRuleEntity.java new file mode 100644 index 0000000..857dea6 --- /dev/null +++ b/src/main/java/com/gaotao/modules/sys/entity/SysSceneExceptionRuleEntity.java @@ -0,0 +1,47 @@ +package com.gaotao.modules.sys.entity; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.gaotao.common.utils.QueryPage; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; +import java.util.List; + +@Data +@EqualsAndHashCode(callSuper = true) +public class SysSceneExceptionRuleEntity extends QueryPage { + + private String site; + private String buNo; + /** + * 前端 BU 选择项,格式:site_buNo + */ + private String bu; + private String ruleCode; + private String ruleName; + /** + * 应用页面 + */ + private String applyPage; + /** + * 应用按钮 + */ + private String applyButton; + /** + * Y: 启用 N: 禁用 + */ + private String enableFlag; + private String userName; + private String createBy; + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date createDate; + private String updateBy; + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date updateDate; + + private List submitList; +} diff --git a/src/main/java/com/gaotao/modules/sys/mapper/SysSceneExceptionRuleMapper.java b/src/main/java/com/gaotao/modules/sys/mapper/SysSceneExceptionRuleMapper.java new file mode 100644 index 0000000..8cf6c09 --- /dev/null +++ b/src/main/java/com/gaotao/modules/sys/mapper/SysSceneExceptionRuleMapper.java @@ -0,0 +1,39 @@ +package com.gaotao.modules.sys.mapper; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.gaotao.modules.sys.entity.SysSceneExceptionRuleConditionEntity; +import com.gaotao.modules.sys.entity.SysSceneExceptionRuleEntity; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +@Mapper +public interface SysSceneExceptionRuleMapper { + + IPage searchRule(Page page, + @Param("query") SysSceneExceptionRuleEntity data); + + void saveRule(SysSceneExceptionRuleEntity data); + + void updateRule(SysSceneExceptionRuleEntity data); + + void deleteRule(SysSceneExceptionRuleEntity data); + + void deleteConditionByRule(SysSceneExceptionRuleEntity data); + + List getConditionList(SysSceneExceptionRuleConditionEntity data); + + SysSceneExceptionRuleConditionEntity getCondition(SysSceneExceptionRuleConditionEntity data); + + Integer countConditionNo(SysSceneExceptionRuleConditionEntity data); + + void saveCondition(SysSceneExceptionRuleConditionEntity data); + + void updateCondition(SysSceneExceptionRuleConditionEntity data); + + void deleteCondition(SysSceneExceptionRuleConditionEntity data); + + void deleteConditionWithChildren(SysSceneExceptionRuleConditionEntity data); +} diff --git a/src/main/java/com/gaotao/modules/sys/service/SysSceneExceptionRuleService.java b/src/main/java/com/gaotao/modules/sys/service/SysSceneExceptionRuleService.java new file mode 100644 index 0000000..0f955ff --- /dev/null +++ b/src/main/java/com/gaotao/modules/sys/service/SysSceneExceptionRuleService.java @@ -0,0 +1,26 @@ +package com.gaotao.modules.sys.service; + +import com.gaotao.common.utils.PageUtils; +import com.gaotao.modules.sys.entity.SysSceneExceptionRuleConditionEntity; +import com.gaotao.modules.sys.entity.SysSceneExceptionRuleEntity; + +import java.util.List; + +public interface SysSceneExceptionRuleService { + + PageUtils searchRule(SysSceneExceptionRuleEntity data); + + void saveRule(SysSceneExceptionRuleEntity data); + + void updateRule(SysSceneExceptionRuleEntity data); + + void deleteRule(SysSceneExceptionRuleEntity data); + + List getConditionList(SysSceneExceptionRuleConditionEntity data); + + void saveCondition(SysSceneExceptionRuleConditionEntity data); + + void updateCondition(SysSceneExceptionRuleConditionEntity data); + + void deleteCondition(SysSceneExceptionRuleConditionEntity data); +} diff --git a/src/main/java/com/gaotao/modules/sys/service/impl/SysSceneExceptionRuleServiceImpl.java b/src/main/java/com/gaotao/modules/sys/service/impl/SysSceneExceptionRuleServiceImpl.java new file mode 100644 index 0000000..175128d --- /dev/null +++ b/src/main/java/com/gaotao/modules/sys/service/impl/SysSceneExceptionRuleServiceImpl.java @@ -0,0 +1,237 @@ +package com.gaotao.modules.sys.service.impl; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.gaotao.common.utils.PageUtils; +import com.gaotao.modules.sys.entity.SysSceneExceptionRuleConditionEntity; +import com.gaotao.modules.sys.entity.SysSceneExceptionRuleEntity; +import com.gaotao.modules.sys.entity.SysUserEntity; +import com.gaotao.modules.sys.mapper.SysSceneExceptionRuleMapper; +import com.gaotao.modules.sys.service.SysSceneExceptionRuleService; +import com.gaotao.modules.trans.dao.TransNoControlMapper; +import com.gaotao.modules.trans.entity.TransNoControl; +import org.apache.commons.lang3.StringUtils; +import org.apache.shiro.SecurityUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +@Service +public class SysSceneExceptionRuleServiceImpl implements SysSceneExceptionRuleService { + + private static final String TRANS_TYPE = "ExR"; + + @Autowired + private SysSceneExceptionRuleMapper sysSceneExceptionRuleMapper; + + @Autowired + private TransNoControlMapper transNoControlMapper; + + @Override + public PageUtils searchRule(SysSceneExceptionRuleEntity data) { + int pageNo = data.getPage() <= 0 ? 1 : data.getPage(); + int pageSize = data.getLimit() <= 0 ? 20 : data.getLimit(); + IPage page = sysSceneExceptionRuleMapper.searchRule( + new Page(pageNo, pageSize), data); + return new PageUtils(page); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void saveRule(SysSceneExceptionRuleEntity data) { + fillSiteAndBu(data); + if (StringUtils.isBlank(data.getRuleName())) { + throw new RuntimeException("规则名称不能为空"); + } + if (StringUtils.isBlank(data.getApplyPage())) { + throw new RuntimeException("应用页面不能为空"); + } + if (StringUtils.isBlank(data.getApplyButton())) { + throw new RuntimeException("应用按钮不能为空"); + } + TransNoControl transNo = transNoControlMapper.getTransNo(data.getSite(), TRANS_TYPE, data.getBuNo()); + if (transNo == null || StringUtils.isBlank(transNo.getNewTransNo())) { + throw new RuntimeException("规则编码生成失败,请先在TransNoControl维护trans_type=" + TRANS_TYPE); + } + data.setRuleCode(transNo.getNewTransNo()); + data.setEnableFlag(StringUtils.isBlank(data.getEnableFlag()) ? "Y" : data.getEnableFlag()); + data.setCreateBy(getCurrentUserName()); + data.setUpdateBy(getCurrentUserName()); + sysSceneExceptionRuleMapper.saveRule(data); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void updateRule(SysSceneExceptionRuleEntity data) { + if (StringUtils.isBlank(data.getSite()) || StringUtils.isBlank(data.getBuNo()) || StringUtils.isBlank(data.getRuleCode())) { + throw new RuntimeException("更新失败,规则主键不能为空"); + } + if (StringUtils.isBlank(data.getRuleName())) { + throw new RuntimeException("规则名称不能为空"); + } + if (StringUtils.isBlank(data.getApplyPage())) { + throw new RuntimeException("应用页面不能为空"); + } + if (StringUtils.isBlank(data.getApplyButton())) { + throw new RuntimeException("应用按钮不能为空"); + } + data.setEnableFlag(StringUtils.isBlank(data.getEnableFlag()) ? "Y" : data.getEnableFlag()); + data.setUpdateBy(getCurrentUserName()); + sysSceneExceptionRuleMapper.updateRule(data); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void deleteRule(SysSceneExceptionRuleEntity data) { + List deleteList = new ArrayList<>(); + if (data.getSubmitList() != null && !data.getSubmitList().isEmpty()) { + deleteList = data.getSubmitList(); + } else { + deleteList.add(data); + } + for (SysSceneExceptionRuleEntity item : deleteList) { + if (StringUtils.isBlank(item.getSite()) || StringUtils.isBlank(item.getBuNo()) || StringUtils.isBlank(item.getRuleCode())) { + continue; + } + sysSceneExceptionRuleMapper.deleteConditionByRule(item); + sysSceneExceptionRuleMapper.deleteRule(item); + } + } + + @Override + public List getConditionList(SysSceneExceptionRuleConditionEntity data) { + List list = sysSceneExceptionRuleMapper.getConditionList(data); + if (list == null || list.isEmpty()) { + return new ArrayList<>(); + } + Map conditionMap = new LinkedHashMap<>(); + List rootList = new ArrayList<>(); + for (SysSceneExceptionRuleConditionEntity item : list) { + item.setChildren(new ArrayList<>()); + conditionMap.put(item.getConditionNo(), item); + } + for (SysSceneExceptionRuleConditionEntity item : list) { + if (StringUtils.isBlank(item.getParentConditionNo())) { + rootList.add(item); + continue; + } + SysSceneExceptionRuleConditionEntity parent = conditionMap.get(item.getParentConditionNo()); + if (parent == null) { + rootList.add(item); + } else { + parent.getChildren().add(item); + } + } + return rootList; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void saveCondition(SysSceneExceptionRuleConditionEntity data) { + checkConditionData(data, true); + data.setCreateBy(getCurrentUserName()); + data.setUpdateBy(getCurrentUserName()); + sysSceneExceptionRuleMapper.saveCondition(data); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void updateCondition(SysSceneExceptionRuleConditionEntity data) { + checkConditionData(data, false); + data.setUpdateBy(getCurrentUserName()); + sysSceneExceptionRuleMapper.updateCondition(data); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void deleteCondition(SysSceneExceptionRuleConditionEntity data) { + List deleteList = new ArrayList<>(); + if (data.getSubmitList() != null && !data.getSubmitList().isEmpty()) { + deleteList = data.getSubmitList(); + } else { + deleteList.add(data); + } + for (SysSceneExceptionRuleConditionEntity item : deleteList) { + if (StringUtils.isBlank(item.getSite()) || StringUtils.isBlank(item.getBuNo()) + || StringUtils.isBlank(item.getRuleCode()) || StringUtils.isBlank(item.getConditionNo())) { + continue; + } + sysSceneExceptionRuleMapper.deleteConditionWithChildren(item); + } + } + + private void checkConditionData(SysSceneExceptionRuleConditionEntity data, boolean checkDuplicate) { + if (StringUtils.isBlank(data.getSite()) || StringUtils.isBlank(data.getBuNo()) || StringUtils.isBlank(data.getRuleCode())) { + throw new RuntimeException("条件主键信息不能为空"); + } + if (StringUtils.isBlank(data.getConditionNo())) { + throw new RuntimeException("条件编号不能为空"); + } + if (StringUtils.isBlank(data.getRelationField())) { + throw new RuntimeException("关系字段不能为空"); + } + if (StringUtils.isBlank(data.getCompareSymbol())) { + throw new RuntimeException("比较符不能为空"); + } + if (data.getConditionParam() == null) { + throw new RuntimeException("条件参数不能为空"); + } + if (StringUtils.isBlank(data.getForceType())) { + throw new RuntimeException("是否强制不能为空"); + } + if (StringUtils.isBlank(data.getTriggerEvent())) { + throw new RuntimeException("触发异常事件不能为空"); + } + if (StringUtils.isNotBlank(data.getParentConditionNo())) { + if (StringUtils.equals(data.getParentConditionNo(), data.getConditionNo())) { + throw new RuntimeException("父条件不能等于当前条件"); + } + SysSceneExceptionRuleConditionEntity parentQuery = new SysSceneExceptionRuleConditionEntity(); + parentQuery.setSite(data.getSite()); + parentQuery.setBuNo(data.getBuNo()); + parentQuery.setRuleCode(data.getRuleCode()); + parentQuery.setConditionNo(data.getParentConditionNo()); + SysSceneExceptionRuleConditionEntity parent = sysSceneExceptionRuleMapper.getCondition(parentQuery); + if (parent == null) { + throw new RuntimeException("父条件不存在,请刷新后重试"); + } + if (StringUtils.isNotBlank(parent.getParentConditionNo())) { + throw new RuntimeException("触发条件仅支持一层树结构"); + } + } + if (checkDuplicate) { + Integer count = sysSceneExceptionRuleMapper.countConditionNo(data); + if (count != null && count > 0) { + throw new RuntimeException("条件编号已存在"); + } + } + } + + private void fillSiteAndBu(SysSceneExceptionRuleEntity data) { + if (StringUtils.isNotBlank(data.getBu()) && data.getBu().contains("_")) { + String[] arr = data.getBu().split("_"); + if (arr.length >= 2) { + data.setSite(arr[0]); + data.setBuNo(arr[1]); + } + } else if (StringUtils.isNotBlank(data.getBu())) { + data.setBuNo(data.getBu()); + } + if (StringUtils.isBlank(data.getSite()) || StringUtils.isBlank(data.getBuNo())) { + throw new RuntimeException("工厂和BU不能为空"); + } + } + + private String getCurrentUserName() { + Object principal = SecurityUtils.getSubject().getPrincipal(); + if (principal instanceof SysUserEntity) { + return ((SysUserEntity) principal).getUsername(); + } + return ""; + } +} diff --git a/src/main/resources/mapper/sys/SysSceneExceptionRuleMapper.xml b/src/main/resources/mapper/sys/SysSceneExceptionRuleMapper.xml new file mode 100644 index 0000000..92bb858 --- /dev/null +++ b/src/main/resources/mapper/sys/SysSceneExceptionRuleMapper.xml @@ -0,0 +1,184 @@ + + + + + + + + + INSERT INTO sys_scene_exception_rule + (site, bu_no, rule_code, rule_name, apply_page, apply_button, enable_flag, create_by, create_date, update_by, update_date) + VALUES + (#{site}, #{buNo}, #{ruleCode}, #{ruleName}, #{applyPage}, #{applyButton}, #{enableFlag}, #{createBy}, GETDATE(), #{updateBy}, GETDATE()) + + + + UPDATE sys_scene_exception_rule + SET + rule_name = #{ruleName}, + apply_page = #{applyPage}, + apply_button = #{applyButton}, + enable_flag = #{enableFlag}, + update_by = #{updateBy}, + update_date = GETDATE() + WHERE site = #{site} + AND bu_no = #{buNo} + AND rule_code = #{ruleCode} + + + + DELETE FROM sys_scene_exception_rule + WHERE site = #{site} + AND bu_no = #{buNo} + AND rule_code = #{ruleCode} + + + + DELETE FROM sys_scene_exception_rule_condition + WHERE site = #{site} + AND bu_no = #{buNo} + AND rule_code = #{ruleCode} + + + + + + + + + + INSERT INTO sys_scene_exception_rule_condition + (site, bu_no, rule_code, condition_no, parent_condition_no, relation_field, compare_symbol, + condition_param, force_type, warning_level, animation_effect, responsibility_dept, trigger_event, + create_by, create_date, update_by, update_date) + VALUES + (#{site}, #{buNo}, #{ruleCode}, #{conditionNo}, #{parentConditionNo}, #{relationField}, #{compareSymbol}, + #{conditionParam}, #{forceType}, #{warningLevel}, #{animationEffect}, #{responsibilityDept}, #{triggerEvent}, + #{createBy}, GETDATE(), #{updateBy}, GETDATE()) + + + + UPDATE sys_scene_exception_rule_condition + SET + parent_condition_no = #{parentConditionNo}, + relation_field = #{relationField}, + compare_symbol = #{compareSymbol}, + condition_param = #{conditionParam}, + force_type = #{forceType}, + warning_level = #{warningLevel}, + animation_effect = #{animationEffect}, + responsibility_dept = #{responsibilityDept}, + trigger_event = #{triggerEvent}, + update_by = #{updateBy}, + update_date = GETDATE() + WHERE site = #{site} + AND bu_no = #{buNo} + AND rule_code = #{ruleCode} + AND condition_no = #{conditionNo} + + + + DELETE FROM sys_scene_exception_rule_condition + WHERE site = #{site} + AND bu_no = #{buNo} + AND rule_code = #{ruleCode} + AND condition_no = #{conditionNo} + + + + DELETE FROM sys_scene_exception_rule_condition + WHERE site = #{site} + AND bu_no = #{buNo} + AND rule_code = #{ruleCode} + AND (condition_no = #{conditionNo} OR parent_condition_no = #{conditionNo}) + +