package com.winsun.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.winsun.bean.HhrSupervisorSchool;
import com.winsun.bean.HhrUser;
import com.winsun.bean.UserSchool;
import com.winsun.mapper.HhrSupervisorSchoolMapper;
import com.winsun.mapper.HhrUserMapper;
import com.winsun.mapper.UserSchoolMapper;
import com.winsun.service.IHhrUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chengfengluo
 * @date 2021-03-11 15:01
 */
@Service
public class HhrUserServiceImpl extends ServiceImpl<HhrUserMapper, HhrUser> implements IHhrUserService {

    @Autowired
    private HhrSupervisorSchoolMapper hhrSupervisorSchoolMapper;

    @Autowired
    private UserSchoolMapper userSchoolMapper;

    /**
     * 获取管辖内用户id
     * @return
     */
    public List<Integer> getUserIds (Integer userId, List<String> roles) {
        List<Integer> resultList = new ArrayList<>();
        StringBuffer sb = new StringBuffer();
        for(String role : roles) {
            sb.append(role).append(",");
        }

        // 职位信息
        HhrUser hhrUser = this.selectById(userId);

        String rolesStr = sb.toString();
        if (rolesStr.indexOf("超级管理员") != -1) {
            return resultList;
        }else if(rolesStr.indexOf("县分管理员（订单）") != -1 || "总经理".equals(hhrUser.getPosition())){
            // 县分管理员、督导(查询管辖内的学校用户)
            resultList = this.getSubNameUser(userId);
        }else if(rolesStr.indexOf("合伙人") != -1 && "楼长".equals(hhrUser.getPosition())) {
            // 合伙人（楼长，查询本人及下级信息）
            resultList = this.getChildrenUser(userId);
        }else if(rolesStr.indexOf("快递公司") != -1){
            // 快递公司(查询管辖内的学校用户)
            resultList = this.getSubNameUser(userId);
        }else{
            // 合伙人（成员，查询本人信息）其他人员
        }
        resultList.add(userId);
        return resultList;
    }

    /**
     * 获取所有用户
     * @param userId
     * @return
     */
    public List<Integer> getAllUser(Integer userId) {
        Wrapper<HhrUser> wrapper = new EntityWrapper<>();
        wrapper.setSqlSelect("id");
        wrapper.isNotNull("id");
        List<HhrUser> hhrUsers = this.selectList(wrapper);
        List<Integer> resultList = new ArrayList<>();
        hhrUsers.forEach(hhrUser -> {
            resultList.add(Integer.parseInt(hhrUser.getId()));
        });
        return resultList;
    }

    /**
     * 获取县分用户
     * @param userId
     * @return
     */
    public List<Integer> getSubNameUser(Integer userId) {
        Wrapper<HhrSupervisorSchool> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId);
        List<HhrSupervisorSchool> hhrSupervisorSchools = hhrSupervisorSchoolMapper.selectList(wrapper);
        if (hhrSupervisorSchools == null || hhrSupervisorSchools.size() == 0) {
            return new ArrayList<>();
        }

        String[] school = new String[hhrSupervisorSchools.size()];
        for(int i = 0; i < hhrSupervisorSchools.size(); ++i) {
            school[i] = hhrSupervisorSchools.get(i).getSchool();
        }

        Wrapper<UserSchool> userSchoolWrapper = new EntityWrapper<>();
        userSchoolWrapper.in("school_id", school);
        List<UserSchool> userSchools = userSchoolMapper.selectList(userSchoolWrapper);
        List<Integer> resultList = new ArrayList<>();
        userSchools.forEach(userSchool -> {
            resultList.add(Integer.parseInt(userSchool.getUserId()));
        });
        return resultList;
    }

    /**
     * 获取子用户
     * @param userId
     * @return
     */
    public List<Integer> getChildrenUser(Integer userId) {

        Wrapper<HhrUser> wrapper = new EntityWrapper<>();
        wrapper.like("parent_ids", userId + "");
        List<HhrUser> hhrUsers = this.selectList(wrapper);
        List<Integer> resultList = new ArrayList<>();
        hhrUsers.forEach(hhrUser -> {
            resultList.add(Integer.parseInt(hhrUser.getId()));
        });
        return resultList;
    }

}
