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.BusinessSampleHandoverDetailPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleHandoverDetailRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleHandoverDetailSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
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.BusinessSampleHandoverDetailDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleHandoverDetailMapper;

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 BusinessSampleHandoverDetailServiceImpl implements BusinessSampleHandoverDetailService {

    @Resource
    private BusinessSampleHandoverDetailMapper businessSampleHandoverDetailMapper;

    @Override
    public BusinessSampleHandoverDetailRespVO createBusinessSampleHandoverDetail(BusinessSampleHandoverDetailSaveReqVO createReqVO) {
        // 插入
        BusinessSampleHandoverDetailDO businessSampleHandoverDetail = BeanUtils.toBean(createReqVO, BusinessSampleHandoverDetailDO.class);
        businessSampleHandoverDetailMapper.insert(businessSampleHandoverDetail);
        // 返回
        return BeanUtils.toBean(businessSampleHandoverDetail, BusinessSampleHandoverDetailRespVO.class);
    }

    @Override
    public void updateBusinessSampleHandoverDetail(BusinessSampleHandoverDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessSampleHandoverDetailExists(updateReqVO.getId());
        // 更新
        BusinessSampleHandoverDetailDO updateObj = BeanUtils.toBean(updateReqVO, BusinessSampleHandoverDetailDO.class);
        businessSampleHandoverDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessSampleHandoverDetail(Long id) {
        // 校验存在
        validateBusinessSampleHandoverDetailExists(id);
        // 删除
        businessSampleHandoverDetailMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessSampleHandoverDetailListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessSampleHandoverDetailExists(ids);
        // 删除
        businessSampleHandoverDetailMapper.deleteByIds(ids);
        }

    private void validateBusinessSampleHandoverDetailExists(List<Long> ids) {
        List<BusinessSampleHandoverDetailDO> list = businessSampleHandoverDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_SAMPLE_HANDOVER_DETAIL_NOT_EXISTS);
        }
    }

    private void validateBusinessSampleHandoverDetailExists(Long id) {
        if (businessSampleHandoverDetailMapper.selectById(id) == null) {
            throw exception(BUSINESS_SAMPLE_HANDOVER_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public BusinessSampleHandoverDetailDO getBusinessSampleHandoverDetail(Long id) {
        return businessSampleHandoverDetailMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessSampleHandoverDetailDO> getBusinessSampleHandoverDetailPage(BusinessSampleHandoverDetailPageReqVO pageReqVO) {
        return businessSampleHandoverDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<BusinessSampleHandoverDetailRespVO> takeOffPageList(BusinessSampleHandoverDetailPageReqVO pageReqVO) {
        return businessSampleHandoverDetailMapper.takeOffPageList(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<BusinessSampleHandoverDetailDO> list) {
        businessSampleHandoverDetailMapper.insertBatch(list);
    }
}