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

import com.jeesharp.business.modules.mall.entity.MallPromotionRule;
import com.jeesharp.business.modules.mall.mapper.MallPromotionRuleMapper;
import com.jeesharp.business.modules.mall.service.MallPromotionRuleService;
import com.jeesharp.business.modules.mall.vo.MallPromotionRuleQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 满减/满赠规则 Service 实现类
 */
@Service
@Transactional(readOnly = true)
public class MallPromotionRuleServiceImpl extends CrudService<MallPromotionRuleMapper, MallPromotionRule, MallPromotionRuleQueryVO> implements MallPromotionRuleService {

    private static final int ENABLED_STATUS = 1;
    private static final String FULL_REDUCTION_RULE = "FULL_REDUCTION";
    private static final String FULL_GIFT_RULE = "FULL_GIFT";
    private static final String IDS_SEPARATOR = ",";

    @Override
    @Transactional(readOnly = false)
    public void createRule(MallPromotionRule entity) {
        save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateRule(MallPromotionRule entity) {
        update(entity);
    }

    @Override
    public List<MallPromotionRule> getAvailableFullReductionRules() {
        MallPromotionRuleQueryVO query = new MallPromotionRuleQueryVO();
        query.setRuleType(FULL_REDUCTION_RULE);
        query.setStatus(ENABLED_STATUS);
        return findList(query);
    }

    @Override
    public List<MallPromotionRule> getAvailableFullGiftRules() {
        MallPromotionRuleQueryVO query = new MallPromotionRuleQueryVO();
        query.setRuleType(FULL_GIFT_RULE);
        query.setStatus(ENABLED_STATUS);
        return findList(query);
    }

    @Override
    public BigDecimal calculateFullReductionDiscount(BigDecimal orderAmount) {
        List<MallPromotionRule> rules = getAvailableFullReductionRules();
        BigDecimal maxDiscount = BigDecimal.ZERO;
        Date now = new Date();
        for (MallPromotionRule rule : rules) {
            if (rule.getStartTime() != null && now.before(rule.getStartTime())) {
                continue;
            }
            if (rule.getEndTime() != null && now.after(rule.getEndTime())) {
                continue;
            }
            if (orderAmount.compareTo(rule.getConditionAmount()) >= 0
                    && rule.getDiscountAmount().compareTo(maxDiscount) > 0) {
                maxDiscount = rule.getDiscountAmount();
            }
        }
        return maxDiscount;
    }

    @Override
    public List<MallPromotionRule> getAvailableGifts(BigDecimal orderAmount) {
        List<MallPromotionRule> rules = getAvailableFullGiftRules();
        List<MallPromotionRule> availableGifts = new ArrayList<>();
        Date now = new Date();
        for (MallPromotionRule rule : rules) {
            if (rule.getStartTime() != null && now.before(rule.getStartTime())) {
                continue;
            }
            if (rule.getEndTime() != null && now.after(rule.getEndTime())) {
                continue;
            }
            if (orderAmount.compareTo(rule.getConditionAmount()) >= 0) {
                availableGifts.add(rule);
            }
        }
        return availableGifts;
    }
}
