package org.qinan.safetyeval.app.support;

import org.qinan.safetyeval.domain.entity.OrgEquipmentEntity;
import org.qinan.safetyeval.domain.entity.QualFilingEquipmentEntity;
import org.qinan.safetyeval.domain.exception.BizException;
import org.qinan.safetyeval.domain.exception.ErrorCode;
import org.qinan.safetyeval.domain.gateway.OrgEquipmentGateway;
import org.qinan.safetyeval.domain.gateway.QualFilingEquipmentGateway;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 从企业装备库批量引入备案装备快照
 */
@Component
public class QualFilingOrgEquipmentImporter {

    @Resource
    private OrgEquipmentGateway orgEquipmentGateway;

    @Resource
    private QualFilingEquipmentGateway qualFilingEquipmentGateway;

    public int importFromOrg(Long filingId, List<Long> sourceEquipmentIds) {
        if (sourceEquipmentIds == null || sourceEquipmentIds.isEmpty()) {
            return 0;
        }
        Set<Long> existing = new HashSet<>();
        for (QualFilingEquipmentEntity item : qualFilingEquipmentGateway.listByFilingId(filingId)) {
            if (item.getSourceEquipmentId() != null) {
                existing.add(item.getSourceEquipmentId());
            }
        }
        int count = 0;
        for (Long sourceId : sourceEquipmentIds) {
            if (existing.contains(sourceId)) {
                throw new BizException(ErrorCode.QUAL_FILING_EQUIPMENT_DUPLICATE);
            }
            OrgEquipmentEntity source = orgEquipmentGateway.get(sourceId);
            if (source == null) {
                throw new BizException(ErrorCode.ORG_EQUIPMENT_NOT_FOUND);
            }
            qualFilingEquipmentGateway.save(toFilingEquipment(filingId, source));
            count++;
        }
        return count;
    }

    private QualFilingEquipmentEntity toFilingEquipment(Long filingId, OrgEquipmentEntity source) {
        QualFilingEquipmentEntity entity = new QualFilingEquipmentEntity();
        entity.setFilingId(filingId);
        entity.setSourceEquipmentId(source.getId());
        entity.setDeviceName(source.getDeviceName());
        entity.setDeviceModel(source.getDeviceModel());
        entity.setInstrumentTypeCode(source.getInstrumentTypeCode());
        entity.setInstrumentTypeName(source.getInstrumentTypeName());
        entity.setDeviceTypeCode(source.getDeviceTypeCode());
        entity.setDeviceTypeName(source.getDeviceTypeName());
        entity.setManufacturer(source.getManufacturer());
        entity.setFlowDesc(source.getFlowDesc());
        entity.setMinFlow(source.getMinFlow());
        entity.setMaxFlow(source.getMaxFlow());
        entity.setCalibrationUnit(source.getCalibrationUnit());
        entity.setCalibrationInitValue(source.getCalibrationInitValue());
        entity.setFieldCalibrationTypeCode(source.getFieldCalibrationTypeCode());
        entity.setFieldCalibrationTypeName(source.getFieldCalibrationTypeName());
        entity.setDualChannelFlag(source.getDualChannelFlag());
        return entity;
    }
}
