package com.multictrl.modules.job.controller;

import com.multictrl.common.annotation.ApiOrder;
import com.multictrl.common.annotation.DataFilter;
import com.multictrl.common.annotation.LogOperation;
import com.multictrl.common.constant.Constant;
import com.multictrl.common.page.PageData;
import com.multictrl.common.utils.Result;
import com.multictrl.common.validator.ValidatorUtils;
import com.multictrl.common.validator.group.AddGroup;
import com.multictrl.common.validator.group.DefaultGroup;
import com.multictrl.common.validator.group.UpdateGroup;
import com.multictrl.modules.job.dto.ScheduleJobDTO;
import com.multictrl.modules.job.service.ScheduleJobService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 定时任务
 *
 * @author Sdy
 */
@RestController
@RequestMapping("/sys/schedule")
@Tag(name = "计划飞行")
@ApiOrder(19)
@RequiredArgsConstructor
public class ScheduleJobController {
    private final ScheduleJobService scheduleJobService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数"),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段"),
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)"),
            @Parameter(name = "beanName", description = "beanName")
    })
    @DataFilter(tableAlias = "s")
    @RequiresPermissions("sys:schedule:page")
    public Result<PageData<ScheduleJobDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<ScheduleJobDTO> page = scheduleJobService.page(params);

        return new Result<PageData<ScheduleJobDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @RequiresPermissions("sys:schedule:info")
    public Result<ScheduleJobDTO> info(@PathVariable("id") Long id) {
        ScheduleJobDTO schedule = scheduleJobService.get(id);

        return new Result<ScheduleJobDTO>().ok(schedule);
    }

    @PostMapping
    @Operation(summary = "保存")
    @LogOperation("新增计划")
    @RequiresPermissions("sys:schedule:save")
    public Result<String> save(@RequestBody ScheduleJobDTO dto) {
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        scheduleJobService.save(dto);

        return new Result<String>();
    }

    @PutMapping
    @Operation(summary = "修改")
    @LogOperation("修改计划")
    @RequiresPermissions("sys:schedule:update")
    public Result<String> update(@RequestBody ScheduleJobDTO dto) {
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        scheduleJobService.update(dto);

        return new Result<String>();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除计划")
    @RequiresPermissions("sys:schedule:delete")
    public Result<String> delete(@RequestBody Long[] ids) {
        scheduleJobService.deleteBatch(ids);

        return new Result<String>();
    }

    @PutMapping("/run")
    @Operation(summary = "立即执行")
    @LogOperation("立即执行")
    @RequiresPermissions("sys:schedule:run")
    public Result<String> run(@RequestBody Long[] ids) {
        scheduleJobService.run(ids);

        return new Result<String>();
    }

    @PutMapping("/pause")
    @Operation(summary = "暂停")
    @LogOperation("暂停计划")
    @RequiresPermissions("sys:schedule:pause")
    public Result<String> pause(@RequestBody Long[] ids) {
        scheduleJobService.pause(ids);

        return new Result<String>();
    }

    @PutMapping("/resume")
    @Operation(summary = "恢复")
    @LogOperation("恢复计划")
    @RequiresPermissions("sys:schedule:resume")
    public Result<String> resume(@RequestBody Long[] ids) {
        scheduleJobService.resume(ids);

        return new Result<String>();
    }

}
