package cn.iocoder.yudao.module.identity.service.generatebatch;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.identity.controller.admin.generatebatch.vo.*;
import cn.iocoder.yudao.module.identity.dal.dataobject.generatebatch.GenerateBatchDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.identity.convert.generatebatch.GenerateBatchConvert;
import cn.iocoder.yudao.module.identity.dal.mysql.generatebatch.GenerateBatchMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.identity.enums.ErrorCodeConstants.*;

/**
 * 码生成记录与批次关联记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class GenerateBatchServiceImpl implements GenerateBatchService {

    @Resource
    private GenerateBatchMapper generateBatchMapper;

    @Override
    public Long createGenerateBatch(GenerateBatchCreateReqVO createReqVO) {
        // 插入
        GenerateBatchDO generateBatch = GenerateBatchConvert.INSTANCE.convert(createReqVO);
        generateBatchMapper.insert(generateBatch);
        // 返回
        return generateBatch.getId();
    }

    @Override
    public void updateGenerateBatch(GenerateBatchUpdateReqVO updateReqVO) {
        // 校验存在
        validateGenerateBatchExists(updateReqVO.getId());
        // 更新
        GenerateBatchDO updateObj = GenerateBatchConvert.INSTANCE.convert(updateReqVO);
        generateBatchMapper.updateById(updateObj);
    }

    @Override
    public void deleteGenerateBatch(Long id) {
        // 校验存在
        validateGenerateBatchExists(id);
        // 删除
        generateBatchMapper.deleteById(id);
    }

    private void validateGenerateBatchExists(Long id) {
        if (generateBatchMapper.selectById(id) == null) {
            throw exception(GENERATE_BATCH_NOT_EXISTS);
        }
    }

    @Override
    public GenerateBatchDO getGenerateBatch(Long id) {
        return generateBatchMapper.selectById(id);
    }

    @Override
    public List<GenerateBatchDO> getGenerateBatchList(Collection<Long> ids) {
        return generateBatchMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<GenerateBatchDO> getGenerateBatchPage(GenerateBatchPageReqVO pageReqVO) {
        return generateBatchMapper.selectPage(pageReqVO);
    }

    @Override
    public List<GenerateBatchDO> getGenerateBatchList(GenerateBatchExportReqVO exportReqVO) {
        return generateBatchMapper.selectList(exportReqVO);
    }

    /**
     * 根据码生成记录ID获取码生成记录与批次关联记录数据信息
     *
     * @param generateRecordId
     * @return
     */
    @Override
    public GenerateBatchDO getGenerateBatchByRecordId(String generateRecordId) {
        QueryWrapper<GenerateBatchDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("generate_record_id",generateRecordId);
        return generateBatchMapper.selectOne(queryWrapper);
    }
}

