package com.ssj.service.sys.schoolmatecircle.service.impl;

import com.ssj.bean.sys.schoolmatecircle.domain.SchoolmateCircleRanking;
import com.ssj.conch.constant.CourseConstant;
import com.ssj.dao.sys.schoolmatecircle.dao.SchoolmateCirclRankingDao;
import com.ssj.dao.sys.schoolmatecircle.dao.SchoolmateCircleQueryDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.weixin.util.DateUtil;
import com.ssj.service.sys.schoolmatecircle.service.SchoolmateRankingService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SchoolmateRankingServiceImpl extends BaseServiceImpl<SchoolmateCircleRanking, String> implements SchoolmateRankingService {
    protected static final Logger logger = LoggerFactory.getLogger(SchoolmateRankingServiceImpl.class);

    @Autowired
    private SchoolmateCircleQueryDao queryDao;

    @Autowired
    private SchoolmateCirclRankingDao dao;

    @Override
    public PagingAndSortingRepository<SchoolmateCircleRanking, String> getDao() {
        return dao;
    }

    @Override
    public List<Map<String, Object>> findCircleRanking(int type, int time) {
        return queryDao.findRankingList(type, time, CourseConstant.RANKING_COUNT);
    }

    @Override
    public void calculateCircleRanking() {
        logger.info("==========================计算排行榜开始==============================");
        logger.info("开始删除");
        dao.deleteAllInBatch();
        queryDao.deleteAllInTempTable();
        logger.info("删除结束");

        Date endDate = new Date();
        String endDateTime = DateUtil.format_yyyyMMddHHmmss(endDate);
        String beginDateTime = null;

        //近一周
        beginDateTime = DateUtil.format_yyyyMMddHHmmss(DateUtil.AddDay(endDate, -7));
        logger.info("近一周开始时间={}", beginDateTime);
        this.circleLikeRanking(beginDateTime, endDateTime, 1);
        this.circleAnswerRanking(beginDateTime, endDateTime, 1);
        this.circleActivityRanking(beginDateTime, endDateTime, 1);

        //近一月
        beginDateTime = DateUtil.format_yyyyMMddHHmmss(DateUtil.AddDay(endDate, -30));
        logger.info("近一月开始时间={}", beginDateTime);
        this.circleLikeRanking(beginDateTime, endDateTime, 2);
        this.circleAnswerRanking(beginDateTime, endDateTime, 2);
        this.circleActivityRanking(beginDateTime, endDateTime, 2);

        //近一年
        beginDateTime = DateUtil.format_yyyyMMddHHmmss(DateUtil.AddDay(endDate, -365));
        logger.info("近一年开始时间={}", beginDateTime);
        this.circleLikeRanking(beginDateTime, endDateTime, 3);
        this.circleAnswerRanking(beginDateTime, endDateTime, 3);
        this.circleActivityRanking(beginDateTime, endDateTime, 3);


        logger.info("==========================计算排行榜结束==============================");
    }

    @Override
    public void circleLikeRanking(String beginTime, String endTime, int time){
        List<Map<String, Object>> list = queryDao.queryLikeCountList(beginTime, endTime, CourseConstant.RANKING_COUNT);
        this.assembleInsertList(list, 3, time);
    }

    @Override
    public void circleAnswerRanking (String beginTime, String endTime, int time){
        List<Map<String, Object>> list = queryDao.queryAnswerCountList(beginTime, endTime, CourseConstant.RANKING_COUNT);
        this.assembleInsertList(list, 2, time);
    }

    @Override
    public void circleActivityRanking(String beginTime, String endTime, int time) {
        queryDao.insertLikeScore2Temp(beginTime, endTime, CourseConstant.RANKING_BASE.get("1"), time);
        queryDao.insertBeLikeScore2Temp(beginTime, endTime, CourseConstant.RANKING_BASE.get("2"), time);
        queryDao.insertPushCircleScore2Temp(beginTime, endTime, CourseConstant.RANKING_BASE.get("3"), time);
        queryDao.insertCommentScore2Temp(beginTime, endTime, CourseConstant.RANKING_BASE.get("4"), time);
        queryDao.insertAcceptScore2Temp(beginTime, endTime, CourseConstant.RANKING_BASE.get("5"), time);

        List<Map<String, Object>> list = queryDao.queryActivityCountList(time, CourseConstant.RANKING_COUNT);
        this.assembleInsertList(list, 1, time);
    }



    @Override
    public  void  assembleInsertList (List<Map<String, Object>> list, int type, int time) {
        List<SchoolmateCircleRanking> insertList = new ArrayList<>();
        SchoolmateCircleRanking temp = null;
        if (list != null) {
            int count = 1;
            for (Map<String, Object> map : list){
                if (StringUtils.isNotBlank(map.get("user_id") + "")){
                    temp = new SchoolmateCircleRanking();
                    temp.setCount(new Double((Double.parseDouble(map.get("count") + ""))).longValue());
                    temp.setUserId(map.get("user_id") + "");
                    temp.setType(type);
                    temp.setTime(time);
                    temp.setRank(count);
                    count++;
                    insertList.add(temp);
                }
            }
            if (insertList.size() > 0){
                this.save(insertList);
            }
        }
    }
}
