/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.systemexpand.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.annotations.*;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springblade.common.cache.DictCache;
import org.springblade.common.enums.DictEnum;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.constant.RoleConstant;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IUserService;
import org.springblade.modules.system.vo.DeptVO;
import org.springblade.modules.system.vo.UserVO;
import org.springblade.modules.system.wrapper.DeptWrapper;
import org.springblade.modules.system.wrapper.UserWrapper;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE;

/**
 * 控制器
 *
 * @author Chill
 */
@NonDS
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SYSTEM_NAME + "/expand")
@Api(value = "扩展", tags = "扩展")
public class ExpandController extends BladeController {

	private final IDeptService deptService;
	private final IUserService userService;

	/**
	 * 获取当前用户的部门信息
	 */
	@GetMapping("/dept/getCurrentDept")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "获取当前用户的部门信息", notes = "")
	public R<DeptVO> getCurrentDept() {
		BladeUser user = AuthUtil.getUser();
		Dept currDept = deptService.getById(user.getDeptId());
		return R.data(DeptWrapper.build().entityVO(currDept));
	}

	/**
	 * 详情
	 */
	@GetMapping("/dept/getChildsDept")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "详情", notes = "传入dept")
	public R getChildsDept() {
		Dept dept = deptService.getById(AuthUtil.getDeptId());
		List<Dept> list = new ArrayList<>();
		if (!dept.getOrgNo().startsWith("999") && !dept.getDeptCategory().equals(1)){
			list.add(dept);
		}
		List<Dept> deptChild = deptService.getDeptChild(dept.getId());
		list.addAll(deptChild);
		return R.data(list);
	}
	/**
	 * 详情
	 */
	@GetMapping("/dept/getAllDept")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "详情", notes = "传入dept")
	public R getAllDept() {
		List<Dept> list = deptService.getDeptChild(0L);
		return R.data(list);
	}

	/**
	 * 获取当前用户的部门信息
	 */
	@GetMapping("/dept/getDeptByOrgNo")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "获取当前用户的部门信息", notes = "")
	public R<Dept> getDeptByOrgNo(String orgNo) {
		Dept dept = deptService.getOne(new QueryWrapper<>(new Dept()).lambda().eq(Dept::getOrgNo, orgNo));
		return R.data(dept);
	}

	/**
	 * 获取部门树形结构
	 */
	@GetMapping("/dept/tree")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "树形结构", notes = "树形结构")
	public R<List<DeptVO>> tree(BladeUser bladeUser) {
		String tenantId = "000000";
		List<DeptVO> tree = deptService.tree(Func.toStrWithEmpty(tenantId, bladeUser.getTenantId()));
		return R.data(tree);
	}

	/**
	 * 自定义用户列表
	 */
	@GetMapping("/user/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "account", value = "账号名", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "realName", value = "姓名", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "列表", notes = "传入account和realName")
	public R<IPage<UserVO>> page(@ApiIgnore User user, Query query, String hasOrgNo, BladeUser bladeUser) {
//		deptId = Long.valueOf(bladeUser.getDeptId());
//		IPage<User> pages = userService.selectUserPage(Condition.getPage(query), user, deptId, (bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID) ? StringPool.EMPTY : bladeUser.getTenantId()));

		IPage<User> pages = userService.selectMyUserPage(Condition.getPage(query), user, Long.valueOf(bladeUser.getDeptId()), hasOrgNo, (bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID) ? StringPool.EMPTY : bladeUser.getTenantId()));
		return R.data(UserWrapper.build().pageVO(pages));
	}

	/**
	 * 自定义用户列表 （全部-除admin超管）
	 */
	@GetMapping("/user/pageAll")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "account", value = "账号名", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "realName", value = "姓名", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "列表", notes = "传入account和realName")
	public R<IPage<UserVO>> pageAll(@ApiIgnore User user, Query query, Long deptId, BladeUser bladeUser) {
		if (deptId == null){
			List<Dept> byParentId = deptService.getByParentId(0L);
			deptId = byParentId.get(0).getId();
		}
		IPage<User> pages = userService.selectMyUserPage(Condition.getPage(query), user, deptId, null, (bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID) ? StringPool.EMPTY : bladeUser.getTenantId()));
		return R.data(UserWrapper.build().pageVO(pages));
	}

}
