package com.yiqi.settlement.service.impl;

import java.util.Collections;
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.dto.SettlementFactoryRateQueryDTO;
import com.yiqi.settlement.domain.vo.SettlementFactoryRateListVO;
import com.yiqi.settlement.domain.vo.SettlementStoreRateListVO;
import com.yiqi.settlement.mapper.SettlementFactoryRateMapper;
import com.yiqi.settlement.service.ISettlementFactoryRateService;
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 SettlementFactoryRateServiceImpl extends ServiceImpl<SettlementFactoryRateMapper, SettlementFactoryRate> implements ISettlementFactoryRateService {

    @Override
    public List<SettlementFactoryRateListVO> queryList(SettlementFactoryRateQueryDTO queryDTO) {
        List<SettlementFactoryRateListVO> voList = baseMapper.queryList(queryDTO.getFactoryId());
        for (SettlementFactoryRateListVO 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 SettlementFactoryRate getByFactoryId(Long factoryId, Date date) {
        QueryWrapper<SettlementFactoryRate> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SettlementFactoryRate::getFactoryId, factoryId)
                .le(SettlementFactoryRate::getStartDate, date)
                .ge(SettlementFactoryRate::getEndDate, date)
                .last("ORDER BY id DESC LIMIT 1");
        return baseMapper.selectOne(queryWrapper);
    }
}
