package com.hzs.activity.pick.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.activity.base.service.IAcBaseConfigService;
import com.hzs.activity.pick.mapper.AcRetailPickLogMapper;
import com.hzs.activity.pick.service.IAcRetailPickLogService;
import com.hzs.activity.pick.service.IAcRetailPickProductConfigService;
import com.hzs.activity.pick.service.IAcRetailPickService;
import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.enums.EActType;
import com.hzs.common.core.enums.ESystemType;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.activity.base.AcBaseConfig;
import com.hzs.common.domain.activity.pick.AcRetailPickLog;
import com.hzs.common.domain.activity.pick.AcRetailPickProductConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员提货赠送活动日志表 服务实现类
 */
@Service
@Slf4j
public class AcRetailPickLogServiceImpl extends ServiceImpl<AcRetailPickLogMapper, AcRetailPickLog> implements IAcRetailPickLogService {

    @Autowired
    private IAcBaseConfigService baseConfigService;
    @Autowired
    private IAcRetailPickProductConfigService iAcRetailPickProductConfigService;
    @Autowired
    private IAcRetailPickService iAcRetailPickService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deliveryLog() {
        Date now = new Date();
        // 获取上个月的第一天和最后一天
        Date lastMonthFirstDay = DateUtils.beforeMonthFirstDate(now);
        Date lastMonthLastDay = DateUtils.beforeMonthLastDate(now);

        for (ESystemType eSystemType : ESystemType.getEnableList()) {
            List<AcBaseConfig> configs = baseConfigService.listProgressList(EActType.PICK_GIFT.getValue(), CountryConstants.CHINA_COUNTRY, eSystemType.getValue(),
                    DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, lastMonthFirstDay), DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, lastMonthLastDay));
            if (CollectionUtil.isEmpty(configs)) {
                log.info("没有进行中的提货赠送活动");
                return;
            }

            for (AcBaseConfig config : configs) {
                if (DateUtils.compareDate(lastMonthFirstDay, config.getActStartDate()) < 0) {
                    lastMonthFirstDay = config.getActStartDate();
                }

                if (DateUtils.compareDate(lastMonthLastDay, config.getActEndDate()) > 0) {
                    lastMonthLastDay = config.getActEndDate();
                }
                if (DateUtils.compareDate(lastMonthFirstDay, lastMonthLastDay) > 0) {
                    log.info("不在活动时间范围内");
                    return;
                }
                //查询赠送活动规则
                LambdaQueryWrapper<AcRetailPickProductConfig> productConfigWrapper = new LambdaQueryWrapper<>();
                productConfigWrapper.eq(AcRetailPickProductConfig::getPkBaseId, config.getPkId());
                List<AcRetailPickProductConfig> productConfigs = iAcRetailPickProductConfigService.list(productConfigWrapper);

                if (CollectionUtil.isNotEmpty(productConfigs)) {
                    List<Integer> pkWaresList = productConfigs.stream().map(AcRetailPickProductConfig::getPkWares).collect(Collectors.toList());
                    handleQualifiedOrders(productConfigs.get(0), lastMonthFirstDay, lastMonthLastDay, pkWaresList, eSystemType.getValue());
                }
            }
        }
    }

    //查询符合提货赠送活动的会员订单
    private void handleQualifiedOrders(AcRetailPickProductConfig productConfig, Date startDate, Date endDate, List<Integer> pkWaresList, Integer systemType) {
        Date nowDate = new Date();
        // 插入提货赠送日志
        baseMapper.insertPickLog(productConfig.getPkBaseId(), DateUtils.getStartTime(startDate), DateUtils.getEndTime(endDate), pkWaresList, productConfig.getWaresQuantity(), productConfig.getGiftQuantity(), nowDate, systemType);
        // 批量更新提货数据
        iAcRetailPickService.handlePick(productConfig.getPkBaseId(), nowDate);
    }

}
