package com.huatek.busi.controller;

import java.util.Map;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.huatek.busi.constant.ApiConstant.Tags;
import com.huatek.busi.dto.ScheduleJobDto;
import com.huatek.busi.dto.ScheduleJobLogDto;
import com.huatek.busi.entity.ScheduleJob;
import com.huatek.busi.service.job.ScheduleJobLogService;
import com.huatek.busi.service.job.ScheduleJobService;
import com.huatek.torch.frame.log.KerLog;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: ScheduleJobController
 * @FullClassPath: com.huatek.busi.controller.ScheduleJobController
 * @Description: 定时任务 控制层
 * @author: edward_luo
 * @date: Jul 15, 2019 09:53:13 AM
 * @version: 1.0
 */
@Slf4j
@Api(tags = {Tags.BUSI_SCHEDULEJOB})
@RestController
@RequestMapping("/api/ScheduleJob")
public class ScheduleJobController {
    @Reference(check = false)
    private ScheduleJobService scheduleJobService;
    @Reference(check = false)
    private ScheduleJobLogService scheduleJobLogService;

    /**
     * 定时任务 分页列表
     *
     * @param scheduleJobDto
     *            定时任务Dto
     * @return map map
     */
    @ApiOperation(value = "定时任务 分页列表")
    @KerLog(projectName = "PROJECTNAME", modelName = "SCHEDULEJOB.MODELNAME",
        methodName = "SCHEDULEJOB.METHODNAME.QUERYLIST")
    @PostMapping(value = "/queryList", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> queryList(@RequestBody ScheduleJobDto scheduleJobDto) {
        return scheduleJobService.findScheduleJobPage(scheduleJobDto);
    }

    /**
     * 添加定时任务
     *
     * scheduleJobDto 定时任务Dto
     * 
     * @return map map
     */
    @ApiOperation(value = "添加定时任务")
    @KerLog(projectName = "PROJECTNAME", modelName = "SCHEDULEJOB.MODELNAME",
        methodName = "SCHEDULEJOB.METHODNAME.ADDBUSITEMPERPOINT")
    @PostMapping(value = "/addScheduleJob", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> addScheduleJob(@RequestBody ScheduleJobDto scheduleJobDto) {
        Boolean flag = scheduleJobService.insertScheduleJob(scheduleJobDto);
        log.debug("addScheduleJob: {}", flag);
        return ResultUtil.put(flag ? ConstantUtil.REQUEST_SUCCESS : ConstantUtil.REQUEST_FAIL, "", "");
    }

    /**
     * 查询定时任务
     * 
     * @param scheduleJobDto
     * @return
     */
    @ApiOperation(value = "查询定时任务")
    @KerLog(projectName = "PROJECTNAME", modelName = "SCHEDULEJOB.MODELNAME",
        methodName = "SCHEDULEJOB.METHODNAME.UPDATESCHEDULEJOB")
    @PostMapping(value = "/findScheduleJobById/{id}", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> findScheduleJobById(@PathVariable(value = "id") String id) {
        ScheduleJob scheduleJob = scheduleJobService.findByPK(id);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", scheduleJob);
    }

    /**
     * 修改定时任务
     *
     * @param scheduleJobDto
     *            定时任务Dto
     * @return the map
     */
    @ApiOperation(value = "修改定时任务")
    @KerLog(projectName = "PROJECTNAME", modelName = "SCHEDULEJOB.MODELNAME",
        methodName = "SCHEDULEJOB.METHODNAME.UPDATESCHEDULEJOB")
    @PostMapping(value = "/updateScheduleJob", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> updateBusiTemperPoint(@RequestBody ScheduleJobDto scheduleJobDto) {
        Boolean flag = scheduleJobService.updateScheduleJob(scheduleJobDto);
        log.debug("updateScheduleJob: {}", flag);
        return ResultUtil.put(flag ? ConstantUtil.REQUEST_SUCCESS : ConstantUtil.REQUEST_FAIL, "", "");
    }

    /**
     * 批量删除定时任务
     *
     * @param String[]
     *            jobIds
     * @return map map
     */
    @ApiOperation(value = "删除定时任务")
    @KerLog(projectName = "PROJECTNAME", modelName = "SCHEDULEJOB.MODELNAME",
        methodName = "SCHEDULEJOB.METHODNAME.DELETEBATCHSCHEDULEJOB")
    @PostMapping(value = "/deleteBatchScheduleJob", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> deleteBatchScheduleJob(@RequestBody String[] jobIds) {
        scheduleJobService.deleteBatch(jobIds);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    /**
     * 暂停定时任务
     *
     * @param String[]
     *            jobIds
     * @return map map
     */
    @ApiOperation(value = "暂停定时任务")
    @KerLog(projectName = "PROJECTNAME", modelName = "SCHEDULEJOB.MODELNAME",
        methodName = "SCHEDULEJOB.METHODNAME.PAUSEBATCHSCHEDULEJOB")
    @PostMapping(value = "/pauseBatchScheduleJob", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> deleteBusiTemperPoint(@RequestBody String[] jobIds) {
        scheduleJobService.pause(jobIds);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    /**
     * 立即执行定时任务
     *
     * @param String[]
     *            jobIds
     * @return map map
     */
    @ApiOperation(value = "立即执行定时任务")
    @KerLog(projectName = "PROJECTNAME", modelName = "SCHEDULEJOB.MODELNAME",
        methodName = "SCHEDULEJOB.METHODNAME.RUNBATCHSCHEDULEJOB")
    @PostMapping(value = "/runBatchScheduleJob", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> runBatchScheduleJob(@RequestBody String[] jobIds) {
        scheduleJobService.run(jobIds);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    /**
     * 恢复定时任务
     *
     * @param String[]
     *            jobIds
     * @return map map
     */
    @ApiOperation(value = "恢复定时任务")
    @KerLog(projectName = "PROJECTNAME", modelName = "SCHEDULEJOB.MODELNAME",
        methodName = "SCHEDULEJOB.METHODNAME.RESUMEBATCHSCHEDULEJOB")
    @PostMapping(value = "/resumeBatchScheduleJob", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> resumeBatchScheduleJob(@RequestBody String[] jobIds) {
        scheduleJobService.resume(jobIds);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    /**
     * 定时任务日志分页列表
     *
     * @param scheduleJobLogDto
     *            定时任务日志Dto
     * @return map map
     */
    @ApiOperation(value = "定时任务日志分页列表")
    @KerLog(projectName = "PROJECTNAME", modelName = "SCHEDULEJOB.MODELNAME",
        methodName = "SCHEDULEJOB.METHODNAME.SCHEDULEJOBLOGLIST")
    @PostMapping(value = "/scheduleJobLogList", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> scheduleJobLogList(@RequestBody ScheduleJobLogDto scheduleJobLogDto) {
        return scheduleJobLogService.findScheduleJobLogPage(scheduleJobLogDto);
    }
}
