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.QualFilingPersonnelChangePageQry;
import org.qinan.safetyeval.domain.constant.BooleanQAEnum;
import org.qinan.safetyeval.infrastructure.persistence.domainobject.QualFilingPersonnelChangeDO;
import org.qinan.safetyeval.infrastructure.persistence.mapper.QualFilingPersonnelChangeMapper;
import org.qinan.safetyeval.infrastructure.persistence.repository.QualFilingPersonnelChangeRepository;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * 备案申请人员-变更
 *
 * @Author: ltq
 * @date 2026-07-02 10:04:33
 */
@Service
@AllArgsConstructor
public class QualFilingPersonnelChangeRepositoryImpl extends BaseRepositoryImpl<QualFilingPersonnelChangeMapper, QualFilingPersonnelChangeDO> implements QualFilingPersonnelChangeRepository {

    private final QualFilingPersonnelChangeMapper qualFilingPersonnelChangeMapper;

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

    @Override
    public IPage<QualFilingPersonnelChangeDO> listQualFilingPersonnelChangePage(IPage iPage, QualFilingPersonnelChangePageQry qualFilingPersonnelChangePageQry) {
        QueryWrapper<QualFilingPersonnelChangeDO> qualFilingPersonnelChangeDoQueryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getFilingChangeId())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("filing_change_id", qualFilingPersonnelChangePageQry.getFilingChangeId());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getSourcePersonnelId())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("source_personnel_id", qualFilingPersonnelChangePageQry.getSourcePersonnelId());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getPersonName())) {
            qualFilingPersonnelChangeDoQueryWrapper.like("person_name", qualFilingPersonnelChangePageQry.getPersonName());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getPersonTypeCode())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("person_type_code", qualFilingPersonnelChangePageQry.getPersonTypeCode());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getPersonTypeName())) {
            qualFilingPersonnelChangeDoQueryWrapper.like("person_type_name", qualFilingPersonnelChangePageQry.getPersonTypeName());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getPositionName())) {
            qualFilingPersonnelChangeDoQueryWrapper.like("position_name", qualFilingPersonnelChangePageQry.getPositionName());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getTitleName())) {
            qualFilingPersonnelChangeDoQueryWrapper.like("title_name", qualFilingPersonnelChangePageQry.getTitleName());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getGenderCode())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("gender_code", qualFilingPersonnelChangePageQry.getGenderCode());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getGenderName())) {
            qualFilingPersonnelChangeDoQueryWrapper.like("gender_name", qualFilingPersonnelChangePageQry.getGenderName());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getBirthDate())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("birth_date", qualFilingPersonnelChangePageQry.getBirthDate());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getJoinWorkDate())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("join_work_date", qualFilingPersonnelChangePageQry.getJoinWorkDate());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getIdCardNo())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("id_card_no", qualFilingPersonnelChangePageQry.getIdCardNo());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getCurrentAddress())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("current_address", qualFilingPersonnelChangePageQry.getCurrentAddress());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getOfficeAddress())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("office_address", qualFilingPersonnelChangePageQry.getOfficeAddress());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getEducationCode())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("education_code", qualFilingPersonnelChangePageQry.getEducationCode());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getEducationName())) {
            qualFilingPersonnelChangeDoQueryWrapper.like("education_name", qualFilingPersonnelChangePageQry.getEducationName());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getGraduateSchool())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("graduate_school", qualFilingPersonnelChangePageQry.getGraduateSchool());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getMajor())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("major", qualFilingPersonnelChangePageQry.getMajor());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getQualScope())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("qual_scope", qualFilingPersonnelChangePageQry.getQualScope());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getPublications())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("publications", qualFilingPersonnelChangePageQry.getPublications());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getProfessionalLevelCert())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("professional_level_cert", qualFilingPersonnelChangePageQry.getProfessionalLevelCert());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getRegisterEngineerFlag())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("register_engineer_flag", qualFilingPersonnelChangePageQry.getRegisterEngineerFlag());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getAbilityDeclaration())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("ability_declaration", qualFilingPersonnelChangePageQry.getAbilityDeclaration());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getWorkExperience())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("work_experience", qualFilingPersonnelChangePageQry.getWorkExperience());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getProofMaterialUrl())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("proof_material_url", qualFilingPersonnelChangePageQry.getProofMaterialUrl());
        }
        if (ObjectUtil.isNotEmpty(qualFilingPersonnelChangePageQry.getEnv())) {
            qualFilingPersonnelChangeDoQueryWrapper.eq("env", qualFilingPersonnelChangePageQry.getEnv());
        }
        qualFilingPersonnelChangeDoQueryWrapper.eq("delete_enum", BooleanQAEnum.FALSE.getValue());
        return qualFilingPersonnelChangeMapper.selectPage(iPage, qualFilingPersonnelChangeDoQueryWrapper);
    }

    @Override
    public Boolean addQualFilingPersonnelChange(QualFilingPersonnelChangeDO qualFilingPersonnelChangeDO) {
        return super.save(qualFilingPersonnelChangeDO);
    }

    @Override
    public Boolean updateQualFilingPersonnelChange(QualFilingPersonnelChangeDO qualFilingPersonnelChangeDO) {
        return super.updateById(qualFilingPersonnelChangeDO);
    }

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

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

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

    @Override
    public int countByFilingId(Long filingId) {
        return super.lambdaQuery()
                .eq(QualFilingPersonnelChangeDO::getFilingChangeId, filingId)
                .eq(QualFilingPersonnelChangeDO::getDeleteEnum, BooleanQAEnum.FALSE.getValue())
                .count().intValue();
    }

    @Override
    public int countByFilingIdAndRegisterEngineerFlag(Long filingId, Integer registerEngineerFlag) {
        return super.lambdaQuery()
                .eq(QualFilingPersonnelChangeDO::getFilingChangeId, filingId)
                .eq(QualFilingPersonnelChangeDO::getDeleteEnum, BooleanQAEnum.FALSE.getValue())
                .eq(QualFilingPersonnelChangeDO::getRegisterEngineerFlag, registerEngineerFlag)
                .count().intValue();
    }

    @Override
    public int countByFilingIdAndJoinWorkDateAfter(Long filingId, java.time.LocalDate joinWorkDate) {
        return super.lambdaQuery()
                .eq(QualFilingPersonnelChangeDO::getFilingChangeId, filingId)
                .eq(QualFilingPersonnelChangeDO::getDeleteEnum, BooleanQAEnum.FALSE.getValue())
                .gt(QualFilingPersonnelChangeDO::getJoinWorkDate, joinWorkDate)
                .count().intValue();
    }
}
