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

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.system.domain.portrait.AgeStats;
import com.sundot.airport.system.domain.portrait.IndicatorResult;
import com.sundot.airport.system.mapper.portrait.AgeIndicatorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 年龄指标类
 * 处理个人和组织的年龄相关指标计算
 */
@Component
public class AgeIndicator implements Indicator<Object> {

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

    /**
     * 年龄数据访问对象
     */
    @Autowired
    private AgeIndicatorMapper mapper;

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

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

    /**
     * 计算指标值
     * 根据用户类型决定是计算个人年龄还是组织年龄统计
     *
     * @param params 参数
     */
    @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.queryPersonalAge(userId);
        } else {
            // 组织年龄统计
            this.value = mapper.queryOrgAgeStats(userIds, userType);
        }
    }

    @Override
    public void setResult(BaseModuleIndicatorResult result) {
        if (result instanceof IndicatorResult) {
            IndicatorResult indicatorResult = (IndicatorResult) result;
            if (value instanceof Integer) {
                indicatorResult.setAge((Integer) value);
            } else if (value instanceof AgeStats) {
                indicatorResult.setAgeStats((AgeStats) value);
            }
        }
    }
}