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

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionarySampleTypePageReqVO;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionarySampleTypeRespVO;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionarySampleTypeSaveReqVO;
import com.zt.plat.module.qms.business.dic.dal.dataobject.DictionarySampleTypeDO;
import com.zt.plat.module.qms.business.dic.dal.mapper.DictionarySampleTypeMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

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

/**
 * 样品类型字典 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class DictionarySampleTypeServiceImpl implements DictionarySampleTypeService {

    @Resource
    private DictionarySampleTypeMapper dictionarySampleTypeMapper;

    @Override
    public DictionarySampleTypeRespVO createDictionarySampleType(DictionarySampleTypeSaveReqVO createReqVO) {
        // 插入
        DictionarySampleTypeDO dictionarySampleType = BeanUtils.toBean(createReqVO, DictionarySampleTypeDO.class);
        dictionarySampleTypeMapper.insert(dictionarySampleType);
        // 返回
        return BeanUtils.toBean(dictionarySampleType, DictionarySampleTypeRespVO.class);
    }

    @Override
    public void updateDictionarySampleType(DictionarySampleTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateDictionarySampleTypeExists(updateReqVO.getId());
        // 更新
        DictionarySampleTypeDO updateObj = BeanUtils.toBean(updateReqVO, DictionarySampleTypeDO.class);
        dictionarySampleTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteDictionarySampleType(Long id) {
        // 校验存在
        validateDictionarySampleTypeExists(id);
        // 删除
        dictionarySampleTypeMapper.deleteById(id);
    }

    @Override
        public void deleteDictionarySampleTypeListByIds(List<Long> ids) {
        // 校验存在
        validateDictionarySampleTypeExists(ids);
        // 删除
        dictionarySampleTypeMapper.deleteByIds(ids);
        }

    private void validateDictionarySampleTypeExists(List<Long> ids) {
        List<DictionarySampleTypeDO> list = dictionarySampleTypeMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DICTIONARY_SAMPLE_TYPE_NOT_EXISTS);
        }
    }

    private void validateDictionarySampleTypeExists(Long id) {
        if (dictionarySampleTypeMapper.selectById(id) == null) {
            throw exception(DICTIONARY_SAMPLE_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public DictionarySampleTypeDO getDictionarySampleType(Long id) {
        return dictionarySampleTypeMapper.selectById(id);
    }

    @Override
    public PageResult<DictionarySampleTypeDO> getDictionarySampleTypePage(DictionarySampleTypePageReqVO pageReqVO) {
        return dictionarySampleTypeMapper.selectPage(pageReqVO);
    }

}