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

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

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.BusinessQCManagementSampleParameterDataDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessQCManagementSampleParameterDataMapper;

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

/**
 * 质控管理样检测任务参数业务表,与子样检测任务参数业务表T_BSN_ASY_TSK_PRM_DAT一样，针对标准样、管理样、质控样、监控样等样品的参数 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class BusinessQCManagementSampleParameterDataServiceImpl implements BusinessQCManagementSampleParameterDataService {

    @Resource
    private BusinessQCManagementSampleParameterDataMapper businessQCManagementSampleParameterDataMapper;

    @Override
    public BusinessQCManagementSampleParameterDataRespVO createBusinessQCManagementSampleParameterData(BusinessQCManagementSampleParameterDataSaveReqVO createReqVO) {
        // 插入
        BusinessQCManagementSampleParameterDataDO businessQCManagementSampleParameterData = BeanUtils.toBean(createReqVO, BusinessQCManagementSampleParameterDataDO.class);
        businessQCManagementSampleParameterDataMapper.insert(businessQCManagementSampleParameterData);
        // 返回
        return BeanUtils.toBean(businessQCManagementSampleParameterData, BusinessQCManagementSampleParameterDataRespVO.class);
    }

    @Override
    public void updateBusinessQCManagementSampleParameterData(BusinessQCManagementSampleParameterDataSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessQCManagementSampleParameterDataExists(updateReqVO.getId());
        // 更新
        BusinessQCManagementSampleParameterDataDO updateObj = BeanUtils.toBean(updateReqVO, BusinessQCManagementSampleParameterDataDO.class);
        businessQCManagementSampleParameterDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessQCManagementSampleParameterData(Long id) {
        // 校验存在
        validateBusinessQCManagementSampleParameterDataExists(id);
        // 删除
        businessQCManagementSampleParameterDataMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessQCManagementSampleParameterDataListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessQCManagementSampleParameterDataExists(ids);
        // 删除
        businessQCManagementSampleParameterDataMapper.deleteByIds(ids);
        }

    private void validateBusinessQCManagementSampleParameterDataExists(List<Long> ids) {
        List<BusinessQCManagementSampleParameterDataDO> list = businessQCManagementSampleParameterDataMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_QC_MANAGEMENT_SAMPLE_PARAMETER_DATA_NOT_EXISTS);
        }
    }

    private void validateBusinessQCManagementSampleParameterDataExists(Long id) {
        if (businessQCManagementSampleParameterDataMapper.selectById(id) == null) {
            throw exception(BUSINESS_QC_MANAGEMENT_SAMPLE_PARAMETER_DATA_NOT_EXISTS);
        }
    }

    @Override
    public BusinessQCManagementSampleParameterDataDO getBusinessQCManagementSampleParameterData(Long id) {
        return businessQCManagementSampleParameterDataMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessQCManagementSampleParameterDataDO> getBusinessQCManagementSampleParameterDataPage(BusinessQCManagementSampleParameterDataPageReqVO pageReqVO) {
        return businessQCManagementSampleParameterDataMapper.selectPage(pageReqVO);
    }

}