package com.kmall.schedule.service;

import com.google.common.collect.Maps;
import com.kmall.common.utils.MapUtils;
import com.kmall.schedule.dao.QzStorePromMapper;
import com.kmall.schedule.dao.QzStoreTopicStatisMapper;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 门店活动统计定时任务
 *
 * @author HUANGYAQIN(CN)
 * @date 2019/5/25
 */
@Service
public class QzStoreTopicStatisService {
    private static Log logger = LogFactory.getLog(QzStoreTopicStatisService.class);
    @Autowired
    private QzStoreTopicStatisMapper qzStoreTopicStatisMapper;

    /**
     * 支付成功且是门店优惠券活动订单统计支付数据
     */
    @Transactional
    public void updateTopicStatByPaySuccess() {
        logger.info(">>>>>>>>>>>>>>>>>>>>updateTopicStatByPaySuccess is start ");
        logger.info(">>>>>>>>>>>>>>>>>>>>查询支付成功的门店优惠券活动订单，修改门店优惠券活动统计数据 ");
        Integer[] statusList = {201};
        //查看未过期且有效的优惠券支付成功的订单信息
        List<Map> topicByPaySuccessList = qzStoreTopicStatisMapper.queryTicketStoreTopicByPaySuccess(statusList);
        updateTopicStatByPaySuccessByList(topicByPaySuccessList, statusList);
        logger.info(">>>>>>>>>>>>>>>>>>>>updateTopicStatByPaySuccess is end ");
    }

    /**
     * 退款成功且是门店优惠券活动订单统计支付数据
     */
    @Transactional
    public void updateTopicStatByRefundSuccess() {
        logger.info(">>>>>>>>>>>>>>>>>>>>updateTopicStatByRefundSuccess is start ");
        logger.info(">>>>>>>>>>>>>>>>>>>>查询退款成功的门店优惠券活动订单，修改门店优惠券活动统计数据 ");

        Integer[] statusList = {401};
        //查看未过期且有效的优惠券退款成功的订单信息
        List<Map> promByRefundSuccessList = qzStoreTopicStatisMapper.queryTicketStoreTopicByPaySuccess(statusList);
        if (null != promByRefundSuccessList && promByRefundSuccessList.size() > 0) {
            updateTopicStatByRefundSuccessByList(promByRefundSuccessList, statusList);
        }else{
            logger.info(">>>>>>>>>>>>>>>>>>>>查询退款成功的门店优惠券活动订单没有数据 ");
        }
        logger.info(">>>>>>>>>>>>>>>>>>>>updateTopicStatByRefundSuccess is end ");
    }

    /**
     * 支付成功且是门店促销满减活动订单统计支付数据
     */
    @Transactional
    public void updateCampTopicStatByPaySuccess() {
        logger.info(">>>>>>>>>>>>>>>>>>>>updateCampTopicStatByPaySuccess is start ");
        logger.info(">>>>>>>>>>>>>>>>>>>>查询支付成功的门店促销满减活动订单，修改门店促销满减活动统计数据 ");
        Integer[] statusList = {201};
        //查看未过期且有效的促销满减支付成功的订单信息
        List<Map> topicByPaySuccessList = qzStoreTopicStatisMapper.queryCampStoreTopicByPaySuccess(statusList);

        if (null != topicByPaySuccessList && topicByPaySuccessList.size() > 0) {
            updateTopicStatByPaySuccessByList(topicByPaySuccessList, statusList);
        }else{
            logger.info(">>>>>>>>>>>>>>>>>>>>查询支付成功的门店促销满减活动订单没有数据 ");
        }
        logger.info(">>>>>>>>>>>>>>>>>>>>updateCampTopicStatByPaySuccess is end ");
    }

