package com.kmall.api.service.mk;

import com.google.common.collect.Maps;
import com.kmall.api.dao.ApiOrderMapper;
import com.kmall.api.dao.ApiUserCouponMapper;
import com.kmall.api.dao.mk.ApiMkStoreTopicStatMapper;
import com.kmall.api.dao.mk.ApiMkStoreTopicVisitorsMapper;
import com.kmall.api.dao.mk.ApiStoreTopicMapper;
import com.kmall.api.entity.OrderGoodsVo;
import com.kmall.api.entity.OrderVo;
import com.kmall.api.entity.UserVo;
import com.kmall.api.entity.mk.*;
import com.kmall.common.utils.MapUtils;
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.Map;

/**
 * 门店活动统计Service实现类
 *
 * @author emato
 * @email admin@qhdswl.com
 * @date 2019-05-24 17:58:24
 */
@Service
public class ApiMkStoreTopicStatService {
    @Autowired
    private ApiMkStoreTopicStatMapper apiMkStoreTopicStatMapper;
    @Autowired
    private ApiMkStoreTopicVisitorsMapper apiMkStoreTopicVisitorsMapper;
    @Autowired
    private ApiStoreTopicMapper apiStoreTopicMapper;
    @Autowired
    private ApiOrderMapper apiOrderMapper;
    @Autowired
    private ApiUserCouponMapper apiUserCouponMapper;
    @Autowired
    private ApiMkStoreTicketDiscountService apiMkStoreTicketDiscountService;

    public MkStoreTopicStatVo queryObject(Integer storeTopicStatId) {
        return apiMkStoreTopicStatMapper.queryObject(storeTopicStatId);
    }

    public List<MkStoreTopicStatVo> queryList(Map<String, Object> map) {
        return apiMkStoreTopicStatMapper.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return apiMkStoreTopicStatMapper.queryTotal(map);
    }

    public int save(MkStoreTopicStatVo mkStoreTopicStat) {
        return apiMkStoreTopicStatMapper.save(mkStoreTopicStat);
    }

    public int update(MkStoreTopicStatVo mkStoreTopicStat) {
        return apiMkStoreTopicStatMapper.update(mkStoreTopicStat);
    }

    public int delete(Integer storeTopicStatId) {
        return apiMkStoreTopicStatMapper.delete(storeTopicStatId);
    }

    public int deleteBatch(Integer[]storeTopicStatIds) {
        return apiMkStoreTopicStatMapper.deleteBatch(storeTopicStatIds);
    }

    /**
     * 记录活动营销统计访客量数据
     * @param storeTopicId
     * @param storeId
     * @param userId
     */
    public void saveStoreTopicStatByVisitData(Integer storeTopicId, Long storeId, Long userId){
        StoreTopicVo storeTopicVo = apiStoreTopicMapper.queryObject(storeTopicId);
        MkStoreTopicStatVo mkStoreTopicStatVo = new MkStoreTopicStatVo();
        if(storeTopicVo != null) {
            //新增统计浏览量
            MkStoreTopicStatVo storeTopicStatVo = apiMkStoreTopicStatMapper.queryObjectByTopicId(storeTopicId);
            //记录活动访客数据
            MkStoreTopicVisitorsVo storeTopicVisitorsVo = apiMkStoreTopicVisitorsMapper.queryVisitorByTopicIdAndUserId(storeTopicId, userId);
            if (storeTopicVisitorsVo == null) {
                MkStoreTopicVisitorsVo mkStoreTopicVisitorsVo = new MkStoreTopicVisitorsVo();
                mkStoreTopicVisitorsVo.setStoreTopicId(storeTopicId);
                mkStoreTopicVisitorsVo.setUserId(Integer.parseInt(String.valueOf(userId)));
                mkStoreTopicVisitorsVo.setIsValid("0");
                mkStoreTopicVisitorsVo.setCreateTime(new Date());
                mkStoreTopicVisitorsVo.setModTime(new Date());
                apiMkStoreTopicVisitorsMapper.save(mkStoreTopicVisitorsVo);
            }
            Integer visitNum = apiMkStoreTopicVisitorsMapper.queryCountByStoreTopicId(storeTopicVo.getId());
            mkStoreTopicStatVo.setVisitPageDeep(1);
            mkStoreTopicStatVo.setStoreId(Integer.parseInt(String.valueOf(storeId)));

            if (storeTopicStatVo != null) {
                mkStoreTopicStatVo.setBrowseNum(storeTopicStatVo.getBrowseNum()==null?1:storeTopicStatVo.getBrowseNum() + 1);
                mkStoreTopicStatVo.setVisitNum(visitNum);
                mkStoreTopicStatVo.setRelaBrowseNum(storeTopicStatVo.getRelaBrowseNum()==null?1:storeTopicStatVo.getRelaBrowseNum() + 1);//连带浏览量
                mkStoreTopicStatVo.setRelaVisitorNum(visitNum);//连带访客量
                mkStoreTopicStatVo.setTransVisitorNum(visitNum);//转发访客量:用户转发了带有推广id的商品，其他的用户点击的访客量
                mkStoreTopicStatVo.setStoreTopicId(storeTopicVo.getId());
                mkStoreTopicStatVo.setStoreTopicStatId(storeTopicStatVo.getStoreTopicStatId());
                apiMkStoreTopicStatMapper.update(mkStoreTopicStatVo);
            } else {
                mkStoreTopicStatVo.setStoreId(Integer.parseInt(String.valueOf(storeId)));
                mkStoreTopicStatVo.setStoreTopicId(storeTopicVo.getId());
                mkStoreTopicStatVo.setBrowseNum(1);
                mkStoreTopicStatVo.setVisitNum(visitNum);
                mkStoreTopicStatVo.setRelaBrowseNum(1);//连带浏览量
                mkStoreTopicStatVo.setRelaVisitorNum(visitNum);//连带访客量
                mkStoreTopicStatVo.setTransVisitorNum(visitNum);//转发访客量:用户转发了带有推广id的商品，其他的用户点击的访客量
                mkStoreTopicStatVo.setIsValid("0");
                apiMkStoreTopicStatMapper.save(mkStoreTopicStatVo);
            }
        }
    }

