package com.yihu.wlyy.web.quota;

import com.yihu.wlyy.job.QuartzHelper;
import com.yihu.wlyy.job.consult.ConsultCleanerJob;
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.quota.JobService;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
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;

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

/**
 * 任务启动
 *
 * @author chenweida
 */
@RestController
@RequestMapping(value = "/job", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "后台-任务控制")
public class JobController extends BaseController {
    private final JobService jobService;
    private final PatientDiseaseService diseaseService;
    private final StringRedisTemplate redisTemplate;
    private final QuartzHelper quartzHelper;
    private final DoctorWorkTimeService workTimeService;
    private final StatisticsService statisticsService;

    @Autowired
    public JobController(StatisticsService statisticsService, JobService jobService, StringRedisTemplate redisTemplate, DoctorWorkTimeService workTimeService, PatientDiseaseService diseaseService, QuartzHelper quartzHelper) {
        this.statisticsService = statisticsService;
        this.jobService = jobService;
        this.redisTemplate = redisTemplate;
        this.workTimeService = workTimeService;
        this.diseaseService = diseaseService;
        this.quartzHelper = quartzHelper;
    }

    /**
     * 启动任务
     *
     * @param id id
     * @return
     */
    @RequestMapping(value = "startNowById", method = RequestMethod.GET)
    public String startNowById(String id) {
        try {
            jobService.startNowById(id);
            return success("启动成功！");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }

    /**
     * 生成过去几天的数据
     *
     * @param day
     * @return
     */
    @RequestMapping(value = "productDataByDay", method = RequestMethod.GET)
    public String productDataByDay(Integer day) {
        try {
            jobService.productDataByDay(day);
            return success("启动成功！");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }

    /**
     * 生成过去某一天的全部的数据
     *
     * @param day
     * @return
     */
    @RequestMapping(value = "productDataByOneDay", method = RequestMethod.GET)
    public String productDataByOneDay(String day) {
        try {
            jobService.productDataByOneDay(day);
            return success("启动成功！");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }

    /**
     * 生成过去某一天的某一个指标的数据
     *
     * @param day
     * @return
     */
    @RequestMapping(value = "productDataByOneDayWithId", method = RequestMethod.GET)
    public String productDataByOneDayWithId(String day, String id) {
        try {
            jobService.productDataByOneDayWithId(day, id);
            return success("启动成功！");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }

    /**
     * 启动任务
     *
     * @param id id
     * @return
     */
    @RequestMapping(value = "startById", method = RequestMethod.GET)
    public String startById(String id) {
        try {

            jobService.startById(id);
            return success("启动成功！");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }

    /**
     * 停止任务
     *
     * @param id id
     * @return
     */
    @RequestMapping(value = "stopById", method = RequestMethod.GET)
    public String stopById(String id) {
        try {
            jobService.stopById(id);
            return success("停止成功！");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }

    /**
     * 停止所有任务
     *
     * @return
     */
    @RequestMapping(value = "stopAll", method = RequestMethod.GET)
    public String stopAll() {
        try {
            jobService.stopAll();
            return success("停止成功！");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }

    /**
     * 启动所有任务
     *
     * @return
     */
    @RequestMapping(value = "startAll", method = RequestMethod.GET)
    public String startAll() {
        try {
            jobService.startAll();
            return success("启动成功！");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }

    @RequestMapping(value = "startaaaa", method = RequestMethod.GET)
    public String startaaaa() {
        try {
            jobService.startaaaa();
            return success("启动成功！");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }

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

    /**
     * 将患者疾病到数据库
     *
     * @return
     */
    @RequestMapping(value = "/disease/to_disease", method = RequestMethod.GET)
    public String updateToDisease() {
        try {
            diseaseService.updateToDisease();
            return write(200, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "更新失败");
        }
    }

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

    /**
     * 开始名医咨询剩余次数统计任务
     *
     * @return
     */
    @RequestMapping(value = "/famous_doctor/start_job", method = RequestMethod.GET)
    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) {
            error(e);
            return error(-1, "启动失败");
        }
    }

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

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

    /**
     * 添加患者咨询超时未响应关闭任务。
     */
    @RequestMapping(value = "/consult_auto_termination", method = RequestMethod.POST)
    @ApiOperation("添加超时咨询自动关闭任务")
    public String addConsultJob() {
        try {
            quartzHelper.startNow(ConsultCleanerJob.class, ConsultTerminatorJobKey, null);
            return write(200, "");
        } catch (Exception e) {
            error(e);
            return error(-1, e.getMessage());
        }
    }
}
