|  | @@ -0,0 +1,196 @@
 | 
	
		
			
				|  |  | +package com.xunmei.core.safetyindex.service.impl;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import cn.hutool.core.util.ObjectUtil;
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.domain.safetyindex.domain.CoreSafetyDict;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.domain.safetyindex.domain.CoreSafetyIndexCalculateRule;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.domain.safetyindex.domain.CoreSafetyIndexCalculateRuleItem;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.domain.safetyindex.dto.*;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.domain.safetyindex.vo.CoreSafetyIndexCalculateRuleDetailVo;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.domain.safetyindex.vo.CoreSafetyIndexCalculateRuleItemVo;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.domain.safetyindex.vo.CoreSafetyIndexCalculateRulePageVo;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.enums.OrgTypeEnum;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.enums.safetyindex.CalculateTypeEnums;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.exception.SystemException;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.util.BeanHelper;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.web.page.TableDataInfo;
 | 
	
		
			
				|  |  | +import com.xunmei.common.security.utils.SecurityUtils;
 | 
	
		
			
				|  |  | +import com.xunmei.core.safetyindex.mapper.CoreSafetyIndexCalculateRuleMapper;
 | 
	
		
			
				|  |  | +import com.xunmei.core.safetyindex.service.ICoreSafetyDictService;
 | 
	
		
			
				|  |  | +import com.xunmei.core.safetyindex.service.ICoreSafetyIndexCalculateRuleItemService;
 | 
	
		
			
				|  |  | +import com.xunmei.core.safetyindex.service.ICoreSafetyIndexCalculateRuleService;
 | 
	
		
			
				|  |  | +import org.springframework.beans.BeanUtils;
 | 
	
		
			
				|  |  | +import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | +import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | +import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import java.util.Arrays;
 | 
	
		
			
				|  |  | +import java.util.Date;
 | 
	
		
			
				|  |  | +import java.util.List;
 | 
	
		
			
				|  |  | +import java.util.Map;
 | 
	
		
			
				|  |  | +import java.util.stream.Collectors;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +/**
 | 
	
		
			
				|  |  | + * 安全指标配置Service业务层处理
 | 
	
		
			
				|  |  | + *
 | 
	
		
			
				|  |  | + * @author xunmei
 | 
	
		
			
				|  |  | + * @date 2023-10-09
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +@Service
 | 
	
		
			
				|  |  | +public class CoreSafetyIndexCalculateRuleServiceImpl extends ServiceImpl<CoreSafetyIndexCalculateRuleMapper, CoreSafetyIndexCalculateRule> implements ICoreSafetyIndexCalculateRuleService {
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private CoreSafetyIndexCalculateRuleMapper coreSafetyIndexCalculateRuleMapper;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private ICoreSafetyDictService safetyDictService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private ICoreSafetyIndexCalculateRuleItemService ruleItemService;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public TableDataInfo<CoreSafetyIndexCalculateRulePageVo> selectPage(CoreSafetyIndexCalculateRulePageDto request) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //获取数据
 | 
	
		
			
				|  |  | +        Page<CoreSafetyIndexCalculateRulePageVo> page = coreSafetyIndexCalculateRuleMapper.selectPageData(request.getPageRequest(), request);
 | 
	
		
			
				|  |  | +        List<CoreSafetyIndexCalculateRulePageVo> records = page.getRecords();
 | 
	
		
			
				|  |  | +        if (ObjectUtil.isEmpty(records)) {
 | 
	
		
			
				|  |  | +            return TableDataInfo.build(page);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        Map<String, String> map =getCoreSafetyDictMap();
 | 
	
		
			
				|  |  | +        for (CoreSafetyIndexCalculateRulePageVo record : page.getRecords()) {
 | 
	
		
			
				|  |  | +            //设置组织名称
 | 
	
		
			
				|  |  | +            record.setOrgTypeText(OrgTypeEnum.getName(record.getOrgType()));
 | 
	
		
			
				|  |  | +            record.setTypeCodeText(map.get(record.getTypeCode()));
 | 
	
		
			
				|  |  | +            record.setProjectCodeText(map.get(record.getProjectCode()));
 | 
	
		
			
				|  |  | +            record.setPointCodeText(map.get(record.getPointCode()));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //抓换为TableDataInfo适配前端
 | 
	
		
			
				|  |  | +        return TableDataInfo.build(page);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private Map<String, String>  getCoreSafetyDictMap(){
 | 
	
		
			
				|  |  | +        List<CoreSafetyDict> dictList = safetyDictService.selectCoreSafetyDictList(null);
 | 
	
		
			
				|  |  | +        return dictList.stream().collect(Collectors.toMap(CoreSafetyDict::getCode, CoreSafetyDict::getName));
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 查询安全指标配置
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param id 安全指标配置主键
 | 
	
		
			
				|  |  | +     * @return 安全指标配置
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public CoreSafetyIndexCalculateRuleDetailVo selectCoreSafetyIndexCalculateRuleById(Long id) {
 | 
	
		
			
				|  |  | +        CoreSafetyIndexCalculateRule calculateRule = coreSafetyIndexCalculateRuleMapper.selectById(id);
 | 
	
		
			
				|  |  | +        if (calculateRule==null){
 | 
	
		
			
				|  |  | +            throw new SystemException("安全指标配置不存在");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        CoreSafetyIndexCalculateRuleDetailVo record = new CoreSafetyIndexCalculateRuleDetailVo();
 | 
	
		
			
				|  |  | +        BeanUtils.copyProperties(calculateRule, record);
 | 
	
		
			
				|  |  | +        Map<String, String> map = getCoreSafetyDictMap();
 | 
	
		
			
				|  |  | +        record.setOrgTypeText(OrgTypeEnum.getName(record.getOrgType()));
 | 
	
		
			
				|  |  | +        record.setTypeCodeText(map.get(record.getTypeCode()));
 | 
	
		
			
				|  |  | +        record.setProjectCodeText(map.get(record.getProjectCode()));
 | 
	
		
			
				|  |  | +        record.setPointCodeText(map.get(record.getPointCode()));
 | 
	
		
			
				|  |  | +        List<CoreSafetyIndexCalculateRuleItem> list = ruleItemService.list(new LambdaQueryWrapper<CoreSafetyIndexCalculateRuleItem>()
 | 
	
		
			
				|  |  | +                .eq(CoreSafetyIndexCalculateRuleItem::getRuleId, id));
 | 
	
		
			
				|  |  | +        List<CoreSafetyIndexCalculateRuleItemVo> properties = BeanHelper.copyProperties(list, CoreSafetyIndexCalculateRuleItemVo.class);
 | 
	
		
			
				|  |  | +        properties.forEach(r->{
 | 
	
		
			
				|  |  | +            r.setCalculateTypeText(CalculateTypeEnums.getName(r.getCalculateType()));
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +        record.setItemList(properties);
 | 
	
		
			
				|  |  | +        return record;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 查询安全指标配置列表
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param coreSafetyIndexCalculateRule 安全指标配置
 | 
	
		
			
				|  |  | +     * @return 安全指标配置
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public List<CoreSafetyIndexCalculateRule> selectCoreSafetyIndexCalculateRuleList(CoreSafetyIndexCalculateRule coreSafetyIndexCalculateRule) {
 | 
	
		
			
				|  |  | +        return coreSafetyIndexCalculateRuleMapper.selectList(new QueryWrapper<>(coreSafetyIndexCalculateRule));
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 新增安全指标配置
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param coreSafetyIndexCalculateRule 安全指标配置
 | 
	
		
			
				|  |  | +     * @return 结果
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  | +    public int insertCoreSafetyIndexCalculateRule(CoreSafetyIndexCalculateRuleInsertDto coreSafetyIndexCalculateRule) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        CoreSafetyIndexCalculateRule calculateRule = new CoreSafetyIndexCalculateRule();
 | 
	
		
			
				|  |  | +        BeanUtils.copyProperties(coreSafetyIndexCalculateRule, calculateRule);
 | 
	
		
			
				|  |  | +        calculateRule.setCreateBy(SecurityUtils.getUserId().toString());
 | 
	
		
			
				|  |  | +        calculateRule.setUpdateBy(SecurityUtils.getUserId().toString());
 | 
	
		
			
				|  |  | +        calculateRule.setCreateTime(new Date());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        List<CoreSafetyIndexCalculateRuleItemInsertDto> itemList = coreSafetyIndexCalculateRule.getItemList();
 | 
	
		
			
				|  |  | +        List<CoreSafetyIndexCalculateRuleItem> properties = BeanHelper.copyProperties(itemList, CoreSafetyIndexCalculateRuleItem.class);
 | 
	
		
			
				|  |  | +        properties.forEach(r->{
 | 
	
		
			
				|  |  | +            r.setCreateBy(SecurityUtils.getUserId().toString());
 | 
	
		
			
				|  |  | +            r.setUpdateBy(SecurityUtils.getUserId().toString());
 | 
	
		
			
				|  |  | +            r.setCreateTime(new Date());
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +        ruleItemService.saveBatch(properties);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return coreSafetyIndexCalculateRuleMapper.insert(calculateRule);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 修改安全指标配置
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param request 安全指标配置
 | 
	
		
			
				|  |  | +     * @return 结果
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  | +    public int updateCoreSafetyIndexCalculateRule(CoreSafetyIndexCalculateRuleUpdateDto request) {
 | 
	
		
			
				|  |  | +        CoreSafetyIndexCalculateRule coreSafetyIndexCalculateRule = new CoreSafetyIndexCalculateRule();
 | 
	
		
			
				|  |  | +        BeanUtils.copyProperties(request, coreSafetyIndexCalculateRule);
 | 
	
		
			
				|  |  | +        coreSafetyIndexCalculateRule.setUpdateBy(SecurityUtils.getUserId().toString());
 | 
	
		
			
				|  |  | +        coreSafetyIndexCalculateRule.setUpdateTime(new Date());
 | 
	
		
			
				|  |  | +        List<CoreSafetyIndexCalculateRuleItemUpdateDto> itemList = request.getItemList();
 | 
	
		
			
				|  |  | +        List<CoreSafetyIndexCalculateRuleItem> properties = BeanHelper.copyProperties(itemList, CoreSafetyIndexCalculateRuleItem.class);
 | 
	
		
			
				|  |  | +        properties.forEach(r->{
 | 
	
		
			
				|  |  | +            r.setUpdateBy(SecurityUtils.getUserId().toString());
 | 
	
		
			
				|  |  | +            r.setUpdateTime(new Date());
 | 
	
		
			
				|  |  | +            if (r.getId()==null){
 | 
	
		
			
				|  |  | +                r.setCreateBy(SecurityUtils.getUserId().toString());
 | 
	
		
			
				|  |  | +                r.setCreateTime(new Date());
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +        ruleItemService.saveOrUpdateBatch(properties);
 | 
	
		
			
				|  |  | +        return coreSafetyIndexCalculateRuleMapper.updateById(coreSafetyIndexCalculateRule);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 批量删除安全指标配置
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param ids 需要删除的安全指标配置主键
 | 
	
		
			
				|  |  | +     * @return 结果
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public int deleteCoreSafetyIndexCalculateRuleByIds(Long[] ids) {
 | 
	
		
			
				|  |  | +        return coreSafetyIndexCalculateRuleMapper.deleteBatchIds(Arrays.asList((ids)));
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 删除安全指标配置信息
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param id 安全指标配置主键
 | 
	
		
			
				|  |  | +     * @return 结果
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public int deleteCoreSafetyIndexCalculateRuleById(Long id) {
 | 
	
		
			
				|  |  | +        return coreSafetyIndexCalculateRuleMapper.deleteById(id);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |