华恒Mes鼎捷代码
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.

319 lines
12 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. namespace ICSSoft.Frame.Data.DAL
  14. {
  15. public class ICSMO2UserDAL
  16. {
  17. #region 增加修改
  18. public static void AddAndEdit(List<FormICSMO2UserUIModel> MO2UserInfoList, string dsconn)
  19. {
  20. FramDataContext db = new FramDataContext(dsconn);
  21. db.Connection.Open();
  22. db.Transaction = db.Connection.BeginTransaction();
  23. try
  24. {
  25. foreach (FormICSMO2UserUIModel MO2UserInfo in MO2UserInfoList)
  26. {
  27. bool isNew = false;
  28. var line = db.ICSMO2User.SingleOrDefault(a => a.LOTNO == MO2UserInfo.LOTNO && a.OPCODE == MO2UserInfo.OPCODE);
  29. //var line = db.ICSMO2User.SingleOrDefault(a => a.LOTNO == MO2UserInfo.LOTNO );
  30. if (line == null)
  31. {
  32. isNew = true;
  33. line = new ICSMO2User();
  34. line.ID = AppConfig.GetGuid();
  35. }
  36. line.MOCODE = MO2UserInfo.MOCODE;
  37. line.MOSEQ = MO2UserInfo.MOSEQ;
  38. line.LOTNO = MO2UserInfo.LOTNO;
  39. line.SEGCODE = MO2UserInfo.SEGCODE;
  40. line.RouteCode = MO2UserInfo.RouteCode;
  41. line.OPCODE = MO2UserInfo.OPCODE;
  42. line.USERCODE = MO2UserInfo.USERCODE;
  43. line.USERName = MO2UserInfo.USERName;
  44. line.EQPCode = MO2UserInfo.EQPCode;
  45. line.EQPName = MO2UserInfo.EQPName;
  46. line.MUSER = MO2UserInfo.MUSER;
  47. line.MUSERName = MO2UserInfo.MUSERName;
  48. line.MTIME = Convert.ToDateTime(MO2UserInfo.MTIME);
  49. line.WorkPoint = MO2UserInfo.WorkPoint;
  50. line.EATTRIBUTE1 = MO2UserInfo.EATTRIBUTE1;
  51. line.StartPlanDate = MO2UserInfo.StartPlanDate;
  52. line.EndPlanDate = MO2UserInfo.EndPlanDate;
  53. if (MO2UserInfo.PRLineID == "False" || MO2UserInfo.PRLineID == "0" || string.IsNullOrWhiteSpace(MO2UserInfo.PRLineID))
  54. {
  55. line.PRLineID = null;
  56. }
  57. else
  58. {
  59. line.PRLineID = MO2UserInfo.PRLineID;
  60. }
  61. if (isNew)
  62. db.ICSMO2User.InsertOnSubmit(line);
  63. db.SubmitChanges();
  64. }
  65. db.SubmitChanges();
  66. db.Transaction.Commit();
  67. }
  68. catch (Exception ex)
  69. {
  70. db.Transaction.Rollback();
  71. throw new Exception(ex.Message);
  72. }
  73. }
  74. #endregion
  75. #region 删除
  76. public static void deleteInfo(List<string> codeList, string dsconn)
  77. {
  78. FramDataContext db = new FramDataContext(dsconn);
  79. db.Connection.Open();
  80. db.Transaction = db.Connection.BeginTransaction();
  81. try
  82. {
  83. var lines = db.ICSMO2User.Where(a => codeList.Contains(a.ID));
  84. db.ICSMO2User.DeleteAllOnSubmit(lines);
  85. db.SubmitChanges();
  86. db.Transaction.Commit();
  87. }
  88. catch (Exception ex)
  89. {
  90. db.Transaction.Rollback();
  91. throw ex;
  92. }
  93. }
  94. #endregion
  95. #region 取消下发
  96. public static void cancelSend(List<string> codeList, string dsconn)
  97. {
  98. FramDataContext db = new FramDataContext(dsconn);
  99. db.Connection.Open();
  100. db.Transaction = db.Connection.BeginTransaction();
  101. try
  102. {
  103. var lotonwip = db.ICSLOTONWIP.Where(a => codeList.Contains(a.LOTNO));
  104. if (lotonwip != null && lotonwip.ToList().Count > 0)
  105. {
  106. throw new Exception("批次已经采集,不可取消派工");
  107. }
  108. var lines = db.ICSMO2User.Where(a => codeList.Contains(a.LOTNO));
  109. db.ICSMO2User.DeleteAllOnSubmit(lines);
  110. db.SubmitChanges();
  111. db.Transaction.Commit();
  112. }
  113. catch (Exception ex)
  114. {
  115. db.Transaction.Rollback();
  116. throw ex;
  117. }
  118. }
  119. public static void cancelSend(List<string> lotno1, List<string> opcode1, string dsconn)
  120. {
  121. FramDataContext db = new FramDataContext(dsconn);
  122. db.Connection.Open();
  123. db.Transaction = db.Connection.BeginTransaction();
  124. try
  125. {
  126. var lotonwip1 = (from a in db.ICSLOTONWIP
  127. where lotno1.Contains(a.LOTNO)
  128. select a).ToList();
  129. var lotonwip = lotonwip1.Where(a => opcode1.Contains(a.OPCODE));
  130. if (lotonwip != null && lotonwip.ToList().Count > 0)
  131. {
  132. throw new Exception("批次已经采集,不可取消派工");
  133. }
  134. List<ICSMO2User> list = new List<ICSMO2User>();
  135. for (int i = 0; i < lotno1.Count(); i++)
  136. {
  137. ICSMO2User[] b = (from a in db.ICSMO2User
  138. where a.LOTNO.Equals(lotno1[i]) && a.OPCODE.Equals(opcode1[i])
  139. select a).ToArray();
  140. list.Add(b[0]);
  141. }
  142. db.ICSMO2User.DeleteAllOnSubmit(list);
  143. db.SubmitChanges();
  144. db.Transaction.Commit();
  145. }
  146. catch (Exception ex)
  147. {
  148. db.Transaction.Rollback();
  149. throw ex;
  150. }
  151. }
  152. public static void cancelSendWW(List<string> lotno1, List<string> opcode1, string dsconn)
  153. {
  154. FramDataContext db = new FramDataContext(dsconn);
  155. db.Connection.Open();
  156. db.Transaction = db.Connection.BeginTransaction();
  157. try
  158. {
  159. List<ICSMO2User> list = new List<ICSMO2User>();
  160. for (int i = 0; i < lotno1.Count(); i++)
  161. {
  162. ICSMO2User[] b = (from a in db.ICSMO2User
  163. where a.LOTNO.Equals(lotno1[i]) && a.OPCODE.Equals(opcode1[i])
  164. select a).ToArray();
  165. list.Add(b[0]);
  166. if (db.ICSLOTONWIP.Where(a => a.MOCODE == b[0].MOCODE && a.OPCODE == b[0].OPCODE).FirstOrDefault() != null)
  167. {
  168. throw new Exception("批次:" + b[0].LOTNO + ",工序:" + b[0].OPCODE + "已经采集,不可取消派工");
  169. }
  170. }
  171. var pos = list.Where(a => string.IsNullOrEmpty(a.PRLineID) && a.PRLineID.Contains("-")).Select(a => a.PRLineID.Substring(0, a.PRLineID.LastIndexOf('-'))).Distinct().ToList();//去重后的po单
  172. foreach (var po in pos)
  173. {
  174. var mo2users = db.ICSMO2User.Where(a => a.PRLineID.Contains(po)).ToList();
  175. foreach (var mo2user in mo2users)
  176. {
  177. var lotonwip = db.ICSLOTONWIP.Where(a => a.LOTNO == mo2user.LOTNO && a.OPCODE == mo2user.OPCODE).FirstOrDefault();
  178. if (lotonwip != null)
  179. {
  180. throw new Exception("批次:" + lotonwip.LOTNO + ",工序:" + lotonwip.OPCODE + "已经采集,不可取消派工");
  181. }
  182. }
  183. }
  184. db.ICSPO_PoMain.DeleteAllOnSubmit(db.ICSPO_PoMain.Where(a => pos.Contains(a.POCode)));//删除mes采购单
  185. db.ICSMO2User.DeleteAllOnSubmit(list);//删除派工记录
  186. db.SubmitChanges();
  187. db.Transaction.Commit();
  188. }
  189. catch (Exception ex)
  190. {
  191. db.Transaction.Rollback();
  192. throw ex;
  193. }
  194. }
  195. #endregion
  196. #region 写入日志
  197. public static void WriteLogFile(string input, string txtName)
  198. {
  199. try
  200. {
  201. string logAdress = ConfigurationManager.AppSettings["logAdress"].ToString() + "\\Log\\";
  202. if (!System.IO.Directory.Exists(logAdress))
  203. {
  204. System.IO.Directory.CreateDirectory(logAdress);//不存在就创建目录
  205. }
  206. string adress = logAdress + txtName;
  207. if (!System.IO.Directory.Exists(adress))
  208. {
  209. System.IO.Directory.CreateDirectory(adress);//不存在就创建目录
  210. }
  211. // string logAdress = ConfigurationManager.AppSettings["logAdress"].ToString();
  212. /**/
  213. ///指定日志文件的目录
  214. string fname = adress + "\\" + "log" + DateTime.Now.ToString("yy-MM-dd") + ".txt";
  215. /**/
  216. ///定义文件信息对象
  217. FileInfo finfo = new FileInfo(fname);
  218. if (!finfo.Exists)
  219. {
  220. FileStream fs;
  221. fs = File.Create(fname);
  222. fs.Close();
  223. finfo = new FileInfo(fname);
  224. }
  225. /**/
  226. ///判断文件是否存在以及是否大于2K
  227. if (finfo.Length > 1024 * 1024 * 10)
  228. {
  229. /**/
  230. ///文件超过10MB则重命名
  231. File.Move(logAdress + "\\Log\\" + txtName + ".txt", Directory.GetCurrentDirectory() + DateTime.Now.TimeOfDay + "\\Log\\" + txtName + ".txt");
  232. /**/
  233. ///删除该文件
  234. //finfo.Delete();
  235. }
  236. //finfo.AppendText();
  237. /**/
  238. ///创建只写文件流
  239. using (FileStream fs = finfo.OpenWrite())
  240. {
  241. /**/
  242. ///根据上面创建的文件流创建写数据流
  243. StreamWriter w = new StreamWriter(fs);
  244. /**/
  245. ///设置写数据流的起始位置为文件流的末尾
  246. w.BaseStream.Seek(0, SeekOrigin.End);
  247. w.WriteLine("*****************Start*****************");
  248. w.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  249. /**/
  250. ///写入当前系统时间并换行
  251. /**/
  252. ///写入日志内容并换行
  253. w.WriteLine(input);
  254. /**/
  255. ///写入------------------------------------“并换行
  256. w.WriteLine("------------------END------------------------");
  257. /**/
  258. ///清空缓冲区内容,并把缓冲区内容写入基础流
  259. w.Flush();
  260. /**/
  261. ///关闭写数据流
  262. w.Close();
  263. }
  264. }
  265. catch (Exception ex)
  266. { throw ex; }
  267. }
  268. #endregion
  269. }
  270. }