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

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 BusinessQCManagementParameterDataServiceImpl implements BusinessQCManagementParameterDataService {

    @Resource
    private BusinessQCManagementParameterDataMapper businessQCManagementParameterDataMapper;

    @Override
    public BusinessQCManagementParameterDataRespVO createBusinessQCManagementParameterData(BusinessQCManagementParameterDataSaveReqVO createReqVO) {
        // 插入
        BusinessQCManagementParameterDataDO businessQCManagementParameterData = BeanUtils.toBean(createReqVO, BusinessQCManagementParameterDataDO.class);
        businessQCManagementParameterDataMapper.insert(businessQCManagementParameterData);
        // 返回
        return BeanUtils.toBean(businessQCManagementParameterData, BusinessQCManagementParameterDataRespVO.class);
    }

    @Override
    public void updateBusinessQCManagementParameterData(BusinessQCManagementParameterDataSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessQCManagementParameterDataExists(updateReqVO.getId());
        // 更新
        BusinessQCManagementParameterDataDO updateObj = BeanUtils.toBean(updateReqVO, BusinessQCManagementParameterDataDO.class);
        businessQCManagementParameterDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessQCManagementParameterData(Long id) {
        // 校验存在
        validateBusinessQCManagementParameterDataExists(id);
        // 删除
        businessQCManagementParameterDataMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessQCManagementParameterDataListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessQCManagementParameterDataExists(ids);
        // 删除
        businessQCManagementParameterDataMapper.deleteByIds(ids);
        }

    private void validateBusinessQCManagementParameterDataExists(List<Long> ids) {
        List<BusinessQCManagementParameterDataDO> list = businessQCManagementParameterDataMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_QC_MANAGEMENT_PARAMETER_DATA_NOT_EXISTS);
        }
    }

    private void validateBusinessQCManagementParameterDataExists(Long id) {
        if (businessQCManagementParameterDataMapper.selectById(id) == null) {
            throw exception(BUSINESS_QC_MANAGEMENT_PARAMETER_DATA_NOT_EXISTS);
        }
    }

    @Override
    public BusinessQCManagementParameterDataDO getBusinessQCManagementParameterData(Long id) {
        return businessQCManagementParameterDataMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessQCManagementParameterDataDO> getBusinessQCManagementParameterDataPage(BusinessQCManagementParameterDataPageReqVO pageReqVO) {
        return businessQCManagementParameterDataMapper.selectPage(pageReqVO);
    }

}