package com.zt.plat.module.qms.business.config.service;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.qms.business.config.controller.vo.BaseSamplePageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.BaseSampleRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.BaseSampleSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.zt.plat.module.qms.business.config.dal.dataobject.BaseSampleDO;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;

import com.zt.plat.module.qms.business.config.dal.mapper.BaseSampleMapper;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.BASE_SAMPLE_NOT_EXISTS;

/**
 * 样品大类管理 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class BaseSampleServiceImpl implements BaseSampleService {

    @Resource private BaseSampleMapper baseSampleMapper;

    @Override
    public List<BaseSampleDO> getBaseSampleListWithAssayStandardCount() {
        return baseSampleMapper.getBaseSampleListWithAssayStandardCount();
    }

    @Override
    public BaseSampleRespVO createBaseSample(BaseSampleSaveReqVO createReqVO) {
        // 插入
        BaseSampleDO baseSample = BeanUtils.toBean(createReqVO, BaseSampleDO.class);
        baseSampleMapper.insert(baseSample);
        // 返回
        return BeanUtils.toBean(baseSample, BaseSampleRespVO.class);
    }

    @Override
    public void updateBaseSample(BaseSampleSaveReqVO updateReqVO) {
        // 校验存在
        validateBaseSampleExists(updateReqVO.getId());
        // 更新
        BaseSampleDO updateObj = BeanUtils.toBean(updateReqVO, BaseSampleDO.class);
        baseSampleMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseSample(Long id) {
        // 校验存在
        validateBaseSampleExists(id);
        // 删除
        baseSampleMapper.deleteById(id);
    }

    @Override
        public void deleteBaseSampleListByIds(List<Long> ids) {
        // 校验存在
        validateBaseSampleExists(ids);
        // 删除
        baseSampleMapper.deleteByIds(ids);
        }

    private void validateBaseSampleExists(List<Long> ids) {
        List<BaseSampleDO> list = baseSampleMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BASE_SAMPLE_NOT_EXISTS);
        }
    }

    private void validateBaseSampleExists(Long id) {
        if (baseSampleMapper.selectById(id) == null) {
            throw exception(BASE_SAMPLE_NOT_EXISTS);
        }
    }

    @Override
    public BaseSampleDO getBaseSample(Long id) {
        return baseSampleMapper.selectById(id);
    }

    @Override
    public PageResult<BaseSampleDO> getBaseSamplePage(BaseSamplePageReqVO pageReqVO) {
        return baseSampleMapper.selectPage(pageReqVO);
    }

	@Override
	public List<BaseSampleRespVO> getBaseSampleList(BaseSamplePageReqVO reqVO) {
		return baseSampleMapper.selectList(reqVO);
	}

}