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

import com.jeesharp.business.modules.mall.entity.MallGiftRule;
import com.jeesharp.business.modules.mall.mapper.MallGiftRuleMapper;
import com.jeesharp.business.modules.mall.service.MallGiftRuleService;
import com.jeesharp.business.modules.mall.vo.MallGiftRuleQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 赠品规则 Service 实现类
 */
@Service
@Transactional(readOnly = true)
public class MallGiftRuleServiceImpl extends CrudService<MallGiftRuleMapper, MallGiftRule, MallGiftRuleQueryVO> implements MallGiftRuleService {

    private static final int ENABLED_STATUS = 1;
    private static final String IDS_SEPARATOR = ",";

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

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

    @Override
    public List<MallGiftRule> getAvailableGiftRules() {
        MallGiftRuleQueryVO query = new MallGiftRuleQueryVO();
        query.setStatus(ENABLED_STATUS);
        return findList(query);
    }

    @Override
    public List<MallGiftRule> getGiftRulesBySku(String skuId) {
        List<MallGiftRule> allRules = getAvailableGiftRules();
        List<MallGiftRule> matchedRules = new ArrayList<>();
        Date now = new Date();
        for (MallGiftRule rule : allRules) {
            if (rule.getStartTime() != null && now.before(rule.getStartTime())) {
                continue;
            }
            if (rule.getEndTime() != null && now.after(rule.getEndTime())) {
                continue;
            }
            if ("BUY_A_GIVE_B".equals(rule.getRuleType())
                    && rule.getConditionSkuIds() != null
                    && Arrays.asList(rule.getConditionSkuIds().split(IDS_SEPARATOR)).contains(skuId)) {
                matchedRules.add(rule);
            }
        }
        return matchedRules;
    }

    @Override
    public List<MallGiftRule> checkFullItemGifts(Map<String, Integer> skuQuantities) {
        List<MallGiftRule> allRules = getAvailableGiftRules();
        List<MallGiftRule> matchedRules = new ArrayList<>();
        Date now = new Date();
        for (MallGiftRule rule : allRules) {
            if (rule.getStartTime() != null && now.before(rule.getStartTime())) {
                continue;
            }
            if (rule.getEndTime() != null && now.after(rule.getEndTime())) {
                continue;
            }
            if ("FULL_ITEM".equals(rule.getRuleType())) {
                int totalQuantity = skuQuantities.values().stream().mapToInt(Integer::intValue).sum();
                if (totalQuantity >= rule.getConditionQuantity()) {
                    matchedRules.add(rule);
                }
            }
        }
        return matchedRules;
    }
}
