AppCoreBLL.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using WMS.Core._02Entity;
  8. using WMS.Info;
  9. using WMS.Info.Models;
  10. using WMS.Util;
  11. namespace WMS.Core
  12. {
  13. public class AppCoreBLL
  14. {
  15. private bool IsSkip;
  16. public string token;
  17. public AppCoreBLL()
  18. {
  19. //token = (string)System.Runtime.Remoting.Messaging.CallContext.GetData("token");
  20. }
  21. public AppCoreBLL(bool skipOauth)
  22. {
  23. IsSkip = skipOauth;
  24. // token = (string)System.Runtime.Remoting.Messaging.CallContext.GetData("token");
  25. }
  26. private LoginUserInfo _LoginUser;
  27. public LoginUserInfo LoginUser
  28. {
  29. get
  30. {
  31. if (_LoginUser != null) return _LoginUser;
  32. if (IsSkip)
  33. {
  34. _LoginUser = new LoginUserInfo { UserNo = "wcs" };
  35. return _LoginUser;
  36. }
  37. if (string.IsNullOrWhiteSpace(token))
  38. {
  39. #if DEBUG
  40. _LoginUser = new LoginUserInfo { UserNo = "TEST" };
  41. # else
  42. throw SysExCore.ThrowToken();
  43. #endif
  44. }
  45. else
  46. {
  47. _LoginUser= LoginBLLCore.GetLoginUser(token);
  48. }
  49. return _LoginUser;
  50. }
  51. }
  52. private HttpHelper _httpHelper;
  53. public HttpHelper httpClinet
  54. {
  55. get
  56. {
  57. if (_httpHelper == null)
  58. _httpHelper = new HttpHelper(SysSetCore.GetSysSet().WCSApiUrl);
  59. return _httpHelper;
  60. }
  61. }
  62. /// <summary>
  63. /// 获取列表数据
  64. /// <summary>
  65. /// <returns></returns>
  66. protected List<FX_sod_det> GetList(string trayNo)
  67. {
  68. try
  69. {
  70. return SysDbCore.GetDbCtx().Queryable<FX_sod_det>().Where(it => it.sod_part == trayNo).ToList();
  71. }
  72. catch (Exception ex)
  73. {
  74. throw ex;
  75. }
  76. }
  77. /// <summary>
  78. /// 根据点位获取类型
  79. /// <param name="keyValue">主键</param>
  80. /// <summary>
  81. /// <returns></returns>
  82. protected BASE_POINT GetPointInfo(string pointNo)
  83. {
  84. try
  85. {
  86. return SysDbCore.GetDbCtx().Queryable<BASE_POINT>().Where(it => it.F_isDelete == 0 && it.F_no == pointNo).First();
  87. }
  88. catch (Exception ex)
  89. {
  90. throw ex;
  91. }
  92. }
  93. /// <summary>
  94. /// 根据类型获取空置点位
  95. /// <param name="keyValue">主键</param>
  96. /// <summary>
  97. /// <returns></returns>
  98. public BASE_POINT GetFreePointInfoByType(int type)
  99. {
  100. var point = SysDbCore.GetDbCtx().Queryable<BASE_POINT>().Where(it => it.F_isDelete == 0 && it.F_type == type && it.F_status == (int)EPointSatus.Idle).First();
  101. if (point == null)
  102. throw SysExCore.ThrowFailException("没有空闲的点位!");
  103. //point.F_status = (int)EPointSatus.Occupied;
  104. //SysDbCore.GetDbCtx().Updateable(point).UpdateColumns(it => new { it.F_status }).WhereColumns(it => it.F_no == point.F_no).ExecuteCommand();
  105. return point;
  106. }
  107. /// <summary>
  108. /// 根据类型获取空置货位
  109. /// <param name="keyValue">主键</param>
  110. /// <summary>
  111. /// <returns></returns>
  112. public BASE_LOCATION GetFreeLocationByType(int type)
  113. {
  114. var locEntity = SysDbCore.GetDbCtx().Queryable<BASE_LOCATION>().Where(it => it.F_isDelete == 0 && it.F_type == type && it.F_status == (int)EWareCellState.Empty).First();
  115. if (locEntity == null)
  116. throw SysExCore.ThrowFailException("没有闲置的货位!");
  117. return locEntity;
  118. }
  119. public ResInfo LocationGenerate(GenerateLocationRequest request)
  120. {
  121. SqlSugarClient ctx = SysDbCore.GetDbCtx();
  122. try
  123. {
  124. ctx.BeginTran();
  125. for (int x = request.StartLine.HasValue ? request.StartLine.Value : 1; x <= request.Line; x++)
  126. {
  127. for (int y = request.StartCell.HasValue ? request.StartCell.Value : 1; y <= request.Cell; y++)
  128. {
  129. if (request.SkinLineCell.Any(a => a.Key == x && a.Value == y))
  130. continue;
  131. var location = new BASE_LOCATION
  132. {
  133. F_addTime = DateTime.Now,
  134. F_addUserNo = "admin",
  135. F_editTime = DateTime.Now,
  136. F_cell = y,
  137. F_depth = 1,
  138. F_isDelete = 0,
  139. F_layer = request.layer,
  140. F_line = x,
  141. F_name = "货位",
  142. F_no = $"{x.ToString().PadLeft(2, '0') }-{y.ToString().PadLeft(2, '0')}-{request.layer.ToString().PadLeft(2, '0')}",
  143. F_status = 1,
  144. F_type = 1,
  145. F_warehouseNo = SysSetCore.GetSysSet().DefaultWarehouseNo
  146. };
  147. ctx.Insertable(location).ExecuteCommand();
  148. }
  149. }
  150. ctx.CommitTran();
  151. }
  152. catch (Exception ex)
  153. {
  154. ctx.RollbackTran();
  155. throw ex;
  156. }
  157. finally
  158. {
  159. ctx.Dispose();
  160. }
  161. return SysExCore.GetResSucc();
  162. }
  163. /// <summary>
  164. /// 获取出库单类型
  165. /// <summary>
  166. /// <returns></returns>
  167. public ResInfo GetStockOutType()
  168. {
  169. try
  170. {
  171. var data = SysDbCore.GetDbCtx().Queryable<SYS_DATA>().Where(it => it.F_PNO == "StockOutOrderType").Select<dynamic>("f_no,f_name,f_num").ToList();
  172. return SysExCore.GetResSucc(data: data);
  173. }
  174. catch (Exception ex)
  175. {
  176. throw ex;
  177. }
  178. }
  179. /// <summary>
  180. /// 转仓类型
  181. /// <summary>
  182. /// <returns></returns>
  183. public ResInfo GetTransferType()
  184. {
  185. try
  186. {
  187. var data = SysDbCore.GetDbCtx().Queryable<SYS_DATA>().Where(it => it.F_PNO == "ETransferType").Select<dynamic>("f_no,f_name,f_num").ToList();
  188. return SysExCore.GetResSucc(data: data);
  189. }
  190. catch (Exception ex)
  191. {
  192. throw ex;
  193. }
  194. }
  195. /// <summary>
  196. /// 更新库存及添加库存事务
  197. /// </summary>
  198. /// <param name="list"></param>
  199. /// <param name="db"></param>
  200. public void UpdateInventory(List<BILL_INVENTORY> inv, SqlSugarClient db)
  201. {
  202. if (!inv.Any()) return;
  203. var strSql = new StringBuilder();
  204. db.Ado.ExecuteCommand(" SELECT * INTO #INVENTORY FROM dbo.BILL_INVENTORY WHERE 1=2");
  205. var data = new List<SugarParameter>();
  206. foreach (var item in inv)
  207. {
  208. var param = GetParameter(item);
  209. db.Ado.ExecuteCommand(@" INSERT INTO #INVENTORY
  210. (
  211. F_warehouseNo,
  212. F_projectNo,
  213. F_matNo,
  214. F_matName,
  215. F_matType,
  216. F_quantity,
  217. F_lockQty,
  218. F_trayNo,
  219. F_batchNo,
  220. F_memo,
  221. F_isBonded,
  222. F_UID,
  223. F_addUserNo,
  224. F_addTime,
  225. F_editUserNo,
  226. F_editTime,
  227. F_boxNo,
  228. F_productDate
  229. )
  230. VALUES
  231. ( @F_warehouseNo,
  232. @F_projectNo,
  233. @F_matNo,
  234. @F_matName,
  235. @F_matType,
  236. @F_quantity,
  237. @F_lockQty,
  238. @F_trayNo,
  239. @F_batchNo,
  240. @F_memo,
  241. @F_isBonded,
  242. @F_UID,
  243. @F_addUserNo,
  244. @F_addTime,
  245. @F_editUserNo,
  246. @F_editTime,
  247. @F_boxNo,
  248. @F_productDate
  249. )", param);
  250. }
  251. strSql.Append($@"
  252. MERGE INTO dbo.BILL_INVENTORY A
  253. USING #INVENTORY C
  254. ON (
  255. A.F_trayNo = C.F_trayNo
  256. AND A.F_matNo = C.F_matNo
  257. AND A.F_matType = C.F_matType
  258. AND isnull(A.F_boxNo,'') = isnull(C.F_boxNo,'')
  259. AND isnull(A.F_UID,'') =isnull(C.F_UID,'')
  260. )
  261. WHEN MATCHED THEN
  262. UPDATE SET A.F_quantity = IIF(C.F_quantity>0 AND C.F_matType=1,C.F_quantity,A.F_quantity + C.F_quantity),
  263. A.F_editTime = GETDATE(),
  264. A.F_editUserNo = C.F_editUserNo
  265. WHEN NOT MATCHED THEN
  266. INSERT
  267. (
  268. F_warehouseNo,
  269. F_projectNo,
  270. F_matNo,
  271. F_matName,
  272. F_matType,
  273. F_quantity,
  274. F_lockQty,
  275. F_trayNo,
  276. F_batchNo,
  277. F_memo,
  278. F_isBonded,
  279. F_UID,
  280. F_addUserNo,
  281. F_addTime,
  282. F_productDate,
  283. F_boxNo
  284. )
  285. VALUES
  286. (C.F_warehouseNo, C.F_projectNo, C.F_matNo, C.F_matName, C.F_matType, C.F_quantity, 0, C.F_trayNo, C.F_batchNo,
  287. C.F_memo, C.F_isBonded, C.F_UID, C.F_addUserNo, GETDATE(),C.F_productDate,C.F_boxNo)
  288. OUTPUT Inserted.F_no,
  289. Inserted.F_warehouseNo,
  290. Inserted.F_projectNo,
  291. C.F_matNo,
  292. C.F_matName,
  293. C.F_matType,
  294. ISNULL(DELETED.F_quantity, 0),
  295. ISNULL(DELETED.F_lockQty, 0),
  296. ISNULL(Inserted.F_quantity, 0),
  297. ISNULL(Inserted.F_lockQty, 0),
  298. DELETED.F_trayNo,
  299. C.F_trayNo,
  300. C.F_batchNo,
  301. C.F_memo,
  302. C.F_isBonded,
  303. C.F_UID,
  304. C.F_addUserNo,
  305. C.F_addTime,
  306. C.F_boxNo,
  307. C.F_productDate
  308. INTO dbo.BILL_INVENTORYTRANSACTION
  309. (
  310. [F_inventoryNo],
  311. [F_warehouseNo],
  312. [F_projectNo],
  313. [F_matNo],
  314. [F_matName],
  315. [F_matType],
  316. [F_sourceQuantity],
  317. [F_sourceLockQty],
  318. [F_targetQuantity],
  319. [F_targetLockQty],
  320. [F_sourceTrayNo],
  321. [F_targetTrayNo],
  322. [F_batchNo],
  323. [F_memo],
  324. [F_isBonded],
  325. [F_UID],
  326. [F_addUserNo],
  327. [F_addTime],
  328. F_boxNo,
  329. F_productDate
  330. );
  331. DELETE dbo.BILL_INVENTORY WHERE EXISTS(SELECT 1 FROM #INVENTORY tmp WHERE tmp.F_matNo=BILL_INVENTORY.F_matNo AND tmp.F_trayNo=BILL_INVENTORY.F_trayNo AND tmp.F_boxNo=BILL_INVENTORY.F_boxNo) AND BILL_INVENTORY.F_quantity=0
  332. DROP TABLE #INVENTORY;");
  333. db.Ado.ExecuteCommand(strSql.ToString());
  334. }
  335. /// <summary>
  336. /// 返回基于该对象的SugarParameters的数组
  337. /// </summary>
  338. /// <param name=""></param>
  339. /// <returns></returns>
  340. public static SugarParameter[] GetParameter<T>(T Info) where T : class
  341. {
  342. Type type = typeof(T);
  343. object obj = Activator.CreateInstance(type);
  344. // 获取所有属性。
  345. PropertyInfo[] properties = type.GetProperties();
  346. SugarParameter[] arParms = new SugarParameter[properties.Length];
  347. for (int i = 0; i < properties.Length; i++)
  348. {
  349. arParms[i] = new SugarParameter($"@{properties[i].Name}", properties[i].GetValue(Info));
  350. }
  351. return arParms;
  352. }
  353. /// <summary>
  354. /// 出库下一个点位分配
  355. /// </summary>
  356. /// <param name="ctx"></param>
  357. /// <returns></returns>
  358. public string StockOutNextPointAllot(SqlSugarClient ctx)
  359. {
  360. var point = ctx.Queryable<BASE_POINT>().First(c => c.F_type == (int)EPointType.CKXYGDZ);
  361. var pointTask = ctx.Queryable<WMS_TASK>().Where(c => ((c.F_posidNext == point.F_no || c.F_posidNext == point.F_cachePoint) && c.F_taskStatus == (int)ETaskStatus.NotIssued || c.F_taskStatus == (int)ETaskStatus.NotExecute || c.F_taskStatus == (int)ETaskStatus.Executing));
  362. if (pointTask.Any())
  363. {
  364. if (pointTask.Any(a => a.F_posidNext == point.F_no))
  365. {
  366. if (!pointTask.Any(a => a.F_posidNext == point.F_cachePoint))
  367. {
  368. return point.F_cachePoint;
  369. }
  370. }
  371. }
  372. return point.F_no;
  373. }
  374. }
  375. }