package com.sundot.airport.system.service.portrait;

import cn.hutool.core.collection.CollectionUtil;
import com.sundot.airport.common.core.domain.entity.SysUser;
import com.sundot.airport.common.domain.portrait.BaseModuleIndicatorResult;
import com.sundot.airport.common.domain.portrait.IndicatorCalculateParams;
import com.sundot.airport.common.enums.portrait.IndicatorType;
import com.sundot.airport.common.enums.portrait.UserType;
import com.sundot.airport.common.service.portrait.Indicator;
import com.sundot.airport.common.utils.SecurityUtils;
import com.sundot.airport.system.domain.portrait.IndicatorResult;
import com.sundot.airport.system.mapper.SysUserMapper;
import com.sundot.airport.system.mapper.portrait.QualificationLevelIndicatorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 资质等级指标类
 * 处理个人和组织的资质等级相关指标计算
 */
@Component
public class QualificationLevelIndicator implements Indicator<Object> {

    /**
     * 指标值
     */
    private Object value;

    /**
     * 资质等级数据访问对象
     */
    @Autowired
    private QualificationLevelIndicatorMapper mapper;


    /**
     * 获取指标名称
     *
     * @return 指标名称
     */
    @Override
    public String getName() {
        return IndicatorType.QUALIFICATION_LEVEL.getCode();
    }

    /**
     * 获取指标值
     *
     * @return 指标值
     */
    @Override
    public Object getValue() {
        return value;
    }

    /**
     * 计算指标值
     * 根据用户类型决定是计算个人资质等级还是组织资质等级统计
     *
     * @param params 用户ID、用户类型、部门ID
     */
    @Override
    public void calculate(IndicatorCalculateParams params) {
        Long userId = params.getUserId();
        UserType userType = params.getUserType();
        List<Long> userIds = params.getUserIds();
        if (UserType.PERSONAL.equals(userType)) {
            // 个人资质等级
            this.value = mapper.queryPersonalQualificationLevel(userId);
        } else {
            // 组织资质等级统计
            this.value = mapper.queryOrgQualificationLevelStats(userIds, userType);
        }
    }

    @Override
    public void setResult(BaseModuleIndicatorResult result) {
        if (result instanceof IndicatorResult) {
            IndicatorResult indicatorResult = (IndicatorResult) result;
            if (value instanceof String) {
                indicatorResult.setQualificationLevel((String) value);
            } else if (value instanceof List) {
                indicatorResult.setQualificationLevelStats((List) value);
            }
        }
    }
}