package com.sundot.airport.web.core.cache;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.sundot.airport.common.constant.CacheConstants;
import com.sundot.airport.common.core.domain.entity.SysDept;
import com.sundot.airport.common.core.domain.entity.SysUser;
import com.sundot.airport.common.core.redis.RedisCache;
import com.sundot.airport.common.dto.UserInfo;
import com.sundot.airport.common.enums.DeptType;
import com.sundot.airport.system.service.ISysDeptService;
import com.sundot.airport.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 用户信息缓存
 *
 * @Author: wangchong
 * @Date: 2025/7/11 14:49
 **/
@Component
public class UserCache {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 获取用户信息
     *
     * @param userId 用户id
     * @return 用户信息
     */
    public UserInfo getUserInfo(Long userId) {
        UserInfo userInfo = redisCache.getCacheObject(CacheConstants.USER_INFO_KEY + userId);
        if (Objects.isNull(userInfo)) {
            userInfo = new UserInfo();
            SysUser sysUser = sysUserService.selectUserById(userId);
            BeanUtils.copyProperties(sysUser, userInfo);
            updateDeptInfo(userInfo);
            redisCache.setCacheObject(CacheConstants.USER_INFO_KEY + userId, userInfo);
            redisCache.expire(CacheConstants.USER_INFO_KEY + userId, 300);
        }
        return userInfo;
    }


    /**
     * 获取用户信息
     *
     * @param userName 用户登录账号
     * @return 用户信息
     */
    public UserInfo getUserInfo(String userName) {
        UserInfo userInfo = redisCache.getCacheObject(CacheConstants.USER_INFO_KEY + userName);
        if (Objects.isNull(userInfo)) {
            userInfo = new UserInfo();
            SysUser sysUser = sysUserService.selectUserByUserName(userName);
            BeanUtils.copyProperties(sysUser, userInfo);
            updateDeptInfo(userInfo);
            redisCache.setCacheObject(CacheConstants.USER_INFO_KEY + userName, userInfo);
            redisCache.expire(CacheConstants.USER_INFO_KEY + userName, 300);
        }
        return userInfo;
    }

    private void updateDeptInfo(UserInfo userInfo) {
        if (Objects.isNull(userInfo.getDeptId())) {
            return;
        }
        List<SysDept> deptList = sysDeptService.selectAllDept(userInfo.getDeptId());
        if (CollectionUtil.isEmpty(deptList)) {
            return;
        }
        Collections.reverse(deptList);
        SysDept teams = deptList.stream().filter(x -> StrUtil.equals(DeptType.TEAMS.getCode(), x.getDeptType())).findFirst().orElse(new SysDept());
        SysDept department = deptList.stream().filter(x -> StrUtil.equals(DeptType.MANAGER.getCode(), x.getDeptType())).findFirst().orElse(new SysDept());
        SysDept brigade = deptList.stream().filter(x -> StrUtil.equals(DeptType.BRIGADE.getCode(), x.getDeptType())).findFirst().orElse(new SysDept());
        SysDept station = deptList.stream().filter(x -> StrUtil.equals(DeptType.STATION.getCode(), x.getDeptType())).findFirst().orElse(new SysDept());
        userInfo.setTeamsId(teams.getDeptId());
        userInfo.setTeamsName(teams.getDeptName());
        userInfo.setDepartmentId(department.getDeptId());
        userInfo.setDepartmentName(department.getDeptName());
        userInfo.setBrigadeId(brigade.getDeptId());
        userInfo.setBrigadeName(brigade.getDeptName());
        userInfo.setStationId(station.getDeptId());
        userInfo.setStationName(station.getDeptName());
    }
}
