MacRecipeDal.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. using Cksoft.Data;
  2. using Cksoft.Unity;
  3. using DllEapCommon.Regex;
  4. using DllEapEntity;
  5. using DllEapEntity.OFILM;
  6. using DllEapEntity.Rms;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. namespace DllEapDal.OFILM
  14. {
  15. public class MacRecipeDal
  16. {
  17. private IDatabase CurrDb = null;
  18. public MacRecipeDal(IDatabase db)
  19. {
  20. CurrDb = db;
  21. }
  22. public IEnumerable<MacRecipeDto> GetMacRecipeDtos(string filter, int start, int length, string sort, string order, out int total, ref string errorinfo)
  23. {
  24. var countSql = $@"select count(1) from machine a
  25. left join macstatus01 b on a.FCode = b.maccode
  26. left join mactprocess c on a.id = c.macid
  27. left join standardstatus d on b.statusid = d.StatusVal
  28. left join factoryregion f on a.factoryId = f.id
  29. where 1=1 {filter}";
  30. total = Convert.ToInt32(CurrDb.FindList<string>(countSql).FirstOrDefault() ?? "0");
  31. var sql = $@"select a.ID MacId,a.FCode maccode,a.RegionId regionId,c.pcode,b.remark programName,a.realip,a.macaddress,
  32. ifnull(d.FName,'离线') statusName,ifnull(d.FCode,'DISCONN') statusCode,f.FName factory,a.ipaddress,a.fVersion,
  33. ifnull(d.red,178) red ,ifnull(d.green,178) green,ifnull(d.blue,178) blue,g.fname floor,b.StatusID,b.STime ,l.FName PlantName,h.FName Line
  34. from machine a
  35. left join macstatus01 b on a.FCode=b.maccode
  36. left join mactprocess c on a.id=c.macid
  37. left join standardstatus d on b.statusid=d.StatusVal
  38. left join factoryregion f on a.factoryId = f.id
  39. left join factoryregion h on a.regionId=h.id
  40. left join factoryregion g on h.parentid=g.id
  41. left outer join factoryregion l on g.parentid =l.id
  42. where 1=1 {filter}
  43. order by {sort} {order} limit {start - 1},{length}";
  44. var datas = CurrDb.FindList<MacRecipeDto>(sql);
  45. if (datas != null && datas.Count() > 0)
  46. {
  47. sql = "select * from machine where realip in (select realip from machine group by realip having count(1)>1)";
  48. var wrongs = CurrDb.FindList<Machine>(sql);
  49. foreach (var item in datas)
  50. {
  51. if (wrongs.Any(c => c.ID == item.MacId))
  52. {
  53. item.RealIpState = -1;
  54. }
  55. else
  56. {
  57. item.RealIpState = 1;
  58. }
  59. }
  60. }
  61. return datas;
  62. }
  63. public IEnumerable<MacRecipeDto> GetMacStatusDtos(string filter, int start, int length, string sort, string order, out int total, ref string errorinfo)
  64. {
  65. var countSql = $@"select count(1) from machine a
  66. left join macstatus b on a.FCode = b.maccode
  67. left join mactprocess c on a.id = c.macid
  68. left join standardstatus d on b.statusid = d.StatusVal
  69. left join factoryregion f on a.factoryId = f.id
  70. where 1=1 {filter}";
  71. total = Convert.ToInt32(CurrDb.FindList<string>(countSql).FirstOrDefault() ?? "0");
  72. var sql = $@"select a.ID MacId,a.FCode maccode,a.RegionId regionId,c.pcode,b.remark programName,a.realip,a.macaddress,
  73. ifnull(d.FName,'离线') statusName,ifnull(d.FCode,'DISCONN') statusCode,f.FName factory,a.ipaddress,
  74. ifnull(d.red,178) red ,ifnull(d.green,178) green,ifnull(d.blue,178) blue,g.fname floor,b.StatusID,b.STime,b.ETime
  75. from machine a
  76. left join macstatus b on a.FCode=b.maccode
  77. left join mactprocess c on a.id=c.macid
  78. left join standardstatus d on b.statusid=d.StatusVal
  79. left join factoryregion f on a.factoryId = f.id
  80. left join factoryregion h on a.regionId=h.id
  81. left join factoryregion g on h.parentid=g.id
  82. where 1=1 {filter}
  83. order by {sort} {order} limit {start - 1},{length}";
  84. var datas = CurrDb.FindList<MacRecipeDto>(sql);
  85. return datas;
  86. }
  87. public IEnumerable<MachineInfo> GetMachineInfoes(string filter, int start, int length, string sort, string order, out int total, ref string errorinfo)
  88. {
  89. var recipeStr = OfilmRecipeProvider.GenRecipeString("b");
  90. var countSql = $@"select count(1) from machine a
  91. left join macstatus01 b on a.FCode = b.maccode
  92. left join macmodel l on a.mmodeid=l.id
  93. left join mactprocess c on a.id = c.macid
  94. left join standardstatus d on b.statusid = d.StatusVal
  95. left join factoryregion f on a.factoryId = f.id
  96. LEFT JOIN machinenumber ON a.ID = machinenumber.MacId
  97. where 1=1 {filter}";
  98. total = Convert.ToInt32(CurrDb.FindList<string>(countSql).FirstOrDefault() ?? "0");
  99. var sql = $@"select a.ID MacId,a.FCode maccode,machinenumber.MacNumber macnumber,a.fName macName,a.RegionId regionId,c.pcode,b.remark programName,a.realip,a.macaddress,
  100. m.id SupplierId,m.FCode SupplierCode,l.id MacModelId,a.LocDir LocDir,a.DeviceID DeviceID,a.FPort FPort,
  101. a.CallPMode CallPMode,a.Remark Remark,
  102. ifnull(d.FName,'离线') statusName,ifnull(d.FCode,'DISCONN') statusCode,f.FName factory,a.ipaddress,
  103. ifnull(d.red,178) red ,ifnull(d.green,178) green,ifnull(d.blue,178) blue,g.fname floor,h.fname line,i.fname plant,b.StatusID,b.STime,b.ETime,
  104. ifnull(j.project,'暂无') project,k.fName modName,a.ModTime ModTime,
  105. {recipeStr} recipe,
  106. l.fCode MacModelCode,p.FCode AppServerCode,p.FName AppServerName,
  107. p.FIp AppServerIp ,CASE when a.AppletS=1 THEN 1 ELSE 2 end as AppletS,a.AppletV
  108. from machine a
  109. left join macstatus01 b on a.FCode=b.maccode
  110. left join macmodel l on a.mmodeid=l.id
  111. left join supplier m on l.supplierid=m.id
  112. left join mactprocess c on a.id=c.macid
  113. left join standardstatus d on b.statusid=d.StatusVal
  114. left join factoryregion f on a.factoryId = f.id
  115. left join factoryregion h on a.regionId=h.id
  116. left join factoryregion g on h.parentid=g.id
  117. left join factoryregion i on g.parentid=i.id
  118. left join projectandprogram j on {recipeStr}=j.recipe
  119. left join staff k on a.modCode = k.fcode
  120. left join eapappservermac o on a.id=o.macid
  121. left join eapappserver p on p.id=o.eapappserverid
  122. LEFT JOIN machinenumber ON a.ID = machinenumber.MacId
  123. where 1=1 {filter}
  124. order by {sort} {order} limit {start - 1},{length}";
  125. var datas = CurrDb.FindList<MachineInfo>(sql);
  126. if (datas != null && datas.Count() > 0)
  127. {
  128. sql = "select * from machine where realip in (select realip from machine group by realip having count(1)>1)";
  129. var wrongs = CurrDb.FindList<Machine>(sql);
  130. foreach (var item in datas)
  131. {
  132. if (wrongs.Any(c => c.ID == item.MacId))
  133. {
  134. item.RealIpState = -1;
  135. }
  136. else
  137. {
  138. item.RealIpState = 1;
  139. }
  140. var changeLogDal = new MachineInfoChangeLogDal(CurrDb);
  141. var log = changeLogDal.GetLastModifyRecord(item.MacCode).Result;
  142. if (log != null && log.RecTime > item.ModTime)
  143. {
  144. item.ModName = log.RecName;
  145. item.ModTime = log.RecTime;
  146. }
  147. if (item.AppletS == 1)
  148. {
  149. item.AppletSName = "已开启";
  150. }
  151. }
  152. }
  153. return datas;
  154. }
  155. public int ImportMachines(MachineInfo[] machines, string userCode, IList<string> existMacCodes, ref string errorinfo)
  156. {
  157. foreach (var item in machines)
  158. {
  159. var ipRegex = new Regex(RegexExpression.IpAddressExp);
  160. if (!ipRegex.IsMatch(item.IpAddress))
  161. {
  162. errorinfo = $"机台[{item.MacCode}]的IP地址[{item.IpAddress}]格式不正确";
  163. return -1;
  164. }
  165. var macRegex = new Regex(RegexExpression.MacAddressExp);
  166. if (!macRegex.IsMatch(item.MacAddress))
  167. {
  168. errorinfo = $"机台[{item.MacCode}]的MAC地址[{item.MacAddress}]格式不正确";
  169. return -1;
  170. }
  171. }
  172. foreach (var item in machines)
  173. {
  174. var exist = Convert.ToInt32(CurrDb.FindList<string>($"select count(1) from machine where fCode='{item.MacCode}'").FirstOrDefault() ?? "-1");
  175. if (exist > 0)
  176. {
  177. existMacCodes.Add(item.MacCode);
  178. continue;
  179. }
  180. #region 获取所在园区及线体
  181. if (string.IsNullOrEmpty(item.Factory) || string.IsNullOrEmpty(item.Plant) || string.IsNullOrEmpty(item.Floor) || string.IsNullOrEmpty(item.Line))
  182. {
  183. errorinfo = $"机台[{item.MacCode}]的位置信息不完整,不能导入";
  184. return -1;
  185. }
  186. var lineId = 0; int? facId = 0;
  187. lineId = GetRegionId(item, out facId, ref errorinfo);
  188. if (lineId < 0)
  189. {
  190. return -1;
  191. }
  192. #endregion
  193. #region 获取机型
  194. var supplierDal = new SupplierDal(CurrDb);
  195. var supplier = supplierDal.AddOrGet(new Supplier { FCode = item.SupplierCode, FName = item.SupplierCode }, userCode, ref errorinfo);
  196. if (supplier == null)
  197. {
  198. errorinfo = $"机台厂家[{item.SupplierCode}]新增失败";
  199. return -1;
  200. }
  201. var macmodelDal = new MacModelDal(CurrDb);
  202. var macModel = new MacModel { FCode = item.MacModelCode, FName = item.MacModelCode, SupplierID = supplier.ID };
  203. macModel = macmodelDal.AddOrGet(macModel, userCode, ref errorinfo);
  204. if (macModel == null)
  205. {
  206. errorinfo = $"机型[{item.MacModelCode}]新增失败";
  207. return -1;
  208. }
  209. #endregion
  210. #region 判断机台IP地址和MAC地址是否重复
  211. if (IsDuplicate("IpAddress", item.IpAddress))
  212. {
  213. errorinfo = $"机台[{item.MacCode}]的IP地址[{item.IpAddress}]在数据库中已存在,请确认";
  214. return -1;
  215. }
  216. if (IsDuplicate("MacAddress", item.MacAddress))
  217. {
  218. errorinfo = $"机台[{item.MacCode}]的MAC地址[{item.MacAddress}]在数据库中已存在,请确认";
  219. return -1;
  220. }
  221. #endregion
  222. #region 插入机台表
  223. var machineDal = new MachineDal(CurrDb);
  224. var machine = new Machine
  225. {
  226. FCode = item.MacCode,
  227. FName = item.MacCode,
  228. IPAddress = item.IpAddress,
  229. MacAddress = item.MacAddress,
  230. DeviceID = 0,
  231. FPort = item.FPort == 0 ? 5000 : item.FPort,
  232. FactoryId = facId.Value,
  233. RegionId = lineId,
  234. LocDir = string.Empty,
  235. MModeID = macModel.ID,
  236. IsConn = 1,
  237. IsControl = -1,
  238. IsAnalysis = 1,
  239. IsRemoveProgram = -1,
  240. Source = "EAP"
  241. };
  242. if (CurrDb.InsertFor(machine, userCode) < 0)
  243. {
  244. errorinfo = $"机台[{machine.FCode}]新增失败";
  245. return -1;
  246. }
  247. var sql = "select @@identity;";
  248. var macid = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault());
  249. #endregion
  250. #region 插入制程关系
  251. if (!string.IsNullOrEmpty(item.PCode))
  252. {
  253. var pCodeDal = new TProcessDal(CurrDb);
  254. var pcode = pCodeDal.AddOrGet(new TProcess
  255. {
  256. FCode = item.PCode,
  257. FName = item.PCode,
  258. IsChange = 1,
  259. UseImage = item.PCode
  260. }, userCode, ref errorinfo);
  261. if (pcode == null)
  262. {
  263. errorinfo = $"制程[{item.PCode}]新增失败";
  264. return -1;
  265. }
  266. var mactProcess = new MacTProcess { MacID = macid.ToString(), PCode = item.PCode };
  267. if (CurrDb.InsertFor(mactProcess, userCode) < 0)
  268. {
  269. errorinfo = $"机台[{item.MacCode}]制程关系新增失败";
  270. return -1;
  271. }
  272. }
  273. #endregion
  274. #region 插入机台编号
  275. if (!string.IsNullOrEmpty(item.MacNumber))
  276. {
  277. MachineNumber num = new MachineNumber();
  278. num.MacId = macid;
  279. num.MacNumber = item.MacNumber;
  280. if (CurrDb.InsertFor<MachineNumber>(num, userCode) < 0)
  281. {
  282. errorinfo = $"机台[{item.MacCode}]机台编号新增失败";
  283. return -1;
  284. }
  285. }
  286. #endregion
  287. }
  288. return 1;
  289. }
  290. public int UpdateMachines(MachineInfo[] machines, string userCode, ref string errorinfo)
  291. {
  292. foreach (var item in machines)
  293. {
  294. var ipRegex = new Regex(RegexExpression.IpAddressExp);
  295. if (!ipRegex.IsMatch(item.IpAddress))
  296. {
  297. errorinfo = $"机台[{item.MacCode}]的IP地址[{item.IpAddress}]格式不正确";
  298. return -1;
  299. }
  300. var macRegex = new Regex(RegexExpression.MacAddressExp);
  301. if (!macRegex.IsMatch(item.MacAddress))
  302. {
  303. errorinfo = $"机台[{item.MacCode}]的MAC地址[{item.MacAddress}]格式不正确";
  304. return -1;
  305. }
  306. }
  307. foreach (var item in machines)
  308. {
  309. #region 获取所在园区及线体
  310. if (string.IsNullOrEmpty(item.Factory) || string.IsNullOrEmpty(item.Plant) || string.IsNullOrEmpty(item.Floor) || string.IsNullOrEmpty(item.Line))
  311. {
  312. errorinfo = $"机台[{item.MacCode}]的位置信息不完整,不能导入";
  313. return -1;
  314. }
  315. var lineId = 0; int? facId = 0;
  316. lineId = GetRegionId(item, out facId, ref errorinfo);
  317. if (lineId < 0)
  318. {
  319. return -1;
  320. }
  321. #endregion
  322. #region 获取机型
  323. var supplierDal = new SupplierDal(CurrDb);
  324. var supplier = supplierDal.AddOrGet(new Supplier { FCode = item.SupplierCode, FName = item.SupplierCode }, userCode, ref errorinfo);
  325. if (supplier == null)
  326. {
  327. errorinfo = $"机台厂家[{item.SupplierCode}]新增失败";
  328. return -1;
  329. }
  330. var macmodelDal = new MacModelDal(CurrDb);
  331. var macModel = new MacModel { FCode = item.MacModelCode, FName = item.MacModelCode, SupplierID = supplier.ID };
  332. macModel = macmodelDal.AddOrGet(macModel, userCode, ref errorinfo);
  333. if (macModel == null)
  334. {
  335. errorinfo = $"机型[{item.MacModelCode}]新增失败";
  336. return -1;
  337. }
  338. #endregion
  339. #region 更新机台表
  340. var machine = CurrDb.FindListForCondition<Machine>($" and a.fcode='{item.MacCode}'",
  341. ref errorinfo).FirstOrDefault();
  342. #region 判断机台IP地址和MAC地址是否重复
  343. if (IsDuplicate("IpAddress", item.IpAddress, machine.ID))
  344. {
  345. errorinfo = $"机台[{item.MacCode}]的IP地址[{item.IpAddress}]在数据库中已存在,请确认";
  346. return -1;
  347. }
  348. if (IsDuplicate("MacAddress", item.MacAddress, machine.ID))
  349. {
  350. errorinfo = $"机台[{item.MacCode}]的MAC地址[{item.MacAddress}]在数据库中已存在,请确认";
  351. return -1;
  352. }
  353. #endregion
  354. var machineDal = new MachineDal(CurrDb);
  355. machine.IPAddress = item.IpAddress;
  356. machine.MacAddress = item.MacAddress;
  357. machine.FactoryId = facId.Value;
  358. machine.RegionId = lineId;
  359. machine.MModeID = macModel.ID;
  360. if (CurrDb.UpdateFor(machine, userCode) < 0)
  361. {
  362. errorinfo = $"机台[{machine.FCode}]更新失败";
  363. return -1;
  364. }
  365. #endregion
  366. #region 插入制程关系
  367. if (!string.IsNullOrEmpty(item.PCode))
  368. {
  369. var pCodeDal = new TProcessDal(CurrDb);
  370. var pcode = pCodeDal.AddOrGet(new TProcess
  371. {
  372. FCode = item.PCode,
  373. FName = item.PCode,
  374. IsChange = 1,
  375. UseImage = item.PCode
  376. }, userCode, ref errorinfo);
  377. if (pcode == null)
  378. {
  379. errorinfo = $"制程[{item.PCode}]新增失败";
  380. return -1;
  381. }
  382. var sql = $"DELETE from MActprocess where macid={machine.ID}";
  383. if (CurrDb.ExecuteBySql(sql) < 0)
  384. {
  385. errorinfo = "删除原制程关系失败";
  386. return -1;
  387. }
  388. var mactProcess = new MacTProcess { MacID = machine.ID.ToString(), PCode = item.PCode };
  389. if (CurrDb.InsertFor(mactProcess, userCode) < 0)
  390. {
  391. errorinfo = $"机台[{item.MacCode}]制程关系新增失败";
  392. return -1;
  393. }
  394. }
  395. #endregion
  396. #region
  397. if (!string.IsNullOrEmpty(item.MacNumber))
  398. {
  399. MachineNumber m = new MachineNumber();
  400. List<MachineNumber> list = (List<MachineNumber>)CurrDb.FindList<MachineNumber>($"select * from machinenumber where macid={machine.ID}");
  401. if (list.Count < 1)
  402. {
  403. m.MacId = machine.ID;
  404. m.MacNumber = item.MacNumber;
  405. if (CurrDb.InsertFor<MachineNumber>(m, userCode) < 0)
  406. {
  407. errorinfo = $"机台[{item.MacCode}]机台编号新增失败";
  408. return -1;
  409. }
  410. }
  411. else
  412. {
  413. m = list[0];
  414. m.MacNumber = item.MacNumber;
  415. m.ModCode = userCode;
  416. if (CurrDb.UpdateFor<MachineNumber>(m, userCode) < 0)
  417. {
  418. errorinfo = $"机台[{item.MacCode}]机台编号修改失败";
  419. return -1;
  420. }
  421. }
  422. }
  423. #endregion
  424. }
  425. return 1;
  426. }
  427. private int GetRegionId(MachineInfo item, out int? factoryId, ref string errorinfo)
  428. {
  429. var facDal = new FactoryRegionDal(CurrDb);
  430. var facId = facDal.Get(1, 1, "ID", "asc", $" and a.FName='{item.Factory.Trim()}'", errorinfo)
  431. .FirstOrDefault()?.Id;
  432. if (facId == null)
  433. {
  434. errorinfo = $"机台[{item.MacCode}]所在区域[{item.Factory}]无效";
  435. factoryId = null;
  436. return -1;
  437. }
  438. var plantId = facDal.Get(1, 1, "ID", "asc", $" and a.FName='{item.Plant.Trim()}' and " +
  439. $"a.parentId={facId}", errorinfo)
  440. .FirstOrDefault()?.Id;
  441. if (plantId == null)
  442. {
  443. errorinfo = $"机台[{item.MacCode}]所在区域[{item.Plant}]无效";
  444. factoryId = null;
  445. return -1;
  446. }
  447. var floorId = facDal.Get(1, 1, "ID", "asc", $" and a.FName='{item.Floor.Trim()}' and " +
  448. $"a.parentId={plantId}", errorinfo)
  449. .FirstOrDefault()?.Id;
  450. if (floorId == null)
  451. {
  452. errorinfo = $"机台[{item.MacCode}]所在区域[{item.Floor}]无效";
  453. factoryId = null;
  454. return -1;
  455. }
  456. var lineId = facDal.Get(1, 1, "ID", "asc", $" and a.FName='{item.Line.Trim()}' and " +
  457. $"a.parentId={floorId}", errorinfo)
  458. .FirstOrDefault()?.Id;
  459. if (lineId == null)
  460. {
  461. errorinfo = $"机台[{item.MacCode}]所在区域[{item.Line}]无效";
  462. factoryId = null;
  463. return -1;
  464. }
  465. factoryId = facId;
  466. return lineId.Value;
  467. }
  468. private bool IsDuplicate(string type, string address, int? macid = null)
  469. {
  470. string errorinfo = string.Empty;
  471. var filter = $" and a.{type}='{address}'";
  472. if (macid != null)
  473. {
  474. filter += $" and a.id<>{macid}";
  475. }
  476. Machine exist = CurrDb.FindListForCondition<Machine>(filter,
  477. ref errorinfo).FirstOrDefault();
  478. return exist != null;
  479. }
  480. }
  481. }