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.mapper.portrait.PoliticalStatusIndicatorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 政治面貌指标类
 */
@Component
public class PoliticalStatusIndicator implements Indicator<Object> {

    private Object value;

    @Autowired
    private PoliticalStatusIndicatorMapper mapper;

    @Override
    public String getName() {
        return IndicatorType.POLITICAL_STATUS.getCode();
    }

    @Override
    public Object getValue() {
        return value;
    }

    @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.queryPersonalPoliticalStatus(userId);
        } else {
            // 组织政治面貌统计
            this.value = mapper.queryOrgPoliticalStatusStats(userIds, userType);
        }
    }

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