ProfitHelperV2.cs 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. DateTime end = DateTime.Parse("2022-06-30 00:00:00");
  34. DateTime check = DateTime.Parse("2022-06-01");
  35. while (check <= end)
  36. {
  37. StatProfitEveryDay(check.ToString("yyyyMMdd"));
  38. check = check.AddDays(1);
  39. }
  40. }
  41. public void StatProfitEveryDay(object sender)
  42. {
  43. string Date = sender.ToString();
  44. string Month = Date.Substring(0, 6);
  45. string check = function.ReadInstance("/ProfitStat/" + Date + ".txt");
  46. if (!string.IsNullOrEmpty(check))
  47. {
  48. return;
  49. }
  50. function.WritePage("/ProfitStat/", Date + ".txt", DateTime.Now.ToString());
  51. WebCMSEntities db = new WebCMSEntities();
  52. for (int i = 1; i <= 7; i++) //品牌
  53. {
  54. for (int j = 0; j <= 1; j++) //卡类型
  55. {
  56. int PageNum = 1;
  57. bool op = true;
  58. while(op)
  59. {
  60. function.WriteLog("BrandId:" + i + ",BankCardType:" + j + ",Date:" + Date + ",PageNum:" + PageNum, "分润监控日志");
  61. List<ProfitResult> result = StartProftForPosByDate(i, j, Date, PageNum);
  62. function.WriteLog("result:" + result.Count, "分润监控日志");
  63. function.WriteLog("\r\n", "分润监控日志");
  64. if(result.Count > 0)
  65. {
  66. foreach (ProfitResult sub in result)
  67. {
  68. int TopUserId = 0;
  69. if (!string.IsNullOrEmpty(sub.UserNav))
  70. {
  71. string[] UserNavList = sub.UserNav.Trim(',').Replace(",,", ",").Split(',');
  72. if (UserNavList.Length > 1)
  73. {
  74. TopUserId = int.Parse(UserNavList[1]);
  75. }
  76. else
  77. {
  78. TopUserId = int.Parse(UserNavList[0]);
  79. }
  80. }
  81. string RecordNo = "P" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  82. decimal ProfitAmt = sub.Money;
  83. decimal TradeAmt = sub.TradeAmount;
  84. decimal ProfitRate = sub.ProfitRate;
  85. if (sub.IsSubsidy == 1 && j == 1)
  86. {
  87. decimal CreditTradeAmt = TradeAmt; //商户贷记卡交易总金额
  88. decimal QrCreditTradeAmt = 0; //商户(云闪付)贷记卡交易总金额
  89. decimal NonQrCreditTradeAmt = 0; //商户(非云闪付)贷记卡交易总金额
  90. decimal CreditProfitRate = 0; //(非云闪付)贷记卡交易分润比例
  91. decimal SubsidyProfit = 0; //商户(非云闪付)贷记卡交易分润补贴金额
  92. if (sub.QrPayFlag == 1)
  93. {
  94. QrCreditTradeAmt = TradeAmt; //商户(云闪付)贷记卡交易总金额
  95. }
  96. else
  97. {
  98. NonQrCreditTradeAmt = TradeAmt; //商户(非云闪付)贷记卡交易总金额
  99. CreditProfitRate = ProfitRate; //(非云闪付)贷记卡交易分润比例
  100. SubsidyProfit = ProfitAmt; //商户(非云闪付)贷记卡交易分润补贴金额
  101. }
  102. MachineForSnNo forSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == sub.PosSn) ?? new MachineForSnNo();
  103. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forSnNo.SnId) ?? new PosMachinesTwo();
  104. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == sub.MerchantId) ?? new PosMerchantInfo();
  105. db.ProfitSubsidyDetail.Add(new ProfitSubsidyDetail()
  106. {
  107. CreateDate = DateTime.Now,
  108. UpdateDate = DateTime.Now,
  109. RecordNo = RecordNo, //流水号
  110. BrandId = sub.BrandId, //品牌
  111. ProductName = RelationClass.GetKqProductBrandInfo(sub.BrandId), //产品名称
  112. TradeMonth = Month, //交易月份
  113. SeoTitle = Date,
  114. MerchantId = sub.MerchantId, //商户
  115. MerNo = merchant.KqMerNo, //渠道商户编号
  116. SnNo = sub.PosSn, //渠道SN号
  117. MerRegTime = pos.BindingTime, //渠道注册时间
  118. MerStandardDate = merchant.MerStandardDate, //商户激活时间
  119. BelongUserId = merchant.UserId, //商户归属人
  120. MerHelpFlag = sub.HelpFlag == 1 ? 1u : 0u, //是否属于扶持周期内商户
  121. SubsidyUserId = sub.UserId, //补贴人
  122. SubsidyType = sub.DirectFlag, //分润补贴类型
  123. SubsidyProfitRate = sub.Money, //创客活动期内直营商户分润万分之十
  124. CreditTradeAmt = CreditTradeAmt, //商户贷记卡交易总金额
  125. QrCreditTradeAmt = QrCreditTradeAmt, //商户(云闪付)贷记卡交易总金额
  126. NonQrCreditTradeAmt = NonQrCreditTradeAmt, //商户(非云闪付)贷记卡交易总金额
  127. CreditProfitRate = CreditProfitRate, //(非云闪付)贷记卡交易分润比例
  128. SubsidyProfit = SubsidyProfit, //商户(非云闪付)贷记卡交易分润补贴金额
  129. });
  130. db.SaveChanges();
  131. }
  132. else
  133. {
  134. decimal CreditTradeAmt = 0;
  135. decimal CreditTradeProfit = 0;
  136. decimal QrCreditTradeAmt = 0;
  137. decimal QrCreditProfitRate = 0;
  138. decimal QrCreditProfitStandardRate = 0;
  139. decimal QrCreditAddOrSubRate = 0;
  140. decimal QrCreditTradeProfit = 0;
  141. decimal NonQrCreditTradeAmt = 0;
  142. decimal NotHelpCreditTradeAmt = 0;
  143. decimal NotHelpCreditTradeProfit = 0;
  144. decimal NotHelpCreditProfitRate = 0;
  145. decimal NotHelpCreditProfitStandardRate = 0;
  146. decimal NotHelpCreditAddOrSubRate = 0;
  147. decimal HelpCreditTradeAmt = 0;
  148. decimal NonQrCreditTradeProfit = 0;
  149. decimal HelpCreditTradeProfit = 0;
  150. decimal HelpCreditProfitRate = 0;
  151. decimal HelpCreditProfitStandardRate = 0;
  152. decimal HelpCreditAddOrSubRate = 0;
  153. decimal DebitNonCapTradeAmt = 0;
  154. decimal DebitCapTradeAmt = 0;
  155. decimal QrDebitNotCapTradeAmt = 0;
  156. decimal NonQrDebitNotCapTradeAmt = 0;
  157. decimal NotHelpDebitNonCapTradeAmt = 0;
  158. decimal HelpDebitNonCapTradeAmt = 0;
  159. decimal DebitNonTradeCapProfit = 0;
  160. decimal QrDebitNonTradeCapProfit = 0;
  161. decimal NonQrDebitNonTradeCapProfit = 0;
  162. decimal NotHelpDebitNonTradeCapProfit = 0;
  163. decimal HelpDebitNonTradeCapProfit = 0;
  164. decimal DebitTradeCapProfit = 0;
  165. decimal NotHelpDebitProfitRate = 0;
  166. decimal NotHelpDebitProfitStandardRate = 0;
  167. decimal NotHelpDebitAddOrSubRate = 0;
  168. decimal HelpDebitProfitRate = 0;
  169. decimal HelpDebitProfitStandardRate = 0;
  170. decimal HelpDebitAddOrSubRate = 0;
  171. decimal DebitCapSingleReward = 0;
  172. decimal QrDebitProfitRate = 0;
  173. decimal QrDebitProfitStandardRate = 0;
  174. decimal QrDebitAddOrSubRate = 0;
  175. if (j == 1)
  176. {
  177. CreditTradeAmt = TradeAmt; //贷记卡交易总金额
  178. CreditTradeProfit = ProfitAmt; //贷记卡分润总金额
  179. if (sub.QrPayFlag == 1)
  180. {
  181. QrCreditTradeAmt = TradeAmt; //(云闪付)贷记卡交易总金额
  182. QrCreditProfitRate = ProfitRate; //(云闪付)贷记卡交易分润比例
  183. QrCreditTradeProfit = ProfitAmt; //(云闪付)贷记卡交易分润
  184. }
  185. else
  186. {
  187. NonQrCreditTradeAmt = TradeAmt; //(非云闪付)贷记卡交易总金额
  188. NonQrCreditTradeProfit = ProfitAmt; //(非云闪付)贷记卡交易分润
  189. if(sub.HelpFlag == 1)
  190. {
  191. HelpCreditTradeProfit = ProfitAmt; //(扶持期)(非云闪付)贷记卡分润总金额
  192. HelpCreditProfitRate = ProfitRate; //(扶持期)(非云闪付)贷记卡商户交易对应分润比例
  193. HelpCreditTradeAmt = TradeAmt; //(扶持期)(非云闪付)贷记卡交易总金额
  194. }
  195. else
  196. {
  197. NotHelpCreditTradeAmt = TradeAmt; //(非扶持期)(非云闪付)贷记卡交易总金额
  198. NotHelpCreditTradeProfit = ProfitAmt; //(非扶持期)(非云闪付)贷记卡分润总金额
  199. NotHelpCreditProfitRate = ProfitRate; //(非扶持期)(非云闪付)贷记卡商户交易对应分润比例
  200. }
  201. }
  202. }
  203. if (j == 0)
  204. {
  205. if (sub.Money < 1)
  206. {
  207. DebitNonCapTradeAmt = TradeAmt; //商户非封顶借记卡交易总额
  208. DebitNonTradeCapProfit = ProfitAmt; //借记卡非封顶交易分润总金额
  209. }
  210. else
  211. {
  212. DebitCapTradeAmt = TradeAmt; //商户借记卡封顶交易总额
  213. DebitCapSingleReward = ProfitAmt; //借记卡合伙人直营封顶交易奖励金额
  214. DebitTradeCapProfit = ProfitAmt; //借记卡封顶交易分润总金额
  215. }
  216. if (sub.QrPayFlag == 1)
  217. {
  218. if (sub.Money < 1)
  219. {
  220. QrDebitNotCapTradeAmt = TradeAmt; //(云闪付)商户非封顶借记卡交易总额
  221. QrDebitProfitRate = ProfitRate; //(云闪付)借记卡非封顶交易分润比例
  222. QrDebitNonTradeCapProfit = ProfitAmt; //(云闪付)借记卡非封顶交易分润总金额
  223. }
  224. }
  225. else
  226. {
  227. if (sub.Money < 1)
  228. {
  229. NonQrDebitNonTradeCapProfit = ProfitAmt; //(非云闪付)借记卡非封顶交易分润总金额
  230. NonQrDebitNotCapTradeAmt = TradeAmt; //(非云闪付)商户非封顶借记卡交易总额
  231. }
  232. if (sub.HelpFlag == 1)
  233. {
  234. if (sub.Money < 1)
  235. {
  236. HelpDebitNonCapTradeAmt = TradeAmt; //(扶持期)(非云闪付)借记卡非封顶交易总金额
  237. HelpDebitNonTradeCapProfit = ProfitAmt; //(扶持期)(非云闪付)借记卡非封顶交易分润总金额
  238. }
  239. HelpDebitProfitRate = ProfitRate; //(扶持期)(非云闪付)借记卡商户交易对应分润比例
  240. }
  241. else
  242. {
  243. if (sub.Money < 1)
  244. {
  245. NotHelpDebitNonCapTradeAmt = TradeAmt; //(非扶持期)(非云闪付)借记卡非封顶交易总金额
  246. NotHelpDebitNonTradeCapProfit = ProfitAmt; //(非扶持期)(非云闪付)借记卡非封顶交易分润总金额
  247. }
  248. NotHelpDebitProfitRate = ProfitRate; //(非扶持期)(非云闪付)借记卡商户交易对应分润比例
  249. }
  250. }
  251. }
  252. db.ProfitRewardRecord.Add(new ProfitRewardRecord()
  253. {
  254. CreateDate = DateTime.Now,
  255. UpdateDate = DateTime.Now,
  256. UserId = sub.UserId, //创客
  257. MerchantId = sub.MerchantId, //商户
  258. BrandId = sub.BrandId, //品牌
  259. ProfitType = sub.DirectFlag, //创客分润类型
  260. TradeAmt = sub.TradeAmount, //商户交易总额
  261. TradeProfit = sub.Money, //交易分润
  262. TradeMonth = Month, //交易月
  263. SeoTitle = Date,
  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. });
  308. db.SaveChanges();
  309. }
  310. }
  311. PageNum += 1;
  312. }
  313. else
  314. {
  315. op = false;
  316. }
  317. }
  318. }
  319. }
  320. db.Dispose();
  321. }
  322. public List<ProfitResult> StartProftForPosByDate(int BrandId, int BankCardType, string Date, int PageNum = 1)
  323. {
  324. // 级差
  325. /*
  326. K1 - K5:无大小市场要求,无押机分润比例为有押金机的70%,如K1的有押金机分润比例为万6,则无押机的分润比例为万4.2
  327. 机具激活大于90天,分润比例为原比例的70%
  328. if(is0y){
  329. 分润比例为原比例的70%
  330. }
  331. if(ActivityDate > now - 90){
  332. 分润比例为原比例的70%
  333. }
  334. K6 - K9:
  335. K6:至少有1个直推大于3000万,且总交易额减去3000万大于1200万,则获得分红奖(月月红)直接级差
  336. K7:总交易额减去最大交易额的直推,大于4000万,则获得分红奖(月月红)直接级差
  337. K8:总交易额减去最大交易额的直推,大于12000万,则获得分红奖(月月红)直接级差
  338. K9:总交易额减去最大交易额的直推,大于32000万,则获得分红奖(月月红)直接级差
  339. 补贴:
  340. 0押金无补贴
  341. 机具激活90天内,机具所属创客获得万10置顶
  342. 若创客为商户型创客,则机具UserId的创客获得万4分润
  343. //查分润
  344. select u.MakerCode,u.RealName,u.UserLevel,r.Rank,pos.SeoKeyword,p.KqSnNo,(case when ProfitType=1 then '直营' else '团队' end) as DirectFlag,
  345. k.Name,CreditTradeAmt,CreditTradeProfit,DebitTradeAmt,DebitTradeProfit,QrCreditTradeAmt,QrCreditTradeProfit from (
  346. select UserId,ProfitType,BrandId,MerchantId,
  347. sum(CreditTradeAmt) as CreditTradeAmt,
  348. sum(CreditTradeProfit) as CreditTradeProfit,
  349. sum(DebitNonCapTradeAmt+DebitCapTradeAmt) as DebitTradeAmt,
  350. sum(DebitNonTradeCapProfit+DebitTradeCapProfit) as DebitTradeProfit,
  351. sum(QrCreditTradeAmt) as QrCreditTradeAmt,
  352. sum(QrCreditTradeProfit) as QrCreditTradeProfit
  353. from ProfitRewardRecord where MerchantId>0
  354. GROUP BY UserId,ProfitType,BrandId,MerchantId
  355. ) tb
  356. left join Users u on tb.UserId=u.Id
  357. LEFT JOIN KqProducts k ON k.Id=tb.BrandId
  358. left join PosMerchantInfo p on tb.MerchantId=p.Id
  359. left join MachineForSnNo fp on fp.SnNo=p.KqSnNo
  360. left join PosMachinesTwo pos on pos.Id=fp.SnId
  361. left join UserRankWhite r on u.Id=r.Id
  362. ORDER BY p.KqSnNo,u.MakerCode
  363. //查补贴
  364. select u.MakerCode,u.RealName,u.UserLevel,r.Rank,m.KqSnNo,m.MerchantName,(case when SubsidyType=1 then '直营' else '团队' end) as DirectFlag,
  365. k.Name,CreditTradeAmt,SubsidyProfit from (
  366. select SubsidyUserId,MerchantId,BrandId,SubsidyType,
  367. sum(CreditTradeAmt) as CreditTradeAmt,
  368. sum(SubsidyProfitRate) as SubsidyProfit
  369. from ProfitSubsidyDetail
  370. GROUP BY SubsidyUserId,MerchantId,BrandId,SubsidyType
  371. ) tb
  372. left join Users u on tb.SubsidyUserId=u.Id
  373. LEFT JOIN KqProducts k ON k.Id=tb.BrandId
  374. left join PosMerchantInfo m on tb.MerchantId=m.Id
  375. left join UserRankWhite r on u.Id=r.Id
  376. // TODO:做到后台查询,换绑机器遗漏交易额补差
  377. select UserId,sum(TradeAmount) from TradeRecord where SnNo in (
  378. select PosSn from PosMachinesTwo where UserId=0 and BuyUserId=0 and CreditTrade>0
  379. ) and CreateDate>='2022-04-01 00:00:00' and CreateDate<'2022-05-01 00:00:00' and UserId>0 group by UserId
  380. */
  381. int PageSize = 20;
  382. int SkipNum = (PageNum - 1) * PageSize;
  383. string LimitString = " limit " + PageSize;
  384. if(PageNum > 1)
  385. {
  386. LimitString = " limit " + SkipNum + "," + PageSize;
  387. }
  388. List<ProfitResult> result = new List<ProfitResult>();
  389. ProfitObjects obj = RedisDbconn.Instance.Get<ProfitObjects>("pobj" + BrandId);
  390. if (obj.Status == 1) //判断分润是否开启
  391. {
  392. string sqlstr = "";
  393. if (BankCardType == 1)
  394. {
  395. sqlstr = "select UserId,QueryCount,sum(HelpDirectTradeAmt),sum(NotHelpDirectTradeAmt) from TradeDaySummary where TradeDate='" + Date + "' and SeoTitle='self' and BrandId=" + BrandId + " group by UserId,QueryCount having sum(HelpDirectTradeAmt)>0 or sum(NotHelpDirectTradeAmt)>0 order by UserId" + LimitString;
  396. }
  397. if (BankCardType == 0)
  398. {
  399. sqlstr = "select UserId,QueryCount,sum(HelpDirectDebitTradeAmt),sum(NotHelpDirectDebitTradeAmt),sum(HelpDirectDebitCapTradeAmt),sum(NotHelpDirectDebitCapTradeAmt),sum(HelpDirectDebitCapNum),sum(NotHelpDirectDebitCapNum) from TradeDaySummary where TradeDate='" + Date + "' and SeoTitle='self' and BrandId=" + BrandId + " group by UserId,QueryCount having sum(HelpDirectDebitTradeAmt)>0 or sum(NotHelpDirectDebitTradeAmt)>0 or sum(HelpDirectDebitCapTradeAmt)>0 or sum(NotHelpDirectDebitCapTradeAmt)>0 order by UserId" + LimitString;
  400. }
  401. function.WriteLog(sqlstr, "分润监控日志");
  402. DataTable dt = dbconn.dtable(sqlstr);
  403. function.WriteLog(dt.Rows.Count.ToString(), "分润监控日志");
  404. if(dt.Rows.Count > 0)
  405. {
  406. foreach (DataRow dr in dt.Rows)
  407. {
  408. WebCMSEntities dbnew = new WebCMSEntities();
  409. try
  410. {
  411. int UserId = int.Parse(dr["UserId"].ToString());
  412. int QrPayFlag = int.Parse(dr["QueryCount"].ToString());
  413. List<UserLevelSet> levels = dbnew.UserLevelSet.ToList();
  414. Users selfUser = dbnew.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  415. int Days = int.Parse(RedisDbconn.Instance.Get<string>("pobjrule:" + BrandId + ":HelpPolicy:Days")); //天数
  416. if (BankCardType == 0)
  417. {
  418. decimal HelpDirectDebitTradeAmt = decimal.Parse(dr[2].ToString()); //借记卡扶持期交易额
  419. decimal NotHelpDirectDebitTradeAmt = decimal.Parse(dr[3].ToString()); //借记卡稳定期交易额
  420. decimal HelpDirectDebitCapTradeAmt = decimal.Parse(dr[4].ToString()); //借记卡扶持期封顶交易额
  421. decimal NotHelpDirectDebitCapTradeAmt = decimal.Parse(dr[5].ToString()); //借记卡稳定期封顶交易额
  422. int HelpDirectDebitCapNum = int.Parse(dr[6].ToString().Split('.')[0]); //借记卡扶持期封顶交易笔数
  423. int NotHelpDirectDebitCapNum = int.Parse(dr[7].ToString()); //借记卡稳定期封顶交易笔数
  424. int UserLevel = selfUser.UserLevel; //当前会员等级
  425. // int LevelKindId = GetLevelKindId(BrandId, HelpTime, 0);
  426. // ProfitObjectLevels objlevel = RedisDbconn.Instance.Get<ProfitObjectLevels>("pobjlv" + LevelKindId + ":" + UserLevel); //获取当前等级参数
  427. decimal getLevelProfit = (HelpDirectDebitTradeAmt + NotHelpDirectDebitTradeAmt - HelpDirectDebitCapTradeAmt - NotHelpDirectDebitCapTradeAmt) * 0.0006M + HelpDirectDebitCapNum + NotHelpDirectDebitCapNum;
  428. result.Add(new ProfitResult()
  429. {
  430. UserId = selfUser.Id,
  431. UserNav = selfUser.ParentNav,
  432. Money = PublicFunction.NumberFormat(getLevelProfit),
  433. ProfitRate = 0.0006M,
  434. Message = "储蓄卡分润",
  435. BankCardType = BankCardType,
  436. QrPayFlag = QrPayFlag,
  437. BrandId = BrandId,
  438. DirectFlag = 1,
  439. IsSubsidy = 0,
  440. TradeAmount = HelpDirectDebitTradeAmt + NotHelpDirectDebitTradeAmt,
  441. });
  442. }
  443. else
  444. {
  445. decimal HelpDirectTradeAmt = decimal.Parse(dr[2].ToString()); //贷记卡扶持期交易额
  446. decimal NotHelpDirectTradeAmt = decimal.Parse(dr[3].ToString()); //贷记卡稳定期交易额
  447. int maxLevel = obj.MaxLevel; //最大等级
  448. decimal diffLevelProfit = 0; //等级级差
  449. int curLevel = 0; //当前层级的会员等级
  450. string ParentNav = selfUser.ParentNav;
  451. if (string.IsNullOrEmpty(ParentNav))
  452. {
  453. ParentNav = ",0,";
  454. }
  455. function.WriteLog(ParentNav, "分润监控日志");
  456. if (!string.IsNullOrEmpty(ParentNav))
  457. {
  458. ParentNav += "," + UserId + ",";
  459. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  460. Array.Reverse(ParentNavList); //反转顺序
  461. int level = 0;
  462. DateTime now = DateTime.Now;
  463. if (HelpDirectTradeAmt > 0)
  464. {
  465. foreach (string UserIdString in ParentNavList)
  466. {
  467. level += 1;
  468. int uid = int.Parse(UserIdString);
  469. Users user = dbnew.Users.FirstOrDefault(m => m.Id == uid) ?? new Users();
  470. int UserLevel = user.UserLevel; //当前会员等级
  471. UserRankWhite rank = dbnew.UserRankWhite.FirstOrDefault(m => m.Id == user.Id && m.UpdateDate > now);
  472. if (rank != null)
  473. {
  474. if (rank.Rank > UserLevel)
  475. {
  476. UserLevel = rank.Rank;
  477. }
  478. }
  479. if (curLevel == maxLevel) //判断当前创客是否有直推的激活机具,并且在活动时间内
  480. {
  481. break;
  482. }
  483. if (UserLevel <= maxLevel && UserLevel > curLevel)
  484. {
  485. int LevelKindId = GetLevelKindId(BrandId, 1, 1);
  486. ProfitObjectLevels objlevel = RedisDbconn.Instance.Get<ProfitObjectLevels>("pobjlv" + LevelKindId + ":" + UserLevel); //获取当前等级参数
  487. if (objlevel != null)
  488. {
  489. decimal getLevelProfit = 0; //等级分润
  490. if (objlevel.Percents > 0)
  491. {
  492. //获取创客分润规则,注册日开始算起,3个自然月内算扶持期
  493. getLevelProfit += HelpDirectTradeAmt * objlevel.Percents;
  494. }
  495. if (objlevel.AddProfitVal > 0)
  496. {
  497. getLevelProfit += objlevel.AddProfitVal;
  498. }
  499. decimal money = getLevelProfit;
  500. getLevelProfit -= diffLevelProfit;
  501. if (objlevel.LevelDiff == 1) //判断是否有级差
  502. {
  503. diffLevelProfit = money;
  504. }
  505. if (getLevelProfit >= obj.MinProfitVal)
  506. {
  507. result.Add(new ProfitResult()
  508. {
  509. UserId = user.Id,
  510. UserNav = user.ParentNav,
  511. Money = PublicFunction.NumberFormat(getLevelProfit),
  512. ProfitRate = objlevel.Percents,
  513. Message = "交易分润",
  514. BankCardType = BankCardType,
  515. QrPayFlag = QrPayFlag,
  516. DirectFlag = selfUser.Id == user.Id ? 1 : 0,
  517. BrandId = BrandId,
  518. IsSubsidy = 0,
  519. TradeAmount = HelpDirectTradeAmt,
  520. HelpFlag = 1,
  521. });
  522. }
  523. // 分润补贴
  524. if (level == 1)
  525. {
  526. int SubsidyUserId = selfUser.ParentUserId; // TODO: 确定是否100%是上级
  527. Users subuser = dbnew.Users.FirstOrDefault(m => m.Id == SubsidyUserId) ?? new Users();
  528. int subUserLevel = subuser.UserLevel;
  529. if (subUserLevel < 5)
  530. {
  531. UserRankWhite subrank = dbnew.UserRankWhite.FirstOrDefault(m => m.Id == subuser.Id && m.UpdateDate > now);
  532. if (subrank != null)
  533. {
  534. if (subrank.Rank > subUserLevel)
  535. {
  536. subUserLevel = subrank.Rank;
  537. }
  538. }
  539. if (subUserLevel < 5)
  540. {
  541. ProfitObjectLevels sublevel = RedisDbconn.Instance.Get<ProfitObjectLevels>("pobjlv" + LevelKindId + ":" + subUserLevel); //获取当前等级参数
  542. if (sublevel != null)
  543. {
  544. decimal otherPercent = 0.001M - sublevel.Percents;
  545. decimal otherMoney = HelpDirectTradeAmt * otherPercent;
  546. if (otherMoney >= obj.MinProfitVal)
  547. {
  548. result.Add(new ProfitResult()
  549. {
  550. UserId = subuser.Id,
  551. UserNav = subuser.ParentNav,
  552. Money = PublicFunction.NumberFormat(otherMoney),
  553. ProfitRate = otherPercent,
  554. Message = "分润补贴",
  555. BankCardType = BankCardType,
  556. QrPayFlag = QrPayFlag,
  557. DirectFlag = 1,
  558. BrandId = BrandId,
  559. IsSubsidy = 1,
  560. TradeAmount = HelpDirectTradeAmt,
  561. HelpFlag = 1,
  562. });
  563. }
  564. }
  565. }
  566. }
  567. }
  568. }
  569. }
  570. curLevel = UserLevel;
  571. }
  572. }
  573. if (NotHelpDirectTradeAmt > 0)
  574. {
  575. level = 0;
  576. curLevel = 0;
  577. foreach (string UserIdString in ParentNavList)
  578. {
  579. level += 1;
  580. int uid = int.Parse(UserIdString);
  581. Users user = dbnew.Users.FirstOrDefault(m => m.Id == uid) ?? new Users();
  582. int UserLevel = user.UserLevel; //当前会员等级
  583. UserRankWhite rank = dbnew.UserRankWhite.FirstOrDefault(m => m.Id == user.Id && m.UpdateDate > now);
  584. if (rank != null)
  585. {
  586. if (rank.Rank > UserLevel)
  587. {
  588. UserLevel = rank.Rank;
  589. }
  590. }
  591. if (curLevel == maxLevel) //判断当前创客是否有直推的激活机具,并且在活动时间内
  592. {
  593. break;
  594. }
  595. if (UserLevel <= maxLevel && UserLevel > curLevel)
  596. {
  597. int LevelKindId = GetLevelKindId(BrandId, 0, 1);
  598. ProfitObjectLevels objlevel = RedisDbconn.Instance.Get<ProfitObjectLevels>("pobjlv" + LevelKindId + ":" + UserLevel); //获取当前等级参数
  599. if (objlevel != null)
  600. {
  601. decimal getLevelProfit = 0; //等级分润
  602. if (objlevel.Percents > 0)
  603. {
  604. //获取创客分润规则,注册日开始算起,3个自然月内算扶持期
  605. getLevelProfit += NotHelpDirectTradeAmt * objlevel.Percents;
  606. }
  607. if (objlevel.AddProfitVal > 0)
  608. {
  609. getLevelProfit += objlevel.AddProfitVal;
  610. }
  611. decimal money = getLevelProfit;
  612. getLevelProfit -= diffLevelProfit;
  613. if (objlevel.LevelDiff == 1) //判断是否有级差
  614. {
  615. diffLevelProfit = money;
  616. }
  617. if (getLevelProfit >= obj.MinProfitVal)
  618. {
  619. result.Add(new ProfitResult()
  620. {
  621. UserId = user.Id,
  622. UserNav = user.ParentNav,
  623. Money = PublicFunction.NumberFormat(getLevelProfit),
  624. ProfitRate = objlevel.Percents,
  625. Message = "交易分润",
  626. BankCardType = BankCardType,
  627. QrPayFlag = QrPayFlag,
  628. BrandId = BrandId,
  629. DirectFlag = selfUser.Id == user.Id ? 1 : 0,
  630. IsSubsidy = 0,
  631. TradeAmount = NotHelpDirectTradeAmt,
  632. HelpFlag = 0,
  633. });
  634. }
  635. }
  636. }
  637. curLevel = UserLevel;
  638. }
  639. }
  640. }
  641. }
  642. }
  643. catch (Exception ex)
  644. {
  645. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "每月统计分润异常");
  646. }
  647. dbnew.Dispose();
  648. }
  649. }
  650. }
  651. return result;
  652. }
  653. #endregion
  654. }
  655. }