MacProgramDal.cs 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  1. using Cksoft.Data;
  2. using Cksoft.Data.Repository;
  3. using Cksoft.Unity;
  4. using DllEapEntity;
  5. using DllEapEntity.Dtos;
  6. using DllEapEntity.Rms;
  7. using DllHsms;
  8. using DllHsmsWeb;
  9. using Microsoft.Extensions.Configuration;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. namespace DllEapDal
  15. {
  16. public class MacProgramDal
  17. {
  18. private string programBaseDir;
  19. private IDatabase CurrDb;
  20. private string commonFilter = null;
  21. public MacProgramDal(IDatabase db)
  22. {
  23. CurrDb = db;
  24. this.programBaseDir = AppConfigurtaionServices.Configuration.GetValue<string>("ProgramDir");
  25. }
  26. public MacProgramDal(IDatabase db, string userCode)
  27. {
  28. CurrDb = db;
  29. this.programBaseDir = AppConfigurtaionServices.Configuration.GetValue<string>("ProgramDir");
  30. //var smDal = new StaffMachineDal(CurrDb);
  31. //string errorinfo = string.Empty;
  32. //var idFilter = smDal.GetFilter(userCode, ref errorinfo);
  33. // commonFilter = // idFilter.Replace("a.id", "b.id");
  34. }
  35. public MacProgram UpProgram(OrderBlock sendentity, string maccode, ref string errorinfo)
  36. {
  37. try
  38. {
  39. string condition = $" and a.fcode='{maccode}'";
  40. List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  41. if (macs.Count <= 0)
  42. {
  43. errorinfo = $"未找到机台编号{maccode}的信息。";
  44. return null;
  45. }
  46. List<OrderData> datas = sendentity.Datalists.Where(t => t.ParentID == 0).OrderBy(t => t.FNum).ToList();
  47. int parentid = datas[0].ID;//获取L的ID值
  48. datas = sendentity.Datalists.Where(t => t.ParentID == parentid).OrderBy(t => t.FNum).ToList();
  49. string programname = datas[0].FContent.Trim();
  50. byte[] filedatas = datas[1].OrgDatas;
  51. if (macs[0].MModeCode.ToLower() == "dad3350" || macs[0].MModeCode.ToLower() == "dfd6341")
  52. {
  53. //从文件中分析文件描述
  54. byte[] tempbytes = new byte[200];
  55. if (filedatas.Length < 200)
  56. {
  57. tempbytes = filedatas;
  58. }
  59. else
  60. {
  61. Array.Copy(filedatas, 0, tempbytes, 0, 200);
  62. }
  63. //string orgname = DiodesProgramFileHelper.GetProgramName(tempbytes, ref errorinfo);
  64. programname = DiodesProgramFileHelper.GetProgramName(tempbytes, ref errorinfo);
  65. if (!string.IsNullOrEmpty(errorinfo))
  66. {
  67. return null;
  68. }
  69. }
  70. if (macs[0].MModeCode.ToLower() == "bg")
  71. {
  72. //从文件中分析文件描述
  73. byte[] tempbytes = new byte[200];
  74. if (filedatas.Length < 200)
  75. {
  76. tempbytes = filedatas;
  77. }
  78. else
  79. {
  80. Array.Copy(filedatas, 0, tempbytes, 0, 200);
  81. }
  82. //string orgname = DiodesProgramFileHelper.GetProgramName(tempbytes, ref errorinfo);
  83. programname = Bg8761ProgramFileHelper.GetProgramName(tempbytes, ref errorinfo);
  84. if (!string.IsNullOrEmpty(errorinfo))
  85. {
  86. return null;
  87. }
  88. }
  89. //获取程序文件存放路径
  90. string filedir = AppConfigurtaionServices.Configuration["ProgramDir"];
  91. MacProgram macprogram = IMacProgram(macs[0], programname, filedir, filedatas, ref errorinfo);
  92. if (macprogram == null)
  93. {
  94. return null;
  95. }
  96. string macmodelfiledir = AppConfigurtaionServices.Configuration["MacModelProgramDir"];
  97. //添加机型程序
  98. MacModelProgram macmodel = IMacModelProgram(macs[0], programname, macmodelfiledir, filedatas, ref errorinfo);
  99. if (macmodel == null)
  100. {
  101. return null;
  102. }
  103. datas[1].OrgDatas = null;//清空文件数据
  104. datas[1].FContent = macprogram.ID.ToString();//保存文件存放路径
  105. return macprogram;
  106. }
  107. catch (Exception ex)
  108. {
  109. errorinfo = ex.Message.ToString();
  110. return null;
  111. }
  112. }
  113. public int ClearProgram(string maccode, ref string errorinfo)
  114. {
  115. try
  116. {
  117. string condition = $" and a.fcode='{maccode}'";
  118. List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  119. if (macs.Count <= 0)
  120. {
  121. errorinfo = $"未找到机台编号{maccode}的信息。";
  122. return -1;
  123. }
  124. //if (macs[0].MModeCode.ToLower() != "dad3350")
  125. //{
  126. // return 1;
  127. //}
  128. int result = ClearProgram(macs[0], ref errorinfo);
  129. return result;
  130. }
  131. catch (Exception ex)
  132. {
  133. errorinfo = ex.Message.ToString();
  134. return -1;
  135. }
  136. }
  137. public int ClearProgram(Machine mac, ref string errorinfo)
  138. {
  139. try
  140. {
  141. MacOrderSendDal dal = new MacOrderSendDal(CurrDb);
  142. OrderDetail order = dal.ReadMachineOrderDetail(mac.ID, 7, 17, "S7F17ClearProgram", ref errorinfo);
  143. if (order == null)
  144. return -1;
  145. string condition = $" and a.preid={order.ID}";
  146. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  147. if (datas == null)
  148. return -1;
  149. HsmsWeb accessmac = new HsmsWeb();
  150. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  151. if (rec == null)
  152. return -1;
  153. if (int.Parse(rec.Datalists[0].FContent) != 0)
  154. {
  155. errorinfo = $"delete program has occ:{rec.Datalists[1].FContent}。";
  156. return -1;
  157. }
  158. return 1;
  159. }
  160. catch (Exception ex)
  161. {
  162. errorinfo = ex.Message.ToString();
  163. return -1;
  164. }
  165. }
  166. public byte[] ModifyProgramName(byte[] datas, string maccode, string programname, ref string errorinfo)
  167. {
  168. try
  169. {
  170. string condition = $" and a.fcode='{maccode}'";
  171. List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  172. if (macs.Count <= 0)
  173. {
  174. errorinfo = $"未找到机台编号{maccode}的信息。";
  175. return null;
  176. }
  177. if (macs[0].MModeCode.ToLower() != "dad3350")
  178. {
  179. return datas;
  180. }
  181. byte[] filedatas = DiodesProgramFileHelper.ReplaceProgramName(datas, programname, ref errorinfo);
  182. if (filedatas == null)
  183. {
  184. return null;
  185. }
  186. return filedatas;
  187. }
  188. catch (Exception ex)
  189. {
  190. errorinfo = ex.Message.ToString();
  191. return null;
  192. }
  193. }
  194. public MacProgram IMacProgram(Machine mac, string programname, string mstdir, byte[] filedatas, ref string errorinfo)
  195. {
  196. try
  197. {
  198. //主目录+机台编号目录+程序名称目录
  199. string filedir = $"{mstdir}\\{mac.FCode}\\{programname}";
  200. if (!Directory.Exists(@filedir))
  201. {
  202. Directory.CreateDirectory(@filedir);
  203. }
  204. //string condition = $" and a.fcode='{maccode}'";
  205. //List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  206. //if (macs.Count <= 0)
  207. //{
  208. // errorinfo = $"未找到机台编号{maccode}的信息。";
  209. // return null;
  210. //}
  211. MacProgram entity = new MacProgram();
  212. entity.MacID = mac.ID;
  213. entity.ProgramName = programname;
  214. entity.IsDefault = -1;
  215. string condition = $" and a.MacID={mac.ID} and a.ProgramName='{programname}'";
  216. List<MacProgram> macprograms = CurrDb.FindListForCondition<MacProgram>(condition, ref errorinfo).ToList();
  217. if (macprograms.Count <= 0)
  218. {
  219. entity.Version = 1;
  220. }
  221. else
  222. {
  223. entity.Version = macprograms.Max(t => t.Version) + 1;
  224. }
  225. entity.FileOrgName = $"{programname}_{entity.Version}";
  226. CurrDb.InsertFor<MacProgram>(entity, "");
  227. //读取记录
  228. object objid = CurrDb.FindObject("select @@IDENTITY");
  229. if (objid.ToString() == "")
  230. {
  231. return null;
  232. }
  233. int id = int.Parse(objid.ToString());
  234. //检查programmst里是否存在此程序,如果不存在则添加
  235. ProgramMstDal pmdal = new ProgramMstDal(CurrDb);
  236. int tempid = pmdal.IUProgramMst(mac, programname, ref errorinfo);
  237. if (tempid <= 0)
  238. return null;
  239. //写入文件
  240. int result = UnityHelper.WriteFile(filedir, programname + $"_{entity.Version}", filedatas, ref errorinfo);
  241. if (result <= 0)
  242. return null;
  243. entity = CurrDb.FindEntityFor<MacProgram>(id);
  244. return entity;
  245. }
  246. catch (Exception ex)
  247. {
  248. errorinfo = ex.Message.ToString();
  249. return null;
  250. }
  251. }
  252. public MacModelProgram IMacModelProgram(Machine mac, string programname, string mstdir, byte[] filedatas, ref string errorinfo)
  253. {
  254. try
  255. {
  256. //string condition = $" and a.fcode='{maccode}'";
  257. //List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  258. //if (macs.Count <= 0)
  259. //{
  260. // errorinfo = $"未找到机台编号{maccode}的信息。";
  261. // return null;
  262. //}
  263. //读取机型
  264. string condition = $" and a.id={mac.MModeID}";
  265. List<MacModel> macmodel = CurrDb.FindListForCondition<MacModel>(condition, ref errorinfo).ToList();
  266. if (macmodel.Count <= 0)
  267. {
  268. errorinfo = $"未找到机台编号{mac.FCode}对应的机型信息。";
  269. return null;
  270. }
  271. //主目录+机台编号目录+程序名称目录
  272. string filedir = $"{mstdir}\\{macmodel[0].FCode}\\{programname}";
  273. if (!Directory.Exists(@filedir))
  274. {
  275. Directory.CreateDirectory(@filedir);
  276. }
  277. MacModelProgram entity = new MacModelProgram();
  278. entity.MacID = mac.ID;
  279. entity.ProgramName = programname;
  280. entity.IsDefault = -1;
  281. entity.MacModelID = macmodel[0].ID;
  282. condition = $" and a.MacModelID={macmodel[0].ID} and a.ProgramName='{programname}'";
  283. List<MacModelProgram> macprograms = CurrDb.FindListForCondition<MacModelProgram>(condition, ref errorinfo).ToList();
  284. if (macprograms.Count <= 0)
  285. {
  286. entity.Version = 1;
  287. }
  288. else
  289. {
  290. entity.Version = macprograms.Max(t => t.Version) + 1;
  291. }
  292. entity.FileOrgName = $"{programname}_{entity.Version}";
  293. CurrDb.InsertFor<MacModelProgram>(entity, "");
  294. //写入文件
  295. int result = UnityHelper.WriteFile(filedir, programname + $"_{entity.Version}", filedatas, ref errorinfo);
  296. if (result <= 0)
  297. return null;
  298. //读取记录
  299. object objid = CurrDb.FindObject("select @@IDENTITY");
  300. if (objid.ToString() == "")
  301. {
  302. return null;
  303. }
  304. int id = int.Parse(objid.ToString());
  305. entity = CurrDb.FindEntityFor<MacModelProgram>(id);
  306. return entity;
  307. }
  308. catch (Exception ex)
  309. {
  310. errorinfo = ex.Message.ToString();
  311. return null;
  312. }
  313. }
  314. //public byte[] ReadMacProgram(string maccode, string programname, string mstdir, ref string errorinfo)
  315. //{
  316. // try
  317. // {
  318. // string condition = $" and a.fcode='{maccode}'";
  319. // List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  320. // if (macs.Count <= 0)
  321. // {
  322. // errorinfo = $"未找到机台编号{maccode}的信息。";
  323. // return null;
  324. // }
  325. // condition = $" and a.MacID={macs[0].ID} and a.ProgramName='{programname}' and a.IsDefault=1";
  326. // List<MacProgram> macprograms = CurrDb.FindListForCondition<MacProgram>(condition, ref errorinfo).ToList();
  327. // if (macprograms.Count <= 0)
  328. // {
  329. // errorinfo = $"未找到机台【{maccode}】,程序名称【{programname}】,对应的标准程序。";
  330. // return null;
  331. // }
  332. // if (macprograms.Count > 1)
  333. // {
  334. // errorinfo = $"机台【{maccode}】,程序名称【{programname}】,对应了多个标准程序。";
  335. // return null;
  336. // }
  337. // string filepath = $"{mstdir}\\{maccode}\\{programname}\\{programname}_{macprograms[0].Version}";
  338. // //写入文件
  339. // byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo);
  340. // return filedatas;
  341. // }
  342. // catch (Exception ex)
  343. // {
  344. // errorinfo = ex.Message.ToString();
  345. // return null;
  346. // }
  347. //}
  348. public int ReadMacProgram(string maccode, string programname, string mstdir, ref int filelen, ref string filepath, ref string errorinfo)
  349. {
  350. try
  351. {
  352. string condition = $" and a.fcode='{maccode}'";
  353. List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  354. if (macs.Count <= 0)
  355. {
  356. errorinfo = $"未找到机台编号{maccode}的信息。";
  357. return -1;
  358. }
  359. condition = $" and a.MacID={macs[0].ID} and a.ProgramName='{programname}' and a.IsDefault=1";
  360. List<MacProgram> macprograms = CurrDb.FindListForCondition<MacProgram>(condition, ref errorinfo).ToList();
  361. if (macprograms.Count <= 0)
  362. {
  363. errorinfo = $"未找到机台【{maccode}】,程序名称【{programname}】,对应的标准程序。";
  364. return -1;
  365. }
  366. if (macprograms.Count > 1)
  367. {
  368. errorinfo = $"机台【{maccode}】,程序名称【{programname}】,对应了多个标准程序。";
  369. return -1;
  370. }
  371. //filepath = $"{mstdir}\\{maccode}\\{programname}\\{programname}_{macprograms[0].Version}";
  372. filepath = $"{mstdir}\\{maccode}\\{programname}\\{macprograms[0].FileOrgName}";
  373. //写入文件
  374. byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo);
  375. if (filedatas == null)
  376. return -1;
  377. filelen = filedatas.Length;
  378. return 1;
  379. }
  380. catch (Exception ex)
  381. {
  382. errorinfo = ex.Message.ToString();
  383. return -1;
  384. }
  385. }
  386. public byte[] ReadMacProgram(Machine mac, string programname, int orgtypeid, string mstdir, ref string errorinfo)
  387. {
  388. try
  389. {
  390. string condition = $" and b.OrgTypeID={orgtypeid} and b.FName='{programname}' and a.IsDefault=1";
  391. if (orgtypeid == 1)
  392. {
  393. condition = $"{condition} and b.OrgID={mac.ID}";
  394. }
  395. else
  396. {
  397. condition = $"{condition} and b.OrgID={mac.MModeID}";
  398. }
  399. List<BusinessFileRelation> templists = CurrDb.FindListForCondition<BusinessFileRelation>(condition, ref errorinfo).ToList();
  400. if (templists.Count <= 0)
  401. {
  402. errorinfo = $"未找到机台【{mac.FCode}】,程序名称【{programname}】,对应的标准程序。";
  403. return null;
  404. }
  405. if (templists.Count > 1)
  406. {
  407. errorinfo = $"机台【{mac.FCode}】,程序名称【{programname}】,对应了多个标准程序。";
  408. return null;
  409. }
  410. condition = $" and a.id={templists[0].FileInfoID}";
  411. TFileInfo files = CurrDb.FindListForCondition<TFileInfo>(condition, ref errorinfo).ToList()[0];
  412. string filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}";
  413. if (files.FilePath.Contains("\\"))
  414. {
  415. filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}";
  416. }
  417. else
  418. {
  419. filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}";
  420. }
  421. //读取文件
  422. byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo);
  423. if (filedatas == null)
  424. return null;
  425. return filedatas;
  426. }
  427. catch (Exception ex)
  428. {
  429. errorinfo = ex.Message.ToString();
  430. return null;
  431. }
  432. }
  433. public byte[] ReadMacProgramD(Machine mac, string programname, int orgtypeid, string mstdir, ref string errorinfo)
  434. {
  435. try
  436. {
  437. string condition = $" and b.OrgTypeID={orgtypeid} and b.FName like '%@{programname}.zip' and a.IsDefault=1 ";
  438. if (orgtypeid == 1)
  439. {
  440. condition = $"{condition} and b.OrgID={mac.ID}";
  441. }
  442. else
  443. {
  444. condition = $"{condition} and b.OrgID={mac.MModeID}";
  445. }
  446. List<BusinessFileRelation> templists = CurrDb.FindListForCondition<BusinessFileRelation>(condition, ref errorinfo).ToList();
  447. if (templists.Count <= 0)
  448. {
  449. errorinfo = $"未找到机台【{mac.FCode}】,程序名称【{programname}】,对应的标准程序。";
  450. return null;
  451. }
  452. if (templists.Count > 1)
  453. {
  454. errorinfo = $"机台【{mac.FCode}】,程序名称【{programname}】,对应了多个标准程序。";
  455. return null;
  456. }
  457. condition = $" and a.id={templists[0].FileInfoID}";
  458. TFileInfo files = CurrDb.FindListForCondition<TFileInfo>(condition, ref errorinfo).ToList()[0];
  459. string filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}";
  460. if (files.FilePath.Contains("\\"))
  461. {
  462. filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}";
  463. }
  464. else
  465. {
  466. filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}";
  467. }
  468. //读取文件
  469. byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo);
  470. if (filedatas == null)
  471. return null;
  472. return filedatas;
  473. }
  474. catch (Exception ex)
  475. {
  476. errorinfo = ex.Message.ToString();
  477. return null;
  478. }
  479. }
  480. public int ReadMacProgram(Machine mac, string programname, int orgtypeid, string mstdir, ref string ppGroup, ref string errorinfo)
  481. {
  482. try
  483. {
  484. string condition = $" and b.OrgTypeID={orgtypeid} and b.FName like '%@{programname}.zip' and a.IsDefault=1";
  485. if (orgtypeid == 1)
  486. {
  487. condition = $"{condition} and b.OrgID={mac.ID}";
  488. }
  489. else
  490. {
  491. condition = $"{condition} and b.OrgID={mac.MModeID}";
  492. }
  493. List<BusinessFileRelation> templists = CurrDb.FindListForCondition<BusinessFileRelation>(condition, ref errorinfo).ToList();
  494. if (templists.Count <= 0)
  495. {
  496. errorinfo = $"未找到机台【{mac.FCode}】,程序名称【{programname}】,对应的标准程序。";
  497. return -1;
  498. }
  499. if (templists.Count > 1)
  500. {
  501. errorinfo = $"机台【{mac.FCode}】,程序名称【{programname}】,对应了多个标准程序。";
  502. return -1;
  503. }
  504. ppGroup = templists[0].FileOrgName.Replace($"@{programname}.zip", "");
  505. // condition = $" and a.id={templists[0].FileInfoID}";
  506. //TFileInfo files = CurrDb.FindListForCondition<TFileInfo>(condition, ref errorinfo).ToList()[0];
  507. //string filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}";
  508. //if (files.FilePath.Contains("\\"))
  509. //{
  510. // filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}";
  511. //}
  512. //else
  513. //{
  514. // filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}";
  515. //}
  516. //读取文件
  517. // byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo);
  518. //if (filedatas == null)
  519. // return null;
  520. // System.IO.File.Copy(filepath, toPath, true);
  521. return 1;
  522. }
  523. catch (Exception ex)
  524. {
  525. errorinfo = ex.Message.ToString();
  526. return -1;
  527. }
  528. }
  529. public byte[] ReadMacProgram(Machine mac, string programname, int ver, int orgtypeid, string mstdir, ref string errorinfo)
  530. {
  531. //try
  532. //{
  533. string condition = $" and b.OrgTypeID={orgtypeid} and b.FName='{programname}' and a.Version={ver}";
  534. if (orgtypeid == 1)
  535. {
  536. condition = $"{condition} and b.OrgID={mac.ID}";
  537. }
  538. else
  539. {
  540. condition = $"{condition} and b.OrgID={mac.MModeID}";
  541. }
  542. List<BusinessFileRelation> templists = CurrDb.FindListForCondition<BusinessFileRelation>(condition, ref errorinfo).ToList();
  543. if (templists.Count <= 0)
  544. {
  545. errorinfo = $"未找到机台【{mac.FCode}】,程序名称【{programname}】,对应的标准程序。";
  546. return null;
  547. }
  548. if (templists.Count > 1)
  549. {
  550. errorinfo = $"机台【{mac.FCode}】,程序名称【{programname}】,对应了多个标准程序。";
  551. return null;
  552. }
  553. condition = $" and a.id={templists[0].FileInfoID}";
  554. TFileInfo files = CurrDb.FindListForCondition<TFileInfo>(condition, ref errorinfo).ToList()[0];
  555. string filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}";
  556. //读取文件
  557. byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo);
  558. if (filedatas == null)
  559. return null;
  560. return filedatas;
  561. //}
  562. //catch (Exception ex)
  563. //{
  564. // errorinfo = ex.Message.ToString();
  565. // return null;
  566. //}
  567. }
  568. public TFileInfo ReadMacProgramForFile(Machine mac, string programname, int ver, int orgtypeid, string mstdir, ref string errorinfo)
  569. {
  570. try
  571. {
  572. string condition = $" and b.OrgTypeID={orgtypeid} and b.FName='{programname}' and a.Version={ver}";
  573. if (orgtypeid == 1)
  574. {
  575. condition = $"{condition} and b.OrgID={mac.ID}";
  576. }
  577. else
  578. {
  579. condition = $"{condition} and b.OrgID={mac.MModeID}";
  580. }
  581. List<BusinessFileRelation> templists = CurrDb.FindListForCondition<BusinessFileRelation>(condition, ref errorinfo).ToList();
  582. if (templists.Count <= 0)
  583. {
  584. errorinfo = $"未找到机台【{mac.FCode}】,程序名称【{programname}】,对应的程序。";
  585. return null;
  586. }
  587. if (templists.Count > 1)
  588. {
  589. errorinfo = $"机台【{mac.FCode}】,程序名称【{programname}】,对应了多个程序。";
  590. return null;
  591. }
  592. condition = $" and a.id={templists[0].FileInfoID}";
  593. TFileInfo files = CurrDb.FindListForCondition<TFileInfo>(condition, ref errorinfo).ToList()[0];
  594. string filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}";
  595. files.FilePath = filepath;
  596. return files;
  597. }
  598. catch (Exception ex)
  599. {
  600. errorinfo = ex.Message.ToString();
  601. return null;
  602. }
  603. }
  604. public TFileInfo ReadMacProgramForFile(Machine mac, string programname, int orgtypeid, string mstdir, ref string ppGroup, ref string errorinfo)
  605. {
  606. try
  607. {
  608. string condition = $" and b.OrgTypeID={orgtypeid} and b.FName like '%@{programname}.zip' and a.IsDefault=1 ";
  609. if (orgtypeid == 1)
  610. {
  611. condition = $"{condition} and b.OrgID={mac.ID}";
  612. }
  613. else
  614. {
  615. condition = $"{condition} and b.OrgID={mac.MModeID}";
  616. }
  617. List<BusinessFileRelation> templists = CurrDb.FindListForCondition<BusinessFileRelation>(condition, ref errorinfo).ToList();
  618. if (templists.Count <= 0)
  619. {
  620. errorinfo = $"未找到机台【{mac.FCode}】,程序名称【{programname}】,对应的程序。";
  621. return null;
  622. }
  623. if (templists.Count > 1)
  624. {
  625. errorinfo = $"机台【{mac.FCode}】,程序名称【{programname}】,对应了多个程序。";
  626. return null;
  627. }
  628. ppGroup = templists[0].FileOrgName.Replace($"@{programname}.zip", "");
  629. condition = $" and a.id={templists[0].FileInfoID}";
  630. TFileInfo files = CurrDb.FindListForCondition<TFileInfo>(condition, ref errorinfo).ToList()[0];
  631. string filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}";
  632. files.FilePath = filepath;
  633. return files;
  634. }
  635. catch (Exception ex)
  636. {
  637. errorinfo = ex.Message.ToString();
  638. return null;
  639. }
  640. }
  641. public byte[] ReadMacProgram(string maccode, string programname, string mstdir, ref string errorinfo)
  642. {
  643. try
  644. {
  645. string condition = $" and a.fcode='{maccode}'";
  646. List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  647. if (macs.Count <= 0)
  648. {
  649. errorinfo = $"未找到机台编号{maccode}的信息。";
  650. return null;
  651. }
  652. condition = $" and a.MacID={macs[0].ID} and a.ProgramName='{programname}' and a.IsDefault=1";
  653. List<MacProgram> macprograms = CurrDb.FindListForCondition<MacProgram>(condition, ref errorinfo).ToList();
  654. if (macprograms.Count <= 0)
  655. {
  656. errorinfo = $"未找到机台【{maccode}】,程序名称【{programname}】,对应的标准程序。";
  657. return null;
  658. }
  659. if (macprograms.Count > 1)
  660. {
  661. errorinfo = $"机台【{maccode}】,程序名称【{programname}】,对应了多个标准程序。";
  662. return null;
  663. }
  664. //filepath = $"{mstdir}\\{maccode}\\{programname}\\{programname}_{macprograms[0].Version}";
  665. string filepath = $"{mstdir}\\{maccode}\\{programname}\\{macprograms[0].FileOrgName}";
  666. //写入文件
  667. byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo);
  668. if (filedatas == null)
  669. return null;
  670. return filedatas;
  671. }
  672. catch (Exception ex)
  673. {
  674. errorinfo = ex.Message.ToString();
  675. return null;
  676. }
  677. }
  678. public int ReadMacModelProgram(string maccode, string programname, string mstdir, ref int filelen, ref string filepath, ref string errorinfo)
  679. {
  680. try
  681. {
  682. string condition = $" and a.fcode='{maccode}'";
  683. List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  684. if (macs.Count <= 0)
  685. {
  686. errorinfo = $"未找到机台编号{maccode}的信息。";
  687. return -1;
  688. }
  689. //读取机型
  690. condition = $" and a.id={macs[0].MModeID}";
  691. List<MacModel> macmodels = CurrDb.FindListForCondition<MacModel>(condition, ref errorinfo).ToList();
  692. if (macmodels.Count <= 0)
  693. {
  694. errorinfo = $"未找到机台【{maccode}】,对应的机型。";
  695. return -1;
  696. }
  697. condition = $" and a.MacModelID={macmodels[0].ID} and a.ProgramName='{programname}' and a.IsDefault=1";
  698. List<MacModelProgram> macprograms = CurrDb.FindListForCondition<MacModelProgram>(condition, ref errorinfo).ToList();
  699. if (macprograms.Count <= 0)
  700. {
  701. errorinfo = $"未找到机型【{macmodels[0].FCode}】,程序名称【{programname}】,对应的标准程序。";
  702. return -1;
  703. }
  704. if (macprograms.Count > 1)
  705. {
  706. errorinfo = $"未找到机型【{macmodels[0].FCode}】,程序名称【{programname}】,对应了多个标准程序。";
  707. return -1;
  708. }
  709. //filepath = $"{mstdir}\\{macmodels[0].FCode}\\{programname}\\{programname}_{macprograms[0].Version}";
  710. filepath = $"{mstdir}\\{macmodels[0].FCode}\\{programname}\\{macprograms[0].FileOrgName}";
  711. //写入文件
  712. byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo);
  713. filelen = filedatas.Length;
  714. return 1;
  715. }
  716. catch (Exception ex)
  717. {
  718. errorinfo = ex.Message.ToString();
  719. return -1;
  720. }
  721. }
  722. public byte[] ReadMacModelProgram(string maccode, string programname, string mstdir, ref string errorinfo)
  723. {
  724. try
  725. {
  726. string condition = $" and a.fcode='{maccode}'";
  727. List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  728. if (macs.Count <= 0)
  729. {
  730. errorinfo = $"未找到机台编号{maccode}的信息。";
  731. return null;
  732. }
  733. //读取机型
  734. condition = $" and a.id={macs[0].MModeID}";
  735. List<MacModel> macmodels = CurrDb.FindListForCondition<MacModel>(condition, ref errorinfo).ToList();
  736. if (macmodels.Count <= 0)
  737. {
  738. errorinfo = $"未找到机台【{maccode}】,对应的机型。";
  739. return null;
  740. }
  741. condition = $" and a.MacModelID={macmodels[0].ID} and a.ProgramName='{programname}' and a.IsDefault=1";
  742. List<MacModelProgram> macprograms = CurrDb.FindListForCondition<MacModelProgram>(condition, ref errorinfo).ToList();
  743. if (macprograms.Count <= 0)
  744. {
  745. errorinfo = $"未找到机型【{macmodels[0].FCode}】,程序名称【{programname}】,对应的标准程序。";
  746. return null;
  747. }
  748. if (macprograms.Count > 1)
  749. {
  750. errorinfo = $"未找到机型【{macmodels[0].FCode}】,程序名称【{programname}】,对应了多个标准程序。";
  751. return null;
  752. }
  753. //filepath = $"{mstdir}\\{macmodels[0].FCode}\\{programname}\\{programname}_{macprograms[0].Version}";
  754. string filepath = $"{mstdir}\\{macmodels[0].FCode}\\{programname}\\{macprograms[0].FileOrgName}";
  755. //写入文件
  756. byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo);
  757. return filedatas;
  758. }
  759. catch (Exception ex)
  760. {
  761. errorinfo = ex.Message.ToString();
  762. return null;
  763. }
  764. }
  765. public IEnumerable<MacProgram> Get()
  766. {
  767. var models = CurrDb.FindList<MacProgram>();
  768. return models;
  769. }
  770. public IEnumerable<MacProgram> Get(int start, int length, string order, string sort, string filter, string errorinfo)
  771. {
  772. var sql = new BusinessFileRelation().GetSelectSql();
  773. var versions = CurrDb.FindListForCondition<BusinessFileRelation>($"{filter} order by {sort} {order} limit {start - 1},{length} ", ref errorinfo);
  774. return versions.Select(c => new MacProgram
  775. {
  776. ID = c.ID,
  777. FileOrgName = c.FileOrgName,
  778. IsDefault = c.IsDefault,
  779. ModCode = c.ModCode,
  780. MacID = c.OrgID,
  781. Version = c.Version,
  782. ModTime = c.ModTime,
  783. RecCode = c.RecCode,
  784. ProgramName = c.ProgramName,
  785. RecTime = c.RecTime,
  786. Remark = c.remark,
  787. FileInfoRemark = c.FileInfoRemark,
  788. UploadMacCode = c.UploadMacCode
  789. });
  790. }
  791. public int GetVersionCount(string filter)
  792. {
  793. string errorinfo = string.Empty;
  794. var versions = CurrDb.FindListForCondition<BusinessFileRelation>($"{filter}", ref errorinfo);
  795. return versions.Count();
  796. }
  797. public IEnumerable<MacProgramDto> GetGrouped(int start, int length, string order, string sort, string filter, string errorinfo)
  798. {
  799. if (!string.IsNullOrEmpty(filter))
  800. filter = filter.Replace("%2B", "+");
  801. var sql = $"select a.Id,a.FName as ProgramName,b.FCode MacCode," +
  802. $"b.FName MacName,c.FCode MacModelCode,c.FName MacModelName," +
  803. $"b.Id MacId,c.Id MacModelId,b.RegionId " +
  804. $"from businessfile a " +
  805. $"inner join Machine b on a.OrgId=b.Id and a.OrgTypeID=1 " +
  806. $"left join MacModel c on b.MModeID=c.id " +
  807. $"where 1=1 {filter + commonFilter} order by {sort} {order} limit {start - 1},{length} ";
  808. var datas = CurrDb.FindList<MacProgramDto>(sql);
  809. if (datas != null && datas.Count() > 0)
  810. {
  811. var regionDal = new FactoryRegionDal(CurrDb);
  812. var allRegions = regionDal.Get(1, 10000, "ID", "asc", string.Empty, errorinfo);
  813. foreach (var item in datas)
  814. {
  815. var regionName = string.Empty;
  816. if (item.RegionId != null)
  817. {
  818. regionName = regionDal.GetFullRegionName(item.RegionId.Value, allRegions);
  819. }
  820. item.RegionName = regionName;
  821. }
  822. }
  823. return datas;
  824. }
  825. public int GetGroupedCount(string filter)
  826. {
  827. if (!string.IsNullOrEmpty(filter))
  828. filter = filter.Replace("%2B", "+");
  829. string sql = $"select count(1) " +
  830. $"from businessfile a " +
  831. $"inner join Machine b on a.OrgId=b.Id and a.OrgTypeID=1 " +
  832. $"left join MacModel c on b.MModeID=c.id " +
  833. $"where 1=1 {filter + commonFilter}";
  834. return Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "0");
  835. }
  836. public int Delete(int id, ref string msg)
  837. {
  838. if (CurrDb.DeleteFor<BusinessFile>(id) < 0)
  839. {
  840. msg = "删除失败";
  841. return -1;
  842. }
  843. msg = string.Empty;
  844. return 1;
  845. }
  846. public BusinessFile getBusinessFile(int id)
  847. {
  848. return CurrDb.FindEntityFor<BusinessFile>(id);
  849. }
  850. public IEnumerable<string> GetProgramFiles(string macCode, string programName, ref string errorinfo)
  851. {
  852. string path = $@"{programBaseDir}\{macCode}\{programName}";
  853. if (!Directory.Exists(path))
  854. {
  855. errorinfo = "程序目录不存在,请确认";
  856. return null;
  857. }
  858. var programs = Directory.EnumerateFiles(path).Select(c =>
  859. {
  860. var arr = c.Split(new string[] { @"\" }, StringSplitOptions.RemoveEmptyEntries);
  861. var temp = arr.Last();
  862. var name = temp.Split(new char[] { '.' }).FirstOrDefault();
  863. return name;
  864. });
  865. return programs;
  866. }
  867. /// <summary>
  868. /// 设置默认版次
  869. /// </summary>
  870. /// <param name="id"></param>
  871. /// <param name="isDefault"></param>
  872. /// <param name="macId"></param>
  873. /// <param name="programName"></param>
  874. /// <param name="errorinfo"></param>
  875. /// <returns></returns>
  876. public int UpdateField(int id, int isDefault, int macId, string programName, ref string errorinfo)
  877. {
  878. if (isDefault != -1)
  879. {
  880. var filter = $" and a.BusinessFileId=(select id from businessfile where orgId={macId} and OrgTypeID=1 and FName='{programName}'" +
  881. $" limit 0,1) and a.isDefault=1";
  882. var entity = CurrDb.FindListForCondition<BusinessFileRelation>(filter, ref errorinfo).FirstOrDefault();
  883. if (entity != null)
  884. {
  885. if (entity.ID != id)
  886. {
  887. errorinfo = "当前机台已经存在默认程序,如需重新设置请先取消原来的默认程序";
  888. }
  889. else
  890. {
  891. errorinfo = "当前程序版次已经是默认版次,无需重复设置";
  892. }
  893. return -1;
  894. }
  895. }
  896. string sql = $"update BusinessFileRelation set isDefault={isDefault} where id={id}";
  897. return CurrDb.ExecuteBySql(sql);
  898. }
  899. /// <summary>
  900. /// 修改程序名称,先修改数据库程序名称(macprogram,macmodelprogram),然后重命名文件夹
  901. /// </summary>
  902. /// <param name="maccode"></param>
  903. /// <param name="orgname"></param>
  904. /// <param name="desname"></param>
  905. /// <param name="errorinfo"></param>
  906. /// <returns></returns>
  907. public int ModifyProgramName(string maccode, string orgname, string desname, ref string errorinfo)
  908. {
  909. try
  910. {
  911. string condition = $" and a.fcode='{maccode}'";
  912. List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  913. if (macs.Count <= 0)
  914. {
  915. errorinfo = $"未找到机台编号{maccode}的信息。";
  916. return -1;
  917. }
  918. //检查目的程序名称是否存在
  919. string sqlstr = $" and a.OrgId={macs[0].ID} and OrgTypeID=1 ";
  920. List<BusinessFile> programs = CurrDb.FindListForCondition<BusinessFile>(sqlstr, ref errorinfo).Where(c => c.FName == desname).ToList();
  921. if (programs.Count > 0)
  922. {
  923. errorinfo = $"机台{maccode}的目的程序名称{desname}已存在,不能修改。";
  924. return -1;
  925. }
  926. //读取机台原程序信息
  927. sqlstr = $" and a.OrgId={macs[0].ID} and OrgTypeID=1";
  928. programs = CurrDb.FindListForCondition<BusinessFile>(sqlstr, ref errorinfo).Where(c => c.FName == orgname).ToList();
  929. sqlstr = $@"select a.* from BusinessFile a
  930. left join businessfilerelation b on a.id=b.businessfileid
  931. left join TFileInfo c on b.FileInfoId=c.Id
  932. where 1=1 and a.OrgTypeID=2 and a.OrgId={macs[0].MModeID}";
  933. IEnumerable<BusinessFile> modelprograms = CurrDb.FindList<BusinessFile>(sqlstr).Where(c => c.FName == desname);
  934. if (modelprograms.Count() > 0)
  935. {
  936. errorinfo = $"机型{macs[0].MModeCode}目的程序名称{desname}已存在,不能修改。";
  937. return -1;
  938. }
  939. sqlstr = $" and OrgTypeId=2 and a.OrgId={macs[0].MModeID}";
  940. modelprograms = CurrDb.FindListForCondition<BusinessFile>(sqlstr, ref errorinfo).Where(c => c.FName == orgname).ToList();
  941. if (programs.Count > 0)
  942. {
  943. foreach (var item in programs)
  944. item.FName = desname;
  945. if (CurrDb.UpdateFor<BusinessFile>(programs, "") < 0)
  946. {
  947. errorinfo = "更新机台程序失败";
  948. return -1;
  949. }
  950. }
  951. if (modelprograms.Count() > 0)
  952. {
  953. foreach (var item in modelprograms)
  954. item.FName = desname;
  955. if (CurrDb.UpdateFor<BusinessFile>(modelprograms, "") < 0)
  956. {
  957. errorinfo = "更新机型程序失败";
  958. return -1;
  959. }
  960. }
  961. var programmstDal = new ProgramMstDal(CurrDb);
  962. if (programmstDal.IUProgramMst(macs[0], desname, ref errorinfo) < 0)
  963. return -1;
  964. return 1;
  965. }
  966. catch (Exception ex)
  967. {
  968. errorinfo = ex.Message.ToString();
  969. return -1;
  970. }
  971. }
  972. /// <summary>
  973. /// 拷贝机台版次
  974. /// </summary>
  975. /// <param name="macCodes"></param>
  976. /// <param name="proid"></param>
  977. /// <param name="userCode"></param>
  978. /// <param name="errorinfo"></param>
  979. /// <returns></returns>
  980. public int CopyProgramVersion(IEnumerable<string> macCodes, int proid, string userCode, ref string errorinfo)
  981. {
  982. if (macCodes == null || macCodes.Count() <= 0)
  983. {
  984. errorinfo = "需要复制的机台不能为空";
  985. return -1;
  986. }
  987. var program = CurrDb.FindEntityFor<BusinessFileRelation>(proid);
  988. // program.ProgramName = program.ProgramName.Trim();
  989. if (program == null)
  990. {
  991. errorinfo = "待复制的程序不存在或已被删除";
  992. return -1;
  993. }
  994. var macId = CurrDb.FindEntityFor<BusinessFile>(program.BusinessFileID).OrgID;
  995. var oriMac = CurrDb.FindEntityFor<Machine>(macId);
  996. foreach (var item in macCodes)
  997. {
  998. var error = string.Empty;
  999. var res = this.CopySingleVersion(oriMac.FCode, item, program, userCode, ref error);
  1000. errorinfo += error + "<br />";
  1001. }
  1002. return 1;
  1003. }
  1004. /// <summary>
  1005. /// 复制单个版次程序
  1006. /// </summary>
  1007. /// <param name="oriMacCode"></param>
  1008. /// <param name="macCode"></param>
  1009. /// <param name="macProgram"></param>
  1010. /// <param name="userCode"></param>
  1011. /// <param name="errorinfo"></param>
  1012. /// <returns></returns>
  1013. private int CopySingleVersion(string oriMacCode, string macCode, BusinessFileRelation macProgram, string userCode, ref string errorinfo)
  1014. {
  1015. using (IDatabase db = DbFactory.Base("eap"))
  1016. {
  1017. CurrDb = db;
  1018. CurrDb.BeginTrans();
  1019. var machine = CurrDb.FindListForCondition<Machine>($" and a.FCode='{macCode}'", ref errorinfo).FirstOrDefault();
  1020. var proVersion = CurrDb.FindListForCondition<BusinessFileRelation>($" and b.OrgId={machine.ID} " +
  1021. $"and b.OrgTypeID={macProgram.OrgTypeID} and b.FName='{macProgram.ProgramName}'" +
  1022. $"and a.Version='{macProgram.Version}'", ref errorinfo).FirstOrDefault();
  1023. if (proVersion != null && proVersion.IsDefault == 1)
  1024. {
  1025. CurrDb.Rollback();
  1026. errorinfo = $"机台【{macCode}】当前程序版次已存在,并且该版次为默认程序,不能覆盖";
  1027. return 1;
  1028. }
  1029. var bf = new BusinessFile
  1030. {
  1031. OrgID = machine.ID,
  1032. OrgTypeID = 1,
  1033. FName = macProgram.ProgramName,
  1034. RecCode = userCode,
  1035. RecTime = DateTime.Now,
  1036. ModCode = userCode,
  1037. ModTime = DateTime.Now,
  1038. };
  1039. var existBf = CurrDb.FindListForCondition<BusinessFile>($" and a.FName='{macProgram.ProgramName}' and a.OrgTypeID=1 " +
  1040. $"and a.OrgId={machine.ID}", ref errorinfo).FirstOrDefault();
  1041. var businessFileId = 0;
  1042. if (existBf != null)
  1043. {
  1044. businessFileId = existBf.ID;
  1045. }
  1046. else
  1047. {
  1048. if (CurrDb.InsertFor(bf, userCode) < 0)
  1049. {
  1050. CurrDb.Rollback();
  1051. errorinfo = "插入主表失败";
  1052. return -1;
  1053. }
  1054. string sql = "select @@identity;";
  1055. businessFileId = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  1056. }
  1057. var obj = new BusinessFileRelation();
  1058. obj.IsDefault = macProgram.IsDefault;
  1059. obj.BusinessFileID = businessFileId;
  1060. obj.ModCode = userCode;
  1061. obj.ModTime = DateTime.Now;
  1062. obj.Version = macProgram.Version;
  1063. obj.RecCode = userCode;
  1064. obj.RecTime = DateTime.Now;
  1065. obj.FileInfoID = macProgram.FileInfoID;
  1066. var existPros = CurrDb.FindListForCondition<BusinessFileRelation>($" and b.OrgId={machine.ID} and a.IsDefault=1 " +
  1067. $"and b.OrgTypeID=1", ref errorinfo);
  1068. if (existPros != null && existPros.Count() > 0)
  1069. {
  1070. obj.IsDefault = -1;
  1071. }
  1072. if (proVersion != null) // 当待复制的程序不是默认程序且目标机台有同名程序时,更新数据库数据并覆盖原程序
  1073. {
  1074. obj.ID = proVersion.ID;
  1075. if (CurrDb.UpdateFor(obj, userCode) < 0)
  1076. {
  1077. errorinfo = $"机台号【{macCode}】的机台覆盖程序失败,原因是【更新数据库表记录失败】";
  1078. CurrDb.Rollback();
  1079. return -1;
  1080. }
  1081. }
  1082. else
  1083. {
  1084. if (CurrDb.InsertFor<BusinessFileRelation>(obj, userCode) < 0)
  1085. {
  1086. errorinfo = $"机台号【{macCode}】的机台复制程序失败,原因是【插入数据库表记录失败】";
  1087. CurrDb.Rollback();
  1088. return -1;
  1089. }
  1090. }
  1091. var program = CurrDb.FindListForCondition<ProgramMst>($" and a.FName='{macProgram.ProgramName}' " +
  1092. $"and a.ModelID='{machine.MModeID}' and a.ProcessCode='{machine.PCode}' ",
  1093. ref errorinfo).FirstOrDefault();
  1094. if (program == null)
  1095. {
  1096. var temp = new ProgramMst
  1097. {
  1098. FName = macProgram.ProgramName,
  1099. ModelID = machine.MModeID,
  1100. ProcessCode = machine.PCode,
  1101. RecCode = userCode,
  1102. RecTime = DateTime.Now,
  1103. ModCode = userCode,
  1104. ModTime = DateTime.Now,
  1105. Remark = $"由机台【{machine.FCode}】上复制而来"
  1106. };
  1107. if (CurrDb.InsertFor(temp, userCode) < 0)
  1108. {
  1109. errorinfo = "程序表新增数据失败";
  1110. return -1;
  1111. }
  1112. }
  1113. errorinfo = $"机台【{macCode}】复制版次成功";
  1114. CurrDb.Commit();
  1115. return 1;
  1116. }
  1117. }
  1118. /// <summary>
  1119. /// 拷贝整个机台的程序到另一个机台
  1120. /// </summary>
  1121. /// <param name="macCodes"></param>
  1122. /// <param name="oriMacCode"></param>
  1123. /// <param name="userCode"></param>
  1124. /// <param name="errorinfo"></param>
  1125. /// <returns></returns>
  1126. public int CopyWholePrograms(IEnumerable<string> macCodes, string oriMacCode, string userCode, ref string errorinfo)
  1127. {
  1128. if (macCodes == null || macCodes.Count() <= 0)
  1129. {
  1130. errorinfo = "需要复制的机台不能为空";
  1131. return -1;
  1132. }
  1133. var machine = CurrDb.FindListForCondition<Machine>($" and a.FCode='{oriMacCode}'", ref errorinfo).FirstOrDefault();
  1134. if (machine == null)
  1135. {
  1136. errorinfo = "机台不存在";
  1137. return -1;
  1138. }
  1139. var toCopyPros = CurrDb.FindListForCondition<BusinessFile>($" and a.OrgId={machine.ID} and a.OrgTypeId=1", ref errorinfo);
  1140. if (toCopyPros == null || toCopyPros.Count() <= 0)
  1141. {
  1142. errorinfo = "待复制的程序为空";
  1143. return -1;
  1144. }
  1145. foreach (var item in toCopyPros)
  1146. {
  1147. var versions = CurrDb.FindListForCondition<BusinessFileRelation>($" and a.BusinessFileId={item.ID}", ref errorinfo);
  1148. item.BusinessFileRelations = versions;
  1149. }
  1150. foreach (var item in macCodes)
  1151. {
  1152. var error = string.Empty;
  1153. var res = this.CopyWholeSinglePro(item, oriMacCode, toCopyPros, userCode, ref error);
  1154. errorinfo += error + "<br />";
  1155. }
  1156. return 1;
  1157. }
  1158. /// <summary>
  1159. /// 拷贝单个程序
  1160. /// </summary>
  1161. /// <param name="macCode"></param>
  1162. /// <param name="oriMacCode"></param>
  1163. /// <param name="pros"></param>
  1164. /// <param name="userCode"></param>
  1165. /// <param name="errorinfo"></param>
  1166. /// <returns></returns>
  1167. private int CopyWholeSinglePro(string macCode, string oriMacCode, IEnumerable<BusinessFile> pros, string userCode, ref string errorinfo)
  1168. {
  1169. try
  1170. {
  1171. CurrDb = DbFactory.Base("eap");
  1172. CurrDb.BeginTrans();
  1173. var machine = CurrDb.FindListForCondition<Machine>($" and a.FCode='{macCode}'", ref errorinfo).FirstOrDefault();
  1174. var existPros = CurrDb.FindListForCondition<BusinessFile>($" and a.OrgId={machine.ID} and a.OrgTypeId=1", ref errorinfo);
  1175. foreach (var item in pros)
  1176. {
  1177. if (existPros.FirstOrDefault(c => c.OrgTypeID == item.OrgTypeID && c.OrgID == machine.ID && c.FName == item.FName) == null)
  1178. {
  1179. item.OrgID = machine.ID;
  1180. if (CurrDb.InsertFor<BusinessFile>(item, userCode) < 0)
  1181. {
  1182. errorinfo = $"机台【{machine.FCode}】复制失败,原因是:插入数据库失败";
  1183. CurrDb.Rollback();
  1184. return -1;
  1185. }
  1186. var sql = "select @@identity;";
  1187. var id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  1188. if (item.BusinessFileRelations != null && item.BusinessFileRelations.Count() > 0)
  1189. {
  1190. foreach (var r in item.BusinessFileRelations)
  1191. {
  1192. var temp = new BusinessFileRelation
  1193. {
  1194. BusinessFileID = id,
  1195. FileInfoID = r.FileInfoID,
  1196. IsDefault = r.IsDefault,
  1197. Version = r.Version,
  1198. RecCode = userCode,
  1199. RecTime = DateTime.Now,
  1200. ModCode = userCode,
  1201. ModTime = DateTime.Now
  1202. };
  1203. if (CurrDb.InsertFor(temp, userCode) < 0)
  1204. {
  1205. CurrDb.Rollback();
  1206. errorinfo = "插入子表失败";
  1207. return -1;
  1208. }
  1209. }
  1210. }
  1211. var program = CurrDb.FindListForCondition<ProgramMst>($" and a.FName='{item.FName}' " +
  1212. $"and a.ModelID='{machine.MModeID}' and a.ProcessCode='{machine.PCode}' ",
  1213. ref errorinfo).FirstOrDefault();
  1214. if (program == null)
  1215. {
  1216. var temp = new ProgramMst
  1217. {
  1218. FName = item.FName,
  1219. ModelID = machine.MModeID,
  1220. ProcessCode = machine.PCode,
  1221. RecCode = userCode,
  1222. RecTime = DateTime.Now,
  1223. ModCode = userCode,
  1224. ModTime = DateTime.Now,
  1225. Remark = $"由机台【{machine.FCode}】上复制而来"
  1226. };
  1227. if (CurrDb.InsertFor(temp, userCode) < 0)
  1228. {
  1229. errorinfo = "程序表新增数据失败";
  1230. return -1;
  1231. }
  1232. }
  1233. }
  1234. }
  1235. errorinfo = $"机台【{macCode}】程序复制成功";
  1236. CurrDb.Commit();
  1237. return 1;
  1238. }
  1239. catch (Exception e)
  1240. {
  1241. errorinfo = e.Message;
  1242. CurrDb.Rollback();
  1243. return -1;
  1244. }
  1245. finally
  1246. {
  1247. if (CurrDb != null)
  1248. CurrDb.Close();
  1249. }
  1250. }
  1251. public BusinessFileRelation getBusinessFileRelation(int id)
  1252. {
  1253. return CurrDb.FindEntityFor<BusinessFileRelation>(id);
  1254. }
  1255. public IEnumerable<BusinessFile> getBusinessFile(BusinessFileRelation macProgram)
  1256. {
  1257. string sql = $"select * from BusinessFile where OrgId='{macProgram.OrgID}' and OrgTypeId=1 and Fname='{macProgram.ProgramName}' and OrgTypeID =1 ";
  1258. return CurrDb.FindList<BusinessFile>(sql);
  1259. }
  1260. /// <summary>
  1261. /// 删除单个版次
  1262. /// </summary>
  1263. /// <param name="id"></param>
  1264. /// <param name="macCode"></param>
  1265. /// <param name="errorinfo"></param>
  1266. /// <returns></returns>
  1267. public int DeleteSingVersion(int id, string macCode, ref string errorinfo)
  1268. {
  1269. var macProgram = CurrDb.FindEntityFor<BusinessFileRelation>(id);
  1270. if (macProgram != null)
  1271. {
  1272. var sql = $"delete from businessfilerelation where id={id}";
  1273. if (CurrDb.ExecuteBySql(sql) < 0)
  1274. {
  1275. errorinfo = "删除数据库中程序版次失败";
  1276. return -1;
  1277. }
  1278. var macPros = CurrDb.FindListForCondition<BusinessFileRelation>($" and b.OrgId='{macProgram.OrgID}' and b.OrgTypeID=1 " +
  1279. $"and b.FName='{macProgram.ProgramName}'", ref errorinfo);
  1280. if (macPros == null || macPros.Count() <= 0)
  1281. {
  1282. sql = $"delete from BusinessFile where OrgId='{macProgram.OrgID}' and OrgTypeId=1 and Fname='{macProgram.ProgramName}' and OrgTypeID =1 ";
  1283. if (CurrDb.ExecuteBySql(sql) < 0)
  1284. {
  1285. errorinfo = "删除程序版次失败";
  1286. return -1;
  1287. }
  1288. }
  1289. }
  1290. return 1;
  1291. }
  1292. public IEnumerable<BusinessFileRelation> getbusinessfilerelation(string proName, string macCode, ref string errorinfo)
  1293. {
  1294. var machine = CurrDb.FindListForCondition<Machine>($" and a.Fcode='{macCode}'", ref errorinfo).FirstOrDefault();
  1295. var busineeFile = CurrDb.FindListForCondition<BusinessFile>($" and a.OrgTypeId=1 and OrgId={machine.ID} and a.FName='{proName}'",
  1296. ref errorinfo).FirstOrDefault();
  1297. if (busineeFile == null)
  1298. {
  1299. errorinfo = "当前程序不存在或已被删除";
  1300. return null;
  1301. }
  1302. var sql = $"select * from businessfilerelation where BusinessFileId={busineeFile.ID}";
  1303. return CurrDb.FindList<BusinessFileRelation>(sql);
  1304. }
  1305. public BusinessFile getBusinessFile(string proName, string macCode, ref string errorinfo)
  1306. {
  1307. var machine = CurrDb.FindListForCondition<Machine>($" and a.Fcode='{macCode}'", ref errorinfo).FirstOrDefault();
  1308. var busineeFile = CurrDb.FindListForCondition<BusinessFile>($" and a.OrgTypeId=1 and OrgId={machine.ID} and a.FName='{proName}'",
  1309. ref errorinfo).FirstOrDefault();
  1310. if (busineeFile == null)
  1311. {
  1312. errorinfo = "当前程序不存在或已被删除";
  1313. return null;
  1314. }
  1315. return CurrDb.FindEntity<BusinessFile>(busineeFile.ID);
  1316. }
  1317. /// <summary>
  1318. /// 删除整个程序
  1319. /// </summary>
  1320. /// <param name="proName"></param>
  1321. /// <param name="macCode"></param>
  1322. /// <param name="errorinfo"></param>
  1323. /// <returns></returns>
  1324. public int DeleteWholeProgram(string proName, string macCode, ref string errorinfo)
  1325. {
  1326. var machine = CurrDb.FindListForCondition<Machine>($" and a.Fcode='{macCode}'", ref errorinfo).FirstOrDefault();
  1327. var busineeFile = CurrDb.FindListForCondition<BusinessFile>($" and a.OrgTypeId=1 and OrgId={machine.ID} and a.FName='{proName}'",
  1328. ref errorinfo).FirstOrDefault();
  1329. if (busineeFile == null)
  1330. {
  1331. errorinfo = "当前程序不存在或已被删除";
  1332. return -1;
  1333. }
  1334. var sql = $"delete from businessfilerelation where BusinessFileId={busineeFile.ID}";
  1335. if (CurrDb.ExecuteBySql(sql) < 0)
  1336. {
  1337. errorinfo = "删除机台程序表失败";
  1338. return -1;
  1339. }
  1340. if (CurrDb.DeleteFor<BusinessFile>(busineeFile.ID) < 0)
  1341. {
  1342. errorinfo = "删除程序主表失败";
  1343. return -1;
  1344. }
  1345. return 1;
  1346. }
  1347. }
  1348. }