package org.dromara.payment.earlyWarn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;

import org.dromara.payment.earlyWarn.domain.LgEarlyWarn;
import org.dromara.payment.earlyWarn.domain.bo.LgEarlyWarnBo;
import org.dromara.payment.earlyWarn.domain.vo.LgEarlyWarnVo;
import org.dromara.payment.earlyWarn.mapper.LgEarlyWarnMapper;
import org.dromara.payment.earlyWarn.service.ILgEarlyWarnService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 风控预警设置Service业务层处理
 *
 * @author LionLi
 * @date 2025-06-10
 */
@RequiredArgsConstructor
@Service
public class LgEarlyWarnServiceImpl implements ILgEarlyWarnService {

    private final LgEarlyWarnMapper baseMapper;

    /**
     * 查询风控预警设置
     */
    @Override
    public LgEarlyWarnVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询风控预警设置列表
     */
    @Override
    public TableDataInfo<LgEarlyWarnVo> queryPageList(LgEarlyWarnBo bo, PageQuery pageQuery) {
        QueryWrapper<LgEarlyWarn> lqw = buildQueryWrapper(bo);
        Page<LgEarlyWarnVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询风控预警设置列表
     */
    @Override
    public List<LgEarlyWarnVo> queryList(LgEarlyWarnBo bo) {
        QueryWrapper<LgEarlyWarn> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<LgEarlyWarn> buildQueryWrapper(LgEarlyWarnBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<LgEarlyWarn> lqw = new QueryWrapper<>();
        lqw.eq(bo.getRiskType() != null, "risk_type", bo.getRiskType());
        lqw.eq(bo.getTriggerRule() != null, "trigger_rule", bo.getTriggerRule());
        lqw.eq(bo.getSingleAmount() != null, "single_amount", bo.getSingleAmount());
        lqw.eq(bo.getTotalAmountBatch() != null, "total_amount_batch", bo.getTotalAmountBatch());
        lqw.eq(bo.getSinglePaymentAmountStart() != null, "single_payment_amount_start", bo.getSinglePaymentAmountStart());
        lqw.eq(bo.getSinglePaymentAmountEnd() != null, "single_payment_amount_end", bo.getSinglePaymentAmountEnd());
        lqw.eq(bo.getProportionTotal() != null, "proportion_total", bo.getProportionTotal());
        lqw.eq(bo.getDispersion() != null, "dispersion", bo.getDispersion());
        lqw.eq(bo.getSingleYearAmount() != null, "single_year_amount", bo.getSingleYearAmount());
        lqw.eq(bo.getWaveAmountStart() != null, "wave_amount_start", bo.getWaveAmountStart());
        lqw.eq(bo.getWaveAmountEnd() != null, "wave_amount_end", bo.getWaveAmountEnd());
        lqw.eq(bo.getWaveAmount() != null, "wave_amount", bo.getWaveAmount());
        lqw.eq(bo.getSingleContinuousMonth() != null, "single_continuous_month", bo.getSingleContinuousMonth());
        lqw.eq(bo.getIssuingAge() != null, "issuing_age", bo.getIssuingAge());
        lqw.eq(bo.getHandleFrequency() != null, "handle_frequency", bo.getHandleFrequency());
        lqw.eq(bo.getHandleWay() != null, "handle_way", bo.getHandleWay());
        lqw.eq(StringUtils.isNotBlank(bo.getRiskReasons()), "risk_reasons", bo.getRiskReasons());
        lqw.eq(bo.getIsDelete() != null, "is_delete", bo.getIsDelete());
        lqw.eq(bo.getCreateUserId() != null, "create_user_id", bo.getCreateUserId());
        lqw.eq(bo.getUpdateUserId() != null, "update_user_id", bo.getUpdateUserId());
        lqw.eq(bo.getSingleMonthLimit() != null, "single_month_limit", bo.getSingleMonthLimit());
        lqw.eq(bo.getSingleMonthLimitAmount() != null, "single_month_limit_amount", bo.getSingleMonthLimitAmount());
        return lqw;
    }

    /**
     * 新增风控预警设置
     */
    @Override
    public Boolean insertByBo(LgEarlyWarnBo bo) {
        LgEarlyWarn add = MapstructUtils.convert(bo, LgEarlyWarn.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改风控预警设置
     */
    @Override
    public Boolean updateByBo(LgEarlyWarnBo bo) {
        LgEarlyWarn update = MapstructUtils.convert(bo, LgEarlyWarn.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LgEarlyWarn entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除风控预警设置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
