package com.vip.saturn.job.console.controller.gui;

import com.vip.saturn.job.console.aop.annotation.Audit;
import com.vip.saturn.job.console.aop.annotation.AuditParam;
import com.vip.saturn.job.console.controller.SuccessResponseEntity;
import com.vip.saturn.job.console.domain.RequestResult;
import com.vip.saturn.job.console.exception.SaturnJobConsoleException;
import com.vip.saturn.job.console.service.JobService;
import com.vip.saturn.job.console.utils.PermissionKeys;
import com.vip.saturn.job.console.vo.UpdateJobConfigVo;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Job config related operations.
 *
 * @author hebelala
 */
@RequestMapping("/console/namespaces/{namespace:.+}/jobs/{jobName}/config")
public class JobConfigController extends AbstractGUIController {

	@Resource
	private JobService jobService;

	@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
	@GetMapping
	public SuccessResponseEntity getJobConfig(final HttpServletRequest request,
			@AuditParam("namespace") @PathVariable String namespace,
			@AuditParam("jobName") @PathVariable String jobName) throws SaturnJobConsoleException {
		return new SuccessResponseEntity(jobService.getJobConfigVo(namespace, jobName));
	}

	@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
	@Audit
	@PostMapping
	public SuccessResponseEntity updateJobConfig(final HttpServletRequest request,
			@AuditParam("namespace") @PathVariable String namespace,
			@AuditParam("jobName") @PathVariable String jobName, UpdateJobConfigVo updateJobConfigVo)
			throws SaturnJobConsoleException {
		assertIsPermitted(PermissionKeys.jobUpdate, namespace);
		if(updateJobConfigVo.getGroups().length() >= 255) {
			throw new SaturnJobConsoleException("分组总数不能超过15个");
		}
		jobService.updateJobConfig(namespace, updateJobConfigVo.toJobConfig(), getCurrentLoginUserName());
		return new SuccessResponseEntity();
	}

	@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
	@Audit
	@PostMapping("/runAtOnce")
	public SuccessResponseEntity runAtOnce(@AuditParam("namespace") @PathVariable String namespace,
			@AuditParam("jobName") @PathVariable String jobName) throws SaturnJobConsoleException {
		assertIsPermitted(PermissionKeys.jobRunAtOnce, namespace);
		jobService.runAtOnce(namespace, jobName);
		return new SuccessResponseEntity();
	}

	@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
	@Audit
	@PostMapping("/stopAtOnce")
	public SuccessResponseEntity stopAtOnce(@AuditParam("namespace") @PathVariable String namespace,
			@AuditParam("jobName") @PathVariable String jobName) throws SaturnJobConsoleException {
		assertIsPermitted(PermissionKeys.jobStopAtOnce, namespace);
		jobService.stopAtOnce(namespace, jobName);
		return new SuccessResponseEntity();
	}

}

@@ -2089,11 +2089,24 @@
 ;%0A%09%09
-if(
+String groups = 
 upda
@@ -2130,16 +2130,48 @@
 Groups()
+;%0A%09%09if (groups != null && groups
 .length(
@@ -2173,17 +2173,16 @@
 ngth() %3E
-=
  255) %7B%0A
@@ -2227,17 +2227,21 @@
 (%22%E5%88%86%E7%BB%84
-%E6%80%BB%E6%95%B0
+%E8%BF%87%E9%95%BF%EF%BC%8C
 %E4%B8%8D%E8%83%BD%E8%B6%85%E8%BF%87
-15%E4%B8%AA
+255%E4%B8%AA%E5%AD%97%E7%AC%A6
 %22);%0A

#654 fix it: NullPointException