package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.OcrImageUpload;
import shop.alien.mapper.OcrImageUploadMapper;
import shop.alien.store.service.OcrImageUploadService;

import java.util.List;

/**
 * OCR图片上传记录服务实现类
 *
 * @author system
 * @since 2025-01-XX
 */
@Service
@RequiredArgsConstructor
public class OcrImageUploadServiceImpl extends ServiceImpl<OcrImageUploadMapper, OcrImageUpload> implements OcrImageUploadService {

    private final OcrImageUploadMapper ocrImageUploadMapper;

    @Override
    public boolean saveOcrImageUpload(OcrImageUpload ocrImageUpload) {
        return ocrImageUploadMapper.insert(ocrImageUpload) > 0;
    }

    @Override
    public OcrImageUpload getById(Integer id) {
        return ocrImageUploadMapper.selectById(id);
    }

    @Override
    public List<OcrImageUpload> getByUserId(String userId) {
        LambdaQueryWrapper<OcrImageUpload> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OcrImageUpload::getUserId, userId);
        queryWrapper.orderByDesc(OcrImageUpload::getCreateTime);
        return ocrImageUploadMapper.selectList(queryWrapper);
    }

    @Override
    public List<OcrImageUpload> getByStoreId(String storeId) {
        LambdaQueryWrapper<OcrImageUpload> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OcrImageUpload::getStoreId, storeId);
        queryWrapper.orderByDesc(OcrImageUpload::getCreateTime);
        return ocrImageUploadMapper.selectList(queryWrapper);
    }

    @Override
    public List<OcrImageUpload> getByUserIdAndStoreId(String userId, String storeId) {
        LambdaQueryWrapper<OcrImageUpload> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(userId), OcrImageUpload::getUserId, userId);
        queryWrapper.eq(StringUtils.isNotEmpty(storeId), OcrImageUpload::getStoreId, storeId);
        queryWrapper.orderByDesc(OcrImageUpload::getCreateTime);
        return ocrImageUploadMapper.selectList(queryWrapper);
    }

    @Override
    public List<OcrImageUpload> getByOcrType(String ocrType) {
        LambdaQueryWrapper<OcrImageUpload> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OcrImageUpload::getOcrType, ocrType);
        queryWrapper.orderByDesc(OcrImageUpload::getCreateTime);
        return ocrImageUploadMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<OcrImageUpload> getPage(Page<OcrImageUpload> page, String userId, String storeId, String ocrType) {
        LambdaQueryWrapper<OcrImageUpload> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(userId), OcrImageUpload::getUserId, userId);
        queryWrapper.eq(StringUtils.isNotEmpty(storeId), OcrImageUpload::getStoreId, storeId);
        queryWrapper.eq(StringUtils.isNotEmpty(ocrType), OcrImageUpload::getOcrType, ocrType);
        queryWrapper.orderByDesc(OcrImageUpload::getCreateTime);
        return ocrImageUploadMapper.selectPage(page, queryWrapper);
    }

    @Override
    public boolean updateOcrResult(Integer id, String ocrResult, String ocrType) {
        LambdaUpdateWrapper<OcrImageUpload> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(OcrImageUpload::getId, id);
        updateWrapper.set(OcrImageUpload::getOcrResult, ocrResult);
        if (StringUtils.isNotEmpty(ocrType)) {
            updateWrapper.set(OcrImageUpload::getOcrType, ocrType);
        }
        return ocrImageUploadMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public boolean deleteById(Integer id) {
        return ocrImageUploadMapper.deleteById(id) > 0;
    }
}

