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

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckSafe;
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.user.api.UserServiceI;
import com.xunhong.erp.turbo.api.user.dto.cmd.*;
import com.xunhong.erp.turbo.api.user.dto.qry.UserListQry;
import com.xunhong.erp.turbo.api.user.dto.qry.UserPageQry;
import com.xunhong.erp.turbo.api.user.dto.qry.UserShowQry;
import com.xunhong.erp.turbo.api.user.dto.vo.UserVO;
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 = "User", description = "用户管理")
@RestController("operationUserController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class UserController {
	@DubboReference(version = "1.0.0")
	private final UserServiceI userService;

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.KHB_BUSINESS_MEMBER_VIEW})
	@GetMapping("pageUser")
	@Operation(summary = "用户列表", method = "GET")
	public PageResponse<UserVO> pageUser(@ModelAttribute @Validated UserPageQry userPageQry) {
		userPageQry.setIsAdmin(Boolean.FALSE);
		PageDTO<UserVO> page = userService.page(userPageQry);

		return PageResponse.of(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.KHB_BUSINESS_MEMBER_VIEW})
	@GetMapping("listUser")
	@Operation(summary = "用户列表", method = "GET")
	public MultiResponse<UserVO> listUser(@ModelAttribute @Validated UserListQry userListQry) {
		return MultiResponse.of(userService.list(userListQry));
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.KHB_BUSINESS_MEMBER_VIEW})
	@GetMapping("/showUser")
	@Operation(summary = "获取用户", method = "GET")
	public SingleResponse<UserVO> showUser(@ModelAttribute @Validated UserShowQry userShowQry) {
		UserVO userVO = userService.show(userShowQry);
		return SingleResponse.of(userVO);
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.KHB_BUSINESS_MEMBER_UPDATE})
	@RequestMapping(value = "/updateUser", method = {RequestMethod.PATCH, RequestMethod.PUT})
	@Operation(summary = "更新用户", method = "PATCH")
	public SingleResponse<UserVO> updateUser(@Validated @RequestBody UserUpdateCmd userUpdateCmd) {
		return SingleResponse.of(userService.update(userUpdateCmd));
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.KHB_BUSINESS_MEMBER_CREATE})
	@PostMapping("/createUser")
	@Operation(summary = "创建会员", method = "POST")
	public SingleResponse<UserVO> createUser(@Validated @RequestBody UserCreateCmd userCreateCmd) {
		return SingleResponse.of(userService.create(userCreateCmd));
	}

	@SaCheckLogin
	@SaCheckSafe
//	@SaCheckPermission(value = {PermissionConstant.KHB_BUSINESS_MEMBER_DELETE})
	@DeleteMapping("/destroyUser")
	@Operation(summary = "删除用户", method = "DELETE")
	public Response destroyUser(@Validated @RequestBody UserDestroyCmd userDestroyCmd) {
		userService.destroy(userDestroyCmd);
		return Response.buildSuccess();
	}

	/**
	 * 禁用
	 */
	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.KHB_BUSINESS_MEMBER_DISABLE})
	@PostMapping("/disableUser")
	@Operation(summary = "禁用用户", method = "POST")
	public Response disableUser(@RequestBody @Validated UserDisableCmd userDisableCmd) {
		userService.disable(userDisableCmd);
		return Response.buildSuccess();
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.KHB_BUSINESS_MEMBER_ENABLE})
	@PostMapping("/enableUser")
	@Operation(summary = "启用用户", method = "POST")
	public Response enableUser(@RequestBody @Validated UserEnableCmd userEnableCmd) {
		userService.enable(userEnableCmd);
		return Response.buildSuccess();
	}
}
