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.BusinessBaseSamplePageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessBaseSampleRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessBaseSampleSaveReqVO;
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.BusinessBaseSampleDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessBaseSampleMapper;

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 BusinessBaseSampleServiceImpl implements BusinessBaseSampleService {

    @Resource
    private BusinessBaseSampleMapper businessBaseSampleMapper;

    @Override
    public BusinessBaseSampleRespVO createBusinessBaseSample(BusinessBaseSampleSaveReqVO createReqVO) {
        // 插入
        BusinessBaseSampleDO businessBaseSample = BeanUtils.toBean(createReqVO, BusinessBaseSampleDO.class);
        businessBaseSampleMapper.insert(businessBaseSample);
        // 返回
        return BeanUtils.toBean(businessBaseSample, BusinessBaseSampleRespVO.class);
    }

    @Override
    public void updateBusinessBaseSample(BusinessBaseSampleSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessBaseSampleExists(updateReqVO.getId());
        // 更新
        BusinessBaseSampleDO updateObj = BeanUtils.toBean(updateReqVO, BusinessBaseSampleDO.class);
        businessBaseSampleMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessBaseSample(Long id) {
        // 校验存在
        validateBusinessBaseSampleExists(id);
        // 删除
        businessBaseSampleMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessBaseSampleListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessBaseSampleExists(ids);
        // 删除
        businessBaseSampleMapper.deleteByIds(ids);
        }

    private void validateBusinessBaseSampleExists(List<Long> ids) {
        List<BusinessBaseSampleDO> list = businessBaseSampleMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_BASE_SAMPLE_NOT_EXISTS);
        }
    }

    private void validateBusinessBaseSampleExists(Long id) {
        if (businessBaseSampleMapper.selectById(id) == null) {
            throw exception(BUSINESS_BASE_SAMPLE_NOT_EXISTS);
        }
    }

    @Override
    public BusinessBaseSampleDO getBusinessBaseSample(Long id) {
        return businessBaseSampleMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessBaseSampleDO> getBusinessBaseSamplePage(BusinessBaseSamplePageReqVO pageReqVO) {
        return businessBaseSampleMapper.selectPage(pageReqVO);
    }

}