package com.yihu.wlyy.job.consult;

import com.yihu.wlyy.entity.consult.ConsultTeam;
import com.yihu.wlyy.entity.consult.ConsultTeamDoctor;
import com.yihu.wlyy.entity.doctor.profile.Doctor;
import com.yihu.wlyy.job.QuartzHelper;
import com.yihu.wlyy.repository.consult.ConsultTeamDao;
import com.yihu.wlyy.repository.consult.ConsultTeamDoctorDao;
import com.yihu.wlyy.repository.doctor.DoctorDao;
import com.yihu.wlyy.service.quota.JobService;
import com.yihu.wlyy.util.DateUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import java.math.BigDecimal;
import java.util.*;

import static com.yihu.wlyy.job.consult.ConsultCleanerJob.ConsultTerminatorJobKey;

/**
 * @author Sand
 * @since 2016/9/26
 */
@Service
public class EvaluateScoreJob implements Job {
    @Autowired
    ConsultTeamDoctorDao consultTeamDoctorDao;
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    DoctorDao doctorDao;

    @Transactional
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            System.out.println("calculate evaluate job start");
            String doctorsSql = "select w.doctor from wlyy_evaluate w group by w.doctor";
            String sql = "select sum(w.score) as score from wlyy_evaluate_score w where w.doctor =? ";
            String evaluateTimesSql = "select count(1) as count from wlyy_evaluate_score w where w.doctor =? GROUP BY w.doctor";
            List<Map<String, Object>> doctorList = jdbcTemplate.queryForList(doctorsSql);
            for (Map<String, Object> doctorMap : doctorList) {
                //获取医生的所有评价信息
                String doctor = doctorMap.get("doctor").toString();
                //统计总的分数
                List<Map<String, Object>> evaluateList = jdbcTemplate.queryForList(sql, new Object[]{doctor});
                //统计评价次数
                List<Map<String, Object>> evaluateTimes = jdbcTemplate.queryForList(evaluateTimesSql, new Object[]{doctor});
                if (evaluateTimes != null && evaluateTimes.size() > 0 && evaluateList != null && evaluateList.size() > 0) {
                    //计算完毕
                    BigDecimal amount = new BigDecimal(evaluateList.get(0).get("score").toString());
                    BigDecimal count = new BigDecimal(evaluateTimes.get(0).get("count").toString());
                    BigDecimal avg = amount.divide(count, 1, BigDecimal.ROUND_HALF_UP);
                    BigDecimal coefficient ;
                    if(count.compareTo(new BigDecimal(100))==-1&&count.compareTo(new BigDecimal(100))==0){
                        //如果数目小于等于100,系数为3
                        coefficient = new BigDecimal(3);
                    }else if(count.compareTo(new BigDecimal(1000))==1){
                        //如果数量大于1000,系数为1
                        coefficient = new BigDecimal(1);
                    }else{
                        //系数为2
                        coefficient = new BigDecimal(2);
                    }
                    //评分=80+（平均分-80）/系数
                    BigDecimal score = new BigDecimal(80).add((avg.subtract(new BigDecimal(80)).divide(coefficient,1,BigDecimal.ROUND_HALF_UP)));
                    Doctor d = doctorDao.findByCode(doctor);
                    if (d != null) {
                        d.setEvaluateScore(score.doubleValue());
                        doctorDao.save(d);
                    }
                }
            }
            System.out.println("calculate evaluate job end");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("calculate evaluate job failed");
        }
    }


}
