package com.xintong.visualinspection.service;

import com.xintong.visualinspection.bean.*;
import com.xintong.visualinspection.dao.master.CheckDeptStatisticsDao;
import com.xintong.visualinspection.dao.master.CheckOtherDao;
import com.xintong.visualinspection.dao.master.CheckTaskPeriodDao;
import com.xintong.visualinspection.dao.master.CheckUserStatisticsDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文件名：ThreadTaskService
 * 版本信息：日期：2018/2/5 Copyright 江苏省交通规划设计院 Corporation 2018 版权所有.
 */
@Service
public class ThreadTaskService {

    @Autowired
    private CheckTaskPeriodDao checkTaskPeriodDao;

    @Autowired
    private CheckUserStatisticsDao checkUserStatisticsDao;

    @Autowired
    private CheckDeptStatisticsDao checkDeptStatisticsDao;

    @Async("threadExecutor")
    public void saveUserStatic(List<StatisticsBo> statisticsBos, Long starttime,Long endtime){

        List<CheckTaskPeriod> checkTaskPeriods = checkTaskPeriodDao.select(new CheckTaskPeriod());

        CheckTaskPeriod period = null;
        // 计算上个周期时间 周期时间：00:00:00 ----- 00:00:00 参数：23:00:00
        for (CheckTaskPeriod p : checkTaskPeriods) {
            if (starttime + 1 * 1000 * 60 * 60 == p.getStarttime().getTime() && endtime + 1 * 1000 * 60 * 60 == p.getEndtime().getTime()) {
                period = p;
                break;
            }
        }
        if(period==null) { return;}
        for (StatisticsBo s:statisticsBos
                ) {
            UserStatistic ss =new UserStatistic();
            ss.setUser_id(s.getUser().getId());
            ss.setUser_score(s.getCheck_all_score());
            ss.setUser_check_num(s.getChecked_num());
            ss.setUser_ranking_c(s.getCompany_ranking());
            ss.setUser_ranking_d(s.getCenter_ranking());
            ss.setUser_ranking_s(s.getFee_station_ranking());
            ss.setDept_id(s.getUser().getOrganid());
            ss.setPeriod_id(period.getId());
            checkUserStatisticsDao.saveandupdate(ss);
        }
    }

    @Async("threadExecutor")
    public void saveDeptStatic(List<StatisticsBean> statisticsBos, Long starttime, Long endtime){

        List<CheckTaskPeriod> checkTaskPeriods = checkTaskPeriodDao.select(new CheckTaskPeriod());

        CheckTaskPeriod period = null;
        // 计算上个周期时间 周期时间：00:00:00 ----- 00:00:00 参数：23:00:00
        for (CheckTaskPeriod p : checkTaskPeriods) {
            if (starttime + 1 * 1000 * 60 * 60 == p.getStarttime().getTime() && endtime + 1 * 1000 * 60 * 60 == p.getEndtime().getTime()) {
                period = p;
                break;
            }
        }
        if(period==null) { return;}
        for (StatisticsBean s:statisticsBos
                ) {
            DeptStatistic ss =new DeptStatistic();
            ss.setScore(Double.parseDouble(s.getScore_avg()));
            ss.setIntegral(statisticsBos.size()-s.getCompany_ranking2()+1);
            ss.setDept_id(s.getDept_id());
            ss.setPeriod_id(period.getId());
            checkDeptStatisticsDao.saveandupdate(ss);
        }
    }
}
