package ink.wgink.login.oauth2.client.service.role.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.SearchException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.interfaces.department.IDepartmentBaseService;
import ink.wgink.interfaces.department.IDepartmentUserBaseService;
import ink.wgink.login.oauth2.client.remote.role.IRoleDepartmentUserRemoteService;
import ink.wgink.login.oauth2.client.service.role.IRoleDepartmentUserService;
import ink.wgink.login.oauth2.client.service.role.IRoleUserService;
import ink.wgink.module.oauth2.manager.OAuth2ClientTokenManager;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.department.DepartmentUserDTO;
import ink.wgink.pojo.pos.DepartmentPO;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.properties.ApiPathProperties;
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.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

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

    @Autowired
    private IRoleUserService roleUserService;
    @Autowired
    private IDepartmentBaseService departmentBaseService;
    @Autowired
    private IDepartmentUserBaseService departmentUserBaseService;
    @Autowired
    private IRoleDepartmentUserRemoteService roleDepartmentUserRemoteService;
    @Autowired
    private ApiPathProperties apiPathProperties;

    @Override
    public List<DepartmentUserDTO> listUserByRoleIdAndAreaCode(String roleId, String areaCode) {
        // 地区下所有组织机构
        List<DepartmentPO> departmentPOs = departmentBaseService.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 = departmentUserBaseService.list(departmentIds);
        if (departmentUserDTOs.isEmpty()) {
            return new ArrayList<>();
        }
        clearNoRoleUser(departmentUserDTOs, roleUserIds);
        return departmentUserDTOs;
    }

    @Override
    public List<DepartmentUserDTO> listDepartmentUserByDepartmentIdAndRoleIdAndKeywords(String departmentId, String roleId, String keywords) {
        try {
            return roleDepartmentUserRemoteService.listDepartmentUserByDepartmentIdAndRoleIdAndKeywords(apiPathProperties.getUserCenter(), departmentId, roleId, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), StringUtils.isBlank(keywords) ? "" : URLEncoder.encode(keywords, ISystemConstant.CHARSET_UTF8));
        } catch (UnsupportedEncodingException e) {
            throw new SearchException(e);
        }
    }

    @Override
    public SuccessResultList<List<DepartmentUserDTO>> listPageDepartmentUserByDepartmentIdAndRoleId(String departmentId, String roleId, ListPage page) {
        return roleDepartmentUserRemoteService.listPageDepartmentUserByDepartmentIdAndRoleId(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), departmentId, roleId, page.getPage(), page.getRows(), page.getParams());
    }

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