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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayReportDataPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayReportDataRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayReportDataSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.qms.business.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayReportDataDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayReportDataMapper;

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.framework.common.util.collection.CollectionUtils.diffList;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.*;

/**
 * 检测报表数据业务 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class BusinessAssayReportDataServiceImpl implements BusinessAssayReportDataService {

    @Resource
    private BusinessAssayReportDataMapper businessAssayReportDataMapper;

    @Override
    public BusinessAssayReportDataRespVO createBusinessAssayReportData(BusinessAssayReportDataSaveReqVO createReqVO) {
        // 插入
        BusinessAssayReportDataDO businessAssayReportData = BeanUtils.toBean(createReqVO, BusinessAssayReportDataDO.class);
        businessAssayReportDataMapper.insert(businessAssayReportData);
        // 返回
        return BeanUtils.toBean(businessAssayReportData, BusinessAssayReportDataRespVO.class);
    }

    @Override
    public void updateBusinessAssayReportData(BusinessAssayReportDataSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessAssayReportDataExists(updateReqVO.getId());
        // 更新
        BusinessAssayReportDataDO updateObj = BeanUtils.toBean(updateReqVO, BusinessAssayReportDataDO.class);
        businessAssayReportDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessAssayReportData(Long id) {
        // 校验存在
        validateBusinessAssayReportDataExists(id);
        // 删除
        businessAssayReportDataMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessAssayReportDataListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessAssayReportDataExists(ids);
        // 删除
        businessAssayReportDataMapper.deleteByIds(ids);
        }

    private void validateBusinessAssayReportDataExists(List<Long> ids) {
        List<BusinessAssayReportDataDO> list = businessAssayReportDataMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_ASSAY_REPORT_DATA_NOT_EXISTS);
        }
    }

    private void validateBusinessAssayReportDataExists(Long id) {
        if (businessAssayReportDataMapper.selectById(id) == null) {
            throw exception(BUSINESS_ASSAY_REPORT_DATA_NOT_EXISTS);
        }
    }

    @Override
    public BusinessAssayReportDataDO getBusinessAssayReportData(Long id) {
        return businessAssayReportDataMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessAssayReportDataDO> getBusinessAssayReportDataPage(BusinessAssayReportDataPageReqVO pageReqVO) {
        return businessAssayReportDataMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BusinessAssayReportDataDO> listByIds(List<Long> ids) {
        return businessAssayReportDataMapper.selectByIds(ids);
    }


    @Override
    public PageResult<BusinessAssayReportDataDO> queryWaitingDataForReport(BusinessAssayReportDataPageReqVO pageReqVO) {
        IPage<BusinessAssayReportDataDO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        Page<BusinessAssayReportDataDO> pageList = businessAssayReportDataMapper.queryWaitingDataForReport(page, pageReqVO);
        PageResult<BusinessAssayReportDataDO> pageResult = new PageResult<>(pageList.getRecords(), pageList.getTotal());
        return pageResult;
    }
}