ProfitWifiBeforeHelper.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using MySystem.PxcModels;
  6. using Library;
  7. namespace MySystem
  8. {
  9. public class ProfitWifiBeforeHelper
  10. {
  11. public readonly static ProfitWifiBeforeHelper Instance = new ProfitWifiBeforeHelper();
  12. private ProfitWifiBeforeHelper()
  13. {
  14. }
  15. #region 获取分润规则Id
  16. public int GetLevelKindId(int BrandId, int Help, int BankCardType)
  17. {
  18. return RedisDbconn.Instance.Get<int>("pobjlv:kid:" + BrandId + ":" + Help + ":" + BankCardType);
  19. }
  20. #endregion
  21. #region POS分润算法
  22. public void StatProfiting()
  23. {
  24. string Month = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  25. string check = function.ReadInstance("/ProfitStat/" + Month + "-1-wifi-before.txt");
  26. if (!string.IsNullOrEmpty(check))
  27. {
  28. return;
  29. }
  30. function.WritePage("/ProfitStat/", Month + "-1-wifi-before.txt", DateTime.Now.ToString());
  31. try
  32. {
  33. WebCMSEntities db = new WebCMSEntities();
  34. List<int> BrandIds = new List<int>();
  35. BrandIds.Add(23);
  36. BrandIds.Add(24);
  37. BrandIds.Add(25);
  38. BrandIds.Add(26);
  39. foreach (int i in BrandIds) //品牌
  40. {
  41. int PageNum = 1;
  42. bool op = true;
  43. while(op)
  44. {
  45. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + "BrandId:" + i + ",Month:" + Month + ",PageNum:" + PageNum, "WIFI分润监控日志");
  46. List<ProfitResult> result = result = StartProftForPosByDate(i, Month, PageNum);
  47. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + "result:" + result.Count, "WIFI分润监控日志");
  48. function.WriteLog("\r\n", "WIFI分润监控日志");
  49. if(result.Count > 0)
  50. {
  51. foreach (ProfitResult sub in result)
  52. {
  53. int TopUserId = 0;
  54. if (!string.IsNullOrEmpty(sub.UserNav))
  55. {
  56. string[] UserNavList = sub.UserNav.Trim(',').Replace(",,", ",").Split(',');
  57. if (UserNavList.Length > 1)
  58. {
  59. TopUserId = int.Parse(UserNavList[1]);
  60. }
  61. else
  62. {
  63. TopUserId = int.Parse(UserNavList[0]);
  64. }
  65. }
  66. string RecordNo = "P" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  67. decimal ProfitAmt = sub.Money;
  68. decimal TradeAmt = sub.TradeAmount;
  69. decimal ProfitRate = sub.ProfitRate;
  70. decimal CreditTradeAmt = 0;
  71. decimal CreditTradeProfit = 0;
  72. decimal QrCreditTradeAmt = 0;
  73. decimal QrCreditProfitRate = 0;
  74. decimal QrCreditProfitStandardRate = 0;
  75. decimal QrCreditAddOrSubRate = 0;
  76. decimal QrCreditTradeProfit = 0;
  77. decimal NonQrCreditTradeAmt = 0;
  78. decimal NotHelpCreditTradeAmt = 0;
  79. decimal NotHelpCreditTradeProfit = 0;
  80. decimal NotHelpCreditProfitRate = 0;
  81. decimal NotHelpCreditProfitStandardRate = 0;
  82. decimal NotHelpCreditAddOrSubRate = 0;
  83. decimal HelpCreditTradeAmt = 0;
  84. decimal NonQrCreditTradeProfit = 0;
  85. decimal HelpCreditTradeProfit = 0;
  86. decimal HelpCreditProfitRate = 0;
  87. decimal HelpCreditProfitStandardRate = 0;
  88. decimal HelpCreditAddOrSubRate = 0;
  89. decimal DebitNonCapTradeAmt = 0;
  90. decimal DebitCapTradeAmt = 0;
  91. decimal QrDebitNotCapTradeAmt = 0;
  92. decimal NonQrDebitNotCapTradeAmt = 0;
  93. decimal NotHelpDebitNonCapTradeAmt = 0;
  94. decimal HelpDebitNonCapTradeAmt = 0;
  95. decimal DebitNonTradeCapProfit = 0;
  96. decimal QrDebitNonTradeCapProfit = 0;
  97. decimal NonQrDebitNonTradeCapProfit = 0;
  98. decimal NotHelpDebitNonTradeCapProfit = 0;
  99. decimal HelpDebitNonTradeCapProfit = 0;
  100. decimal DebitTradeCapProfit = 0;
  101. decimal NotHelpDebitProfitRate = 0;
  102. decimal NotHelpDebitProfitStandardRate = 0;
  103. decimal NotHelpDebitAddOrSubRate = 0;
  104. decimal HelpDebitProfitRate = 0;
  105. decimal HelpDebitProfitStandardRate = 0;
  106. decimal HelpDebitAddOrSubRate = 0;
  107. decimal DebitCapSingleReward = 0;
  108. decimal QrDebitProfitRate = 0;
  109. decimal QrDebitProfitStandardRate = 0;
  110. decimal QrDebitAddOrSubRate = 0;
  111. decimal ProfitCreditTradeProfit = 0;
  112. decimal ProfitCreditProfitRate = 0;
  113. decimal ProfitCreditTradeAmt = 0;
  114. decimal ProfitCreditTradeProfit2 = 0;
  115. decimal ProfitCreditProfitRate2 = 0;
  116. decimal ProfitCreditTradeAmt2 = 0;
  117. decimal ProfitDebitNonCapTradeAmt = 0;
  118. decimal ProfitDebitNonTradeCapProfit = 0;
  119. decimal ProfitDebitProfitRate = 0;
  120. CreditTradeAmt = TradeAmt; //贷记卡交易总金额
  121. CreditTradeProfit = ProfitAmt; //贷记卡分润总金额
  122. NonQrCreditTradeAmt = TradeAmt; //(非云闪付)贷记卡交易总金额
  123. NonQrCreditTradeProfit = ProfitAmt; //(非云闪付)贷记卡交易分润
  124. if(sub.HelpFlag == 1)
  125. {
  126. HelpCreditTradeProfit = ProfitAmt; //分润总金额
  127. HelpCreditProfitRate = ProfitRate; //商户交易对应分润比例
  128. HelpCreditTradeAmt = TradeAmt; //交易总金额
  129. }
  130. db.ProfitRewardRecord.Add(new ProfitRewardRecord()
  131. {
  132. Version = 1,
  133. CreateDate = DateTime.Now,
  134. UpdateDate = DateTime.Now,
  135. UserId = sub.UserId, //创客
  136. MerchantId = sub.MerchantId, //商户
  137. BrandId = sub.BrandId, //品牌
  138. ProfitType = sub.DirectFlag, //创客分润类型
  139. TradeAmt = sub.TradeAmount, //商户交易总额
  140. TradeProfit = sub.Money, //交易分润
  141. TradeMonth = Month, //交易月
  142. RecordNo = RecordNo, //记录单号
  143. TopUserId = TopUserId, //顶级创客
  144. CreditTradeAmt = CreditTradeAmt, //贷记卡交易总金额
  145. QrCreditTradeAmt = QrCreditTradeAmt, //(云闪付)贷记卡交易总金额
  146. NonQrCreditTradeAmt = NonQrCreditTradeAmt, //(非云闪付)贷记卡交易总金额
  147. NotHelpCreditTradeAmt = NotHelpCreditTradeAmt, //(非扶持期)(非云闪付)贷记卡交易总金额
  148. HelpCreditTradeAmt = HelpCreditTradeAmt, //(扶持期)(非云闪付)贷记卡交易总金额
  149. DebitNonCapTradeAmt = DebitNonCapTradeAmt, //商户非封顶借记卡交易总额
  150. QrDebitNotCapTradeAmt = QrDebitNotCapTradeAmt, //(云闪付)商户非封顶借记卡交易总额
  151. NonQrDebitNotCapTradeAmt = NonQrDebitNotCapTradeAmt, //(非云闪付)商户非封顶借记卡交易总额
  152. NotHelpDebitNonCapTradeAmt = NotHelpDebitNonCapTradeAmt, //(非扶持期)(非云闪付)借记卡非封顶交易总金额
  153. HelpDebitNonCapTradeAmt = HelpDebitNonCapTradeAmt, //(扶持期)(非云闪付)借记卡非封顶交易总金额
  154. DebitCapTradeAmt = DebitCapTradeAmt, //商户借记卡封顶交易总额
  155. CreditTradeProfit = CreditTradeProfit, //贷记卡分润总金额
  156. QrCreditTradeProfit = QrCreditTradeProfit, //(云闪付)贷记卡交易分润
  157. NonQrCreditTradeProfit = NonQrCreditTradeProfit, //(非云闪付)贷记卡交易分润
  158. NotHelpCreditTradeProfit = NotHelpCreditTradeProfit, //(非扶持期)(非云闪付)贷记卡分润总金额
  159. HelpCreditTradeProfit = HelpCreditTradeProfit, //(扶持期)(非云闪付)贷记卡分润总金额
  160. DebitNonTradeCapProfit = DebitNonTradeCapProfit, //借记卡非封顶交易分润总金额
  161. QrDebitNonTradeCapProfit = QrDebitNonTradeCapProfit, //(云闪付)借记卡非封顶交易分润总金额
  162. NonQrDebitNonTradeCapProfit = NonQrDebitNonTradeCapProfit, //(非云闪付)借记卡非封顶交易分润总金额
  163. NotHelpDebitNonTradeCapProfit = NotHelpDebitNonTradeCapProfit, //(非扶持期)(非云闪付)借记卡非封顶交易分润总金额
  164. HelpDebitNonTradeCapProfit = HelpDebitNonTradeCapProfit, //(扶持期)(非云闪付)借记卡非封顶交易分润总金额
  165. DebitTradeCapProfit = DebitTradeCapProfit, //借记卡封顶交易分润总金额
  166. NotHelpCreditProfitRate = NotHelpCreditProfitRate, //(非扶持期)(非云闪付)贷记卡商户交易对应分润比例
  167. NotHelpCreditProfitStandardRate = NotHelpCreditProfitStandardRate, //(非扶持期)(非云闪付)贷记卡标准分润比例
  168. NotHelpCreditAddOrSubRate = NotHelpCreditAddOrSubRate, //(非扶持期)(非云闪付)贷记卡成本或增或减比例
  169. HelpCreditProfitRate = HelpCreditProfitRate, //(扶持期)(非云闪付)贷记卡商户交易对应分润比例
  170. HelpCreditProfitStandardRate = HelpCreditProfitStandardRate, //(扶持期)(非云闪付)贷记卡-标准分润比例
  171. HelpCreditAddOrSubRate = HelpCreditAddOrSubRate, //(扶持期)(非云闪付)贷记卡成本或增或减比例
  172. NotHelpDebitProfitRate = NotHelpDebitProfitRate, //(非扶持期)(非云闪付)借记卡商户交易对应分润比例
  173. NotHelpDebitProfitStandardRate = NotHelpDebitProfitStandardRate, //(非扶持期)(非云闪付)借记卡标准分润比例
  174. NotHelpDebitAddOrSubRate = NotHelpDebitAddOrSubRate, //(非扶持期)(非云闪付)借记卡成本或增或减比例
  175. HelpDebitProfitRate = HelpDebitProfitRate, //(扶持期)(非云闪付)借记卡商户交易对应分润比例
  176. HelpDebitProfitStandardRate = HelpDebitProfitStandardRate, //(扶持期)(非云闪付)借记卡-标准分润比例
  177. HelpDebitAddOrSubRate = HelpDebitAddOrSubRate, //(扶持期)(非云闪付)借记卡成本或增或减比例
  178. DebitCapSingleReward = DebitCapSingleReward, //借记卡合伙人直营封顶交易奖励金额
  179. QrCreditProfitRate = QrCreditProfitRate, //(云闪付)贷记卡交易分润比例
  180. QrCreditProfitStandardRate = QrCreditProfitStandardRate, //(云闪付)贷记卡分润基准
  181. QrCreditAddOrSubRate = QrCreditAddOrSubRate, //(云闪付)贷记卡分润比例或增或减
  182. QrDebitProfitRate = QrDebitProfitRate, //(云闪付)借记卡非封顶交易分润比例
  183. QrDebitProfitStandardRate = QrDebitProfitStandardRate, //(云闪付)借记卡非封顶交易分润基准
  184. QrDebitAddOrSubRate = QrDebitAddOrSubRate, //(云闪付)借记卡非封顶交易分润比例或增或减
  185. ProfitCreditTradeProfit = ProfitCreditTradeProfit, //(盈利期)(非云闪付)贷记卡分润总金额
  186. ProfitCreditProfitRate = ProfitCreditProfitRate, //(盈利期)(非云闪付)贷记卡商户交易对应分润比例
  187. ProfitCreditTradeAmt = ProfitCreditTradeAmt, //(盈利期)(非云闪付)贷记卡交易总金额
  188. ProfitCreditTradeProfit2 = ProfitCreditTradeProfit2, //(盈利期)(非云闪付)贷记卡分润总金额
  189. ProfitCreditProfitRate2 = ProfitCreditProfitRate2, //(盈利期)(非云闪付)贷记卡商户交易对应分润比例
  190. ProfitCreditTradeAmt2 = ProfitCreditTradeAmt2, //(盈利期)(非云闪付)贷记卡交易总金额
  191. ProfitDebitNonCapTradeAmt = ProfitDebitNonCapTradeAmt, //(盈利期)(非云闪付)借记卡非封顶交易总金额
  192. ProfitDebitNonTradeCapProfit = ProfitDebitNonTradeCapProfit, //(盈利期)(非云闪付)借记卡非封顶交易分润总金额
  193. ProfitDebitProfitRate = ProfitDebitProfitRate, //(盈利期)(非云闪付)借记卡商户交易对应分润比例
  194. });
  195. }
  196. db.SaveChanges();
  197. PageNum += 1;
  198. }
  199. else
  200. {
  201. op = false;
  202. }
  203. }
  204. }
  205. db.Dispose();
  206. }
  207. catch(Exception ex)
  208. {
  209. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "WIFI分润监控异常");
  210. }
  211. }
  212. public List<ProfitResult> StartProftForPosByDate(int BrandId, string Month, int PageNum = 1)
  213. {
  214. int PageSize = 200;
  215. int SkipNum = (PageNum - 1) * PageSize;
  216. string LimitString = " limit " + PageSize;
  217. if(PageNum > 1)
  218. {
  219. LimitString = " limit " + SkipNum + "," + PageSize;
  220. }
  221. List<ProfitResult> result = new List<ProfitResult>();
  222. ProfitObjects obj = RedisDbconn.Instance.Get<ProfitObjects>("pobj23");
  223. if (obj.Status == 1) //判断分润是否开启
  224. {
  225. string sqlstr = "select UserId,HelpDirectTradeAmt from TradeDaySummaryBefore where TradeMonth='" + Month + "' and HelpDirectTradeAmt>0 and BrandId=" + BrandId + LimitString;
  226. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + sqlstr, "WIFI分润监控日志");
  227. DataTable dt = CustomerSqlConn.dtable(sqlstr, MysqlConn.ReadSqlConnStr);
  228. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + dt.Rows.Count.ToString(), "WIFI分润监控日志");
  229. if(dt.Rows.Count > 0)
  230. {
  231. foreach (DataRow dr in dt.Rows)
  232. {
  233. WebCMSEntities dbnew = new WebCMSEntities();
  234. try
  235. {
  236. int UserId = int.Parse(dr["UserId"].ToString());
  237. List<UserLevelSet> levels = dbnew.UserLevelSet.ToList();
  238. SubUser selfUser = ProfitBeforeHelper.Instance.GetUser(UserId, Month) ?? new SubUser();
  239. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "WIFI分润监控日志");
  240. decimal HelpDirectTradeAmt = decimal.Parse(dr[1].ToString()); //交易额
  241. int maxLevel = obj.MaxLevel; //最大等级
  242. decimal diffLevelProfit = 0; //等级级差
  243. int curLevel = 0; //当前层级的会员等级
  244. string ParentNav = selfUser.ParentNav;
  245. if (string.IsNullOrEmpty(ParentNav))
  246. {
  247. ParentNav = ",0,";
  248. }
  249. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + ParentNav, "WIFI分润监控日志");
  250. if (!string.IsNullOrEmpty(ParentNav))
  251. {
  252. ParentNav += "," + UserId + ",";
  253. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  254. Array.Reverse(ParentNavList); //反转顺序
  255. int level = 0;
  256. DateTime now = DateTime.Now;
  257. DateTime ThisMonth = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00");
  258. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":扶持期:" + HelpDirectTradeAmt, "WIFI分润监控日志");
  259. if (HelpDirectTradeAmt > 0)
  260. {
  261. level = 0;
  262. curLevel = 0;
  263. diffLevelProfit = 0; //等级级差
  264. decimal checkProfit = HelpDirectTradeAmt * 0.0012M;
  265. decimal curProfit = 0;
  266. foreach (string UserIdString in ParentNavList)
  267. {
  268. level += 1;
  269. int uid = int.Parse(UserIdString);
  270. SubUser user = ProfitBeforeHelper.Instance.GetUser(uid, Month) ?? new SubUser();
  271. int UserLevel = ProfitBeforeHelper.Instance.GetUserLevel(user); //当前会员等级
  272. if (curLevel == maxLevel) //判断当前创客是否有直推的激活机具,并且在活动时间内
  273. {
  274. break;
  275. }
  276. if (UserLevel <= maxLevel && UserLevel >= curLevel)
  277. {
  278. int LevelKindId = GetLevelKindId(23, 1, 1);
  279. ProfitObjectLevels objlevel = RedisDbconn.Instance.Get<ProfitObjectLevels>("pobjlv" + LevelKindId + ":" + UserLevel); //获取当前等级参数
  280. if (objlevel != null)
  281. {
  282. decimal getLevelProfit = 0; //等级分润
  283. if (objlevel.Percents > 0)
  284. {
  285. getLevelProfit += HelpDirectTradeAmt * objlevel.Percents;
  286. }
  287. if (objlevel.AddProfitVal > 0)
  288. {
  289. getLevelProfit += objlevel.AddProfitVal;
  290. }
  291. decimal money = getLevelProfit;
  292. getLevelProfit -= diffLevelProfit;
  293. if (objlevel.LevelDiff == 1) //判断是否有级差
  294. {
  295. diffLevelProfit = money;
  296. }
  297. if (getLevelProfit >= obj.MinProfitVal)
  298. {
  299. int DirectFlag = selfUser.Id == user.Id ? 1 : 0;
  300. result.Add(new ProfitResult()
  301. {
  302. UserId = user.Id,
  303. UserNav = user.ParentNav,
  304. Money = PublicFunction.NumberFormat(getLevelProfit),
  305. ProfitRate = objlevel.Percents,
  306. Message = "交易分润",
  307. BankCardType = 1,
  308. DirectFlag = DirectFlag,
  309. BrandId = BrandId,
  310. IsSubsidy = 0,
  311. TradeAmount = HelpDirectTradeAmt,
  312. HelpFlag = 1,
  313. });
  314. curProfit += getLevelProfit;
  315. function.WriteLog(user.Id + "-" + UserLevel + "-" + HelpDirectTradeAmt + "-" + DirectFlag + "-扶持期分润:" + getLevelProfit, "WIFI分润监控日志");
  316. }
  317. }
  318. }
  319. if(curLevel < UserLevel)
  320. {
  321. curLevel = UserLevel;
  322. }
  323. }
  324. if(curProfit > checkProfit)
  325. {
  326. decimal more = curProfit - checkProfit;
  327. function.WriteLog("超标了:" + more, "WIFI分润监控日志");
  328. }
  329. }
  330. function.WriteLog("------------------------", "WIFI分润监控日志");
  331. }
  332. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":over", "WIFI分润监控日志");
  333. }
  334. catch (Exception ex)
  335. {
  336. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "WIFI分润监控异常");
  337. }
  338. dbnew.Dispose();
  339. }
  340. }
  341. }
  342. return result;
  343. }
  344. #endregion
  345. }
  346. }