package com.jeethink.web.controller.system;

import com.jeethink.common.config.JeeThinkConfig;
import com.jeethink.common.core.domain.AjaxResult;
import com.jeethink.common.core.domain.entity.SysUser;
import com.jeethink.common.core.domain.model.LoginUser;
import com.jeethink.common.utils.DateUtils;
import com.jeethink.common.utils.ServletUtils;
import com.jeethink.common.utils.StringUtils;
import com.jeethink.common.utils.file.FileUploadUtils;
import com.jeethink.common.utils.file.MimeTypeUtils;
import com.jeethink.common.utils.uuid.IdUtils;
import com.jeethink.framework.config.ServerConfig;
import com.jeethink.framework.web.service.TokenService;
import com.jeethink.system.domain.SystemFile;
import com.jeethink.system.service.ISystemFileService;
import org.apache.commons.io.FilenameUtils;
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;

/**
 * @author Administrator
 * @version 1.0
 * @description: 附件上传
 * @date 2022-03-24 14:12
 */
@RestController
@RequestMapping("/system/sysFile")
public class SysFileController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private ISystemFileService systemFileService;
    /**
     * @auter：gd
     * @des 附件上传
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public AjaxResult avatar(@RequestParam("uploadfile") MultipartFile file,@RequestParam("uid") String uid) throws IOException
    {
        if (!file.isEmpty())
        {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            SysUser user = loginUser.getUser();
            String filePath = JeeThinkConfig.getUploadPath();//文件磁盘存放路径
            // 上传并返回新文件名称
            String urlName = "",fileName="";
            urlName = FileUploadUtils.upload(filePath, file);
            fileName = file.getOriginalFilename();
            String extension = getExtension(file);
            filePath = filePath+ "/"  + DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;
            SystemFile sysfile = new SystemFile();
            sysfile.setFileId(uid);
            sysfile.setFileName(fileName);
            String url = serverConfig.getUrl() + urlName;
            sysfile.setFileUrl(url);//浏览器直接访问地址
            sysfile.setFilePath(filePath);
            sysfile.setFileSize(file.getSize());
            sysfile.setFileSuffix(extension);
            sysfile.setCreateUserId(user.getUserId());
            sysfile.setFileCreateTime(DateUtils.getNowDate());
            int rows = systemFileService.insertSystemFile(sysfile);
            return AjaxResult.success("上传成功",sysfile);
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }


    /**
     * 获取文件名的后缀
     *
     * @param file 表单文件
     * @return 后缀名
     */
    public static final String getExtension(MultipartFile file)
    {
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        if (StringUtils.isEmpty(extension))
        {
            extension = MimeTypeUtils.getExtension(file.getContentType());
        }
        return extension;
    }
}
