package com.springboot.cloud.sysadmin.organization.service.impl;

/*
 **********************************************
 *      DATE           PERSON       REASON
 *    2020-12-29          FXY        Created
 **********************************************
 */


import com.springboot.cloud.sysadmin.organization.entity.po.User;
import com.springboot.cloud.sysadmin.organization.entity.po.application.ApplicationPopedom;
import com.springboot.cloud.sysadmin.organization.entity.po.company.CompanyApplicationPopedom;
import com.springboot.cloud.sysadmin.organization.entity.po.company.CompanyRolePopedom;
import com.springboot.cloud.sysadmin.organization.entity.po.company.CompanyRoleUser;
import com.springboot.cloud.sysadmin.organization.entity.po.company.CompanyUser;
import com.springboot.cloud.sysadmin.organization.entity.vo.Popedom;
import com.springboot.cloud.sysadmin.organization.service.CompanyService;
import com.springboot.cloud.sysadmin.organization.service.UserService;
import com.springboot.cloud.sysadmin.organization.service.application.ApplicationPopedomService;
import com.springboot.cloud.sysadmin.organization.service.company.CompanyApplicationPopedomService;
import com.springboot.cloud.sysadmin.organization.service.company.CompanyRolePopedomService;
import com.springboot.cloud.sysadmin.organization.service.company.CompanyRoleUserService;
import com.springboot.cloud.sysadmin.organization.service.company.CompanyUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@Slf4j
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    UserService userService;

    @Autowired
    CompanyUserService companyUserService;

    @Autowired
    CompanyRoleUserService companyRoleUserService;

    @Autowired
    CompanyRolePopedomService companyRolePopedomService;

    @Autowired
    ApplicationPopedomService applicationPopedomService;

    @Autowired
    CompanyApplicationPopedomService companyApplicationPopedomService;

    @Override
    public List<CompanyRoleUser> getCompanyRoleUser(String companyId, String userId) {
        return companyRoleUserService.getRoleByCompanyIdAndUserId(companyId, userId);
    }

    @Override
    public List<CompanyRoleUser> getCompanyRoleUser(String userId) {
        //找到用户对应的所有公司
        List<CompanyUser> companyUsers = companyUserService.getCompanyByUserId(userId);
        //找到用户默认的公司
        CompanyUser companyUser = companyUsers.stream().filter(a -> a.getIsDefault() == 1).findAny().orElseGet(CompanyUser::new);

        //找到用户的所有角色
        return companyRoleUserService.getRoleByCompanyIdAndUserId(companyUser.getCompanyId(), userId);
    }

    @Override
    public List<CompanyRolePopedom> getCompanyRolePopedom(String companyId, String roleId) {
        return companyRolePopedomService.getCompanyRolePopedom(companyId, roleId);
    }

    @Override
    public List<CompanyRolePopedom> getCompanyRolePopedom(String companyId, List<String> roleIds) {
        return companyRolePopedomService.getCompanyRolePopedom(companyId, roleIds);
    }

    @Override
    public List<ApplicationPopedom> getCompanyUserPopedom(String companyId, String username) {
        User user = userService.getUserByUsernameOrMobile(username);
        List<CompanyRoleUser> companyRoleUsers = getCompanyRoleUser(companyId, user.getId());
        List<String> roleIds = companyRoleUsers.stream().map(CompanyRoleUser::getRoleId).collect(Collectors.toList());
        List<CompanyRolePopedom> companyRolePopedoms = getCompanyRolePopedom(companyId, roleIds);
        List<String> popedomIds = companyRolePopedoms.stream().map(CompanyRolePopedom::getPopedomId).collect(Collectors.toList());
        return applicationPopedomService.getApplicationPopedom(popedomIds);
    }

    @Override
    public List<ApplicationPopedom> getCompanyAllPopedom(String companyId) {
        List<CompanyApplicationPopedom> companyApplicationPopedoms = companyApplicationPopedomService.getApplicationPopedom(companyId);
        List<String> popedomIds = companyApplicationPopedoms.stream().map(CompanyApplicationPopedom::getPopedomId).collect(Collectors.toList());
        return applicationPopedomService.getApplicationPopedom(popedomIds);
    }

    @Override
    public List<Popedom> getAllPopedom() {
        //先获取所有公司的功能
        List<CompanyApplicationPopedom> companyApplicationPopedoms = companyApplicationPopedomService.getApplicationPopedom();
        Map<String, List<String>> company2PopedomIds = companyApplicationPopedoms.stream().collect(Collectors.groupingBy(CompanyApplicationPopedom::getCompanyId, Collectors.mapping(CompanyApplicationPopedom::getPopedomId, Collectors.toList())));
        return company2PopedomIds.keySet().stream().flatMap(a -> applicationPopedomService.getApplicationPopedom(company2PopedomIds.get(a)).stream().map(b -> new Popedom(b, a))).collect(Collectors.toList());
    }

    @Override
    public List<CompanyUser> getUserAllCompany(String userId) {
        return companyUserService.getCompanyByUserId(userId);
    }
}
