123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- using LYFZ.BLL;
- using LYFZ.Model;
- using LYFZ.Network.FileOperations;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Runtime.Serialization.Json;
- using System.Text;
- using System.Windows.Forms;
- namespace LYFZ.Network.FTPOperation
- {
- /// <summary>
- /// 客户端发送来的ftp需要发送的信息
- /// </summary>
- public class FTPDataCommand
- {
-
- BLL.BaseBLL.BaseBLL_FTPServiceUserAccount bll = new BLL.BaseBLL.BaseBLL_FTPServiceUserAccount();
- /// <summary>
- /// ftp控制的操作指令
- /// </summary>
- /// <param name="tcpData"></param>
- /// <returns></returns>
- public string CommandFtpData(string tcpData)
- {
-
- CheckExeOpen();
- string resultData="";
- CommandData comData=new CommandData();
- comData = (CommandData)JsonToObject(tcpData,comData);
- switch(comData.CommandType)
- {
- case "A":
- AddJsonDataToFtpPools(tcpData, comData.CommandType, comData.UpArtwork, comData.dirTypeData);
- break;
- case "P":
- string filePath = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath)
- + "\\FTPClient\\UpFileManage\\Action.txt";
- if (File.Exists(filePath))
- {
- resultData = File.ReadAllText(filePath, Encoding.UTF8);
- }
- else
- {
- resultData = "[]";
- }
- break;
- case "C":
- string filePath1 = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\FTPClient\\UpFileManage\\Action.txt";
- string txtFileInfo = "";
- if (File.Exists(filePath1))
- {
- txtFileInfo = File.ReadAllText(filePath1, Encoding.UTF8);
- }
- List<UpFTPServiceFileShop> shopData = new List<UpFTPServiceFileShop>();
- shopData = (List<UpFTPServiceFileShop>)Json.JsonTool.JsonToObject(txtFileInfo, shopData);
- int totalCount = 0;
- foreach (var shop in shopData)
- {
- totalCount += shop.items.FindAll(p=>!p.bSuccess&&p.bOpenStatus>-1).Count;
- }
- resultData = totalCount.ToString();
- break;
- case "S":
- File.WriteAllText(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\FTPClient\\UpFileManage\\" + comData.bUpfileCode + "_Command.txt", comData.dirTypeData);
- SendCommandToExe(comData.CommandType);
- break;
- case "O":
- resultData = CommandComplete();
- File.WriteAllText(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\FTPClient\\UpFileManage\\所有_Command.txt", resultData);
- break;
- }
- return resultData;
- }
- string CommandComplete()
- {
- DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(Application.ExecutablePath) + "\\FTPClient\\UpFileManage\\Complete");
- DirectoryInfo[] arrDir = di.GetDirectories();
- SortAsFolderName(ref arrDir);
- List<UpFTPServiceFileShop> shopData = new List<UpFTPServiceFileShop>();
- foreach (var mod in arrDir)
- {
- string fileText = File.ReadAllText(mod.FullName + "\\Complete.txt");
- shopData.AddRange(Newtonsoft.Json.JsonConvert.DeserializeObject<List<UpFTPServiceFileShop>>(fileText));
- }
- List<dynamic> dyObjList = new List<dynamic>();
- for (int i = 0; i < shopData.Count; i++)
- {
- dynamic dyObj = new System.Dynamic.ExpandoObject();
- dyObj.localShopInfo = shopData[i].localShopInfo;
- List<dynamic> dataItemList = new List<dynamic>();
- foreach (var mod in shopData[i].items)
- {
- dynamic dyItem = new System.Dynamic.ExpandoObject();
- dyItem.Text = "" + mod.OrderNumber + "(" + mod.DirFileType + "上传到——" + shopData[i].upShopText + "(" + shopData[i].hostPath + ":" + shopData[i].ftpName + ")" + ")";
- dyItem.StartTime = mod.StartTime;
- dyItem.EndTime = mod.EndTime;
- dataItemList.Add(dyItem);
- //ListViewItem item = new ListViewItem(new string[] {"" + mod.OrderNumber + "(" + mod.DirFileType + "上传到——" +
- //list[i].upShopText + "(" + list[i].hostPath + ":" + list[i].ftpName + ")" + ")",mod.StartTime,mod.EndTime});
- //item.Group = group;
- //this.listViewCompletedTask.Items.Add(item);
- }
- dyObj.Item = dataItemList;
- dyObjList.Add(dyObj);
- }
- return Newtonsoft.Json.JsonConvert.SerializeObject(dyObjList);
- }
- /// <summary>
- /// 文件夹排序
- /// </summary>
- /// <param name="dirs"></param>
- private void SortAsFolderName(ref DirectoryInfo[] dirs)
- {
- Array.Sort(dirs, delegate(DirectoryInfo x, DirectoryInfo y) { return x.Name.CompareTo(y.Name); });
- }
- /// <summary>
- /// 文件添加入新增池
- /// </summary>
- /// <param name="jsonData"></param>
- void AddJsonDataToFtpPools(string jsonData, string comType, bool UpArtwork, string dirTypeData)
- {
-
- //BLL_ErpOrder erpOrderBll= new BLL_ErpOrder();
- //BLL_ErpOrderDigital digtalBll = new BLL_ErpOrderDigital();
- UpFTPServiceFileShop shop = new UpFTPServiceFileShop();
- //DAL.DAL_Config dalConfig = new DAL.DAL_Config();
- //Model_ServerConfig modConfig = dalConfig.GetModelServerSysConfig();
- shop = (UpFTPServiceFileShop)JsonToObject(jsonData, shop);
- DataSet ds = bll.GetAllList();
- //SelectPhotoHandling handling ;
- /* LYFZ.Model.Model_ServerConfig.PhotoDirectoryCategory PrimitivePicture = Model.Model_ServerConfig.PhotoDirectoryCategory.原片保存目录;
- LYFZ.Model.Model_ServerConfig.PhotoDirectoryCategory EarlyRetouch = Model.Model_ServerConfig.PhotoDirectoryCategory.初修片保存目录;
- LYFZ.Model.Model_ServerConfig.PhotoDirectoryCategory FineRetouch = Model.Model_ServerConfig.PhotoDirectoryCategory.精修片保存目录;
- LYFZ.Model.Model_ServerConfig.PhotoDirectoryCategory DesignPiece = Model.Model_ServerConfig.PhotoDirectoryCategory.设计片保存目录;
- string tempPath =PrimitivePicture.ToString() + ">" + modConfig.GetPhotoDirectoryList(PrimitivePicture).ToString()
- + "*" + EarlyRetouch.ToString() + ">" + modConfig.GetPhotoDirectoryList(EarlyRetouch).ToString()
- + "*" + FineRetouch.ToString() + ">" + modConfig.GetPhotoDirectoryList(FineRetouch).ToString()
- + "*" + DesignPiece.ToString() + ">" + modConfig.GetPhotoDirectoryList(DesignPiece).ToString();
- if (LYFZ.BLL.RedPhotoFilePathList.CurrentRedPhotoFilePathList == null)
- {
- LYFZ.BLL.RedPhotoFilePathList.CurrentRedPhotoFilePathList = new BLL.RedPhotoFilePathList(tempPath);
- }*/
- foreach(DataRow dr in ds.Tables[0].Rows)
- {
- if(dr["Ftp_Dividedshop"].ToString().Equals(shop.upShopCode))
- {
- // int port = 0;
-
- UpFTPServiceFileGroup group = shop.items[0];
- //group.physicsPath = GetServicePhysicsPath(group.DirFileType, modConfig);
- //Model_ErpOrder erpOrder = erpOrderBll.GetModel(group.OrderNumber);
- //Model_ErpOrderDigital digtalOrder = digtalBll.GetModel(group.ViceNumber);
- //string strOrdType = erpOrder.Ord_Type;
- //erpOrder.Ord_Type = "0";
- //handling = new SelectPhotoHandling(erpOrder, digtalOrder);
- //erpOrder.Ord_Type = strOrdType;
- //CheckDirectoryOrFile(handling,group, UpArtwork, dirTypeData);
- FileOperation.WriteFile(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath)
- + "/FTPClient/UpFileManage/NewJson/" + group.OrderNumber + ".txt", ObjectToJson(shop));
- SendCommandToExe(comType);
- break;
- }
- }
- }
- /// <summary>
- /// 检查文件夹和文件
- /// </summary>
- void CheckDirectoryOrFile( SelectPhotoHandling handling,UpFTPServiceFileGroup group, bool UpArtwork, string dirTypeData)
- {
-
- List<string> Original_List=new List<string>();
- List<string> Preview_List = new List<string>();
- List<string> Small_List = new List<string>();
- if (UpArtwork)
- {
- Original_List = handling.GetSpecifyTypePhotoList(GetDirectoryType(group.DirFileType), SelectPhotoHandling.PhotoType.Original);
- }
- Preview_List = handling.GetSpecifyTypePhotoList(GetDirectoryType(group.DirFileType), SelectPhotoHandling.PhotoType.Preview);
- Small_List = handling.GetSpecifyTypePhotoList(GetDirectoryType(group.DirFileType), SelectPhotoHandling.PhotoType.Small);
- if (group.DirFileType.Equals("原片"))
- {
- ///
- string[] dirList = dirTypeData.Split(',');
- foreach (string dirData in dirList)
- {
- BindImageData(group, Original_List.Where(p => p.IndexOf("\\" + dirData + "\\") != -1).ToList());
- BindImageData(group, Preview_List.Where(p => p.IndexOf("\\" + dirData + "\\") != -1).ToList());
- BindImageData(group, Small_List.Where(p => p.IndexOf("\\" + dirData + "\\") != -1).ToList());
- }
- }
- else
- {
- if (group.PhotoType.Equals("儿童订单"))
- {
- string[] dirList = dirTypeData.Split(',');
- foreach (string dirData in dirList)
- {
- BindImageData(group, Original_List.Where(p => p.IndexOf("\\" + dirData + "\\") != -1).ToList());
- BindImageData(group, Preview_List.Where(p => p.IndexOf("\\" + dirData + "\\") != -1).ToList());
- BindImageData(group, Small_List.Where(p => p.IndexOf("\\" + dirData + "\\") != -1).ToList());
- }
- }
- else
- {
- BindImageData(group, Original_List);
- BindImageData(group, Preview_List);
- BindImageData(group, Small_List);
- }
- }
- /*string[] dirList = dirTypeData.Split(',');
- if(!string.IsNullOrEmpty(group.localServicePath))
- {
- string directoryData = GetServicePhysicsDir(group.DirFileType);
- foreach (string dirData in dirList)
- {
- string localServicePath = "\\" + directoryData + "\\" + group.OrderNumber + "\\" + dirData + "\\";
- group.localServicePath = localServicePath;
- string dirPath = group.physicsPath + directoryData + "\\" + group.OrderNumber + "\\" + dirData + "\\";
- if (Directory.Exists(dirPath))
- {
- BindImagePreviewOrSmallData(group, "Preview", dirPath, localServicePath);
- BindImagePreviewOrSmallData(group, "Small", dirPath, localServicePath);
- if(UpArtwork)
- {
- BindImageOriginalData(group, dirPath, localServicePath);
- }
- }
- }
- }*/
- }
- void BindImageData(UpFTPServiceFileGroup group, List<string> filePathList)
- {
- foreach(string path in filePathList)
- {
- group.items.Add(new UpFTPServiceFileItem()
- {
- localFilePath = path,
- serviceFilePath = path.Split('$').Length>0? path.Split('$')[1]:"",
- });
- }
- /* if (Directory.Exists(dirPath + "" ))
- {
- string[] fileList = Directory.GetFiles(dirPath );
- foreach (string file in fileList)
- {
- string fileName = Path.GetFileName(file);
- group.items.Add(new UpFTPServiceFileItem()
- {
- localFilePath = file,
- serviceFilePath = servicePath + fileName,
- });
- }
- }*/
- }
-
-
- /// <summary>
- /// 获取不同图片在服务器上的物理路径
- /// </summary>
- /// <param name="type">类型</param>
- /// <param name="modConfig">服务配置实体</param>
- /// <returns></returns>
- public string GetServicePhysicsPath(string type, Model_ServerConfig modConfig )
- {
- switch(type)
- {
- case "原片":
- return ConvertPath(modConfig.PrimitivePicturePath);
-
- case "初修":
- return ConvertPath(modConfig.EarlyRetouchPath);
-
- case "精修":
- return ConvertPath(modConfig.FineRetouchPath);
-
- case "设计":
- return ConvertPath(modConfig.DesignPiecePath);
-
- }
- return "";
- }
- /// <summary>
- /// 打印文件夹类型
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public LYFZ.Model.Model_ErpOrder.DirectoryType GetDirectoryType(string type)
- {
- switch(type)
- {
- case "原片":
- return LYFZ.Model.Model_ErpOrder.DirectoryType.PrimitivePicture;
- case "初修":
- return LYFZ.Model.Model_ErpOrder.DirectoryType.EarlyRetouch;
- case "精修":
- return LYFZ.Model.Model_ErpOrder.DirectoryType.FineRetouch;
- default:
- return LYFZ.Model.Model_ErpOrder.DirectoryType.DesignPiece;
- }
-
- }
- /// <summary>
- /// 获取不同图片在服务器上的物理路径
- /// </summary>
- /// <param name="type">类型</param>
- /// <param name="modConfig">服务配置实体</param>
- /// <returns></returns>
- public string GetServicePhysicsDir(string type)
- {
- switch (type)
- {
- case "原片":
- return "客户原片(管理软件)";
- case "初修":
- return "初修片(管理软件)";
- case "精修":
- return "精修片(管理软件)";
- case "设计":
- return "设计片(管理软件)";
- }
- return "";
- }
- public string ConvertPath(string path)
- {
- string[] pathList = path.Split('|');
- foreach(string tpath in pathList)
- {
- if(!string.IsNullOrEmpty(tpath))
- {
- string[] dataColum = tpath.Split('?');
- if(Convert.ToBoolean(dataColum[2]))
- {
- return dataColum[0];
- }
- }
- }
- return "";
- }
- #region Json对象实例化方式
- /// <summary>
- /// 实体转json
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static string ObjectToJson(object obj)
- {
- DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
- MemoryStream stream = new MemoryStream();
- serializer.WriteObject(stream, obj);
- byte[] dataBytes = new byte[stream.Length];
- stream.Position = 0;
- stream.Read(dataBytes, 0, (int)stream.Length);
- return Encoding.UTF8.GetString(dataBytes);
- }
- // 从一个Json串生成对象信息
- public static object JsonToObject(string jsonString, object obj)
- {
- DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
- MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
- return serializer.ReadObject(mStream);
- }
- public static object JsonToObjectByte(byte[] byteData, object obj)
- {
- DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
- MemoryStream mStream = new MemoryStream(byteData);
- return serializer.ReadObject(mStream);
- }
- #endregion
- #region 通过程序指针获取对象并传输指令
- [DllImport("user32.dll")]
- public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
- [DllImport("user32.dll")]
- public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
- [DllImport("user32.dll", CharSet = CharSet.Unicode)]
- public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
- public void SendCommandToExe(string code)
- {
- IntPtr fWnd = FindWindow(null, "远程相片上传工具");
-
- IntPtr txtbox = FindWindowEx(fWnd, new IntPtr(0), null, "");
- // File.WriteAllText("D://txt2.txt", txtbox.ToString());
- const int WM_CHAR = 0x0102;
- string strtext = code;
- UnicodeEncoding encode = new UnicodeEncoding();
- char[] chars = encode.GetChars(encode.GetBytes(strtext));
- Message msg;
- foreach (char c in chars)
- {
- msg = Message.Create(txtbox, WM_CHAR, new IntPtr(c), new IntPtr(0));
- PostMessage(msg.HWnd, msg.Msg, msg.WParam, msg.LParam);
- }
- }
- public void CheckExeOpen()
- {
- if (System.Diagnostics.Process.GetProcessesByName("LYFZ.PhotoUpload").ToList().Count == 0)
- {
- //File.WriteAllText("D://txt1.txt", Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "/FTPClient/LYFZ.PhotoUpload.exe");
- Process p = new Process();
-
- p.StartInfo.FileName = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "/FTPClient/"+"LYFZ.PhotoUpload.exe";
- p.Start();
-
- }
- }
- #endregion
- }
- public class CommandData : UpFTPServiceFileShop
- {
- /// <summary>
- /// A :数据上传
- /// P: 查询进度
- /// C: 重复检查
- /// </summary>
- public string CommandType { get; set; }
- /// <summary>
- /// 是否上传原图
- /// </summary>
- public bool UpArtwork { get; set; }
- /// <summary>
- /// 文件夹类型
- /// </summary>
- public string dirTypeData { get; set; }
- /// <summary>
- /// 如果存在
- /// </summary>
- public bool isExist { get; set; }
- }
- }
|