    /**
     * 退款成功且是门店促销满减活动订单统计支付数据
     */
    @Transactional
    public void updateCampTopicStatByRefundSuccess() {
        logger.info(">>>>>>>>>>>>>>>>>>>>updateCampTopicStatByRefundSuccess is start ");
        logger.info(">>>>>>>>>>>>>>>>>>>>查询退款成功的门店促销满减活动订单，修改门店促销满减活动统计数据 ");
        Integer[] statusList = {401};
        //查看未过期且有效的促销满减退款成功的订单信息
        List<Map> campByRefundSuccessList = qzStoreTopicStatisMapper.queryCampStoreTopicByPaySuccess(statusList);
        if (null != campByRefundSuccessList && campByRefundSuccessList.size() > 0) {
            updateTopicStatByRefundSuccessByList(campByRefundSuccessList, statusList);
        }else{
            logger.info(">>>>>>>>>>>>>>>>>>>>查询退款成功的门店促销满减活动订单没有数据 ");
        }

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


    private void updateTopicStatByPaySuccessByList(List<Map> list, Integer[] statusList){
        for (Map map : list) {
            Integer storeTopicId = MapUtils.getInteger("storeTopicId", map);
            Integer storeTopicStatId = MapUtils.getInteger("storeTopicStatId", map);
            Map updateParam = Maps.newHashMap();

            Map queryMap = new HashMap();
            queryMap.put("storeTopicId", storeTopicId);
            queryMap.put("statusListOne", statusList);
            queryMap.put("statusListTwo", statusList);
            Map statDataMap = qzStoreTopicStatisMapper.getStatDataByStoreTopicId(queryMap);
            Double receivMoney = parseDoubleValid(MapUtils.getDouble("receivMoney", statDataMap));
            Double orderActualPrice = parseDoubleValid(MapUtils.getDouble("orderActualPrice", statDataMap));
            Integer payCaseNum = parseIntegerValid(MapUtils.getInteger("payCaseNum", statDataMap));
            Integer payOrderNum = parseIntegerValid(MapUtils.getInteger("payOrderNum", statDataMap));
            Integer payerNum = parseIntegerValid(MapUtils.getInteger("payerNum", statDataMap));
            // TODO: 2019/5/6 实收金额：已包含优惠券、运费等的扣减
            updateParam.put("receivMoney", receivMoney);
            updateParam.put("actMoney", orderActualPrice);
            updateParam.put("payCaseNum", payCaseNum);
            updateParam.put("payOrderNum", payOrderNum);
            updateParam.put("payerNum", payerNum);
            updateParam.put("storeTopicStatId", storeTopicStatId);
            updateParam.put("storeTopicId", storeTopicId);
            qzStoreTopicStatisMapper.updateStoreTopicStat(updateParam);//修改状态
        }
    }

    private void updateTopicStatByRefundSuccessByList(List<Map> list, Integer[] statusList){
        for (Map refundMap : list) {
            Integer storeTopicId = MapUtils.getInteger("storeTopicId", refundMap);
            Integer storeTopicStatId = MapUtils.getInteger("storeTopicStatId", refundMap);
            //查出退款的原统计数据
            Double orgReceivMoney = parseDoubleValid(MapUtils.getDouble("receivMoney", refundMap));
            Double orgActMoney = parseDoubleValid(MapUtils.getDouble("actMoney", refundMap));
            Integer orgPayCaseNum = parseIntegerValid(MapUtils.getInteger("payCaseNum", refundMap));
            Integer orgPayOrderNum = parseIntegerValid(MapUtils.getInteger("payOrderNum", refundMap));
            Integer orgPayerNum = parseIntegerValid(MapUtils.getInteger("payerNum", refundMap));
            if(orgReceivMoney == 0 || orgActMoney == 0){
                return;
            }
            Map updateParam = Maps.newHashMap();
            //查出该商品的总退款统计数据
            Map queryMap = new HashMap();
            queryMap.put("storeTopicId", storeTopicId);
            queryMap.put("statusListOne", statusList);
            queryMap.put("statusListTwo", statusList);
            Map statDataMap = qzStoreTopicStatisMapper.getStatDataByStoreTopicId(queryMap);
            Double receivMoney = parseDoubleValid(MapUtils.getDouble("receivMoney", statDataMap));
            Double orderActualPrice = parseDoubleValid(MapUtils.getDouble("orderActualPrice", statDataMap));
            Integer payCaseNum = parseIntegerValid(MapUtils.getInteger("payCaseNum", statDataMap));
            Integer payOrderNum = parseIntegerValid(MapUtils.getInteger("payOrderNum", statDataMap));
            Integer payerNum = parseIntegerValid(MapUtils.getInteger("payerNum", statDataMap));
            // TODO: 2019/5/6 实收金额：还需要计算优惠券、运费等
            updateParam.put("receivMoney", orgReceivMoney - receivMoney);
            updateParam.put("actMoney", orgActMoney - orderActualPrice);
            updateParam.put("payCaseNum", orgPayCaseNum - payCaseNum);
            updateParam.put("payOrderNum", orgPayOrderNum - payOrderNum);
            updateParam.put("payerNum", orgPayerNum - payerNum);

            updateParam.put("storeTopicStatId", storeTopicStatId);
            updateParam.put("storeTopicId", storeTopicId);
            qzStoreTopicStatisMapper.updateStoreTopicStat(updateParam);//修改状态
        }
    }

    private Double parseDoubleValid(Double param){
        param = param == null ? 0 : param;
        return param;
    }

    private Integer parseIntegerValid(Integer param){
        param = param == null ? 0 : param;
        return param;
    }

    public static void main(String[] args) {

    }

    /**
     * 统计门店促销满减活动订单，下单单数、下单人数数据
     *//*
    @Transactional
    public void updateCampTopicStatBySubmit() {
        logger.info(">>>>>>>>>>>>>>>>>>>>updateCampTopicStatBySubmit is start ");
        logger.info(">>>>>>>>>>>>>>>>>>>>查询门店促销满减活动订单，修改门店促销满减活动统计数据 ");
        List<Map> topicByOrderList = qzStoreTopicStatisMapper.queryCampByOrderList();
        if (null != topicByOrderList && topicByOrderList.size() > 0) {
            for (Map map : topicByOrderList) {
                Integer storeTopicId = MapUtils.getInteger("storeTopicId", map);
                Integer storeTopicStatId = MapUtils.getInteger("storeTopicStatId", map);
                Integer submitOrderNum = MapUtils.getInteger("submitOrderNum", map);

                List<Map> submitNumList = qzStoreTopicStatisMapper.querySubmitNumByCampOrder(storeTopicId);
                Integer submitNum = submitNumList != null ? submitNumList.size() : 0;
                Map updateParam = Maps.newHashMap();

                updateParam.put("submitOrderNum", submitOrderNum);
                updateParam.put("submitNum", submitNum);
                updateParam.put("storeTopicStatId", storeTopicStatId);
                qzStoreTopicStatisMapper.updateStoreTopicStatSubmit(updateParam);//修改状态
            }
        }
        logger.info(">>>>>>>>>>>>>>>>>>>>updateCampTopicStatBySubmit is end ");
    }*/
}
