package com.hotwj.platform.common.helper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hotwj.platform.driverManagement.driver.domain.HotDriver;
import com.hotwj.platform.driverManagement.driver.mapper.HotDriverMapper;
import com.hotwj.platform.resourceManagement.company.domain.vo.SysUserLoginPortVo;
import com.hotwj.platform.resourceManagement.company.service.ISysUserLoginPortService;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.satoken.utils.LoginHelper;

import java.util.List;

/**
 * 驾驶员登录上下文帮助类
 *
 * @author shihongwei
 * @date 2026/02/22
 */
public final class DriverLoginContextHelper {

    private DriverLoginContextHelper() {
    }

    public static boolean isDriverPort() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            return false;
        }
        return StringUtils.equals(loginUser.getLoginPort(), ISysUserLoginPortService.DRIVER_PORT);
    }

    public static HotDriver getCurrentDriver() {
        if (!isDriverPort()) {
            throw new ServiceException("当前登录端口不是驾驶员端");
        }
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("登录信息不存在");
        }
        String businessUserId = loginUser.getBusinessUserId();
        if (StringUtils.isBlank(businessUserId)) {
            Long userId = loginUser.getUserId();
            if (userId == null) {
                throw new ServiceException("用户ID不存在");
            }
            ISysUserLoginPortService userLoginPortService = SpringUtils.getBean(ISysUserLoginPortService.class);
            List<SysUserLoginPortVo> ports = userLoginPortService.selectLoginPortListByPort(userId, ISysUserLoginPortService.DRIVER_PORT);
            Long companyId = ports.stream()
                    .map(SysUserLoginPortVo::getCompanyId)
                    .filter(id -> id != null)
                    .findFirst()
                    .orElse(null);
            if (companyId == null) {
                throw new ServiceException("未找到驾驶员所属企业信息");
            }
            LambdaQueryWrapper<HotDriver> lqw = new LambdaQueryWrapper<>();
            lqw.eq(HotDriver::getCompanyId, companyId);
            lqw.eq(HotDriver::getId, userId);
            lqw.eq(HotDriver::getStatus, 1L);
            lqw.eq(HotDriver::getAuditStatus, 1);
            lqw.orderByDesc(HotDriver::getCreateTime);
            lqw.last("limit 1");
            HotDriverMapper hotDriverMapper = SpringUtils.getBean(HotDriverMapper.class);
            HotDriver driver = hotDriverMapper.selectOne(lqw);
            if (driver == null) {
                throw new ServiceException("未找到当前登录用户对应的驾驶员信息");
            }
            return driver;
        }
        HotDriverMapper hotDriverMapper = SpringUtils.getBean(HotDriverMapper.class);
        HotDriver driver = hotDriverMapper.selectById(businessUserId);
        if (driver == null) {
            throw new ServiceException("驾驶员信息不存在");
        }
        return driver;
    }
}
