package ink.wgink.service.role.service.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.department.DepartmentUserDTO;
import ink.wgink.pojo.dtos.user.UserDTO;
import ink.wgink.pojo.pos.DepartmentPO;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.service.department.service.IDepartmentService;
import ink.wgink.service.department.service.IDepartmentUserService;
import ink.wgink.service.role.service.IRoleDepartmentUserService;
import ink.wgink.service.role.service.IRoleUserService;
import ink.wgink.service.user.service.IUserService;
import ink.wgink.util.ArrayListUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @ClassName: RoleDepartmentUserServiceImpl
 * @Description: 角色部门用户
 * @Author: wanggeng
 * @Date: 2021/11/3 3:05 下午
 * @Version: 1.0
 */
@Service
public class RoleDepartmentUserServiceImpl extends DefaultBaseService implements IRoleDepartmentUserService {

    @Autowired
    private IRoleUserService roleUserService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IDepartmentUserService departmentUserService;

    @Override
    public List<DepartmentUserDTO> listUserByRoleIdAndAreaCode(String roleId, String areaCode) {
        // 地区下所有组织机构
        List<DepartmentPO> departmentPOs = departmentService.listPOByAreaCode(areaCode);
        if (departmentPOs.isEmpty()) {
            return new ArrayList<>();
        }
        List<String> roleUserIds = roleUserService.listUserId(roleId);
        if (roleUserIds.isEmpty()) {
            return new ArrayList<>();
        }
        List<String> departmentIds = ArrayListUtil.listBeanStringIdValue(departmentPOs, "departmentId", DepartmentPO.class);
        List<DepartmentUserDTO> departmentUserDTOs = departmentUserService.list(departmentIds);
        if (departmentUserDTOs.isEmpty()) {
            return new ArrayList<>();
        }
        clearNoRoleUser(departmentUserDTOs, roleUserIds);
        return departmentUserDTOs;
    }

    @Override
    public List<DepartmentUserDTO> listUserByDepartmentIdAndRoleId(String departmentId, String roleId) {
        if (StringUtils.equals(departmentId, ISystemConstant.TREE_BASE_ROOT_ID_VALUE)) {
            return listUserByExcludeDepartment(roleId);
        }
        return listUser(departmentId, roleId);
    }

    @Override
    public SuccessResultList<List<DepartmentUserDTO>> listPageUserByDepartmentIdAndRoleId(String departmentId, String roleId, ListPage page) {
        if (StringUtils.equals(departmentId, ISystemConstant.TREE_BASE_ROOT_ID_VALUE)) {
            return listPageUserByExcludeDepartment(roleId, page);
        }
        return listPageUser(departmentId, roleId, page);
    }

    private List<DepartmentUserDTO> listUser(String departmentId, String roleId) {
        List<DepartmentUserDTO> departmentUserDTOs = departmentUserService.list(departmentId);
        if (departmentUserDTOs.isEmpty()) {
            return new ArrayList<>();
        }
        List<String> roleUserIds = roleUserService.listUserId(roleId);
        if (roleUserIds.isEmpty()) {
            return new ArrayList<>();
        }
        clearNoRoleUser(departmentUserDTOs, roleUserIds);
        return departmentUserDTOs;
    }

    private List<DepartmentUserDTO> listUserByExcludeDepartment(String roleId) {
        List<String> roleUserIds = roleUserService.listUserId(roleId);
        if (roleUserIds.isEmpty()) {
            return new ArrayList<>();
        }
        List<DepartmentUserDTO> hasDepartmentUsers = departmentUserService.list(roleUserIds);
        List<String> departmentUserIds = ArrayListUtil.listBeanStringIdValue(hasDepartmentUsers, "userId", DepartmentUserDTO.class);
        List<UserDTO> userDTOs = userService.listByExcludeIds(departmentUserIds, roleUserIds, null);
        return departmentUserService.listExcludeDepartmentUser(userDTOs);
    }

    /**
     * 没有组织机构的用户分页列表
     *
     * @param roleId 角色ID
     * @param page
     * @return
     */
    private SuccessResultList<List<DepartmentUserDTO>> listPageUserByExcludeDepartment(String roleId, ListPage page) {
        List<String> roleUserIds = roleUserService.listUserId(roleId);
        if (roleUserIds.isEmpty()) {
            return new SuccessResultList<>(new ArrayList<>(), 1, 0L);
        }
        // 获取所有部门的用户列表
        List<DepartmentUserDTO> hasDepartmentUsers = departmentUserService.list(getHashMap(0));
        List<String> hasDepartmentUserIds = ArrayListUtil.listBeanStringIdValue(hasDepartmentUsers, "userId", DepartmentUserDTO.class);
        SuccessResultList<List<UserDTO>> successResultList = userService.listPageByExcludeIdsAndIncludeIds(hasDepartmentUserIds, roleUserIds, page);
        return departmentUserService.listPageExcludeDepartmentUser(successResultList);
    }

    /**
     * 组织机构的用户分页列表
     *
     * @param departmentId 组织机构ID
     * @param page
     * @return
     */
    private SuccessResultList<List<DepartmentUserDTO>> listPageUser(String departmentId, String roleId, ListPage page) {
        List<String> roleUserIds = roleUserService.listUserId(roleId);
        if (roleUserIds.isEmpty()) {
            return new SuccessResultList<>(new ArrayList<>(), 1, 0L);
        }
        List<DepartmentUserDTO> hasDepartmentUsers = departmentUserService.listByDepartmentIdAndUserIds(departmentId, roleUserIds);
        List<String> userIds = new ArrayList<>();
        for (DepartmentUserDTO hasDepartmentUserId : hasDepartmentUsers) {
            userIds.add(hasDepartmentUserId.getUserId());
        }
        SuccessResultList<List<UserDTO>> successResultList = userService.listPageByIds(userIds, page);
        return departmentUserService.listPageDepartmentUser(successResultList, hasDepartmentUsers);
    }

    /**
     * 清除没有角色的用户
     *
     * @param departmentUserDTOs
     * @param roleUserIds
     */
    private void clearNoRoleUser(List<DepartmentUserDTO> departmentUserDTOs, List<String> roleUserIds) {
        // 去除没有角色的用户
        for (int i = 0; i < departmentUserDTOs.size(); i++) {
            DepartmentUserDTO departmentUserDTO = departmentUserDTOs.get(i);
            boolean isExist = false;
            for (String roleUserId : roleUserIds) {
                if (StringUtils.equals(departmentUserDTO.getUserId(), roleUserId)) {
                    isExist = true;
                    break;
                }
            }
            if (!isExist) {
                departmentUserDTOs.remove(i);
                i--;
            }
        }
        departmentUserService.setUser(departmentUserDTOs);
    }


}
