飞依诺接口
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.

416 lines
22 KiB

2 years ago
  1. using ICSSoft.ERPWMS.Entity;
  2. using Microsoft.Data.SqlClient;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace ICSSoft.ERPWMS.SQL
  11. {
  12. public class CreateOrDeleteInventoryLot
  13. {
  14. public static Result Create(List<CreateInventoryLotEntity> entityList)
  15. {
  16. Result res = new Result();
  17. try
  18. {
  19. string jsonstr = JsonConvert.SerializeObject(entityList);
  20. Log.WriteLogFile(jsonstr, "创建删除条码日志");
  21. StringBuilder sb = new StringBuilder();//接口返回Message
  22. foreach (CreateInventoryLotEntity entity in entityList)
  23. {
  24. SqlConnection conn = new SqlConnection(ICSHelper.ReadConfig(ICSHelper.FileNameCompanyCon)["WMS"].ToString());
  25. conn.Open();
  26. SqlTransaction sqlTran = conn.BeginTransaction();
  27. SqlCommand cmd = new SqlCommand();
  28. cmd.Transaction = sqlTran;
  29. cmd.Connection = conn;
  30. try
  31. {
  32. if (string.IsNullOrWhiteSpace(entity.LotNo))
  33. {
  34. throw new Exception("存在物料条码为空!");
  35. }
  36. if (string.IsNullOrWhiteSpace(entity.InvCode))
  37. {
  38. throw new Exception("物料编码为空!");
  39. }
  40. if (string.IsNullOrWhiteSpace(entity.TransNo))
  41. {
  42. throw new Exception("来源单据号为空!");
  43. }
  44. if (entity.TransLine == null)
  45. {
  46. throw new Exception("来源单据行号为空!");
  47. }
  48. if (entity.Quantity == null)
  49. {
  50. throw new Exception("数量为空!");
  51. }
  52. if (string.IsNullOrWhiteSpace(entity.Type))
  53. {
  54. throw new Exception("类型为空!");
  55. }
  56. if (string.IsNullOrWhiteSpace(entity.WorkPoint))
  57. {
  58. throw new Exception("站点为空!");
  59. }
  60. if (string.IsNullOrWhiteSpace(entity.SRMLotGroup))
  61. {
  62. throw new Exception("SRM条码Group为空为空!");
  63. }
  64. string WorkPoint = ICSHelper.GetConnectStringTest(entity.WorkPoint);
  65. if (WorkPoint == "NotExit")
  66. {
  67. throw new Exception("站点编码不存在!");
  68. }
  69. string sqlLot = "Select LotNo,ExtensionID from ICSInventoryLot where workpoint='{0}' and LotNo='{1}'";
  70. sqlLot = string.Format(sqlLot, WorkPoint, entity.LotNo);
  71. DataTable dtLot = ICSHelper.SQlReturnData(sqlLot, cmd);
  72. if (dtLot != null && dtLot.Rows.Count > 0)
  73. {
  74. //检查条码是否绑定送货单,若绑定则不能删除
  75. string sqlASN = "Select LotNo from ICSASNDetail Where workpoint='{0}' and LotNo ='{1}'";
  76. sqlASN = string.Format(sqlASN, WorkPoint, entity.LotNo);
  77. DataTable dtASN = ICSHelper.SQlReturnData(sqlASN, cmd);
  78. if (dtASN != null && dtASN.Rows.Count > 0)
  79. {
  80. throw new Exception("条码已绑定送货单,不能删除!");
  81. }
  82. //删除条码表表头
  83. string sqlLotDelete = "Delete ICSInventoryLot where workpoint='{0}' and LotNo='{1}'";
  84. sqlLotDelete = string.Format(sqlLotDelete, WorkPoint, dtLot.Rows[0]["LotNo"].ToString());
  85. Log.WriteLogFile(sqlLotDelete, "创建删除条码SQL日志");
  86. if (!ICSHelper.ExecuteNonQuery(sqlLotDelete, cmd))
  87. {
  88. throw new Exception("删除条码表表头失败!");
  89. }
  90. //删除条码表表体
  91. string sqlLotDetailDelete = "Delete ICSInventoryLotDetail where workpoint='{0}' and LotNo='{1}'";
  92. sqlLotDetailDelete = string.Format(sqlLotDetailDelete, WorkPoint, dtLot.Rows[0]["LotNo"].ToString());
  93. Log.WriteLogFile(sqlLotDetailDelete, "创建删除条码SQL日志");
  94. if (!ICSHelper.ExecuteNonQuery(sqlLotDetailDelete, cmd))
  95. {
  96. throw new Exception("删除条码表表体失败!");
  97. }
  98. //删除自由项表
  99. //string sqlCheck = "Select * from ICSInventoryLot where ExtensionID ='" + dtLot.Rows[0]["ExtensionID"].ToString() + "' and";
  100. //DataTable dtCheck = ICSHelper.SQlReturnData(sqlCheck, cmd);
  101. //if (dtCheck.Rows.Count < 1)
  102. //{
  103. // string sqlFreeDelete = "Delete ICSExtension where workpoint='{0}' and ID ='{1}'";
  104. // sqlFreeDelete = string.Format(sqlFreeDelete, WorkPoint, dtLot.Rows[0]["ExtensionID"].ToString());
  105. // Log.WriteLogFile(sqlFreeDelete, "创建删除条码SQL日志");
  106. // if (!ICSHelper.ExecuteNonQuery(sqlFreeDelete, cmd))
  107. // {
  108. // throw new Exception("删除自由项表失败!");
  109. // }
  110. //}
  111. #region 新增条码
  112. string sqlInvCheck = "Select InvCode from ICSInventory Where workpoint='{0}' and InvCode ='{1}'";
  113. sqlInvCheck = string.Format(sqlInvCheck, WorkPoint, entity.InvCode);
  114. DataTable dtInvCheck = ICSHelper.SQlReturnData(sqlInvCheck, cmd);
  115. if (dtInvCheck.Rows.Count < 1)
  116. {
  117. throw new Exception("物料代码不存在!");
  118. }
  119. if (entity.LotNo.StartsWith("WW"))
  120. {
  121. entity.Type = "8";
  122. }
  123. else
  124. {
  125. entity.Type = "7";
  126. }
  127. String EffectiveEnable = "";
  128. String Colspan = "";
  129. String IDD = "";
  130. int EffectiveDays = 0;
  131. String Time = "";
  132. DateTime? dtt;
  133. DateTime now = DateTime.Now;
  134. string sql = @"select EffectiveEnable,EffectiveDays from ICSInventory
  135. where InvCode='{0}' and WorkPoint='{1}'";
  136. sql = string.Format(sql, entity.InvCode, WorkPoint);
  137. DataTable dt = ICSHelper.SQlReturnData(sql, cmd);
  138. if (dt.Rows.Count == 0)
  139. {
  140. throw new Exception("有效天数为空!");
  141. }
  142. else
  143. {
  144. EffectiveEnable = dt.Rows[0]["EffectiveEnable"].ToString();
  145. EffectiveDays = Convert.ToInt32(dt.Rows[0]["EffectiveDays"]);
  146. }
  147. //EffectiveDayss = Convert.ToInt32(EffectiveDays);
  148. if (!EffectiveEnable.Equals("False"))
  149. {
  150. Time = Convert.ToDateTime(entity.ProductDate).AddDays(EffectiveDays).ToString();
  151. }
  152. else
  153. {
  154. Time = "2999-12-31 00:00:00";
  155. }
  156. dtt = Convert.ToDateTime(Time);
  157. if (entity.dDisableDate != null)
  158. {
  159. dtt = entity.dDisableDate;
  160. }
  161. //检验自由项
  162. Colspan = "" + "~" + entity.BatchNo + "~" + ""
  163. + "~" + "" + "~" + entity.Extension1
  164. + "~" + entity.Extension2 + "~" + entity.Extension3 + "~" + entity.Extension4
  165. + "~" + entity.Extension5 + "~" + "" + "~" + ""
  166. + "~" + "" + "~" + "" + "~" + "";
  167. sql = @"select ID,Colspan from ICSExtension a
  168. where Colspan='{0}' and WorkPoint='{1}'";
  169. sql = string.Format(sql, Colspan, WorkPoint);
  170. DataTable dttt = ICSHelper.SQlReturnData(sql, cmd);
  171. if (dttt.Rows.Count == 0)
  172. {
  173. IDD = Guid.NewGuid().ToString();
  174. sql = @"Insert into ICSExtension(ID, Colspan, ProjectCode, BatchCode, Version, Brand, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10, MTIME, MUSER, MUSERName, WorkPoint)
  175. select '{18}','{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}',GETDATE(),'{15}','{16}','{17}'";
  176. sql = string.Format(sql, Colspan, "", entity.BatchNo, "", "", entity.Extension1, entity.Extension2, entity.Extension3,
  177. entity.Extension4, entity.Extension5, "", "", "", "", "", entity.MUSER, entity.MUSERName, WorkPoint, IDD);
  178. Log.WriteLogFile(sql, "创建删除条码SQL日志");
  179. if (!ICSHelper.ExecuteNonQuery(sql, cmd))
  180. {
  181. throw new Exception("新增自由项失败!");
  182. }
  183. }
  184. else
  185. {
  186. IDD = dttt.Rows[0]["ID"].ToString();
  187. }
  188. //存入条码表
  189. sql = @"
  190. INSERT INTO ICSInventoryLot(ID,LotNo,InvCode,ProductDate,ExpirationDate,
  191. Quantity,Amount,ExtensionID,Type,PrintTimes,
  192. LastPrintUser,LastPrintTime,MUSER,MUSERName,MTIME,
  193. WorkPoint,EATTRIBUTE1)
  194. SELECT TOP 1 NEWID(),'{0}','{1}','{2}','{3}',
  195. '{4}','{5}','{6}','{7}','{8}',
  196. '{9}','{10}','{11}','{12}','{13}',
  197. '{14}','{15}'";
  198. sql = string.Format(sql, entity.LotNo, entity.InvCode, entity.ProductDate, dtt, entity.Quantity, entity.AmountRate, IDD,
  199. entity.Type, entity.PrintTimes, entity.LastPrintUser, entity.LastPrintTime, entity.MUSER,
  200. entity.MUSERName, entity.MTIME, WorkPoint, entity.SRMLotGroup);
  201. Log.WriteLogFile(sql, "创建删除条码SQL日志");
  202. if (!ICSHelper.ExecuteNonQuery(sql, cmd))
  203. {
  204. throw new Exception("新增条码表表头失败!");
  205. }
  206. ///存入条码关联表
  207. sql = @"
  208. INSERT INTO ICSInventoryLotDetail(LotNo,TransCode,TransSequence,MUSER,MUSERName,MTIME,
  209. WorkPoint,EATTRIBUTE1)
  210. SELECT '{0}','{1}','{2}','{3}','{4}','{5}','{6}',''
  211. ";
  212. sql = string.Format(sql, entity.LotNo, entity.TransNo, entity.TransLine, entity.MUSER, entity.MUSERName, entity.MTIME, WorkPoint);
  213. Log.WriteLogFile(sql, "创建删除条码SQL日志");
  214. if (!ICSHelper.ExecuteNonQuery(sql, cmd))
  215. {
  216. throw new Exception("新增条码表表体失败!");
  217. }
  218. #endregion
  219. }
  220. else
  221. {
  222. //if (!string.IsNullOrWhiteSpace(entity.MUSER))
  223. //{
  224. // string sqlUser = "Select F_RealName from Sys_SRM_User where f_location='{0}' and F_Account='{1}'";
  225. // sqlUser = string.Format(sqlUser, WorkPoint, entity.MUSER);
  226. // DataTable dtUser = ICSHelper.SQlReturnData(sqlUser, cmd);
  227. // if (dtUser != null && dtUser.Rows.Count > 0)
  228. // {
  229. // if (!string.IsNullOrWhiteSpace(entity.MUSERName))
  230. // {
  231. // if (dtUser.Rows[0]["F_RealName"].ToString() != entity.MUSERName)
  232. // {
  233. // throw new Exception("操作人编码对应的人员名称与传入人员名称不一致!");
  234. // }
  235. // else
  236. // {
  237. // entity.MUSERName = dtUser.Rows[0]["F_RealName"].ToString();
  238. // }
  239. // }
  240. // }
  241. // else
  242. // {
  243. // throw new Exception("操作人不存在!");
  244. // }
  245. //}
  246. string sqlInvCheck = "Select InvCode from ICSInventory Where workpoint='{0}' and InvCode ='{1}'";
  247. sqlInvCheck = string.Format(sqlInvCheck, WorkPoint, entity.InvCode);
  248. DataTable dtInvCheck = ICSHelper.SQlReturnData(sqlInvCheck, cmd);
  249. if (dtInvCheck.Rows.Count < 1)
  250. {
  251. throw new Exception("物料代码不存在!");
  252. }
  253. String EffectiveEnable = "";
  254. String Colspan = "";
  255. String IDD = "";
  256. int EffectiveDays = 0;
  257. String Time = "";
  258. DateTime? dtt;
  259. DateTime now = DateTime.Now;
  260. string sql = @"select EffectiveEnable,EffectiveDays from ICSInventory
  261. where InvCode='{0}' and WorkPoint='{1}'";
  262. sql = string.Format(sql, entity.InvCode, WorkPoint);
  263. DataTable dt = ICSHelper.SQlReturnData(sql, cmd);
  264. if (dt.Rows.Count == 0)
  265. {
  266. throw new Exception("有效天数为空!");
  267. }
  268. else
  269. {
  270. EffectiveEnable = dt.Rows[0]["EffectiveEnable"].ToString();
  271. EffectiveDays = Convert.ToInt32(dt.Rows[0]["EffectiveDays"]);
  272. }
  273. //EffectiveDayss = Convert.ToInt32(EffectiveDays);
  274. if (!EffectiveEnable.Equals("False"))
  275. {
  276. Time = Convert.ToDateTime(entity.ProductDate).AddDays(EffectiveDays).ToString();
  277. }
  278. else
  279. {
  280. Time = "2999-12-31 00:00:00";
  281. }
  282. dtt = Convert.ToDateTime(Time);
  283. if (entity.dDisableDate != null)
  284. {
  285. dtt = entity.dDisableDate;
  286. }
  287. //检验自由项
  288. Colspan = "" + "~" + entity.BatchNo + "~" + ""
  289. + "~" + "" + "~" + entity.Extension1
  290. + "~" + entity.Extension2 + "~" + entity.Extension3 + "~" + entity.Extension4
  291. + "~" + entity.Extension5 + "~" + "" + "~" + ""
  292. + "~" + "" + "~" + "" + "~" + "";
  293. sql = @"select ID,Colspan from ICSExtension a
  294. where Colspan='{0}' and WorkPoint='{1}'";
  295. sql = string.Format(sql, Colspan, WorkPoint);
  296. DataTable dttt = ICSHelper.SQlReturnData(sql, cmd);
  297. if (dttt.Rows.Count == 0)
  298. {
  299. IDD = Guid.NewGuid().ToString();
  300. sql = @"Insert into ICSExtension(ID, Colspan, ProjectCode, BatchCode, Version, Brand, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10, MTIME, MUSER, MUSERName, WorkPoint)
  301. select '{18}','{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}',GETDATE(),'{15}','{16}','{17}'";
  302. sql = string.Format(sql, Colspan, "", entity.BatchNo, "", "", entity.Extension1, entity.Extension2, entity.Extension3,
  303. entity.Extension4, entity.Extension5, "", "", "", "", "", entity.MUSER, entity.MUSERName, WorkPoint, IDD);
  304. Log.WriteLogFile(sql, "创建删除条码SQL日志");
  305. if (!ICSHelper.ExecuteNonQuery(sql, cmd))
  306. {
  307. throw new Exception("新增自由项失败!");
  308. }
  309. }
  310. else
  311. {
  312. IDD = dttt.Rows[0]["ID"].ToString();
  313. }
  314. //存入条码表
  315. sql = @"
  316. INSERT INTO ICSInventoryLot(ID,LotNo,InvCode,ProductDate,ExpirationDate,
  317. Quantity,Amount,ExtensionID,Type,PrintTimes,
  318. LastPrintUser,LastPrintTime,MUSER,MUSERName,MTIME,
  319. WorkPoint,EATTRIBUTE1)
  320. SELECT TOP 1 NEWID(),'{0}','{1}','{2}','{3}',
  321. '{4}','{5}','{6}','{7}','{8}',
  322. '{9}','{10}','{11}','{12}','{13}',
  323. '{14}','{15}'";
  324. sql = string.Format(sql, entity.LotNo, entity.InvCode, entity.ProductDate, dtt, entity.Quantity, entity.AmountRate, IDD,
  325. entity.Type, entity.PrintTimes, entity.LastPrintUser, entity.LastPrintTime, entity.MUSER,
  326. entity.MUSERName, entity.MTIME, WorkPoint, entity.SRMLotGroup);
  327. Log.WriteLogFile(sql, "创建删除条码SQL日志");
  328. if (!ICSHelper.ExecuteNonQuery(sql, cmd))
  329. {
  330. throw new Exception("新增条码表表头失败!");
  331. }
  332. ///存入条码关联表
  333. sql = @"
  334. INSERT INTO ICSInventoryLotDetail(LotNo,TransCode,TransSequence,MUSER,MUSERName,MTIME,
  335. WorkPoint,EATTRIBUTE1)
  336. SELECT '{0}','{1}','{2}','{3}','{4}','{5}','{6}',''
  337. ";
  338. sql = string.Format(sql, entity.LotNo, entity.TransNo, entity.TransLine, entity.MUSER, entity.MUSERName, entity.MTIME, WorkPoint);
  339. Log.WriteLogFile(sql, "创建删除条码SQL日志");
  340. if (!ICSHelper.ExecuteNonQuery(sql, cmd))
  341. {
  342. throw new Exception("新增条码表表体失败!");
  343. }
  344. }
  345. cmd.Transaction.Commit();
  346. }
  347. catch (Exception ex)
  348. {
  349. cmd.Transaction.Rollback();
  350. sb.Append("执行报错,条码为:" + entity.LotNo + ",报错信息:" + ex.Message + "!!!");
  351. continue;
  352. }
  353. finally
  354. {
  355. if (conn.State == ConnectionState.Open)
  356. {
  357. conn.Close();
  358. }
  359. conn.Dispose();
  360. }
  361. }
  362. if (sb.Length > 0)
  363. {
  364. res.IsSuccess = false;
  365. res.Message = sb.ToString();
  366. }
  367. else
  368. {
  369. res.IsSuccess = true;
  370. res.Message = "执行成功!";
  371. }
  372. return res;
  373. }
  374. catch (Exception ex)
  375. {
  376. res.IsSuccess = false;
  377. res.Message = ex.Message;
  378. return res;
  379. }
  380. }
  381. }
  382. }