VirtualApplyHelper.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Linq;
  5. using System.Data;
  6. using MySystem;
  7. using MySystem.PxcModels;
  8. using Library;
  9. using LitJson;
  10. public class VirtualApplyHelper
  11. {
  12. public readonly static VirtualApplyHelper Instance = new VirtualApplyHelper();
  13. private VirtualApplyHelper()
  14. {
  15. }
  16. public void Start()
  17. {
  18. Thread th = new Thread(StartDo);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. private void StartDo()
  23. {
  24. while (true)
  25. {
  26. try
  27. {
  28. string data = RedisDbconn.Instance.RPop<string>("VirtualApplyQueue");
  29. if(!string.IsNullOrEmpty(data))
  30. {
  31. TransferOneDo(data);
  32. }
  33. else
  34. {
  35. Thread.Sleep(5000);
  36. }
  37. }
  38. catch (Exception ex)
  39. {
  40. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "申请虚拟卡异常");
  41. }
  42. }
  43. }
  44. public void TransferOneDo(string value)
  45. {
  46. WebCMSEntities maindb = new WebCMSEntities();
  47. JsonData data = JsonMapper.ToObject(value);
  48. int OrderId = int.Parse(function.CheckInt(data["OrderId"].ToString()));
  49. int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库
  50. int ToUserId = int.Parse(function.CheckInt(data["ToUserId"].ToString())); //收货人
  51. int SnCount = int.Parse(function.CheckInt(data["SnCount"].ToString()));
  52. Dictionary<string, object> Obj = new Dictionary<string, object>();
  53. StoreHouse store = maindb.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
  54. string SnNos = ""; //划拨的机具号(多个)
  55. Orders order = maindb.Orders.FirstOrDefault(m => m.Id == OrderId);
  56. if (order != null)
  57. {
  58. if (order.Status == 2)
  59. {
  60. return;
  61. }
  62. int ApplyType = 0; // 申请类型,1-机具SN,2-200兑换码,3-300券
  63. List<string> SourceSnNos = new List<string>();
  64. if (order.Sort > 0)
  65. {
  66. MachineApply Apply = maindb.MachineApply.FirstOrDefault(m => m.Id == order.Sort);
  67. if (Apply != null)
  68. {
  69. Apply.Status = 1;
  70. Apply.QueryCount = order.Id;
  71. ApplyType = Apply.Sort;
  72. if (!string.IsNullOrEmpty(Apply.SwapSnExpand))
  73. {
  74. string[] SwapSnExpands = Apply.SwapSnExpand.Split('\n');
  75. foreach (string sub in SwapSnExpands)
  76. {
  77. if (!string.IsNullOrEmpty(sub))
  78. {
  79. SourceSnNos.Add(sub.Split(':')[0]);
  80. }
  81. }
  82. }
  83. }
  84. }
  85. Dictionary<int, string> couponIds = new Dictionary<int, string>();
  86. int SnIndex = 0;
  87. List<PosMachinesTwo> PosList = maindb.PosMachinesTwo.Where(m => m.BrandId == 14 && m.StoreId == 8664 && m.BuyUserId == 0 && m.BindingState == 0 && m.ActivationState == 0).Take(SnCount).ToList();
  88. foreach (PosMachinesTwo pos in PosList)
  89. {
  90. string ChangeNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  91. int SnIdNum = pos.Id;
  92. DateTime RecycEndDate = DateTime.Now.AddDays(360);
  93. int LeaderUserId = 0;
  94. if (SourceSnNos.Count > SnIndex)
  95. {
  96. string SourceSnNo = SourceSnNos[SnIndex];
  97. if (ApplyType <= 1)
  98. {
  99. MachineForSnNo forSnNo = maindb.MachineForSnNo.FirstOrDefault(m => m.SnNo == SourceSnNo) ?? new MachineForSnNo();
  100. PosMachinesTwo spos = maindb.PosMachinesTwo.FirstOrDefault(m => m.Id == forSnNo.SnId) ?? new PosMachinesTwo();
  101. RecycEndDate = spos.RecycEndDate == null ? RecycEndDate = DateTime.Now.AddDays(360) : spos.RecycEndDate.Value;
  102. LeaderUserId = spos.LeaderUserId;
  103. }
  104. else if (ApplyType > 1)
  105. {
  106. PosCoupons coupon = maindb.PosCoupons.FirstOrDefault(m => m.ExchangeCode == SourceSnNo) ?? new PosCoupons();
  107. LeaderUserId = coupon.LeaderUserId;
  108. if (coupon.OpId > 0)
  109. {
  110. if (couponIds.ContainsKey(coupon.OpId))
  111. {
  112. string[] datas = couponIds[coupon.OpId].Split(',');
  113. int Num = int.Parse(datas[2]) + 1;
  114. couponIds[coupon.OpId] = datas[0] + "," + datas[1] + "," + Num;
  115. }
  116. else
  117. {
  118. couponIds.Add(coupon.OpId, coupon.Id + "," + coupon.QueryCount + ",1");
  119. }
  120. }
  121. }
  122. }
  123. if (StoreId == 0)
  124. {
  125. StoreId = pos.StoreId;
  126. }
  127. SnNos += pos.PosSn + ",";
  128. Users toUser = maindb.Users.FirstOrDefault(m => m.Id == ToUserId) ?? new Users();
  129. StoreStockChange query = maindb.StoreStockChange.Add(new StoreStockChange()
  130. {
  131. CreateDate = DateTime.Now,
  132. StoreId = StoreId, //仓库
  133. BrandId = pos.BrandId, //产品类型
  134. ProductName = RelationClass.GetKqProductBrandInfo(pos.BrandId), //产品名称
  135. ChangeNo = ChangeNo, //变更单号
  136. TransType = 11, //交易类型
  137. SnNo = pos.PosSn, //SN编号
  138. SnType = pos.PosSnType, //SN机具类型
  139. StockOpDirect = 1, //库存操作方向
  140. DeviceType = pos.DeviceType, //设备类型
  141. FromUserId = store.UserId, //出货人
  142. FromDate = DateTime.Now, //出库时间
  143. ToUserId = ToUserId, //收货人
  144. ToStoreId = StoreId, //退货收货仓库
  145. OpId = pos.OpId
  146. }).Entity;
  147. UserStoreChange userstore = maindb.UserStoreChange.Add(new UserStoreChange()
  148. {
  149. CreateDate = DateTime.Now,
  150. UserId = store.UserId, //创客
  151. BrandId = pos.BrandId, //产品类型
  152. ChangeRecordNo = ChangeNo, //变更记录单号
  153. TransType = 0, //交易类型
  154. SnNo = pos.PosSn, //SN编号
  155. SnType = pos.PosSnType, //SN机具类型
  156. StockOpDirect = 0, //库存操作方向
  157. DeviceVendor = pos.DeviceName, //设备厂商
  158. DeviceType = pos.DeviceKind, //设备类型
  159. DeviceModel = pos.DeviceType, //设备型号
  160. ToUserId = ToUserId, //收货创客
  161. ToDate = DateTime.Now, //入库时间
  162. SourceStoreId = pos.SourceStoreId, //源仓库
  163. SnStatus = 1, //SN状态
  164. BindStatus = (int)pos.BindingState, //绑定状态
  165. BindMerchantId = pos.BindMerchantId, //绑定商户
  166. ActiveStatus = (int)pos.ActivationState, //激活状态
  167. ActRewardUserId = pos.BuyUserId, //激活奖励创客
  168. BrandType = pos.DeviceType, //品牌类型
  169. }).Entity;
  170. StoreChangeHistory history = maindb.StoreChangeHistory.Add(new StoreChangeHistory()
  171. {
  172. CreateDate = DateTime.Now,
  173. UserId = store.UserId, //创客
  174. BrandId = pos.BrandId, //产品类型
  175. ChangeRecordNo = ChangeNo, //变更记录单号
  176. TransType = 2, //交易类型
  177. SnNo = pos.PosSn, //SN编号
  178. SnType = pos.PosSnType, //SN机具类型
  179. StockOpDirect = 1, //库存操作方向
  180. DeviceVendor = pos.DeviceName, //设备厂商
  181. DeviceModel = pos.DeviceKind, //设备型号
  182. DeviceType = pos.DeviceType, //设备类型
  183. ToUserId = ToUserId, //收货创客
  184. FromUserId = store.UserId, //出货创客
  185. FromDate = DateTime.Now, //出库时间
  186. SourceStoreId = pos.SourceStoreId, //源仓库
  187. StoreId = store.Id, //仓库
  188. OpId = pos.OpId
  189. }).Entity;
  190. maindb.SaveChanges();
  191. PublicFunction.StatUserMachineData(ToUserId, pos.BrandId, 1);
  192. store.LaveNum -= 1;
  193. store.OutNum += 1;
  194. pos.OrderId = OrderId;
  195. pos.BuyUserId = ToUserId;
  196. pos.UserId = ToUserId;
  197. pos.TransferTime = DateTime.Now;
  198. pos.RecycEndDate = RecycEndDate; // 循环结束时间
  199. pos.PosSnType = order.QueryCount;
  200. pos.LeaderUserId = LeaderUserId;
  201. maindb.SaveChanges();
  202. SnIndex += 1;
  203. }
  204. order.Status = 2;
  205. order.SendStatus = 1;
  206. order.SendDate = DateTime.Now;
  207. order.SnNos = SnNos.TrimEnd(',');
  208. maindb.SaveChanges();
  209. }
  210. maindb.Dispose();
  211. }
  212. }