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

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 BusinessQCParameterDataServiceImpl implements BusinessQCParameterDataService {

    @Resource
    private BusinessQCParameterDataMapper businessQCParameterDataMapper;

    @Override
    public BusinessQCParameterDataRespVO createBusinessQCParameterData(BusinessQCParameterDataSaveReqVO createReqVO) {
        // 插入
        BusinessQCParameterDataDO businessQCParameterData = BeanUtils.toBean(createReqVO, BusinessQCParameterDataDO.class);
        businessQCParameterDataMapper.insert(businessQCParameterData);
        // 返回
        return BeanUtils.toBean(businessQCParameterData, BusinessQCParameterDataRespVO.class);
    }

    @Override
    public void updateBusinessQCParameterData(BusinessQCParameterDataSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessQCParameterDataExists(updateReqVO.getId());
        // 更新
        BusinessQCParameterDataDO updateObj = BeanUtils.toBean(updateReqVO, BusinessQCParameterDataDO.class);
        businessQCParameterDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessQCParameterData(Long id) {
        // 校验存在
        validateBusinessQCParameterDataExists(id);
        // 删除
        businessQCParameterDataMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessQCParameterDataListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessQCParameterDataExists(ids);
        // 删除
        businessQCParameterDataMapper.deleteByIds(ids);
        }

    private void validateBusinessQCParameterDataExists(List<Long> ids) {
        List<BusinessQCParameterDataDO> list = businessQCParameterDataMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_QC_PARAMETER_DATA_NOT_EXISTS);
        }
    }

    private void validateBusinessQCParameterDataExists(Long id) {
        if (businessQCParameterDataMapper.selectById(id) == null) {
            throw exception(BUSINESS_QC_PARAMETER_DATA_NOT_EXISTS);
        }
    }

    @Override
    public BusinessQCParameterDataDO getBusinessQCParameterData(Long id) {
        return businessQCParameterDataMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessQCParameterDataDO> getBusinessQCParameterDataPage(BusinessQCParameterDataPageReqVO pageReqVO) {
        return businessQCParameterDataMapper.selectPage(pageReqVO);
    }

}