MachineDal.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. using Cksoft.Data;
  2. using Cksoft.Unity;
  3. using DllEapEntity;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using DllEapEntity.Dtos;
  8. using System.ComponentModel.DataAnnotations;
  9. using Cksoft.Unity.Log4NetConfig;
  10. using DllEapCommon;
  11. using DllEapDal.OFILM;
  12. using Cksoft.Data.Repository;
  13. using System.Threading.Tasks;
  14. using DllEapCommon.NPOI;
  15. using DllEapEntity.Dtos.Export;
  16. using AutoMapper;
  17. using NPOI.SS.UserModel;
  18. namespace DllEapDal
  19. {
  20. public class MachineDal
  21. {
  22. private IDatabase CurrDb = null;
  23. private string commonFilter = null;
  24. private IMapper _mapper;
  25. public MachineDal(IDatabase db)
  26. {
  27. CurrDb = db;
  28. }
  29. public MachineDal(IDatabase db, string userCode)
  30. {
  31. CurrDb = db;
  32. }
  33. public MachineDal(IDatabase db, IMapper mapper)
  34. {
  35. CurrDb = db;
  36. _mapper = mapper;
  37. }
  38. public Machine IUMachine(Machine mst, string usercode, ref string errorinfo)
  39. {
  40. try
  41. {
  42. int result = 0;
  43. int id = mst.ID;
  44. if (mst.EntityStatusID == 1)
  45. {
  46. mst.RecCode = usercode;
  47. mst.RecTime = DateTime.Now;
  48. mst.ModCode = usercode;
  49. mst.ModTime = DateTime.Now;
  50. result = CurrDb.InsertFor(mst, usercode);
  51. if (result < 0)
  52. {
  53. return null;
  54. }
  55. object objid = CurrDb.FindObject("select @@IDENTITY");
  56. if (objid.ToString() == "")
  57. {
  58. return null;
  59. }
  60. id = int.Parse(objid.ToString());
  61. }
  62. else
  63. {
  64. mst.ModCode = usercode;
  65. mst.ModTime = DateTime.Now;
  66. result = CurrDb.UpdateFor(mst, usercode);
  67. if (result < 0)
  68. {
  69. return null;
  70. }
  71. }
  72. mst = CurrDb.FindEntityFor<Machine>(id);
  73. return mst;
  74. }
  75. catch (Exception e)
  76. {
  77. errorinfo = e.Message;
  78. return null;
  79. }
  80. }
  81. public object DelMachine(List<Machine> msts, ref string errorinfo)
  82. {
  83. try
  84. {
  85. int result = CurrDb.DeleteForEntity<Machine>(msts);
  86. if (result < 0)
  87. return null;
  88. return result;
  89. }
  90. catch (Exception e)
  91. {
  92. errorinfo = e.Message;
  93. return null;
  94. }
  95. }
  96. public IEnumerable<CascaderDto> GetCascaders()
  97. {
  98. var processDal = new TProcessDal(CurrDb);
  99. var processes = processDal.Get();
  100. var macmodelDal = new MacModelDal(CurrDb);
  101. var macmodels = macmodelDal.GetSecMacModels();
  102. var list = new List<CascaderDto>();
  103. for (var i = 0; i < processes.Count(); i++)
  104. {
  105. var casDto = new CascaderDto()
  106. {
  107. Key = processes.ElementAt(i).ID,
  108. Title = processes.ElementAt(i).FCode,
  109. Label = processes.ElementAt(i).FCode,
  110. Value = processes.ElementAt(i).FCode,
  111. Group = true,
  112. IsLeaf = false
  113. };
  114. var children = macmodels.Where(c => c.TPCode == casDto.Value);
  115. var cList = new List<CascaderDto>();
  116. if (children != null && children.Count() > 0)
  117. {
  118. foreach (var item in children)
  119. {
  120. if (cList.Count == 0 || cList.FirstOrDefault(c => c.Value == item.FCode) == null)
  121. {
  122. cList.Add(new CascaderDto
  123. {
  124. Key = item.ID,
  125. Title = item.FCode,
  126. Label = item.FCode,
  127. Value = item.FCode,
  128. Children = null,
  129. IsLeaf = true,
  130. });
  131. }
  132. }
  133. casDto.Children = cList;
  134. }
  135. else
  136. {
  137. casDto.IsLeaf = true;
  138. }
  139. list.Add(casDto);
  140. }
  141. return list;
  142. }
  143. #region Web展示机台
  144. public IEnumerable<Machine> Get(int start, int length, string order, string sort, string filter, ref string errorinfo)
  145. {
  146. var pros = CurrDb.FindListForCondition<Machine>($" {commonFilter + filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo);
  147. if (pros != null && pros.Count() > 0)
  148. {
  149. var regionDal = new FactoryRegionDal(CurrDb);
  150. var allRegions = regionDal.Get(1, 10000, "ID", "asc", string.Empty, errorinfo);
  151. foreach (var item in pros)
  152. {
  153. item.RegionName = regionDal.GetFullRegionName(item.RegionId, allRegions);
  154. }
  155. }
  156. return pros;
  157. }
  158. public int GetCount(string filter)
  159. {
  160. string errorinfo = string.Empty;
  161. //string sql = $"select count(1) from ProgramRule a where 1=1 {filter}";
  162. var entities = CurrDb.FindListForCondition<Machine>(commonFilter + filter, ref errorinfo);
  163. if (entities != null)
  164. {
  165. return entities.GroupBy(c => c.FCode).Count();
  166. }
  167. return 0;
  168. }
  169. public Machine Get(int id)
  170. {
  171. var pro = CurrDb.FindEntityFor<Machine>(id);
  172. return pro;
  173. }
  174. public Machine GetMac(string filter)
  175. {
  176. try
  177. {
  178. string sql = $@"SELECT A.FCode,A.IPAddress,A.ID,B.ProRootPath,A.MacAddress,A.RegionId,g.FCode PCode,a.MModeID
  179. from machine as A
  180. LEFT JOIN macmodel as B on A.MModeID = B.id
  181. left outer join mactprocess f on a.id =f.macid
  182. left outer join tprocess g on f.PCode =g.FCode
  183. WHERE a.MacAddress = '{filter}'";
  184. var entities = CurrDb.FindList<Machine>(sql).FirstOrDefault();
  185. return entities;
  186. }
  187. catch (Exception ex)
  188. {
  189. return null;
  190. }
  191. }
  192. public string GetMacType(string macCode)
  193. {
  194. try
  195. {
  196. string sql = $@"select Remark from MacStatus01 where MacCode='{macCode}'";
  197. var entities = CurrDb.FindList<MacStatus01>(sql).FirstOrDefault();
  198. return entities?.Remark;
  199. }
  200. catch (Exception ex)
  201. {
  202. return null;
  203. }
  204. }
  205. /// <summary>
  206. /// 添加机台并返回机台Id
  207. /// </summary>
  208. /// <param name="role"></param>
  209. /// <param name="userCode"></param>
  210. /// <returns></returns>
  211. public int Add(Machine pro, string userCode, ref string errorinfo)
  212. {
  213. var entities = CurrDb.FindListForCondition<Machine>($" and a.FCode='{pro.FCode}' ", ref errorinfo);
  214. if (entities != null && entities.Count() > 0)
  215. {
  216. errorinfo = "机台已存在,请确认";
  217. return -1;
  218. }
  219. pro.FName = string.IsNullOrEmpty(pro.FName) ? pro.FCode : pro.FName;
  220. // AOI机台排除与当前机台成对的另一个机台
  221. if (pro.FCode.StartsWith("LXJC"))
  222. {
  223. entities = CurrDb.FindListForCondition<Machine>($"and a.IpAddress='{pro.IPAddress}' " +
  224. $"and a.FCode<>'{pro.FCode.Substring(0, pro.FCode.Length - 1) + "L"}' " +
  225. $"and a.FCode<>'{pro.FCode.Substring(0, pro.FCode.Length - 1) + "R"}'", ref errorinfo);
  226. }
  227. else
  228. {
  229. entities = CurrDb.FindListForCondition<Machine>($" and a.IpAddress='{pro.IPAddress}'", ref errorinfo);
  230. }
  231. if (entities != null && entities.Count() > 0)
  232. {
  233. errorinfo = $"IP地址与机台[{entities.First().FCode}]重复,请确认";
  234. return -1;
  235. }
  236. if (pro.FCode.StartsWith("LXJC"))
  237. {
  238. entities = CurrDb.FindListForCondition<Machine>($"and a.MacAddress='{pro.MacAddress}' " +
  239. $"and a.FCode<>'{pro.FCode.Substring(0, pro.FCode.Length - 1) + "L"}' " +
  240. $"and a.FCode<>'{pro.FCode.Substring(0, pro.FCode.Length - 1) + "R"}'", ref errorinfo);
  241. }
  242. else
  243. {
  244. entities = CurrDb.FindListForCondition<Machine>($" and a.MacAddress='{pro.MacAddress}'", ref errorinfo);
  245. }
  246. if (entities != null && entities.Count() > 0)
  247. {
  248. errorinfo = $"MAC地址与机台[{entities.First().FCode}]重复,请确认";
  249. return -1;
  250. }
  251. pro.RecCode = userCode;
  252. pro.ModCode = userCode;
  253. pro.RecTime = DateTime.Now;
  254. pro.ModTime = DateTime.Now;
  255. pro.FPort = pro.FPort ?? 5001;
  256. pro.DeviceID = pro.DeviceID ?? 0;
  257. pro.IsConn = pro.IsConn ?? 1;
  258. pro.IsControl = pro.IsControl ?? -1;
  259. pro.IsAnalysis = pro.IsAnalysis ?? 1;
  260. pro.CallPMode = pro.CallPMode ?? 1;
  261. pro.IsRemoveProgram = pro.IsRemoveProgram ?? -1;
  262. if (pro.RegionId != 0)
  263. {
  264. var regionDal = new FactoryRegionDal(CurrDb);
  265. var factoryId = regionDal.GetRoot(pro.RegionId).Id;
  266. pro.FactoryId = factoryId;
  267. }
  268. string sql = $"insert into Machine(MModeID,FCode,FName,IPAddress,FPort,SupplierID,DeviceID,T3,T5,T6,T7,T8,Remark,RecCode,RecTime," +
  269. $"ModCode,ModTime,ScanIp,CallPMode,LocDir," +
  270. $"MacAddress,RegionId,FactoryId) values('{pro.MModeID}','{pro.FCode}','{pro.FName}','{pro.IPAddress}','{pro.FPort}','{pro.SupplierID}'," +
  271. $"'{pro.DeviceID}','{pro.T3}','{pro.T5}','{pro.T6}','{pro.T7}','{pro.T8}','{pro.Remark}','{pro.RecCode}'," +
  272. $"'{pro.RecTime.ToString("yyyy-MM-dd HH:mm:ss")}','{pro.ModCode}','{pro.ModTime.ToString("yyyy-MM-dd HH:mm:ss")}','{pro.ScanIp}','{pro.CallPMode}','{pro.LocDir}'," +
  273. $"'{pro.MacAddress}','{pro.RegionId}','{pro.FactoryId}');";
  274. sql += "select @@identity;";
  275. var id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  276. if (id < 0)
  277. {
  278. errorinfo = "机台信息新增失败";
  279. return -1;
  280. }
  281. // 插入制程关系
  282. if (!string.IsNullOrEmpty(pro.PCode))
  283. {
  284. sql = "insert into mactprocess(MacID,PCode,reccode,rectime,modcode,modtime) " +
  285. $"values('{id}','{pro.PCode}','{userCode}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}'," +
  286. $"'{userCode}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
  287. if (CurrDb.ExecuteBySql(sql) < 0)
  288. {
  289. errorinfo = "插入制程信息失败";
  290. return -1;
  291. }
  292. }
  293. if (!string.IsNullOrEmpty(pro.macnumber))
  294. {
  295. MachineNumber m = new MachineNumber();
  296. m.MacNumber = pro.macnumber;
  297. m.RecCode = userCode;
  298. m.MacId = id;
  299. if (CurrDb.InsertFor<MachineNumber>(m, pro.RecCode) < 0)
  300. {
  301. errorinfo = "机台编号添加失败";
  302. return -1;
  303. }
  304. }
  305. return id;
  306. }
  307. /// <summary>
  308. /// 修改机台信息
  309. /// </summary>
  310. /// <param name="role"></param>
  311. /// <param name="userCode"></param>
  312. /// <param name="errorinfo"></param>
  313. /// <returns></returns>
  314. public int Update(Machine role, string userCode, ref string errorinfo)
  315. {
  316. var entities = CurrDb.FindListForCondition<Machine>($" and a.FCode='{role.FCode}' " +
  317. $"and a.ID<>{role.ID}", ref errorinfo);
  318. if (entities != null && entities.Count() > 0)
  319. {
  320. errorinfo = "已存在相同的机台,请确认";
  321. return -1;
  322. }
  323. if (role.FCode.StartsWith("LXJC"))
  324. {
  325. entities = CurrDb.FindListForCondition<Machine>($"and a.ID<>{role.ID} and a.IpAddress='{role.IPAddress}' " +
  326. $"and a.FCode<>'{role.FCode.Substring(0, role.FCode.Length - 1) + "L"}' " +
  327. $"and a.FCode<>'{role.FCode.Substring(0, role.FCode.Length - 1) + "R"}'", ref errorinfo);
  328. }
  329. else
  330. {
  331. entities = CurrDb.FindListForCondition<Machine>($" and a.ID<>{role.ID} and a.IpAddress='{role.IPAddress}'", ref errorinfo);
  332. }
  333. if (entities != null && entities.Count() > 0)
  334. {
  335. errorinfo = $"IP地址与机台[{entities.First().FCode}]重复,请确认";
  336. return -1;
  337. }
  338. if (role.FCode.StartsWith("LXJC"))
  339. {
  340. entities = CurrDb.FindListForCondition<Machine>($"and a.ID<>{role.ID} and a.MacAddress='{role.MacAddress}' " +
  341. $"and a.FCode<>'{role.FCode.Substring(0, role.FCode.Length - 1) + "L"}' " +
  342. $"and a.FCode<>'{role.FCode.Substring(0, role.FCode.Length - 1) + "R"}'", ref errorinfo);
  343. }
  344. else
  345. {
  346. entities = CurrDb.FindListForCondition<Machine>($" and a.ID<>{role.ID} and a.MacAddress='{role.MacAddress}'", ref errorinfo);
  347. }
  348. if (entities != null && entities.Count() > 0)
  349. {
  350. errorinfo = $"MAC地址与机台[{entities.First().FCode}]重复,请确认";
  351. return -1;
  352. }
  353. var entity = CurrDb.FindEntityFor<Machine>(role.ID);
  354. entity.FCode = role.FCode;
  355. entity.FName = role.FName ?? entity.FName;
  356. entity.CallPMode = role.CallPMode ?? entity.CallPMode;
  357. entity.DeviceID = role.DeviceID ?? entity.DeviceID;
  358. entity.FPort = role.FPort ?? entity.FPort;
  359. entity.FVersion = role.FVersion ?? entity.FVersion;
  360. entity.IPAddress = role.IPAddress;
  361. entity.LocDir = role.LocDir ?? entity.LocDir;
  362. entity.MacAddress = role.MacAddress;
  363. entity.MModeID = role.MModeID;
  364. entity.IsConn = role.IsConn ?? entity.IsConn;
  365. entity.IsAnalysis = role.IsAnalysis ?? entity.IsAnalysis;
  366. entity.IsControl = role.IsControl ?? entity.IsControl;
  367. entity.IsRemoveProgram = role.IsRemoveProgram ?? entity.IsRemoveProgram;
  368. entity.ProRootPath = role.ProRootPath ?? entity.ProRootPath;
  369. entity.Remark = role.Remark ?? entity.Remark;
  370. entity.ModCode = userCode;
  371. entity.ModTime = DateTime.Now;
  372. entity.macnumber = role.macnumber ?? entity.macnumber;
  373. // var autoReconnct = false;
  374. if (role.RegionId != 0)
  375. {
  376. var regionDal = new FactoryRegionDal(CurrDb);
  377. var factoryId = regionDal.GetRoot(role.RegionId).Id;
  378. // autoReconnct = entity.FactoryId != factoryId ? true : false;
  379. //if (entity.IsConn == -1)
  380. //{
  381. // autoReconnct = false;
  382. //}
  383. entity.FactoryId = factoryId;
  384. entity.RegionId = role.RegionId;
  385. }
  386. if (CurrDb.UpdateFor(entity, userCode) < 0)
  387. {
  388. return -1;
  389. }
  390. // 更新制程关系
  391. if (!string.IsNullOrEmpty(role.PCode))
  392. {
  393. var sql = $"delete from mactprocess where macid='{role.ID}'";
  394. if (CurrDb.ExecuteBySql(sql) < 0)
  395. {
  396. errorinfo = "更新制程信息失败";
  397. return -1;
  398. }
  399. sql = "insert into mactprocess(MacID,PCode,reccode,rectime,modcode,modtime) " +
  400. $"values('{role.ID}','{role.PCode}','{userCode}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}'," +
  401. $"'{userCode}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
  402. if (CurrDb.ExecuteBySql(sql) < 0)
  403. {
  404. errorinfo = "更新制程信息失败";
  405. return -1;
  406. }
  407. }
  408. if (true)
  409. {
  410. MachineNumber m = new MachineNumber();
  411. List<MachineNumber> list = CurrDb.FindList<MachineNumber>($"select * from machinenumber where macid={role.ID}")
  412. .ToList();
  413. if (list.Count > 0)
  414. {
  415. m = list[0];
  416. if (m.MacNumber != entity.macnumber)
  417. {
  418. m.MacNumber = entity.macnumber;
  419. m.ModCode = userCode;
  420. if (CurrDb.UpdateFor<MachineNumber>(m, userCode) < 0)
  421. {
  422. errorinfo = "机台编号修改失败";
  423. return -1;
  424. }
  425. }
  426. }
  427. else
  428. {
  429. if (!string.IsNullOrEmpty(entity.macnumber))
  430. {
  431. m.MacId = role.ID;
  432. m.MacNumber = role.macnumber;
  433. if (CurrDb.InsertFor<MachineNumber>(m, userCode) < 0)
  434. {
  435. errorinfo = "机台编号修改失败";
  436. return -1;
  437. }
  438. }
  439. }
  440. }
  441. return role.ID;
  442. }
  443. public int DoAutoReconnect(Machine preMac, int macId, ref string errorinfo)
  444. {
  445. if (preMac.ID == 0)
  446. {
  447. return this.AutoReconnet(macId, ref errorinfo, preMac.FCode);
  448. }
  449. var shouldReconnct = false;
  450. var regionDal = new FactoryRegionDal(CurrDb);
  451. var preFactoryId = regionDal.GetRoot(preMac.RegionId).Id;
  452. var currMac = CurrDb.FindEntityFor<Machine>(macId);
  453. var currFactoryId = regionDal.GetRoot(currMac.RegionId).Id;
  454. if (preFactoryId != currFactoryId || preMac.FCode.ToUpper() != currMac.FCode.ToUpper()
  455. || preMac.IPAddress != currMac.IPAddress || preMac.MModeID != currMac.MModeID)
  456. {
  457. shouldReconnct = true;
  458. }
  459. if (shouldReconnct)
  460. {
  461. return this.AutoReconnet(macId, ref errorinfo, preMac.FCode);
  462. }
  463. return 1;
  464. }
  465. /// <summary>
  466. /// 自动重连
  467. /// </summary>
  468. /// <returns></returns>
  469. public int AutoReconnet(int macId, ref string errorinfo, string preMacCode = null,
  470. bool moveMacServer = true)
  471. {
  472. using (IDatabase db = DbFactory.Base("eap"))
  473. {
  474. db.BeginTrans();
  475. var mac = db.FindEntityFor<Machine>(macId);
  476. // 当机台设置成不连接时直接返回
  477. //if (mac.IsConn == -1)
  478. // return 1;
  479. var macOrderDal = new MacOrderDal(db);
  480. // 检查指令是否正确
  481. if (macOrderDal.BindOrders(mac, mac.ModCode, ref errorinfo) < 0)
  482. {
  483. db.Rollback();
  484. return -1;
  485. }
  486. // 关闭现有连接
  487. AppserverHelper appServerHelper = new AppserverHelper();
  488. if (appServerHelper.StopMachine(db, new Machine() { ID = macId, FCode = preMacCode }, ref errorinfo) < 0)
  489. {
  490. db.Commit();
  491. return -2;
  492. }
  493. if (moveMacServer)
  494. {
  495. // 将机台移动到对应园区的AP服务器上
  496. var serverDal = new EapAppServerDal(db);
  497. var server = serverDal.GetMacLeastServer(mac);
  498. if (server == null)
  499. {
  500. errorinfo = "未找到当前园区对应的AP服务器";
  501. db.Commit();
  502. return -1;
  503. }
  504. var serverMacDal = new EapAppServerMacDal(db);
  505. if (serverMacDal.Update(mac.ID, server.ID, mac.ModCode, ref errorinfo) < 0)
  506. {
  507. db.Rollback();
  508. return -1;
  509. }
  510. db.Commit();
  511. // 刷新RMS服务
  512. if (appServerHelper.RmsRefreshOrders(db, mac.FCode, ref errorinfo) < 0)
  513. return -2;
  514. }
  515. // 重新打开连接
  516. if (appServerHelper.StartMachine(db, mac.FCode, ref errorinfo) < 0)
  517. return -2;
  518. }
  519. return 1;
  520. }
  521. /// <summary>
  522. /// 修改真实ip
  523. /// </summary>
  524. /// <param name="role"></param>
  525. /// <param name="errorinfo"></param>
  526. /// <returns></returns>
  527. public int UpdateRealIP(Machine role, ref string errorinfo)
  528. {
  529. string filter = $" and a.RealIP='{role.RealIP}' and AppletV='{role.AppletV}' and a.id={role.ID}";
  530. var mdoels = CurrDb.FindListForCondition<Machine>(filter, ref errorinfo);
  531. if (mdoels != null && mdoels.Count() > 0)
  532. {
  533. // LogHelper<Machine>.LogFatal("Machine真实IP,版本号与采集到的一致-->filter:" + filter, "小程序操作", "none");
  534. }
  535. else
  536. {
  537. string sql = $" update machine set RealIP='{role.RealIP}',AppletV='{role.AppletV}' where id={role.ID}";
  538. if (CurrDb.ExecuteBySql(sql) < 0)
  539. {
  540. return -1;
  541. }
  542. //LogHelper<Machine>.LogFatal("修改Machine真实IP,版本号-->sql:" + sql, "小程序操作", "none");
  543. }
  544. string insert_sql = $@"INSERT INTO ofappletlog ( macCode, AppletV, AppletS )VALUES( '{role.FCode}', '{role.AppletV}', {1});";
  545. CurrDb.ExecuteBySql(insert_sql);
  546. return role.ID;
  547. }
  548. public int UpdateRealIPForMac(Machine ma, ref string errorinfo)
  549. {
  550. try
  551. {
  552. var entities = CurrDb.FindListForCondition<Machine>($" and locate('{ma.MacAddress}',MacAddress)>0 ", ref errorinfo);
  553. if (entities != null && entities.Count() > 0)//存在则更新RealIP
  554. {
  555. string filter = $" and a.RealIP='{ma.RealIP}' and a.id={ma.ID}";
  556. var mdoels = CurrDb.FindListForCondition<Machine>(filter, ref errorinfo);
  557. if (mdoels != null && mdoels.Count() > 0)
  558. {
  559. //LogHelper<Machine>.LogFatal("Machine真实IP与采集到的一致-->filter:" + filter, "小程序操作", "none");
  560. }
  561. else
  562. {
  563. string sql = $" update machine set RealIP='{ma.RealIP}' where locate('{ma.MacAddress}',MacAddress)>0";
  564. if (CurrDb.ExecuteBySql(sql) < 0)
  565. {
  566. errorinfo = $"mac地址为:{ma.MacAddress}机台的RealIP{ma.RealIP}更新失败!";
  567. return -1;
  568. }
  569. }
  570. }
  571. else//否则新增一条此MAC\RealIP的机台
  572. {
  573. string NewID = "";
  574. String Unsql = $" SELECT max(right(FCode,5)) from machine where substr(FCode,1,6)='Unknow' ";
  575. object Unid = CurrDb.FindObject(Unsql);
  576. if (Unid.ToString() == "")
  577. {
  578. NewID = "Unknow00001";
  579. }
  580. else
  581. {
  582. NewID = (int.Parse(Unid.ToString()) + 1).ToString("00000");
  583. }
  584. string sql = $"insert into Machine(FCode,RecTime,ModTime,MacAddress,RealIP) values('Unknow{NewID}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}','{ma.MacAddress}','{ma.RealIP}') ";
  585. if (CurrDb.ExecuteBySql(sql) < 0)
  586. {
  587. errorinfo = $"机台号为:{NewID},mac地址为:{ma.MacAddress}机台的RealIP{ma.RealIP}新增失败!";
  588. return -1;
  589. }
  590. }
  591. return 1;
  592. }
  593. catch (Exception ex)
  594. {
  595. errorinfo = "UpdateRealIPForMac报错:" + ex.Message;
  596. return -1;
  597. }
  598. }
  599. public int Delete(int id, ref string msg)
  600. {
  601. var sql = $"delete from staffmachine where macid={id}";
  602. if (CurrDb.ExecuteBySql(sql) < 0)
  603. {
  604. msg = "删除已绑定该机台的相关员工数据时发生错误";
  605. return -1;
  606. }
  607. sql = $"delete from eapappservermac where macid={id}";
  608. if (CurrDb.ExecuteBySql(sql) < 0)
  609. {
  610. msg = "将机台从绑定的AP服务器中移除失败";
  611. return -1;
  612. }
  613. sql = $"delete from mactprocess where macid={id}";
  614. if (CurrDb.ExecuteBySql(sql) < 0)
  615. {
  616. msg = "移除机台制程关系失败";
  617. return -1;
  618. }
  619. sql = $"delete from macorder where macid={id}";
  620. if (CurrDb.ExecuteBySql(sql) < 0)
  621. {
  622. msg = "移除机台指令失败";
  623. return -1;
  624. }
  625. if (CurrDb.DeleteFor<Machine>(id) < 0)
  626. {
  627. msg = "删除失败";
  628. return -1;
  629. }
  630. msg = string.Empty;
  631. return 1;
  632. }
  633. public int ChangeIsControl(string field, int id, int value)
  634. {
  635. string errorinfo = string.Empty;
  636. string sql = string.Format($"update machine set {field}={value} where id={id}");
  637. int count = CurrDb.ExecuteBySql(sql);
  638. if (count > 0)
  639. {
  640. LogHelper<Machine>.LogFatal("修改Machine-->sql:" + sql, "用户操作", "none");
  641. if (field.ToLower() == "isconn" && value == 1)
  642. {
  643. //var mac = CurrDb.FindEntityFor<Machine>(id);
  644. //this.AutoReconnet(mac, ref errorinfo);
  645. }
  646. }
  647. return count;
  648. }
  649. public IEnumerable<Machine> Get(string filter)
  650. {
  651. string error = string.Empty;
  652. var entities = CurrDb.FindListForCondition<Machine>(filter, ref error);
  653. var regionDal = new FactoryRegionDal(CurrDb);
  654. var allRegions = regionDal.Get(1, 10000, "ID", "asc", string.Empty, error);
  655. var machineNm = CurrDb.FindListForCondition<MachineNumber>("", ref error);
  656. foreach (var item in entities)
  657. {
  658. item.RegionName = regionDal.GetFullRegionName(item.RegionId, allRegions);
  659. item.macnumber = machineNm.Where(c => c.MacId == item.ID).Select(c => c.MacNumber).FirstOrDefault();
  660. }
  661. return entities;
  662. }
  663. /// <summary>
  664. /// 获取机台多选框
  665. /// </summary>
  666. /// <param name="filter"></param>
  667. /// <returns></returns>
  668. public IEnumerable<SelectDto<int>> GetMultipleSelects(string filter)
  669. {
  670. var sql = "select a.id value,a.FCode label from machine a " +
  671. "left join factoryregion b on a.regionid=b.id " +
  672. "left join factoryregion c on b.parentid=c.id " +
  673. "left join factoryregion d on c.parentid=d.id " +
  674. $" where 1=1 {filter}";
  675. var entities = CurrDb.FindList<SelectDto<int>>(sql);
  676. return entities;
  677. }
  678. public int BindMachine(string pCode, IEnumerable<string> macIds, string userCode, ref string errorinfo)
  679. {
  680. string sql = string.Format($"delete from mactprocess where pcode='{pCode}'");
  681. CurrDb.ExecuteBySql(sql);
  682. if (macIds != null && macIds.Count() > 0)
  683. {
  684. foreach (var item in macIds)
  685. {
  686. var mactprocess = new DllEapEntity.Rms.MacTProcess
  687. {
  688. MacID = item,
  689. modcode = userCode,
  690. PCode = pCode,
  691. reccode = userCode,
  692. modtime = DateTime.Now,
  693. rectime = DateTime.Now
  694. };
  695. if (CurrDb.InsertFor<DllEapEntity.Rms.MacTProcess>(mactprocess, userCode) < 0)
  696. {
  697. errorinfo = "修改绑定机台失败";
  698. return -1;
  699. }
  700. }
  701. }
  702. return 1;
  703. }
  704. public int BindMachineToPM(int pmId, IEnumerable<string> macIds, string userCode, ref string errorinfo)
  705. {
  706. string sql = string.Format($"delete from pmmachine where pmid='{pmId}'");
  707. CurrDb.ExecuteBySql(sql);
  708. if (macIds != null && macIds.Count() > 0)
  709. {
  710. foreach (var item in macIds)
  711. {
  712. var mactprocess = new DllEapEntity.PM.PMMachine
  713. {
  714. MacID = Convert.ToInt32(item),
  715. ModCode = userCode,
  716. PMID = pmId,
  717. StartTime = DateTime.Now,
  718. RecCode = userCode,
  719. ModTime = DateTime.Now,
  720. RecTime = DateTime.Now
  721. };
  722. if (CurrDb.InsertFor<DllEapEntity.PM.PMMachine>(mactprocess, userCode) < 0)
  723. {
  724. errorinfo = "修改绑定机台失败";
  725. return -1;
  726. }
  727. }
  728. }
  729. return 1;
  730. }
  731. public IEnumerable<CascaderDto> GetGroupedMachines(int? facotryId)
  732. {
  733. string errorinfo = string.Empty;
  734. var macmodelDal = new MacModelDal(CurrDb);
  735. var macmodels = macmodelDal.GetSecMacModels();
  736. var filter = string.Empty;
  737. if (facotryId != null)
  738. {
  739. filter += $" and a.factoryId={facotryId}";
  740. }
  741. var sql = $@"select a.*,b.FCode as FModel from Machine a
  742. left join Macmodel b on a.MModeID=b.ID
  743. where 1=1 {filter}";
  744. var machines = CurrDb.FindList<SecMachine>(sql);
  745. var list = new List<CascaderDto>();
  746. for (var i = 0; i < macmodels.Count(); i++)
  747. {
  748. if (list.FirstOrDefault(c => c.Value == macmodels.ElementAt(i).FCode) != null)
  749. continue;
  750. var casDto = new CascaderDto()
  751. {
  752. Key = macmodels.ElementAt(i).FCode,
  753. Title = macmodels.ElementAt(i).FCode,
  754. Label = macmodels.ElementAt(i).FCode,
  755. Value = macmodels.ElementAt(i).FCode,
  756. Group = true,
  757. IsLeaf = false
  758. };
  759. var children = machines.Where(c => c.FModel == casDto.Value);
  760. var cList = new List<CascaderDto>();
  761. if (children != null && children.Count() > 0)
  762. {
  763. foreach (var item in children)
  764. {
  765. if (cList.Count == 0 || cList.FirstOrDefault(c => c.Value == item.FCode) == null)
  766. {
  767. cList.Add(new CascaderDto
  768. {
  769. Key = item.FCode,
  770. Title = item.FCode,
  771. Label = item.FCode,
  772. Value = item.FCode,
  773. Children = null,
  774. IsLeaf = true,
  775. });
  776. }
  777. }
  778. casDto.Children = cList;
  779. }
  780. else
  781. {
  782. casDto.IsLeaf = true;
  783. }
  784. list.Add(casDto);
  785. }
  786. return list;
  787. }
  788. public IEnumerable<Machine> GetUnAuthorizedMachines(string filter, ref string errorinfo)
  789. {
  790. return CurrDb.FindListForCondition<Machine>(filter, ref errorinfo);
  791. }
  792. /// <summary>
  793. /// 根据员工工号获取已授权的机台
  794. /// </summary>
  795. /// <param name="staffCode"></param>
  796. /// <returns></returns>
  797. public IEnumerable<string> GetAuthedMacs(string staffCode, ref string errorinfo)
  798. {
  799. return CurrDb.FindListForCondition<StaffMachine>($" and a.StaffCode='{staffCode}'", ref errorinfo)
  800. .Select(c => c.MacCode);
  801. }
  802. /// <summary>
  803. /// 授权机台
  804. /// </summary>
  805. /// <param name="staffCode">员工工号</param>
  806. /// <param name="macIds">机台编号</param>
  807. /// <param name="userCode">操作人</param>
  808. /// <param name="errorinfo">错误信息</param>
  809. /// <returns></returns>
  810. public int SetMacs(string staffCode, IEnumerable<int> macIds, string userCode, ref string errorinfo)
  811. {
  812. string sql = $"delete from StaffMachine where StaffCode='{staffCode}'";
  813. if (CurrDb.ExecuteBySql(sql) < 0)
  814. {
  815. errorinfo = "删除原数据失败";
  816. return -1;
  817. }
  818. if (macIds != null && macIds.Count() > 0)
  819. {
  820. foreach (var item in macIds)
  821. {
  822. sql = $@"insert into StaffMachine(StaffCode,MacId,RecCode,RecTime,ModCode,ModTime)
  823. values('{staffCode}','{item}','{userCode}',
  824. '{DateTime.Now.ToString("yyyy-MM-dd")}','{userCode}',
  825. '{DateTime.Now.ToString("yyyy-MM-dd")}')";
  826. if (CurrDb.ExecuteBySql(sql) < 0)
  827. {
  828. errorinfo = "插入新纪录失败";
  829. return -1;
  830. }
  831. }
  832. }
  833. return 1;
  834. }
  835. /// <summary>
  836. /// 批量设置是否管控等参数
  837. /// </summary>
  838. public int SetControlByTrans(IEnumerable<int> ids, string fileld, string value, ref string errorinfo)
  839. {
  840. var idFilter = $" and id in ({string.Join(",", ids.Select(c => c))})";
  841. var sql = $"Update machine set {fileld}={value} where 1=1 {idFilter}";
  842. if (CurrDb.ExecuteBySql(sql) < 0)
  843. {
  844. errorinfo = "更新数据库失败";
  845. return -1;
  846. }
  847. return 1;
  848. }
  849. public IEnumerable<Machine> GetMachineList()
  850. {
  851. string sql = $@"select a.*,b.FCode MModeCode,b.FName MModeName,c.FName FactoryName,d.FName RegionName,
  852. e.FName Floor,g.FCode PCode,
  853. f.FName as plantName,f.id as plantId,e.id as FloorId,d.FName as LineName
  854. from machine a
  855. left join macmodel b on a.MModeID=b.Id
  856. left outer join mactprocess h on a.id =h.macid
  857. left outer join tprocess g on h.PCode =g.FCode
  858. left join factoryregion c on a.factoryId = c.id
  859. left join factoryregion d on a.regionId=d.id
  860. left join factoryregion e on d.parentid=e.id
  861. left join factoryregion f on e.parentid=f.id
  862. where g.FCode is not null
  863. order by a.FCode ";
  864. return CurrDb.FindList<Machine>(sql);
  865. }
  866. /// <summary>
  867. /// 导出Excel
  868. /// </summary>
  869. /// <param name="filter"></param>
  870. /// <returns></returns>
  871. public async Task<IWorkbook> Export(string filter)
  872. {
  873. string errorinfo = string.Empty;
  874. var pros = CurrDb.FindListForCondition<Machine>($" {commonFilter + filter} order by a.fcode", ref errorinfo);
  875. if (pros != null && pros.Count() > 0)
  876. {
  877. var regionDal = new FactoryRegionDal(CurrDb);
  878. var allRegions = regionDal.Get(1, 10000, "ID", "asc", string.Empty, errorinfo);
  879. foreach (var item in pros)
  880. {
  881. item.RegionName = regionDal.GetFullRegionName(item.RegionId, allRegions);
  882. }
  883. }
  884. var list = _mapper.Map<IEnumerable<MachineExportDto>>(pros);
  885. var book = DataExportHelper.EntityToExcel(list.ToList());
  886. await Task.CompletedTask;
  887. return book;
  888. }
  889. #endregion
  890. }
  891. }