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

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 BusinessSampleEntrustDepartmentDetailServiceImpl implements BusinessSampleEntrustDepartmentDetailService {

    @Resource
    private BusinessSampleEntrustDepartmentDetailMapper businessSampleEntrustDepartmentDetailMapper;

    @Override
    public BusinessSampleEntrustDepartmentDetailRespVO createBusinessSampleEntrustDepartmentDetail(BusinessSampleEntrustDepartmentDetailSaveReqVO createReqVO) {
        // 插入
        BusinessSampleEntrustDepartmentDetailDO businessSampleEntrustDepartmentDetail = BeanUtils.toBean(createReqVO, BusinessSampleEntrustDepartmentDetailDO.class);
        businessSampleEntrustDepartmentDetailMapper.insert(businessSampleEntrustDepartmentDetail);
        // 返回
        return BeanUtils.toBean(businessSampleEntrustDepartmentDetail, BusinessSampleEntrustDepartmentDetailRespVO.class);
    }

    @Override
    public void updateBusinessSampleEntrustDepartmentDetail(BusinessSampleEntrustDepartmentDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessSampleEntrustDepartmentDetailExists(updateReqVO.getId());
        // 更新
        BusinessSampleEntrustDepartmentDetailDO updateObj = BeanUtils.toBean(updateReqVO, BusinessSampleEntrustDepartmentDetailDO.class);
        businessSampleEntrustDepartmentDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessSampleEntrustDepartmentDetail(Long id) {
        // 校验存在
        validateBusinessSampleEntrustDepartmentDetailExists(id);
        // 删除
        businessSampleEntrustDepartmentDetailMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessSampleEntrustDepartmentDetailListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessSampleEntrustDepartmentDetailExists(ids);
        // 删除
        businessSampleEntrustDepartmentDetailMapper.deleteByIds(ids);
        }

    private void validateBusinessSampleEntrustDepartmentDetailExists(List<Long> ids) {
        List<BusinessSampleEntrustDepartmentDetailDO> list = businessSampleEntrustDepartmentDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_SAMPLE_ENTRUST_DEPARTMENT_DETAIL_NOT_EXISTS);
        }
    }

    private void validateBusinessSampleEntrustDepartmentDetailExists(Long id) {
        if (businessSampleEntrustDepartmentDetailMapper.selectById(id) == null) {
            throw exception(BUSINESS_SAMPLE_ENTRUST_DEPARTMENT_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public BusinessSampleEntrustDepartmentDetailDO getBusinessSampleEntrustDepartmentDetail(Long id) {
        return businessSampleEntrustDepartmentDetailMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessSampleEntrustDepartmentDetailDO> getBusinessSampleEntrustDepartmentDetailPage(BusinessSampleEntrustDepartmentDetailPageReqVO pageReqVO) {
        return businessSampleEntrustDepartmentDetailMapper.selectPage(pageReqVO);
    }

}