package cn.wisenergy.service.app.impl;

import cn.wisenergy.common.utils.FrameGrabberKit;

import cn.wisenergy.model.app.shopZx;
import cn.wisenergy.service.app.UploadService;

import java.io.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import cn.wisenergy.mapper.ShopZxMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by m1991 on 2021/2/24 13:42
 */
@Slf4j
@Transactional
@Service("UploadService")
public class UploadServiceImpl implements UploadService {

    @Autowired
    private ShopZxMapper shopZxMapper;

        /**
         * 视频文件上传
         */
        @Override
        public Map<String, Object> uploadVideo(MultipartFile file,HttpServletRequest request,String userId,String zxField,String zxAddress) throws Exception {
            Map<String, Object> resultMap=new HashMap<String, Object>();

            String basePath = request.getScheme() + "://" + request.getServerName()
                    + ":" + request.getServerPort()+"/mimi/upload/video/";

            Long time = new Date().getTime();

            String zxName = file.getOriginalFilename();//文件原始名称
            String suffixName = zxName.substring(zxName.lastIndexOf("."));//从最后一个.开始截取。截取zxName的后缀名
            String newzxName = time+suffixName; //文件新名称
            //设置文件存储路径,可以存放在你想要指定的路径里面
            String rootPath="D:/mimi/"+File.separator+"upload/video/"; //上传视频存放位置

            String filePath = rootPath+newzxName;

            File newFile = new File(filePath);


            //判断目标文件所在目录是否存在
            if(!newFile.getParentFile().exists()){
                //如果目标文件所在的目录不存在,则创建父目录
                newFile.getParentFile().mkdirs();
            }

            //将内存中的数据写入磁盘
            file.transferTo(newFile);
            //视频上传保存url
            String zxUrl = basePath + newzxName;

            //视频封面图处理
            String newImgName = time+".jpg";
            String framefile = rootPath + newImgName;
            String imgUrlSave = basePath+newImgName;//图片最终位置路径


            //视频截取封面图
            String imgUrl=FrameGrabberKit.getVedioImg(zxUrl, framefile, imgUrlSave);


            resultMap.put("zxUrl", zxUrl);
            resultMap.put("imgUrl", imgUrl);
            resultMap.put("returnCode", 0);
            System.out.println("上传的文件名为:"+zxName+",后缀名为:"+newzxName);

            shopZxMapper.zxadd(zxUrl,userId,zxName,zxField,imgUrl,zxAddress);
            return resultMap;
        }

        /**
         * 图片文件上传
         */
        @Override
        public Map<String, Object> uploadImage(MultipartFile file, HttpServletRequest request,String userId,String zxField, String zxAddress) throws Exception {
            Map<String, Object> resultMap=new HashMap<String, Object>();

            String basePath = request.getScheme() + "://" + request.getServerName()
                    + ":" + request.getServerPort()+"/mimi/upload/images/";

            Long time = new Date().getTime();

            String zxName = file.getOriginalFilename();//文件原始名称
            String suffixName = zxName.substring(zxName.lastIndexOf("."));//从最后一个.开始截取。截取zxName的后缀名
            String newzxName = time+suffixName; //文件新名称
            //设置文件存储路径,可以存放在你想要指定的路径里面
            String rootPath="D:/mimi/"+File.separator+"upload/images/"; //上传图片存放位置

            String filePath = rootPath+newzxName;
            File newFile = new File(filePath);
            //判断目标文件所在目录是否存在
            if(!newFile.getParentFile().exists()){
                //如果目标文件所在的目录不存在,则创建父目录
                newFile.getParentFile().mkdirs();
            }

            //将内存中的数据写入磁盘
            file.transferTo(newFile);
            //图片上传保存url
            String imgUrl = basePath + newzxName;

            resultMap.put("imgUrl", imgUrl);
            resultMap.put("returnCode", 0);
            String zxUrl="";
            shopZxMapper.zxadd(zxUrl,userId,zxName,zxField,imgUrl,zxAddress);
            return resultMap;
        }

        /**
         * 项目目录下的图片文件上传
         */
        @Override
        public Map<String, Object> getImageUrl(HttpServletRequest request) throws Exception {
            Map<String,Object> result = new HashMap<String,Object>();
            //获取图片在项目路径下的地址
            String basePath= ClassUtils.getDefaultClassLoader().getResource("").getPath();
            String oldPath=basePath+"/static/images/animate.png";

            Long res =new Date().getTime();
            //设置文件存储路径,可以存放在你想要指定的路径里面
            String rootPath="D:/mimi/"+File.separator+"upload/images/";
            // 新文件名
            String newzxName =res + oldPath.substring(oldPath.lastIndexOf("."));
            //新文件
            File newFile=new File(rootPath+File.separator+newzxName);
            //判断文件目录是否存在
            if(!newFile.getParentFile().exists()){
                //如果目标文件所在的目录不存在,则创建父目录
                newFile.getParentFile().mkdirs();
            }

            //-------把图片文件写入磁盘 start ----------------
            FileOutputStream fos = new FileOutputStream(newFile);
            //读取本地文件
            File localFile = new File(oldPath);
            //获取本地文件输入流
            InputStream stream=new FileInputStream(localFile);
            //写入目标文件
            byte[] buffer=new byte[1024*1024];
            int byteRead=0;
            //stream.read(buffer) 每次读到的数据存放在 buffer 数组中
            while((byteRead=stream.read(buffer))!=-1){
                //在 buffer 数组中 取出数据 写到 (输出流)磁盘上
                fos.write(buffer, 0, byteRead);
                fos.flush();
            }
            fos.close();
            stream.close();
            //-------把图片文件写入磁盘 end ----------------

            //服务器图片地址
            String baseURL = request.getScheme() + "://" + request.getServerName()
                    + ":" + request.getServerPort()+"/mimi/upload/images/";
            String imgUrl=baseURL+newzxName;

            result.put("imgUrl", imgUrl);
            result.put("returnCode", 0);
            return result;
        }


//    /**
//     *  获取资讯分页数据
//     * @return
//     */
//    @Override
}