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.OrgEquipmentEntity;
import org.qinan.safetyeval.domain.gateway.OrgEquipmentGateway;
import org.qinan.safetyeval.domain.query.OrgEquipmentQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.infrastructure.dataobject.OrgEquipmentDO;
import org.qinan.safetyeval.infrastructure.mapper.OrgEquipmentMapper;
import org.qinan.safetyeval.infrastructure.support.InsertFieldDefaults;
import org.qinan.safetyeval.infrastructure.support.OrgContextResolver;
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 OrgEquipmentGatewayImpl implements OrgEquipmentGateway {

    @Resource
    private OrgEquipmentMapper orgEquipmentMapper;

    @Resource
    private OrgContextResolver orgContextResolver;

    @Override
    public OrgEquipmentEntity save(OrgEquipmentEntity entity) {
        OrgEquipmentDO dataObject = toDO(entity);
        InsertFieldDefaults.apply(dataObject);
        orgEquipmentMapper.insert(dataObject);
        entity.setId(dataObject.getId());
        return entity;
    }

    @Override
    public OrgEquipmentEntity get(Long id) {
        OrgEquipmentDO dataObject = orgEquipmentMapper.selectById(id);
        return toEntity(dataObject);
    }

    @Override
    public OrgEquipmentEntity modify(OrgEquipmentEntity entity) {
        OrgEquipmentDO dataObject = toDO(entity);
        dataObject.setId(entity.getId());
        InsertFieldDefaults.applyForUpdate(dataObject);
        orgEquipmentMapper.updateById(dataObject);
        return get(entity.getId());
    }

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

    @Override
    public PageResult<OrgEquipmentEntity> page(OrgEquipmentQuery query) {
        LambdaQueryWrapper<OrgEquipmentDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrgEquipmentDO::getDeleteEnum, "false");
        if (query.getOrgId() != null) {
            wrapper.eq(OrgEquipmentDO::getOrgId, query.getOrgId());
        }
        if (StringUtils.hasText(query.getDeviceName())) {
            wrapper.like(OrgEquipmentDO::getDeviceName, query.getDeviceName());
        }
        if (StringUtils.hasText(query.getInstrumentType())) {
            wrapper.and(w -> w.like(OrgEquipmentDO::getInstrumentTypeName, query.getInstrumentType())
                    .or().like(OrgEquipmentDO::getInstrumentTypeCode, query.getInstrumentType()));
        }
        if (StringUtils.hasText(query.getDeviceType())) {
            wrapper.and(w -> w.like(OrgEquipmentDO::getDeviceTypeName, query.getDeviceType())
                    .or().like(OrgEquipmentDO::getDeviceTypeCode, query.getDeviceType()));
        }
        if (query.getEnableFlag() != null) {
            wrapper.eq(OrgEquipmentDO::getEnableFlag, query.getEnableFlag());
        }

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

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

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

    private OrgEquipmentDO toDO(OrgEquipmentEntity entity) {
        OrgEquipmentDO dataObject = new OrgEquipmentDO();
        dataObject.setOrgId(orgContextResolver.resolveOrgId(entity.getOrgId()));
        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.setEnableFlag(entity.getEnableFlag());
        dataObject.setTenantId(entity.getTenantId());
        return dataObject;
    }

    private OrgEquipmentEntity toEntity(OrgEquipmentDO dataObject) {
        if (dataObject == null) {
            return null;
        }
        OrgEquipmentEntity entity = new OrgEquipmentEntity();
        entity.setId(dataObject.getId());
        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.setEnableFlag(dataObject.getEnableFlag());
        entity.setTenantId(dataObject.getTenantId());
        return entity;
    }
}
