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.ReportDocumentFilePageReqVO;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.ReportDocumentFileRespVO;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.ReportDocumentFileSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.zt.plat.module.qms.business.reportdoc.dal.dataobject.ReportDocumentFileDO;
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.ReportDocumentFileMapper;

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.*;

/**
 * 检测报告附件 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ReportDocumentFileServiceImpl implements ReportDocumentFileService {

    @Resource
    private ReportDocumentFileMapper reportDocumentFileMapper;


    @Override
    public ReportDocumentFileDO getByQueryVO(ReportDocumentFileSaveReqVO vo) {
        LambdaQueryWrapper<ReportDocumentFileDO> query = new LambdaQueryWrapper<>();
        if(vo.getMainId() != null)
            query.eq(ReportDocumentFileDO::getMainId, vo.getMainId());
        if(vo.getType() != null)
            query.eq(ReportDocumentFileDO::getType, vo.getType());
        if(vo.getVersion() != null)
            query.eq(ReportDocumentFileDO::getVersion, vo.getVersion());
        query.orderByDesc(ReportDocumentFileDO::getCreateTime);
        query.last("LIMIT 1");
        return reportDocumentFileMapper.selectOne(query);
    }

    @Override
    public ReportDocumentFileRespVO createReportDocumentFile(ReportDocumentFileSaveReqVO createReqVO) {
        // 插入
        ReportDocumentFileDO reportDocumentFile = BeanUtils.toBean(createReqVO, ReportDocumentFileDO.class);
        reportDocumentFileMapper.insert(reportDocumentFile);
        // 返回
        return BeanUtils.toBean(reportDocumentFile, ReportDocumentFileRespVO.class);
    }

    @Override
    public ReportDocumentFileDO createReportDocumentFile(ReportDocumentFileDO fileDO) {
        reportDocumentFileMapper.insert(fileDO);
        return fileDO;
    }

    @Override
    public void updateReportDocumentFile(ReportDocumentFileSaveReqVO updateReqVO) {
        // 校验存在
        validateReportDocumentFileExists(updateReqVO.getId());
        // 更新
        ReportDocumentFileDO updateObj = BeanUtils.toBean(updateReqVO, ReportDocumentFileDO.class);
        reportDocumentFileMapper.updateById(updateObj);
    }

    @Override
    public void deleteReportDocumentFile(Long id) {
        // 校验存在
        validateReportDocumentFileExists(id);
        // 删除
        reportDocumentFileMapper.deleteById(id);
    }

    @Override
        public void deleteReportDocumentFileListByIds(List<Long> ids) {
        // 校验存在
        validateReportDocumentFileExists(ids);
        // 删除
        reportDocumentFileMapper.deleteByIds(ids);
        }

    private void validateReportDocumentFileExists(List<Long> ids) {
        List<ReportDocumentFileDO> list = reportDocumentFileMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(REPORT_DOCUMENT_FILE_NOT_EXISTS);
        }
    }

    private void validateReportDocumentFileExists(Long id) {
        if (reportDocumentFileMapper.selectById(id) == null) {
            throw exception(REPORT_DOCUMENT_FILE_NOT_EXISTS);
        }
    }

    @Override
    public ReportDocumentFileDO getReportDocumentFile(Long id) {
        return reportDocumentFileMapper.selectById(id);
    }

    @Override
    public PageResult<ReportDocumentFileDO> getReportDocumentFilePage(ReportDocumentFilePageReqVO pageReqVO) {
        return reportDocumentFileMapper.selectPage(pageReqVO);
    }

}