package com.huyi.service.common.file;

import com.tianhu.common.core.domain.R;
import com.tianhu.common.core.utils.SecurityUtils;
import com.tianhu.common.core.utils.ServletUtils;
import com.tianhu.common.core.utils.StringUtils;
import com.tianhu.common.core.web.controller.BaseController;
import com.tianhu.common.core.web.domain.AjaxResult;
import com.tianhu.common.security.service.TokenService;
import com.tianhu.system.api.RemoteFileService;
import com.tianhu.system.api.domain.PubFileInf;
import com.tianhu.system.api.domain.SysFile;
import com.tianhu.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件Controller
 *
 * @author tianhu
 * @date 2021-02-04
 */
@RestController
@RequestMapping("/file")
public class FileController extends BaseController
{
    @Autowired
    private TokenService tokenService;
    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 永久接口存储
     * 文件上传
     */
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file,@RequestParam("fileType") String fileType) throws IOException
    {
        if (!file.isEmpty())
        {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            String companyId = loginUser.getSysUser().getCompanyId();
            //获取token
            String token = SecurityUtils.getToken(ServletUtils.getRequest());
            //文件传输
            R<PubFileInf> fileResult = remoteFileService.uploadFile(file,"01",fileType,"00",companyId,token);
            if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData()))
            {
                return AjaxResult.error("文件服务异常，请联系管理员");
            }
            String url = fileResult.getData().getPfiUrl();
            String fileUrl = fileResult.getData().getPfiFileUrl();
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileUrl",fileUrl);
            ajax.put("url", url);
            ajax.put("fileId", fileResult.getData().getPfiFileId());
            ajax.put("fileName", fileResult.getData().getPfiFileName());

            return ajax;
        }
        return AjaxResult.error("上传文件异常，请联系管理员");
    }

    /**
     * 文件上传  自定义文件存储时间
     * @param file
     * @param fileType
     * storageType 00:永久存储 01：7天临时存储 02 14天临时存储 03  30天临时存储
     * @return
     * @throws IOException
     */
    @PostMapping("/customizeUploadFile")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file,@RequestParam("fileType") String fileType,@RequestParam("storageType") String  storageType) throws IOException
    {
        if (!file.isEmpty())
        {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            String companyId = loginUser.getSysUser().getCompanyId();
            //获取token
            String token = SecurityUtils.getToken(ServletUtils.getRequest());
            //文件传输
            R<PubFileInf> fileResult = remoteFileService.uploadFile(file,"01",fileType,storageType,companyId,token);
            if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData()))
            {
                return AjaxResult.error("文件服务异常，请联系管理员");
            }
            String url = fileResult.getData().getPfiUrl();
            String fileUrl = fileResult.getData().getPfiFileUrl();
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileUrl",fileUrl);
            ajax.put("url", url);
            ajax.put("fileId", fileResult.getData().getPfiFileId());
            ajax.put("fileName", fileResult.getData().getPfiFileName());

            return ajax;
        }
        return AjaxResult.error("上传文件异常，请联系管理员");
    }
    @PostMapping("/upload")
    public AjaxResult avatar(@RequestParam("file") MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            R<SysFile> fileResult = remoteFileService.upload(file,"01");
            if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData()))
            {
                return AjaxResult.error("文件服务异常，请联系管理员");
            }
            String url = fileResult.getData().getUrl();
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            return ajax;
        }
        return AjaxResult.error("上传文件异常，请联系管理员");
    }
}
