package com.xunhong.erp.turbo.admin.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.xunhong.erp.turbo.api.rbac.api.MenuServiceI;
import com.xunhong.erp.turbo.api.rbac.dto.cmd.MenuCreateCmd;
import com.xunhong.erp.turbo.api.rbac.dto.cmd.MenuDestroyCmd;
import com.xunhong.erp.turbo.api.rbac.dto.cmd.MenuDragCmd;
import com.xunhong.erp.turbo.api.rbac.dto.cmd.MenuUpdateCmd;
import com.xunhong.erp.turbo.api.rbac.dto.constants.PermissionConstant;
import com.xunhong.erp.turbo.api.rbac.dto.qry.MenuListQry;
import com.xunhong.erp.turbo.api.rbac.dto.qry.MenuShowQry;
import com.xunhong.erp.turbo.api.rbac.dto.qry.MenuTreeQry;
import com.xunhong.erp.turbo.api.rbac.dto.vo.MenuVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author shenyifei
 */
@Tag(name = "Menu", description = "应用菜单")
@RestController("operationMenuController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class MenuController {
	@DubboReference(version = "1.0.0")
	private final MenuServiceI menuService;

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_MENU_VIEW})
	@GetMapping("/treeMenu")
	@Operation(summary = "菜单列表", method = "GET")
	public MultiResponse<Tree<Long>> treeMenu(@ModelAttribute @Validated MenuTreeQry menuTreeQry) {
		return MultiResponse.of(menuService.tree(menuTreeQry));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_MENU_VIEW})
	@GetMapping("/listMenu")
	@Operation(summary = "菜单列表", method = "GET")
	public MultiResponse<MenuVO> listMenu(@ModelAttribute @Validated MenuListQry menuListQry) {
		return MultiResponse.of(menuService.list(menuListQry));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_MENU_CREATE})
	@PostMapping("/createMenu")
	@Operation(summary = "创建菜单", method = "POST")
	public SingleResponse<MenuVO> createMenu(@RequestBody @Validated MenuCreateCmd menuCreateCmd) {
		return SingleResponse.of(menuService.create(menuCreateCmd));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_MENU_VIEW})
	@GetMapping("/showMenu")
	@Operation(summary = "菜单详情", method = "GET")
	public SingleResponse<MenuVO> showMenu(@ModelAttribute @Validated MenuShowQry menuShowQry) {
		return SingleResponse.of(menuService.show(menuShowQry));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_MENU_UPDATE})
	@RequestMapping(value = "/updateMenu", method = {RequestMethod.PATCH, RequestMethod.PUT})
	@Operation(summary = "菜单更新", method = "PATCH")
	public SingleResponse<MenuVO> updateMenu(@RequestBody @Validated MenuUpdateCmd menuUpdateCmd) {
		return SingleResponse.of(menuService.update(menuUpdateCmd));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_MENU_DELETE})
	@DeleteMapping("/destroyMenu")
	@Operation(summary = "菜单更新", method = "DELETE")
	public Response destroyMenu(@RequestBody @Validated MenuDestroyCmd menuDestroyCmd) {
		menuService.destroy(menuDestroyCmd);
		return Response.buildSuccess();
	}

	@SaCheckLogin
	@PostMapping("dragMenu")
	@Operation(summary = "菜单拖拽", method = "POST")
	public Response dragMenu(@RequestBody @Validated MenuDragCmd menuDragCmd) {
		menuService.drag(menuDragCmd);
		return Response.buildSuccess();
	}
}
