package com.bocloud.ims.booter.controller.scheduler;

import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.ims.entity.params.ServerTaskParam;
import com.bocloud.ims.interfaces.scheduler.ServerTimedTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * X86_SERVER 服务器定时同步controller
 *
 * @author ymh
 * @时间 2019年12月27日
 */
@RestController
@RequestMapping("/v1/server/task")
@Api(value = "服务器定时同步管理", tags = "服务器定时同步管理")
public class ServerTimedTaskController {

	@Autowired
	private ServerTimedTaskService serverTimedTaskService;

	/**
	 * 添加定时任务
	 *
	 * @param context
	 * @return
	 */
	@PostMapping
	@ApiOperation(tags = { "CMC", "CSC" }, value = "定时任务管理")
	public BocloudResult create(@RequestBody ServerTaskParam param, @Value(Common.REQ_CONTEXT) RequestContext context) {
		if (param.getAssetCategory() == null || param.getCron() == null) {
			return new BocloudResult<>(false, "参数传入错误，assetCategory或cron为空");
		}
		return serverTimedTaskService.create(param.getAssetCategory(), param.getCron(), context);
	}

	/**
	 * 更新平台定时任务
	 *
	 * @param assetCategory
	 * @param cron
	 * @param context
	 * @return
	 */
	@PutMapping
	@ApiOperation(tags = { "CMC", "CSC" }, value = "平台定时管理")
	public BocloudResult modify(@RequestParam(value = "assetCategory") String assetCategory,
			@RequestParam(value = "cron") String cron, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return serverTimedTaskService.modify(assetCategory, cron, context);
	}

	/**
	 * 获取定时表达式cron
	 *
	 * @param assetCategory
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "获取定时表达式")
	@GetMapping
	public BocloudResult serverCron(@RequestParam(value = "assetCategory") String assetCategory,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return serverTimedTaskService.serverCron(assetCategory, context);
	}
}
