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

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 后台管理-1
 */
@Service
@Validated
public class BusinessSubSampleParentRetestServiceImpl implements BusinessSubSampleParentRetestService {

    @Resource
    private BusinessSubSampleParentRetestMapper businessSubSampleParentRetestMapper;

    @Override
    public BusinessSubSampleParentRetestRespVO createBusinessSubSampleParentRetest(BusinessSubSampleParentRetestSaveReqVO createReqVO) {
        // 插入
        BusinessSubSampleParentRetestDO businessSubSampleParentRetest = BeanUtils.toBean(createReqVO, BusinessSubSampleParentRetestDO.class);
        businessSubSampleParentRetestMapper.insert(businessSubSampleParentRetest);
        // 返回
        return BeanUtils.toBean(businessSubSampleParentRetest, BusinessSubSampleParentRetestRespVO.class);
    }

    @Override
    public void updateBusinessSubSampleParentRetest(BusinessSubSampleParentRetestSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessSubSampleParentRetestExists(updateReqVO.getId());
        // 更新
        BusinessSubSampleParentRetestDO updateObj = BeanUtils.toBean(updateReqVO, BusinessSubSampleParentRetestDO.class);
        businessSubSampleParentRetestMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessSubSampleParentRetest(Long id) {
        // 校验存在
        validateBusinessSubSampleParentRetestExists(id);
        // 删除
        businessSubSampleParentRetestMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessSubSampleParentRetestListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessSubSampleParentRetestExists(ids);
        // 删除
        businessSubSampleParentRetestMapper.deleteByIds(ids);
        }

    private void validateBusinessSubSampleParentRetestExists(List<Long> ids) {
        List<BusinessSubSampleParentRetestDO> list = businessSubSampleParentRetestMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_SUB_SAMPLE_PARENT_RETEST_NOT_EXISTS);
        }
    }

    private void validateBusinessSubSampleParentRetestExists(Long id) {
        if (businessSubSampleParentRetestMapper.selectById(id) == null) {
            throw exception(BUSINESS_SUB_SAMPLE_PARENT_RETEST_NOT_EXISTS);
        }
    }

    @Override
    public BusinessSubSampleParentRetestDO getBusinessSubSampleParentRetest(Long id) {
        return businessSubSampleParentRetestMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessSubSampleParentRetestDO> getBusinessSubSampleParentRetestPage(BusinessSubSampleParentRetestPageReqVO pageReqVO) {
        return businessSubSampleParentRetestMapper.selectPage(pageReqVO);
    }

}