package com.ho.user.controller;

import cn.hutool.core.lang.Snowflake;
import com.ho.common.tools.annotation.TokenIgnore;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.entity.OrderDept;
import com.ho.common.tools.exception.DataResult;
import com.ho.user.api.entity.SysDept;
import com.ho.user.api.entity.SysLog;
import com.ho.user.api.entity.SysSubDict;
import com.ho.user.api.entity.SysUser;
import com.ho.user.api.vo.req.OrderUserSelectReqVo;
import com.ho.user.api.vo.req.QueryDeptReqVO;
import com.ho.user.api.vo.req.QueryUserReqVO;
import com.ho.user.api.vo.req.SysSubDictVO;
import com.ho.user.entity.SysUserPosition;
import com.ho.user.mapper.SysDictMapper;
import com.ho.user.mapper.SysLogMapper;
import com.ho.user.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author fancl
 * @desc:
 * @date 2022/9/2
 */
@RestController
@ApiIgnore
@RequestMapping("outerApi")
@Slf4j
public class UserCenterApiController {

    @Autowired
    DeptService deptService;

    @Autowired
    PositionService positionService;

    @Autowired
    UserPostService userPostService;

    @Autowired
    DeptOrderUserService deptOrderUserService;

    @Autowired
    UserService userService;

    @Autowired
    SysLogMapper sysLogMapper;

    @Autowired
    Snowflake snowflake;

    @Autowired
    SysDictMapper sysDictMapper;


    @PostMapping( "selectBySubId")
    DataResult<SysSubDict> getDictById(@RequestBody Integer priority){
        SysSubDict dict = sysDictMapper.selectBySubId(priority);
        return DataResult.success(dict);
    }

    @PostMapping( "selectBySubIds")
    DataResult<List<SysSubDict>> getDictByIds(@RequestBody List<Integer> Ids){
        List<SysSubDict> dicts = sysDictMapper.getDictByIds(Ids);
        return DataResult.success(dicts);
    }

    //根据部门id 查询顶级部门
    @PostMapping("getTopDept")
    public DataResult<SysDept> getTopDept(@RequestBody QueryDeptReqVO vo) {
        SysDept topDept = deptService.getTopDept(vo.getDeptId());
        return DataResult.success(topDept);
    }


    //得到部门
    @PostMapping("getDept")
    public DataResult<List<SysDept>> getDept(@RequestBody List<QueryDeptReqVO> queryDeptReqVO) {
        DataResult<List<SysDept>> result = DataResult.success();
        List<Integer> deptIds = queryDeptReqVO.stream().map(QueryDeptReqVO::getDeptId).collect(Collectors.toList());
        result.setData(deptService.selectByIds(deptIds));
        return result;
    }

    @PostMapping("getUsers")
    public DataResult<List<SysUser>> queryUser(@RequestBody List<QueryUserReqVO> queryUserReqVO) {
        List<String> userIds = new ArrayList<>();
        List<SysUser> sysUsers = new ArrayList<>();
        if (queryUserReqVO != null && !queryUserReqVO.isEmpty()) {
            for (QueryUserReqVO userReqVO : queryUserReqVO) {
                userIds.add(userReqVO.getUserId());
            }
        }
        if (!userIds.isEmpty()) {
            sysUsers = userService.selectIds(userIds);
        }
        return DataResult.success(sysUsers);
    }

    //根据模糊匹配用户名查询用户 多个
    @PostMapping("getUserByNameConcat")
    public DataResult<List<SysUser>> getUserByNameConcat(@RequestBody QueryUserReqVO queryUserReqVO) {
        return DataResult.success(userService.getUserListByNameConcat(queryUserReqVO.getUserName(), queryUserReqVO.getGroupId()));
    }

    //根据用户id查询用户
    @PostMapping("getUserById")
    public DataResult<SysUser> getUserById(@RequestBody String userId) {
        SysUser sysUser = userService.selectById(userId);
        return DataResult.success(sysUser);
    }

    @PostMapping("queryUserById")
    public SysUser queryUserById(@RequestBody String userId) {
        return userService.selectById(userId);
    }

