package tech.easyflow.admin.controller.job;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.date.DateUtil;
import org.quartz.CronExpression;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.easyflow.common.domain.Result;
import tech.easyflow.common.satoken.util.SaTokenUtil;
import tech.easyflow.common.web.controller.BaseCurdController;
import tech.easyflow.job.entity.SysJob;
import tech.easyflow.job.service.SysJobService;

import tech.easyflow.common.entity.LoginAccount;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 系统任务表 控制层。
 *
 * @author xiaoma
 * @since 2025-05-20
 */
@RestController
@RequestMapping("/api/v1/sysJob")
public class SysJobController extends BaseCurdController<SysJobService, SysJob> {
    public SysJobController(SysJobService service) {
        super(service);
    }

    @GetMapping("/start")
    @SaCheckPermission("/api/v1/sysJob/save")
    public Result<Void> start(BigInteger id) {
        service.startJob(id);
        return Result.ok();
    }

    @GetMapping("/stop")
    @SaCheckPermission("/api/v1/sysJob/save")
    public Result<Void> stop(BigInteger id) {
        service.stopJob(id);
        return Result.ok();
    }

    @GetMapping("/getNextTimes")
    public Result<List<String>> getNextTimes(String cronExpression) throws  Exception{
        CronExpression ex = new CronExpression(cronExpression);
        List<String> times = new ArrayList<>();
        Date date = new Date();
        for (int i = 0; i < 5; i++) {
            Date next = ex.getNextValidTimeAfter(date);
            times.add(DateUtil.formatDateTime(next));
            date = next;
        }
        return Result.ok(times);
    }

    @Override
    protected Result onSaveOrUpdateBefore(SysJob entity, boolean isSave) {
        LoginAccount loginUser = SaTokenUtil.getLoginAccount();
        if (isSave) {
            commonFiled(entity,loginUser.getId(),loginUser.getTenantId(), loginUser.getDeptId());
        } else {
            entity.setModified(new Date());
            entity.setModifiedBy(loginUser.getId());
        }
        return super.onSaveOrUpdateBefore(entity, isSave);
    }

    @Override
    protected Result onRemoveBefore(Collection<Serializable> ids) {
        service.deleteJob(ids);
        return super.onRemoveBefore(ids);
    }
}
