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.*;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;

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

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 BusinessSubSampleServiceImpl implements BusinessSubSampleService {

    @Resource
    private BusinessSubSampleMapper businessSubSampleMapper;

    @Override
    public BusinessSubSampleRespVO createBusinessSubSample(BusinessSubSampleSaveReqVO createReqVO) {
        // 插入
        BusinessSubSampleDO businessSubSample = BeanUtils.toBean(createReqVO, BusinessSubSampleDO.class);
        businessSubSampleMapper.insert(businessSubSample);
        // 返回
        return BeanUtils.toBean(businessSubSample, BusinessSubSampleRespVO.class);
    }

    @Override
    public void updateBusinessSubSample(BusinessSubSampleSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessSubSampleExists(updateReqVO.getId());
        // 更新
        BusinessSubSampleDO updateObj = BeanUtils.toBean(updateReqVO, BusinessSubSampleDO.class);
        businessSubSampleMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessSubSample(Long id) {
        // 校验存在
        validateBusinessSubSampleExists(id);
        // 删除
        businessSubSampleMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessSubSampleListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessSubSampleExists(ids);
        // 删除
        businessSubSampleMapper.deleteByIds(ids);
        }

    private void validateBusinessSubSampleExists(List<Long> ids) {
        List<BusinessSubSampleDO> list = businessSubSampleMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_SUB_SAMPLE_NOT_EXISTS);
        }
    }

    private void validateBusinessSubSampleExists(Long id) {
        if (businessSubSampleMapper.selectById(id) == null) {
            throw exception(BUSINESS_SUB_SAMPLE_NOT_EXISTS);
        }
    }

    @Override
    public BusinessSubSampleDO getBusinessSubSample(Long id) {
        return businessSubSampleMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessSubSampleExtendRespVO> getBusinessSubSamplePage(BusinessSubSamplePageReqVO pageReqVO) {
        return businessSubSampleMapper.selectPage(pageReqVO);
    }

	@Override
	public List<BusinessSubSampleExtendRespVO> getBusinessSubSampleList(@Valid BusinessSubSampleReqVO reqVO) {
		return businessSubSampleMapper.selectList(reqVO);
	}

	@Override
	public BusinessSubSampleExtendRespVO getBySampleCodeAndFlowKey(@Valid BusinessSubSampleReqVO reqVO) {
		return businessSubSampleMapper.getBySampleCodeAndFlowKey(reqVO);
	}

}