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.infra.api.PlatformServiceI;
import com.xunhong.erp.turbo.api.infra.dto.cmd.PlatformCreateCmd;
import com.xunhong.erp.turbo.api.infra.dto.cmd.PlatformDestroyCmd;
import com.xunhong.erp.turbo.api.infra.dto.cmd.PlatformUpdateCmd;
import com.xunhong.erp.turbo.api.infra.dto.qry.PlatformListQry;
import com.xunhong.erp.turbo.api.infra.dto.qry.PlatformPageQry;
import com.xunhong.erp.turbo.api.infra.dto.qry.PlatformShowQry;
import com.xunhong.erp.turbo.api.infra.dto.vo.PlatformVO;
import com.xunhong.erp.turbo.api.rbac.dto.constants.PermissionConstant;
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.*;

/**
 * @author shenyifei
 */
@Tag(name = "Platform", description = "平台管理")
@RestController("operationPlatformController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class PlatformController {
	@DubboReference(version = "1.0.0")
	private final PlatformServiceI platformService;

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_PLATFORM_VIEW})
	@GetMapping("/listPlatform")
	@Operation(summary = "平台列表", method = "GET")
	public MultiResponse<PlatformVO> listPlatform(@ModelAttribute @Validated PlatformListQry platformListQry) {
		return MultiResponse.of(platformService.list(platformListQry));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_PLATFORM_CREATE})
	@PostMapping("/createPlatform")
	@Operation(summary = "创建平台", method = "POST")
	public SingleResponse<PlatformVO> createPlatform(@RequestBody @Validated PlatformCreateCmd platformCreateCmd) {
		return SingleResponse.of(platformService.create(platformCreateCmd));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_PLATFORM_VIEW})
	@GetMapping("/showPlatform")
	@Operation(summary = "平台详情", method = "GET")
	public SingleResponse<PlatformVO> showPlatform(@ModelAttribute @Validated PlatformShowQry platformShowQry) {
		return SingleResponse.of(platformService.show(platformShowQry));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_PLATFORM_VIEW})
	@GetMapping("/pagePlatform")
	@Operation(summary = "平台列表", method = "GET")
	public PageResponse<PlatformVO> pagePlatform(@ModelAttribute @Validated PlatformPageQry platformPageQry) {
		PageDTO<PlatformVO> page = platformService.page(platformPageQry);
		return PageResponse.of(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_PLATFORM_UPDATE})
	@RequestMapping(value = "/updatePlatform", method = {RequestMethod.PATCH, RequestMethod.PUT})
	@Operation(summary = "平台更新", method = "PATCH")
	public SingleResponse<PlatformVO> updatePlatform(@RequestBody @Validated PlatformUpdateCmd platformUpdateCmd) {
		return SingleResponse.of(platformService.update(platformUpdateCmd));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_PLATFORM_DELETE})
	@DeleteMapping("/destroyPlatform")
	@Operation(summary = "平台删除", method = "DELETE")
	public Response destroyPlatform(@RequestBody @Validated PlatformDestroyCmd platformDestroyCmd) {
		platformService.destroy(platformDestroyCmd);
		return Response.buildSuccess();
	}
}
