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.QualFilingEquipmentEntity;
import org.qinan.safetyeval.domain.gateway.QualFilingEquipmentGateway;
import org.qinan.safetyeval.domain.query.QualFilingEquipmentQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.infrastructure.dataobject.QualFilingEquipmentDO;
import org.qinan.safetyeval.infrastructure.mapper.QualFilingEquipmentMapper;
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 QualFilingEquipmentGatewayImpl implements QualFilingEquipmentGateway {

    @Resource
    private QualFilingEquipmentMapper qualFilingEquipmentMapper;

    @Override
    public QualFilingEquipmentEntity save(QualFilingEquipmentEntity entity) {
        QualFilingEquipmentDO dataObject = toDO(entity);
        InsertFieldDefaults.apply(dataObject);
        qualFilingEquipmentMapper.insert(dataObject);
        entity.setId(dataObject.getId());
        return entity;
    }

    @Override
    public QualFilingEquipmentEntity get(Long id) {
        QualFilingEquipmentDO dataObject = qualFilingEquipmentMapper.selectById(id);
        return toEntity(dataObject);
    }
@Override
    public List<QualFilingEquipmentEntity> listByFilingId(Long filingId) {
        LambdaQueryWrapper<QualFilingEquipmentDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingEquipmentDO::getFilingId, filingId);
        wrapper.eq(QualFilingEquipmentDO::getDeleteEnum, "false");
        List<QualFilingEquipmentDO> dataObjects = qualFilingEquipmentMapper.selectList(wrapper);
        return dataObjects.stream().map(this::toEntity).collect(Collectors.toList());
    }

    @Override
    public QualFilingEquipmentEntity modify(QualFilingEquipmentEntity entity) {
        QualFilingEquipmentDO dataObject = toDO(entity);
        dataObject.setId(entity.getId());
        InsertFieldDefaults.applyForUpdate(dataObject);
        qualFilingEquipmentMapper.updateById(dataObject);
        return get(entity.getId());
    }

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

    @Override
    public PageResult<QualFilingEquipmentEntity> page(QualFilingEquipmentQuery query) {
        LambdaQueryWrapper<QualFilingEquipmentDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingEquipmentDO::getDeleteEnum, "false");
        if (query.getFilingId() != null) {
            wrapper.eq(QualFilingEquipmentDO::getFilingId, query.getFilingId());
        }
        if (StringUtils.hasText(query.getDeviceName())) {
            wrapper.like(QualFilingEquipmentDO::getDeviceName, query.getDeviceName());
        }

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

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

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

    private QualFilingEquipmentDO toDO(QualFilingEquipmentEntity entity) {
        QualFilingEquipmentDO dataObject = new QualFilingEquipmentDO();
        dataObject.setFilingId(entity.getFilingId());
        dataObject.setSourceEquipmentId(entity.getSourceEquipmentId());
        dataObject.setDeviceName(entity.getDeviceName());
        dataObject.setDeviceModel(entity.getDeviceModel());
        dataObject.setInstrumentTypeCode(entity.getInstrumentTypeCode());
        dataObject.setInstrumentTypeName(entity.getInstrumentTypeName());
        dataObject.setDeviceTypeCode(entity.getDeviceTypeCode());
        dataObject.setDeviceTypeName(entity.getDeviceTypeName());
        dataObject.setManufacturer(entity.getManufacturer());
        dataObject.setFlowDesc(entity.getFlowDesc());
        dataObject.setMinFlow(entity.getMinFlow());
        dataObject.setMaxFlow(entity.getMaxFlow());
        dataObject.setCalibrationUnit(entity.getCalibrationUnit());
        dataObject.setCalibrationInitValue(entity.getCalibrationInitValue());
        dataObject.setFieldCalibrationTypeCode(entity.getFieldCalibrationTypeCode());
        dataObject.setFieldCalibrationTypeName(entity.getFieldCalibrationTypeName());
        dataObject.setDualChannelFlag(entity.getDualChannelFlag());
        dataObject.setCalibrationReportUrl(entity.getCalibrationReportUrl());
        dataObject.setTenantId(entity.getTenantId());
        return dataObject;
    }

    private QualFilingEquipmentEntity toEntity(QualFilingEquipmentDO dataObject) {
        if (dataObject == null) {
            return null;
        }
        QualFilingEquipmentEntity entity = new QualFilingEquipmentEntity();
        entity.setId(dataObject.getId());
        entity.setFilingId(dataObject.getFilingId());
        entity.setSourceEquipmentId(dataObject.getSourceEquipmentId());
        entity.setDeviceName(dataObject.getDeviceName());
        entity.setDeviceModel(dataObject.getDeviceModel());
        entity.setInstrumentTypeCode(dataObject.getInstrumentTypeCode());
        entity.setInstrumentTypeName(dataObject.getInstrumentTypeName());
        entity.setDeviceTypeCode(dataObject.getDeviceTypeCode());
        entity.setDeviceTypeName(dataObject.getDeviceTypeName());
        entity.setManufacturer(dataObject.getManufacturer());
        entity.setFlowDesc(dataObject.getFlowDesc());
        entity.setMinFlow(dataObject.getMinFlow());
        entity.setMaxFlow(dataObject.getMaxFlow());
        entity.setCalibrationUnit(dataObject.getCalibrationUnit());
        entity.setCalibrationInitValue(dataObject.getCalibrationInitValue());
        entity.setFieldCalibrationTypeCode(dataObject.getFieldCalibrationTypeCode());
        entity.setFieldCalibrationTypeName(dataObject.getFieldCalibrationTypeName());
        entity.setDualChannelFlag(dataObject.getDualChannelFlag());
        entity.setCalibrationReportUrl(dataObject.getCalibrationReportUrl());
        entity.setTenantId(dataObject.getTenantId());
        return entity;
    }
}
