ProfitHelperV2.cs 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using MySystem.PxcModels;
  6. using Library;
  7. using System.Threading;
  8. using Microsoft.Extensions.Hosting;
  9. using System.Threading.Tasks;
  10. namespace MySystem
  11. {
  12. public class ProfitHelperV2
  13. {
  14. public readonly static ProfitHelperV2 Instance = new ProfitHelperV2();
  15. private ProfitHelperV2()
  16. {
  17. }
  18. #region 获取分润规则Id
  19. public int GetLevelKindId(int BrandId, int Help, int BankCardType)
  20. {
  21. return RedisDbconn.Instance.Get<int>("pobjlv:kid:" + BrandId + ":" + Help + ":" + BankCardType);
  22. }
  23. #endregion
  24. #region POS分润算法
  25. public void StatProfit()
  26. {
  27. Thread th = new Thread(StatProfitDo);
  28. th.IsBackground = true;
  29. th.Start();
  30. }
  31. public void StatProfitDo()
  32. {
  33. while (true)
  34. {
  35. string content = RedisDbconn.Instance.RPop<string>("DoProfitQueue");
  36. if(!string.IsNullOrEmpty(content) && DateTime.Now.Day < 14)
  37. {
  38. StatProfiting();
  39. }
  40. else
  41. {
  42. Thread.Sleep(60000);
  43. }
  44. }
  45. }
  46. public void StatProfiting()
  47. {
  48. string Month = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  49. string check = function.ReadInstance("/ProfitStat/" + Month + "-1.txt");
  50. if (!string.IsNullOrEmpty(check))
  51. {
  52. return;
  53. }
  54. function.WritePage("/ProfitStat/", Month + "-1.txt", DateTime.Now.ToString());
  55. try
  56. {
  57. WebCMSEntities db = new WebCMSEntities();
  58. List<int> BrandIds = db.KqProducts.OrderBy(m => m.Id).ToList().Select(m => m.Id).ToList();
  59. foreach (int i in BrandIds) //品牌
  60. {
  61. for (int j = 0; j <= 1; j++) //卡类型
  62. {
  63. int PageNum = 1;
  64. bool op = true;
  65. while(op)
  66. {
  67. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + "BrandId:" + i + ",BankCardType:" + j + ",Month:" + Month + ",PageNum:" + PageNum, "分润监控日志");
  68. List<ProfitResult> result = new List<ProfitResult>();
  69. if(i == 14)
  70. {
  71. result = StartProftForSimByDate(Month, i, PageNum);
  72. }
  73. else
  74. {
  75. result = StartProftForPosByDate(i, j, Month, PageNum);
  76. }
  77. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + "result:" + result.Count, "分润监控日志");
  78. function.WriteLog("\r\n", "分润监控日志");
  79. if(result.Count > 0)
  80. {
  81. foreach (ProfitResult sub in result)
  82. {
  83. int TopUserId = 0;
  84. if (!string.IsNullOrEmpty(sub.UserNav))
  85. {
  86. string[] UserNavList = sub.UserNav.Trim(',').Replace(",,", ",").Split(',');
  87. if (UserNavList.Length > 1)
  88. {
  89. TopUserId = int.Parse(UserNavList[1]);
  90. }
  91. else
  92. {
  93. TopUserId = int.Parse(UserNavList[0]);
  94. }
  95. }
  96. string RecordNo = "P" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  97. decimal ProfitAmt = sub.Money;
  98. decimal TradeAmt = sub.TradeAmount;
  99. decimal ProfitRate = sub.ProfitRate;
  100. if (sub.IsSubsidy == 1 && j == 1)
  101. {
  102. }
  103. else
  104. {
  105. decimal CreditTradeAmt = 0;
  106. decimal CreditTradeProfit = 0;
  107. decimal QrCreditTradeAmt = 0;
  108. decimal QrCreditProfitRate = 0;
  109. decimal QrCreditProfitStandardRate = 0;
  110. decimal QrCreditAddOrSubRate = 0;
  111. decimal QrCreditTradeProfit = 0;
  112. decimal NonQrCreditTradeAmt = 0;
  113. decimal NotHelpCreditTradeAmt = 0;
  114. decimal NotHelpCreditTradeProfit = 0;
  115. decimal NotHelpCreditProfitRate = 0;
  116. decimal NotHelpCreditProfitStandardRate = 0;
  117. decimal NotHelpCreditAddOrSubRate = 0;
  118. decimal HelpCreditTradeAmt = 0;
  119. decimal NonQrCreditTradeProfit = 0;
  120. decimal HelpCreditTradeProfit = 0;
  121. decimal HelpCreditProfitRate = 0;
  122. decimal HelpCreditProfitStandardRate = 0;
  123. decimal HelpCreditAddOrSubRate = 0;
  124. decimal DebitNonCapTradeAmt = 0;
  125. decimal DebitCapTradeAmt = 0;
  126. decimal QrDebitNotCapTradeAmt = 0;
  127. decimal NonQrDebitNotCapTradeAmt = 0;
  128. decimal NotHelpDebitNonCapTradeAmt = 0;
  129. decimal HelpDebitNonCapTradeAmt = 0;
  130. decimal DebitNonTradeCapProfit = 0;
  131. decimal QrDebitNonTradeCapProfit = 0;
  132. decimal NonQrDebitNonTradeCapProfit = 0;
  133. decimal NotHelpDebitNonTradeCapProfit = 0;
  134. decimal HelpDebitNonTradeCapProfit = 0;
  135. decimal DebitTradeCapProfit = 0;
  136. decimal NotHelpDebitProfitRate = 0;
  137. decimal NotHelpDebitProfitStandardRate = 0;
  138. decimal NotHelpDebitAddOrSubRate = 0;
  139. decimal HelpDebitProfitRate = 0;
  140. decimal HelpDebitProfitStandardRate = 0;
  141. decimal HelpDebitAddOrSubRate = 0;
  142. decimal DebitCapSingleReward = 0;
  143. decimal QrDebitProfitRate = 0;
  144. decimal QrDebitProfitStandardRate = 0;
  145. decimal QrDebitAddOrSubRate = 0;
  146. decimal ProfitCreditTradeProfit = 0;
  147. decimal ProfitCreditProfitRate = 0;
  148. decimal ProfitCreditTradeAmt = 0;
  149. decimal ProfitCreditTradeProfit2 = 0;
  150. decimal ProfitCreditProfitRate2 = 0;
  151. decimal ProfitCreditTradeAmt2 = 0;
  152. decimal ProfitDebitNonCapTradeAmt = 0;
  153. decimal ProfitDebitNonTradeCapProfit = 0;
  154. decimal ProfitDebitProfitRate = 0;
  155. if (j == 1)
  156. {
  157. CreditTradeAmt = TradeAmt; //贷记卡交易总金额
  158. CreditTradeProfit = ProfitAmt; //贷记卡分润总金额
  159. if (sub.QrPayFlag == 1)
  160. {
  161. QrCreditTradeAmt = TradeAmt; //(云闪付)贷记卡交易总金额
  162. QrCreditProfitRate = ProfitRate; //(云闪付)贷记卡交易分润比例
  163. QrCreditTradeProfit = ProfitAmt; //(云闪付)贷记卡交易分润
  164. }
  165. else
  166. {
  167. NonQrCreditTradeAmt = TradeAmt; //(非云闪付)贷记卡交易总金额
  168. NonQrCreditTradeProfit = ProfitAmt; //(非云闪付)贷记卡交易分润
  169. if(sub.HelpFlag == 1)
  170. {
  171. HelpCreditTradeProfit = ProfitAmt; //(扶持期)(非云闪付)贷记卡分润总金额
  172. HelpCreditProfitRate = ProfitRate; //(扶持期)(非云闪付)贷记卡商户交易对应分润比例
  173. HelpCreditTradeAmt = TradeAmt; //(扶持期)(非云闪付)贷记卡交易总金额
  174. }
  175. else if(sub.HelpFlag == 2)
  176. {
  177. ProfitCreditTradeProfit = ProfitAmt; //(盈利期)(非云闪付)贷记卡分润总金额
  178. ProfitCreditProfitRate = ProfitRate; //(盈利期)(非云闪付)贷记卡商户交易对应分润比例
  179. ProfitCreditTradeAmt = TradeAmt; //(盈利期)(非云闪付)贷记卡交易总金额
  180. }
  181. else if(sub.HelpFlag == 3)
  182. {
  183. ProfitCreditTradeProfit2 = ProfitAmt; //(盈利期)(非云闪付)贷记卡分润总金额
  184. ProfitCreditProfitRate2 = ProfitRate; //(盈利期)(非云闪付)贷记卡商户交易对应分润比例
  185. ProfitCreditTradeAmt2 = TradeAmt; //(盈利期)(非云闪付)贷记卡交易总金额
  186. }
  187. else
  188. {
  189. NotHelpCreditTradeAmt = TradeAmt; //(非扶持期)(非云闪付)贷记卡交易总金额
  190. NotHelpCreditTradeProfit = ProfitAmt; //(非扶持期)(非云闪付)贷记卡分润总金额
  191. NotHelpCreditProfitRate = ProfitRate; //(非扶持期)(非云闪付)贷记卡商户交易对应分润比例
  192. }
  193. }
  194. }
  195. if (j == 0)
  196. {
  197. if (sub.Money < 1)
  198. {
  199. DebitNonCapTradeAmt = TradeAmt; //商户非封顶借记卡交易总额
  200. DebitNonTradeCapProfit = ProfitAmt; //借记卡非封顶交易分润总金额
  201. }
  202. else
  203. {
  204. DebitCapTradeAmt = TradeAmt; //商户借记卡封顶交易总额
  205. DebitCapSingleReward = ProfitAmt; //借记卡合伙人直营封顶交易奖励金额
  206. DebitTradeCapProfit = ProfitAmt; //借记卡封顶交易分润总金额
  207. }
  208. if (sub.QrPayFlag == 1)
  209. {
  210. if (sub.Money < 1)
  211. {
  212. QrDebitNotCapTradeAmt = TradeAmt; //(云闪付)商户非封顶借记卡交易总额
  213. QrDebitProfitRate = ProfitRate; //(云闪付)借记卡非封顶交易分润比例
  214. QrDebitNonTradeCapProfit = ProfitAmt; //(云闪付)借记卡非封顶交易分润总金额
  215. }
  216. }
  217. else
  218. {
  219. if (sub.Money < 1)
  220. {
  221. NonQrDebitNonTradeCapProfit = ProfitAmt; //(非云闪付)借记卡非封顶交易分润总金额
  222. NonQrDebitNotCapTradeAmt = TradeAmt; //(非云闪付)商户非封顶借记卡交易总额
  223. }
  224. if (sub.HelpFlag == 1)
  225. {
  226. if (sub.Money < 1)
  227. {
  228. HelpDebitNonCapTradeAmt = TradeAmt; //(扶持期)(非云闪付)借记卡非封顶交易总金额
  229. HelpDebitNonTradeCapProfit = ProfitAmt; //(扶持期)(非云闪付)借记卡非封顶交易分润总金额
  230. }
  231. HelpDebitProfitRate = ProfitRate; //(扶持期)(非云闪付)借记卡商户交易对应分润比例
  232. }
  233. else if (sub.HelpFlag >= 2)
  234. {
  235. if (sub.Money < 1)
  236. {
  237. ProfitDebitNonCapTradeAmt = TradeAmt; //(盈利期)(非云闪付)借记卡非封顶交易总金额
  238. ProfitDebitNonTradeCapProfit = ProfitAmt; //(盈利期)(非云闪付)借记卡非封顶交易分润总金额
  239. }
  240. ProfitDebitProfitRate = ProfitRate; //(盈利期)(非云闪付)借记卡商户交易对应分润比例
  241. }
  242. else
  243. {
  244. if (sub.Money < 1)
  245. {
  246. NotHelpDebitNonCapTradeAmt = TradeAmt; //(非扶持期)(非云闪付)借记卡非封顶交易总金额
  247. NotHelpDebitNonTradeCapProfit = ProfitAmt; //(非扶持期)(非云闪付)借记卡非封顶交易分润总金额
  248. }
  249. NotHelpDebitProfitRate = ProfitRate; //(非扶持期)(非云闪付)借记卡商户交易对应分润比例
  250. }
  251. }
  252. }
  253. db.ProfitRewardRecord.Add(new ProfitRewardRecord()
  254. {
  255. CreateDate = DateTime.Now,
  256. UpdateDate = DateTime.Now,
  257. UserId = sub.UserId, //创客
  258. MerchantId = sub.MerchantId, //商户
  259. BrandId = sub.BrandId, //品牌
  260. ProfitType = sub.DirectFlag, //创客分润类型
  261. TradeAmt = sub.TradeAmount, //商户交易总额
  262. TradeProfit = sub.Money, //交易分润
  263. TradeMonth = Month, //交易月
  264. RecordNo = RecordNo, //记录单号
  265. TopUserId = TopUserId, //顶级创客
  266. CreditTradeAmt = CreditTradeAmt, //贷记卡交易总金额
  267. QrCreditTradeAmt = QrCreditTradeAmt, //(云闪付)贷记卡交易总金额
  268. NonQrCreditTradeAmt = NonQrCreditTradeAmt, //(非云闪付)贷记卡交易总金额
  269. NotHelpCreditTradeAmt = NotHelpCreditTradeAmt, //(非扶持期)(非云闪付)贷记卡交易总金额
  270. HelpCreditTradeAmt = HelpCreditTradeAmt, //(扶持期)(非云闪付)贷记卡交易总金额
  271. DebitNonCapTradeAmt = DebitNonCapTradeAmt, //商户非封顶借记卡交易总额
  272. QrDebitNotCapTradeAmt = QrDebitNotCapTradeAmt, //(云闪付)商户非封顶借记卡交易总额
  273. NonQrDebitNotCapTradeAmt = NonQrDebitNotCapTradeAmt, //(非云闪付)商户非封顶借记卡交易总额
  274. NotHelpDebitNonCapTradeAmt = NotHelpDebitNonCapTradeAmt, //(非扶持期)(非云闪付)借记卡非封顶交易总金额
  275. HelpDebitNonCapTradeAmt = HelpDebitNonCapTradeAmt, //(扶持期)(非云闪付)借记卡非封顶交易总金额
  276. DebitCapTradeAmt = DebitCapTradeAmt, //商户借记卡封顶交易总额
  277. CreditTradeProfit = CreditTradeProfit, //贷记卡分润总金额
  278. QrCreditTradeProfit = QrCreditTradeProfit, //(云闪付)贷记卡交易分润
  279. NonQrCreditTradeProfit = NonQrCreditTradeProfit, //(非云闪付)贷记卡交易分润
  280. NotHelpCreditTradeProfit = NotHelpCreditTradeProfit, //(非扶持期)(非云闪付)贷记卡分润总金额
  281. HelpCreditTradeProfit = HelpCreditTradeProfit, //(扶持期)(非云闪付)贷记卡分润总金额
  282. DebitNonTradeCapProfit = DebitNonTradeCapProfit, //借记卡非封顶交易分润总金额
  283. QrDebitNonTradeCapProfit = QrDebitNonTradeCapProfit, //(云闪付)借记卡非封顶交易分润总金额
  284. NonQrDebitNonTradeCapProfit = NonQrDebitNonTradeCapProfit, //(非云闪付)借记卡非封顶交易分润总金额
  285. NotHelpDebitNonTradeCapProfit = NotHelpDebitNonTradeCapProfit, //(非扶持期)(非云闪付)借记卡非封顶交易分润总金额
  286. HelpDebitNonTradeCapProfit = HelpDebitNonTradeCapProfit, //(扶持期)(非云闪付)借记卡非封顶交易分润总金额
  287. DebitTradeCapProfit = DebitTradeCapProfit, //借记卡封顶交易分润总金额
  288. NotHelpCreditProfitRate = NotHelpCreditProfitRate, //(非扶持期)(非云闪付)贷记卡商户交易对应分润比例
  289. NotHelpCreditProfitStandardRate = NotHelpCreditProfitStandardRate, //(非扶持期)(非云闪付)贷记卡标准分润比例
  290. NotHelpCreditAddOrSubRate = NotHelpCreditAddOrSubRate, //(非扶持期)(非云闪付)贷记卡成本或增或减比例
  291. HelpCreditProfitRate = HelpCreditProfitRate, //(扶持期)(非云闪付)贷记卡商户交易对应分润比例
  292. HelpCreditProfitStandardRate = HelpCreditProfitStandardRate, //(扶持期)(非云闪付)贷记卡-标准分润比例
  293. HelpCreditAddOrSubRate = HelpCreditAddOrSubRate, //(扶持期)(非云闪付)贷记卡成本或增或减比例
  294. NotHelpDebitProfitRate = NotHelpDebitProfitRate, //(非扶持期)(非云闪付)借记卡商户交易对应分润比例
  295. NotHelpDebitProfitStandardRate = NotHelpDebitProfitStandardRate, //(非扶持期)(非云闪付)借记卡标准分润比例
  296. NotHelpDebitAddOrSubRate = NotHelpDebitAddOrSubRate, //(非扶持期)(非云闪付)借记卡成本或增或减比例
  297. HelpDebitProfitRate = HelpDebitProfitRate, //(扶持期)(非云闪付)借记卡商户交易对应分润比例
  298. HelpDebitProfitStandardRate = HelpDebitProfitStandardRate, //(扶持期)(非云闪付)借记卡-标准分润比例
  299. HelpDebitAddOrSubRate = HelpDebitAddOrSubRate, //(扶持期)(非云闪付)借记卡成本或增或减比例
  300. DebitCapSingleReward = DebitCapSingleReward, //借记卡合伙人直营封顶交易奖励金额
  301. QrCreditProfitRate = QrCreditProfitRate, //(云闪付)贷记卡交易分润比例
  302. QrCreditProfitStandardRate = QrCreditProfitStandardRate, //(云闪付)贷记卡分润基准
  303. QrCreditAddOrSubRate = QrCreditAddOrSubRate, //(云闪付)贷记卡分润比例或增或减
  304. QrDebitProfitRate = QrDebitProfitRate, //(云闪付)借记卡非封顶交易分润比例
  305. QrDebitProfitStandardRate = QrDebitProfitStandardRate, //(云闪付)借记卡非封顶交易分润基准
  306. QrDebitAddOrSubRate = QrDebitAddOrSubRate, //(云闪付)借记卡非封顶交易分润比例或增或减
  307. ProfitCreditTradeProfit = ProfitCreditTradeProfit, //(盈利期)(非云闪付)贷记卡分润总金额
  308. ProfitCreditProfitRate = ProfitCreditProfitRate, //(盈利期)(非云闪付)贷记卡商户交易对应分润比例
  309. ProfitCreditTradeAmt = ProfitCreditTradeAmt, //(盈利期)(非云闪付)贷记卡交易总金额
  310. ProfitCreditTradeProfit2 = ProfitCreditTradeProfit2, //(盈利期)(非云闪付)贷记卡分润总金额
  311. ProfitCreditProfitRate2 = ProfitCreditProfitRate2, //(盈利期)(非云闪付)贷记卡商户交易对应分润比例
  312. ProfitCreditTradeAmt2 = ProfitCreditTradeAmt2, //(盈利期)(非云闪付)贷记卡交易总金额
  313. ProfitDebitNonCapTradeAmt = ProfitDebitNonCapTradeAmt, //(盈利期)(非云闪付)借记卡非封顶交易总金额
  314. ProfitDebitNonTradeCapProfit = ProfitDebitNonTradeCapProfit, //(盈利期)(非云闪付)借记卡非封顶交易分润总金额
  315. ProfitDebitProfitRate = ProfitDebitProfitRate, //(盈利期)(非云闪付)借记卡商户交易对应分润比例
  316. });
  317. // db.SaveChanges();
  318. }
  319. }
  320. db.SaveChanges();
  321. PageNum += 1;
  322. }
  323. else
  324. {
  325. op = false;
  326. }
  327. }
  328. }
  329. }
  330. db.Dispose();
  331. }
  332. catch(Exception ex)
  333. {
  334. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "分润监控异常");
  335. }
  336. ProfitHelper.Instance.StatProfit();
  337. }
  338. public List<ProfitResult> StartProftForPosByDate(int BrandId, int BankCardType, string Month, int PageNum = 1)
  339. {
  340. // 级差
  341. /*
  342. K1 - K5:无大小市场要求,无押机分润比例为有押金机的70%,如K1的有押金机分润比例为万6,则无押机的分润比例为万4.2
  343. 机具激活大于90天,分润比例为原比例的70%
  344. if(is0y){
  345. 分润比例为原比例的70%
  346. }
  347. if(ActivityDate > now - 90){
  348. 分润比例为原比例的70%
  349. }
  350. K6 - K9:
  351. K6:至少有1个直推大于3000万,且总交易额减去3000万大于1200万,则获得分红奖(月月红)直接级差
  352. K7:总交易额减去最大交易额的直推,大于4000万,则获得分红奖(月月红)直接级差
  353. K8:总交易额减去最大交易额的直推,大于12000万,则获得分红奖(月月红)直接级差
  354. K9:总交易额减去最大交易额的直推,大于32000万,则获得分红奖(月月红)直接级差
  355. 补贴:
  356. 0押金无补贴
  357. 机具激活90天内,机具所属创客获得万10置顶
  358. 若创客为商户型创客,则机具UserId的创客获得万4分润
  359. //查分润
  360. select u.MakerCode,u.RealName,u.UserLevel,r.Rank,(case when ProfitType=1 then '直营' else '团队' end) as DirectFlag,
  361. k.Name,CreditTradeAmt,CreditTradeProfit,QrCreditTradeAmt,QrCreditTradeProfit,NonQrCreditTradeAmt,NonQrCreditTradeProfit,HelpCreditTradeAmt,HelpCreditTradeProfit,NotHelpCreditTradeAmt,NotHelpCreditTradeProfit,DebitTradeAmt,DebitTradeProfit from (
  362. select UserId,ProfitType,BrandId,
  363. sum(CreditTradeAmt) as CreditTradeAmt,
  364. sum(CreditTradeProfit) as CreditTradeProfit,
  365. sum(QrCreditTradeAmt) as QrCreditTradeAmt,
  366. sum(QrCreditTradeProfit) as QrCreditTradeProfit,
  367. sum(NonQrCreditTradeAmt) as NonQrCreditTradeAmt,
  368. sum(NonQrCreditTradeProfit) as NonQrCreditTradeProfit,
  369. sum(HelpCreditTradeAmt) as HelpCreditTradeAmt,
  370. sum(HelpCreditTradeProfit) as HelpCreditTradeProfit,
  371. sum(NotHelpCreditTradeAmt) as NotHelpCreditTradeAmt,
  372. sum(NotHelpCreditTradeProfit) as NotHelpCreditTradeProfit,
  373. sum(DebitNonCapTradeAmt+DebitCapTradeAmt) as DebitTradeAmt,
  374. sum(DebitNonTradeCapProfit+DebitTradeCapProfit) as DebitTradeProfit
  375. from ProfitRewardRecord where TradeMonth='202208'
  376. GROUP BY UserId,ProfitType,BrandId
  377. ) tb
  378. left join Users u on tb.UserId=u.Id
  379. LEFT JOIN KqProducts k ON k.Id=tb.BrandId
  380. left join UserRankWhite r on u.Id=r.Id and r.CreateDate<'2022-09-01 00:00:00' and r.UpdateDate>'2022-09-01 00:00:00'
  381. ORDER BY u.MakerCode
  382. //查补贴
  383. select u.MakerCode,u.RealName,u.UserLevel,r.Rank,(case when SubsidyType=1 then '直营' else '团队' end) as DirectFlag,
  384. k.Name,CreditTradeAmt,SubsidyProfit from (
  385. select SubsidyUserId,BrandId,SubsidyType,
  386. sum(CreditTradeAmt) as CreditTradeAmt,
  387. sum(SubsidyProfitRate) as SubsidyProfit
  388. from ProfitSubsidyDetail where TradeMonth='202206'
  389. GROUP BY SubsidyUserId,BrandId,SubsidyType
  390. ) tb
  391. left join Users u on tb.SubsidyUserId=u.Id
  392. LEFT JOIN KqProducts k ON k.Id=tb.BrandId
  393. left join UserRankWhite r on u.Id=r.Id
  394. // TODO:做到后台查询,换绑机器遗漏交易额补差
  395. select UserId,sum(TradeAmount) from TradeRecord where SnNo in (
  396. select PosSn from PosMachinesTwo where UserId=0 and BuyUserId=0 and CreditTrade>0
  397. ) and CreateDate>='2022-04-01 00:00:00' and CreateDate<'2022-05-01 00:00:00' and UserId>0 group by UserId
  398. */
  399. int PageSize = 200;
  400. int SkipNum = (PageNum - 1) * PageSize;
  401. string LimitString = " limit " + PageSize;
  402. if(PageNum > 1)
  403. {
  404. LimitString = " limit " + SkipNum + "," + PageSize;
  405. }
  406. List<ProfitResult> result = new List<ProfitResult>();
  407. ProfitObjects obj = RedisDbconn.Instance.Get<ProfitObjects>("pobj" + BrandId);
  408. if (obj.Status == 1) //判断分润是否开启
  409. {
  410. string sqlstr = "";
  411. if (BankCardType == 1)
  412. {
  413. sqlstr = "select UserId,QrPayFlag,HelpDirectTradeAmt,NotHelpDirectTradeAmt,ProfitDirectTradeAmt from CreditTradeDaySummary" + Month + " where BrandId=" + BrandId + LimitString;
  414. }
  415. if (BankCardType == 0)
  416. {
  417. sqlstr = "select UserId,QrPayFlag,HelpDirectDebitTradeAmt,NotHelpDirectDebitTradeAmt,HelpDirectDebitCapTradeAmt,NotHelpDirectDebitCapTradeAmt,HelpDirectDebitCapNum,NotHelpDirectDebitCapNum,ProfitDirectDebitTradeAmt,ProfitDirectDebitCapTradeAmt,ProfitDirectDebitCapNum from DebitTradeDaySummary" + Month + " where BrandId=" + BrandId + LimitString;
  418. }
  419. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + sqlstr, "分润监控日志");
  420. DataTable dt = GetDataTable(sqlstr);
  421. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + dt.Rows.Count.ToString(), "分润监控日志");
  422. if(dt.Rows.Count > 0)
  423. {
  424. foreach (DataRow dr in dt.Rows)
  425. {
  426. WebCMSEntities dbnew = new WebCMSEntities();
  427. try
  428. {
  429. int UserId = int.Parse(dr["UserId"].ToString());
  430. int QrPayFlag = int.Parse(dr["QrPayFlag"].ToString());
  431. List<UserLevelSet> levels = dbnew.UserLevelSet.ToList();
  432. SubUser selfUser = GetUser(UserId, Month) ?? new SubUser();
  433. int Days = int.Parse(RedisDbconn.Instance.Get<string>("pobjrule:" + BrandId + ":HelpPolicy:Days")); //天数
  434. if (BankCardType == 0)
  435. {
  436. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":借记卡", "分润监控日志");
  437. decimal HelpDirectDebitTradeAmt = decimal.Parse(dr[2].ToString()); //借记卡扶持期交易额
  438. decimal NotHelpDirectDebitTradeAmt = decimal.Parse(dr[3].ToString()); //借记卡稳定期交易额
  439. decimal ProfitDirectDebitTradeAmt = decimal.Parse(dr[8].ToString()); //借记卡扶持期交易额
  440. decimal HelpDirectDebitCapTradeAmt = decimal.Parse(dr[4].ToString()); //借记卡扶持期封顶交易额
  441. decimal NotHelpDirectDebitCapTradeAmt = decimal.Parse(dr[5].ToString()); //借记卡稳定期封顶交易额
  442. decimal ProfitDirectDebitCapTradeAmt = decimal.Parse(dr[9].ToString()); //借记卡扶持期封顶交易额
  443. int HelpDirectDebitCapNum = int.Parse(dr[6].ToString().Split('.')[0]); //借记卡扶持期封顶交易笔数
  444. int NotHelpDirectDebitCapNum = int.Parse(dr[7].ToString().Split('.')[0]); //借记卡稳定期封顶交易笔数
  445. int ProfitDirectDebitCapNum = int.Parse(dr[10].ToString().Split('.')[0]); //借记卡扶持期封顶交易笔数
  446. int UserLevel = selfUser.UserLevel; //当前会员等级
  447. decimal getLevelProfit = (HelpDirectDebitTradeAmt + NotHelpDirectDebitTradeAmt + ProfitDirectDebitTradeAmt - HelpDirectDebitCapTradeAmt - NotHelpDirectDebitCapTradeAmt - ProfitDirectDebitCapTradeAmt) * 0.0006M + HelpDirectDebitCapNum + NotHelpDirectDebitCapNum + ProfitDirectDebitCapNum;
  448. result.Add(new ProfitResult()
  449. {
  450. UserId = selfUser.Id,
  451. UserNav = selfUser.ParentNav,
  452. Money = PublicFunction.NumberFormat(getLevelProfit),
  453. ProfitRate = 0.0006M,
  454. Message = "储蓄卡分润",
  455. BankCardType = BankCardType,
  456. QrPayFlag = QrPayFlag,
  457. BrandId = BrandId,
  458. DirectFlag = 1,
  459. IsSubsidy = 0,
  460. TradeAmount = HelpDirectDebitTradeAmt + NotHelpDirectDebitTradeAmt + ProfitDirectDebitTradeAmt,
  461. });
  462. }
  463. else
  464. {
  465. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":贷记卡", "分润监控日志");
  466. decimal HelpDirectTradeAmt = decimal.Parse(dr[2].ToString()); //贷记卡扶持期交易额
  467. decimal NotHelpDirectTradeAmt = decimal.Parse(dr[3].ToString()); //贷记卡稳定期交易额
  468. decimal ProfitDirectTradeAmt = decimal.Parse(dr[4].ToString()); //贷记卡盈利期期交易额
  469. int maxLevel = obj.MaxLevel; //最大等级
  470. decimal diffLevelProfit = 0; //等级级差
  471. int curLevel = 0; //当前层级的会员等级
  472. string ParentNav = selfUser.ParentNav;
  473. if (string.IsNullOrEmpty(ParentNav))
  474. {
  475. ParentNav = ",0,";
  476. }
  477. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + ParentNav, "分润监控日志");
  478. if (!string.IsNullOrEmpty(ParentNav))
  479. {
  480. ParentNav += "," + UserId + ",";
  481. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  482. Array.Reverse(ParentNavList); //反转顺序
  483. int level = 0;
  484. DateTime now = DateTime.Now;
  485. DateTime ThisMonth = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00");
  486. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":扶持期:" + HelpDirectTradeAmt, "分润监控日志");
  487. if (HelpDirectTradeAmt > 0)
  488. {
  489. level = 0;
  490. curLevel = 0;
  491. diffLevelProfit = 0; //等级级差
  492. decimal checkProfit = HelpDirectTradeAmt * 0.0012M;
  493. decimal curProfit = 0;
  494. foreach (string UserIdString in ParentNavList)
  495. {
  496. level += 1;
  497. int uid = int.Parse(UserIdString);
  498. SubUser user = GetUser(uid, Month) ?? new SubUser();
  499. int UserLevel = user.PreUserLevel > user.UserLevel ? user.PreUserLevel : user.UserLevel; //当前会员等级
  500. if (curLevel == maxLevel) //判断当前创客是否有直推的激活机具,并且在活动时间内
  501. {
  502. break;
  503. }
  504. if (UserLevel <= maxLevel && UserLevel > curLevel)
  505. {
  506. int LevelKindId = GetLevelKindId(BrandId, 1, 1);
  507. ProfitObjectLevels objlevel = RedisDbconn.Instance.Get<ProfitObjectLevels>("pobjlv" + LevelKindId + ":" + UserLevel); //获取当前等级参数
  508. if (objlevel != null)
  509. {
  510. decimal getLevelProfit = 0; //等级分润
  511. if (objlevel.Percents > 0)
  512. {
  513. //获取创客分润规则,注册日开始算起,3个自然月内算扶持期
  514. getLevelProfit += HelpDirectTradeAmt * objlevel.Percents;
  515. }
  516. if (objlevel.AddProfitVal > 0)
  517. {
  518. getLevelProfit += objlevel.AddProfitVal;
  519. }
  520. decimal money = getLevelProfit;
  521. getLevelProfit -= diffLevelProfit;
  522. if (objlevel.LevelDiff == 1) //判断是否有级差
  523. {
  524. diffLevelProfit = money;
  525. }
  526. if (getLevelProfit >= obj.MinProfitVal)
  527. {
  528. int DirectFlag = selfUser.Id == user.Id ? 1 : 0;
  529. result.Add(new ProfitResult()
  530. {
  531. UserId = user.Id,
  532. UserNav = user.ParentNav,
  533. Money = PublicFunction.NumberFormat(getLevelProfit),
  534. ProfitRate = objlevel.Percents,
  535. Message = "交易分润",
  536. BankCardType = BankCardType,
  537. QrPayFlag = QrPayFlag,
  538. DirectFlag = DirectFlag,
  539. BrandId = BrandId,
  540. IsSubsidy = 0,
  541. TradeAmount = HelpDirectTradeAmt,
  542. HelpFlag = 1,
  543. });
  544. curProfit += getLevelProfit;
  545. function.WriteLog(user.Id + "-" + UserLevel + "-" + HelpDirectTradeAmt + "-" + DirectFlag + "-扶持期分润:" + getLevelProfit, "分润监控日志");
  546. }
  547. }
  548. }
  549. if(curLevel < UserLevel)
  550. {
  551. curLevel = UserLevel;
  552. }
  553. }
  554. if(curProfit > checkProfit)
  555. {
  556. decimal more = curProfit - checkProfit;
  557. function.WriteLog("超标了:" + more, "分润监控日志");
  558. }
  559. }
  560. function.WriteLog("------------------------", "分润监控日志");
  561. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":稳定期:" + NotHelpDirectTradeAmt, "分润监控日志");
  562. if (NotHelpDirectTradeAmt > 0)
  563. {
  564. level = 0;
  565. curLevel = 0;
  566. diffLevelProfit = 0; //等级级差
  567. foreach (string UserIdString in ParentNavList)
  568. {
  569. level += 1;
  570. int uid = int.Parse(UserIdString);
  571. SubUser user = GetUser(uid, Month) ?? new SubUser();
  572. int UserLevel = user.PreUserLevel > user.UserLevel ? user.PreUserLevel : user.UserLevel; //当前会员等级
  573. if (curLevel == maxLevel) //判断当前创客是否有直推的激活机具,并且在活动时间内
  574. {
  575. break;
  576. }
  577. if (UserLevel <= maxLevel && UserLevel > curLevel)
  578. {
  579. int LevelKindId = GetLevelKindId(BrandId, 0, 1);
  580. ProfitObjectLevels objlevel = RedisDbconn.Instance.Get<ProfitObjectLevels>("pobjlv" + LevelKindId + ":" + UserLevel); //获取当前等级参数
  581. if (objlevel != null)
  582. {
  583. decimal getLevelProfit = 0; //等级分润
  584. if (objlevel.Percents > 0)
  585. {
  586. //获取创客分润规则,注册日开始算起,3个自然月内算扶持期
  587. getLevelProfit += NotHelpDirectTradeAmt * objlevel.Percents;
  588. }
  589. if (objlevel.AddProfitVal > 0)
  590. {
  591. getLevelProfit += objlevel.AddProfitVal;
  592. }
  593. decimal money = getLevelProfit;
  594. getLevelProfit -= diffLevelProfit;
  595. if (objlevel.LevelDiff == 1) //判断是否有级差
  596. {
  597. diffLevelProfit = money;
  598. }
  599. if (getLevelProfit >= obj.MinProfitVal)
  600. {
  601. result.Add(new ProfitResult()
  602. {
  603. UserId = user.Id,
  604. UserNav = user.ParentNav,
  605. Money = PublicFunction.NumberFormat(getLevelProfit),
  606. ProfitRate = objlevel.Percents,
  607. Message = "交易分润",
  608. BankCardType = BankCardType,
  609. QrPayFlag = QrPayFlag,
  610. BrandId = BrandId,
  611. DirectFlag = selfUser.Id == user.Id ? 1 : 0,
  612. IsSubsidy = 0,
  613. TradeAmount = NotHelpDirectTradeAmt,
  614. HelpFlag = 0,
  615. });
  616. }
  617. }
  618. }
  619. if(curLevel < UserLevel)
  620. {
  621. curLevel = UserLevel;
  622. }
  623. }
  624. }
  625. function.WriteLog("------------------------", "分润监控日志");
  626. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":盈利期:" + ProfitDirectTradeAmt, "分润监控日志");
  627. if (ProfitDirectTradeAmt > 0)
  628. {
  629. level = 0;
  630. curLevel = 0;
  631. diffLevelProfit = 0; //等级级差
  632. foreach (string UserIdString in ParentNavList)
  633. {
  634. level += 1;
  635. int uid = int.Parse(UserIdString);
  636. SubUser user = GetUser(uid, Month) ?? new SubUser();
  637. int UserLevel = user.PreUserLevel > user.UserLevel ? user.PreUserLevel : user.UserLevel; //当前会员等级
  638. if (curLevel == maxLevel) //判断当前创客是否有直推的激活机具,并且在活动时间内
  639. {
  640. break;
  641. }
  642. if (UserLevel <= maxLevel && UserLevel > curLevel)
  643. {
  644. int LevelKindId = GetLevelKindId(BrandId, 2, 1);
  645. ProfitObjectLevels objlevel = RedisDbconn.Instance.Get<ProfitObjectLevels>("pobjlv" + LevelKindId + ":" + UserLevel); //获取当前等级参数
  646. if (objlevel != null)
  647. {
  648. decimal getLevelProfit = 0; //等级分润
  649. if (objlevel.Percents > 0)
  650. {
  651. getLevelProfit += ProfitDirectTradeAmt * objlevel.Percents;
  652. }
  653. if (objlevel.AddProfitVal > 0)
  654. {
  655. getLevelProfit += objlevel.AddProfitVal;
  656. }
  657. decimal money = getLevelProfit;
  658. getLevelProfit -= diffLevelProfit;
  659. if (objlevel.LevelDiff == 1) //判断是否有级差
  660. {
  661. diffLevelProfit = money;
  662. }
  663. if (getLevelProfit >= obj.MinProfitVal)
  664. {
  665. result.Add(new ProfitResult()
  666. {
  667. UserId = user.Id,
  668. UserNav = user.ParentNav,
  669. Money = PublicFunction.NumberFormat(getLevelProfit),
  670. ProfitRate = objlevel.Percents,
  671. Message = "交易分润",
  672. BankCardType = BankCardType,
  673. QrPayFlag = QrPayFlag,
  674. BrandId = BrandId,
  675. DirectFlag = selfUser.Id == user.Id ? 1 : 0,
  676. IsSubsidy = 0,
  677. TradeAmount = ProfitDirectTradeAmt,
  678. HelpFlag = 2,
  679. });
  680. }
  681. }
  682. }
  683. if(curLevel < UserLevel)
  684. {
  685. curLevel = UserLevel;
  686. }
  687. }
  688. }
  689. }
  690. }
  691. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":over", "分润监控日志");
  692. }
  693. catch (Exception ex)
  694. {
  695. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "每月统计分润异常");
  696. }
  697. dbnew.Dispose();
  698. }
  699. }
  700. sqlstr = "";
  701. if (BankCardType == 1)
  702. {
  703. sqlstr = "select UserId,QrPayFlag,ProfitDirectTradeAmt from CreditTradeDaySummary2" + Month + " where BrandId=" + BrandId + LimitString;
  704. }
  705. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + sqlstr, "分润监控日志");
  706. dt = GetDataTable(sqlstr);
  707. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + dt.Rows.Count.ToString(), "分润监控日志");
  708. if(dt.Rows.Count > 0)
  709. {
  710. foreach (DataRow dr in dt.Rows)
  711. {
  712. WebCMSEntities dbnew = new WebCMSEntities();
  713. try
  714. {
  715. int UserId = int.Parse(dr["UserId"].ToString());
  716. int QrPayFlag = int.Parse(dr["QrPayFlag"].ToString());
  717. List<UserLevelSet> levels = dbnew.UserLevelSet.ToList();
  718. SubUser selfUser = GetUser(UserId, Month) ?? new SubUser();
  719. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":贷记卡", "分润监控日志");
  720. decimal ProfitDirectTradeAmt = decimal.Parse(dr[2].ToString()); //贷记卡盈利期期交易额
  721. int maxLevel = obj.MaxLevel; //最大等级
  722. decimal diffLevelProfit = 0; //等级级差
  723. int curLevel = 0; //当前层级的会员等级
  724. string ParentNav = selfUser.ParentNav;
  725. if (string.IsNullOrEmpty(ParentNav))
  726. {
  727. ParentNav = ",0,";
  728. }
  729. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + ParentNav, "分润监控日志");
  730. if (!string.IsNullOrEmpty(ParentNav))
  731. {
  732. ParentNav += "," + UserId + ",";
  733. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  734. Array.Reverse(ParentNavList); //反转顺序
  735. int level = 0;
  736. DateTime now = DateTime.Now;
  737. DateTime ThisMonth = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00");
  738. function.WriteLog("------------------------", "分润监控日志");
  739. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":盈利期2:" + ProfitDirectTradeAmt, "分润监控日志");
  740. if (ProfitDirectTradeAmt > 0)
  741. {
  742. level = 0;
  743. curLevel = 0;
  744. diffLevelProfit = 0; //等级级差
  745. foreach (string UserIdString in ParentNavList)
  746. {
  747. level += 1;
  748. int uid = int.Parse(UserIdString);
  749. SubUser user = GetUser(uid, Month) ?? new SubUser();
  750. int UserLevel = user.PreUserLevel > user.UserLevel ? user.PreUserLevel : user.UserLevel; //当前会员等级
  751. if (curLevel == maxLevel) //判断当前创客是否有直推的激活机具,并且在活动时间内
  752. {
  753. break;
  754. }
  755. if (UserLevel <= maxLevel && UserLevel > curLevel)
  756. {
  757. int LevelKindId = GetLevelKindId(BrandId, 3, 1);
  758. ProfitObjectLevels objlevel = RedisDbconn.Instance.Get<ProfitObjectLevels>("pobjlv" + LevelKindId + ":" + UserLevel); //获取当前等级参数
  759. if (objlevel != null)
  760. {
  761. decimal getLevelProfit = 0; //等级分润
  762. if (objlevel.Percents > 0)
  763. {
  764. getLevelProfit += ProfitDirectTradeAmt * objlevel.Percents;
  765. }
  766. if (objlevel.AddProfitVal > 0)
  767. {
  768. getLevelProfit += objlevel.AddProfitVal;
  769. }
  770. decimal money = getLevelProfit;
  771. getLevelProfit -= diffLevelProfit;
  772. if (objlevel.LevelDiff == 1) //判断是否有级差
  773. {
  774. diffLevelProfit = money;
  775. }
  776. if (getLevelProfit >= obj.MinProfitVal)
  777. {
  778. result.Add(new ProfitResult()
  779. {
  780. UserId = user.Id,
  781. UserNav = user.ParentNav,
  782. Money = PublicFunction.NumberFormat(getLevelProfit),
  783. ProfitRate = objlevel.Percents,
  784. Message = "交易分润",
  785. BankCardType = BankCardType,
  786. QrPayFlag = QrPayFlag,
  787. BrandId = BrandId,
  788. DirectFlag = selfUser.Id == user.Id ? 1 : 0,
  789. IsSubsidy = 0,
  790. TradeAmount = ProfitDirectTradeAmt,
  791. HelpFlag = 3,
  792. });
  793. }
  794. }
  795. }
  796. if(curLevel < UserLevel)
  797. {
  798. curLevel = UserLevel;
  799. }
  800. }
  801. }
  802. }
  803. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":over", "分润监控日志");
  804. }
  805. catch (Exception ex)
  806. {
  807. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "每月统计分润异常");
  808. }
  809. dbnew.Dispose();
  810. }
  811. }
  812. }
  813. return result;
  814. }
  815. public List<ProfitResult> StartProftForSimByDate(string Month, int BrandId = 14, int PageNum = 1)
  816. {
  817. int PageSize = 200;
  818. int SkipNum = (PageNum - 1) * PageSize;
  819. string LimitString = " limit " + PageSize;
  820. if(PageNum > 1)
  821. {
  822. LimitString = " limit " + SkipNum + "," + PageSize;
  823. }
  824. List<ProfitResult> result = new List<ProfitResult>();
  825. ProfitObjects obj = RedisDbconn.Instance.Get<ProfitObjects>("pobj" + BrandId);
  826. if (obj.Status == 1) //判断分润是否开启
  827. {
  828. string sqlstr = "select UserId,sum(HelpTradeAmount),sum(NotHelpTradeAmount) from SimCardDaySummary where TradeMonth='" + Month + "' and SeoTitle='self'" + LimitString;
  829. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + sqlstr, "广电卡分润监控日志");
  830. DataTable dt = GetDataTable(sqlstr);
  831. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + dt.Rows.Count.ToString(), "广电卡分润监控日志");
  832. if(dt.Rows.Count > 0)
  833. {
  834. foreach (DataRow dr in dt.Rows)
  835. {
  836. WebCMSEntities dbnew = new WebCMSEntities();
  837. try
  838. {
  839. int UserId = int.Parse(dr["UserId"].ToString());
  840. List<UserLevelSet> levels = dbnew.UserLevelSet.ToList();
  841. SubUser selfUser = GetUser(UserId, Month) ?? new SubUser();
  842. int Days = int.Parse(RedisDbconn.Instance.Get<string>("pobjrule:" + BrandId + ":HelpPolicy:Days")); //天数
  843. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "", "广电卡分润监控日志");
  844. decimal HelpDirectTradeAmt = decimal.Parse(dr[1].ToString()); //扶持期交易额
  845. decimal NotHelpDirectTradeAmt = decimal.Parse(dr[2].ToString()); //稳定期交易额
  846. int maxLevel = obj.MaxLevel; //最大等级
  847. decimal diffLevelProfit = 0; //等级级差
  848. int curLevel = 0; //当前层级的会员等级
  849. string ParentNav = selfUser.ParentNav;
  850. if (string.IsNullOrEmpty(ParentNav))
  851. {
  852. ParentNav = ",0,";
  853. }
  854. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + ParentNav, "广电卡分润监控日志");
  855. if (!string.IsNullOrEmpty(ParentNav))
  856. {
  857. ParentNav += "," + UserId + ",";
  858. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  859. Array.Reverse(ParentNavList); //反转顺序
  860. int level = 0;
  861. DateTime now = DateTime.Now;
  862. DateTime ThisMonth = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00");
  863. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":扶持期:" + HelpDirectTradeAmt, "广电卡分润监控日志");
  864. if (HelpDirectTradeAmt > 0)
  865. {
  866. level = 0;
  867. curLevel = 0;
  868. diffLevelProfit = 0; //等级级差
  869. decimal curProfit = 0;
  870. foreach (string UserIdString in ParentNavList)
  871. {
  872. level += 1;
  873. int uid = int.Parse(UserIdString);
  874. SubUser user = GetUser(uid, Month) ?? new SubUser();
  875. int UserLevel = user.PreUserLevel > user.UserLevel ? user.PreUserLevel : user.UserLevel; //当前会员等级
  876. if (curLevel == maxLevel) //判断当前创客是否有直推的激活机具,并且在活动时间内
  877. {
  878. break;
  879. }
  880. if (UserLevel <= maxLevel && UserLevel > curLevel)
  881. {
  882. int LevelKindId = GetLevelKindId(BrandId, 1, 1);
  883. ProfitObjectLevels objlevel = RedisDbconn.Instance.Get<ProfitObjectLevels>("pobjlv" + LevelKindId + ":" + UserLevel); //获取当前等级参数
  884. if (objlevel != null)
  885. {
  886. decimal getLevelProfit = 0; //等级分润
  887. if (objlevel.Percents > 0)
  888. {
  889. //获取创客分润规则,注册日开始算起,3个自然月内算扶持期
  890. getLevelProfit += HelpDirectTradeAmt * objlevel.Percents;
  891. }
  892. if (objlevel.AddProfitVal > 0)
  893. {
  894. getLevelProfit += objlevel.AddProfitVal;
  895. }
  896. decimal money = getLevelProfit;
  897. getLevelProfit -= diffLevelProfit;
  898. if (objlevel.LevelDiff == 1) //判断是否有级差
  899. {
  900. diffLevelProfit = money;
  901. }
  902. if (getLevelProfit >= obj.MinProfitVal)
  903. {
  904. int DirectFlag = selfUser.Id == user.Id ? 1 : 0;
  905. result.Add(new ProfitResult()
  906. {
  907. UserId = user.Id,
  908. UserNav = user.ParentNav,
  909. Money = PublicFunction.NumberFormat(getLevelProfit),
  910. ProfitRate = objlevel.Percents,
  911. Message = "交易分润",
  912. BankCardType = 0,
  913. QrPayFlag = 0,
  914. DirectFlag = DirectFlag,
  915. BrandId = BrandId,
  916. IsSubsidy = 0,
  917. TradeAmount = HelpDirectTradeAmt,
  918. HelpFlag = 1,
  919. });
  920. curProfit += getLevelProfit;
  921. function.WriteLog(user.Id + "-" + UserLevel + "-" + HelpDirectTradeAmt + "-" + DirectFlag + "-扶持期分润:" + getLevelProfit, "广电卡分润监控日志");
  922. }
  923. }
  924. }
  925. if(curLevel < UserLevel)
  926. {
  927. curLevel = UserLevel;
  928. }
  929. }
  930. }
  931. function.WriteLog("------------------------", "广电卡分润监控日志");
  932. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":稳定期:" + NotHelpDirectTradeAmt, "广电卡分润监控日志");
  933. if (NotHelpDirectTradeAmt > 0)
  934. {
  935. level = 0;
  936. curLevel = 0;
  937. diffLevelProfit = 0; //等级级差
  938. foreach (string UserIdString in ParentNavList)
  939. {
  940. level += 1;
  941. int uid = int.Parse(UserIdString);
  942. SubUser user = GetUser(uid, Month) ?? new SubUser();
  943. int UserLevel = user.PreUserLevel > user.UserLevel ? user.PreUserLevel : user.UserLevel; //当前会员等级
  944. if (curLevel == maxLevel) //判断当前创客是否有直推的激活机具,并且在活动时间内
  945. {
  946. break;
  947. }
  948. if (UserLevel <= maxLevel && UserLevel > curLevel)
  949. {
  950. int LevelKindId = GetLevelKindId(BrandId, 0, 1);
  951. ProfitObjectLevels objlevel = RedisDbconn.Instance.Get<ProfitObjectLevels>("pobjlv" + LevelKindId + ":" + UserLevel); //获取当前等级参数
  952. if (objlevel != null)
  953. {
  954. decimal getLevelProfit = 0; //等级分润
  955. if (objlevel.Percents > 0)
  956. {
  957. //获取创客分润规则,注册日开始算起,3个自然月内算扶持期
  958. getLevelProfit += NotHelpDirectTradeAmt * objlevel.Percents;
  959. }
  960. if (objlevel.AddProfitVal > 0)
  961. {
  962. getLevelProfit += objlevel.AddProfitVal;
  963. }
  964. decimal money = getLevelProfit;
  965. getLevelProfit -= diffLevelProfit;
  966. if (objlevel.LevelDiff == 1) //判断是否有级差
  967. {
  968. diffLevelProfit = money;
  969. }
  970. if (getLevelProfit >= obj.MinProfitVal)
  971. {
  972. result.Add(new ProfitResult()
  973. {
  974. UserId = user.Id,
  975. UserNav = user.ParentNav,
  976. Money = PublicFunction.NumberFormat(getLevelProfit),
  977. ProfitRate = objlevel.Percents,
  978. Message = "交易分润",
  979. BankCardType = 0,
  980. QrPayFlag = 0,
  981. BrandId = BrandId,
  982. DirectFlag = selfUser.Id == user.Id ? 1 : 0,
  983. IsSubsidy = 0,
  984. TradeAmount = NotHelpDirectTradeAmt,
  985. HelpFlag = 0,
  986. });
  987. }
  988. }
  989. }
  990. if(curLevel < UserLevel)
  991. {
  992. curLevel = UserLevel;
  993. }
  994. }
  995. }
  996. function.WriteLog("------------------------", "广电卡分润监控日志");
  997. }
  998. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":over", "广电卡分润监控日志");
  999. }
  1000. catch (Exception ex)
  1001. {
  1002. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "广电卡分润异常");
  1003. }
  1004. dbnew.Dispose();
  1005. }
  1006. }
  1007. }
  1008. return result;
  1009. }
  1010. #endregion
  1011. public SubUser GetUser(int UserId, string Month)
  1012. {
  1013. SubUser user = new SubUser();
  1014. DataTable dt = GetDataTable("select * from Users" + Month + " where Id=" + UserId);
  1015. if(dt.Rows.Count > 0)
  1016. {
  1017. user.Id = int.Parse(dt.Rows[0]["Id"].ToString());
  1018. user.ParentUserId = int.Parse(dt.Rows[0]["ParentUserId"].ToString());
  1019. user.ParentNav = dt.Rows[0]["ParentNav"].ToString();
  1020. user.UserLevel = int.Parse(dt.Rows[0]["UserLevel"].ToString());
  1021. user.PreUserLevel = int.Parse(dt.Rows[0]["PreUserLevel"].ToString());
  1022. }
  1023. return user;
  1024. }
  1025. public DataTable GetDataTable(string sqlstr)
  1026. {
  1027. DataTable dt = CustomerSqlConn.dtable(sqlstr, Library.ConfigurationManager.AppSettings["MainSqlConnStr"].ToString());
  1028. return dt;
  1029. }
  1030. }
  1031. }