package org.sky.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.sky.core.cache.utils.CacheUtil;
import org.sky.core.excel.util.ExcelUtil;
import org.sky.core.mp.support.Condition;
import org.sky.core.mp.support.Query;
import org.sky.core.secure.KdUser;
import org.sky.core.secure.utils.AuthUtil;
import org.sky.core.tenant.annotation.NonDS;
import org.sky.core.tool.api.R;
import org.sky.core.tool.constant.KdConstant;
import org.sky.core.tool.utils.DateUtil;
import org.sky.core.tool.utils.StringPool;
import org.sky.system.excel.UserExcel;
import org.sky.system.excel.UserImporter;
import org.sky.system.pojo.entity.User;
import org.sky.system.pojo.vo.UserVO;
import org.sky.system.service.IUserService;
import org.sky.system.wrapper.UserWrapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static org.sky.core.cache.constant.CacheConstant.USER_CACHE;

/**
 * 控制器
 *
 */
@NonDS
@RestController
@RequestMapping("/user")
@Tag(name = "用户", description = "用户")
@AllArgsConstructor
public class UserController {

	private final IUserService userService;

	/**
	 * 查询单条
	 */
//	@IsAdmin
	@ApiOperationSupport(order = 1)
	@Operation(summary = "查看详情", description = "传入id")
	@GetMapping("/detail")
	public R<UserVO> detail(User user) {
		User detail = userService.getOne(Condition.getQueryWrapper(user));
		return R.data(UserWrapper.build().entityVO(detail));
	}

	/**
	 * 查询单条
	 */
	@ApiOperationSupport(order = 2)
	@Operation(summary = "查看详情", description = "传入id")
	@GetMapping("/info")
	public R<UserVO> info(KdUser user) {
		User detail = userService.getById(user.getUserId());
		return R.data(UserWrapper.build().entityVO(detail));
	}

	/**
	 * 用户列表
	 */
//	@IsAdmin
	@GetMapping("/list")
	@Parameters({
		@Parameter(name = "account", description = "账号名", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
		@Parameter(name = "realName", description = "姓名", in = ParameterIn.QUERY, schema = @Schema(type = "string"))
	})
	@ApiOperationSupport(order = 3)
	@Operation(summary = "列表", description = "传入account和realName")
	public R<IPage<UserVO>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> user, Query query, KdUser kdUser) {
		QueryWrapper<User> queryWrapper = Condition.getQueryWrapper(user, User.class);
		IPage<User> pages = userService.page(Condition.getPage(query), (!kdUser.getTenantId().equals(KdConstant.ADMIN_TENANT_ID)) ? queryWrapper.lambda().eq(User::getTenantId, kdUser.getTenantId()) : queryWrapper);
		return R.data(UserWrapper.build().pageVO(pages));
	}

	/**
	 * 自定义用户列表
	 */
//	@IsAdmin
	@GetMapping("/page")
	@Parameters({
		@Parameter(name = "account", description = "账号名", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
		@Parameter(name = "realName", description = "姓名", in = ParameterIn.QUERY, schema = @Schema(type = "string"))
	})
	@ApiOperationSupport(order = 3)
	@Operation(summary = "列表", description = "传入account和realName")
	public R<IPage<UserVO>> page(@Parameter(hidden = true) User user, Query query, Long deptId, KdUser kdUser) {
		Date now = new Date();
		System.out.println("开始：" + now);
		IPage<User> pages = userService.selectUserPage(Condition.getPage(query), user, deptId, (kdUser.getTenantId().equals(KdConstant.ADMIN_TENANT_ID) ? StringPool.EMPTY : kdUser.getTenantId()));
		System.out.println("开始1：" + (new Date().getTime()-now.getTime()));
		now = new Date();
		IPage<UserVO> page = UserWrapper.build().pageVO(pages);
		System.out.println("耗时：" + (new Date().getTime() - now.getTime()));

		return R.data(page);
	}

	/**
	 * 新增或修改
	 */
//	@IsAdmin
	@PostMapping("/submit")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增或修改", description = "传入User")
	public R submit(@Valid @RequestBody User user) {
		CacheUtil.clear(USER_CACHE);
		return R.status(userService.submit(user));
	}

	/**
	 * 修改
	 */
//	@IsAdmin
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description = "传入User")
	public R update(@Valid @RequestBody User user) {
		CacheUtil.clear(USER_CACHE);
		return R.status(userService.updateUser(user));
	}

	/**
	 * 删除
	 */
//	@IsAdmin
	@PostMapping("/remove")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "删除", description = "传入id集合")
	public R remove(@RequestParam String ids) {
		CacheUtil.clear(USER_CACHE);
		return R.status(userService.removeUser(ids));
	}

	/**
	 * 设置菜单权限
	 */
//	@IsAdmin
	@PostMapping("/grant")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "权限设置", description = "传入roleId集合以及menuId集合")
	public R grant(@Parameter(description = "userId集合", required = true) @RequestParam String userIds,
				   @Parameter(description = "roleId集合", required = true) @RequestParam String roleIds) {
		boolean temp = userService.grant(userIds, roleIds);
		return R.status(temp);
	}

	/**
	 * 密码重制
	 */
