package com.gci.boot.modules.vehicles.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gci.boot.constant.CommonConstant;
import com.gci.boot.modules.system.entity.SysRole;
import com.gci.boot.modules.system.entity.SysUser;
import com.gci.boot.modules.system.entity.SysUserRole;
import com.gci.boot.modules.system.service.*;
import com.gci.boot.modules.vehicles.service.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CommonServiceImpl implements CommonService {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysUserRoleService sysUserRoleService;
    @Autowired
    private ISysRoleService sysRoleService;


    @Override
    public String getAuthority(String userName) {
        SysUser sysUser = sysUserService.getByUsername(userName);
        if(Objects.isNull(sysUser)){
            return null;
        }
        //1-司机，2-维修厂，3-司机&维修厂，4-维保管理员，5-维保领导，6-维保管理员&维保领导
        //查用户角色
        LambdaQueryWrapper<SysUserRole> userRoleQuery = new LambdaQueryWrapper<>();
        userRoleQuery.eq(SysUserRole::getUserId,sysUser.getId());
        List<SysUserRole> sysUserRoleList = sysUserRoleService.list(userRoleQuery);
        if(Objects.isNull(sysUserRoleList) || sysUserRoleList.size() == 0){
            return null;
        }
        List<String> roleIds = sysUserRoleList.stream().filter(s-> StrUtil.isNotBlank(s.getRoleId()))
                .map(SysUserRole::getRoleId).distinct().collect(Collectors.toList());
        if(Objects.isNull(roleIds) || roleIds.size() == 0){
            return null;
        }

        LambdaQueryWrapper<SysRole> roleQuery = new LambdaQueryWrapper<>();
        roleQuery.in(SysRole::getId,roleIds);
        List<SysRole> roleList = sysRoleService.list(roleQuery);
        if(Objects.isNull(roleList) || roleList.size() == 0){
            return null;
        }
        List<String> roleCodes = roleList.stream().map(SysRole::getRoleCode).distinct().collect(Collectors.toList());
        if(Objects.isNull(roleCodes) || roleList.size() == 0){
            return null;
        }
        return getAuthorityType(roleCodes);
    }

    public String getAuthorityType(List<String> roleCodes){
        boolean hasDriver = false;
        boolean hasRepairAdmin = false;
        boolean hasMaintenanceLeader = false;
        boolean hasMaintenanceAdmin = false;
        for (String roleCode : roleCodes) {
            switch (roleCode) {
                case CommonConstant.Driver:
                    hasDriver = true;
                    break;
                case CommonConstant.Repair_admin:
                    hasRepairAdmin = true;
                    break;
                case CommonConstant.Maintenance_leader:
                    hasMaintenanceLeader = true;
                    break;
                case CommonConstant.Maintenance_admin:
                    hasMaintenanceAdmin = true;
                    break;
            }
        }
        //1-司机，2-维修厂，3-司机&维修厂，4-维保管理员，5-维保领导，6-维保管理员&维保领导
        if (hasDriver && !hasRepairAdmin && !hasMaintenanceLeader && !hasMaintenanceAdmin) {
            return "1";
        } else if (!hasDriver && hasRepairAdmin && !hasMaintenanceLeader && !hasMaintenanceAdmin) {
            return "2";
        } else if (hasDriver && hasRepairAdmin && !hasMaintenanceLeader && !hasMaintenanceAdmin) {
            return "3";
//            //多个身份的默认返回最高身份
//            return "2";
        }else if (!hasMaintenanceLeader && hasMaintenanceAdmin) {
            return "4";
        } else if (hasMaintenanceLeader && !hasMaintenanceAdmin) {
            return "5";
        } else if (hasMaintenanceLeader && hasMaintenanceAdmin) {
            return "6";
//            //多个身份的默认返回最高身份
//            return "5";
        }
        return null;
    }
}
