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

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.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;
import java.util.Map;

/**
 * @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> listDepartmentUserByDepartmentIdAndRoleIdAndKeywords(String departmentId, String roleId, String keywords) {
        if (StringUtils.equals(departmentId, ISystemConstant.TREE_BASE_ROOT_ID_VALUE)) {
            return listUserByExcludeDepartmentAndKeywords(roleId, keywords);
        }
        return listUserByDepartmentIdAndRoleIdAndKeywords(departmentId, roleId, keywords);
    }

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

    private List<DepartmentUserDTO> listUserByDepartmentIdAndRoleIdAndKeywords(String departmentId, String roleId, String keywords) {
        Map<String, Object> params = getHashMap(4);
        params.put("departmentId", departmentId);
        if (!StringUtils.equals(roleId, ISystemConstant.TREE_BASE_ROOT_ID_VALUE)) {
            // 有部门有角色用户列表
            params.put("roleId", roleId);
            return roleUserService.listDepartmentUser(params);
        }
        // 有部门没有角色的用户列表
        params.put("noRole", true);
        return roleUserService.listDepartmentUser(params);
    }

    /**
     * 不包含部门的部门用户
     *
     * @param roleId
     * @return
     */
    private List<DepartmentUserDTO> listUserByExcludeDepartmentAndKeywords(String roleId, String keywords) {
        Map<String, Object> params = getHashMap(6);
        params.put("keywords", keywords);
        params.put("noDepartment", true);
        params.put("excludeUserType", 3);
        if (StringUtils.equals(roleId, ISystemConstant.TREE_BASE_ROOT_ID_VALUE)) {
            // 无部门，无角色
            params.put("noRole", true);
        } else {
            params.put("roleId", roleId);
        }
        return roleUserService.listDepartmentUser(params);
    }

    /**
     * 没有组织机构的用户分页列表
     *
     * @param roleId 角色ID
     * @param page
     * @return
     */
    private SuccessResultList<List<DepartmentUserDTO>> listPageUserByExcludeDepartment(String roleId, ListPage page) {
        if (StringUtils.equals(roleId, ISystemConstant.TREE_BASE_ROOT_ID_VALUE)) {
            // 有部门，没有角色
            page.getParams().put("noRole", true);
        } else {
            // 有部门，有角色
            page.getParams().put("roleId", roleId);
        }
        page.getParams().put("noDepartment", true);
        return listPageDepartmentUser(page);
    }

    /**
     * 组织机构的用户分页列表
     *
     * @param departmentId 组织机构ID
     * @param page
     * @return
     */
    private SuccessResultList<List<DepartmentUserDTO>> listPageDepartmentUser(String departmentId, String roleId, ListPage page) {
        if (StringUtils.equals(roleId, ISystemConstant.TREE_BASE_ROOT_ID_VALUE)) {
            // 有部门，没有角色
            page.getParams().put("noRole", true);
        } else {
            // 有部门，有角色
            page.getParams().put("roleId", roleId);
        }
        page.getParams().put("departmentId", departmentId);
        return listPageDepartmentUser(page);
    }

    /**
     * 部门用户分页列表
     *
     * @param page
     * @return
     */
    private SuccessResultList<List<DepartmentUserDTO>> listPageDepartmentUser(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<DepartmentUserDTO> departmentUserDTOs = roleUserService.listDepartmentUser(page.getParams());
        PageInfo<DepartmentUserDTO> pageInfo = new PageInfo<>(departmentUserDTOs);
        return new SuccessResultList<>(departmentUserDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

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


}
