| | |
| | | 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.query.DataScreenConfigQuery; |
| | | 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; |
| | | |
| | |
| | | TbDataScreenConfig tbDataScreenConfig = this.lambdaQuery() |
| | | .eq(TbDataScreenConfig::getType, DataScreenConfigEnum.RISK_LEVEL) |
| | | .one(); |
| | | return Objects.nonNull(tbDataScreenConfig) ? BeanUtils.copyBean(tbDataScreenConfig, |
| | | DataScreenConfigVO.class) : new DataScreenConfigVO(); |
| | | 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); |
| | | return BeanUtils.copyBean(tbDataScreenConfig, DataScreenConfigVO.class); |
| | | 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(DataScreenConfigQuery query) { |
| | | public List<DataScreenConfigVO> getIndicatorsConfig(@NotNull DataScreenConfigEnum type) { |
| | | List<TbDataScreenConfig> list = this.lambdaQuery() |
| | | .eq(TbDataScreenConfig::getType, query.getType()).list(); |
| | | return BeanUtils.copyList(list, DataScreenConfigVO.class); |
| | | .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 DataScreenConfigVO editDataIndicatorsConfig(DataIndicatorsUpdDTO dto) { |
| | | //查询是否有该指标配置 |
| | | TbDataScreenConfig tbDataScreenConfig = this.lambdaQuery() |
| | | .eq(TbDataScreenConfig::getType, DataScreenConfigEnum.DATA_INDICATORS) |
| | | .eq(TbDataScreenConfig::getSubType, |
| | | dto.getSubType()).oneOpt().orElseGet(() -> BeanUtils.copyBean(dto, |
| | | TbDataScreenConfig.class)); |
| | | tbDataScreenConfig.setType(DataScreenConfigEnum.DATA_INDICATORS); |
| | | this.saveOrUpdate(tbDataScreenConfig); |
| | | return BeanUtils.copyBean(tbDataScreenConfig, DataScreenConfigVO.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 DataScreenConfigVO editFormalIndicatorsConfig(FormalIndicatorsUpdDTO dto) { |
| | | //查询是否有该指标配置 |
| | | 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); |
| | | this.saveOrUpdate(tbDataScreenConfig); |
| | | return BeanUtils.copyBean(tbDataScreenConfig, DataScreenConfigVO.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; |
| | | } |
| | | } |