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

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.pojo.dtos.department.DepartmentUserDTO;
import ink.wgink.pojo.pos.DepartmentPO;
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.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 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) {
        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;
    }

    /**
     * 清除没有角色的用户
     *
     * @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);
    }


}
