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.QualFilingMaterialChangePageQry;
import org.qinan.safetyeval.domain.constant.BooleanQAEnum;
import org.qinan.safetyeval.infrastructure.persistence.domainobject.QualFilingMaterialChangeDO;
import org.qinan.safetyeval.infrastructure.persistence.mapper.QualFilingMaterialChangeMapper;
import org.qinan.safetyeval.infrastructure.persistence.repository.QualFilingMaterialChangeRepository;
import org.springframework.stereotype.Service;

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

/**
 * 备案申请材料-变更
 *
 * @Author: ltq
 * @date 2026-07-02 10:04:07
 */
@Service
@AllArgsConstructor
public class QualFilingMaterialChangeRepositoryImpl extends BaseRepositoryImpl<QualFilingMaterialChangeMapper, QualFilingMaterialChangeDO> implements QualFilingMaterialChangeRepository {

    private final QualFilingMaterialChangeMapper qualFilingMaterialChangeMapper;

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

    @Override
    public IPage<QualFilingMaterialChangeDO> listQualFilingMaterialChangePage(IPage iPage, QualFilingMaterialChangePageQry qualFilingMaterialChangePageQry) {
        QueryWrapper<QualFilingMaterialChangeDO> qualFilingMaterialChangeDoQueryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(qualFilingMaterialChangePageQry.getFilingChangeId())) {
            qualFilingMaterialChangeDoQueryWrapper.eq("filing_change_id", qualFilingMaterialChangePageQry.getFilingChangeId());
        }
        if (ObjectUtil.isNotEmpty(qualFilingMaterialChangePageQry.getMaterialType())) {
            qualFilingMaterialChangeDoQueryWrapper.eq("material_type", qualFilingMaterialChangePageQry.getMaterialType());
        }
        if (ObjectUtil.isNotEmpty(qualFilingMaterialChangePageQry.getMaterialContent())) {
            qualFilingMaterialChangeDoQueryWrapper.eq("material_content", qualFilingMaterialChangePageQry.getMaterialContent());
        }
        if (ObjectUtil.isNotEmpty(qualFilingMaterialChangePageQry.getMaterialFormat())) {
            qualFilingMaterialChangeDoQueryWrapper.eq("material_format", qualFilingMaterialChangePageQry.getMaterialFormat());
        }
        if (ObjectUtil.isNotEmpty(qualFilingMaterialChangePageQry.getSortOrder())) {
            qualFilingMaterialChangeDoQueryWrapper.eq("sort_order", qualFilingMaterialChangePageQry.getSortOrder());
        }
        if (ObjectUtil.isNotEmpty(qualFilingMaterialChangePageQry.getRequiredFlag())) {
            qualFilingMaterialChangeDoQueryWrapper.eq("required_flag", qualFilingMaterialChangePageQry.getRequiredFlag());
        }
        if (ObjectUtil.isNotEmpty(qualFilingMaterialChangePageQry.getUploadStatusCode())) {
            qualFilingMaterialChangeDoQueryWrapper.eq("upload_status_code", qualFilingMaterialChangePageQry.getUploadStatusCode());
        }
        if (ObjectUtil.isNotEmpty(qualFilingMaterialChangePageQry.getUploadStatusName())) {
            qualFilingMaterialChangeDoQueryWrapper.like("upload_status_name", qualFilingMaterialChangePageQry.getUploadStatusName());
        }
        if (ObjectUtil.isNotEmpty(qualFilingMaterialChangePageQry.getAttachmentDesc())) {
            qualFilingMaterialChangeDoQueryWrapper.eq("attachment_desc", qualFilingMaterialChangePageQry.getAttachmentDesc());
        }
        if (ObjectUtil.isNotEmpty(qualFilingMaterialChangePageQry.getAttachmentUrl())) {
            qualFilingMaterialChangeDoQueryWrapper.eq("attachment_url", qualFilingMaterialChangePageQry.getAttachmentUrl());
        }
        if (ObjectUtil.isNotEmpty(qualFilingMaterialChangePageQry.getMaterialRemark())) {
            qualFilingMaterialChangeDoQueryWrapper.eq("material_remark", qualFilingMaterialChangePageQry.getMaterialRemark());
        }
        if (ObjectUtil.isNotEmpty(qualFilingMaterialChangePageQry.getEnv())) {
            qualFilingMaterialChangeDoQueryWrapper.eq("env", qualFilingMaterialChangePageQry.getEnv());
        }
        qualFilingMaterialChangeDoQueryWrapper.eq("delete_enum", BooleanQAEnum.FALSE.getValue());
        return qualFilingMaterialChangeMapper.selectPage(iPage, qualFilingMaterialChangeDoQueryWrapper);
    }

    @Override
    public Boolean addQualFilingMaterialChange(QualFilingMaterialChangeDO qualFilingMaterialChangeDO) {
        return super.save(qualFilingMaterialChangeDO);
    }

    @Override
    public Boolean updateQualFilingMaterialChange(QualFilingMaterialChangeDO qualFilingMaterialChangeDO) {
        return super.updateById(qualFilingMaterialChangeDO);
    }

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

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

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

    @Override
    public int countByFilingIdAndUploadStatusCodeAndMaterialType(Long filingId, Integer uploadStatusCode, String materialType) {
        return super.lambdaQuery()
                .eq(QualFilingMaterialChangeDO::getFilingChangeId, filingId)
                .eq(QualFilingMaterialChangeDO::getDeleteEnum, BooleanQAEnum.FALSE.getValue())
                .eq(QualFilingMaterialChangeDO::getUploadStatusCode, uploadStatusCode)
                .eq(QualFilingMaterialChangeDO::getMaterialType, materialType)
                .count().intValue();
    }
}
