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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleDispatchDetailExtendRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleDispatchDetailPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleDispatchDetailRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleDispatchDetailSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleDispatchDetailDO;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;

import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleDispatchDetailMapper;

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.module.qms.enums.ErrorCodeConstants.*;

/**
 * 样品调拨明细 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class BusinessSampleDispatchDetailServiceImpl implements BusinessSampleDispatchDetailService {

    @Resource
    private BusinessSampleDispatchDetailMapper businessSampleDispatchDetailMapper;

    @Override
    public List<BusinessSampleDispatchDetailDO> listByParID(Long parID) {
        LambdaQueryWrapper<BusinessSampleDispatchDetailDO> query = new LambdaQueryWrapper<>();
        query.eq(BusinessSampleDispatchDetailDO::getParentId, parID);
        return businessSampleDispatchDetailMapper.selectList(query);
    }

    @Override
    public void insertBatch(List<BusinessSampleDispatchDetailDO> list) {
        businessSampleDispatchDetailMapper.insertBatch(list);
    }

    @Override
    public void updateBatch(List<BusinessSampleDispatchDetailDO> list) {
        businessSampleDispatchDetailMapper.updateBatch(list);
    }

    @Override
    public BusinessSampleDispatchDetailRespVO createBusinessSampleDispatchDetail(BusinessSampleDispatchDetailSaveReqVO createReqVO) {
        // 插入
        BusinessSampleDispatchDetailDO businessSampleDispatchDetail = BeanUtils.toBean(createReqVO, BusinessSampleDispatchDetailDO.class);
        businessSampleDispatchDetailMapper.insert(businessSampleDispatchDetail);
        // 返回
        return BeanUtils.toBean(businessSampleDispatchDetail, BusinessSampleDispatchDetailRespVO.class);
    }

    @Override
    public void updateBusinessSampleDispatchDetail(BusinessSampleDispatchDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessSampleDispatchDetailExists(updateReqVO.getId());
        // 更新
        BusinessSampleDispatchDetailDO updateObj = BeanUtils.toBean(updateReqVO, BusinessSampleDispatchDetailDO.class);
        businessSampleDispatchDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessSampleDispatchDetail(Long id) {
        // 校验存在
        validateBusinessSampleDispatchDetailExists(id);
        // 删除
        businessSampleDispatchDetailMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessSampleDispatchDetailListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessSampleDispatchDetailExists(ids);
        // 删除
        businessSampleDispatchDetailMapper.deleteByIds(ids);
        }

    private void validateBusinessSampleDispatchDetailExists(List<Long> ids) {
        List<BusinessSampleDispatchDetailDO> list = businessSampleDispatchDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_SAMPLE_DISPATCH_DETAIL_NOT_EXISTS);
        }
    }

    private void validateBusinessSampleDispatchDetailExists(Long id) {
        if (businessSampleDispatchDetailMapper.selectById(id) == null) {
            throw exception(BUSINESS_SAMPLE_DISPATCH_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public BusinessSampleDispatchDetailDO getBusinessSampleDispatchDetail(Long id) {
        return businessSampleDispatchDetailMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessSampleDispatchDetailExtendRespVO> getBusinessSampleDispatchDetailPage(BusinessSampleDispatchDetailPageReqVO pageReqVO) {
        return businessSampleDispatchDetailMapper.selectPage(pageReqVO);
    }

}