//	@IsAdmin
	@PostMapping("/reset-password")
	@ApiOperationSupport(order = 8)
	@Operation(summary = "初始化密码", description = "传入userId集合")
	public R resetPassword(@Parameter(description = "userId集合", required = true) @RequestParam String userIds) {
		boolean temp = userService.resetPassword(userIds);
		return R.status(temp);
	}

	/**
	 * 修改密码
	 */
	@PostMapping("/update-password")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "修改密码", description = "传入密码")
	public R updatePassword(KdUser user, @Parameter(description = "旧密码", required = true) @RequestParam String oldPassword,
							@Parameter(description = "新密码", required = true) @RequestParam String newPassword,
							@Parameter(description = "新密码", required = true) @RequestParam String newPassword1) {
		boolean temp = userService.updatePassword(user.getUserId(), oldPassword, newPassword, newPassword1);
		return R.status(temp);
	}

	/**
	 * 修改基本信息
	 */
	@PostMapping("/update-info")
	@ApiOperationSupport(order = 10)
	@Operation(summary = "修改基本信息", description = "传入User")
	public R updateInfo(@Valid @RequestBody User user) {
		CacheUtil.clear(USER_CACHE);
		return R.status(userService.updateUserInfo(user));
	}

	/**
	 * 用户列表
	 */
	@GetMapping("/user-list")
	@ApiOperationSupport(order = 11)
	@Operation(summary = "用户列表", description = "传入user")
	public R<List<UserVO>> userList(User user, KdUser kdUser) {
		QueryWrapper<User> queryWrapper = Condition.getQueryWrapper(user);
		List<User> list = userService.list((!AuthUtil.isAdministrator()) ? queryWrapper.lambda().eq(User::getTenantId, kdUser.getTenantId()) : queryWrapper);
		return R.data(UserWrapper.build().listVO(list));
	}

	/**
	 * 导入用户
	 */
//	@IsAdmin
	@PostMapping("import-user")
	@ApiOperationSupport(order = 12)
	@Operation(summary = "导入用户", description = "传入excel")
	public R importUser(MultipartFile file, Integer isCovered) {
		UserImporter userImporter = new UserImporter(userService, isCovered == 1);
		ExcelUtil.save(file, userImporter, UserExcel.class);
		return R.success("操作成功");
	}

	/**
	 * 导出用户
	 */
//	@IsAdmin
	@GetMapping("export-user")
	@ApiOperationSupport(order = 13)
	@Operation(summary = "导出用户", description = "传入user")
	public void exportUser(@Parameter(hidden = true) @RequestParam Map<String, Object> user, KdUser kdUser, HttpServletResponse response) {
		QueryWrapper<User> queryWrapper = Condition.getQueryWrapper(user, User.class);
		if (!AuthUtil.isAdministrator()) {
			queryWrapper.lambda().eq(User::getTenantId, kdUser.getTenantId());
		}
		queryWrapper.lambda().eq(User::getIsDeleted, KdConstant.DB_NOT_DELETED);
		List<UserExcel> list = userService.exportUser(queryWrapper);
		ExcelUtil.export(response, "用户数据" + DateUtil.time(), "用户数据表", list, UserExcel.class);
	}

	/**
	 * 导出模板
	 */
	@GetMapping("export-template")
	@ApiOperationSupport(order = 14)
	@Operation(summary = "导出模板")
	public void exportUser(HttpServletResponse response) {
		List<UserExcel> list = new ArrayList<>();
		ExcelUtil.export(response, "用户数据模板", "用户数据表", list, UserExcel.class);
	}


	/**
	 * 第三方注册用户
	 */
	@PostMapping("/register-guest")
	@ApiOperationSupport(order = 15)
	@Operation(summary = "第三方注册用户", description = "传入user")
	public R registerGuest(User user, Long oauthId) {
		return R.status(userService.registerGuest(user, oauthId));
	}


	/**
	 * 配置用户平台信息
	 */
	@PostMapping("/update-platform")
	@ApiOperationSupport(order = 16)
	@Operation(summary = "配置用户平台信息", description = "传入user")
	public R updatePlatform(Long userId, Integer userType, String userExt) {
		return R.status(userService.updatePlatform(userId, userType, userExt));
	}

	/**
	 * 查看平台详情
	 */
//	@IsAdmin
	@ApiOperationSupport(order = 17)
	@Operation(summary = "查看平台详情", description = "传入id")
	@GetMapping("/platform-detail")
	public R<UserVO> platformDetail(User user) {
		return R.data(userService.platformDetail(user));
	}

	/**
	 * 用户解锁
	 */
//	@IsAdmin
	@PostMapping("/unlock")
	@ApiOperationSupport(order = 18)
	@Operation(summary = "账号解锁", description = "传入id集合")
	public R unlock(String userIds) {
		return R.status(userService.unlock(userIds));
	}

	/**
	 * 审核通过
	 */
//	@IsAdmin
	@PostMapping("/audit-pass")
	@ApiOperationSupport(order = 19)
	@Operation(summary = "审核通过", description = "传入id集合")
	public R auditPass(String userIds) {
		return R.status(userService.auditPass(userIds));
	}

	/**
	 * 审核拒绝
	 */
//	@IsAdmin
	@PostMapping("/audit-refuse")
	@ApiOperationSupport(order = 20)
	@Operation(summary = "审核拒绝", description = "传入id集合")
	public R auditRefuse(String userIds) {
		return R.status(userService.auditRefuse(userIds));
	}

}
