package com.zt.plat.module.qms.business.reportdoc.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.ReportDocumentMainCorrelationPageReqVO;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.ReportDocumentMainCorrelationRespVO;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.ReportDocumentMainCorrelationSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.zt.plat.module.qms.business.reportdoc.dal.dataobject.ReportDocumentMainCorrelationDO;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;

import com.zt.plat.module.qms.business.reportdoc.dal.mapper.ReportDocumentMainCorrelationMapper;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.framework.common.util.collection.CollectionUtils.convertList;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.REPORT_DOCUMENT_MAIN_CORRELATION_NOT_EXISTS;

/**
 * 检测报告关系表 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ReportDocumentMainCorrelationServiceImpl implements ReportDocumentMainCorrelationService {

    @Resource
    private ReportDocumentMainCorrelationMapper reportDocumentMainCorrelationMapper;


    @Override
    public List<ReportDocumentMainCorrelationDO> listByMainId(Long mainId, String correlationType) {
        LambdaQueryWrapper<ReportDocumentMainCorrelationDO> query = new LambdaQueryWrapper<>();
        query.eq(ReportDocumentMainCorrelationDO::getMainId, mainId);
        if(!ObjectUtils.isEmpty(correlationType))
            query.eq(ReportDocumentMainCorrelationDO::getCorrelationType, correlationType);
        query.orderByAsc(ReportDocumentMainCorrelationDO::getCreateTime);
        return reportDocumentMainCorrelationMapper.selectList(query);
    }

    @Override
    public ReportDocumentMainCorrelationRespVO createReportDocumentMainCorrelation(ReportDocumentMainCorrelationSaveReqVO createReqVO) {
        // 插入
        ReportDocumentMainCorrelationDO reportDocumentMainCorrelation = BeanUtils.toBean(createReqVO, ReportDocumentMainCorrelationDO.class);
        reportDocumentMainCorrelationMapper.insert(reportDocumentMainCorrelation);
        // 返回
        return BeanUtils.toBean(reportDocumentMainCorrelation, ReportDocumentMainCorrelationRespVO.class);
    }


    @Override
    public void deleteByMainId(Long mainId) {
        LambdaQueryWrapper<ReportDocumentMainCorrelationDO> query = new LambdaQueryWrapper<>();
        query.eq(ReportDocumentMainCorrelationDO::getMainId, mainId);
        reportDocumentMainCorrelationMapper.delete(query);
    }

    @Override
    public void insertBatch(List<ReportDocumentMainCorrelationDO> list) {
        reportDocumentMainCorrelationMapper.insertBatch(list);
    }

    @Override
    public void updateReportDocumentMainCorrelation(ReportDocumentMainCorrelationSaveReqVO updateReqVO) {
        // 校验存在
        validateReportDocumentMainCorrelationExists(updateReqVO.getId());
        // 更新
        ReportDocumentMainCorrelationDO updateObj = BeanUtils.toBean(updateReqVO, ReportDocumentMainCorrelationDO.class);
        reportDocumentMainCorrelationMapper.updateById(updateObj);
    }

    @Override
    public void deleteReportDocumentMainCorrelation(Long id) {
        // 校验存在
        validateReportDocumentMainCorrelationExists(id);
        // 删除
        reportDocumentMainCorrelationMapper.deleteById(id);
    }

    @Override
        public void deleteReportDocumentMainCorrelationListByIds(List<Long> ids) {
        // 校验存在
        validateReportDocumentMainCorrelationExists(ids);
        // 删除
        reportDocumentMainCorrelationMapper.deleteByIds(ids);
        }

    private void validateReportDocumentMainCorrelationExists(List<Long> ids) {
        List<ReportDocumentMainCorrelationDO> list = reportDocumentMainCorrelationMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(REPORT_DOCUMENT_MAIN_CORRELATION_NOT_EXISTS);
        }
    }

    private void validateReportDocumentMainCorrelationExists(Long id) {
        if (reportDocumentMainCorrelationMapper.selectById(id) == null) {
            throw exception(REPORT_DOCUMENT_MAIN_CORRELATION_NOT_EXISTS);
        }
    }

    @Override
    public ReportDocumentMainCorrelationDO getReportDocumentMainCorrelation(Long id) {
        return reportDocumentMainCorrelationMapper.selectById(id);
    }

    @Override
    public PageResult<ReportDocumentMainCorrelationDO> getReportDocumentMainCorrelationPage(ReportDocumentMainCorrelationPageReqVO pageReqVO) {
        return reportDocumentMainCorrelationMapper.selectPage(pageReqVO);
    }

}