package org.qinan.safetyeval.infrastructure.gatewayimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.qinan.safetyeval.domain.entity.QualFilingChangeEntity;
import org.qinan.safetyeval.domain.gateway.QualFilingChangeGateway;
import org.qinan.safetyeval.domain.query.QualFilingChangeQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.infrastructure.dataobject.QualFilingChangeDO;
import org.qinan.safetyeval.infrastructure.mapper.QualFilingChangeMapper;
import org.qinan.safetyeval.infrastructure.support.InsertFieldDefaults;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 备案变更Gateway实现（Infrastructure层）
 *
 * @author safety-eval
 */
@Component
public class QualFilingChangeGatewayImpl implements QualFilingChangeGateway {

    @Resource
    private QualFilingChangeMapper qualFilingChangeMapper;

    @Override
    public QualFilingChangeEntity save(QualFilingChangeEntity entity) {
        QualFilingChangeDO dataObject = toDO(entity);
        InsertFieldDefaults.apply(dataObject);
        qualFilingChangeMapper.insert(dataObject);
        entity.setId(dataObject.getId());
        return entity;
    }

    @Override
    public QualFilingChangeEntity get(Long id) {
        QualFilingChangeDO dataObject = qualFilingChangeMapper.selectById(id);
        return toEntity(dataObject);
    }
@Override
    public List<QualFilingChangeEntity> listByOriginFilingId(Long originFilingId) {
        LambdaQueryWrapper<QualFilingChangeDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingChangeDO::getOriginFilingId, originFilingId);
        wrapper.eq(QualFilingChangeDO::getDeleteEnum, "false");
        List<QualFilingChangeDO> dataObjects = qualFilingChangeMapper.selectList(wrapper);
        return dataObjects.stream().map(this::toEntity).collect(Collectors.toList());
    }

    @Override
    public QualFilingChangeEntity modify(QualFilingChangeEntity entity) {
        QualFilingChangeDO dataObject = toDO(entity);
        dataObject.setId(entity.getId());
        InsertFieldDefaults.applyForUpdate(dataObject);
        qualFilingChangeMapper.updateById(dataObject);
        return get(entity.getId());
    }

    @Override
    public void delete(Long id) {
        qualFilingChangeMapper.deleteById(id);
    }

    @Override
    public PageResult<QualFilingChangeEntity> page(QualFilingChangeQuery query) {
        LambdaQueryWrapper<QualFilingChangeDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingChangeDO::getDeleteEnum, "false");
        if (query.getOrgId() != null) {
            wrapper.eq(QualFilingChangeDO::getOrgId, query.getOrgId());
        }
        if (query.getOriginFilingId() != null) {
            wrapper.eq(QualFilingChangeDO::getOriginFilingId, query.getOriginFilingId());
        }
        if (StringUtils.hasText(query.getFilingNo())) {
            wrapper.eq(QualFilingChangeDO::getFilingNo, query.getFilingNo());
        }
        if (query.getFilingStatusCode() != null) {
            wrapper.eq(QualFilingChangeDO::getFilingStatusCode, query.getFilingStatusCode());
        }

        Page<QualFilingChangeDO> page = new Page<>(query.getPageNum(), query.getPageSize());
        IPage<QualFilingChangeDO> result = qualFilingChangeMapper.selectPage(page, wrapper);

        List<QualFilingChangeEntity> entities = result.getRecords().stream()
                .map(this::toEntity)
                .collect(Collectors.toList());

        return PageResult.of(entities, result.getTotal(), result.getCurrent(), result.getSize());
    }

    private QualFilingChangeDO toDO(QualFilingChangeEntity entity) {
        QualFilingChangeDO dataObject = new QualFilingChangeDO();
        dataObject.setOriginFilingId(entity.getOriginFilingId());
        dataObject.setChangeFilingId(entity.getChangeFilingId());
        dataObject.setRejectReason(entity.getRejectReason());
        dataObject.setSubmitTime(entity.getSubmitTime());
        dataObject.setFilingTerritoryCode(entity.getFilingTerritoryCode());
        dataObject.setFilingTerritoryName(entity.getFilingTerritoryName());
        dataObject.setFilingUnitName(entity.getFilingUnitName());
        dataObject.setFilingUnitTypeCode(entity.getFilingUnitTypeCode());
        dataObject.setFilingUnitTypeName(entity.getFilingUnitTypeName());
        dataObject.setFilingNo(entity.getFilingNo());
        dataObject.setBusinessScope(entity.getBusinessScope());
        dataObject.setRegisterAddress(entity.getRegisterAddress());
        dataObject.setOfficeAddress(entity.getOfficeAddress());
        dataObject.setCreditCode(entity.getCreditCode());
        dataObject.setQualCertNo(entity.getQualCertNo());
        dataObject.setLegalPersonPhone(entity.getLegalPersonPhone());
        dataObject.setContactPhone(entity.getContactPhone());
        dataObject.setInfoDisclosureUrl(entity.getInfoDisclosureUrl());
        dataObject.setFixedAssetAmount(entity.getFixedAssetAmount());
        dataObject.setArchiveRoomArea(entity.getArchiveRoomArea());
        dataObject.setFulltimeEvaluatorCount(entity.getFulltimeEvaluatorCount());
        dataObject.setRegisteredEngineerCount(entity.getRegisteredEngineerCount());
        dataObject.setWorkplaceArea(entity.getWorkplaceArea());
        dataObject.setUnitIntro(entity.getUnitIntro());
        dataObject.setAttachmentUrl(entity.getAttachmentUrl());
        dataObject.setFilingStatusCode(entity.getFilingStatusCode());
        dataObject.setFilingStatusName(entity.getFilingStatusName());
        dataObject.setTenantId(entity.getTenantId());
        return dataObject;
    }

    private QualFilingChangeEntity toEntity(QualFilingChangeDO dataObject) {
        if (dataObject == null) {
            return null;
        }
        QualFilingChangeEntity entity = new QualFilingChangeEntity();
        entity.setId(dataObject.getId());
        entity.setOriginFilingId(dataObject.getOriginFilingId());
        entity.setChangeFilingId(dataObject.getChangeFilingId());
        entity.setRejectReason(dataObject.getRejectReason());
        entity.setSubmitTime(dataObject.getSubmitTime());
        entity.setFilingTerritoryCode(dataObject.getFilingTerritoryCode());
        entity.setFilingTerritoryName(dataObject.getFilingTerritoryName());
        entity.setFilingUnitName(dataObject.getFilingUnitName());
        entity.setFilingUnitTypeCode(dataObject.getFilingUnitTypeCode());
        entity.setFilingUnitTypeName(dataObject.getFilingUnitTypeName());
        entity.setFilingNo(dataObject.getFilingNo());
        entity.setBusinessScope(dataObject.getBusinessScope());
        entity.setRegisterAddress(dataObject.getRegisterAddress());
        entity.setOfficeAddress(dataObject.getOfficeAddress());
        entity.setCreditCode(dataObject.getCreditCode());
        entity.setQualCertNo(dataObject.getQualCertNo());
        entity.setLegalPersonPhone(dataObject.getLegalPersonPhone());
        entity.setContactPhone(dataObject.getContactPhone());
        entity.setInfoDisclosureUrl(dataObject.getInfoDisclosureUrl());
        entity.setFixedAssetAmount(dataObject.getFixedAssetAmount());
        entity.setArchiveRoomArea(dataObject.getArchiveRoomArea());
        entity.setFulltimeEvaluatorCount(dataObject.getFulltimeEvaluatorCount());
        entity.setRegisteredEngineerCount(dataObject.getRegisteredEngineerCount());
        entity.setWorkplaceArea(dataObject.getWorkplaceArea());
        entity.setUnitIntro(dataObject.getUnitIntro());
        entity.setAttachmentUrl(dataObject.getAttachmentUrl());
        entity.setFilingStatusCode(dataObject.getFilingStatusCode());
        entity.setFilingStatusName(dataObject.getFilingStatusName());
        entity.setTenantId(dataObject.getTenantId());
        return entity;
    }
}
