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.Hazard;
import com.ruoyi.doubledefense.dto.RiskEventDTO;
import com.ruoyi.doubledefense.dto.res.RiskEventResultDTO;
import com.ruoyi.doubledefense.dtoGov.RiskEventDTOGov;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.doubledefense.mapper.RiskEventMapper;
import com.ruoyi.doubledefense.domain.RiskEvent;
import com.ruoyi.doubledefense.service.IRiskEventService;
import org.springframework.util.CollectionUtils;

/**
 * 风险事件Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-03
 */
@Service
public class RiskEventServiceImpl implements IRiskEventService
{
    @Autowired
    private RiskEventMapper riskEventMapper;

    /**
     * 重预防汇聚计算节点
     * 查询风险事件
     *
     * @param riskEvent
     * @return
     */
    @Override
    public PageInfo<RiskEventDTOGov> govSelectRiskEventList(RiskEventDTOGov riskEvent) {
        PageHelper.startPage(riskEvent.getPageNo(),riskEvent.getPageSize());
        List<RiskEventDTOGov> riskEventDTOGovList = riskEventMapper.govSelectRiskEventList(riskEvent);

        PageInfo<RiskEventDTOGov> pageInfo = new PageInfo<>(riskEventDTOGovList);
        return pageInfo;
    }

    /**
     * 查询风险事件
     *
     * @param id 风险事件主键
     * @return 风险事件
     */
    @Override
    public RiskEventResultDTO selectRiskEventById(String id)
    {
        RiskEventDTO riskEventDTO = new RiskEventDTO();
        riskEventDTO.setId(id);
        List<RiskEventResultDTO> resultDTOS = riskEventMapper.selectRiskEventList(riskEventDTO);
        if(CollectionUtils.isEmpty(resultDTOS)){
            return null;
        }
        return resultDTOS.get(0);
    }

    /**
     * 查询风险事件列表
     *
     * @param riskEventDTO 风险事件
     * @return 风险事件
     */
    @Override
    public List<RiskEventResultDTO> selectRiskEventList(RiskEventDTO riskEventDTO)
    {
        return riskEventMapper.selectRiskEventList(riskEventDTO);
    }

    /**
     * 新增风险事件
     *
     * @param riskEventDTO 风险事件
     * @return 结果
     */
    @Override
    public int insertRiskEvent(RiskEventDTO riskEventDTO)
    {
        RiskEvent riskEvent = new RiskEvent();
        BeanUtils.copyProperties(riskEventDTO,riskEvent);
        riskEvent.setId(UUID.randomUUID().toString());
        riskEvent.setCreateDefault();
        return riskEventMapper.insert(riskEvent);
    }

    /**
     * 修改风险事件
     *
     * @param riskEventDTO 风险事件
     * @return 结果
     */
    @Override
    public int updateRiskEvent(RiskEventDTO riskEventDTO)
    {
        RiskEvent riskEvent = new RiskEvent();
        BeanUtils.copyProperties(riskEventDTO,riskEvent);
        riskEvent.setUpdateDefault();
        return riskEventMapper.updateById(riskEvent);
    }

    /**
     * 批量删除风险事件
     *
     * @param ids 需要删除的风险事件主键
     * @return 结果
     */
    @Override
    public int deleteRiskEventByIds(String[] ids)
    {
        RiskEvent riskEvent = new RiskEvent();
        riskEvent.setUpdateDefault();
        riskEvent.setIsDel(DeletedEnum.INVALID.getValue());
        QueryWrapper<RiskEvent> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",ids);
        return riskEventMapper.update(riskEvent,queryWrapper);
    }

}
