From d68c61517a3a6d4ec4d47844c21bdc143f99cbab Mon Sep 17 00:00:00 2001
From: mitao <2763622819@qq.com>
Date: 星期三, 17 四月 2024 22:58:05 +0800
Subject: [PATCH] 大屏接口

---
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TbDataScreenConfigServiceImpl.java |  178 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 177 insertions(+), 1 deletions(-)

diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TbDataScreenConfigServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TbDataScreenConfigServiceImpl.java
index 87624d8..485f241 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TbDataScreenConfigServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TbDataScreenConfigServiceImpl.java
@@ -1,10 +1,31 @@
 package com.ruoyi.system.service.impl;
 
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONArray;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.enums.DataScreenConfigEnum;
+import com.ruoyi.common.exception.ServiceException;
+import com.ruoyi.common.utils.BeanUtils;
+import com.ruoyi.common.utils.CalculateUtil;
+import com.ruoyi.common.utils.Checker;
+import com.ruoyi.common.utils.CollUtils;
+import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.system.domain.TbDataScreenConfig;
+import com.ruoyi.system.dto.CalculateItemDTO;
+import com.ruoyi.system.dto.update.DataIndicatorsUpdDTO;
+import com.ruoyi.system.dto.update.FormalIndicatorsUpdDTO;
+import com.ruoyi.system.dto.update.RiskLevelUpdDTO;
 import com.ruoyi.system.mapper.TbDataScreenConfigMapper;
 import com.ruoyi.system.service.TbDataScreenConfigService;
+import com.ruoyi.system.vo.DataScreenConfigVO;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import javax.validation.constraints.NotNull;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 /**
  * <p>
@@ -15,6 +36,161 @@
  * @since 2024-03-13
  */
 @Service
