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

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.xunhong.erp.turbo.api.rbac.api.PermissionServiceI;
import com.xunhong.erp.turbo.api.rbac.dto.cmd.PermissionCreateCmd;
import com.xunhong.erp.turbo.api.rbac.dto.cmd.PermissionDestroyCmd;
import com.xunhong.erp.turbo.api.rbac.dto.cmd.PermissionUpdateCmd;
import com.xunhong.erp.turbo.api.rbac.dto.constants.PermissionConstant;
import com.xunhong.erp.turbo.api.rbac.dto.qry.PermissionListQry;
import com.xunhong.erp.turbo.api.rbac.dto.qry.PermissionPageQry;
import com.xunhong.erp.turbo.api.rbac.dto.qry.PermissionShowQry;
import com.xunhong.erp.turbo.api.rbac.dto.vo.PermissionVO;
import com.xunhong.erp.turbo.base.dto.PageDTO;
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.*;

@Tag(name = "Permission", description = "权限管理")
@RestController("operationPermissionController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class PermissionController {

	@DubboReference(version = "1.0.0")
	private final PermissionServiceI permissionService;

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_PERMISSION_VIEW})
	@GetMapping("/listPermission")
	@Operation(summary = "权限列表", method = "GET")
	public MultiResponse<PermissionVO> listPermission(@ModelAttribute @Validated PermissionListQry permissionListQry) {
		return MultiResponse.of(permissionService.list(permissionListQry));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_PERMISSION_VIEW})
	@GetMapping("/pagePermission")
	@Operation(summary = "权限分页", method = "GET")
	public PageResponse<PermissionVO> pagePermission(@ModelAttribute @Validated PermissionPageQry permissionPageQry) {
		PageDTO<PermissionVO> page = permissionService.page(permissionPageQry);
		return PageResponse.of(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_PERMISSION_CREATE})
	@PostMapping("/createPermission")
	@Operation(summary = "创建权限", method = "POST")
	public SingleResponse<PermissionVO> createPermission(@RequestBody @Validated PermissionCreateCmd permissionCreateCmd) {
		return SingleResponse.of(permissionService.create(permissionCreateCmd));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_PERMISSION_VIEW})
	@GetMapping("/showPermission")
	@Operation(summary = "查看权限", method = "GET")
	public SingleResponse<PermissionVO> showPermission(@ModelAttribute @Validated PermissionShowQry permissionShowQry) {
		return SingleResponse.of(permissionService.show(permissionShowQry));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_PERMISSION_UPDATE})
	@PostMapping("updatePermission")
	@Operation(summary = "更新权限", method = "POST")
	public SingleResponse<PermissionVO> updatePermission(@RequestBody @Validated PermissionUpdateCmd permissionUpdateCmd) {
		return SingleResponse.of(permissionService.update(permissionUpdateCmd));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_PERMISSION_DELETE})
	@PostMapping("destroyPermission")
	@Operation(summary = "删除权限", method = "POST")
	public Response destroyPermission(@RequestBody @Validated PermissionDestroyCmd permissionDestroyCmd) {
		permissionService.destroy(permissionDestroyCmd);
		return Response.buildSuccess();
	}
}
