package com.ruoyi.doubledefense.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.DeletedEnum;
import com.ruoyi.doubledefense.domain.RiskEvent;
import com.ruoyi.doubledefense.dto.RiskMeasureDTO;
import com.ruoyi.doubledefense.dto.res.RiskMeasureResultDTO;
import com.ruoyi.doubledefense.dtoGov.RiskMeasureDTOGov;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.doubledefense.mapper.RiskMeasureMapper;
import com.ruoyi.doubledefense.domain.RiskMeasure;
import com.ruoyi.doubledefense.service.IRiskMeasureService;
import org.springframework.util.CollectionUtils;

/**
 * 风险管控措施Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-03
 */
@Service
public class RiskMeasureServiceImpl implements IRiskMeasureService {
    @Autowired
    private RiskMeasureMapper riskMeasureMapper;


    /**
     * 管控措施
     * 重预防汇聚计算节点
     *
     * @param riskMeasureDTOGov
     * @return
     */
    @Override
    public PageInfo<RiskMeasureDTOGov> govSelectRiskMeasureList(RiskMeasureDTOGov riskMeasureDTOGov) {
        PageHelper.startPage(riskMeasureDTOGov.getPageNo(), riskMeasureDTOGov.getPageSize());
        List<RiskMeasureDTOGov> riskMeasureDTOGovList = riskMeasureMapper.govSelectRiskMeasureList(riskMeasureDTOGov);
        PageInfo<RiskMeasureDTOGov> riskMeasureDTOGovPageInfo = new PageInfo<>(riskMeasureDTOGovList);
        return riskMeasureDTOGovPageInfo;
    }

    /**
     * 查询风险管控措施
     *
     * @param id 风险管控措施主键
     * @return 风险管控措施
     */
    @Override
    public RiskMeasureResultDTO selectRiskMeasureById(String id) {
        RiskMeasureDTO riskMeasureDTO = new RiskMeasureDTO();
        riskMeasureDTO.setId(id);
        List<RiskMeasureResultDTO> riskMeasureResultDTOS = selectRiskMeasureList(riskMeasureDTO);
        if (CollectionUtils.isEmpty(riskMeasureResultDTOS)) {
            return null;
        }
        return riskMeasureResultDTOS.get(0);
    }

    /**
     * 查询风险管控措施列表
     *
     * @param riskMeasure 风险管控措施
     * @return 风险管控措施
     */
    @Override
    public List<RiskMeasureResultDTO> selectRiskMeasureList(RiskMeasureDTO riskMeasure) {
        return riskMeasureMapper.selectRiskMeasureList(riskMeasure);
    }

    /**
     * 新增风险管控措施
     *
     * @param riskMeasureDTO 风险管控措施
     * @return 结果
     */
    @Override
    public int insertRiskMeasure(RiskMeasureDTO riskMeasureDTO) {
        RiskMeasure riskMeasure = new RiskMeasure();
        BeanUtils.copyProperties(riskMeasureDTO, riskMeasure);
        riskMeasure.setId(UUID.randomUUID().toString());
        riskMeasure.setCreateDefault();
        return riskMeasureMapper.insert(riskMeasure);
    }

    /**
     * 修改风险管控措施
     *
     * @param riskMeasureDTO 风险管控措施
     * @return 结果
     */
    @Override
    public int updateRiskMeasure(RiskMeasureDTO riskMeasureDTO) {
        RiskMeasure riskMeasure = new RiskMeasure();
        BeanUtils.copyProperties(riskMeasureDTO, riskMeasure);
        riskMeasure.setUpdateDefault();
        return riskMeasureMapper.updateById(riskMeasure);
    }

    /**
     * 批量删除风险管控措施
     *
     * @param ids 需要删除的风险管控措施主键
     * @return 结果
     */
    @Override
    public int deleteRiskMeasureByIds(String[] ids) {
        RiskMeasure riskMeasure = new RiskMeasure();
        riskMeasure.setUpdateDefault();
        riskMeasure.setIsDel(DeletedEnum.INVALID.getValue());
        QueryWrapper<RiskMeasure> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        return riskMeasureMapper.update(riskMeasure, queryWrapper);
    }

}
