package org.qinan.safetyeval.domain.service;

import org.qinan.safetyeval.domain.change.model.QualFilingCommitmentChangeE;
import org.qinan.safetyeval.domain.change.model.QualFilingEquipmentChangeE;
import org.qinan.safetyeval.domain.change.model.QualFilingMaterialChangeE;
import org.qinan.safetyeval.domain.change.model.QualFilingPersonnelCertChangeE;
import org.qinan.safetyeval.domain.change.model.QualFilingPersonnelChangeE;
import org.qinan.safetyeval.domain.entity.QualFilingChangeEntity;
import org.qinan.safetyeval.domain.exception.BizException;
import org.qinan.safetyeval.domain.exception.ErrorCode;
import org.qinan.safetyeval.domain.gateway.QualFilingChangeGateway;
import org.qinan.safetyeval.domain.query.QualFilingChangeQuery;
import org.qinan.safetyeval.domain.query.PageResult;

import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 备案变更领域服务
 *
 * @author safety-eval
 */
@Service
public class QualFilingChangeDomainService {

    @Resource
    private QualFilingChangeGateway qualFilingChangeGateway;

    public QualFilingChangeEntity add(QualFilingChangeEntity entity) {
        return qualFilingChangeGateway.save(entity);
    }

    public QualFilingChangeEntity get(Long id) {
        return qualFilingChangeGateway.get(id);
    }

    public List<QualFilingChangeEntity> listByFilingId(Long filingId) {
        return qualFilingChangeGateway.listByFilingId(filingId);
    }

    public int countByFilingIdAndStatusNot(Long filingId, Integer excludeStatusCode) {
        return qualFilingChangeGateway.countByFilingIdAndStatusNot(filingId, excludeStatusCode);
    }

    public QualFilingChangeEntity modify(QualFilingChangeEntity entity) {
        QualFilingChangeEntity existing = qualFilingChangeGateway.get(entity.getId());
        if (existing == null) {
            throw new BizException(ErrorCode.QUAL_FILING_CHANGE_NOT_FOUND);
        }
        return qualFilingChangeGateway.modify(entity);
    }

    public void delete(Long id) {
        QualFilingChangeEntity existing = qualFilingChangeGateway.get(id);
        if (existing == null) {
            throw new BizException(ErrorCode.QUAL_FILING_CHANGE_NOT_FOUND);
        }
        qualFilingChangeGateway.delete(id);
    }

    public PageResult<QualFilingChangeEntity> page(QualFilingChangeQuery query) {
        return qualFilingChangeGateway.page(query);
    }

    public Long aggregationSaveOrEdit(QualFilingChangeEntity entity, QualFilingCommitmentChangeE commitmentEntity,
                                      List<QualFilingEquipmentChangeE> equipmentEntities, List<QualFilingMaterialChangeE> materialEntities,
                                      List<QualFilingPersonnelChangeE> personnelEntities, List<QualFilingPersonnelCertChangeE> personnelCertEntities, boolean add) {
        return qualFilingChangeGateway.aggregationSaveOrEdit(entity, commitmentEntity, equipmentEntities, materialEntities
                , personnelEntities, personnelCertEntities,add);
    }
}
