ProfitHelperV2.cs 43 KB

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