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.BusinessSubParentSamplePageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSubParentSampleRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSubParentSampleSaveReqVO;
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.BusinessSubParentSampleDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSubParentSampleMapper;

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 BusinessSubParentSampleServiceImpl implements BusinessSubParentSampleService {

    @Resource
    private BusinessSubParentSampleMapper businessSubParentSampleMapper;

    @Override
    public BusinessSubParentSampleRespVO createBusinessSubParentSample(BusinessSubParentSampleSaveReqVO createReqVO) {
        // 插入
        BusinessSubParentSampleDO businessSubParentSample = BeanUtils.toBean(createReqVO, BusinessSubParentSampleDO.class);
        businessSubParentSampleMapper.insert(businessSubParentSample);
        // 返回
        return BeanUtils.toBean(businessSubParentSample, BusinessSubParentSampleRespVO.class);
    }

    @Override
    public void updateBusinessSubParentSample(BusinessSubParentSampleSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessSubParentSampleExists(updateReqVO.getId());
        // 更新
        BusinessSubParentSampleDO updateObj = BeanUtils.toBean(updateReqVO, BusinessSubParentSampleDO.class);
        businessSubParentSampleMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessSubParentSample(Long id) {
        // 校验存在
        validateBusinessSubParentSampleExists(id);
        // 删除
        businessSubParentSampleMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessSubParentSampleListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessSubParentSampleExists(ids);
        // 删除
        businessSubParentSampleMapper.deleteByIds(ids);
        }

    private void validateBusinessSubParentSampleExists(List<Long> ids) {
        List<BusinessSubParentSampleDO> list = businessSubParentSampleMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_SUB_PARENT_SAMPLE_NOT_EXISTS);
        }
    }

    private void validateBusinessSubParentSampleExists(Long id) {
        if (businessSubParentSampleMapper.selectById(id) == null) {
            throw exception(BUSINESS_SUB_PARENT_SAMPLE_NOT_EXISTS);
        }
    }

    @Override
    public BusinessSubParentSampleDO getBusinessSubParentSample(Long id) {
        return businessSubParentSampleMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessSubParentSampleDO> getBusinessSubParentSamplePage(BusinessSubParentSamplePageReqVO pageReqVO) {
        return businessSubParentSampleMapper.selectPage(pageReqVO);
    }

}