package com.ruoyi.ss.risk.service.impl;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.risk.domain.enums.RiskType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ss.risk.mapper.RiskMapper;
import com.ruoyi.ss.risk.domain.Risk;
import com.ruoyi.ss.risk.domain.RiskVO;
import com.ruoyi.ss.risk.domain.RiskQuery;
import com.ruoyi.ss.risk.service.RiskService;

/**
 * 风控Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-18
 */
@Service
public class RiskServiceImpl implements RiskService
{
    @Autowired
    private RiskMapper riskMapper;

    /**
     * 查询风控
     *
     * @param riskId 风控主键
     * @return 风控
     */
    @Override
    public RiskVO selectRiskByRiskId(Long riskId)
    {
        return riskMapper.selectRiskByRiskId(riskId);
    }

    /**
     * 查询风控列表
     *
     * @param risk 风控
     * @return 风控
     */
    @Override
    public List<RiskVO> selectRiskList(RiskQuery risk)
    {
        return riskMapper.selectRiskList(risk);
    }

    /**
     * 新增风控
     *
     * @param risk 风控
     * @return 结果
     */
    @Override
    public int insertRisk(Risk risk)
    {
        risk.setCreateTime(DateUtils.getNowDate());
        return riskMapper.insertRisk(risk);
    }

    /**
     * 修改风控
     *
     * @param risk 风控
     * @return 结果
     */
    @Override
    public int updateRisk(Risk risk)
    {
        return riskMapper.updateRisk(risk);
    }

    /**
     * 批量删除风控
     *
     * @param riskIds 需要删除的风控主键
     * @return 结果
     */
    @Override
    public int deleteRiskByRiskIds(Long[] riskIds)
    {
        return riskMapper.deleteRiskByRiskIds(riskIds);
    }

    /**
     * 删除风控信息
     *
     * @param riskId 风控主键
     * @return 结果
     */
    @Override
    public int deleteRiskByRiskId(Long riskId)
    {
        return riskMapper.deleteRiskByRiskId(riskId);
    }

    @Override
    public List<RiskVO> selectNotFinishedList(RiskQuery query) {
        query.setIsFinished(false);
        return this.selectRiskList(query);
    }

    @Override
    public int batchSetRealName(List<Long> riskIds, Long realNameId) {
        if (CollectionUtils.isEmptyElement(riskIds)) {
            return 0;
        }
        return riskMapper.batchSetRealName(riskIds, realNameId);
    }

    @Override
    public int limitWithdraw(Long userId, String reason) {
        Risk risk = new Risk();
        risk.setUserId(userId);
        risk.setType(Collections.singletonList(RiskType.WITHDRAW.getType()));
        risk.setReason(reason);
        risk.setEndTime(LocalDateTime.now().plusDays(1));
        risk.setUnsealSelf(true);
        return this.insertRisk(risk);
    }

    @Override
    public int finish(Long riskId) {
        if (riskId == null) {
            return 0;
        }
        Risk data = new Risk();
        data.setRiskId(riskId);
        data.setEndTime(LocalDateTime.now());
        return riskMapper.updateRisk(data);
    }
}
