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

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 BusinessQCCoefficientParameterDataServiceImpl implements BusinessQCCoefficientParameterDataService {

    @Resource
    private BusinessQCCoefficientParameterDataMapper businessQCCoefficientParameterDataMapper;

    @Override
    public BusinessQCCoefficientParameterDataRespVO createBusinessQCCoefficientParameterData(BusinessQCCoefficientParameterDataSaveReqVO createReqVO) {
        // 插入
        BusinessQCCoefficientParameterDataDO businessQCCoefficientParameterData = BeanUtils.toBean(createReqVO, BusinessQCCoefficientParameterDataDO.class);
        businessQCCoefficientParameterDataMapper.insert(businessQCCoefficientParameterData);
        // 返回
        return BeanUtils.toBean(businessQCCoefficientParameterData, BusinessQCCoefficientParameterDataRespVO.class);
    }

    @Override
    public void updateBusinessQCCoefficientParameterData(BusinessQCCoefficientParameterDataSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessQCCoefficientParameterDataExists(updateReqVO.getId());
        // 更新
        BusinessQCCoefficientParameterDataDO updateObj = BeanUtils.toBean(updateReqVO, BusinessQCCoefficientParameterDataDO.class);
        businessQCCoefficientParameterDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessQCCoefficientParameterData(Long id) {
        // 校验存在
        validateBusinessQCCoefficientParameterDataExists(id);
        // 删除
        businessQCCoefficientParameterDataMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessQCCoefficientParameterDataListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessQCCoefficientParameterDataExists(ids);
        // 删除
        businessQCCoefficientParameterDataMapper.deleteByIds(ids);
        }

    private void validateBusinessQCCoefficientParameterDataExists(List<Long> ids) {
        List<BusinessQCCoefficientParameterDataDO> list = businessQCCoefficientParameterDataMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_QC_COEFFICIENT_PARAMETER_DATA_NOT_EXISTS);
        }
    }

    private void validateBusinessQCCoefficientParameterDataExists(Long id) {
        if (businessQCCoefficientParameterDataMapper.selectById(id) == null) {
            throw exception(BUSINESS_QC_COEFFICIENT_PARAMETER_DATA_NOT_EXISTS);
        }
    }

    @Override
    public BusinessQCCoefficientParameterDataDO getBusinessQCCoefficientParameterData(Long id) {
        return businessQCCoefficientParameterDataMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessQCCoefficientParameterDataDO> getBusinessQCCoefficientParameterDataPage(BusinessQCCoefficientParameterDataPageReqVO pageReqVO) {
        return businessQCCoefficientParameterDataMapper.selectPage(pageReqVO);
    }

}