package com.yiqi.settlement.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.settlement.domain.SettlementFactoryRate;
import com.yiqi.settlement.domain.SettlementStoreRate;
import com.yiqi.settlement.domain.dto.SettlementStoreRateQueryDTO;
import com.yiqi.settlement.domain.vo.SettlementStoreRateListVO;
import com.yiqi.settlement.mapper.SettlementStoreRateMapper;
import com.yiqi.settlement.service.ISettlementStoreRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 门店结算费率配置Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-03
 */
@Service
public class SettlementStoreRateServiceImpl extends ServiceImpl<SettlementStoreRateMapper, SettlementStoreRate> implements ISettlementStoreRateService {

    @Override
    public List<SettlementStoreRateListVO> queryList(SettlementStoreRateQueryDTO queryDTO) {
        List<SettlementStoreRateListVO> voList = baseMapper.queryList(queryDTO.getStoreId());
        for (SettlementStoreRateListVO vo : voList) {
            vo.setIsValid(0);
            if (vo.getId() != null) {
                if (vo.getEndDate() != null && DateUtil.compare(vo.getEndDate(), DateUtils.getNowDate()) > 0) {
                    vo.setIsValid(1);
                }
            }
        }
        return voList;
    }

    @Override
    public SettlementStoreRate getByStoreId(Long storeId, Date date) {
        QueryWrapper<SettlementStoreRate> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SettlementStoreRate::getStoreId, storeId)
                .le(SettlementStoreRate::getStartDate, date)
                .ge(SettlementStoreRate::getEndDate, date)
                .last("ORDER BY id DESC LIMIT 1");
        return baseMapper.selectOne(queryWrapper);
    }


}
