package com.jiayue.insu.incloud.controller;

import cn.hutool.core.date.DateUtil;
import com.jiayue.insu.common.core.util.R;
import com.jiayue.insu.incloud.compermisson.QNHLComPermisson;
import com.jiayue.insu.incloud.entity.Quartz;
import com.jiayue.insu.incloud.entity.Station;
import com.jiayue.insu.incloud.pulldata.PullCorrectDataForQNHL;
import com.jiayue.insu.incloud.pulldata.PullInitData;
import com.jiayue.insu.incloud.pushdata.PushDataForQNHL;
import com.jiayue.insu.incloud.quartz.ScheduledTask;
import com.jiayue.insu.incloud.service.StationService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;

@RestController
@RequestMapping("/test")
public class TestController {
   @GetMapping
    public R test(){
       return R.ok("ok");
   }

    @Autowired
    ScheduledTask scheduledTask;
    @Autowired
    PullInitData pullInitData;

    @Autowired
    PushDataForQNHL pushDataForQNHL;
    @Autowired
    StationService stationService;
    @Autowired
    PullCorrectDataForQNHL pullCorrectDataForQNHL;

    @GetMapping("/job")
    public R testJob() throws ClassNotFoundException, SchedulerException, ParseException {
        scheduledTask.deleteJob("testJob2");
        Quartz quartz = new Quartz();
        quartz.setExecuteClass("com.jiayue.insu.incloud.job.data.pull.ZGHpullDataJob");
        quartz.setCronExpression("0 37 9 * * ?");
        quartz.setJobName("testJob2");
        quartz.setStartTime(DateUtil.current());
        Class c = Class.forName(quartz.getExecuteClass());
        scheduledTask.scheduleJob(c,quartz.getJobName(),quartz.getStartTime(),quartz.getCronExpression(),null);
        return R.ok();
    }



    @GetMapping("/job1")
    public R testJob1() throws ClassNotFoundException, SchedulerException, ParseException {
        Station station = new Station();
        station.setStationCode("J00219");
        station.setInCode("QNHL");
        pullInitData.pullDQAndNWPData(station);
        return R.ok();
    }



    @GetMapping("/init")
    public R testInit() {
        Station j00243 = stationService.findByStationCode("J00243");
        pullInitData.pullDQAndNWPData(j00243);
        return R.ok();
    }

    @GetMapping("/push")
    public R testPush()  {
        Station j00243 = stationService.findByStationCode("J00243");
        pushDataForQNHL.pushDQAndNWPData(j00243);
        return R.ok();
    }

    @GetMapping("/pull")
    public R testPull(){
        Station j00243 = stationService.findByStationCode("J00243");
        pullCorrectDataForQNHL.pullDQAndNWPData(j00243);
        return R.ok();
    }
    @Autowired
    QNHLComPermisson qnhlComPermisson;
    @GetMapping("/token")
    public R testToken(){
        Station j00243 = stationService.findByStationCode("J00243");
        qnhlComPermisson.generateKey(j00243);
        return R.ok();
    }
}
