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

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 BusinessQCManagementDataServiceImpl implements BusinessQCManagementDataService {

    @Resource
    private BusinessQCManagementDataMapper businessQCManagementDataMapper;

    @Override
    public BusinessQCManagementDataRespVO createBusinessQCManagementData(BusinessQCManagementDataSaveReqVO createReqVO) {
        // 插入
        BusinessQCManagementDataDO businessQCManagementData = BeanUtils.toBean(createReqVO, BusinessQCManagementDataDO.class);
        businessQCManagementDataMapper.insert(businessQCManagementData);
        // 返回
        return BeanUtils.toBean(businessQCManagementData, BusinessQCManagementDataRespVO.class);
    }

    @Override
    public void updateBusinessQCManagementData(BusinessQCManagementDataSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessQCManagementDataExists(updateReqVO.getId());
        // 更新
        BusinessQCManagementDataDO updateObj = BeanUtils.toBean(updateReqVO, BusinessQCManagementDataDO.class);
        businessQCManagementDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessQCManagementData(Long id) {
        // 校验存在
        validateBusinessQCManagementDataExists(id);
        // 删除
        businessQCManagementDataMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessQCManagementDataListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessQCManagementDataExists(ids);
        // 删除
        businessQCManagementDataMapper.deleteByIds(ids);
        }

    private void validateBusinessQCManagementDataExists(List<Long> ids) {
        List<BusinessQCManagementDataDO> list = businessQCManagementDataMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_QC_MANAGEMENT_DATA_NOT_EXISTS);
        }
    }

    private void validateBusinessQCManagementDataExists(Long id) {
        if (businessQCManagementDataMapper.selectById(id) == null) {
            throw exception(BUSINESS_QC_MANAGEMENT_DATA_NOT_EXISTS);
        }
    }

    @Override
    public BusinessQCManagementDataDO getBusinessQCManagementData(Long id) {
        return businessQCManagementDataMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessQCManagementDataExtendRespVO> getBusinessQCManagementDataPage(BusinessQCManagementDataPageReqVO pageReqVO) {
        return businessQCManagementDataMapper.selectPage(pageReqVO);
    }

	@Override
	public List<BusinessQCManagementDataExtendRespVO> getBusinessQCManagementDataList(BusinessQCManagementDataReqVO reqVO) {
		return businessQCManagementDataMapper.selectList(reqVO);
	}

}