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

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
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.qry.PlatformListQry;
import com.xunhong.erp.turbo.api.infra.dto.vo.PlatformVO;
import com.xunhong.erp.turbo.api.rbac.api.RoleServiceI;
import com.xunhong.erp.turbo.api.rbac.dto.cmd.RoleCreateCmd;
import com.xunhong.erp.turbo.api.rbac.dto.cmd.RoleDestroyCmd;
import com.xunhong.erp.turbo.api.rbac.dto.cmd.RoleUpdateCmd;
import com.xunhong.erp.turbo.api.rbac.dto.constants.PermissionConstant;
import com.xunhong.erp.turbo.api.rbac.dto.qry.RoleListQry;
import com.xunhong.erp.turbo.api.rbac.dto.qry.RolePageQry;
import com.xunhong.erp.turbo.api.rbac.dto.qry.RoleShowQry;
import com.xunhong.erp.turbo.api.rbac.dto.vo.RoleVO;
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.*;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Tag(name = "Role", description = "应用角色管理")
@RestController("operationRoleController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class RoleController {

	@DubboReference(version = "1.0.0")
	private final RoleServiceI roleService;

	@DubboReference(version = "1.0.0")
	private final PlatformServiceI platformService;

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_ROLE_VIEW})
	@GetMapping("/listRole")
	@Operation(summary = "应用角色列表", method = "GET")
	public MultiResponse<RoleVO> listRole(@ModelAttribute @Validated RoleListQry roleListQry) {
		List<RoleVO> roleVOList = roleService.listRole(roleListQry);

		List<Long> platformIdList = roleVOList.stream().map(RoleVO::getPlatformId).toList();
		if (CollUtil.isNotEmpty(platformIdList)) {
			PlatformListQry platformListQry = new PlatformListQry();
			List<PlatformVO> platformVOList = platformService.list(platformListQry);
			Map<Long, PlatformVO> platformVOMap = platformVOList.stream().collect(Collectors.toMap(PlatformVO::getPlatformId, Function.identity()));
			roleVOList.forEach(roleVO -> roleVO.setPlatformVO(platformVOMap.get(roleVO.getPlatformId())));
		}

		return MultiResponse.of(roleVOList);
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_ROLE_VIEW})
	@GetMapping("/pageRole")
	@Operation(summary = "应用角色分页", method = "GET")
	public PageResponse<RoleVO> pageRole(@ModelAttribute @Validated RolePageQry rolePageQry) {
		PageDTO<RoleVO> page = roleService.pageRole(rolePageQry);
		return PageResponse.of(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_ROLE_CREATE})
	@PostMapping("/createRole")
	@Operation(summary = "创建应用角色", method = "POST")
	public SingleResponse<RoleVO> createRole(@RequestBody @Validated RoleCreateCmd roleCreateCmd) {
		return SingleResponse.of(roleService.createRole(roleCreateCmd));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_ROLE_VIEW})
	@GetMapping("showRole")
	@Operation(summary = "查看应用角色", method = "GET")
	public SingleResponse<RoleVO> showRole(@ModelAttribute @Validated RoleShowQry roleShowQry) {
		RoleVO roleVO = roleService.showRole(roleShowQry);
		return SingleResponse.of(roleVO);
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_ROLE_UPDATE})
	@PostMapping("updateRole")
	@Operation(summary = "更新应用应用角色", method = "POST")
	public SingleResponse<RoleVO> updateRole(@RequestBody @Validated RoleUpdateCmd roleUpdateCmd) {
		return SingleResponse.of(roleService.updateRole(roleUpdateCmd));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_ROLE_DELETE})
	@PostMapping("destroyRole")
	@Operation(summary = "删除应用角色", method = "POST")
	public Response destroyRole(@RequestBody @Validated RoleDestroyCmd roleDestroyCmd) {
		roleService.destroyRole(roleDestroyCmd);
		return Response.buildSuccess();
	}

}
