SycnSpTradeService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.Models;
  5. using Library;
  6. using LitJson;
  7. namespace MySystem
  8. {
  9. public class SycnSpTradeService
  10. {
  11. public readonly static SycnSpTradeService Instance = new SycnSpTradeService();
  12. private SycnSpTradeService()
  13. { }
  14. public void Start(string PosSn, DateTime start, DateTime end, int StartId = 0)
  15. {
  16. SpModels.WebCMSEntities spdb = new SpModels.WebCMSEntities();
  17. WebCMSEntities db = new WebCMSEntities();
  18. IQueryable<SpModels.TradeRecord> trades = spdb.TradeRecord.Where(m => m.Id > StartId && m.CreateDate >= start && m.CreateDate < end && m.TradeSnNo == PosSn);
  19. foreach (SpModels.TradeRecord trade in trades.ToList())
  20. {
  21. bool op = true;
  22. if (trade.SerEntryMode == "N")
  23. {
  24. if (trade.TradeAmount == 9900 || trade.TradeAmount == 19900 || trade.TradeAmount == 29900)
  25. {
  26. op = false;
  27. MachineForMerNo posFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == trade.MerNo) ?? new MachineForMerNo();
  28. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  29. if (pos != null)
  30. {
  31. if (trade.SerEntryMode == "N" && trade.ProductType == "1")
  32. {
  33. pos.SeoKeyword = trade.TradeAmount.ToString("f2");
  34. // pos.IsPurchase = 0;
  35. db.SaveChanges();
  36. }
  37. }
  38. SpModels.TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  39. if (edit != null)
  40. {
  41. edit.Status = 2;
  42. }
  43. spdb.SaveChanges();
  44. }
  45. }
  46. if (trade.DiscountRateFlag == "True")
  47. {
  48. op = false;
  49. SpModels.TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  50. if (edit != null)
  51. {
  52. edit.Status = 2;
  53. }
  54. spdb.SaveChanges();
  55. }
  56. if(trade.ProductType == "7" && trade.TradeStatus != "00")
  57. {
  58. op = false;
  59. SpModels.TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  60. if (edit != null)
  61. {
  62. edit.Status = 2;
  63. }
  64. spdb.SaveChanges();
  65. }
  66. if (op)
  67. {
  68. MachineForMerNo posFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == trade.MerNo) ?? new MachineForMerNo();
  69. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  70. if (pos != null)
  71. {
  72. if (pos.BindMerchantId > 0)
  73. {
  74. Users user = db.Users.FirstOrDefault(m => m.Id == pos.UserId) ?? new Users();
  75. int TopUserId = 0;
  76. if (!string.IsNullOrEmpty(user.ParentNav))
  77. {
  78. TopUserId = int.Parse(user.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  79. }
  80. bool check = db.SpOrderNos.Any(m => m.OrderNo == trade.TradeSerialNo);
  81. if (!check)
  82. {
  83. decimal TradeAmount = trade.TradeAmount;
  84. int BankCardType = -1;
  85. int QrPayFlag = 0;
  86. int VipFlag = 0;
  87. int PayType = 0;
  88. if (pos.BrandId == 1 || pos.BrandId == 3)
  89. {
  90. TradeAmount = TradeAmount / 100;
  91. BankCardType = int.Parse(trade.BankCardType);
  92. if (trade.TradeType == "02") QrPayFlag = 1;
  93. else if (trade.TradeType == "NQ") PayType = 1;
  94. if (trade.SettleMethod == "E") VipFlag = 1;
  95. }
  96. else if (pos.BrandId == 2)
  97. {
  98. if (trade.TradeType == "DEBIT_BY_CARD")
  99. {
  100. BankCardType = 0;
  101. }
  102. else
  103. {
  104. BankCardType = 1;
  105. }
  106. if (trade.TradeType == "CLOUD_PAY") QrPayFlag = 1;
  107. }
  108. else if (pos.BrandId == 4 || pos.BrandId == 5 || pos.BrandId == 8 || pos.BrandId == 9)
  109. {
  110. TradeAmount = TradeAmount / 100;
  111. if (trade.BankCardType == "100")
  112. {
  113. BankCardType = 1;
  114. }
  115. else if (trade.BankCardType == "200")
  116. {
  117. BankCardType = 0;
  118. }
  119. if (trade.TradeType == "200") QrPayFlag = 1;
  120. if (trade.Field1 == "1") VipFlag = 1;
  121. }
  122. else if (pos.BrandId == 6)
  123. {
  124. TradeAmount = TradeAmount / 100;
  125. if (trade.BankCardType == "01" || trade.BankCardType == "11")
  126. {
  127. BankCardType = 0;
  128. }
  129. else
  130. {
  131. BankCardType = 1;
  132. }
  133. }
  134. else if (pos.BrandId == 7)
  135. {
  136. TradeAmount = TradeAmount / 100;
  137. if (trade.BankCardType == "OA" || trade.BankCardType == "CC" || trade.BankCardType == "SCC")
  138. {
  139. BankCardType = 1;
  140. }
  141. else
  142. {
  143. BankCardType = 0;
  144. }
  145. }
  146. int BrandId = int.Parse(trade.ProductType);
  147. if (BrandId == 1 && trade.MerNo.StartsWith("M900"))
  148. {
  149. BrandId = 3;
  150. }
  151. if (BrandId == 4 && trade.DeviceType == "200")
  152. {
  153. BrandId = 5;
  154. }
  155. decimal SourceTradeAmount = TradeAmount;
  156. if (pos.Sort == 0 && pos.BrandId == 7)
  157. {
  158. decimal deposit = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  159. if (deposit > 0)
  160. {
  161. TradeAmount -= deposit;
  162. pos.Sort = 1;
  163. }
  164. }
  165. if (TradeAmount > 0)
  166. {
  167. ulong MerHelpFlag = 0;
  168. decimal DepositAmt = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  169. if (DepositAmt > 0)
  170. {
  171. int Days = int.Parse(RedisDbconn.Instance.Get<string>("pobjrule:" + pos.BrandId + ":HelpPolicy:Days")); //天数
  172. if (pos.BindingTime != null)
  173. {
  174. if (pos.BindingTime.Value.AddDays(Days) > DateTime.Now)
  175. {
  176. MerHelpFlag = 1;
  177. }
  178. }
  179. }
  180. int CapFlag = 0;
  181. if(BankCardType == 0 && TradeAmount >= 1666.67M)
  182. {
  183. CapFlag = 1;
  184. }
  185. db.TradeRecord.Add(new TradeRecord()
  186. {
  187. ParentNav = user.ParentNav,
  188. CreateDate = trade.CreateDate,
  189. UpdateDate = trade.UpdateDate,
  190. RecordNo = trade.TradeSerialNo, //单号
  191. UserId = pos.UserId, //创客
  192. MerchantId = pos.BindMerchantId, //商户
  193. MerNo = trade.MerNo, //渠道商户编号
  194. MerHelpFlag = MerHelpFlag, //是否属于扶持周期内商户
  195. HelpMonthCount = 0, //扶持第几个月
  196. MerBuddyType = user.MerchantType, //商户创客类型
  197. SnNo = trade.TradeSnNo, //渠道SN号
  198. TradeSerialNo = trade.ChannelSerial, //交易流水号
  199. TradeAmount = TradeAmount, //交易金额
  200. BankCardType = BankCardType, //银行卡类型
  201. QrPayFlag = QrPayFlag, //云闪付标识
  202. CapFlag = CapFlag, //借记卡是否封顶
  203. VipFlag = VipFlag, //会员标记
  204. PayType = PayType, //支付方式
  205. BrandId = BrandId, //品牌
  206. Remark = trade.Remark, //备注
  207. TopUserId = TopUserId, //顶级创客
  208. MerUserId = pos.UserId, //商户直属创客
  209. });
  210. }
  211. decimal CheckMoney = 1000;
  212. int CheckDays = 30;
  213. if (pos.BrandId == 6)
  214. {
  215. CheckMoney = 5000;
  216. }
  217. if (BankCardType == 1 || (BankCardType == 2 && pos.BrandId == 1) || (BankCardType == 2 && pos.BrandId == 3))
  218. {
  219. pos.CreditTrade += SourceTradeAmount;
  220. }
  221. else if (BankCardType == 0)
  222. {
  223. pos.DebitCardTrade += TradeAmount;
  224. }
  225. // if (pos.CreditTrade < CheckMoney)
  226. // {
  227. // decimal deposit = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  228. // if (deposit > 0 && pos.IsPurchase != 1)
  229. // {
  230. // pos.IsPurchase = 0;
  231. // }
  232. // }
  233. if (pos.CreditTrade >= CheckMoney && pos.ActivationState == 0 && pos.TransferTime < pos.BindingTime && pos.BindingTime > DateTime.Now.AddDays(-CheckDays))
  234. {
  235. // decimal deposit = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  236. // if (deposit == 0 && pos.IsPurchase != 1)
  237. // {
  238. // pos.IsPurchase = 0;
  239. // }
  240. pos.ActivationState = 1;
  241. pos.ActivationTime = DateTime.Now;
  242. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  243. if (merchant != null)
  244. {
  245. merchant.ActiveStatus = 1;
  246. merchant.MerStandardDate = DateTime.Now;
  247. db.SaveChanges();
  248. }
  249. //发放大盟主奖励
  250. if(pos.LeaderUserId > 0)
  251. {
  252. RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id);
  253. }
  254. }
  255. db.SaveChanges();
  256. }
  257. SpModels.TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  258. if (edit != null)
  259. {
  260. edit.Status = 2;
  261. }
  262. spdb.SaveChanges();
  263. }
  264. }
  265. }
  266. }
  267. spdb.Dispose();
  268. db.Dispose();
  269. }
  270. }
  271. }