-public class TbDataScreenConfigServiceImpl extends ServiceImpl<TbDataScreenConfigMapper, TbDataScreenConfig> implements TbDataScreenConfigService {
+public class TbDataScreenConfigServiceImpl extends
+        ServiceImpl<TbDataScreenConfigMapper, TbDataScreenConfig> implements
+        TbDataScreenConfigService {
 
+    @Override
+    public DataScreenConfigVO getRiskLevel() {
+        TbDataScreenConfig tbDataScreenConfig = this.lambdaQuery()
+                .eq(TbDataScreenConfig::getType, DataScreenConfigEnum.RISK_LEVEL)
+                .one();
+        if (Objects.isNull(tbDataScreenConfig)) {
+            return new DataScreenConfigVO();
+        }
+        DataScreenConfigVO vo = BeanUtils.copyBean(tbDataScreenConfig,
+                DataScreenConfigVO.class);
+        List<CalculateItemDTO> scoreCalculateList = JSONArray.parseArray(
+                tbDataScreenConfig.getCalculateFormula(),
+                CalculateItemDTO.class);
+        vo.setScoreCalculateList(scoreCalculateList);
+        return vo;
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public DataScreenConfigVO editRiskLevel(RiskLevelUpdDTO dto) {
+       /* if (dto.areRiskLevelsDistinct()) {
+            throw new ServiceException("得分范围不能重叠");
+        }*/
+        //查询是否有该指标配置
+        TbDataScreenConfig tbDataScreenConfig = this.lambdaQuery()
+                .eq(TbDataScreenConfig::getType, DataScreenConfigEnum.RISK_LEVEL)
+                .oneOpt().orElseGet(() -> BeanUtils.copyBean(dto,
+                        TbDataScreenConfig.class));
+        List<CalculateItemDTO> calculateItemList = dto.getCalculateItemList();
+        validateCalculateItemList(calculateItemList);
+        String numberCalculateFormulaJsonStr = JSON.toJSONString(calculateItemList);
+        tbDataScreenConfig.setCalculateFormula(numberCalculateFormulaJsonStr);
+        tbDataScreenConfig.setType(DataScreenConfigEnum.RISK_LEVEL);
+        this.saveOrUpdate(tbDataScreenConfig);
+        DataScreenConfigVO dataScreenConfigVO = BeanUtils.copyBean(tbDataScreenConfig,
+                DataScreenConfigVO.class);
+        dataScreenConfigVO.setRateCalculateList(calculateItemList);
+        return dataScreenConfigVO;
+    }
+
+    private void validateCalculateItemList(List<CalculateItemDTO> calculateItemList) {
+        if (CollUtils.isEmpty(calculateItemList)) {
+            throw new ServiceException("计算公式不能为空");
+        }
+        CollUtils.check(calculateItemList, new Checker<CalculateItemDTO>() {
+            @Override
+            public void check(CalculateItemDTO data) {
+                if (Objects.isNull(data.getValueType())) {
+                    throw new ServiceException("基础数据配置计算规则值类型不能为空");
+                }
+                if (StringUtils.isBlank(data.getContent())) {
+                    throw new ServiceException("基础数据配置计算规则值内容不能为空");
+                }
+            }
+        });
+        boolean result = hasConsecutiveDuplicates(calculateItemList);
+        if (result) {
+            throw new ServiceException("计算表达式有误,请修改");
+        }
+        //参数校验
+        String numberCalculateFormulaStr = calculateItemList.stream()
+                .map(CalculateItemDTO::getContent)
+                .collect(Collectors.joining());
+        //校验表达式是否正确
+        try {
+            CalculateUtil.calculate(numberCalculateFormulaStr);
+        } catch (Exception e) {
+            throw new ServiceException("计算表达式有误,请修改");
+        }
+    }
+
+    //表达式校验
+    public boolean hasConsecutiveDuplicates(List<CalculateItemDTO> calculateItemList) {
+        return IntStream.range(1, calculateItemList.size())
+                .anyMatch(i -> calculateItemList.get(i - 1).getValueType()
+                        .equals(calculateItemList.get(i).getValueType()));
+    }
+
+    @Override
+    public List<DataScreenConfigVO> getIndicatorsConfig(@NotNull DataScreenConfigEnum type) {
+        List<TbDataScreenConfig> list = this.lambdaQuery()
+                .eq(TbDataScreenConfig::getType, type).list();
+        return list.stream().map(item -> {
+            DataScreenConfigVO vo = BeanUtils.copyBean(item,
+                    DataScreenConfigVO.class);
+            List<CalculateItemDTO> scoreCalculateList = JSONArray.parseArray(
+                    item.getCalculateFormula(),
+                    CalculateItemDTO.class);
+            vo.setScoreCalculateList(scoreCalculateList);
+            List<CalculateItemDTO> rateCalculateList = JSONArray.parseArray(
+                    item.getCalculateRateFormula(),
+                    CalculateItemDTO.class);
+            vo.setRateCalculateList(rateCalculateList);
+            return vo;
+        }).collect(Collectors.toList());
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public List<DataScreenConfigVO> editDataIndicatorsConfig(List<DataIndicatorsUpdDTO> dtoList) {
+        List<DataScreenConfigVO> vosList = new ArrayList<>();
+        for (DataIndicatorsUpdDTO dto : dtoList) {
+            //查询是否有该指标配置
+            TbDataScreenConfig tbDataScreenConfig = this.lambdaQuery()
+                    .eq(TbDataScreenConfig::getType, DataScreenConfigEnum.DATA_INDICATORS)
+                    .eq(TbDataScreenConfig::getSubType,
+                            dto.getSubType()).oneOpt().orElseGet(() -> BeanUtils.copyBean(dto,
+                            TbDataScreenConfig.class));
+            List<CalculateItemDTO> scoreCalculateList = dto.getScoreCalculateList();
+            List<CalculateItemDTO> rateCalculateList = dto.getRateCalculateList();
+            validateCalculateItemList(scoreCalculateList);
+            validateCalculateItemList(rateCalculateList);
+            String scoreCalculateFormulaJsonStr = JSON.toJSONString(scoreCalculateList);
+            tbDataScreenConfig.setCalculateFormula(scoreCalculateFormulaJsonStr);
+            String rateCalculateFormulaJsonStr = JSON.toJSONString(rateCalculateList);
+            tbDataScreenConfig.setCalculateRateFormula(rateCalculateFormulaJsonStr);
+            tbDataScreenConfig.setType(DataScreenConfigEnum.DATA_INDICATORS);
+            this.saveOrUpdate(tbDataScreenConfig);
+            DataScreenConfigVO dataScreenConfigVO = BeanUtils.copyBean(tbDataScreenConfig,
+                    DataScreenConfigVO.class);
+            dataScreenConfigVO.setScoreCalculateList(scoreCalculateList);
+            dataScreenConfigVO.setRateCalculateList(rateCalculateList);
+            vosList.add(dataScreenConfigVO);
+        }
+        return vosList;
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public List<DataScreenConfigVO> editFormalIndicatorsConfig(
+            List<FormalIndicatorsUpdDTO> dtoList) {
+        List<DataScreenConfigVO> vosList = new ArrayList<>();
+        for (FormalIndicatorsUpdDTO dto : dtoList) {
+            //查询是否有该指标配置
+            TbDataScreenConfig tbDataScreenConfig = this.lambdaQuery()
+                    .eq(TbDataScreenConfig::getType, DataScreenConfigEnum.FORMAL_INDICATORS)
+                    .eq(TbDataScreenConfig::getSubType,
+                            dto.getSubType()).oneOpt().orElseGet(() -> BeanUtils.copyBean(dto,
+                            TbDataScreenConfig.class));
+            tbDataScreenConfig.setType(DataScreenConfigEnum.FORMAL_INDICATORS);
+            tbDataScreenConfig.setMaxScore(dto.getMaxScore());
+            List<CalculateItemDTO> calculateItemList = dto.getCalculateItemList();
+            validateCalculateItemList(calculateItemList);
+            String calculateFormulaJsonStr = JSON.toJSONString(calculateItemList);
+            tbDataScreenConfig.setCalculateFormula(calculateFormulaJsonStr);
+            this.saveOrUpdate(tbDataScreenConfig);
+            DataScreenConfigVO dataScreenConfigVO = BeanUtils.copyBean(tbDataScreenConfig,
+                    DataScreenConfigVO.class);
+            dataScreenConfigVO.setScoreCalculateList(calculateItemList);
+            vosList.add(dataScreenConfigVO);
+        }
+        return vosList;
+    }
 }

--
Gitblit v1.7.1