package com.yh.saas.plugin.base.service.impl;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.winsea.svc.base.base.entity.CommonCompany;
import com.winsea.svc.base.base.entity.CommonStaffVessel;
import com.winsea.svc.base.base.entity.CommonVessel;
import com.winsea.svc.base.base.service.ICommonCompanyService;
import com.winsea.svc.base.base.service.ICommonStaffVesselService;
import com.winsea.svc.base.base.service.ICommonVesselService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 记录Common共通查询 服务实现类
 * </p>
 *
 * @author SongJin
 * @since 2020-01-03
 */
@Component
@Slf4j
public class CommonInfoImpl {

    @SofaReference
    private ICommonStaffVesselService commonStaffVesselService;

    @SofaReference
    private ICommonVesselService vesselService;

    @SofaReference
    private ICommonCompanyService commonCompanyService;

    /**
     * 根据staffId查询关联船舶ID信息
     *
     * @param staffId
     * @return
     */
    public List<String> getVesselIdsByStaffId(String staffId) {
        List<String> vesselIds = new ArrayList<>();
        List<CommonStaffVessel> infos = this.commonStaffVesselService.findStaffVessel(staffId);
        if (CollectionUtils.isNotEmpty(infos)) {
            vesselIds = infos.stream().map(CommonStaffVessel::getVesselId).collect(Collectors.toList());
        }
        return vesselIds;
    }

    /**
     * 获取公司名称
     *
     * @param vesselId
     * @return
     */
    public String getCompNameByVesselId(String vesselId) {
        String compName = "";
        if (StringUtils.isNotBlank(vesselId)) {
            CommonVessel vessel = this.vesselService.selectById(vesselId);
            if (vessel != null && StringUtils.isNotBlank(vessel.getUnderCompId())) {
                CommonCompany company = this.commonCompanyService.selectById(vessel.getUnderCompId());
                if (company != null) {
                    compName = company.getCompName();
                }
            }
        }
        return compName;
    }
}