SycnProfitService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading;
  6. using MySystem.Models;
  7. using Library;
  8. namespace MySystem
  9. {
  10. public class SycnProfitService
  11. {
  12. public readonly static SycnProfitService Instance = new SycnProfitService();
  13. private SycnProfitService()
  14. { }
  15. public void Start()
  16. {
  17. Thread th = new Thread(doSomething);
  18. th.IsBackground = true;
  19. th.Start();
  20. }
  21. public void doSomething()
  22. {
  23. while (true)
  24. {
  25. string content = RedisDbconn.Instance.RPop<string>("SycnProfitQueue");
  26. if (!string.IsNullOrEmpty(content))
  27. {
  28. try
  29. {
  30. function.WriteLog(DateTime.Now.ToString() + "\r\n\r\n", "同步分润数据");
  31. string[] data = content.Split(new string[]{ "#cut#" }, StringSplitOptions.None);
  32. int BrandId = int.Parse(data[0]);
  33. string date = data[1];
  34. int OpType = int.Parse(data[2]);
  35. string SysUserName = data[3];
  36. DoTradeProfit(BrandId, date, SysUserName, OpType);
  37. // DoSubsidyProfit(BrandId, date, OpType);
  38. function.WriteLog(DateTime.Now.ToString() + "\r\n\r\n", "同步分润数据");
  39. }
  40. catch (Exception ex)
  41. {
  42. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "同步分润数据异常");
  43. }
  44. }
  45. else
  46. {
  47. Thread.Sleep(60000);
  48. }
  49. }
  50. }
  51. private void DoTradeProfit(int BrandId, string date, string SysUserName, int OpType = 0)
  52. {
  53. int OpTypeDo = OpType + 1;
  54. WebCMSEntities db = new WebCMSEntities();
  55. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  56. DataTable dt = OtherMySqlConn.dtable("select UserId,ProfitType,sum(CreditTradeProfit+DebitNonTradeCapProfit+DebitTradeCapProfit+QrCreditTradeProfit) from ProfitRewardRecord where CheckStatus=" + OpType + " and BrandId=" + BrandId + " and TradeMonth='" + date + "' group by UserId,ProfitType");
  57. function.WriteLog("分润:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  58. int index = 0;
  59. foreach (DataRow dr in dt.Rows)
  60. {
  61. index += 1;
  62. int UserId = int.Parse(dr["UserId"].ToString());
  63. ulong ProfitType = ulong.Parse(dr["ProfitType"].ToString());
  64. OtherMySqlConn.op("update ProfitRewardRecord set CheckStatus=" + OpTypeDo + " where BrandId=" + BrandId + " and TradeMonth='" + date + "' and UserId=" + UserId + " and ProfitType=" + ProfitType);
  65. decimal ProfitMoney = decimal.Parse(dr[2].ToString());
  66. var tran = db.Database.BeginTransaction();
  67. try
  68. {
  69. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  70. if (OpType == 0)
  71. {
  72. ProfitRecord profit = db.ProfitRecord.FirstOrDefault(m => m.UserId == UserId && m.BrandId == BrandId && m.SeoTitle == date);
  73. if (profit == null)
  74. {
  75. profit = db.ProfitRecord.Add(new ProfitRecord()
  76. {
  77. CreateDate = DateTime.Now,
  78. CreateMan = SysUserName,
  79. SeoTitle = date,
  80. ParentNav = user.ParentNav,
  81. BrandId = BrandId,
  82. UserId = UserId,
  83. }).Entity;
  84. db.SaveChanges();
  85. }
  86. profit.ProfitAmount += ProfitMoney;
  87. string IdBrand = UserId + "_" + BrandId;
  88. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  89. if (MachineData != null)
  90. {
  91. MachineData.TradeProfit += ProfitMoney;
  92. }
  93. db.SaveChanges();
  94. }
  95. else if (OpType == 1)
  96. {
  97. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  98. if (account == null)
  99. {
  100. account = db.UserAccount.Add(new UserAccount()
  101. {
  102. Id = UserId,
  103. UserId = UserId,
  104. }).Entity;
  105. db.SaveChanges();
  106. }
  107. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  108. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  109. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  110. account.BalanceAmount += ProfitMoney;
  111. account.TotalAmount += ProfitMoney;
  112. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  113. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  114. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  115. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  116. {
  117. CreateDate = DateTime.Now,
  118. UpdateDate = DateTime.Now,
  119. UserId = UserId, //创客
  120. ProductType = BrandId,
  121. ChangeType = 1, //变动类型
  122. ChangeAmount = ProfitMoney, //变更金额
  123. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  124. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  125. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  126. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  127. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  128. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  129. Remark = ProfitType == 1 ? "直拓商户分润" : "品牌推广服务费",
  130. }).Entity;
  131. db.SaveChanges();
  132. RedisDbconn.Instance.Clear("UserAccount:" + UserId);
  133. RedisDbconn.Instance.Clear("UserAccount:Income:" + UserId + ":" + DateTime.Now.ToString("yyyyMM"));
  134. }
  135. tran.Commit();
  136. }
  137. catch (Exception ex)
  138. {
  139. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitType + "," + ProfitMoney + "\n" + ex.ToString(), "同步到余额异常");
  140. tran.Rollback();
  141. }
  142. function.WriteLog(index.ToString(), "同步分润数据");
  143. }
  144. db.Dispose();
  145. }
  146. //分润补贴
  147. private void DoSubsidyProfit(int BrandId, string date, int OpType = 0)
  148. {
  149. int OpTypeDo = OpType + 1;
  150. WebCMSEntities db = new WebCMSEntities();
  151. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  152. DataTable dt = OtherMySqlConn.dtable("select SubsidyUserId,sum(SubsidyProfitRate) from ProfitSubsidyDetail where Status=" + OpType + " and BrandId=" + BrandId + " and TradeMonth='" + date + "' GROUP BY SubsidyUserId");
  153. function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  154. int index = 0;
  155. foreach (DataRow dr in dt.Rows)
  156. {
  157. index += 1;
  158. int UserId = int.Parse(dr["SubsidyUserId"].ToString());
  159. decimal ProfitMoney = decimal.Parse(dr[1].ToString());
  160. OtherMySqlConn.op("update ProfitSubsidyDetail set Status=" + OpTypeDo + " where BrandId=" + BrandId + " and TradeMonth='" + date + "' and SubsidyUserId=" + UserId + "");
  161. var tran = db.Database.BeginTransaction();
  162. try
  163. {
  164. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  165. if (OpType == 0)
  166. {
  167. string IdBrand = UserId + "_" + BrandId;
  168. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  169. if (MachineData != null)
  170. {
  171. MachineData.OtherProfit += ProfitMoney;
  172. }
  173. db.SaveChanges();
  174. }
  175. else if (OpType == 1)
  176. {
  177. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  178. if (account == null)
  179. {
  180. account = db.UserAccount.Add(new UserAccount()
  181. {
  182. Id = UserId,
  183. UserId = UserId,
  184. }).Entity;
  185. db.SaveChanges();
  186. }
  187. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  188. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  189. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  190. account.BalanceAmount += ProfitMoney;
  191. account.TotalAmount += ProfitMoney;
  192. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  193. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  194. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  195. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  196. {
  197. CreateDate = DateTime.Now,
  198. UpdateDate = DateTime.Now,
  199. UserId = UserId, //创客
  200. ProductType = BrandId,
  201. ChangeType = 111, //变动类型
  202. ChangeAmount = ProfitMoney, //变更金额
  203. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  204. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  205. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  206. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  207. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  208. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  209. Remark = "直拓商户补贴",
  210. }).Entity;
  211. db.SaveChanges();
  212. RedisDbconn.Instance.Clear("UserAccount:" + UserId);
  213. RedisDbconn.Instance.Clear("UserAccount:Income:" + UserId + ":" + DateTime.Now.ToString("yyyyMM"));
  214. }
  215. tran.Commit();
  216. }
  217. catch (Exception ex)
  218. {
  219. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到补贴异常");
  220. tran.Rollback();
  221. }
  222. function.WriteLog(index.ToString(), "同步分润数据");
  223. }
  224. db.Dispose();
  225. }
  226. }
  227. }