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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zt.plat.framework.common.pojo.CommonResult;
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.controller.vo.*;
import com.zt.plat.module.qms.business.reportdoc.dal.mapper.ReportDocumentDataMapper;
import com.zt.plat.module.qms.business.reportdoc.dal.dataobject.ReportDocumentDataDO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.REPORT_DOCUMENT_DATA_NOT_EXISTS;

/**
 * 检测报告明细 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ReportDocumentDataServiceImpl implements ReportDocumentDataService {

    @Resource
    private ReportDocumentDataMapper reportDocumentDataMapper;

    @Override
    public CommonResult<List<ReportDocumentDataDO>> listByMainDataId(Long mainDataId) {
        QueryWrapper<ReportDocumentDataDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("MAIN_ID", mainDataId);
        queryWrapper.orderByAsc("ID");
        List<ReportDocumentDataDO> list = reportDocumentDataMapper.selectList(queryWrapper);
        return CommonResult.success(list);
    }

    @Override
    public CommonResult<String> removeByMainIdAndDetailIds(Long mainDataId, List<Long> detailIds) {
        QueryWrapper<ReportDocumentDataDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("MAIN_ID", mainDataId);
        queryWrapper.in("ID", detailIds);
        reportDocumentDataMapper.delete(queryWrapper);
        return CommonResult.success("移除成功");
    }

    @Override
    public ReportDocumentDataRespVO createReportDocumentData(ReportDocumentDataSaveReqVO createReqVO) {
        // 插入
        ReportDocumentDataDO reportDocumentData = BeanUtils.toBean(createReqVO, ReportDocumentDataDO.class);
        reportDocumentDataMapper.insert(reportDocumentData);
        // 返回
        return BeanUtils.toBean(reportDocumentData, ReportDocumentDataRespVO.class);
    }

    @Override
    public CommonResult<String> insertBatch(List<ReportDocumentDataDO> list) {
        reportDocumentDataMapper.insertBatch(list);
        return CommonResult.success("添加成功");
    }

    @Override
    public void updateReportDocumentData(ReportDocumentDataSaveReqVO updateReqVO) {
        // 校验存在
        validateReportDocumentDataExists(updateReqVO.getId());
        // 更新
        ReportDocumentDataDO updateObj = BeanUtils.toBean(updateReqVO, ReportDocumentDataDO.class);
        reportDocumentDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteReportDocumentData(Long id) {
        // 校验存在
        validateReportDocumentDataExists(id);
        // 删除
        reportDocumentDataMapper.deleteById(id);
    }

    @Override
    public void deleteReportDocumentDataListByIds(List<Long> ids) {
        // 校验存在
        validateReportDocumentDataExists(ids);
        // 删除
        reportDocumentDataMapper.deleteByIds(ids);
    }

    private void validateReportDocumentDataExists(List<Long> ids) {
        List<ReportDocumentDataDO> list = reportDocumentDataMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(REPORT_DOCUMENT_DATA_NOT_EXISTS);
        }
    }

    private void validateReportDocumentDataExists(Long id) {
        if (reportDocumentDataMapper.selectById(id) == null) {
            throw exception(REPORT_DOCUMENT_DATA_NOT_EXISTS);
        }
    }

    @Override
    public ReportDocumentDataDO getReportDocumentData(Long id) {
        return reportDocumentDataMapper.selectById(id);
    }

    @Override
    public PageResult<ReportDocumentDataDO> getReportDocumentDataPage(ReportDocumentDataPageReqVO pageReqVO) {
        return reportDocumentDataMapper.selectPage(pageReqVO);
    }

}