package com.casic.missiles.modular.system.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.casic.missiles.modular.system.dao.DataScopeMapper;
import com.casic.missiles.modular.system.model.User;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@DS("log")
public class DataScopeBuilder {

    private final DataScopeMapper dataScopeMapper;

    public DataScopeBuilder(DataScopeMapper dataScopeMapper) {
        this.dataScopeMapper = dataScopeMapper;
    }

    /**
     * 1、所有数据，得到的用户ID
     * 2、所属查上级及上级所有部门,得到一个list
     * 3、所属本部门  得到list
     * 4、本部门自定义的一批部门信息  得到一个list
     *
     * @retur
     */
    public List<User> DataScopeProvider(Long deptId) {
        List<Map<String, String>> userIdDataScopeList = dataScopeMapper.getUserIdDataScope();
        Map<String, List<Map<String, String>>> userDataScopeListMap = userIdDataScopeList.stream()
                .collect(Collectors.groupingBy(a -> a.get("scopeType")));
        List<Long> userIdList = new ArrayList<>();
        userIdList.addAll(getAllUser(userDataScopeListMap.get("1")));
        userIdList.addAll(customDeptList(userDataScopeListMap.get("4"), deptId));
        List<Map<String, String>> deptUserList = new ArrayList<>();
        deptUserList.addAll(userDataScopeListMap.get("2"));
        deptUserList.addAll(userDataScopeListMap.get("3"));
        userIdList.addAll(getParentDeptList(deptUserList, deptId));
        List<User> userList = userProvider(userIdList);
        return userList;
    }

    /**
     * 1、所有数据的用户要发送
     * 2、
     *
     * @return
     */
    private List<Long> getAllUser(List<Map<String, String>> allUserList) {
        List<Long> userIdList = new ArrayList<>();
        allUserList.forEach(
                userDataScope -> userIdList.add(Long.valueOf(String.valueOf(userDataScope.get("userId"))))
        );
        return userIdList;
    }

    /**
     * 1 所有数据的用户要发送
     *
     * @return
     */
    private List<Long> getParentDeptList(List<Map<String, String>> allUserList, Long deptId) {
        List<Long> userIdList = new ArrayList<>();
        String tempDeptIds = "";
        List<Map<String, Long>> deptList = dataScopeMapper.getDeptList();
        Map<Long, Long> deptMap = deptList.stream().collect(
                Collectors.toMap(e -> Long.valueOf(e.get("id")), e -> Long.valueOf(e.get("pid")))
        );
        while (deptId != -1) {
            tempDeptIds += "-" + deptId;
            deptId = deptMap.get(deptId);
        }
        final String deptIds = tempDeptIds;
        allUserList.forEach(
                userDataScope -> {
                    if (deptIds.contains(String.valueOf(userDataScope.get("deptid")))) {
                        userIdList.add(Long.valueOf(String.valueOf(userDataScope.get("userId"))));
                    }
                }
        );
        return userIdList;
    }

    private List<Long> customDeptList(List<Map<String, String>> userList, Long deptId) {
        List<Long> userIdList = new ArrayList<>();
        userList.forEach(
                userDataScope -> {
                    if (userDataScope.get("dataScope").contains(String.valueOf(deptId))) {
                        userIdList.add(Long.valueOf(String.valueOf(userDataScope.get("userId"))));
                    }
                }
        );
        return userIdList;
    }


    private List<User> userProvider(List<Long> userIdList) {
        List<User> allUserList = dataScopeMapper.getUserList();
        Map<Long, User> usersMap = allUserList.stream().collect(
                Collectors.toMap(e -> e.getId(), Function.identity())
        );
        List<User> scopeUserList = new ArrayList<>();
        userIdList.forEach(
                userId -> {
                    if (usersMap.containsKey(userId)) {
                        scopeUserList.add(usersMap.get(userId));
                    }
                }
        );
        return scopeUserList;
    }

}
