package com.jeesharp.business.modules.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.food.entity.FoodCompanySubsidyRule;
import com.jeesharp.business.modules.food.mapper.FoodCompanySubsidyRuleMapper;
import com.jeesharp.business.modules.food.service.FoodCompanySubsidyRuleService;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import com.jeesharp.framework.pojo.query.PaginationVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class FoodCompanySubsidyRuleServiceImpl extends CrudService<FoodCompanySubsidyRuleMapper, FoodCompanySubsidyRule, PaginationVO> implements FoodCompanySubsidyRuleService {

    private static final String DEL_NORMAL = "0";
    private static final int STATUS_ENABLED = 1;

    @Override
    public List<FoodCompanySubsidyRule> listEffectiveRules(String companyId, LocalDateTime now) {
        return getBaseMapper().selectList(new LambdaQueryWrapper<FoodCompanySubsidyRule>()
            .eq(FoodCompanySubsidyRule::getCompanyId, companyId)
            .eq(FoodCompanySubsidyRule::getStatus, STATUS_ENABLED)
            .eq(FoodCompanySubsidyRule::getDelFlag, DEL_NORMAL)
            .le(FoodCompanySubsidyRule::getEffectiveTime, now)
            .and(w -> w.isNull(FoodCompanySubsidyRule::getExpireTime).or().gt(FoodCompanySubsidyRule::getExpireTime, now))
            .orderByDesc(FoodCompanySubsidyRule::getEffectiveTime));
    }
}
