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

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 BusinessQCDataServiceImpl implements BusinessQCDataService {

    @Resource
    private BusinessQCDataMapper businessQCDataMapper;

    @Override
    public BusinessQCDataRespVO createBusinessQCData(BusinessQCDataSaveReqVO createReqVO) {
        // 插入
        BusinessQCDataDO businessQCData = BeanUtils.toBean(createReqVO, BusinessQCDataDO.class);
        businessQCDataMapper.insert(businessQCData);
        // 返回
        return BeanUtils.toBean(businessQCData, BusinessQCDataRespVO.class);
    }

    @Override
    public void updateBusinessQCData(BusinessQCDataSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessQCDataExists(updateReqVO.getId());
        // 更新
        BusinessQCDataDO updateObj = BeanUtils.toBean(updateReqVO, BusinessQCDataDO.class);
        businessQCDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessQCData(Long id) {
        // 校验存在
        validateBusinessQCDataExists(id);
        // 删除
        businessQCDataMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessQCDataListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessQCDataExists(ids);
        // 删除
        businessQCDataMapper.deleteByIds(ids);
        }

    private void validateBusinessQCDataExists(List<Long> ids) {
        List<BusinessQCDataDO> list = businessQCDataMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_QC_DATA_NOT_EXISTS);
        }
    }

    private void validateBusinessQCDataExists(Long id) {
        if (businessQCDataMapper.selectById(id) == null) {
            throw exception(BUSINESS_QC_DATA_NOT_EXISTS);
        }
    }

    @Override
    public BusinessQCDataDO getBusinessQCData(Long id) {
        return businessQCDataMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessQCDataExtendRespVO> getBusinessQCDataPage(BusinessQCDataPageReqVO pageReqVO) {
        return businessQCDataMapper.selectPage(pageReqVO);
    }

	@Override
	public List<BusinessQCDataExtendRespVO> getBusinessQCDataList(BusinessQCDataReqVO reqVO) {
		return businessQCDataMapper.selectList(reqVO);
	}

}