package com.storlead.centre.system;

import com.storlead.framework.auth.vo.LoginUser;
import com.storlead.framework.util.LoginUserUtil;
import com.storlead.framework.web.assemble.Result;
import com.storlead.user.model.UserInfo;
import com.storlead.user.model.UserQueryModel;
import com.storlead.user.pojo.entity.UserEntity;
import com.storlead.user.pojo.vo.WxUserVO;
import com.storlead.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @Author scott
 * @since 2018-12-20
 */
@Slf4j
@RestController
@RequestMapping("/lingcun/user")
@Api(tags="5.公共接口")
public class UserController {

	@Autowired
	private IUserService sysUserService;

	/**
	 * 查询数据 查出所有部门,并以树结构数据格式响应给前端
	 *
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ApiOperation(value = "公共接口-用户信息-列表", notes = "公共接口-用户信息-列表")
	@ApiResponses({
			@ApiResponse(code = 200, message = "", response = UserInfo.class)
	})
	public Result<List<UserInfo>> queryTreeList(UserQueryModel userQueryModel) {
		Result<List<UserInfo>> result = new Result<>();
		try {
			List<UserInfo> list = sysUserService.getUserListBySearch(userQueryModel);
			result.setResult(list);
			result.setSuccess(true);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return result;
	}

	 /***
	  * @Description: 用户列表，按拼音首字母排序
	  * @Param:
	  * @return:
	  * @Author: YPZ
	  * @Date: 2023/3/16 11:54
	  */
	@RequestMapping(value = "/user-list-pinyin", method = RequestMethod.GET)
	@ApiOperation(value = "公共接口-用户信息-列表（按拼音首字母排序）", notes = "公共接口-用户信息-列表（按拼音首字母排序）")
	@ApiResponses({
			@ApiResponse(code = 200, message = "", response = UserInfo.class)
	})
	public Result<List<WxUserVO>> userListPinyin(UserQueryModel userQueryModel) {
		Result<List<WxUserVO>> result = new Result<>();
		try {
			List<WxUserVO> wxUserInfoList =sysUserService.getPingyinUserList(userQueryModel);
			result.setResult(wxUserInfoList);
			result.setSuccess(true);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return result;
	}

	@RequestMapping(value = "/list-subordinate", method = RequestMethod.GET)
	@ApiOperation(value = "公共接口-下级用户信息-列表", notes = "公共接口-下级用户信息-列表")
	@ApiResponses({
			@ApiResponse(code = 200, message = "", response = UserInfo.class)
	})
	public Result<List<UserInfo>> listSubordinate(UserQueryModel userQueryModel) {
		Result<List<UserInfo>> result = new Result<>();
		try {
			LoginUser loginUser = LoginUserUtil.getLoginUser();
			List<UserInfo> list = sysUserService.getUserListBySearch(userQueryModel);
			List<Long> allSubordinate = sysUserService.getAllSubordinate(loginUser.getId());
			Set<Long> allSubordinateSet =new HashSet<>(allSubordinate);
			list = list.stream().filter((u)->allSubordinateSet.contains(u.getId())).collect(Collectors.toList());
			result.setResult(list);
			result.setSuccess(true);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return result;
	}

	/**
	 * 查询数据 查出所有部门,并以树结构数据格式响应给前端
	 *
	 * @return
	 */
	@GetMapping(value = "/findManager")
	@ApiOperation(value = "公共接口-用户信息-查看当前用户领导", notes = "公共接口-用户信息-查看当前用户领导")
	public Result findManager() {
		LoginUser loginUser = LoginUserUtil.getLoginUser();
		UserEntity user = sysUserService.getById(loginUser.getManagerId());
		return Result.ok(user);
	}

}