    /**
     * 外部请求记录日志
     *
     * @param sysLog
     * @return
     */
    @PostMapping("insertLog")
    public DataResult insertLog(@RequestBody SysLog sysLog) {

        sysLogMapper.insertSelective(sysLog);
        return DataResult.success();
    }

    //根据用户id查询用户
    @PostMapping("getUserByIds")
    public DataResult<List<SysUser>> getUserByIds(@RequestBody Set<String> userIds) {
        List<String> list = userIds.stream().collect(Collectors.toList());
        return DataResult.success(userService.selectIds(list));
    }

    /**
     * 根据岗位编码查询岗位用户
     * @param postIds
     * @return
     */
    @PostMapping("selectByPostIds")
    public DataResult<List<SysUser>> selectByPostIds(@RequestBody List<Integer> postIds){
        //根据岗位id查询对应的用户
        List<String> userIds = userPostService.getUserIdsByPostIds(postIds);
        //根据用户id查询用户
        List<SysUser> sysUsers = userService.selectPostUserIds(userIds,postIds);
        return DataResult.success(sysUsers);
    }

    @PostMapping("selectAllPostUser")
    public DataResult<List<SysUser>> selectAllPostUser(){
        //根据岗位id查询对应的用户
        List<SysUserPosition> sysUserPositions = userPostService.getByPostIds(new ArrayList<>());
        List<Integer> postIds = sysUserPositions.stream().map(SysUserPosition::getPositionId).collect(Collectors.toList());
        List<String> userIds = sysUserPositions.stream().map(SysUserPosition::getUserId).collect(Collectors.toList());
        //根据用户id查询用户
        List<SysUser> sysUsers = userService.selectPostUserIds(userIds,postIds);
        return DataResult.success(sysUsers);
    }

    /**
     * 根据postCodes 查岗位id  在查询到对应的人员id 根据人员id查询到人
     *
     * @param vo
     * @return
     */
    @PostMapping("selectByPostCodeTree")
    public DataResult<List<OrderDept>> selectByPostCodeTree(@RequestBody OrderUserSelectReqVo vo) {
        //根据groupId查询顶级部门
        SysDept topDept = deptService.detailInfo(vo.getGroupId());
        List<OrderDept> orderDepts = new ArrayList<>();
        //没查到顶级部门就不往下走了
        if (topDept == null) {
            return DataResult.success(orderDepts);
        }
        List<Integer> postIds = positionService.selectPostIds(vo.getPostCodes());
        //查不到信息返回空,不要报错
        if (postIds == null || postIds.isEmpty()) {
            //throw new BusinessException(BaseResponseCode.NO_RELEVANT_POSITIONS);
            return DataResult.success(orderDepts);
        }
        List<SysUser> sysUsers = userPostService.selectUserByPostIds(vo, postIds);
        if (sysUsers == null || sysUsers.isEmpty()) {
            //throw new BusinessException(BaseResponseCode.NO_QUALIFIED_PERSONNEL_IN_ALL_DEPARTMENTS);
            return DataResult.success(orderDepts);
        }
        //
        OrderDept orderDeptTop = deptOrderUserService.getDeptUser(topDept, sysUsers);

        //等级的那个部门可以直接挂人
        //以List形势返回
        List<OrderDept> deptAndUsers = new ArrayList<>();
        deptAndUsers.add(orderDeptTop);
        return DataResult.success(deptAndUsers);
    }

    /**
     * 对外提供上送数据专用
     * @param vo
     * @return
     */
    @PostMapping("getSysSubDict")
    @TokenIgnore
    public Map<String, String> getSysSubDict(@RequestBody SysSubDictVO vo) {
        Map<String, String> sysSubDict = userService.getSysSubDict(vo);
        return sysSubDict;
    }

    /**
     * 对外提供上送数据专用
     * @param deptId
     * @return
     */
    @PostMapping("getSysDeptByDeptId")
    @TokenIgnore
    public List<SysDept> getSysDeptByDeptId(@RequestBody Integer deptId) {
        List<SysDept> sysSubDict = deptService.depListByDeptId(deptId);
        return sysSubDict;
    }
}


