package com.persagy.dmp.file.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.file.dao.FileMapper;
import com.persagy.dmp.file.domain.FileRequestData;
import com.persagy.dmp.file.model.FileInfo;
import com.persagy.dmp.file.service.FileUploadService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/***
 * Description: 文件上传逻辑处理类
 * @author : lijie
 * @date :2021/10/9 20:09
 * Update By lijie 2021/10/9 20:09
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class FileUploadServiceImpl extends ServiceImpl<FileMapper, FileInfo> implements FileUploadService {

    /**上传成功*/
    private static final Integer UPLOAD_SUCCESS = 0;

    /**部分上传*/
    private static final Integer UPLOAD_PART = 1;

    /***
     * Description: 初始化文件上传路径列表
     * @param requestData : 请求参数
     * @return : java.util.List<com.persagy.dmp.file.model.FileInfo>
     * @author : lijie
     * @date :2021/10/9 20:27
     * Update By lijie 2021/10/9 20:27
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<FileInfo> initChunkUpload(FileRequestData requestData) {
        if (StrUtil.isBlank(requestData.getFileMd5())){
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        // 1.根据文件md5值查询数据库
        FileInfo fileInfo = queryFileInfosByFileMd5(requestData.getFileMd5());
        // 2.历史已上传且已完成上传
        if (ObjectUtil.isNotNull(fileInfo) && UPLOAD_SUCCESS.equals(fileInfo.getUploadStatus())){
            return CollUtil.newArrayList(fileInfo);
        }
        // 3.历史已上传


        return firstInitChunkUploadInternal(requestData);
    }
    /***
     * Description: 初次上传和已有文件信息但未上传任何分片的情况下则直接生成所有上传url 
     * @param requestData : 
     * @return : java.util.List<com.persagy.dmp.file.model.FileInfo>  
     * @author : lijie
     * @date :2021/10/9 20:36
     * Update By lijie 2021/10/9 20:36
     */
    private List<FileInfo> firstInitChunkUploadInternal(FileRequestData requestData) {
        return new ArrayList<>();
    }

    /***
     * Description: 根据文件md5查询文件信息
     * @param fileMd5 : 文件md5值
     * @return : java.util.List<com.persagy.dmp.file.model.FileInfo>
     * @author : lijie
     * @date :2021/10/9 20:32
     * Update By lijie 2021/10/9 20:32
     */
    private FileInfo queryFileInfosByFileMd5(String fileMd5) {
        LambdaQueryWrapper<FileInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FileInfo::getFileMd5,fileMd5);
        queryWrapper.eq(FileInfo::getValid,true);
        return this.getOne(queryWrapper);
    }
}
