package com.kmall.schedule.service;

import com.google.common.collect.Maps;
import com.kmall.common.constant.Dict;
import com.kmall.common.utils.DateUtils;
import com.kmall.common.utils.MapUtils;
import com.kmall.schedule.dao.QzStoreCampMinusMapper;
import com.kmall.schedule.dao.QzStoreTicketDiscountMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 满减满折定时任务
 *
 * @author HUANGYAQIN(CN)
 * @date 2019/5/5
 */
@Service
public class QzStoreCampMinusService {
    private static Log logger = LogFactory.getLog(QzStoreCampMinusService.class);
    @Autowired
    private QzStoreCampMinusMapper qzStoreCampMinusMapper;

    /**
     * 修改过期满减满折数据结束状态为无效，修改未开始且开始时间已小于或等于当前时间的满减满折结束状态为进行中
     */
    @Transactional
    public void updateQueryStoreCampMinus() {
        logger.info(">>>>>>>>>>>>>>>>>>>>updateQueryStoreCampMinus is start ");
        logger.info(">>>>>>>>>>>>>>>>>>>>修改过期满减满折数据结束状态为无效，修改未开始且开始时间已小于或等于当前时间的满减满折结束状态为进行中");
        //查询进行中且超时的满减满折数据
        List<Map> storeCampMinusList = qzStoreCampMinusMapper.queryCampMinusListByPasting();
        if (null != storeCampMinusList && storeCampMinusList.size() > 0) {
            for (Map map : storeCampMinusList) {
                Integer campMinusId = MapUtils.getInteger("campMinusId", map);
                Integer storeTopicId = MapUtils.getInteger("storeTopicId", map);
                //修改满减满折活动为不可用
                Map param = Maps.newHashMap();
                param.put("campMinusId", campMinusId);
                param.put("isValid", "1");
                param.put("isPast", Dict.isPast.item_1.getItem());//已结束
                qzStoreCampMinusMapper.updateStoreCampMinus(param);

                Map adMap = qzStoreCampMinusMapper.selectAdIdByTopicId(storeTopicId);
                if(adMap != null){
                    Integer adId = MapUtils.getInteger("adId", adMap);
                    Map updateAdMap = new HashMap();
                    updateAdMap.put("adId", adId);
                    updateAdMap.put("enabled", 0);
                    qzStoreCampMinusMapper.updateAd(updateAdMap);
                }
                if(storeTopicId != null){
                    qzStoreCampMinusMapper.updateStoreTopic(storeTopicId);
                }
            }
        }

        //查询未开始，且开始时间已小于或等于当前时间的满减满折活动
        List<Map> notStartList = qzStoreCampMinusMapper.queryCampMinusListByPastNotStart();
        if (null != notStartList && notStartList.size() > 0) {
            for (Map map : notStartList) {
                Integer campMinusId = MapUtils.getInteger("campMinusId", map);
                //修改满减满折活动状态为进行中
                Map param = Maps.newHashMap();
                param.put("campMinusId", campMinusId);
                param.put("isPast", Dict.isPast.item_0.getItem());//进行中
                qzStoreCampMinusMapper.updateStoreCampMinus(param);
            }
        }
        logger.info(">>>>>>>>>>>>>>>>>>>>updateQueryStoreCampMinus is end ");
    }
}
