package org.qinan.safetyeval.infrastructure.support;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.qinan.safetyeval.infrastructure.dataobject.OrgDepartmentDO;
import org.qinan.safetyeval.infrastructure.dataobject.OrgEquipmentDO;
import org.qinan.safetyeval.infrastructure.dataobject.OrgPersonnelCertDO;
import org.qinan.safetyeval.infrastructure.dataobject.OrgPersonnelChangeDO;
import org.qinan.safetyeval.infrastructure.dataobject.OrgPersonnelDO;
import org.qinan.safetyeval.infrastructure.dataobject.OrgPositionDO;
import org.qinan.safetyeval.infrastructure.dataobject.OrgQualificationDO;
import org.qinan.safetyeval.infrastructure.dataobject.OrgResignApplyDO;
import org.qinan.safetyeval.infrastructure.dataobject.QualFilingChangeDetailDO;
import org.qinan.safetyeval.infrastructure.dataobject.QualFilingChangeDO;
import org.qinan.safetyeval.infrastructure.dataobject.QualFilingCommitmentDO;
import org.qinan.safetyeval.infrastructure.dataobject.QualFilingDO;
import org.qinan.safetyeval.infrastructure.dataobject.QualFilingEquipmentDO;
import org.qinan.safetyeval.infrastructure.dataobject.QualFilingMaterialDO;
import org.qinan.safetyeval.infrastructure.dataobject.QualFilingPersonnelCertDO;
import org.qinan.safetyeval.infrastructure.dataobject.QualFilingPersonnelDO;
import org.qinan.safetyeval.infrastructure.mapper.OrgDepartmentMapper;
import org.qinan.safetyeval.infrastructure.mapper.OrgEquipmentMapper;
import org.qinan.safetyeval.infrastructure.mapper.OrgPersonnelCertMapper;
import org.qinan.safetyeval.infrastructure.mapper.OrgPersonnelChangeMapper;
import org.qinan.safetyeval.infrastructure.mapper.OrgPersonnelMapper;
import org.qinan.safetyeval.infrastructure.mapper.OrgPositionMapper;
import org.qinan.safetyeval.infrastructure.mapper.OrgQualificationMapper;
import org.qinan.safetyeval.infrastructure.mapper.OrgResignApplyMapper;
import org.qinan.safetyeval.infrastructure.mapper.QualFilingChangeDetailMapper;
import org.qinan.safetyeval.infrastructure.mapper.QualFilingChangeMapper;
import org.qinan.safetyeval.infrastructure.mapper.QualFilingCommitmentMapper;
import org.qinan.safetyeval.infrastructure.mapper.QualFilingEquipmentMapper;
import org.qinan.safetyeval.infrastructure.mapper.QualFilingMapper;
import org.qinan.safetyeval.infrastructure.mapper.QualFilingMaterialMapper;
import org.qinan.safetyeval.infrastructure.mapper.QualFilingPersonnelCertMapper;
import org.qinan.safetyeval.infrastructure.mapper.QualFilingPersonnelMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 按 org_info.id 级联删除关联数据（应用层维护，无数据库外键）。
 */
@Component
public class OrgInfoCascadeDeleter {

    @Resource
    private QualFilingChangeDetailMapper qualFilingChangeDetailMapper;
    @Resource
    private QualFilingChangeMapper qualFilingChangeMapper;
    @Resource
    private QualFilingPersonnelCertMapper qualFilingPersonnelCertMapper;
    @Resource
    private QualFilingEquipmentMapper qualFilingEquipmentMapper;
    @Resource
    private QualFilingPersonnelMapper qualFilingPersonnelMapper;
    @Resource
    private QualFilingMaterialMapper qualFilingMaterialMapper;
    @Resource
    private QualFilingCommitmentMapper qualFilingCommitmentMapper;
    @Resource
    private QualFilingMapper qualFilingMapper;
    @Resource
    private OrgPersonnelChangeMapper orgPersonnelChangeMapper;
    @Resource
    private OrgPersonnelCertMapper orgPersonnelCertMapper;
    @Resource
    private OrgResignApplyMapper orgResignApplyMapper;
    @Resource
    private OrgPersonnelMapper orgPersonnelMapper;
    @Resource
    private OrgEquipmentMapper orgEquipmentMapper;
    @Resource
    private OrgQualificationMapper orgQualificationMapper;
    @Resource
    private OrgPositionMapper orgPositionMapper;
    @Resource
    private OrgDepartmentMapper orgDepartmentMapper;

    public void deleteRelatedData(Long orgInfoId) {
        deleteByOrgId(qualFilingChangeDetailMapper, QualFilingChangeDetailDO::getOrgId, orgInfoId);
        deleteByOrgId(qualFilingChangeMapper, QualFilingChangeDO::getOrgId, orgInfoId);
        deleteByOrgId(qualFilingPersonnelCertMapper, QualFilingPersonnelCertDO::getOrgId, orgInfoId);
        deleteByOrgId(qualFilingEquipmentMapper, QualFilingEquipmentDO::getOrgId, orgInfoId);
        deleteByOrgId(qualFilingPersonnelMapper, QualFilingPersonnelDO::getOrgId, orgInfoId);
        deleteByOrgId(qualFilingMaterialMapper, QualFilingMaterialDO::getOrgId, orgInfoId);
        deleteByOrgId(qualFilingCommitmentMapper, QualFilingCommitmentDO::getOrgId, orgInfoId);
        deleteByOrgId(qualFilingMapper, QualFilingDO::getOrgId, orgInfoId);
        deleteByOrgId(orgPersonnelChangeMapper, OrgPersonnelChangeDO::getOrgId, orgInfoId);
        deleteByOrgId(orgPersonnelCertMapper, OrgPersonnelCertDO::getOrgId, orgInfoId);
        deleteByOrgId(orgResignApplyMapper, OrgResignApplyDO::getOrgId, orgInfoId);
        deleteByOrgId(orgPersonnelMapper, OrgPersonnelDO::getOrgId, orgInfoId);
        deleteByOrgId(orgEquipmentMapper, OrgEquipmentDO::getOrgId, orgInfoId);
        deleteByOrgId(orgQualificationMapper, OrgQualificationDO::getOrgId, orgInfoId);
        deleteByOrgId(orgPositionMapper, OrgPositionDO::getOrgId, orgInfoId);
        deleteByOrgId(orgDepartmentMapper, OrgDepartmentDO::getOrgId, orgInfoId);
    }

    private <T> void deleteByOrgId(BaseMapper<T> mapper, SFunction<T, Long> orgIdColumn, Long orgInfoId) {
        mapper.delete(new LambdaQueryWrapper<T>().eq(orgIdColumn, orgInfoId));
    }
}
