package com.ai.da.service.impl;


import com.ai.da.common.config.exception.BusinessException;
import com.ai.da.mapper.primary.PythonTAllInfoMapper;
import com.ai.da.mapper.primary.entity.PythonTAllInfo;
import com.ai.da.service.PythonTAllInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * (PythonTAllInfo)表服务实现类
 *
 * @author SHAHAIBO
 * @since 2023-09-08 17:03:47
 */
@Service
@Slf4j
public class PythonTAllInfoServiceImpl extends ServiceImpl<PythonTAllInfoMapper, PythonTAllInfo> implements PythonTAllInfoService {
    @Resource
    private PythonTAllInfoMapper pythonTAllInfoMapper;


    @Override
    public Long getImageIdByPath(String path) {
        if (StringUtils.isEmpty(path)) {
            throw new BusinessException("path.cannot.be.empty");
        }
        QueryWrapper<PythonTAllInfo> qw = new QueryWrapper<>();
        qw.lambda().eq(PythonTAllInfo::getImagePath, path);
        qw.lambda().last("limit 1");
        List<PythonTAllInfo> pythonTAllInfos = pythonTAllInfoMapper.selectList(qw);
        if (!CollectionUtils.isEmpty(pythonTAllInfos)) {
            return pythonTAllInfos.get(0).getId();
        }
        PythonTAllInfo pythonTAllInfo = new PythonTAllInfo();
        pythonTAllInfo.setImagePath(path);
        int insert = pythonTAllInfoMapper.insert(pythonTAllInfo);
        if (insert != 1) {
            throw new BusinessException("save.pythonTAllInfo.failed");
        }
        return pythonTAllInfo.getId();
    }
}
