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.common.utils.StringUtils;
import com.kmall.schedule.dao.QzStorePromMapper;
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 QzStoreTicketDiscountService {
    private static Log logger = LogFactory.getLog(QzStoreTicketDiscountService.class);
    @Autowired
    private QzStoreTicketDiscountMapper qzStoreTicketDiscountMapper;

    /**
     * 查询优惠券有效时间是否过期，修改过期优惠券数据结束状态为无效，修改过期用户优惠券状态为无效
     */
    @Transactional
    public void updateQueryTicketDiscount() {
        logger.info(">>>>>>>>>>>>>>>>>>>>updateQueryTicketDiscount is start ");
        logger.info(">>>>>>>>>>>>>>>>>>>>修改过期优惠券数据结束状态为无效，修改过期用户优惠券状态为无效");
        //查询时间类型为固定时间，且超时的优惠券数据
        List<Map> storeTicketDiscountList00 = qzStoreTicketDiscountMapper.queryStoreTicketDiscountListBy00();
        if (null != storeTicketDiscountList00 && storeTicketDiscountList00.size() > 0) {
            for (Map map : storeTicketDiscountList00) {
                Integer ticketDistId = MapUtils.getInteger("ticketDistId", map);
                Integer userCouponId = MapUtils.getInteger("userCouponId", map);
                Integer storeTopicId = MapUtils.getInteger("storeTopicId", map);
                if(userCouponId != null) {
                    //修改用户优惠券为不可用
                    Map userCouponMap = Maps.newHashMap();
                    userCouponMap.put("userCouponId", userCouponId);
                    qzStoreTicketDiscountMapper.updateUserCoupon(userCouponMap);//修改用户领取优惠券状态为不可用
                }
                if(ticketDistId != null){
                    Map param = Maps.newHashMap();
                    param.put("ticketDistId", ticketDistId);
                    param.put("isPast", Dict.discountIsPast.item_1.getItem());//已过期 是否过期，0：否，1：是
                    param.put("isValid", 1);//不可用
                    qzStoreTicketDiscountMapper.updateStoreTicketDiscount(param);//修改状态

                    Map adMap = qzStoreTicketDiscountMapper.selectAdIdByDiscId(ticketDistId);
                    if(adMap != null){
                        Integer adId = MapUtils.getInteger("adId", adMap);
                        Map updateAdMap = new HashMap();
                        updateAdMap.put("enabled", 0);
                        updateAdMap.put("adId", adId);
                        qzStoreTicketDiscountMapper.updateAd(updateAdMap);
                    }
                }
                if(storeTopicId != null){
                    qzStoreTicketDiscountMapper.updateStoreTopic(storeTopicId);
                }
            }
        }
        //查询时间类型为领取后，且时间失效的优惠券数据
        List<Map> storeTicketDiscountList01 = qzStoreTicketDiscountMapper.queryStoreTicketDiscountListBy01();
        if (null != storeTicketDiscountList01 && storeTicketDiscountList01.size() > 0) {
            for (Map map : storeTicketDiscountList01) {
                Integer userCouponId = MapUtils.getInteger("userCouponId", map);
                Date endTime = MapUtils.getDate("endTime", map);
                if (!DateUtils.compareDate(new Date(), endTime)) {
                    if (userCouponId != null) {
                        //修改用户优惠券为不可用
                        Map userCouponMap = Maps.newHashMap();
                        userCouponMap.put("userCouponId", userCouponId);
                        qzStoreTicketDiscountMapper.updateUserCoupon(userCouponMap);
                    }
                }
            }
        }
        /*//查询没库存的优惠券
        List<Map> storeTicketDiscountList = qzStoreTicketDiscountMapper.queryStoreTicketDiscountListByLimit();
        if (null != storeTicketDiscountList && storeTicketDiscountList.size() > 0) {
            for (Map map : storeTicketDiscountList) {
                Integer ticketDistId = MapUtils.getInteger("ticketDistId", map);
                Map param = Maps.newHashMap();
                param.put("ticketDistId", ticketDistId);
                param.put("isValid", 1);//不可用
                qzStoreTicketDiscountMapper.updateStoreTicketDiscount(param);//修改状态
            }
        }*/

        logger.info(">>>>>>>>>>>>>>>>>>>>updateQueryTicketDiscount is end ");
    }

    public static void main(String[] args) {

    }
}
