StoreStockChangeUtil.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using Library;
  2. using MySystem.Service.Main;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace MySystem
  6. {
  7. /// <summary>
  8. /// 仓库库存变动工具类
  9. /// </summary>
  10. public class StoreStockChangeUtil
  11. {
  12. #region 首页-仓库管理-整箱/逐台划拨
  13. /// <summary>
  14. /// 首页-仓库管理-整箱/逐台划拨
  15. /// </summary>
  16. /// <param name="OrderId">订单Id</param>
  17. /// <param name="StoreId">仓库Id</param>
  18. /// <param name="ToUserId">收获创客Id</param>
  19. /// <param name="SnIds">机具SN的Id集合</param>
  20. public static string TransferWholeOrOneDo(int OrderId, int StoreId, int ToUserId, string SnIds)
  21. {
  22. var store = StoreHouseService.Query(StoreId);
  23. string[] SnIdList = SnIds.Split(',');
  24. string SnNos = ""; //划拨的机具号(多个)
  25. var order = OrdersService.Query(OrderId);
  26. if (order.Id > 0)
  27. {
  28. if (order.Status == 2)
  29. {
  30. return "该订单已处理,请勿重复操作";
  31. }
  32. int ApplyType = 0; // 申请类型,1-机具SN,2-200兑换码,3-300券
  33. List<string> SourceSnNos = new List<string>();
  34. if (order.Sort > 0)
  35. {
  36. var Apply = MachineApplyService.Query(order.Sort);
  37. if (Apply.Id > 0)
  38. {
  39. ApplyType = Apply.Sort;
  40. Dictionary<string, object> File = new Dictionary<string, object>();
  41. File.Add("Status", 1);
  42. File.Add("QueryCount", order.Id);
  43. MachineApplyService.Edit(File, Apply.Id);
  44. if (!string.IsNullOrEmpty(Apply.SwapSnExpand))
  45. {
  46. string[] SwapSnExpands = Apply.SwapSnExpand.Split('\n');
  47. foreach (string sub in SwapSnExpands)
  48. {
  49. if (!string.IsNullOrEmpty(sub))
  50. {
  51. SourceSnNos.Add(sub.Split(':')[0]);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. Dictionary<int, string> couponIds = new Dictionary<int, string>();
  58. int SnIndex = 0;
  59. foreach (string SnId in SnIdList)
  60. {
  61. string ChangeNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  62. int SnIdNum = int.Parse(SnId);
  63. int LeaderUserId = 0;
  64. DateTime RecycEndDate = DateTime.Now.AddDays(360);
  65. if (SourceSnNos.Count > SnIndex)
  66. {
  67. string SourceSnNo = SourceSnNos[SnIndex];
  68. if (ApplyType <= 1)
  69. {
  70. var forSnNo = MachineForSnNoService.Query("SnNo='" + SourceSnNo + "'");
  71. var spos = PosMachinesTwoService.Query(forSnNo.SnId);
  72. RecycEndDate = spos.RecycEndDate == null ? RecycEndDate = DateTime.Now.AddDays(360) : spos.RecycEndDate.Value;
  73. LeaderUserId = spos.LeaderUserId;
  74. }
  75. else if (ApplyType > 1)
  76. {
  77. var coupon = PosCouponsService.Query("ExchangeCode='" + SourceSnNo + "'");
  78. LeaderUserId = coupon.LeaderUserId;
  79. if (coupon.OpId > 0)
  80. {
  81. if (couponIds.ContainsKey(coupon.OpId))
  82. {
  83. string[] datas = couponIds[coupon.OpId].Split(',');
  84. int Num = int.Parse(datas[2]) + 1;
  85. couponIds[coupon.OpId] = datas[0] + "," + datas[1] + "," + Num;
  86. }
  87. else
  88. {
  89. couponIds.Add(coupon.OpId, coupon.Id + "," + coupon.QueryCount + ",1");
  90. }
  91. }
  92. }
  93. }
  94. var pos = PosMachinesTwoService.Query(SnIdNum);
  95. //扣减分仓所关联的运营中心总机具数
  96. if (pos.OpId > 0)
  97. {
  98. var sysAdmin = MySystem.Service.Operate.SysAdminService.Query("QueryCount=1 and UserId='" + pos.OpId + "'");
  99. if (sysAdmin.Id > 0)
  100. {
  101. sysAdmin.TotalMachineCount -= 1;
  102. Dictionary<string, object> File = new Dictionary<string, object>();
  103. File.Add("TotalMachineCount", sysAdmin.TotalMachineCount);
  104. MySystem.Service.Operate.SysAdminService.Edit(File, sysAdmin.Id);
  105. }
  106. }
  107. SnNos += pos.PosSn + ",";
  108. AddForTransfer(StoreId, pos.BrandId, KqProductsService.Query(pos.BrandId).Name, ChangeNo, 10, pos.PosSn, pos.PosSnType, 1, pos.DeviceType, store.UserId, DateTime.Now, ToUserId, StoreId, pos.OpId);
  109. UserStoreChangeUtil.AddForTransfer(store.UserId, pos.BrandId, ChangeNo, 0, pos.PosSn, pos.PosSnType, 0, pos.DeviceName, pos.DeviceKind, pos.DeviceType, ToUserId, DateTime.Now, pos.SourceStoreId, 1, (int)pos.BindingState, pos.BindMerchantId, (int)pos.ActivationState, pos.BuyUserId, pos.DeviceType);
  110. StoreChangeHistoryUtil.AddForTransfer(store.UserId, pos.BrandId, ChangeNo, 2, pos.PosSn, pos.PosSnType, 1, pos.DeviceName, pos.DeviceKind, pos.DeviceType, ToUserId, store.UserId, DateTime.Now, pos.SourceStoreId, store.Id, pos.OpId);
  111. PublicFunction.StatUserMachineData(ToUserId, pos.BrandId, 1);
  112. StoreHouseUtil.EditStoreStock(store.Id, store.LaveNum -= 1, store.OutNum += 1);
  113. PosMachinesTwoUtil.EditPosInfo(pos.Id, OrderId, ToUserId, ToUserId, DateTime.Now, RecycEndDate, order.QueryCount, LeaderUserId);
  114. }
  115. OrdersUtil.EditOrdersInfo(order.Id, 2, 1, DateTime.Now, SnNos.TrimEnd(','));
  116. string SendData = "{\"Kind\":\"4\",\"Data\":{\"StoreId\":\"" + StoreId + "\",\"SnIds\":\"" + SnIds + "\"}}";
  117. RedisDbconn.Instance.AddList("StoreApplyQueue", SendData);
  118. }
  119. return "success";
  120. }
  121. #endregion
  122. #region 首页-仓库管理-售后单划拨
  123. /// <summary>
  124. /// 首页-仓库管理-售后单划拨
  125. /// </summary>
  126. /// <param name="OrderId">订单Id</param>
  127. /// <param name="StoreId">仓库Id</param>
  128. /// <param name="ToUserId">收获创客Id</param>
  129. /// <param name="SnIds">机具SN的Id集合</param>
  130. public static string TransferForChangeDo(int OrderId, int StoreId, string SnIds)
  131. {
  132. int CheckCount = MachineChangeService.Count("OutStoreId='" + StoreId + "' and AuditResult=0");
  133. if (CheckCount >= 10)
  134. {
  135. return "待审核订单超过10单,请等待管理员审核";
  136. }
  137. var store = StoreHouseService.Query(StoreId);
  138. string[] SnIdList = SnIds.Split(',');
  139. string SnNos = ""; //划拨的机具号(多个)
  140. var order = OrdersService.Query(OrderId);
  141. if (order.Id > 0)
  142. {
  143. if (order.Status == 2)
  144. {
  145. return "该订单已处理,请勿重复操作";
  146. }
  147. int ToUserId = order.UserId;
  148. int ChangeId = 0;
  149. List<string> SourceSnNos = new List<string>();
  150. if (order.Sort > 0)
  151. {
  152. var Apply = MachineChangeService.Query(order.Sort);
  153. if (Apply.Id > 0)
  154. {
  155. Apply.Status = 1;
  156. if (!string.IsNullOrEmpty(Apply.ChangeSnExpand))
  157. {
  158. string[] ChangeSnExpand = Apply.ChangeSnExpand.Split('\n');
  159. foreach (string sub in ChangeSnExpand)
  160. {
  161. if (!string.IsNullOrEmpty(sub))
  162. {
  163. SourceSnNos.Add(sub.Split(':')[0]);
  164. }
  165. }
  166. }
  167. ChangeId = Apply.Id;
  168. }
  169. }
  170. int SnIndex = 0;
  171. List<string> contents = new List<string>();
  172. foreach (string SnId in SnIdList)
  173. {
  174. string ChangeNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  175. int SnIdNum = int.Parse(SnId);
  176. DateTime RecycEndDate = DateTime.Now.AddDays(360);
  177. string SourceSnNo = "";
  178. if (SourceSnNos.Count > SnIndex)
  179. {
  180. SourceSnNo = SourceSnNos[SnIndex];
  181. var forSnNo = MachineForSnNoService.Query("SnNo='" + SourceSnNo + "'");
  182. var spos = PosMachinesTwoService.Query(forSnNo.SnId);
  183. RecycEndDate = spos.RecycEndDate == null ? RecycEndDate = DateTime.Now.AddDays(360) : spos.RecycEndDate.Value;
  184. }
  185. var pos = PosMachinesTwoService.Query(SnIdNum);
  186. //扣减分仓所关联的运营中心总机具数
  187. if (pos.OpId > 0)
  188. {
  189. var sysAdmin = MySystem.Service.Operate.SysAdminService.Query("QueryCount=1 and UserId='" + pos.OpId + "'");
  190. if (sysAdmin.Id > 0)
  191. {
  192. sysAdmin.TotalMachineCount -= 1;
  193. Dictionary<string, object> File = new Dictionary<string, object>();
  194. File.Add("TotalMachineCount", sysAdmin.TotalMachineCount);
  195. MySystem.Service.Operate.SysAdminService.Edit(File, sysAdmin.Id);
  196. }
  197. }
  198. var merchant = PosMerchantInfoService.Query(pos.BindMerchantId);
  199. SnNos += pos.PosSn + ",";
  200. AddForTransfer(StoreId, pos.BrandId, KqProductsService.Query(pos.BrandId).Name, ChangeNo, 11, pos.PosSn, pos.PosSnType, 1, pos.DeviceType, store.UserId, DateTime.Now, ToUserId, StoreId, pos.OpId);
  201. UserStoreChangeUtil.AddForTransfer(store.UserId, pos.BrandId, ChangeNo, 0, pos.PosSn, pos.PosSnType, 0, pos.DeviceName, pos.DeviceKind, pos.DeviceType, ToUserId, DateTime.Now, pos.SourceStoreId, 1, (int)pos.BindingState, pos.BindMerchantId, (int)pos.ActivationState, pos.BuyUserId, pos.DeviceType);
  202. StoreChangeHistoryUtil.AddForTransfer(store.UserId, pos.BrandId, ChangeNo, 2, pos.PosSn, pos.PosSnType, 1, pos.DeviceName, pos.DeviceKind, pos.DeviceType, ToUserId, store.UserId, DateTime.Now, pos.SourceStoreId, store.Id, pos.OpId);
  203. PublicFunction.StatUserMachineData(ToUserId, pos.BrandId, 1);
  204. StoreHouseUtil.EditStoreStock(store.Id, store.LaveNum -= 1, store.OutNum += 1);
  205. PosMachinesTwoUtil.EditPosInfo(pos.Id, OrderId, ToUserId, ToUserId, DateTime.Now, RecycEndDate, order.QueryCount, 0);
  206. string content = "{"; //执行机具数据同步
  207. content += "\"OldSn\":\"" + SourceSnNo + "\",";
  208. content += "\"NewSn\":\"" + pos.PosSn + "\",";
  209. content += "\"MerNo\":\"" + merchant.KqMerNo + "\",";
  210. content += "\"ChangeId\":\"" + ChangeId + "\"";
  211. content += "}";
  212. contents.Add(content);
  213. }
  214. OrdersUtil.EditOrdersInfo(order.Id, 2, 1, DateTime.Now, SnNos.TrimEnd(','));
  215. foreach (string content in contents)
  216. {
  217. RedisDbconn.Instance.AddList("ChangePosTimerQueue", content);
  218. }
  219. string SendData = "{\"Kind\":\"4\",\"Data\":{\"StoreId\":\"" + StoreId + "\",\"SnIds\":\"" + SnIds + "\"}}";
  220. RedisDbconn.Instance.AddList("StoreApplyQueue", SendData);
  221. }
  222. return "success";
  223. }
  224. #endregion
  225. #region 添加仓库库存变动记录(仓库出货)
  226. /// <summary>
  227. /// 添加仓库库存变动记录(仓库出货)
  228. /// </summary>
  229. /// <param name="StoreId">仓库</param>
  230. /// <param name="BrandId">产品类型</param>
  231. /// <param name="ProductName">产品名称</param>
  232. /// <param name="ChangeNo">变更单号</param>
  233. /// <param name="TransType">交易类型</param>
  234. /// <param name="SnNo">SN编号</param>
  235. /// <param name="SnType">SN机具类型</param>
  236. /// <param name="StockOpDirect">库存操作方向</param>
  237. /// <param name="DeviceType">设备类型</param>
  238. /// <param name="FromUserId">出货人</param>
  239. /// <param name="FromDate">出库时间</param>
  240. /// <param name="ToUserId">收货人</param>
  241. /// <param name="ToStoreId">退货收货仓库</param>
  242. /// <param name="OpId">运营中心创客Id</param>
  243. public static void AddForTransfer(int StoreId, int BrandId, string ProductName, string ChangeNo, int TransType, string SnNo, int SnType, int StockOpDirect, string DeviceType, int FromUserId, DateTime? FromDate, int ToUserId, int ToStoreId, int OpId)
  244. {
  245. Dictionary<string, object> query = new Dictionary<string, object>();
  246. query.Add("StoreId", StoreId);//仓库
  247. query.Add("BrandId", BrandId);//产品类型
  248. query.Add("ProductName", ProductName);//产品名称
  249. query.Add("ChangeNo", ChangeNo);//变更单号
  250. query.Add("TransType", TransType);//交易类型
  251. query.Add("SnNo", SnNo);//SN编号
  252. query.Add("SnType", SnType);//SN机具类型
  253. query.Add("StockOpDirect", 1);//库存操作方向
  254. query.Add("DeviceType", DeviceType);//设备类型
  255. query.Add("FromUserId", FromUserId);//出货人
  256. query.Add("FromDate", FromDate);//出库时间
  257. query.Add("ToUserId", ToUserId);//收货人
  258. query.Add("ToStoreId", ToStoreId);//退货收货仓库
  259. query.Add("OpId", OpId);//运营中心创客Id
  260. StoreStockChangeService.Add(query);
  261. }
  262. #endregion
  263. }
  264. }