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

/**
 * 安检工作职务指标实现类
 */
@Component
public class SecurityWorkPositionIndicator implements Indicator<Object> {

    @Autowired
    private SecurityWorkPositionIndicatorMapper securityWorkPositionIndicatorMapper;

    private Object value;

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

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

    @Override
    public void calculate(IndicatorCalculateParams params) {
        Long userId = params.getUserId();
        UserType userType = params.getUserType();
        if (UserType.PERSONAL.equals(userType)) {
            // 个人在安检工作中担任过的最高职务
            this.value = securityWorkPositionIndicatorMapper.queryPersonalSecurityWorkPosition(userId);
        } else {
            // 组织担任过班组长的人数统计
            this.value = securityWorkPositionIndicatorMapper.queryOrgSecurityWorkPositionStats(params.getUserIds(), userType);
        }
    }

    @Override
    public void setResult(BaseModuleIndicatorResult result) {
        if (result instanceof IndicatorResult) {
            IndicatorResult indicatorResult = (IndicatorResult) result;
            if (value instanceof String) {
                indicatorResult.setSecurityWorkPosition((String) value);
            } else if (value instanceof SecurityWorkPositionStats) {
                indicatorResult.setSecurityWorkPositionStats((SecurityWorkPositionStats) value);
            }
        }
    }
}