package com.fdkankan.scene.service.impl;

import cn.hutool.core.collection.CollUtil;
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 QueryWrapper<SceneFileUpload>()
                            .eq("tb_status", TbStatus.VALID.code())
                            .eq("rec_status", RecStatus.VALID.code())
                            .eq("file_id", fileId)
                            .eq("upload_status", UploadStatus.SUCCESS.code()));

    }

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

        List<SceneFileUpload> list = this.list(new QueryWrapper<SceneFileUpload>()
                .eq("tb_status", TbStatus.VALID.code())
                .eq("rec_status", RecStatus.VALID.code())
                .eq("file_id", fileId)
                .eq("chunk", chunk)
                .orderByDesc("id"));

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

        return list.get(0);
    }
}
