ProfitHelper.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using MySystem.Models.Main;
  6. using Library;
  7. using System.Threading;
  8. using Microsoft.Extensions.Hosting;
  9. using System.Threading.Tasks;
  10. using LitJson;
  11. using MySystem.Models;
  12. namespace MySystem
  13. {
  14. public class ProfitHelper
  15. {
  16. public readonly static ProfitHelper Instance = new ProfitHelper();
  17. private ProfitHelper()
  18. {
  19. }
  20. #region 消费队列(执行消费退款金额的统计)
  21. public void StartListenTrade()
  22. {
  23. Thread th = new Thread(StartListenTradeDo);
  24. th.IsBackground = true;
  25. th.Start();
  26. }
  27. public void StartListenTradeDo()
  28. {
  29. while (true)
  30. {
  31. string orderidstring = RedisDbconn.Instance.RPop<string>("ConsumerOrdersHd:Divi:1:List");
  32. if (!string.IsNullOrEmpty(orderidstring))
  33. {
  34. ReturnStat(orderidstring, 1);
  35. }
  36. else
  37. {
  38. Thread.Sleep(60000);
  39. }
  40. }
  41. }
  42. public void StartListenWxTrade()
  43. {
  44. Thread th = new Thread(StartListenWxTradeDo);
  45. th.IsBackground = true;
  46. th.Start();
  47. }
  48. public void StartListenWxTradeDo()
  49. {
  50. while (true)
  51. {
  52. string orderidstring = RedisDbconn.Instance.RPop<string>("ConsumerOrdersHd:Divi:2:List");
  53. if (!string.IsNullOrEmpty(orderidstring))
  54. {
  55. ReturnStat(orderidstring, 2);
  56. }
  57. else
  58. {
  59. Thread.Sleep(10000);
  60. }
  61. }
  62. }
  63. public void ReturnStat(string orderidstring, int PayMode)
  64. {
  65. function.WriteLog("开始返现:" + orderidstring, "返现逻辑日志");
  66. int OrderId = int.Parse(function.CheckInt(orderidstring));
  67. WebCMSEntities db = new WebCMSEntities();
  68. using (var tran = db.Database.BeginTransaction())
  69. {
  70. try
  71. {
  72. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == OrderId);
  73. if (order != null)
  74. {
  75. decimal PayMoney = order.PayMoney;
  76. int MerchantId = order.MerchantId;
  77. function.WriteLog("MerchantId:" + MerchantId, "返现逻辑日志");
  78. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == MerchantId);
  79. if (merchant != null)
  80. {
  81. int UserId = merchant.UserId;
  82. List<CustomTagSet> customTagSets = db.CustomTagSet.ToList(); //获取公用配置参数集和
  83. //获取参数
  84. CustomTagSet profitPercentSet = customTagSets.FirstOrDefault(m => m.Tags == "CusumerProfitPercent") ?? new CustomTagSet();
  85. decimal profitPercent = decimal.Parse(function.CheckNum(profitPercentSet.Contents)); //基于收款金额的总分润比例;
  86. CustomTagSet cusumerFeePercentSet = customTagSets.FirstOrDefault(m => m.Tags == "CusumerFeePercent") ?? new CustomTagSet();
  87. decimal cusumerFeePercent = decimal.Parse(function.CheckNum(cusumerFeePercentSet.Contents)); //费率;
  88. CustomTagSet minProfitSet = customTagSets.FirstOrDefault(m => m.Tags == "MinProfit") ?? new CustomTagSet();
  89. decimal minProfit = decimal.Parse(function.CheckNum(minProfitSet.Contents)); //最小分润值;
  90. MerchantParamSet set = Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantParamSet>(order.SeoDescription);
  91. int DiviPersons = set.DiviPersons; //单笔订单分红人数
  92. decimal DiviPercent = set.DiviPercent; //单人最大分红比例
  93. int ProfitDays = set.ProfitDays; //分红期限(天)
  94. decimal GetPercent = set.GetPercent / 100; //商家实收比例
  95. decimal MinPayMoney = set.MinPayMoney; //订单参与门槛
  96. int IsAll = set.IsAll; //是否收全额
  97. // decimal TotalActual = 0;
  98. if (IsAll == 0 && PayMoney >= MinPayMoney && GetPercent < 1)
  99. {
  100. function.WriteLog("活动开启", "返现逻辑日志");
  101. // PayMoney * (1 - 0.1 - 0.0038 - 0.01);
  102. decimal DiviMoney = PayMoney * (1 - GetPercent - cusumerFeePercent - profitPercent);
  103. if (DiviMoney > 0)
  104. {
  105. function.WriteLog("返现队列开始", "返现逻辑日志");
  106. RedisDbconn.Instance.AddRightList("ConsumerOrdersHd:Divi:" + PayMode + ":" + order.MerchantId, order);
  107. order.ReturnFlag = 1;
  108. db.SaveChanges();
  109. DiviMoney = DiviMoney / DiviPersons;
  110. DiviMoney = Math.Round(DiviMoney, 2);
  111. List<int> deletes = new List<int>();
  112. long CurDiviPersons = DiviPersons;
  113. long QueueCount = RedisDbconn.Instance.Count("ConsumerOrdersHd:Divi:" + PayMode + ":" + MerchantId);
  114. long CurQueueCount = QueueCount;
  115. if (CurDiviPersons >= CurQueueCount)
  116. {
  117. CurDiviPersons = CurQueueCount;
  118. }
  119. if (DiviPersons < QueueCount)
  120. {
  121. QueueCount = DiviPersons;
  122. }
  123. // 减去自己付的订单
  124. List<int> orderids = new List<int>();
  125. function.WriteLog("人数:" + CurDiviPersons, "返现逻辑日志");
  126. while (CurDiviPersons > 0)
  127. {
  128. ConsumerOrders suborder = RedisDbconn.Instance.RPopLPush<ConsumerOrders>("ConsumerOrdersHd:Divi:" + PayMode + ":" + MerchantId, "ConsumerOrdersHd:Divi:" + PayMode + ":" + MerchantId);
  129. if (suborder != null)
  130. {
  131. decimal TmpCurDivi = suborder.CurDivi;
  132. function.WriteLog("返现对象订单:" + suborder.Id, "返现逻辑日志");
  133. MerchantParamSet subset = Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantParamSet>(suborder.SeoDescription);
  134. int subProfitDays = subset.ProfitDays; //分红期限(天)
  135. bool IsOut = suborder.UpdateDate.Value.AddDays(subProfitDays) < DateTime.Now ? true : false;
  136. if (suborder.CurDivi < suborder.MaxDivi && suborder.MaxDivi - suborder.CurDivi >= 0.01M && !IsOut)
  137. {
  138. function.WriteLog("没出局:" + suborder.CurDivi + "--" + suborder.MaxDivi, "返现逻辑日志");
  139. //如果没过期并且当前退款金额没超过最大退款金额,就执行
  140. decimal GetMoney = suborder.MaxDivi - suborder.CurDivi;
  141. if (GetMoney >= DiviMoney)
  142. {
  143. GetMoney = DiviMoney;
  144. }
  145. function.WriteLog("GetMoney:" + GetMoney, "返现逻辑日志");
  146. if (GetMoney > minProfit)
  147. {
  148. suborder.CurDivi += GetMoney;
  149. MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == suborder.MerchantId) ?? new MerchantAddInfo();
  150. //聚合支付退款
  151. bool BackStatus = false;
  152. decimal RefundAmount = GetMoney * 100;
  153. RefundAmount = Math.Round(RefundAmount, 2);
  154. if(!orderids.Contains(suborder.Id))
  155. {
  156. function.WriteLog("请求退款接口", "返现逻辑日志");
  157. string RefundNo = "HDTK" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  158. var info = HaoDaHelper.Instance.AggregatedPayRefund(merchantAdd.OutMchtNo, merchantAdd.StoreNo, RefundNo, suborder.OrderNo, RefundAmount.ToString("f0"));
  159. if(info.Contains("\"resultCode\":\"1\"") && info.Contains("\"tradeStatus\":\"2\""))
  160. {
  161. db.ConsumerProfit.Add(new ConsumerProfit()
  162. {
  163. CreateDate = DateTime.Now,
  164. ConsumerId = suborder.ConsumerId,
  165. MerchantId = suborder.MerchantId,
  166. OrderId = suborder.Id,
  167. GetMoney = Math.Round(GetMoney, 2),
  168. QueryCount = PayMode,
  169. Sort = OrderId,
  170. SeoTitle = suborder.OrderNo,
  171. SeoKeyword = suborder.PayMoney.ToString(),
  172. SeoDescription = RefundNo,
  173. });
  174. db.SaveChanges();
  175. BackStatus = true;
  176. }
  177. function.WriteLog("接口返回:" + info, "返现逻辑日志");
  178. orderids.Add(suborder.Id);
  179. }
  180. if(BackStatus)
  181. {
  182. merchant.ActCurrentAmount += Math.Round(GetMoney, 2); //活动已返金额
  183. db.SaveChanges();
  184. function.WriteLog(suborder.CurDivi + ":" + suborder.MaxDivi, "返现逻辑日志");
  185. if (suborder.CurDivi < suborder.MaxDivi)
  186. {
  187. function.WriteLog("返现未达最大值继续", "返现逻辑日志");
  188. // RedisDbconn.Instance.AddList("ConsumerOrdersHd:Divi:" + PayMode + ":" + MerchantId, suborder);
  189. RedisDbconn.Instance.SetList("ConsumerOrdersHd:Divi:" + PayMode + ":" + MerchantId, 0, suborder);
  190. RedisDbconn.Instance.AddList("ConsumerOrdersSetDiviHd", "{\"OrderId\":\"" + suborder.Id + "\",\"DiviAmt\":\"" + suborder.CurDivi + "\"}");
  191. }
  192. else
  193. {
  194. suborder.CurDivi = TmpCurDivi;
  195. function.WriteLog("返现达最大值,出局", "返现逻辑日志");
  196. ConsumerOrders editOrder = db.ConsumerOrders.FirstOrDefault(m => m.Id == suborder.Id);
  197. if (editOrder != null)
  198. {
  199. editOrder.ReturnFlag = 0;
  200. db.SaveChanges();
  201. RedisDbconn.Instance.AddList("ConsumerOrdersSetDiviHd", "{\"OrderId\":\"" + suborder.Id + "\",\"DiviAmt\":\"" + suborder.MaxDivi + "\"}");
  202. }
  203. RedisDbconn.Instance.DelFromList("ConsumerOrdersHd:Divi:" + PayMode + ":" + MerchantId, suborder);
  204. }
  205. }
  206. Thread.Sleep(1000);
  207. }
  208. }
  209. else
  210. {
  211. //否者计算人数不计入退款名额
  212. suborder.CurDivi = TmpCurDivi;
  213. function.WriteLog("返现达最大值,出局", "返现逻辑日志");
  214. ConsumerOrders editOrder = db.ConsumerOrders.FirstOrDefault(m => m.Id == suborder.Id);
  215. if (editOrder != null)
  216. {
  217. editOrder.ReturnFlag = 0;
  218. db.SaveChanges();
  219. RedisDbconn.Instance.AddList("ConsumerOrdersSetDiviHd", "{\"OrderId\":\"" + suborder.Id + "\",\"DiviAmt\":\"" + suborder.MaxDivi + "\"}");
  220. }
  221. RedisDbconn.Instance.DelFromList("ConsumerOrdersHd:Divi:" + PayMode + ":" + MerchantId, suborder);
  222. CurDiviPersons += 1;
  223. function.WriteLog("出局:CurDiviPersons:" + CurDiviPersons, "返现逻辑日志");
  224. }
  225. }
  226. CurDiviPersons -= 1;
  227. QueueCount = RedisDbconn.Instance.Count("ConsumerOrdersHd:Divi:" + PayMode + ":" + MerchantId);
  228. function.WriteLog("当前队列数量:" + QueueCount, "返现逻辑日志");
  229. if (QueueCount == 0)
  230. {
  231. CurDiviPersons = 0;
  232. }
  233. }
  234. function.WriteLog("返现队列结束", "返现逻辑日志");
  235. }
  236. }
  237. }
  238. }
  239. tran.Commit();
  240. }
  241. catch (Exception ex)
  242. {
  243. LogHelper.Instance.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + ex.ToString(), "分红返现异常");
  244. tran.Rollback();
  245. RedisDbconn.Instance.AddList("ConsumerProfitCheck", orderidstring);
  246. }
  247. }
  248. db.Dispose();
  249. function.WriteLog("结束返现:" + orderidstring + "\n\n\n", "返现逻辑日志");
  250. }
  251. #endregion
  252. #region 分润
  253. public void StartListenProfit()
  254. {
  255. Thread th = new Thread(StartListenProfitDo);
  256. th.IsBackground = true;
  257. th.Start();
  258. }
  259. public void StartListenProfitDo()
  260. {
  261. while(true)
  262. {
  263. if(DateTime.Now.Day < 10 && DateTime.Now.Hour > 2 && DateTime.Now.Hour < 23)
  264. {
  265. try
  266. {
  267. DoProfit();
  268. }
  269. catch(Exception ex)
  270. {
  271. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "来客吧分润异常");
  272. }
  273. Thread.Sleep(600000);
  274. }
  275. else
  276. {
  277. Thread.Sleep(3600000);
  278. }
  279. }
  280. }
  281. //分润算法
  282. public void DoProfit()
  283. {
  284. string TradeMonth = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  285. string check = function.ReadInstance("/ProfitFlag/" + TradeMonth + ".txt");
  286. if(!string.IsNullOrEmpty(check))
  287. {
  288. return;
  289. }
  290. function.WritePage("/ProfitFlag/", TradeMonth + ".txt", DateTime.Now.ToString());
  291. Models.KxsMain.WebCMSEntities kxsdb = new Models.KxsMain.WebCMSEntities();
  292. WebCMSEntities dbnew = new WebCMSEntities();
  293. DataTable dt = CustomerSqlConn.dtable("select IsAct,UserId,sum(TotalAmount) from UserAmountSummary where TradeMonth='" + TradeMonth + "' and SeoTitle='self' group by IsAct,UserId", AppConfig.Base.SqlConnStr);
  294. foreach(DataRow dr in dt.Rows)
  295. {
  296. int UserId = int.Parse(dr["UserId"].ToString());
  297. bool IsActive = dr["IsAct"].ToString() == "1";
  298. decimal TotalAmount = decimal.Parse(dr[2].ToString());
  299. List<ProfitUsers> users = new List<ProfitUsers>();
  300. int TopUserId = 0; //顶级创客Id
  301. Models.KxsMain.Users us = kxsdb.Users.FirstOrDefault(a => a.Id == UserId);
  302. if (us != null)
  303. {
  304. string uidstring = us.Id.ToString();
  305. if (!string.IsNullOrEmpty(us.ParentNav))
  306. {
  307. uidstring = us.ParentNav.Trim(',').Replace(",,", ",") + "," + uidstring;
  308. string[] parents = us.ParentNav.Trim(',').Replace(",,", ",").Split(',');
  309. if(parents.Length > 1)
  310. {
  311. TopUserId = int.Parse(function.CheckInt(parents[1]));
  312. }
  313. else
  314. {
  315. TopUserId = int.Parse(function.CheckInt(parents[0]));
  316. }
  317. }
  318. string[] uidlist = uidstring.Split(',');
  319. Array.Reverse(uidlist);
  320. foreach (string uidstr in uidlist)
  321. {
  322. int puid = int.Parse(function.CheckInt(uidstr));
  323. Models.KxsMain.Users pus = kxsdb.Users.FirstOrDefault(a => a.Id == puid);
  324. if (pus != null)
  325. {
  326. users.Add(new ProfitUsers()
  327. {
  328. UserId = pus.Id,
  329. MakerCode = pus.MakerCode,
  330. RealName = pus.RealName,
  331. UserNav = pus.ParentNav,
  332. UserLevel = pus.UserLevel,
  333. CreateDate = pus.CreateDate.Value,
  334. });
  335. }
  336. }
  337. }
  338. Models.KxsMain.Users topUser = kxsdb.Users.FirstOrDefault(a => a.Id == TopUserId) ?? new Models.KxsMain.Users();
  339. LogHelper.Instance.WriteLog("---UserId:" + UserId + ";TotalAmount:" + TotalAmount + ";", "来客吧分润日志");
  340. List<ProfitResult> list = new List<ProfitResult>();
  341. list = StartProft(TotalAmount, 1, users, IsActive);
  342. LogHelper.Instance.WriteLog("---list:" + list.Count + ";", "来客吧分润日志");
  343. try
  344. {
  345. foreach (ProfitResult sub in list)
  346. {
  347. int ProfitType = sub.UserId == UserId ? 0 : 1;
  348. ProfitRewardRecord editprofitrecord = dbnew.ProfitRewardRecord.FirstOrDefault(m => m.UserId == sub.UserId && m.BrandId == 1 && m.ProfitType == ProfitType && m.TradeMonth == TradeMonth);
  349. if (editprofitrecord == null)
  350. {
  351. editprofitrecord = dbnew.ProfitRewardRecord.Add(new ProfitRewardRecord()
  352. {
  353. CreateDate = DateTime.Now,
  354. UserId = sub.UserId, //创客
  355. BrandId = 1, //品牌
  356. ProfitType = ProfitType, //创客分润类型
  357. TradeMonth = TradeMonth, //交易月
  358. TopUserId = TopUserId, //顶级创客
  359. CreateMan = sub.MakerCode, //创客编号
  360. UpdateMan = topUser.MakerCode, //顶级编号
  361. SeoTitle = sub.RealName, //创客姓名
  362. SeoKeyword = topUser.RealName, //创客姓名
  363. }).Entity;
  364. dbnew.SaveChanges();
  365. string RecordNo = "KPM";
  366. int RecordId = editprofitrecord.Id;
  367. string RecordIdString = RecordId.ToString();
  368. for (int i = 0; i < 14 - RecordId.ToString().Length; i++)
  369. {
  370. RecordIdString = "0" + RecordIdString;
  371. }
  372. RecordNo += RecordIdString;
  373. editprofitrecord.RecordNo = RecordNo;
  374. dbnew.SaveChanges();
  375. }
  376. if(IsActive)
  377. {
  378. editprofitrecord.CreditTradeAmt += TotalAmount;
  379. editprofitrecord.CreditTradeProfit += sub.Money;
  380. }
  381. else
  382. {
  383. editprofitrecord.TradeAmt += TotalAmount;
  384. editprofitrecord.TradeProfit += sub.Money;
  385. }
  386. dbnew.SaveChanges();
  387. }
  388. dbnew.SaveChanges();
  389. }
  390. catch (Exception ex)
  391. {
  392. LogHelper.Instance.WriteLog(ex.ToString(), "分润异常");
  393. }
  394. }
  395. dbnew.Dispose();
  396. kxsdb.Dispose();
  397. }
  398. #endregion
  399. #region 分润算法
  400. public List<ProfitResult> StartProft(decimal TotalAmount, int LevelKindId, List<ProfitUsers> users, bool IsActive = true)
  401. {
  402. LogHelper.Instance.WriteLog("\n\nTotalAmount:" + TotalAmount + ";", "来客吧分润日志");
  403. string Month = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  404. WebCMSEntities db = new WebCMSEntities();
  405. List<UserLevelSet> levels = db.UserLevelSet.ToList();
  406. List<ProfitResult> result = new List<ProfitResult>();
  407. ProfitObjects obj = db.ProfitObjects.FirstOrDefault();
  408. if (obj.Status == 1) //判断分润是否开启
  409. {
  410. int maxLevel = obj.MaxLevel; //最大等级
  411. int maxFloor = obj.MaxFloor; //最大层级
  412. decimal diffLevelProfit = 0; //等级级差
  413. ProfitObjectLevels maxlevel = db.ProfitObjectLevels.FirstOrDefault(m => m.Id == 9) ?? new ProfitObjectLevels(); //获取最高等级参数
  414. decimal maxPercent = IsActive ? maxlevel.DebitPercents : maxlevel.Percents;
  415. LogHelper.Instance.WriteLog("maxPercent:" + maxPercent + ";", "来客吧分润日志");
  416. decimal maxLevelProfit = TotalAmount * maxPercent;
  417. LogHelper.Instance.WriteLog("maxLevelProfit:" + maxLevelProfit + ";", "来客吧分润日志");
  418. maxLevelProfit = PublicFunction.NumberFormat(maxLevelProfit);
  419. LogHelper.Instance.WriteLog("maxLevelProfit:" + maxLevelProfit + ";", "来客吧分润日志");
  420. // decimal diffDiviProfit = 0; //分红级差
  421. int curLevel = 0; //当前层级的会员等级
  422. for (int curFloor = 1; curFloor <= users.Count; curFloor++)
  423. {
  424. ProfitUsers user = new ProfitUsers();
  425. if (curFloor <= users.Count)
  426. {
  427. user = users[curFloor - 1];
  428. }
  429. SubUser selfUser = GetUser(user.UserId, Month) ?? new SubUser();
  430. int UserLevel = GetUserLevel(selfUser); //当前会员等级
  431. //判断当前创客是否有直推的激活机具,并且在活动时间内
  432. if (curLevel == maxLevel)
  433. {
  434. break;
  435. }
  436. if (UserLevel <= maxLevel && UserLevel >= curLevel)
  437. {
  438. ProfitObjectLevels objlevel = db.ProfitObjectLevels.FirstOrDefault(m => m.Id == UserLevel); //获取当前等级参数
  439. if (objlevel != null)
  440. {
  441. decimal getLevelProfit = 0; //等级分润
  442. UserProfitSet profitSet = new UserProfitSet();
  443. decimal profitPercent = IsActive ? objlevel.DebitPercents : objlevel.Percents;
  444. LogHelper.Instance.WriteLog("money:" + UserLevel + ":" + profitPercent + ";", "来客吧分润日志");
  445. if (profitPercent > 0)
  446. {
  447. decimal profitTmp = TotalAmount * profitPercent;
  448. getLevelProfit += profitTmp;
  449. LogHelper.Instance.WriteLog("money:" + UserLevel + ":" + profitTmp + ";", "来客吧分润日志");
  450. }
  451. decimal money = getLevelProfit;
  452. getLevelProfit -= diffLevelProfit;
  453. if (objlevel.LevelDiff == 1) //判断是否有级差
  454. {
  455. diffLevelProfit = money;
  456. }
  457. if (getLevelProfit >= obj.MinProfitVal)
  458. {
  459. result.Add(new ProfitResult()
  460. {
  461. UserId = user.UserId,
  462. MakerCode = user.MakerCode,
  463. RealName = user.RealName,
  464. UserNav = user.UserNav,
  465. Money = PublicFunction.NumberFormat(getLevelProfit),
  466. ProfitRate = profitSet.ProfitRate,
  467. ProfitPercent = profitSet.ProfitPercent,
  468. AddOrSubRate = profitSet.AddOrSubRate,
  469. ProfitRateBase = profitSet.ProfitRateBase,
  470. });
  471. LogHelper.Instance.WriteLog("money:" + UserLevel + ":" + PublicFunction.NumberFormat(getLevelProfit) + ";", "来客吧分润日志");
  472. }
  473. }
  474. }
  475. if(curLevel < UserLevel)
  476. {
  477. curLevel = UserLevel;
  478. }
  479. }
  480. }
  481. db.Dispose();
  482. return result;
  483. }
  484. public SubUser GetUser(int UserId, string Month)
  485. {
  486. SubUser user = new SubUser();
  487. DataTable dt = GetDataTable("select * from Users" + Month + " where Id=" + UserId);
  488. if(dt.Rows.Count > 0)
  489. {
  490. user.Id = int.Parse(dt.Rows[0]["Id"].ToString());
  491. user.ParentUserId = int.Parse(dt.Rows[0]["ParentUserId"].ToString());
  492. user.ParentNav = dt.Rows[0]["ParentNav"].ToString();
  493. user.UserLevel = int.Parse(dt.Rows[0]["UserLevel"].ToString());
  494. user.PreUserLevel = int.Parse(dt.Rows[0]["PreUserLevel"].ToString());
  495. user.PreLeaderLevel = int.Parse(dt.Rows[0]["PreLeaderLevel"].ToString());
  496. user.PreOpLevel = int.Parse(dt.Rows[0]["PreOpLevel"].ToString());
  497. }
  498. return user;
  499. }
  500. public int GetUserLevel(SubUser user)
  501. {
  502. int level = user.UserLevel;
  503. if(user.PreUserLevel > level) level = user.PreUserLevel;
  504. if(user.PreLeaderLevel > level) level = user.PreLeaderLevel;
  505. if(user.PreOpLevel > level) level = user.PreOpLevel;
  506. return level;
  507. }
  508. public DataTable GetDataTable(string sqlstr)
  509. {
  510. DataTable dt = CustomerSqlConn.dtable(sqlstr, Library.ConfigurationManager.AppSettings["KxsStatSqlConnStr"].ToString());
  511. return dt;
  512. }
  513. #endregion
  514. public void StartSetDivi()
  515. {
  516. Thread th = new Thread(StartSetDiviDo);
  517. th.IsBackground = true;
  518. th.Start();
  519. }
  520. public void StartSetDiviDo()
  521. {
  522. while (true)
  523. {
  524. string content = RedisDbconn.Instance.RPop<string>("ConsumerOrdersSetDiviHd");
  525. if (!string.IsNullOrEmpty(content))
  526. {
  527. SetDivi(content);
  528. }
  529. else
  530. {
  531. Thread.Sleep(10000);
  532. }
  533. }
  534. }
  535. public void SetDivi(string content)
  536. {
  537. try
  538. {
  539. JsonData jsonObj = JsonMapper.ToObject(content);
  540. int orderId = int.Parse(function.CheckInt(jsonObj["OrderId"].ToString()));
  541. decimal diviAmt = decimal.Parse(function.CheckNum(jsonObj["DiviAmt"].ToString()));
  542. WebCMSEntities db = new WebCMSEntities();
  543. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == orderId);
  544. if(order != null)
  545. {
  546. order.CurDivi = diviAmt;
  547. db.SaveChanges();
  548. }
  549. db.Dispose();
  550. }
  551. catch(Exception ex)
  552. {
  553. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "设置订单当前返现金额异常");
  554. }
  555. }
  556. }
  557. }