package cn.api.atv;

import cn.cloud.util.file.MinioUtil;
import cn.cloud.util.file.OssBootUtil;
import cn.common.annotation.Log;
import cn.common.enums.BizOpType;
import cn.constant.CommonConst;
import cn.facade.abs.IService;
import cn.util.core.IsKit;
import cn.util.core.LogKit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/atv")
public class AtvSysCtrl extends IService
{
    /**
     * 本地：local minio：minio 阿里：alioss
     */
    @Value(value="${sam.file.uploadType}")
    private String uploadType;

    /**
     * TTS文件上传统一方法
     * 页面发文字到控制台。 控制台转换为对应语音文件，然后通过此APi发送语音文件
     * 到服务特定目录下。 文件名不限
     */
    @PostMapping(value = "/sys/common/uploadTts")
    @Log(title = "TTS文件上传统一方法-uploadTts", businessType = BizOpType.UPDATEFILE)
    public Object ttsUpload(HttpServletRequest request)
    {
        return this.uploadTts(request);
    }

    /**ATV 用*/
    public Object uploadTts(HttpServletRequest request)
    {
        LogKit.info("uploadTts-1uploadType："+uploadType);
        Map<String,Object> resultMap = new HashMap<>();
        String savePath = "";
        String bizPath = request.getParameter("biz");
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");// 获取上传文件对象

        if(IsKit.isEmpty(bizPath)){
            if(CommonConst.UPLOAD_TYPE_OSS.equals(uploadType)){
                LogKit.info("uploadTts-OSS：");
                //未指定目录，则用阿里云默认目录 upload
                bizPath = "upload";
            }else{
                bizPath = "";
            }
        }
        if(CommonConst.UPLOAD_TYPE_LOCAL.equals(uploadType)){
            //针对jeditor编辑器如何使 lcaol模式，采用 base64格式存储
            String jeditor = request.getParameter("jeditor");
            if(IsKit.isNotEmpty(jeditor)){
                LogKit.info("uploadTts-jeditor：");
                resultMap.put("success",true);
                resultMap.put("message",CommonConst.UPLOAD_TYPE_LOCAL);
                return resultMap;
            }else{
                LogKit.info("uploadTts-Local：");
                //原来代码：所保留位置由控制台法老的参数决定。bjtu修订为audio目录
                //savePath = this._uploadLocal(file,bizPath);
                savePath = uploadFileService.uploadLocal(file,"audio");
            }
        }
        else if(CommonConst.UPLOAD_TYPE_MINIO.equals(uploadType)){
            savePath = MinioUtil.upload(file,bizPath);
        }
        else if(CommonConst.UPLOAD_TYPE_OSS.equals(uploadType)) {
            savePath = OssBootUtil.upload(file,bizPath);
        }
        if(IsKit.isNotEmpty(savePath)){
            resultMap.put("success",true);
            resultMap.put("message",savePath);
        }else {
            resultMap.put("success",false);
            resultMap.put("message","上传失败！");
        }
        LogKit.info("uploadTts-resultMap："+resultMap);
        return resultMap;
    }
}
