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

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

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.other.controller.vo.*;
import com.zt.plat.module.qms.business.other.dal.dataobject.OtherBaseSampleDO;
import com.zt.plat.module.qms.business.other.dal.mapper.OtherBaseSampleMapper;

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 OtherBaseSampleServiceImpl implements OtherBaseSampleService {

    @Resource
    private OtherBaseSampleMapper otherBaseSampleMapper;

    @Override
    public OtherBaseSampleRespVO createOtherBaseSample(OtherBaseSampleSaveReqVO createReqVO) {
        // 插入
        OtherBaseSampleDO otherBaseSample = BeanUtils.toBean(createReqVO, OtherBaseSampleDO.class);
        otherBaseSampleMapper.insert(otherBaseSample);
        // 返回
        return BeanUtils.toBean(otherBaseSample, OtherBaseSampleRespVO.class);
    }

    @Override
    public void updateOtherBaseSample(OtherBaseSampleSaveReqVO updateReqVO) {
        // 校验存在
        validateOtherBaseSampleExists(updateReqVO.getId());
        // 更新
        OtherBaseSampleDO updateObj = BeanUtils.toBean(updateReqVO, OtherBaseSampleDO.class);
        otherBaseSampleMapper.updateById(updateObj);
    }

    @Override
    public void deleteOtherBaseSample(Long id) {
        // 校验存在
        validateOtherBaseSampleExists(id);
        // 删除
        otherBaseSampleMapper.deleteById(id);
    }

    @Override
        public void deleteOtherBaseSampleListByIds(List<Long> ids) {
        // 校验存在
        validateOtherBaseSampleExists(ids);
        // 删除
        otherBaseSampleMapper.deleteByIds(ids);
        }

    private void validateOtherBaseSampleExists(List<Long> ids) {
        List<OtherBaseSampleDO> list = otherBaseSampleMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(OTHER_BASE_SAMPLE_NOT_EXISTS);
        }
    }

    private void validateOtherBaseSampleExists(Long id) {
        if (otherBaseSampleMapper.selectById(id) == null) {
            throw exception(OTHER_BASE_SAMPLE_NOT_EXISTS);
        }
    }

    @Override
    public OtherBaseSampleDO getOtherBaseSample(Long id) {
        return otherBaseSampleMapper.selectById(id);
    }

    @Override
    public PageResult<OtherBaseSampleDO> getOtherBaseSamplePage(OtherBaseSamplePageReqVO pageReqVO) {
        return otherBaseSampleMapper.selectPage(pageReqVO);
    }

}