
using Mall.Education.Helper;
using Mall.Education.Models;
using Mall.Education.RabbitMQ;
using System;
using System.ServiceProcess;
using COSSnippet;
using Mall.Education.Offices;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Mall.Education
{
    public class Program
    {
        static void Main(string[] args)
        {
            //ServiceBase[] ServicesToRun;
            //ServicesToRun = new ServiceBase[]
            //{
            //    new RebornTimerServer(),
            //};
            //ServiceBase.Run(ServicesToRun);

            //RabbitConfig rabbit = new RabbitConfig()
            //{
            //    HostName = "47.96.25.130",
            //    Port = 5672,
            //    UserName = "guest",
            //    Password = "viitto2019",
            //    QueenName = RabbitKey.QUEEN_GENERATE_EDUCARION
            //};
            //RabbiMQManager.DealWithMessage(rabbit);


            //new OfficeHelper().GetWordToImagePathList("C:/Users/Administrator/Desktop/testImage/网课需求(1).docx", "C:/Users/Administrator/Desktop/testImage/Image/", null);

            //new Word2ImageConverter().ConvertToImage("https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Test/Upload/Education/wkxq.docx", "C:/Users/Administrator/Desktop/testImage/Image/", System.Drawing.Imaging.ImageFormat.Png, out int PageNum);
            //new Pdf2ImageConverter().ConvertToImage("C:/Users/Administrator/Desktop/testImage/MA10048W_zh.pdf", "C:/Users/Administrator/Desktop/testImage/PdfImage/");
            //new Ppt2ImageConverter().ConvertToImage("C:/Users/Administrator/Desktop/testImage/kjljlkjk.ppt", "C:/Users/Administrator/Desktop/testImage/PptImage/");


            //FileDataHelper.InsertGoodsFileImage();
            //TransferUploadObjectModel m = new TransferUploadObjectModel();
            //m.TransferUploadFile("C:/Users/Administrator/Desktop/testImage/Image/网课需求(1)_001.Png", "网课需求(1)_001.Png");

            ImageConvertData(new GoodsModel() { CourseId = 1, GoodsId = 1, FilePath = "" });

            Console.ReadKey();
        }

        /// <summary>
        /// 处理文件
        /// </summary>
        /// <param name="GoodsModel"></param>
        private static void ImageConvertData(GoodsModel GoodsModel)
        {
            string tempDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tempfile/Image");
            if (!Directory.Exists(tempDir))
                Directory.CreateDirectory(tempDir);
            //是否需要先把文件下载到服务器
            GoodsModel.FilePath = "https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Test/Upload/Education/wkxq.docx";
            //文件生成图片暂存本地
            string FileExtension = Path.GetExtension(GoodsModel.FilePath);
            string FileName = Path.GetFileNameWithoutExtension(GoodsModel.FilePath);
            string FileDirectory = Path.GetDirectoryName(GoodsModel.FilePath);
            int PageNum;
            System.Drawing.Imaging.ImageFormat imageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
            if (FileExtension == ".doc" || FileExtension == ".docx")
            {
                new Word2ImageConverter().ConvertToImage(GoodsModel.FilePath, tempDir + "/", imageFormat, out PageNum);
            }
            else if (FileExtension == ".pdf")
            {
                new Pdf2ImageConverter().ConvertToImage(GoodsModel.FilePath, tempDir + "/", imageFormat, out PageNum);
            }
            else if (FileExtension == ".ppt" || FileExtension == ".pptx")
            {
                new Ppt2ImageConverter().ConvertToImage(GoodsModel.FilePath, tempDir + "/", imageFormat, out PageNum);
            }
            else
            {
                return;
            }

            if (PageNum > 0)
            {
                List<GoodsImage> goodsImages = new List<GoodsImage>();
                //开始上传图片至服务器
                TransferUploadObjectModel m = new TransferUploadObjectModel();
                for (var i = 1; i <= PageNum; i++)
                {
                    string newName = FileName + "_" + i.ToString("000") + "." + imageFormat.ToString();
                    m.TransferUploadFile(tempDir + "/" + newName, newName);

                    //上传之后 写入商品课程图片数据
                    goodsImages.Add(new GoodsImage
                    {
                        Sort = i,
                        Path = FileDirectory + "/" + newName
                    });
                }

                //更新图片数据
                bool flag = FileDataHelper.InsertGoodsFileImage(GoodsModel.CourseId, JsonConvert.SerializeObject(goodsImages)) > 0;
                if (flag == false)
                {
                    Console.WriteLine("更新课程图片列表失败：CourseId:" + GoodsModel.CourseId);
                }
            }
        }
    }
}
