package com.fdkankan.scene.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fdkankan.common.constant.RecStatus;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.common.constant.UploadStatus;
import com.fdkankan.scene.entity.SceneFileUpload;
import com.fdkankan.scene.mapper.ISceneFileUploadMapper;
import com.fdkankan.scene.service.ISceneFileUploadService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 场景文件上传表 服务实现类
 * </p>
 *
 * @author dengsixing
 * @since 2021-12-23
 */
@Service
public class SceneFileUploadServiceImpl extends ServiceImpl<ISceneFileUploadMapper, SceneFileUpload> implements ISceneFileUploadService {


    @Override
    public Long countUploadSuccessByFileId(String fileId) {

        return this.count(new LambdaQueryWrapper<SceneFileUpload>()
                            .eq(SceneFileUpload::getTbStatus, TbStatus.VALID.code())
                            .eq(SceneFileUpload::getFileId, fileId)
                            .eq(SceneFileUpload::getUploadStatus ,UploadStatus.SUCCESS.code()));

    }

    @Override
    public SceneFileUpload findByFileIdAndChunk(String fileId, Integer chunk) {

        List<SceneFileUpload> list = this.list(new LambdaQueryWrapper<SceneFileUpload>()
                .eq(SceneFileUpload::getTbStatus, TbStatus.VALID.code())
                .eq(SceneFileUpload::getFileId, fileId)
                .eq(SceneFileUpload::getChunk, chunk)
                .orderByDesc(SceneFileUpload::getId));

        if(CollUtil.isEmpty(list))
            return  null;

        return list.get(0);
    }
}
