package com.ruoyi.frequency.withdrawalconfig.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.DictConstant;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.frequency.withdrawalconfig.entity.WithdrawalConfig;
import com.ruoyi.frequency.withdrawalconfig.mapper.WithdrawalConfigMapper;
import com.ruoyi.frequency.withdrawalconfig.service.WithdrawalConfigService;
import com.ruoyi.utils.BusinessUtil;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 提现配置Service业务层处理
 *
 * @author liwenlong
 * @date 2023-07-20
 */
@Service
public class WithdrawalConfigServiceImpl extends ServiceImpl<WithdrawalConfigMapper, WithdrawalConfig>  implements WithdrawalConfigService
{
    @Autowired
    private WithdrawalConfigMapper withdrawalConfigMapper;

    /**
     * 查询提现配置
     *
     * @param id 提现配置主键
     * @return 提现配置
     */
    @Override
    public WithdrawalConfig selectWithdrawalConfigById(Long id)
    {
        return withdrawalConfigMapper.selectWithdrawalConfigById(id);
    }

    /**
     * 查询提现配置列表
     *
     * @param withdrawalConfig 提现配置
     * @return 提现配置
     */
    @Override
    public List<WithdrawalConfig> selectWithdrawalConfigList(WithdrawalConfig withdrawalConfig)
    {
        return withdrawalConfigMapper.selectWithdrawalConfigList(withdrawalConfig);
    }

    /**
     * 新增提现配置
     *
     * @param withdrawalConfig 提现配置
     * @return 结果
     */
    @Override
    public int insertWithdrawalConfig(WithdrawalConfig withdrawalConfig)
    {
        withdrawalConfig.setCreateTime(DateUtils.getNowDate());
        return withdrawalConfigMapper.insertWithdrawalConfig(withdrawalConfig);
    }

    /**
     * 修改提现配置
     *
     * @param withdrawalConfig 提现配置
     * @return 结果
     */
    @Override
    public int updateWithdrawalConfig(WithdrawalConfig withdrawalConfig)
    {
        withdrawalConfig.setUpdateTime(DateUtils.getNowDate());
        return withdrawalConfigMapper.updateWithdrawalConfig(withdrawalConfig);
    }

    /**
     * 批量删除提现配置
     *
     * @param ids 需要删除的提现配置主键
     * @return 结果
     */
    @Override
    public int deleteWithdrawalConfigByIds(Long[] ids)
    {
        return withdrawalConfigMapper.deleteWithdrawalConfigByIds(ids);
    }

    /**
     * 删除提现配置信息
     *
     * @param id 提现配置主键
     * @return 结果
     */
    @Override
    public int deleteWithdrawalConfigById(Long id)
    {
        return withdrawalConfigMapper.deleteWithdrawalConfigById(id);
    }


    @Autowired
    private BusinessUtil businessUtil;

    @Override
    public void withdrawalJudge(UserVo userVo, BigDecimal amount) {

        SysDictData dict = businessUtil.getDict(DictConstant.提现日期);

        if (dict == null || ObjectUtil.isEmpty(dict.getDictValue())){
            return;
        }

        String[] split = dict.getDictValue().split("~");

        //每月可以提现开始时间(小于等于0表示不限制)
        Integer monthRangeMin = Integer.valueOf(split[0]);

        //每月可以提现结束时间(小于等于0表示不限制)
        Integer monthRangeMax =  Integer.valueOf(split[1]);

        if (monthRangeMin != null && monthRangeMax != null && monthRangeMin > 0  && monthRangeMax > 0) {
            //获取日期
            String minDay =  monthRangeMin.toString().length()==1?"0"+monthRangeMin:monthRangeMin.toString();
            //转换时间
            Date minDate = DateUtil.parse(DateUtil.format(new Date(),"yyyy-MM-"+minDay), "yyyy-MM-dd");

            //获取日期
            String maxDay =  monthRangeMax.toString().length()==1?"0"+monthRangeMax:monthRangeMax.toString();
            //转换时间
            Date maxDate = DateUtil.parse(DateUtil.format(new Date(),"yyyy-MM-"+maxDay), "yyyy-MM-dd");

            if (DateUtil.beginOfDay(minDate).getTime()> System.currentTimeMillis() || DateUtil.endOfDay(maxDate).getTime() < System.currentTimeMillis()){
                throw new CustomException("提现只能在每月的" +monthRangeMin+ "~"+monthRangeMax+"号申请");
            }
        }
    }
}
