package org.qinan.safetyeval.infrastructure.persistence.repository.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jjb.saas.framework.enums.enums.BooleanEnum;
import com.jjb.saas.framework.repository.repo.impl.BaseRepositoryImpl;
import lombok.AllArgsConstructor;
import org.qinan.safetyeval.client.change.request.QualFilingEquipmentChangePageQry;
import org.qinan.safetyeval.domain.constant.BooleanQAEnum;
import org.qinan.safetyeval.infrastructure.persistence.domainobject.QualFilingEquipmentChangeDO;
import org.qinan.safetyeval.infrastructure.persistence.mapper.QualFilingEquipmentChangeMapper;
import org.qinan.safetyeval.infrastructure.persistence.repository.QualFilingEquipmentChangeRepository;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 备案申请装备-变更
 *
 * @Author: ltq
 * @date 2026-07-02 10:03:59
 */
@Service
@AllArgsConstructor
public class QualFilingEquipmentChangeRepositoryImpl extends BaseRepositoryImpl<QualFilingEquipmentChangeMapper, QualFilingEquipmentChangeDO> implements QualFilingEquipmentChangeRepository {

    private final QualFilingEquipmentChangeMapper qualFilingEquipmentChangeMapper;

    @Override
    public QualFilingEquipmentChangeDO getById(Long id) {
        return super.lambdaQuery().eq(QualFilingEquipmentChangeDO::getId, id).eq(QualFilingEquipmentChangeDO::getDeleteEnum, BooleanQAEnum.FALSE.getValue()).one();
    }

    @Override
    public IPage<QualFilingEquipmentChangeDO> listQualFilingEquipmentChangePage(IPage iPage, QualFilingEquipmentChangePageQry qualFilingEquipmentChangePageQry) {
        QueryWrapper<QualFilingEquipmentChangeDO> qualFilingEquipmentChangeDoQueryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getFilingChangeId())) {
            qualFilingEquipmentChangeDoQueryWrapper.eq("filing_change_id", qualFilingEquipmentChangePageQry.getFilingChangeId());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getSourceEquipmentId())) {
            qualFilingEquipmentChangeDoQueryWrapper.eq("source_equipment_id", qualFilingEquipmentChangePageQry.getSourceEquipmentId());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getDeviceName())) {
            qualFilingEquipmentChangeDoQueryWrapper.like("device_name", qualFilingEquipmentChangePageQry.getDeviceName());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getDeviceModel())) {
            qualFilingEquipmentChangeDoQueryWrapper.eq("device_model", qualFilingEquipmentChangePageQry.getDeviceModel());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getInstrumentTypeCode())) {
            qualFilingEquipmentChangeDoQueryWrapper.eq("instrument_type_code", qualFilingEquipmentChangePageQry.getInstrumentTypeCode());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getInstrumentTypeName())) {
            qualFilingEquipmentChangeDoQueryWrapper.like("instrument_type_name", qualFilingEquipmentChangePageQry.getInstrumentTypeName());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getDeviceTypeCode())) {
            qualFilingEquipmentChangeDoQueryWrapper.eq("device_type_code", qualFilingEquipmentChangePageQry.getDeviceTypeCode());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getDeviceTypeName())) {
            qualFilingEquipmentChangeDoQueryWrapper.like("device_type_name", qualFilingEquipmentChangePageQry.getDeviceTypeName());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getManufacturer())) {
            qualFilingEquipmentChangeDoQueryWrapper.eq("manufacturer", qualFilingEquipmentChangePageQry.getManufacturer());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getFlowDesc())) {
            qualFilingEquipmentChangeDoQueryWrapper.eq("flow_desc", qualFilingEquipmentChangePageQry.getFlowDesc());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getMinFlow())) {
            qualFilingEquipmentChangeDoQueryWrapper.eq("min_flow", qualFilingEquipmentChangePageQry.getMinFlow());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getMaxFlow())) {
            qualFilingEquipmentChangeDoQueryWrapper.eq("max_flow", qualFilingEquipmentChangePageQry.getMaxFlow());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getCalibrationUnit())) {
            qualFilingEquipmentChangeDoQueryWrapper.eq("calibration_unit", qualFilingEquipmentChangePageQry.getCalibrationUnit());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getCalibrationInitValue())) {
            qualFilingEquipmentChangeDoQueryWrapper.eq("calibration_init_value", qualFilingEquipmentChangePageQry.getCalibrationInitValue());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getFieldCalibrationTypeCode())) {
            qualFilingEquipmentChangeDoQueryWrapper.eq("field_calibration_type_code", qualFilingEquipmentChangePageQry.getFieldCalibrationTypeCode());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getFieldCalibrationTypeName())) {
            qualFilingEquipmentChangeDoQueryWrapper.like("field_calibration_type_name", qualFilingEquipmentChangePageQry.getFieldCalibrationTypeName());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getDualChannelFlag())) {
            qualFilingEquipmentChangeDoQueryWrapper.eq("dual_channel_flag", qualFilingEquipmentChangePageQry.getDualChannelFlag());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getCalibrationReportUrl())) {
            qualFilingEquipmentChangeDoQueryWrapper.eq("calibration_report_url", qualFilingEquipmentChangePageQry.getCalibrationReportUrl());
        }
        if (ObjectUtil.isNotEmpty(qualFilingEquipmentChangePageQry.getEnv())) {
            qualFilingEquipmentChangeDoQueryWrapper.eq("env", qualFilingEquipmentChangePageQry.getEnv());
        }
        qualFilingEquipmentChangeDoQueryWrapper.eq("delete_enum", BooleanQAEnum.FALSE.getValue());
        return qualFilingEquipmentChangeMapper.selectPage(iPage, qualFilingEquipmentChangeDoQueryWrapper);
    }

    @Override
    public Boolean addQualFilingEquipmentChange(QualFilingEquipmentChangeDO qualFilingEquipmentChangeDO) {
        return super.save(qualFilingEquipmentChangeDO);
    }

    @Override
    public Boolean updateQualFilingEquipmentChange(QualFilingEquipmentChangeDO qualFilingEquipmentChangeDO) {
        return super.updateById(qualFilingEquipmentChangeDO);
    }

    @Override
    public Boolean deletedQualFilingEquipmentChangeById(Long id) {
        return super.removeEnById(id);
    }

    @Override
    public Boolean deletedQualFilingEquipmentChangeByIds(Long[] ids) {
        return super.removeBatchEnByIds(Arrays.asList(ids));
    }

    @Override
    public List<QualFilingEquipmentChangeDO> listByFilingId(Long filingId) {
        return super.lambdaQuery()
                .eq(QualFilingEquipmentChangeDO::getFilingChangeId, filingId)
                .eq(QualFilingEquipmentChangeDO::getDeleteEnum, BooleanQAEnum.FALSE.getValue())
                .list();
    }
}
