package com.crunii.micro.service.sysmgr.controller;

import com.crunii.micro.common.dto.PageReq;
import com.crunii.micro.common.dto.PageRsp;
import com.crunii.micro.common.dto.Result;
import com.crunii.micro.domain.entity.main.TsmUrl;
import com.crunii.micro.domain.entity.main.TsmUrlGroup;
import com.crunii.micro.service.sysmgr.dto.sysmgr.*;
import com.crunii.micro.service.sysmgr.service.UrlService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 田平 create 2023-02-21 10:48:41
 */
@Api(value = "Url相关")
@Controller
@Slf4j
@RequestMapping("/auth/UrlController")
public class UrlController {

	private final UrlService urlService;

	public UrlController(UrlService urlService) {
		this.urlService = urlService;
	}

	@ApiOperation(value = "URL分页")
	@RequestMapping(path = "/pageUrl", method = RequestMethod.POST)
	@ResponseBody
	public Result<PageRsp<TsmUrl>> pageUrl(@RequestBody @ApiParam PageReq<UrlReq> req) {
		log.info("req:" + req.toString());
		Result<PageRsp<TsmUrl>> result = new Result<>();
		result.setData(urlService.pageUrl(req));
		return result;
	}

	@ApiOperation(value = "创建Url")
	@RequestMapping(path = "/createUrl", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> createUrl(@RequestBody @ApiParam UrlC urlC) {
		Result<String> result = new Result<>();
		urlService.createUrl(urlC);
		return result;
	}

	@ApiOperation(value = "更新Url")
	@RequestMapping(path = "/updateUrl", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> updateUrl(@RequestBody @ApiParam UrlU urlU) {
		Result<String> result = new Result<>();
		urlService.updateUrl(urlU);
		return result;
	}

	@ApiOperation(value = "URL组分页")
	@RequestMapping(path = "/pageUrlGroup", method = RequestMethod.POST)
	@ResponseBody
	public Result<PageRsp<TsmUrlGroup>> pageUrlGroup(@RequestBody @ApiParam PageReq<UrlGroupReq> req) {
		log.info("req:" + req.toString());
		Result<PageRsp<TsmUrlGroup>> result = new Result<>();
		result.setData(urlService.pageUrlGroup(req));
		return result;
	}

	@ApiOperation(value = "创建Url组")
	@RequestMapping(path = "/createUrlGroup", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> createUrlGroup(@RequestBody @ApiParam UrlGroupC urlGroupC) {
		Result<String> result = new Result<>();
		urlService.createUrlGroup(urlGroupC);
		return result;
	}

	@ApiOperation(value = "更新Url组")
	@RequestMapping(path = "/updateUrlGroup", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> updateUrlGroup(@RequestBody @ApiParam UrlGroupU urlGroupU) {
		Result<String> result = new Result<>();
		urlService.updateUrlGroup(urlGroupU);
		return result;
	}

	@ApiOperation(value = "Url组关联Url")
	@RequestMapping(path = "/doUrlGroupRelaUrl", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> doUrlGroupRelaUrl(@RequestBody @ApiParam UrlGroupRelaUrl urlGroupRelaUrl) {
		Result<String> result = new Result<>();
		urlService.doUrlGroupRelaUrl(urlGroupRelaUrl);
		return result;
	}

	@ApiOperation(value = "获取url组拥有的url")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "groupId", value = "组Id", required = true)
	})
	@RequestMapping(path = "/getUrlsOfGroup", method = RequestMethod.GET)
	@ResponseBody
	public Result<List<TsmUrl>> getUrlsOfGroup(int groupId) {
		Result<List<TsmUrl>> result = new Result<>();
		result.setData(urlService.getUrlsOfGroup(groupId));
		return result;
	}

	@ApiOperation(value = "获取Url组的url的CheckBox列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "groupId", value = "Url组ID", required = true)
	})
	@RequestMapping(path = "/getCheckedUrlGroupUrl", method = RequestMethod.GET)
	@ResponseBody
	public Result<List<UrlGroupUrlCB>> getCheckedUrlGroupUrl(int groupId) {
		Result<List<UrlGroupUrlCB>> result = new Result<>();
		result.setData(urlService.getCheckedUrlGroupUrl(groupId));
		return result;
	}

	@ApiOperation(value = "通过urlId获取url")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "urlId", value = "urlId", required = true)
	})
	@RequestMapping(path = "/getUrl", method = RequestMethod.GET)
	@ResponseBody
	public Result<TsmUrl> getUrl(int urlId) {
		Result<TsmUrl> result = new Result<>();
		result.setData(urlService.getTsmUrl(urlId));
		return result;
	}

}
