package com.platform.service.custom;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.dao.customDao.MyTmGoodsVisitHistoryDao;
import com.platform.entity.TmGoodsVisitHistory;
import com.platform.enums.IdStarterEnum;
import com.platform.service.impl.TmGoodsVisitHistoryServiceImpl;
import com.platform.utils.UUidGenerator;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author abnerhou
 * @date 2020/7/17 15:56
 * @desciption
 */
@Service
@Slf4j
public class GoodsVisitHistoryService extends TmGoodsVisitHistoryServiceImpl {

/*    @Autowired
    private MyTmGoodsVisitHistoryDao myTmGoodsVisitHistoryDao;*/

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public int addNewHistory(TmGoodsVisitHistory goodsVisitHistory) {
        if (null == goodsVisitHistory) {
            return -1;
        }
        if (!StringUtils.isNoneBlank(goodsVisitHistory.getVisiterId(), goodsVisitHistory.getGoodsId())) {
            log.error("参观者或者展品ID为空，无法新增展位浏览记录");
            return -1;
        }
        goodsVisitHistory.setVisitId(UUidGenerator.generatorUuid(IdStarterEnum.DEFAULT_ID.getStarter()));
        goodsVisitHistory.setCreateTime(LocalDateTime.now());
        goodsVisitHistory.setLastModifyDatetime(LocalDateTime.now());
        goodsVisitHistory.setEnable(1);
        return getBaseMapper().insert(goodsVisitHistory);
    }


    public TmGoodsVisitHistory getHistoryByIdAndDate(String visiterId, String goodsId, LocalDate date) {
        if (!StringUtils.isNoneBlank(visiterId, goodsId) || null == date) {
            log.error("入参[visitId:{}-boothId:{}-date{}]不完整，无法获取展位浏览记录", visiterId, goodsId, date);
            return null;
        }
        QueryWrapper<TmGoodsVisitHistory> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(visiterId)){
            queryWrapper.eq("visiter_id", visiterId);
        }
        if(StringUtils.isNotBlank(goodsId)){
            queryWrapper.eq("goods_id", goodsId);
        }
        queryWrapper.eq("visit_date", date);
        queryWrapper.last("limit 1");
        return getBaseMapper().selectOne(queryWrapper);
    }

    public long countTotalBySceneNum(String sceneNum){
        if(StringUtils.isBlank(sceneNum)){
            return 0;
        }
        return  doCountTotalBySceneNum(sceneNum);
    }

    public long doCountTotalBySceneNum(String sceneNum){
        IPage<TmGoodsVisitHistory> page = new Page<>( 1 , 10);
        IPage<TmGoodsVisitHistory> resultPage = new Page<>();
        QueryWrapper<TmGoodsVisitHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enable" , 1);
        queryWrapper.eq("scene_num" , sceneNum);
        resultPage = getBaseMapper().selectPage(page , queryWrapper);
        if(null != resultPage){
            return resultPage.getTotal();
        }
        return 0l;

    }

    public List<Map<String ,Object>> getHistoryByRangeTwo(LocalDate start , int size, String sceneNum){
        List<Map<String ,Object>> resultCount = new ArrayList<>();
        if(StringUtils.isBlank(sceneNum)){
            return resultCount;
        }
        long offSet = size;
        if(offSet == 0){
            offSet = 7;
        }
        if(null == start){
            start = LocalDate.now().plusDays(-7);
        }
        log.info("时间起始为:{},当前时间跨度为:{}" ,start, offSet);
        for(int i = 0 ; i < offSet ; i++){
            LocalDate cur = start;
            long count  = countCurDateBySceneNum(sceneNum , start);
            Map<String , Object> mp = new HashMap<>();
            mp.put(cur.toString() , count);
            log.info("日期:{}的统计数为:{}" , cur.toString() , count);
            resultCount.add(mp);
            start = cur.plusDays(1);
        }
        return resultCount;
    }

    public  long countCurDateBySceneNum(String sceneNum , LocalDate start){
        IPage<TmGoodsVisitHistory> page = new Page<>( 1 , 10);
        IPage<TmGoodsVisitHistory> resultPage = new Page<>();
        QueryWrapper<TmGoodsVisitHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enable" , 1);
        queryWrapper.eq("scene_num" , sceneNum);
        queryWrapper.eq("visit_date" , start);
        resultPage = getBaseMapper().selectPage(page , queryWrapper);
        if(null != resultPage){
            return resultPage.getTotal();
        }
        return 0l;
    }


    public String getHistoryByRange(LocalDate start , int size, String sceneNum){
        if(StringUtils.isBlank(sceneNum)){
            return null;
        }
        long offSet = size;
        if(offSet == 0){
            offSet = 7;
        }
        if(null == start){
            start = LocalDate.now().plusDays(-7);
        }
        log.info("时间起始为:{},当前时间跨度为:{}" ,start, offSet);
        List<Map<String ,Object>> resultCount = new ArrayList<>();
        for(int i = 0 ; i < offSet ; i++){
            LocalDate cur = start;
            long count  = countCurDateBySceneNum(sceneNum , start);
            Map<String , Object> mp = new HashMap<>();
            mp.put(cur.toString() , count);
            log.info("日期:{}的统计数为:{}" , cur.toString() , count);
            resultCount.add(mp);
            start = cur.plusDays(1);
        }
        return JSON.toJSONString(resultCount);
    }

}
