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

969 lines
43 KiB

5 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ICSSoft.Base.Config.DBHelper;
  6. using System.Data;
  7. using ICSSoft.Frame.Data.Entity;
  8. using System.Data.Linq;
  9. using System.Data.SqlClient;
  10. using ICSSoft.Base.Config.AppConfig;
  11. using System.Configuration;
  12. using System.IO;
  13. using ICSSoft.Frame.Data.Entity.NcApiEntity;
  14. using Newtonsoft.Json;
  15. namespace ICSSoft.Frame.Data.DAL
  16. {
  17. public class ICSMO2UserDAL
  18. {
  19. #region 增加修改
  20. public static void Add(List<FormICSMO2UserUIModel> MO2UserInfoList, string dsconn)
  21. {
  22. FramDataContext db = new FramDataContext(dsconn);
  23. db.Connection.Open();
  24. db.Transaction = db.Connection.BeginTransaction();
  25. try
  26. {
  27. foreach (FormICSMO2UserUIModel MO2UserInfo in MO2UserInfoList)
  28. {
  29. bool isNew = false;
  30. var line = db.ICSMO2User.SingleOrDefault(a => a.LOTNO == MO2UserInfo.LOTNO && a.OPCODE == MO2UserInfo.OPCODE);
  31. //var line = db.ICSMO2User.SingleOrDefault(a => a.LOTNO == MO2UserInfo.LOTNO );
  32. if (line == null)
  33. {
  34. isNew = true;
  35. line = new ICSMO2User();
  36. line.ID = AppConfig.GetGuid();
  37. }
  38. line.MOCODE = MO2UserInfo.MOCODE;
  39. line.MOSEQ = MO2UserInfo.MOSEQ;
  40. line.LOTNO = MO2UserInfo.LOTNO;
  41. line.SEGCODE = MO2UserInfo.SEGCODE;
  42. line.RouteCode = MO2UserInfo.RouteCode;
  43. line.OPCODE = MO2UserInfo.OPCODE;
  44. line.USERCODE = MO2UserInfo.USERCODE;
  45. line.USERName = MO2UserInfo.USERName;
  46. line.EQPCode = MO2UserInfo.EQPCode;
  47. line.EQPName = MO2UserInfo.EQPName;
  48. line.MUSER = MO2UserInfo.MUSER;
  49. line.MUSERName = MO2UserInfo.MUSERName;
  50. line.MTIME = Convert.ToDateTime(MO2UserInfo.MTIME);
  51. line.WorkPoint = MO2UserInfo.WorkPoint;
  52. line.EATTRIBUTE1 = MO2UserInfo.EATTRIBUTE1;
  53. line.StartPlanDate = MO2UserInfo.StartPlanDate;
  54. line.EndPlanDate = MO2UserInfo.EndPlanDate;
  55. if (MO2UserInfo.PRLineID == "False" || MO2UserInfo.PRLineID == "0" || string.IsNullOrWhiteSpace(MO2UserInfo.PRLineID))
  56. {
  57. line.PRLineID = null;
  58. }
  59. else
  60. {
  61. line.PRLineID = MO2UserInfo.PRLineID;
  62. }
  63. if (isNew)
  64. db.ICSMO2User.InsertOnSubmit(line);
  65. //log
  66. db.SubmitChanges();
  67. }
  68. db.SubmitChanges();
  69. db.Transaction.Commit();
  70. }
  71. catch (Exception ex)
  72. {
  73. db.Transaction.Rollback();
  74. throw new Exception(ex.Message);
  75. }
  76. finally
  77. {
  78. db.Connection.Close();
  79. }
  80. }
  81. #endregion
  82. #region 删除
  83. public static void Delete(List<string> idList, string dsconn)
  84. {
  85. FramDataContext db = new FramDataContext(dsconn);
  86. db.Connection.Open();
  87. db.Transaction = db.Connection.BeginTransaction();
  88. try
  89. {
  90. var lines = db.ICSMO2User.Where(a => idList.Contains(a.ID));
  91. db.ICSMO2User.DeleteAllOnSubmit(lines);
  92. db.SubmitChanges();
  93. db.Transaction.Commit();
  94. }
  95. catch (Exception ex)
  96. {
  97. db.Transaction.Rollback();
  98. throw ex;
  99. }
  100. finally
  101. {
  102. db.Connection.Close();
  103. }
  104. }
  105. #endregion
  106. #region 取消下发
  107. public static void cancelSend(List<string> codeList, string dsconn)
  108. {
  109. FramDataContext db = new FramDataContext(dsconn);
  110. db.Connection.Open();
  111. db.Transaction = db.Connection.BeginTransaction();
  112. try
  113. {
  114. var lotonwip = db.ICSLOTONWIP.Where(a => codeList.Contains(a.LOTNO));
  115. if (lotonwip != null && lotonwip.ToList().Count > 0)
  116. {
  117. throw new Exception("批次已经采集,不可取消派工");
  118. }
  119. var lines = db.ICSMO2User.Where(a => codeList.Contains(a.LOTNO));
  120. db.ICSMO2User.DeleteAllOnSubmit(lines);
  121. db.SubmitChanges();
  122. db.Transaction.Commit();
  123. }
  124. catch (Exception ex)
  125. {
  126. db.Transaction.Rollback();
  127. throw ex;
  128. }
  129. }
  130. #endregion
  131. public static void cancelSend(List<string> lotno1, List<string> opcode1, string dsconn)
  132. {
  133. FramDataContext db = new FramDataContext(dsconn);
  134. db.Connection.Open();
  135. db.Transaction = db.Connection.BeginTransaction();
  136. try
  137. {
  138. var lotonwip1 = (from a in db.ICSLOTONWIP
  139. where lotno1.Contains(a.LOTNO)
  140. select a).ToList();
  141. var lotonwip = lotonwip1.Where(a => opcode1.Contains(a.OPCODE));
  142. if (lotonwip != null && lotonwip.ToList().Count > 0)
  143. {
  144. throw new Exception("批次已经采集,不可取消派工");
  145. }
  146. List<ICSMO2User> list = new List<ICSMO2User>();
  147. for (int i = 0; i < lotno1.Count(); i++)
  148. {
  149. ICSMO2User[] b = (from a in db.ICSMO2User
  150. where a.LOTNO.Equals(lotno1[i]) && a.OPCODE.Equals(opcode1[i])
  151. select a).ToArray();
  152. list.Add(b[0]);
  153. }
  154. db.ICSMO2User.DeleteAllOnSubmit(list);
  155. db.SubmitChanges();
  156. db.Transaction.Commit();
  157. }
  158. catch (Exception ex)
  159. {
  160. db.Transaction.Rollback();
  161. throw ex;
  162. }
  163. }
  164. #region 写入日志
  165. public static void WriteLogFile(string input, string txtName)
  166. {
  167. try
  168. {
  169. string logAdress = ConfigurationManager.AppSettings["logAdress"].ToString() + "\\Log\\";
  170. if (!System.IO.Directory.Exists(logAdress))
  171. {
  172. System.IO.Directory.CreateDirectory(logAdress);//不存在就创建目录
  173. }
  174. string adress = logAdress + txtName;
  175. if (!System.IO.Directory.Exists(adress))
  176. {
  177. System.IO.Directory.CreateDirectory(adress);//不存在就创建目录
  178. }
  179. // string logAdress = ConfigurationManager.AppSettings["logAdress"].ToString();
  180. /**/
  181. ///指定日志文件的目录
  182. string fname = adress + "\\" + "log" + DateTime.Now.ToString("yy-MM-dd") + ".txt";
  183. /**/
  184. ///定义文件信息对象
  185. FileInfo finfo = new FileInfo(fname);
  186. if (!finfo.Exists)
  187. {
  188. FileStream fs;
  189. fs = File.Create(fname);
  190. fs.Close();
  191. finfo = new FileInfo(fname);
  192. }
  193. /**/
  194. ///判断文件是否存在以及是否大于2K
  195. if (finfo.Length > 1024 * 1024 * 10)
  196. {
  197. /**/
  198. ///文件超过10MB则重命名
  199. File.Move(logAdress + "\\Log\\" + txtName + ".txt", Directory.GetCurrentDirectory() + DateTime.Now.TimeOfDay + "\\Log\\" + txtName + ".txt");
  200. /**/
  201. ///删除该文件
  202. //finfo.Delete();
  203. }
  204. //finfo.AppendText();
  205. /**/
  206. ///创建只写文件流
  207. using (FileStream fs = finfo.OpenWrite())
  208. {
  209. /**/
  210. ///根据上面创建的文件流创建写数据流
  211. StreamWriter w = new StreamWriter(fs);
  212. /**/
  213. ///设置写数据流的起始位置为文件流的末尾
  214. w.BaseStream.Seek(0, SeekOrigin.End);
  215. w.WriteLine("*****************Start*****************");
  216. w.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  217. /**/
  218. ///写入当前系统时间并换行
  219. /**/
  220. ///写入日志内容并换行
  221. w.WriteLine(input);
  222. /**/
  223. ///写入------------------------------------“并换行
  224. w.WriteLine("------------------END------------------------");
  225. /**/
  226. ///清空缓冲区内容,并把缓冲区内容写入基础流
  227. w.Flush();
  228. /**/
  229. ///关闭写数据流
  230. w.Close();
  231. }
  232. }
  233. catch (Exception ex)
  234. { throw ex; }
  235. }
  236. #endregion
  237. public static void AddAndEdit(List<FormICSMO2UserUIModel> list, List<ICSMO2UserDetail> list_detail, string dsconn)
  238. {
  239. FramDataContext db = new FramDataContext(dsconn);
  240. db.Connection.Open();
  241. db.Transaction = db.Connection.BeginTransaction();
  242. try
  243. {
  244. foreach (FormICSMO2UserUIModel MO2UserInfo in list)
  245. {
  246. bool isNew = false;
  247. var line = db.ICSMO2User.SingleOrDefault(a => a.LOTNO == MO2UserInfo.LOTNO && a.OPCODE == MO2UserInfo.OPCODE);
  248. //var line = db.ICSMO2User.SingleOrDefault(a => a.LOTNO == MO2UserInfo.LOTNO );
  249. if (line == null)
  250. {
  251. isNew = true;
  252. line = new ICSMO2User();
  253. line.ID = AppConfig.GetGuid();
  254. }
  255. line.MOCODE = MO2UserInfo.MOCODE;
  256. line.MOSEQ = MO2UserInfo.MOSEQ;
  257. line.LOTNO = MO2UserInfo.LOTNO;
  258. line.SEGCODE = MO2UserInfo.SEGCODE;
  259. line.RouteCode = MO2UserInfo.RouteCode;
  260. line.OPCODE = MO2UserInfo.OPCODE;
  261. line.USERCODE = MO2UserInfo.USERCODE;
  262. line.USERName = MO2UserInfo.USERName;
  263. line.EQPCode = MO2UserInfo.EQPCode;
  264. line.EQPName = MO2UserInfo.EQPName;
  265. line.MUSER = MO2UserInfo.MUSER;
  266. line.MUSERName = MO2UserInfo.MUSERName;
  267. line.MTIME = Convert.ToDateTime(MO2UserInfo.MTIME);
  268. line.WorkPoint = MO2UserInfo.WorkPoint;
  269. line.EATTRIBUTE1 = MO2UserInfo.EATTRIBUTE1;
  270. line.StartPlanDate = MO2UserInfo.StartPlanDate;
  271. line.EndPlanDate = MO2UserInfo.EndPlanDate;
  272. if (MO2UserInfo.PRLineID == "False" || MO2UserInfo.PRLineID == "0" || string.IsNullOrWhiteSpace(MO2UserInfo.PRLineID))
  273. {
  274. line.PRLineID = null;
  275. }
  276. else
  277. {
  278. line.PRLineID = MO2UserInfo.PRLineID;
  279. }
  280. if (isNew)
  281. {
  282. db.ICSMO2User.InsertOnSubmit(line);
  283. }
  284. db.SubmitChanges();
  285. foreach (ICSMO2UserDetail detail in list_detail)
  286. {
  287. bool isNewDetail = false;
  288. var lineDetail = db.ICSMO2UserDetail.SingleOrDefault(a => a.DID == detail.DID);
  289. if (lineDetail == null)
  290. {
  291. isNewDetail = true;
  292. lineDetail = new ICSMO2UserDetail();
  293. }
  294. lineDetail.ID = line.ID;
  295. lineDetail.EQPID = detail.EQPID;
  296. lineDetail.DID = detail.DID;
  297. lineDetail.AVAILABLE = detail.AVAILABLE;
  298. lineDetail.PLANEND = detail.PLANEND;
  299. lineDetail.PLANSTART = detail.PLANSTART;
  300. lineDetail.PLANQTY = detail.PLANQTY;
  301. lineDetail.PLANTIME = detail.PLANTIME;
  302. if (isNewDetail)
  303. {
  304. db.ICSMO2UserDetail.InsertOnSubmit(lineDetail);
  305. }
  306. db.SubmitChanges();
  307. }
  308. }
  309. db.SubmitChanges();
  310. db.Transaction.Commit();
  311. }
  312. catch (Exception ex)
  313. {
  314. db.Transaction.Rollback();
  315. throw new Exception(ex.Message);
  316. }
  317. }
  318. public static void AddAndEdit(FormICSMO2UserUIModel MO2UserInfo, string dsconn)
  319. {
  320. FramDataContext db = new FramDataContext(dsconn);
  321. db.Connection.Open();
  322. db.Transaction = db.Connection.BeginTransaction();
  323. try
  324. {
  325. bool isNew = false;
  326. var line = db.ICSMO2User.SingleOrDefault(a => a.LOTNO == MO2UserInfo.LOTNO && a.OPCODE == MO2UserInfo.OPCODE);
  327. if (line == null)
  328. {
  329. isNew = true;
  330. line = new ICSMO2User();
  331. line.ID = AppConfig.GetGuid();
  332. }
  333. line.MOCODE = MO2UserInfo.MOCODE;
  334. line.MOSEQ = MO2UserInfo.MOSEQ;
  335. line.LOTNO = MO2UserInfo.LOTNO;
  336. line.SEGCODE = MO2UserInfo.SEGCODE;
  337. line.RouteCode = MO2UserInfo.RouteCode;
  338. line.OPCODE = MO2UserInfo.OPCODE;
  339. line.USERCODE = MO2UserInfo.USERCODE;
  340. line.USERName = MO2UserInfo.USERName;
  341. line.EQPCode = MO2UserInfo.EQPCode;
  342. line.EQPName = MO2UserInfo.EQPName;
  343. line.MUSER = MO2UserInfo.MUSER;
  344. line.MUSERName = MO2UserInfo.MUSERName;
  345. line.MTIME = Convert.ToDateTime(MO2UserInfo.MTIME);
  346. line.WorkPoint = MO2UserInfo.WorkPoint;
  347. line.EATTRIBUTE1 = MO2UserInfo.EATTRIBUTE1;
  348. line.StartPlanDate = MO2UserInfo.StartPlanDate;
  349. line.EndPlanDate = MO2UserInfo.EndPlanDate;
  350. if (MO2UserInfo.PRLineID == "False" || MO2UserInfo.PRLineID == "0" || string.IsNullOrWhiteSpace(MO2UserInfo.PRLineID))
  351. {
  352. line.PRLineID = null;
  353. }
  354. else
  355. {
  356. line.PRLineID = MO2UserInfo.PRLineID;
  357. }
  358. foreach (ICSMO2UserDetail detail in MO2UserInfo.ListDetail)
  359. {
  360. var lineDetail = db.ICSMO2UserDetail.SingleOrDefault(a => a.DID == detail.DID);
  361. if (lineDetail != null)
  362. {
  363. lineDetail.AVAILABLE = detail.AVAILABLE;
  364. }
  365. else
  366. {
  367. lineDetail = new ICSMO2UserDetail();
  368. lineDetail.ID = line.ID;
  369. lineDetail.EQPID = detail.EQPID;
  370. lineDetail.DID = detail.DID;
  371. lineDetail.PLANEND = detail.PLANEND;
  372. lineDetail.PLANSTART = detail.PLANSTART;
  373. lineDetail.PLANQTY = detail.PLANQTY;
  374. lineDetail.PLANTIME = detail.PLANTIME;
  375. lineDetail.AVAILABLE = detail.AVAILABLE;
  376. db.ICSMO2UserDetail.InsertOnSubmit(lineDetail);
  377. }
  378. db.SubmitChanges();
  379. }
  380. if (isNew)
  381. db.ICSMO2User.InsertOnSubmit(line);
  382. db.SubmitChanges();
  383. db.Transaction.Commit();
  384. }
  385. catch (Exception ex)
  386. {
  387. db.Transaction.Rollback();
  388. throw new Exception(ex.Message);
  389. }
  390. finally
  391. {
  392. db.Connection.Close();
  393. }
  394. }
  395. public static void CancelSended(List<string> list, string StrCon)
  396. {
  397. FramDataContext db = new FramDataContext(StrCon);
  398. db.Connection.Open();
  399. db.Transaction = db.Connection.BeginTransaction();
  400. try
  401. {
  402. for (int i = 0; i < list.Count; i++)
  403. {
  404. var line = db.ICSMO2User.SingleOrDefault(a => a.ID == list[i]);
  405. if (line != null)
  406. {
  407. //1.已开工
  408. var linesOnwip = db.ICSLOTONWIP.Where(a => a.LOTNO == line.LOTNO && a.OPCODE == line.OPCODE);
  409. if (linesOnwip.Count() != 0)
  410. {
  411. throw new Exception("跟踪单" + line.LOTNO + "工序" + line.OPCODE + "已生产,不可取消");
  412. }
  413. //2.删除派工设备表
  414. var lineDetail = db.ICSMO2UserDetail.Where(a => a.ID == list[i]);
  415. if (lineDetail.Count() != 0)
  416. {
  417. db.ICSMO2UserDetail.DeleteAllOnSubmit(lineDetail);
  418. db.SubmitChanges();
  419. }
  420. //3.删除派工主表
  421. db.ICSMO2User.DeleteOnSubmit(line);
  422. db.SubmitChanges();
  423. }
  424. }
  425. db.SubmitChanges();
  426. db.Transaction.Commit();
  427. }
  428. catch (Exception ex)
  429. {
  430. db.Transaction.Rollback();
  431. throw new Exception(ex.Message);
  432. }
  433. finally
  434. {
  435. db.Connection.Close();
  436. }
  437. }
  438. public static void CancelSendedWW(List<string> list, string StrCon)
  439. {
  440. FramDataContext db = new FramDataContext(StrCon);
  441. db.Connection.Open();
  442. db.Transaction = db.Connection.BeginTransaction();
  443. try
  444. {
  445. for (int i = 0; i < list.Count; i++)
  446. {
  447. var line = db.ICSMO2User.SingleOrDefault(a => a.ID == list[i]);
  448. if (line != null)
  449. {
  450. //1.已产生PR单
  451. if (!string.IsNullOrEmpty(line.PRLineID))
  452. {
  453. throw new Exception("跟踪单" + line.LOTNO + "工序" + line.OPCODE + "已产生PR单,不可撤销" + line.PRLineID);
  454. }
  455. var linesOnwip = db.ICSLOTONWIP.Where(a => a.LOTNO == line.LOTNO && a.OPCODE == line.OPCODE);
  456. if (linesOnwip.Count() != 0)
  457. {
  458. throw new Exception("跟踪单" + line.LOTNO + "工序" + line.OPCODE + "已发料,不可撤销");
  459. }
  460. //2.
  461. //ICSMO2UserLog log = new ICSMO2UserLog();
  462. //log.
  463. //3.删除派工主表
  464. db.ICSMO2User.DeleteOnSubmit(line);
  465. db.SubmitChanges();
  466. }
  467. }
  468. db.SubmitChanges();
  469. db.Transaction.Commit();
  470. }
  471. catch (Exception ex)
  472. {
  473. db.Transaction.Rollback();
  474. throw new Exception(ex.Message);
  475. }
  476. finally
  477. {
  478. db.Connection.Close();
  479. }
  480. }
  481. public static DataTable SelectMO2USER(string MOCODE, string MOSEQ, string StrCon, string wp)
  482. {
  483. string sql = @"SELECT MOCODE,MOSEQ,LOTNO,OPCODE FROM dbo.ICSMO2User WHERE MOCODE='{0}' AND MOSEQ={1} AND WorkPoint='{2}'";
  484. sql = string.Format(sql, MOCODE, MOSEQ, wp);
  485. return DBHelper.ExecuteDataset(StrCon, CommandType.Text, sql).Tables[0];
  486. }
  487. public static void Import(List<ICSMO2User> mo2userList, string appConnectString)
  488. {
  489. FramDataContext db = new FramDataContext(appConnectString);
  490. db.Connection.Open();
  491. db.Transaction = db.Connection.BeginTransaction();
  492. try
  493. {
  494. foreach (ICSMO2User info in mo2userList)
  495. {
  496. bool add = false;
  497. string EQPID = "";
  498. string EQPNAME = "";
  499. var lineItemLot = db.ICSITEMLot.SingleOrDefault(a => a.LotNO == info.LOTNO && a.WorkPoint == info.WorkPoint && (a.TYPE == "工单" || a.TYPE == "跟踪单分批"));
  500. if (lineItemLot == null)
  501. {
  502. throw new Exception("跟踪单:" + info.LOTNO + ",不存在");
  503. }
  504. var lineRoute = db.ICSROUTE.Where(a => a.ROUTECODE == info.RouteCode && a.WorkPoint == info.WorkPoint);
  505. if (lineRoute == null)
  506. {
  507. throw new Exception("工艺:" + info.RouteCode + ",不存在");
  508. }
  509. var lineOP = db.ICSOP.SingleOrDefault(a => a.OPCODE == info.OPCODE && a.WorkPoint == info.WorkPoint);
  510. if (lineOP == null)
  511. {
  512. throw new Exception("工序:" + info.OPCODE + ",不存在");
  513. }
  514. //分批时,route中可能不包含此op,是额外添加的,以ICSITEMROUTE2OPLot为准.
  515. //var lineRouteOP = db.ICSROUTE2OP.SingleOrDefault(a => a.OPCODE == info.OPCODE && a.ROUTECODE == info.RouteCode && a.WORKPOINT == info.WorkPoint);
  516. //if (lineRouteOP == null)
  517. //{
  518. // throw new Exception("工艺:" + info.RouteCode + ",不包含工序:" + info.OPCODE);
  519. //}
  520. var lineItemR2OPLot = db.ICSITEMROUTE2OPLot.SingleOrDefault(a => a.LotNo == info.LOTNO && a.ROUTECODE == info.RouteCode && a.OPCODE == info.OPCODE);
  521. if (lineItemR2OPLot == null)
  522. {
  523. throw new Exception("跟踪单:" + info.LOTNO + ",工艺:" + info.RouteCode + ",工序:" + info.OPCODE + ",不存在");
  524. }
  525. List<ICSEQPSTP> listTime = db.ICSEQPSTP.Where(a => a.ITEMCODE == lineItemLot.ItemCode && a.EATTRIBUTE1 == info.RouteCode && a.OPCODE == info.OPCODE && a.ISREF == "是").ToList();
  526. if (listTime.Count == 0)
  527. {
  528. throw new Exception("跟踪单:" + info.LOTNO + "对应物料:" + lineItemLot.ItemCode + ",工艺:" + info.OPCODE + ",工序:" + info.OPCODE + ",没有默认的标准工时");
  529. }
  530. if (listTime.Count > 1)
  531. {
  532. throw new Exception("跟踪单:" + info.LOTNO + "对应物料:" + lineItemLot.ItemCode + ",工艺:" + info.OPCODE + ",工序:" + info.OPCODE + ",有多个默认标准工时");
  533. }
  534. var lineTime = listTime[0];
  535. if (info.EATTRIBUTE1 == "0")
  536. {
  537. var lineEQ = db.ICSEquipment.SingleOrDefault(a => a.EQPCode == info.EQPCode && a.WorkPoint == info.WorkPoint);
  538. if (lineEQ == null)
  539. {
  540. throw new Exception("设备不存在:" + info.EQPCode);
  541. }
  542. EQPID = lineEQ.EQPID;
  543. EQPNAME = lineEQ.EQPDESC;
  544. //ahwit委外工序只能委外派工
  545. if (lineOP.EATTRIBUTE1 == "1")
  546. {
  547. throw new Exception("工序:" + info.OPCODE + "是自制工序,不可转委外");
  548. }
  549. if (lineTime.EQPTypeCode == "固定设备")
  550. {
  551. if (lineTime.EQPCODE != info.EQPCode)
  552. {
  553. throw new Exception("标准工时维护的是固定设备:" + lineTime.EQPCODE);
  554. }
  555. //info.EQPCode = lineTime.EQPCODE;
  556. }
  557. else
  558. {
  559. if (lineTime.cClass != lineEQ.cClass || Convert.ToInt32(lineTime.cHandles) > lineEQ.cHandles || Convert.ToInt32(lineTime.cAxis) > lineEQ.cAxis ||
  560. Convert.ToInt32(lineTime.cMachinableProfiles) > lineEQ.cMachinableProfiles || Convert.ToInt32(lineTime.cToolMagazine) > lineEQ.cToolMagazine || string.Compare(lineTime.cAccuracy.ToUpper(), lineEQ.cAccuracy.ToUpper()) == -1)
  561. {
  562. throw new Exception("设备" + info.EQPCode + "\r\n"
  563. + "类型" + lineEQ.cClass + "刀柄数" + lineEQ.cHandles + "轴数" + lineEQ.cAxis + "刀库数" + lineEQ.cToolMagazine + "可加工外形" + lineEQ.cMachinableProfiles + "精度" + lineEQ.cAccuracy + "\r\n"
  564. + "对应的6大维度不满足标准工时:\r\n"
  565. + "类型" + lineTime.cClass + "刀柄数" + lineTime.cHandles + "轴数" + lineTime.cAxis + "刀库数" + lineTime.cToolMagazine + "可加工外形" + lineTime.cMachinableProfiles + "精度" + lineTime.cAccuracy);
  566. }
  567. }
  568. }
  569. var linePR = db.ICSMO2User.SingleOrDefault(a => a.LOTNO == info.LOTNO && a.OPCODE == info.OPCODE && a.PRLineID != null);
  570. if (linePR != null)
  571. {
  572. throw new Exception("跟踪单:" + info.LOTNO + "工序:" + info.OPCODE + "已请购");
  573. }
  574. var lineOnwip = db.ICSLOTONWIP.SingleOrDefault(a => a.LOTNO == info.LOTNO && a.OPCODE == info.OPCODE);
  575. if (lineOnwip != null)
  576. {
  577. throw new Exception("跟踪单:" + info.LOTNO + "工序:" + info.OPCODE + "已开工");
  578. }
  579. var lineMo2User = db.ICSMO2User.SingleOrDefault(a => a.LOTNO == info.LOTNO && a.OPCODE == info.OPCODE);
  580. if (lineMo2User == null)
  581. {
  582. add = true;
  583. lineMo2User = new ICSMO2User();
  584. lineMo2User.ID = AppConfig.GetGuid();
  585. lineMo2User.MOCODE = lineItemLot.TransNO;
  586. lineMo2User.MOSEQ = Convert.ToInt32(lineItemLot.TransLine);
  587. lineMo2User.LOTNO = info.LOTNO;
  588. lineMo2User.RouteCode = info.RouteCode;
  589. lineMo2User.OPCODE = info.OPCODE;
  590. lineMo2User.WorkPoint = info.WorkPoint;
  591. lineMo2User.USERCODE = "";
  592. lineMo2User.USERName = "";
  593. lineMo2User.EQPCode = "";
  594. lineMo2User.EQPName = "";
  595. }
  596. lineMo2User.MTIME = info.MTIME;
  597. lineMo2User.MUSER = info.MUSER;
  598. lineMo2User.MUSERName = info.MUSERName;
  599. lineMo2User.EATTRIBUTE1 = info.EATTRIBUTE1;
  600. if (info.EATTRIBUTE1 == "0")
  601. {
  602. var lineMD = db.ICSMO2UserDetail.Where(a => a.ID == lineMo2User.ID);
  603. foreach (ICSMO2UserDetail item in lineMD)
  604. {
  605. item.AVAILABLE = false;
  606. }
  607. db.SubmitChanges();
  608. ICSMO2UserDetail md = new ICSMO2UserDetail();
  609. md.ID = lineMo2User.ID;
  610. md.EQPID = EQPID;
  611. md.DID = AppConfig.GetGuid();
  612. md.PLANSTART = (DateTime)info.StartPlanDate;
  613. md.PLANEND = (DateTime)info.EndPlanDate;
  614. md.PLANQTY = lineItemLot.LOTQTY;
  615. md.PLANTIME = Convert.ToInt32(Math.Round((md.PLANEND - md.PLANSTART).TotalMinutes, 0));
  616. md.AVAILABLE = true;
  617. db.ICSMO2UserDetail.InsertOnSubmit(md);
  618. }
  619. if (add)
  620. {
  621. db.ICSMO2User.InsertOnSubmit(lineMo2User);
  622. }
  623. db.SubmitChanges();
  624. }
  625. db.SubmitChanges();
  626. db.Transaction.Commit();
  627. }
  628. catch (Exception ex)
  629. {
  630. db.Transaction.Rollback();
  631. throw new Exception(ex.Message);
  632. }
  633. finally
  634. {
  635. db.Connection.Close();
  636. }
  637. }
  638. public static DataTable ExportTemplate(string appConnectString, string workpoint, bool onlyNotSend, string mocode)
  639. {
  640. string sql = @"
  641. SELECT
  642. LOT.LotNo AS ,
  643. LOT.ROUTECODE AS 线,
  644. LOT.OPSEQ ,
  645. LOT.OPCODE AS ,
  646. op.OPDESC as ,
  647. (CASE LOT.OPAttr WHEN '' THEN 'Y' ELSE 'N' END) AS ,
  648. NULL AS ,
  649. CONVERT(DATETIME,NULL) AS ,
  650. CONVERT(DATETIME,NULL) AS
  651. FROM dbo.ICSITEMROUTE2OPLot LOT
  652. INNER JOIN dbo.ICSOP op ON op.OPCODE=LOT.OPCODE AND op.WorkPoint=lot.WorkPoint
  653. LEFT JOIN dbo.ICSMO2User US ON US.LOTNO=LOT.LotNo AND US.RouteCode=LOT.ROUTECODE AND US.OPCODE=LOT.OPCODE AND US.WorkPoint=LOT.WorkPoint
  654. WHERE US.ID IS NULL
  655. ORDER BY LOT.LotNo,LOT.OPSEQ
  656. ";
  657. //if (!string.IsNullOrEmpty(mocode))
  658. //{
  659. // sql = sql.Replace("--REPLACE--", " AND MO.MOCODE ='" + mocode + "'");
  660. //}
  661. return DBHelper.ExecuteDataset(appConnectString, CommandType.Text, sql).Tables[0];
  662. }
  663. static string APIURL = System.Configuration.ConfigurationSettings.AppSettings["APIURL"].ToString();
  664. static string urlCreatErpPR = APIURL + "sendRequisition";//生成erp请购单
  665. static string urlCreatVisualItem = APIURL + "sendXML";//生成虚拟物料
  666. public static void SendAhwit(string LOTNO, string ROUTECODE, int OPSEQ, string OPCODE, string WorkPoint,DateTime timeCreated,string appConnectString)
  667. {
  668. string msgHead = "";
  669. decimal PRQTY = 0;
  670. decimal CHKOK = 0;
  671. decimal NCROK = 0;
  672. decimal WTIME = 3;
  673. FramDataContext db = new FramDataContext(appConnectString);
  674. db.Connection.Open();
  675. db.Transaction = db.Connection.BeginTransaction();
  676. try
  677. {
  678. //1
  679. var line_cInvCCode = db.Sys_EnumValues.SingleOrDefault(a => a.EnumKey == "00010" && a.WorkPointCode == WorkPoint);
  680. if (line_cInvCCode == null)
  681. {
  682. throw new Exception("未配置虚拟物料类型,00010.");
  683. }
  684. var line_cTranTypeID = db.Sys_EnumValues.SingleOrDefault(a => a.EnumKey == "00110" && a.WorkPointCode == WorkPoint);
  685. if (line_cTranTypeID == null)
  686. {
  687. throw new Exception("未配置请购类型,00110.");
  688. }
  689. //2
  690. var lineITEMLot = db.ICSITEMLot.SingleOrDefault(a => a.LotNO == LOTNO && a.WorkPoint == WorkPoint);
  691. if (lineITEMLot == null)
  692. {
  693. throw new Exception("跟踪单条码已被删除" + LOTNO);
  694. }
  695. var lineInv = db.ICSINVENTORY.SingleOrDefault(a => a.INVCODE == lineITEMLot.ItemCode && a.WorkPoint == WorkPoint);
  696. if (lineInv == null)
  697. {
  698. throw new Exception("物料不存在" + lineITEMLot.ItemCode);
  699. }
  700. var lineOP = db.ICSOP.SingleOrDefault(a => a.OPCODE == OPCODE && a.WorkPoint == WorkPoint);
  701. if (lineOP == null)
  702. {
  703. throw new Exception("工序不存在" + OPCODE);
  704. }
  705. msgHead = "跟踪单:" + LOTNO + ",工序: " + OPCODE + "(" + OPSEQ.ToString() + "), ";
  706. var lineMO2User = db.ICSMO2User.SingleOrDefault(a => a.LOTNO == LOTNO && a.RouteCode == ROUTECODE && a.OPCODE == OPCODE && a.WorkPoint == WorkPoint);
  707. if (lineMO2User == null)
  708. {
  709. throw new Exception("请先派工.");
  710. }
  711. if (!string.IsNullOrEmpty(lineMO2User.PRLineID))
  712. {
  713. throw new Exception("已产生请购单.");
  714. }
  715. if (lineMO2User.EATTRIBUTE1 != "1")
  716. {
  717. throw new Exception("不是委外工序.");
  718. }
  719. var linePreOPCode = db.ICSITEMROUTE2OPLot.SingleOrDefault(a => a.LotNo == LOTNO && a.ROUTECODE == ROUTECODE && a.OPSEQ < OPSEQ && a.WorkPoint == WorkPoint);
  720. if (linePreOPCode == null)
  721. {
  722. //此处的LOTQTY 是分批之后的数量,不用再减去分批数
  723. PRQTY = lineITEMLot.LOTQTY;
  724. }
  725. else
  726. {
  727. //无论上工序是否是委外工序, 都要走开工表 , 对于委外工序不必单独判断
  728. //另外 阿威特 要求即使上工序是委外,也要等委外收料后, 检验完成才能请购本次委外工序
  729. //(注)委外收料时的欠料数(ICSLOTONWIP.RcvNGQTY),记为不良
  730. var linePreOnwip = db.ICSLOTONWIP.SingleOrDefault(a => a.LOTNO == linePreOPCode.LotNo && a.ROUTECODE == linePreOPCode.ROUTECODE && a.OPSEQ == linePreOPCode.OPSEQ && a.OPCODE == linePreOPCode.OPCODE && a.WorkPoint == WorkPoint);
  731. msgHead = "跟踪单:" + LOTNO + ",上道工序: " + linePreOPCode.OPCODE + "(" + linePreOPCode.OPSEQ.ToString() + "), ";
  732. if (linePreOPCode == null)
  733. {
  734. throw new Exception("未开工. ");
  735. }
  736. if (linePreOnwip.ACTIONRESULT != "COLLECT_END")
  737. {
  738. throw new Exception("未完工. ");
  739. }
  740. var linePreCheck = db.ICSLOTONWIPCheck.SingleOrDefault(a => a.FORTRANID == linePreOnwip.ID);
  741. if (linePreCheck == null)
  742. {
  743. throw new Exception("未做完工检验.");
  744. }
  745. if (linePreCheck.EndDateTime == null)
  746. {
  747. throw new Exception("完工检验还在进行中.");
  748. }
  749. CHKOK = (decimal)linePreCheck.OKQuantity;
  750. if (linePreCheck.NGQuantity > 0)
  751. {
  752. List<ICSLOTONWIPCheckDetail> linePreCheckDet = db.ICSLOTONWIPCheckDetail.Where(a => a.FORTRANID == linePreCheck.ID && a.Type == "不良").ToList();
  753. if (linePreCheckDet.Count == 0)
  754. {
  755. throw new Exception("数据获取错误,检验不良详情数据被删除.");
  756. }
  757. foreach (ICSLOTONWIPCheckDetail det in linePreCheckDet)
  758. {
  759. var lineNcr = db.ICSLOTONWIPCheckNCR.SingleOrDefault(a => a.CheckID == det.ID);
  760. if (lineNcr == null)
  761. {
  762. throw new Exception("NCR结果未出.");
  763. }
  764. if (lineNcr.Status == "让步接受")
  765. {
  766. NCROK += Convert.ToDecimal(det.SetValue);
  767. }
  768. }
  769. }
  770. PRQTY = CHKOK + NCROK;
  771. if (PRQTY == 0)
  772. {
  773. throw new Exception("完工检验OK数为 0 .");
  774. }
  775. }
  776. //获取委外工时
  777. var lineSTP = db.ICSEQPSTP.SingleOrDefault(a => a.ITEMCODE == lineITEMLot.ItemCode && a.EATTRIBUTE1 == ROUTECODE && a.OPCODE == OPCODE && a.ISREF == "是" && a.WorkPoint == WorkPoint);
  778. if (lineSTP == null)
  779. {
  780. var lineSTP1 = db.ICSITEMROUTE2OPLot.SingleOrDefault(a => a.LotNo == LOTNO && a.ROUTECODE == ROUTECODE && a.OPSEQ == OPSEQ && a.WorkPoint == WorkPoint);
  781. if (lineSTP1 != null && lineSTP1.WTIME != null)
  782. {
  783. WTIME = (decimal)lineSTP1.WTIME;
  784. }
  785. }
  786. else
  787. {
  788. if (lineSTP.WTIME != null)
  789. {
  790. WTIME = (decimal)lineSTP.WTIME;
  791. }
  792. }
  793. NcApiInputArguments intpuInfo = new NcApiInputArguments();
  794. intpuInfo.datasource = "design";
  795. intpuInfo.usercode = "mestoerp";
  796. intpuInfo.password = "sH05R21u1";
  797. intpuInfo.list = new List<object>();
  798. string pk_group = "Ahwit";
  799. string pk_org = WorkPoint;
  800. string billmaker = "mestoerp";
  801. string code = lineInv.INVCODE + "_" + OPCODE;
  802. string name = lineInv.INVDESC + "_" + lineOP.OPDESC;
  803. string pk_marbasclass = line_cInvCCode.EnumText;
  804. string ctrantypeid = line_cTranTypeID.EnumText;
  805. msgHead = "调用生成虚拟物料接口,";//(注意:此接口只能单个物料调用)
  806. CreateVitemIntity cvItemInfo = new CreateVitemIntity();
  807. cvItemInfo.pk_org = pk_org;
  808. cvItemInfo.code = code;
  809. cvItemInfo.name = name;
  810. cvItemInfo.materialtype = lineInv.INVTYPE;
  811. cvItemInfo.pk_measdoc = lineInv.INVUOM;
  812. cvItemInfo.pk_marbasclass = pk_marbasclass;// "原材料";
  813. cvItemInfo.pk_mattaxes = "一般纳税商品";
  814. cvItemInfo.graphid = "";
  815. intpuInfo.list.Add(cvItemInfo);
  816. string jsonCreatItem = JsonConvert.SerializeObject(intpuInfo);
  817. string resStrCreatItem = HttpPost(urlCreatVisualItem, jsonCreatItem);
  818. NcApiOutArguments res1 = new NcApiOutArguments();
  819. res1 = JsonConvert.DeserializeObject<NcApiOutArguments>(resStrCreatItem);
  820. if (res1.flat != "1")
  821. {
  822. throw new Exception("产生虚拟物料失败!" + res1.list_info);
  823. }
  824. msgHead = "调用生成请购单接口,";
  825. intpuInfo.list.Clear();
  826. CreatePRIntity cPrInfo = new CreatePRIntity();
  827. cPrInfo.pk_group = pk_group;
  828. cPrInfo.pk_org = pk_org;
  829. cPrInfo.billmaker = billmaker;
  830. cPrInfo.ccurrencyid = "人民币";
  831. cPrInfo.ctrantypeid = ctrantypeid;
  832. cPrInfo.dbilldate = timeCreated.ToString("yyyy-MM-dd HH:mm:ss");
  833. cPrInfo.time = timeCreated.ToString("yyyy-MM-dd HH:mm:ss");
  834. cPrInfo.list = new List<CreatePRIntityLine>();
  835. CreatePRIntityLine line = new CreatePRIntityLine();
  836. line.pk_group = cPrInfo.pk_group;
  837. line.pk_org = cPrInfo.pk_org;
  838. line.crowno = "1";
  839. line.pk_purchaseorg = cPrInfo.pk_org;
  840. line.pk_srcmaterial = code;
  841. line.pk_material = code;
  842. line.nastnum = PRQTY.ToString();
  843. line.castunitid = "个";
  844. line.vchangerate = "1/1";
  845. line.dreqdate = timeCreated.AddDays((double)WTIME).ToString();
  846. line.dsuggestdate = timeCreated.ToString("yyyy-MM-dd HH:mm:ss");
  847. line.vbmemo = LOTNO;
  848. line.vsourcecode = "";
  849. line.vsourcerowno = "";
  850. cPrInfo.list.Add(line);
  851. intpuInfo.list.Add(cPrInfo);
  852. string jsonCreatErpPR = JsonConvert.SerializeObject(intpuInfo);
  853. string resStrCreatErpPR = HttpPost(urlCreatErpPR, jsonCreatErpPR);
  854. NcApiOutArguments res2 = new NcApiOutArguments();
  855. res2 = JsonConvert.DeserializeObject<NcApiOutArguments>(resStrCreatErpPR);
  856. if (res2.flat != "1")
  857. {
  858. throw new Exception("产生委外请购单失败!" + res2.list_info);
  859. }
  860. msgHead = "回写PRLineID至mes,";
  861. List<CreatePRIntityReturn> _list = new List<CreatePRIntityReturn>();
  862. if (string.IsNullOrWhiteSpace(res2.list_info.ToString()))
  863. {
  864. throw new Exception("未获取到请购单数据!");
  865. }
  866. _list = JsonConvert.DeserializeObject<List<CreatePRIntityReturn>>(res2.list_info.ToString());
  867. foreach (var item in _list)
  868. {
  869. string itemcodeReturn = item.VirtualItem.Split('_')[0].ToUpper();
  870. string opcodeReturn = item.VirtualItem.Split('_')[1].ToUpper();
  871. lineMO2User.PRLineID = item.PrLine;
  872. lineMO2User.PRQTY = PRQTY;
  873. db.SubmitChanges();
  874. }
  875. db.SubmitChanges();
  876. db.Transaction.Commit();
  877. }
  878. catch (Exception ex)
  879. {
  880. db.Transaction.Rollback();
  881. throw new Exception(msgHead + ex.Message);
  882. }
  883. finally
  884. {
  885. db.Connection.Close();
  886. }
  887. }
  888. private static string HttpPost(string url, string body)
  889. {
  890. try
  891. {
  892. Encoding encoding = Encoding.UTF8;
  893. System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
  894. request.Method = "POST";
  895. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  896. request.ContentType = "application/json; charset=utf-8";
  897. byte[] buffer = encoding.GetBytes(body);
  898. request.ContentLength = buffer.Length;
  899. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  900. System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
  901. using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
  902. {
  903. return reader.ReadToEnd();
  904. }
  905. }
  906. catch (System.Net.WebException ex)
  907. {
  908. throw new Exception(ex.Message);
  909. }
  910. }
  911. }
  912. }