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

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 BusinessQCCoefficientDataServiceImpl implements BusinessQCCoefficientDataService {

    @Resource
    private BusinessQCCoefficientDataMapper businessQCCoefficientDataMapper;

    @Override
    public BusinessQCCoefficientDataRespVO createBusinessQCCoefficientData(BusinessQCCoefficientDataSaveReqVO createReqVO) {
        // 插入
        BusinessQCCoefficientDataDO businessQCCoefficientData = BeanUtils.toBean(createReqVO, BusinessQCCoefficientDataDO.class);
        businessQCCoefficientDataMapper.insert(businessQCCoefficientData);
        // 返回
        return BeanUtils.toBean(businessQCCoefficientData, BusinessQCCoefficientDataRespVO.class);
    }

    @Override
    public void updateBusinessQCCoefficientData(BusinessQCCoefficientDataSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessQCCoefficientDataExists(updateReqVO.getId());
        // 更新
        BusinessQCCoefficientDataDO updateObj = BeanUtils.toBean(updateReqVO, BusinessQCCoefficientDataDO.class);
        businessQCCoefficientDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessQCCoefficientData(Long id) {
        // 校验存在
        validateBusinessQCCoefficientDataExists(id);
        // 删除
        businessQCCoefficientDataMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessQCCoefficientDataListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessQCCoefficientDataExists(ids);
        // 删除
        businessQCCoefficientDataMapper.deleteByIds(ids);
        }

    private void validateBusinessQCCoefficientDataExists(List<Long> ids) {
        List<BusinessQCCoefficientDataDO> list = businessQCCoefficientDataMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_QC_COEFFICIENT_DATA_NOT_EXISTS);
        }
    }

    private void validateBusinessQCCoefficientDataExists(Long id) {
        if (businessQCCoefficientDataMapper.selectById(id) == null) {
            throw exception(BUSINESS_QC_COEFFICIENT_DATA_NOT_EXISTS);
        }
    }

    @Override
    public BusinessQCCoefficientDataDO getBusinessQCCoefficientData(Long id) {
        return businessQCCoefficientDataMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessQCCoefficientDataExtendRespVO> getBusinessQCCoefficientDataPage(BusinessQCCoefficientDataPageReqVO pageReqVO) {
        return businessQCCoefficientDataMapper.selectPage(pageReqVO);
    }

	@Override
	public List<BusinessQCCoefficientDataExtendRespVO> getBusinessQCCoefficientDataList(BusinessQCCoefficientDataReqVO reqVO) {
		return businessQCCoefficientDataMapper.selectList(reqVO);
	}

}