package com.ruoyi.project.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.project.business.domain.TbCurrency;
import com.ruoyi.project.business.domain.vo.currency.CurrencyReq;
import com.ruoyi.project.business.enums.DelFlagEnum;
import com.ruoyi.project.business.mapper.TbCurrencyMapper;
import com.ruoyi.project.business.service.ITbCurrencyService;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * 币种汇率Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-14
 */
@Service
public class TbCurrencyServiceImpl extends ServiceImpl<TbCurrencyMapper, TbCurrency> implements ITbCurrencyService {

    /**
     * 查询币种汇率列表
     *
     * @param currencyReq 币种汇率
     * @return 币种汇率
     */
    @Override
    public List<TbCurrency> selectTbCurrencyList(CurrencyReq currencyReq) {
        //// 构建查询条件
        //Wrapper<TbCurrency> wrapper = Wrappers.<TbCurrency>lambdaQuery()
        //        .like(StringUtils.isNotBlank(currencyReq.getCurrencyName()), TbCurrency::getCurrencyName, currencyReq.getCurrencyName())
        //        .eq(currencyReq.getExchangeRate() != null, TbCurrency::getExchangeRate, currencyReq.getExchangeRate())
        //        .between(currencyReq.getBeginTime() != null && currencyReq.getEndTime() != null, TbCurrency::getBeginTime, currencyReq.getBeginTime(), currencyReq.getEndTime())
        //        .like(StringUtils.isNotBlank(currencyReq.getRemark()), TbCurrency::getRemark, currencyReq.getRemark());
        //wrapper.getExpression()
        //        .add(Wrappers.<SysUser>lambdaQuery()
        //                .like(StringUtils.isNotBlank(currencyReq.getCreateUserNick()), SysUser::getNickName, currencyReq.getCreateUserNick()));
        //
        //return baseMapper.selectTbCurrencyList2(wrapper);
        return baseMapper.selectTbCurrencyList(currencyReq);

    }

    @Override
    public boolean saveCurrency(TbCurrency tbCurrency) {
        // 根据币种名称查询币种信息，校验有效时间范围不能重叠

        LocalDateTime effectiveDate = tbCurrency.getEffectiveDate();
        LocalDateTime expirationDate = tbCurrency.getExpirationDate();

        LambdaQueryWrapper<TbCurrency> wrapper = Wrappers.lambdaQuery(TbCurrency.class).last(" limit 1")
            .eq(TbCurrency::getCurrencyName, tbCurrency.getCurrencyName())
            .and(item ->
                item.ge(TbCurrency::getEffectiveDate, effectiveDate).le(TbCurrency::getEffectiveDate, expirationDate)
                    .or(k -> k.le(TbCurrency::getEffectiveDate, effectiveDate)
                        .ge(TbCurrency::getExpirationDate, expirationDate))
                    .or(m -> m.ge(TbCurrency::getExpirationDate, effectiveDate)
                        .le(TbCurrency::getExpirationDate, expirationDate))
            );

        // 存在重叠
        boolean existOverlap = baseMapper.exists(wrapper);
        if (existOverlap) {
            throw new RuntimeException(MessageUtils.message("currency.error.dateRangeOverlap"));
        }
        return save(tbCurrency);

    }

    @Override
    public TbCurrency getUsableCurrencyRate(Long currencyId) {
        List<TbCurrency> list = this.list(Wrappers.<TbCurrency>lambdaQuery()
            .eq(TbCurrency::getCurrencyId, currencyId)
            .le(TbCurrency::getEffectiveDate, DateUtils.getNowDate())
            .ge(TbCurrency::getExpirationDate, DateUtils.getNowDate())
            .eq(TbCurrency::getDelFlag, DelFlagEnum.NORMAL.getValue())
        );
        if (CollUtil.isEmpty(list)) {
            throw new ServiceException(MessageUtils.message("quotation.currency.unknown"));
        }
        return list.get(0);
    }
}
