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.QualFilingPersonnelCertChangePageQry;
import org.qinan.safetyeval.domain.constant.BooleanQAEnum;
import org.qinan.safetyeval.infrastructure.persistence.domainobject.QualFilingPersonnelCertChangeDO;
import org.qinan.safetyeval.infrastructure.persistence.mapper.QualFilingPersonnelCertChangeMapper;
import org.qinan.safetyeval.infrastructure.persistence.repository.QualFilingPersonnelCertChangeRepository;
import org.springframework.stereotype.Service;

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

/**
 * 备案申请人员证书
 *
 * @Author: ltq
 * @date 2026-07-02 10:04:23
 */
@Service
@AllArgsConstructor
public class QualFilingPersonnelCertChangeRepositoryImpl extends BaseRepositoryImpl<QualFilingPersonnelCertChangeMapper, QualFilingPersonnelCertChangeDO> implements QualFilingPersonnelCertChangeRepository {

    private final QualFilingPersonnelCertChangeMapper qualFilingPersonnelCertChangeMapper;

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

    @Override
    public IPage<QualFilingPersonnelCertChangeDO> listQualFilingPersonnelCertChangePage(IPage iPage, QualFilingPersonnelCertChangePageQry qualFilingPersonnelCertChangePageQry) {
        QueryWrapper<QualFilingPersonnelCertChangeDO> qualFilingPersonnelCertChangeDoQueryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelCertChangePageQry.getFilingChangeId())) {
            qualFilingPersonnelCertChangeDoQueryWrapper.eq("filing_change_id", qualFilingPersonnelCertChangePageQry.getFilingChangeId());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelCertChangePageQry.getFilingPersonnelId())) {
            qualFilingPersonnelCertChangeDoQueryWrapper.eq("filing_personnel_id", qualFilingPersonnelCertChangePageQry.getFilingPersonnelId());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelCertChangePageQry.getCertName())) {
            qualFilingPersonnelCertChangeDoQueryWrapper.like("cert_name", qualFilingPersonnelCertChangePageQry.getCertName());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelCertChangePageQry.getCertTypeCode())) {
            qualFilingPersonnelCertChangeDoQueryWrapper.eq("cert_type_code", qualFilingPersonnelCertChangePageQry.getCertTypeCode());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelCertChangePageQry.getCertTypeName())) {
            qualFilingPersonnelCertChangeDoQueryWrapper.like("cert_type_name", qualFilingPersonnelCertChangePageQry.getCertTypeName());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelCertChangePageQry.getCertCategoryCode())) {
            qualFilingPersonnelCertChangeDoQueryWrapper.eq("cert_category_code", qualFilingPersonnelCertChangePageQry.getCertCategoryCode());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelCertChangePageQry.getCertCategoryName())) {
            qualFilingPersonnelCertChangeDoQueryWrapper.like("cert_category_name", qualFilingPersonnelCertChangePageQry.getCertCategoryName());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelCertChangePageQry.getOperationCategoryCode())) {
            qualFilingPersonnelCertChangeDoQueryWrapper.eq("operation_category_code", qualFilingPersonnelCertChangePageQry.getOperationCategoryCode());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelCertChangePageQry.getOperationCategoryName())) {
            qualFilingPersonnelCertChangeDoQueryWrapper.like("operation_category_name", qualFilingPersonnelCertChangePageQry.getOperationCategoryName());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelCertChangePageQry.getCertNo())) {
            qualFilingPersonnelCertChangeDoQueryWrapper.eq("cert_no", qualFilingPersonnelCertChangePageQry.getCertNo());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelCertChangePageQry.getIssueOrg())) {
            qualFilingPersonnelCertChangeDoQueryWrapper.eq("issue_org", qualFilingPersonnelCertChangePageQry.getIssueOrg());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelCertChangePageQry.getValidStartDate())) {
            qualFilingPersonnelCertChangeDoQueryWrapper.eq("valid_start_date", qualFilingPersonnelCertChangePageQry.getValidStartDate());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelCertChangePageQry.getValidEndDate())) {
            qualFilingPersonnelCertChangeDoQueryWrapper.eq("valid_end_date", qualFilingPersonnelCertChangePageQry.getValidEndDate());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelCertChangePageQry.getReviewDate())) {
            qualFilingPersonnelCertChangeDoQueryWrapper.eq("review_date", qualFilingPersonnelCertChangePageQry.getReviewDate());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelCertChangePageQry.getCertAttachmentUrl())) {
            qualFilingPersonnelCertChangeDoQueryWrapper.eq("cert_attachment_url", qualFilingPersonnelCertChangePageQry.getCertAttachmentUrl());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelCertChangePageQry.getSourceCertId())) {
            qualFilingPersonnelCertChangeDoQueryWrapper.eq("source_cert_id", qualFilingPersonnelCertChangePageQry.getSourceCertId());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelCertChangePageQry.getEnv())) {
            qualFilingPersonnelCertChangeDoQueryWrapper.eq("env", qualFilingPersonnelCertChangePageQry.getEnv());
        }
        qualFilingPersonnelCertChangeDoQueryWrapper.eq("delete_enum", BooleanQAEnum.FALSE.getValue());
        return qualFilingPersonnelCertChangeMapper.selectPage(iPage, qualFilingPersonnelCertChangeDoQueryWrapper);
    }

    @Override
    public Boolean addQualFilingPersonnelCertChange(QualFilingPersonnelCertChangeDO qualFilingPersonnelCertChangeDO) {
        return super.save(qualFilingPersonnelCertChangeDO);
    }

    @Override
    public Boolean updateQualFilingPersonnelCertChange(QualFilingPersonnelCertChangeDO qualFilingPersonnelCertChangeDO) {
        return super.updateById(qualFilingPersonnelCertChangeDO);
    }

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

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

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