FTPDataCommand.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. using LYFZ.BLL;
  2. using LYFZ.Model;
  3. using LYFZ.Network.FileOperations;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Runtime.InteropServices;
  11. using System.Runtime.Serialization.Json;
  12. using System.Text;
  13. using System.Windows.Forms;
  14. namespace LYFZ.Network.FTPOperation
  15. {
  16. /// <summary>
  17. /// 客户端发送来的ftp需要发送的信息
  18. /// </summary>
  19. public class FTPDataCommand
  20. {
  21. BLL.BaseBLL.BaseBLL_FTPServiceUserAccount bll = new BLL.BaseBLL.BaseBLL_FTPServiceUserAccount();
  22. /// <summary>
  23. /// ftp控制的操作指令
  24. /// </summary>
  25. /// <param name="tcpData"></param>
  26. /// <returns></returns>
  27. public string CommandFtpData(string tcpData)
  28. {
  29. CheckExeOpen();
  30. string resultData="";
  31. CommandData comData=new CommandData();
  32. comData = (CommandData)JsonToObject(tcpData,comData);
  33. switch(comData.CommandType)
  34. {
  35. case "A":
  36. AddJsonDataToFtpPools(tcpData, comData.CommandType, comData.UpArtwork, comData.dirTypeData);
  37. break;
  38. case "P":
  39. string filePath = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath)
  40. + "\\FTPClient\\UpFileManage\\Action.txt";
  41. if (File.Exists(filePath))
  42. {
  43. resultData = File.ReadAllText(filePath, Encoding.UTF8);
  44. }
  45. else
  46. {
  47. resultData = "[]";
  48. }
  49. break;
  50. case "C":
  51. string filePath1 = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\FTPClient\\UpFileManage\\Action.txt";
  52. string txtFileInfo = "";
  53. if (File.Exists(filePath1))
  54. {
  55. txtFileInfo = File.ReadAllText(filePath1, Encoding.UTF8);
  56. }
  57. List<UpFTPServiceFileShop> shopData = new List<UpFTPServiceFileShop>();
  58. shopData = (List<UpFTPServiceFileShop>)Json.JsonTool.JsonToObject(txtFileInfo, shopData);
  59. int totalCount = 0;
  60. foreach (var shop in shopData)
  61. {
  62. totalCount += shop.items.FindAll(p=>!p.bSuccess&&p.bOpenStatus>-1).Count;
  63. }
  64. resultData = totalCount.ToString();
  65. break;
  66. case "S":
  67. File.WriteAllText(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\FTPClient\\UpFileManage\\" + comData.bUpfileCode + "_Command.txt", comData.dirTypeData);
  68. SendCommandToExe(comData.CommandType);
  69. break;
  70. case "O":
  71. resultData = CommandComplete();
  72. File.WriteAllText(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\FTPClient\\UpFileManage\\所有_Command.txt", resultData);
  73. break;
  74. }
  75. return resultData;
  76. }
  77. string CommandComplete()
  78. {
  79. DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(Application.ExecutablePath) + "\\FTPClient\\UpFileManage\\Complete");
  80. DirectoryInfo[] arrDir = di.GetDirectories();
  81. SortAsFolderName(ref arrDir);
  82. List<UpFTPServiceFileShop> shopData = new List<UpFTPServiceFileShop>();
  83. foreach (var mod in arrDir)
  84. {
  85. string fileText = File.ReadAllText(mod.FullName + "\\Complete.txt");
  86. shopData.AddRange(Newtonsoft.Json.JsonConvert.DeserializeObject<List<UpFTPServiceFileShop>>(fileText));
  87. }
  88. List<dynamic> dyObjList = new List<dynamic>();
  89. for (int i = 0; i < shopData.Count; i++)
  90. {
  91. dynamic dyObj = new System.Dynamic.ExpandoObject();
  92. dyObj.localShopInfo = shopData[i].localShopInfo;
  93. List<dynamic> dataItemList = new List<dynamic>();
  94. foreach (var mod in shopData[i].items)
  95. {
  96. dynamic dyItem = new System.Dynamic.ExpandoObject();
  97. dyItem.Text = "" + mod.OrderNumber + "(" + mod.DirFileType + "上传到——" + shopData[i].upShopText + "(" + shopData[i].hostPath + ":" + shopData[i].ftpName + ")" + ")";
  98. dyItem.StartTime = mod.StartTime;
  99. dyItem.EndTime = mod.EndTime;
  100. dataItemList.Add(dyItem);
  101. //ListViewItem item = new ListViewItem(new string[] {"" + mod.OrderNumber + "(" + mod.DirFileType + "上传到——" +
  102. //list[i].upShopText + "(" + list[i].hostPath + ":" + list[i].ftpName + ")" + ")",mod.StartTime,mod.EndTime});
  103. //item.Group = group;
  104. //this.listViewCompletedTask.Items.Add(item);
  105. }
  106. dyObj.Item = dataItemList;
  107. dyObjList.Add(dyObj);
  108. }
  109. return Newtonsoft.Json.JsonConvert.SerializeObject(dyObjList);
  110. }
  111. /// <summary>
  112. /// 文件夹排序
  113. /// </summary>
  114. /// <param name="dirs"></param>
  115. private void SortAsFolderName(ref DirectoryInfo[] dirs)
  116. {
  117. Array.Sort(dirs, delegate(DirectoryInfo x, DirectoryInfo y) { return x.Name.CompareTo(y.Name); });
  118. }
  119. /// <summary>
  120. /// 文件添加入新增池
  121. /// </summary>
  122. /// <param name="jsonData"></param>
  123. void AddJsonDataToFtpPools(string jsonData, string comType, bool UpArtwork, string dirTypeData)
  124. {
  125. //BLL_ErpOrder erpOrderBll= new BLL_ErpOrder();
  126. //BLL_ErpOrderDigital digtalBll = new BLL_ErpOrderDigital();
  127. UpFTPServiceFileShop shop = new UpFTPServiceFileShop();
  128. //DAL.DAL_Config dalConfig = new DAL.DAL_Config();
  129. //Model_ServerConfig modConfig = dalConfig.GetModelServerSysConfig();
  130. shop = (UpFTPServiceFileShop)JsonToObject(jsonData, shop);
  131. DataSet ds = bll.GetAllList();
  132. //SelectPhotoHandling handling ;
  133. /* LYFZ.Model.Model_ServerConfig.PhotoDirectoryCategory PrimitivePicture = Model.Model_ServerConfig.PhotoDirectoryCategory.原片保存目录;
  134. LYFZ.Model.Model_ServerConfig.PhotoDirectoryCategory EarlyRetouch = Model.Model_ServerConfig.PhotoDirectoryCategory.初修片保存目录;
  135. LYFZ.Model.Model_ServerConfig.PhotoDirectoryCategory FineRetouch = Model.Model_ServerConfig.PhotoDirectoryCategory.精修片保存目录;
  136. LYFZ.Model.Model_ServerConfig.PhotoDirectoryCategory DesignPiece = Model.Model_ServerConfig.PhotoDirectoryCategory.设计片保存目录;
  137. string tempPath =PrimitivePicture.ToString() + ">" + modConfig.GetPhotoDirectoryList(PrimitivePicture).ToString()
  138. + "*" + EarlyRetouch.ToString() + ">" + modConfig.GetPhotoDirectoryList(EarlyRetouch).ToString()
  139. + "*" + FineRetouch.ToString() + ">" + modConfig.GetPhotoDirectoryList(FineRetouch).ToString()
  140. + "*" + DesignPiece.ToString() + ">" + modConfig.GetPhotoDirectoryList(DesignPiece).ToString();
  141. if (LYFZ.BLL.RedPhotoFilePathList.CurrentRedPhotoFilePathList == null)
  142. {
  143. LYFZ.BLL.RedPhotoFilePathList.CurrentRedPhotoFilePathList = new BLL.RedPhotoFilePathList(tempPath);
  144. }*/
  145. foreach(DataRow dr in ds.Tables[0].Rows)
  146. {
  147. if(dr["Ftp_Dividedshop"].ToString().Equals(shop.upShopCode))
  148. {
  149. // int port = 0;
  150. UpFTPServiceFileGroup group = shop.items[0];
  151. //group.physicsPath = GetServicePhysicsPath(group.DirFileType, modConfig);
  152. //Model_ErpOrder erpOrder = erpOrderBll.GetModel(group.OrderNumber);
  153. //Model_ErpOrderDigital digtalOrder = digtalBll.GetModel(group.ViceNumber);
  154. //string strOrdType = erpOrder.Ord_Type;
  155. //erpOrder.Ord_Type = "0";
  156. //handling = new SelectPhotoHandling(erpOrder, digtalOrder);
  157. //erpOrder.Ord_Type = strOrdType;
  158. //CheckDirectoryOrFile(handling,group, UpArtwork, dirTypeData);
  159. FileOperation.WriteFile(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath)
  160. + "/FTPClient/UpFileManage/NewJson/" + group.OrderNumber + ".txt", ObjectToJson(shop));
  161. SendCommandToExe(comType);
  162. break;
  163. }
  164. }
  165. }
  166. /// <summary>
  167. /// 检查文件夹和文件
  168. /// </summary>
  169. void CheckDirectoryOrFile( SelectPhotoHandling handling,UpFTPServiceFileGroup group, bool UpArtwork, string dirTypeData)
  170. {
  171. List<string> Original_List=new List<string>();
  172. List<string> Preview_List = new List<string>();
  173. List<string> Small_List = new List<string>();
  174. if (UpArtwork)
  175. {
  176. Original_List = handling.GetSpecifyTypePhotoList(GetDirectoryType(group.DirFileType), SelectPhotoHandling.PhotoType.Original);
  177. }
  178. Preview_List = handling.GetSpecifyTypePhotoList(GetDirectoryType(group.DirFileType), SelectPhotoHandling.PhotoType.Preview);
  179. Small_List = handling.GetSpecifyTypePhotoList(GetDirectoryType(group.DirFileType), SelectPhotoHandling.PhotoType.Small);
  180. if (group.DirFileType.Equals("原片"))
  181. {
  182. ///
  183. string[] dirList = dirTypeData.Split(',');
  184. foreach (string dirData in dirList)
  185. {
  186. BindImageData(group, Original_List.Where(p => p.IndexOf("\\" + dirData + "\\") != -1).ToList());
  187. BindImageData(group, Preview_List.Where(p => p.IndexOf("\\" + dirData + "\\") != -1).ToList());
  188. BindImageData(group, Small_List.Where(p => p.IndexOf("\\" + dirData + "\\") != -1).ToList());
  189. }
  190. }
  191. else
  192. {
  193. if (group.PhotoType.Equals("儿童订单"))
  194. {
  195. string[] dirList = dirTypeData.Split(',');
  196. foreach (string dirData in dirList)
  197. {
  198. BindImageData(group, Original_List.Where(p => p.IndexOf("\\" + dirData + "\\") != -1).ToList());
  199. BindImageData(group, Preview_List.Where(p => p.IndexOf("\\" + dirData + "\\") != -1).ToList());
  200. BindImageData(group, Small_List.Where(p => p.IndexOf("\\" + dirData + "\\") != -1).ToList());
  201. }
  202. }
  203. else
  204. {
  205. BindImageData(group, Original_List);
  206. BindImageData(group, Preview_List);
  207. BindImageData(group, Small_List);
  208. }
  209. }
  210. /*string[] dirList = dirTypeData.Split(',');
  211. if(!string.IsNullOrEmpty(group.localServicePath))
  212. {
  213. string directoryData = GetServicePhysicsDir(group.DirFileType);
  214. foreach (string dirData in dirList)
  215. {
  216. string localServicePath = "\\" + directoryData + "\\" + group.OrderNumber + "\\" + dirData + "\\";
  217. group.localServicePath = localServicePath;
  218. string dirPath = group.physicsPath + directoryData + "\\" + group.OrderNumber + "\\" + dirData + "\\";
  219. if (Directory.Exists(dirPath))
  220. {
  221. BindImagePreviewOrSmallData(group, "Preview", dirPath, localServicePath);
  222. BindImagePreviewOrSmallData(group, "Small", dirPath, localServicePath);
  223. if(UpArtwork)
  224. {
  225. BindImageOriginalData(group, dirPath, localServicePath);
  226. }
  227. }
  228. }
  229. }*/
  230. }
  231. void BindImageData(UpFTPServiceFileGroup group, List<string> filePathList)
  232. {
  233. foreach(string path in filePathList)
  234. {
  235. group.items.Add(new UpFTPServiceFileItem()
  236. {
  237. localFilePath = path,
  238. serviceFilePath = path.Split('$').Length>0? path.Split('$')[1]:"",
  239. });
  240. }
  241. /* if (Directory.Exists(dirPath + "" ))
  242. {
  243. string[] fileList = Directory.GetFiles(dirPath );
  244. foreach (string file in fileList)
  245. {
  246. string fileName = Path.GetFileName(file);
  247. group.items.Add(new UpFTPServiceFileItem()
  248. {
  249. localFilePath = file,
  250. serviceFilePath = servicePath + fileName,
  251. });
  252. }
  253. }*/
  254. }
  255. /// <summary>
  256. /// 获取不同图片在服务器上的物理路径
  257. /// </summary>
  258. /// <param name="type">类型</param>
  259. /// <param name="modConfig">服务配置实体</param>
  260. /// <returns></returns>
  261. public string GetServicePhysicsPath(string type, Model_ServerConfig modConfig )
  262. {
  263. switch(type)
  264. {
  265. case "原片":
  266. return ConvertPath(modConfig.PrimitivePicturePath);
  267. case "初修":
  268. return ConvertPath(modConfig.EarlyRetouchPath);
  269. case "精修":
  270. return ConvertPath(modConfig.FineRetouchPath);
  271. case "设计":
  272. return ConvertPath(modConfig.DesignPiecePath);
  273. }
  274. return "";
  275. }
  276. /// <summary>
  277. /// 打印文件夹类型
  278. /// </summary>
  279. /// <param name="type"></param>
  280. /// <returns></returns>
  281. public LYFZ.Model.Model_ErpOrder.DirectoryType GetDirectoryType(string type)
  282. {
  283. switch(type)
  284. {
  285. case "原片":
  286. return LYFZ.Model.Model_ErpOrder.DirectoryType.PrimitivePicture;
  287. case "初修":
  288. return LYFZ.Model.Model_ErpOrder.DirectoryType.EarlyRetouch;
  289. case "精修":
  290. return LYFZ.Model.Model_ErpOrder.DirectoryType.FineRetouch;
  291. default:
  292. return LYFZ.Model.Model_ErpOrder.DirectoryType.DesignPiece;
  293. }
  294. }
  295. /// <summary>
  296. /// 获取不同图片在服务器上的物理路径
  297. /// </summary>
  298. /// <param name="type">类型</param>
  299. /// <param name="modConfig">服务配置实体</param>
  300. /// <returns></returns>
  301. public string GetServicePhysicsDir(string type)
  302. {
  303. switch (type)
  304. {
  305. case "原片":
  306. return "客户原片(管理软件)";
  307. case "初修":
  308. return "初修片(管理软件)";
  309. case "精修":
  310. return "精修片(管理软件)";
  311. case "设计":
  312. return "设计片(管理软件)";
  313. }
  314. return "";
  315. }
  316. public string ConvertPath(string path)
  317. {
  318. string[] pathList = path.Split('|');
  319. foreach(string tpath in pathList)
  320. {
  321. if(!string.IsNullOrEmpty(tpath))
  322. {
  323. string[] dataColum = tpath.Split('?');
  324. if(Convert.ToBoolean(dataColum[2]))
  325. {
  326. return dataColum[0];
  327. }
  328. }
  329. }
  330. return "";
  331. }
  332. #region Json对象实例化方式
  333. /// <summary>
  334. /// 实体转json
  335. /// </summary>
  336. /// <param name="obj"></param>
  337. /// <returns></returns>
  338. public static string ObjectToJson(object obj)
  339. {
  340. DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
  341. MemoryStream stream = new MemoryStream();
  342. serializer.WriteObject(stream, obj);
  343. byte[] dataBytes = new byte[stream.Length];
  344. stream.Position = 0;
  345. stream.Read(dataBytes, 0, (int)stream.Length);
  346. return Encoding.UTF8.GetString(dataBytes);
  347. }
  348. // 从一个Json串生成对象信息
  349. public static object JsonToObject(string jsonString, object obj)
  350. {
  351. DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
  352. MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
  353. return serializer.ReadObject(mStream);
  354. }
  355. public static object JsonToObjectByte(byte[] byteData, object obj)
  356. {
  357. DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
  358. MemoryStream mStream = new MemoryStream(byteData);
  359. return serializer.ReadObject(mStream);
  360. }
  361. #endregion
  362. #region 通过程序指针获取对象并传输指令
  363. [DllImport("user32.dll")]
  364. public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  365. [DllImport("user32.dll")]
  366. public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
  367. [DllImport("user32.dll", CharSet = CharSet.Unicode)]
  368. public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
  369. public void SendCommandToExe(string code)
  370. {
  371. IntPtr fWnd = FindWindow(null, "远程相片上传工具");
  372. IntPtr txtbox = FindWindowEx(fWnd, new IntPtr(0), null, "");
  373. // File.WriteAllText("D://txt2.txt", txtbox.ToString());
  374. const int WM_CHAR = 0x0102;
  375. string strtext = code;
  376. UnicodeEncoding encode = new UnicodeEncoding();
  377. char[] chars = encode.GetChars(encode.GetBytes(strtext));
  378. Message msg;
  379. foreach (char c in chars)
  380. {
  381. msg = Message.Create(txtbox, WM_CHAR, new IntPtr(c), new IntPtr(0));
  382. PostMessage(msg.HWnd, msg.Msg, msg.WParam, msg.LParam);
  383. }
  384. }
  385. public void CheckExeOpen()
  386. {
  387. if (System.Diagnostics.Process.GetProcessesByName("LYFZ.PhotoUpload").ToList().Count == 0)
  388. {
  389. //File.WriteAllText("D://txt1.txt", Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "/FTPClient/LYFZ.PhotoUpload.exe");
  390. Process p = new Process();
  391. p.StartInfo.FileName = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "/FTPClient/"+"LYFZ.PhotoUpload.exe";
  392. p.Start();
  393. }
  394. }
  395. #endregion
  396. }
  397. public class CommandData : UpFTPServiceFileShop
  398. {
  399. /// <summary>
  400. /// A :数据上传
  401. /// P: 查询进度
  402. /// C: 重复检查
  403. /// </summary>
  404. public string CommandType { get; set; }
  405. /// <summary>
  406. /// 是否上传原图
  407. /// </summary>
  408. public bool UpArtwork { get; set; }
  409. /// <summary>
  410. /// 文件夹类型
  411. /// </summary>
  412. public string dirTypeData { get; set; }
  413. /// <summary>
  414. /// 如果存在
  415. /// </summary>
  416. public bool isExist { get; set; }
  417. }
  418. }