    /**
     * 记录优惠券活动营销统计下单量数据
     * @param userCouponId
     */
    public void saveTicketTopicStatBySubmitData(Integer userCouponId){
        if(userCouponId != null) {
            //根据用户优惠券id查询统计数据
            MkStoreTopicStatVo statVo = apiMkStoreTopicStatMapper.queryStatDataByUserCouponId(userCouponId);
            if (statVo != null) {
                MkStoreTopicStatVo mkStorePromStatVo = new MkStoreTopicStatVo();
                //获取订单表中根据活动id查询下单人数
                List<OrderVo> submitNumList = apiOrderMapper.querySubmitNumByStoreTopicId(statVo.getStoreTopicId());
                Integer submitNum = submitNumList != null ? submitNumList.size() : 0;
                Integer submitOrderNum = apiOrderMapper.querySubmitOrderNumByStoreTopicId(statVo.getStoreTopicId());
                mkStorePromStatVo.setSubmitNum(submitNum == null ? 0 : submitNum);//下单人数
                mkStorePromStatVo.setSubmitOrderNum(submitOrderNum);//下单单数
                mkStorePromStatVo.setStoreTopicStatId(statVo.getStoreTopicStatId());
                apiMkStoreTopicStatMapper.update(mkStorePromStatVo);
            }
        }
    }

    /**
     * 记录促销满减活动营销统计下单量数据
     * @param storeTopicId
     */
    public void saveCampTopicStatBySubmitData(Long storeTopicId, Integer campMinusId){
        if(storeTopicId != null) {
            StoreTopicVo storeTopicVo = apiStoreTopicMapper.queryObject(storeTopicId);
            if(storeTopicVo != null) {
                //根据用户优惠券id查询统计数据
                MkStoreTopicStatVo statVo = apiMkStoreTopicStatMapper.queryCampByOrder(campMinusId);
                MkStoreTopicStatVo mkStoretatVo = new MkStoreTopicStatVo();
                //获取订单表中根据活动id查询下单人数
                List<Map> submitNumList = apiMkStoreTopicStatMapper.querySubmitNumByCampOrder(storeTopicVo.getId());
                Integer submitNum = submitNumList != null ? submitNumList.size() : 0;
                if (statVo != null) {
                    Integer storeTopicStatId = statVo.getStoreTopicStatId();
                    Integer submitOrderNum = statVo.getSubmitOrderNum();
                    mkStoretatVo.setSubmitNum(submitNum == null ? 0 : submitNum);//下单人数
                    mkStoretatVo.setSubmitOrderNum(submitOrderNum);//下单单数
                    mkStoretatVo.setStoreTopicStatId(storeTopicStatId);
                    apiMkStoreTopicStatMapper.update(mkStoretatVo);
                } else {
                    mkStoretatVo.setStoreId(storeTopicVo.getStoreId());
                    mkStoretatVo.setStoreTopicId(storeTopicVo.getId());
                    mkStoretatVo.setSubmitNum(submitNum == null ? 0 : submitNum);//下单人数
                    mkStoretatVo.setSubmitOrderNum(1);//下单单数
                    mkStoretatVo.setIsValid("0");
                    mkStoretatVo.setVisitPageDeep(1);
                    apiMkStoreTopicStatMapper.save(mkStoretatVo);
                }
            }
        }
    }

    /**
     * 记录活动营销统计领取量数据
     * @param userCouponId
     */
    @Transactional
    public void saveStoreTopicStatByReceiveData(Integer userCouponId){
        if(userCouponId != null) {
            MkStoreTicketDiscountVo discountVo = apiMkStoreTicketDiscountService.getDiscountByUserCouponId(userCouponId);
            if(discountVo != null){
                //根据用户优惠券id查询统计数据
                MkStoreTopicStatVo statVo = apiMkStoreTopicStatMapper.queryStatDataByUserCouponId(userCouponId);
                MkStoreTopicStatVo mkStorePromStatVo = new MkStoreTopicStatVo();
                //获取订单表中根据活动id查询领取量
                Integer receiveNum = apiUserCouponMapper.queryReceiveNumByStoreTopicId(discountVo.getStoreTopicId());
                if (statVo != null) {
                    mkStorePromStatVo.setReceiveNum(receiveNum == null ? 0 : receiveNum);//领取量
                    mkStorePromStatVo.setStoreTopicStatId(statVo.getStoreTopicStatId());
                    apiMkStoreTopicStatMapper.update(mkStorePromStatVo);
                } else {
                    mkStorePromStatVo.setStoreId(discountVo.getStoreId());
                    mkStorePromStatVo.setStoreTopicId(discountVo.getStoreTopicId());
                    mkStorePromStatVo.setReceiveNum(receiveNum == null ? 0 : receiveNum);//领取量
                    mkStorePromStatVo.setIsValid("0");
                    apiMkStoreTopicStatMapper.save(mkStorePromStatVo);
                }
            }
        }
    }
}
