package com.persagy.dmp.file.controller;

import cn.hutool.core.util.StrUtil;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.constant.ValidEnum;
import com.persagy.dmp.common.context.AppContext;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.lang.PsDateTime;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.common.utils.ResultHelper;
import com.persagy.dmp.file.model.FileInfo;
import com.persagy.dmp.file.model.FileInfoCreator;
import com.persagy.dmp.file.model.FileRequestData;
import com.persagy.dmp.file.model.UploadMes;
import com.persagy.dmp.file.service.IFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文件管理Controller
 * @author Charlie Yu
 * @date 2021-05-15
 */
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileController {

    private final IFileService fileService;

    @PostMapping("/insert")
    public CommonResult<FileInfo> insert(@RequestBody FileInfo fileInfo) {
        if(StrUtil.isBlank(fileInfo.getFileName()) ||
                StrUtil.isBlank(fileInfo.getGroupCode())) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        fileInfo.setCreationTime(new PsDateTime());
        fileInfo.setValid(ValidEnum.TRUE.getType());
        fileInfo = fileService.insertFile(fileInfo);
        return ResultHelper.single(fileInfo);
    }

    @PostMapping("/update")
    public CommonResult<FileInfo> update(@RequestBody FileInfo fileInfo) {
        if(StrUtil.isBlank(fileInfo.getId()) ||
                StrUtil.isBlank(fileInfo.getFileName()) ||
                StrUtil.isBlank(fileInfo.getGroupCode())) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        fileInfo.setModifiedTime(new PsDateTime());
        fileInfo = fileService.updateFile(fileInfo);
        return ResultHelper.single(fileInfo);
    }

    @PostMapping("/delete")
    public CommonResult<String> delete(@RequestBody String id) {
        if(StrUtil.isBlank(id)) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        FileInfo fileInfo = fileService.load(id);
        if(fileInfo == null) {
            throw new BusinessException("找不到对应的文件信息");
        }
        fileService.deleteFile(fileInfo);
        return ResultHelper.success();
    }

    @PostMapping("/load")
    public CommonResult<FileInfo> load(@RequestBody String id) {
        if(StrUtil.isBlank(id)) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        FileInfo fileInfo = fileService.load(id);
        if(fileInfo == null) {
            throw new BusinessException("找不到对应的文件信息");
        }
        return ResultHelper.single(fileInfo);
    }

    @PostMapping("/queryByBusiness")
    public CommonResult<List<FileInfo>> queryByBusiness(@RequestBody String businessId) {
        if(StrUtil.isBlank(businessId)) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        List<FileInfo> voList = fileService.queryByBusinessId(businessId);
        return ResultHelper.multi(voList);
    }

    @PostMapping("/queryByFileMd5")
    public CommonResult<List<FileInfo>> queryByFileMd5(@RequestBody String fileMd5) {
        if(StrUtil.isBlank(fileMd5)) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        List<FileInfo> voList = fileService.queryByFileMd5(fileMd5);
        return ResultHelper.multi(voList);
    }

    /***
     * Description: 初始化断点上传文件
     * @param requestData : 请求参数
     * @return : com.persagy.dmp.common.model.response.CommonResult<com.persagy.dmp.file.model.FileInfo>
     * @author : lijie
     * @date :2021/10/9 20:12
     * Update By lijie 2021/10/9 20:12
     */
    @PostMapping("/initChunkUpload")
    @CrossOrigin(origins = "*",allowCredentials = "true",methods = {RequestMethod.POST,RequestMethod.GET},maxAge = 3600)
    public CommonResult<UploadMes> initChunkUpload(@RequestBody FileRequestData requestData) {
        // 1.兼容一下groupCode
        fillGroupCode(requestData);
        // 2.判断必填项
        if(StrUtil.isBlank(requestData.getFileName())
                || StrUtil.isBlank(requestData.getFileMd5())
                || StrUtil.isBlank(requestData.getGroupCode())
                || null==requestData.getChunkCount()
                || requestData.getChunkCount()<=0) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        return ResultHelper.single(fileService.initChunkUpload(requestData));
    }

    /***
     * Description: 初始化大文件上传
     * @param requestData : 请求参数
     * @return : com.persagy.dmp.common.model.response.CommonResult<com.persagy.dmp.file.model.FileInfo>
     * @author : lijie
     * @date :2021/10/9 20:12
     * Update By lijie 2021/10/9 20:12
     */
    @PostMapping("/composeFile")
    @CrossOrigin(origins = "*",allowCredentials = "true",methods = {RequestMethod.POST,RequestMethod.GET},maxAge = 3600)
    public CommonResult<FileInfo> composeFile(@RequestBody FileRequestData requestData) {
        //基础参数校验
        // 1.兼容一下groupCode
        fillGroupCode(requestData);
        // 2.判断必填项
        if(StrUtil.isBlank(requestData.getFileName())
                || StrUtil.isBlank(requestData.getFileMd5())
                || StrUtil.isBlank(requestData.getGroupCode())
                || StrUtil.isBlank(requestData.getFileBucket())) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        FileInfoCreator.checkBucketNameValid(requestData.getFileBucket());
        return ResultHelper.single(fileService.composeFile(requestData));
    }
    /***
     * Description: 初始化单个文件上传
     * @param requestData : 请求参数
     * @return : com.persagy.dmp.common.model.response.CommonResult<com.persagy.dmp.file.model.FileInfo>
     * @author : lijie
     * @date :2021/10/9 20:12
     * Update By lijie 2021/10/9 20:12
     */
    @PostMapping("/initSingleUpload")
    @CrossOrigin(origins = "*",allowCredentials = "true",methods = {RequestMethod.POST,RequestMethod.GET},maxAge = 3600)
    public CommonResult<UploadMes> initSingleUpload(@RequestBody FileRequestData requestData) {
        //基础参数校验
        // 1.兼容一下groupCode
        fillGroupCode(requestData);
        // 2.判断必填项
        if(StrUtil.isBlank(requestData.getFileName())
                || StrUtil.isBlank(requestData.getFileMd5())
                || StrUtil.isBlank(requestData.getGroupCode())
                || StrUtil.isBlank(requestData.getFileBucket())
                || null==requestData.getFileSize()) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        return ResultHelper.single(fileService.initSingleUpload(requestData));
    }
    /***
     * Description: 初始化文件下载
     * @param requestData : 请求参数
     * @return : com.persagy.dmp.common.model.response.CommonResult<com.persagy.dmp.file.model.FileInfo>
     * @author : lijie
     * @date :2021/10/9 20:12
     * Update By lijie 2021/10/9 20:12
     */
    @PostMapping("/initFileDownload")
    @CrossOrigin(origins = "*",allowCredentials = "true",methods = {RequestMethod.POST,RequestMethod.GET},maxAge = 3600)
    public CommonResult<FileInfo> initFileDownload(@RequestBody FileRequestData requestData) {
        //基础参数校验
        if(StrUtil.isBlank(requestData.getFileId())) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        return ResultHelper.single(fileService.initFileDownload(requestData));
    }
    /***
     * Description: 填充一下集团编码
     * @param requestData : 请求参数
     * @return : void
     * @author : lijie
     * @date :2021/10/21 20:14
     * Update By lijie 2021/10/21 20:14
     */
    private void fillGroupCode(FileRequestData requestData) {
        if (StrUtil.isNotBlank(requestData.getGroupCode())
                || StrUtil.isBlank(AppContext.getContext().getGroupCode())){
            return;
        }
        requestData.setGroupCode(AppContext.getContext().getGroupCode());
    }

}
