package com.yihu.wlyy.web.common.util;

import com.yihu.wlyy.job.QuartzHelper;
import com.yihu.wlyy.job.consult.FamousConsultTimesJob;
import com.yihu.wlyy.service.app.disease.PatientDiseaseService;
import com.yihu.wlyy.service.app.scheduling.DoctorWorkTimeService;
import com.yihu.wlyy.service.app.statistics.StatisticsService;
import com.yihu.wlyy.service.common.util.ManageUtilService;
import com.yihu.wlyy.service.common.util.SignTeamAndGroupRunnable;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.hash.HashMapper;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by lyr on 2016/09/13.
 */
@RestController
@RequestMapping(value = "/manage_util", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "管理工具及后台任务控制")
public class ManagerUtilController extends BaseController {

    @Autowired
    PatientDiseaseService diseaseService;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    QuartzHelper quartzHelper;
    @Autowired
    DoctorWorkTimeService workTimeService;
    @Autowired
    StatisticsService statisticsService;
    @Autowired
    ManageUtilService manageUtilService;

    /*********************************************患者疾病相关******************************************/
    /**
     * 更新到redis
     *
     * @return
     */
    @RequestMapping(value = "/disease/to_redis")
    public String updateToRedid() {
        try {
            diseaseService.updateToRedis();
            return write(200, "更新成功");
        } catch (Exception e) {
            return error(-1, "更新失败");
        }
    }

    /**
     * 更新到疾病表
     *
     * @return
     */
    @RequestMapping(value = "/disease/to_disease")
    public String updateToDisease() {
        try {
            diseaseService.updateToDisease();
            return write(200, "更新成功");
        } catch (Exception e) {
            return error(-1, "更新失败");
        }
    }

    /**
     * 查询患者redis疾病
     *
     * @param patient
     * @return
     */
    @RequestMapping(value = "/disease/patient")
    public String getDiseaseFromRedis(String patient) {
        try {
            return write(200, "查询成功", "data", redisTemplate.opsForValue().get("disease:" + patient));
        } catch (Exception e) {
            return error(-1, "查询失败");
        }
    }

    /***********************************************名医咨询相关****************************************/
    /**
     * 开始名医咨询剩余次数统计任务
     *
     * @return
     */
    @RequestMapping(value = "/famous_doctor/start_job")
    public String startConsultTimesJob() {
        try {
            if (!quartzHelper.isExistJob("famous-doctor-times")) {
                quartzHelper.addJob(FamousConsultTimesJob.class, "0 0 0 * * ?", "famous-doctor-times", new HashMap<String, Object>());
                return write(200, "启动成功");
            } else {
                return write(200, "任务已存在");
            }
        } catch (Exception e) {
            return error(-1, "启动失败");
        }
    }

    /**
     * 名医咨询剩余次数手动更新
     *
     * @return
     */
    @RequestMapping(value = "/famous_doctor/times_update")
    public String famousConsultTimeUpdate() {
        try {
            workTimeService.consultTimesRemain(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            return write(200, "更新成功");
        } catch (Exception e) {
            return error(-1, "更新失败");
        }
    }

    /*********************************************统计相关*******************************************/
    /**
     * 更新人口数据到redis
     *
     * @return
     */
    @RequestMapping(value = "/people_num_to_redis")
    public String peopleNumToRedis() {
        try {
            statisticsService.peopleNumToRedis();
            return write(200, "更新成功");
        } catch (Exception e) {
            return error(-1, "更新失败");
        }
    }

    /**
     * 签约同步数据设置团队和分组
     *
     * @return
     */
    @RequestMapping(value = "/sign_set_group_team")
    public String signSetGroupAndTeam(){
        try {
            manageUtilService.signPatientSetGroup();
            return write(200, "更新成功");
        } catch (Exception e) {
            return error(-1, "更新失败");
        }
    }

    /**
     * 签约同步数据设置团队和分组
     *
     * @return
     */
    @RequestMapping(value = "/sign_set_group_team_page")
    public String signSetGroupAndTeamPage(int page){
        try {
            manageUtilService.setSignTeamAndGroupByPage(page);
            return write(200, "更新成功");
        } catch (Exception e) {
            return error(-1, "更新失败");
        }
    }

    /**
     * 签约同步数据设置团队和分组
     *
     * @return
     */
    @RequestMapping(value = "/sign_set_group_team_id")
    public String signSetGroupAndTeamId(Long id){
        try {
            manageUtilService.setSignTeamAndGroupById(id);
            return write(200, "更新成功");
        } catch (Exception e) {
            return error(-1, "更新失败");
        }
    }

    /**
     * 签约同步数据设置团队和分组
     *
     * @return
     */
    @RequestMapping(value = "/sign_error_page_id")
    public String getErrorPage(int type){
        try {
            if(type == 1){
                return write(200, "查询成功","data",ManageUtilService.page);
            }else if(type == 2){
                return write(200, "查询成功","data",ManageUtilService.errorPages);
            } else{
                return write(200, "查询成功","data",ManageUtilService.errorSigns);
            }
        } catch (Exception e) {
            return error(-1, "查询失败");
        }
    }

    /**
     * 签约同步数据设置团队和分组
     *
     * @return
     */
    @RequestMapping(value = "/sign_team_group_thread")
    public String getErrorPage(long start,long end){
        try {
            new Thread(new SignTeamAndGroupRunnable(start,end)).start();
            return write(200,"启动成功");
        } catch (Exception e) {
            return error(-1, "启动失败");
        }
    }

}
