锐腾搅拌上料功能
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
8.3 KiB

5 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ICSSoft.Frame.Data.Entity;
  6. using System.ComponentModel;
  7. using System.IO;
  8. using Newtonsoft.Json.Linq;
  9. namespace ICSSoft.Frame.Common
  10. {
  11. public class File4Kod
  12. {
  13. /// <summary>
  14. /// 下载,保存到项目根目录,路径/文件名与可道云一致,同时将信息保存到按钮tag中.
  15. /// 非长连接,每次都执行:登录-下载-登出.
  16. /// </summary>
  17. /// <param name="ITEMCODE">物料编码,用于文件夹规则匹配</param>
  18. /// <param name="custITEMCODE">客户料号,用于文件夹规则匹配</param>
  19. /// <param name="KodSetting">可道云维护的基本信息</param>
  20. /// <param name="tag">按钮tag,保存图纸类型,本地路径,远端路径</param>
  21. /// <param name="OPSEQ">工序次序,工序图纸和刀具图纸文件名匹配规则时使用</param>
  22. /// <param name="test">是否是测试,测试阶段取固定'客户料号'下的图纸</param>
  23. public static void DownLoad(string ITEMCODE, string custITEMCODE, KODFolder KodSetting, BtnTagPDF tag, int OPSEQ, bool test)
  24. {
  25. KodboxHelper kodhelper = new KodboxHelper(KodSetting.URL);
  26. string filename = "";
  27. string DrawingFolder = "";
  28. string fullFilename = "";
  29. //路径:
  30. //URL/Dispalypath/物料1~3/物料4~6_物料1~3/客户料号/图纸文件夹/图纸.pdf
  31. string SubDic1 = ITEMCODE.Substring(0, 3) + "/" + ITEMCODE.Substring(3, 3) + "_" + ITEMCODE.Substring(0, 3);// +"/" + custITEMCODE;
  32. string SubDic2 = custITEMCODE;
  33. string SubDic3 = "";
  34. switch (tag.type)
  35. {
  36. case DrawingType.General:
  37. DrawingFolder = KodSetting.General;
  38. break;
  39. case DrawingType.OP:
  40. DrawingFolder = KodSetting.OP;
  41. break;
  42. case DrawingType.Cutters:
  43. DrawingFolder = KodSetting.Cutters;
  44. break;
  45. case DrawingType.SOP:
  46. DrawingFolder = KodSetting.SOP;
  47. break;
  48. case DrawingType.SIP:
  49. DrawingFolder = KodSetting.SIP;
  50. break;
  51. default:
  52. break;
  53. }
  54. SubDic2 = Path.Combine(SubDic2, DrawingFolder);
  55. if (test)
  56. {
  57. KodSetting.PATH = @"{source:91040000922230}/";
  58. SubDic1 = @"B58/407_B58/";
  59. SubDic2 = "3AAM1087001";
  60. SubDic3 = DrawingFolder;
  61. }
  62. try
  63. {
  64. //1.登录
  65. string msgKod = kodhelper.Login(KodSetting.USER, KodSetting.PWD);
  66. JObject o = (JObject)JToken.Parse(msgKod);
  67. msgKod = o["data"].ToString();
  68. if (msgKod != "ok")
  69. {
  70. throw new Exception(msgKod);
  71. }
  72. try
  73. {
  74. string searchResJson = "";
  75. //2.获取路径
  76. string sourse = "";
  77. string dispalypath = "";
  78. string savepath = "";
  79. string filenameMatch = OPSEQ.ToString().PadLeft(3, '0');
  80. List<string> listRes = kodhelper.GetSourcePath(KodSetting.PATH, "pdf,xls,xlsx", SubDic1, SubDic2, SubDic3, out searchResJson);
  81. foreach (string res in listRes)
  82. {
  83. bool m = false;
  84. sourse = res.Split('|')[0];
  85. filename = res.Split('|')[1];
  86. dispalypath = res.Split('|')[2];
  87. switch (tag.type)
  88. {
  89. case DrawingType.OP:
  90. if (filename.Substring(0, filename.LastIndexOf('.')).EndsWith(filenameMatch))
  91. {
  92. m = true;
  93. }
  94. break;
  95. case DrawingType.Cutters:
  96. if (filename.StartsWith(filenameMatch))
  97. {
  98. m = true;
  99. }
  100. break;
  101. default:
  102. break;
  103. }
  104. if (m)
  105. {
  106. break;
  107. }
  108. }
  109. savepath = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + dispalypath;
  110. if (!Directory.Exists(savepath))
  111. {
  112. Directory.CreateDirectory(savepath);
  113. }
  114. fullFilename = Path.Combine(savepath, filename);
  115. if (string.IsNullOrEmpty(tag.local) || tag.local.ToString() != fullFilename || !File.Exists(fullFilename))
  116. {
  117. //3.下载
  118. string downres = kodhelper.DownFile(sourse, savepath, filename);
  119. if (downres != "OK")
  120. {
  121. throw new Exception(downres);
  122. }
  123. }
  124. tag.remote = sourse;
  125. tag.local = fullFilename;
  126. }
  127. finally
  128. {
  129. //4.登出
  130. msgKod = kodhelper.Logout(false);
  131. }
  132. }
  133. finally
  134. {
  135. //5.释放
  136. kodhelper.Dispose();
  137. }
  138. }
  139. public static void DownLoad(KODFolder KodSetting, string sourse, string savePath, string saveName)
  140. {
  141. KodboxHelper kodhelper = new KodboxHelper(KodSetting.URL);
  142. try
  143. {
  144. //登录
  145. string msgKod = kodhelper.Login(KodSetting.USER, KodSetting.PWD);
  146. JObject o = (JObject)JToken.Parse(msgKod);
  147. msgKod = o["data"].ToString();
  148. if (msgKod != "ok")
  149. {
  150. throw new Exception(msgKod);
  151. }
  152. try
  153. {
  154. string fullfilepath = "";
  155. if (!Directory.Exists(savePath))
  156. {
  157. Directory.CreateDirectory(savePath);
  158. }
  159. fullfilepath = Path.Combine(savePath, saveName);
  160. //下载
  161. string downres = kodhelper.DownFile(sourse, savePath, saveName);
  162. if (downres != "OK")
  163. {
  164. throw new Exception(downres);
  165. }
  166. }
  167. finally
  168. {
  169. //登出
  170. msgKod = kodhelper.Logout(false);
  171. }
  172. }
  173. finally
  174. {
  175. //释放
  176. kodhelper.Dispose();
  177. }
  178. }
  179. public class BtnTagPDF
  180. {
  181. public DrawingType type { get; set; }
  182. public string remote { get; set; }
  183. public string local { get; set; }
  184. }
  185. public enum DrawingType
  186. {
  187. [Description("总图")]
  188. [DescriptionEn("General Drawing")]
  189. General,
  190. [Description("工序图")]
  191. [DescriptionEn("Process Diagram")]
  192. OP,
  193. [Description("刀具单")]
  194. [DescriptionEn("Cutters")]
  195. Cutters,
  196. [Description("作业指导书")]
  197. [DescriptionEn("Standard Operation Procedure")]
  198. SOP,
  199. [Description("检验指导书")]
  200. [DescriptionEn("Standard Inspection Procedure")]
  201. SIP
  202. }
  203. private class DescriptionEn : Attribute
  204. {
  205. private string _showName;
  206. public DescriptionEn(string desc)
  207. {
  208. _showName = desc;
  209. }
  210. public string Description
  211. {
  212. get
  213. {
  214. return _showName;
  215. }
  216. }
  217. }
  218. }
  219. }