package com.zt.plat.module.qms.business.bus.service;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.qms.business.bus.controller.vo.*;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

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

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 BusinessSampleEntrustDetailServiceImpl implements BusinessSampleEntrustDetailService {

    @Resource
    private BusinessSampleEntrustDetailMapper businessSampleEntrustDetailMapper;

    @Override
    public BusinessSampleEntrustDetailRespVO createBusinessSampleEntrustDetail(BusinessSampleEntrustDetailSaveReqVO createReqVO) {
        // 插入
        BusinessSampleEntrustDetailDO businessSampleEntrustDetail = BeanUtils.toBean(createReqVO, BusinessSampleEntrustDetailDO.class);
        businessSampleEntrustDetailMapper.insert(businessSampleEntrustDetail);
        // 返回
        return BeanUtils.toBean(businessSampleEntrustDetail, BusinessSampleEntrustDetailRespVO.class);
    }

    @Override
    public void updateBusinessSampleEntrustDetail(BusinessSampleEntrustDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessSampleEntrustDetailExists(updateReqVO.getId());
        // 更新
        BusinessSampleEntrustDetailDO updateObj = BeanUtils.toBean(updateReqVO, BusinessSampleEntrustDetailDO.class);
        businessSampleEntrustDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessSampleEntrustDetail(Long id) {
        // 校验存在
        validateBusinessSampleEntrustDetailExists(id);
        // 删除
        businessSampleEntrustDetailMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessSampleEntrustDetailListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessSampleEntrustDetailExists(ids);
        // 删除
        businessSampleEntrustDetailMapper.deleteByIds(ids);
        }

    private void validateBusinessSampleEntrustDetailExists(List<Long> ids) {
        List<BusinessSampleEntrustDetailDO> list = businessSampleEntrustDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_SAMPLE_ENTRUST_DETAIL_NOT_EXISTS);
        }
    }

    private void validateBusinessSampleEntrustDetailExists(Long id) {
        if (businessSampleEntrustDetailMapper.selectById(id) == null) {
            throw exception(BUSINESS_SAMPLE_ENTRUST_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public BusinessSampleEntrustDetailDO getBusinessSampleEntrustDetail(Long id) {
        return businessSampleEntrustDetailMapper.selectById(id);
    }

	@Override
	public List<BusinessSampleEntrustDetailExtendRespVO> getBusinessSampleEntrustDetailList(BusinessSampleEntrustDetailReqVO reqVO) {
		return businessSampleEntrustDetailMapper.selectList(reqVO);
	}

    @Override
    public PageResult<BusinessSampleEntrustDetailExtendRespVO> getBusinessSampleEntrustDetailPage(BusinessSampleEntrustDetailPageReqVO pageReqVO) {
        return businessSampleEntrustDetailMapper.selectPage(pageReqVO);
    }

}