VirtualApplyHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. int BigPosCouponKind = 0;
  95. if (SourceSnNos.Count > SnIndex)
  96. {
  97. string SourceSnNo = SourceSnNos[SnIndex];
  98. if (ApplyType <= 1)
  99. {
  100. MachineForSnNo forSnNo = maindb.MachineForSnNo.FirstOrDefault(m => m.SnNo == SourceSnNo) ?? new MachineForSnNo();
  101. PosMachinesTwo spos = maindb.PosMachinesTwo.FirstOrDefault(m => m.Id == forSnNo.SnId) ?? new PosMachinesTwo();
  102. RecycEndDate = spos.RecycEndDate == null ? RecycEndDate = DateTime.Now.AddDays(360) : spos.RecycEndDate.Value;
  103. LeaderUserId = spos.LeaderUserId;
  104. BigPosCouponKind = spos.OpId;
  105. if(BigPosCouponKind == 0)
  106. {
  107. KqProducts brand = maindb.KqProducts.FirstOrDefault(m => m.Id == spos.BrandId) ?? new KqProducts();
  108. BigPosCouponKind = brand.Kind;
  109. }
  110. }
  111. else if (ApplyType > 1)
  112. {
  113. PosCoupons coupon = maindb.PosCoupons.FirstOrDefault(m => m.ExchangeCode == SourceSnNo) ?? new PosCoupons();
  114. LeaderUserId = coupon.LeaderUserId;
  115. BigPosCouponKind = coupon.QueryCount;
  116. if (coupon.OpId > 0)
  117. {
  118. if (couponIds.ContainsKey(coupon.OpId))
  119. {
  120. string[] datas = couponIds[coupon.OpId].Split(',');
  121. int Num = int.Parse(datas[2]) + 1;
  122. couponIds[coupon.OpId] = datas[0] + "," + datas[1] + "," + Num;
  123. }
  124. else
  125. {
  126. couponIds.Add(coupon.OpId, coupon.Id + "," + coupon.QueryCount + ",1");
  127. }
  128. }
  129. }
  130. }
  131. if (StoreId == 0)
  132. {
  133. StoreId = pos.StoreId;
  134. }
  135. SnNos += pos.PosSn + ",";
  136. Users toUser = maindb.Users.FirstOrDefault(m => m.Id == ToUserId) ?? new Users();
  137. StoreStockChange query = maindb.StoreStockChange.Add(new StoreStockChange()
  138. {
  139. CreateDate = DateTime.Now,
  140. StoreId = StoreId, //仓库
  141. BrandId = pos.BrandId, //产品类型
  142. ProductName = RelationClass.GetKqProductBrandInfo(pos.BrandId), //产品名称
  143. ChangeNo = ChangeNo, //变更单号
  144. TransType = 11, //交易类型
  145. SnNo = pos.PosSn, //SN编号
  146. SnType = pos.PosSnType, //SN机具类型
  147. StockOpDirect = 1, //库存操作方向
  148. DeviceType = pos.DeviceType, //设备类型
  149. FromUserId = store.UserId, //出货人
  150. FromDate = DateTime.Now, //出库时间
  151. ToUserId = ToUserId, //收货人
  152. ToStoreId = StoreId, //退货收货仓库
  153. OpId = pos.OpId
  154. }).Entity;
  155. UserStoreChange userstore = maindb.UserStoreChange.Add(new UserStoreChange()
  156. {
  157. CreateDate = DateTime.Now,
  158. UserId = store.UserId, //创客
  159. BrandId = pos.BrandId, //产品类型
  160. ChangeRecordNo = ChangeNo, //变更记录单号
  161. TransType = 0, //交易类型
  162. SnNo = pos.PosSn, //SN编号
  163. SnType = pos.PosSnType, //SN机具类型
  164. StockOpDirect = 0, //库存操作方向
  165. DeviceVendor = pos.DeviceName, //设备厂商
  166. DeviceType = pos.DeviceKind, //设备类型
  167. DeviceModel = pos.DeviceType, //设备型号
  168. ToUserId = ToUserId, //收货创客
  169. ToDate = DateTime.Now, //入库时间
  170. SourceStoreId = pos.SourceStoreId, //源仓库
  171. SnStatus = 1, //SN状态
  172. BindStatus = (int)pos.BindingState, //绑定状态
  173. BindMerchantId = pos.BindMerchantId, //绑定商户
  174. ActiveStatus = (int)pos.ActivationState, //激活状态
  175. ActRewardUserId = pos.BuyUserId, //激活奖励创客
  176. BrandType = pos.DeviceType, //品牌类型
  177. }).Entity;
  178. StoreChangeHistory history = maindb.StoreChangeHistory.Add(new StoreChangeHistory()
  179. {
  180. CreateDate = DateTime.Now,
  181. UserId = store.UserId, //创客
  182. BrandId = pos.BrandId, //产品类型
  183. ChangeRecordNo = ChangeNo, //变更记录单号
  184. TransType = 2, //交易类型
  185. SnNo = pos.PosSn, //SN编号
  186. SnType = pos.PosSnType, //SN机具类型
  187. StockOpDirect = 1, //库存操作方向
  188. DeviceVendor = pos.DeviceName, //设备厂商
  189. DeviceModel = pos.DeviceKind, //设备型号
  190. DeviceType = pos.DeviceType, //设备类型
  191. ToUserId = ToUserId, //收货创客
  192. FromUserId = store.UserId, //出货创客
  193. FromDate = DateTime.Now, //出库时间
  194. SourceStoreId = pos.SourceStoreId, //源仓库
  195. StoreId = store.Id, //仓库
  196. OpId = pos.OpId
  197. }).Entity;
  198. maindb.SaveChanges();
  199. PublicFunction.StatUserMachineData(ToUserId, pos.BrandId, 1);
  200. store.LaveNum -= 1;
  201. store.OutNum += 1;
  202. PosMachinesTwo editPos = maindb.PosMachinesTwo.FirstOrDefault(m => m.Id == pos.Id);
  203. if(editPos != null)
  204. {
  205. pos.OrderId = OrderId;
  206. pos.BuyUserId = ToUserId;
  207. pos.UserId = ToUserId;
  208. pos.TransferTime = DateTime.Now;
  209. pos.RecycEndDate = RecycEndDate; // 循环结束时间
  210. pos.PosSnType = order.QueryCount;
  211. pos.LeaderUserId = LeaderUserId;
  212. pos.OpId = BigPosCouponKind;
  213. }
  214. maindb.SaveChanges();
  215. SnIndex += 1;
  216. }
  217. order.Status = 2;
  218. order.SendStatus = 1;
  219. order.SendDate = DateTime.Now;
  220. order.SnNos = SnNos.TrimEnd(',');
  221. maindb.SaveChanges();
  222. }
  223. maindb.Dispose();
  224. }
  225. }