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

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

/**
 * 身体健康状况指标类
 * 处理个人和组织的身体健康状况相关指标计算
 */
@Component
public class PhysicalHealthIndicator implements Indicator<Object> {

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

    /**
     * 身体健康状况数据访问对象
     */
    @Autowired
    private PhysicalHealthIndicatorMapper mapper;

    /**
     * 获取指标名称
     *
     * @return 指标名称
     */
    @Override
    public String getName() {
        return IndicatorType.PHYSICAL_HEALTH_STATUS.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.queryPersonalHealthStatus(userId);
        } else {
            // 组织身体健康状况统计
            this.value = mapper.queryOrgHealthStats(userIds, userType);
        }
    }

    @Override
    public void setResult(BaseModuleIndicatorResult result) {
        if (result instanceof IndicatorResult) {
            IndicatorResult indicatorResult = (IndicatorResult) result;
            if (value instanceof String) {
                indicatorResult.setIsHealthy(value.toString());
            } else if (value instanceof PhysicalHealthStats) {
                indicatorResult.setPhysicalHealthStats((PhysicalHealthStats) value);
            }
        }
    }
}