SycnProfitService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 = db.UserMachineData.Add(new UserMachineData()
  92. {
  93. IdBrand = IdBrand,
  94. }).Entity;
  95. db.SaveChanges();
  96. }
  97. MachineData.TradeProfit += ProfitMoney;
  98. db.SaveChanges();
  99. }
  100. else if (OpType == 1)
  101. {
  102. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  103. if (account == null)
  104. {
  105. account = db.UserAccount.Add(new UserAccount()
  106. {
  107. Id = UserId,
  108. UserId = UserId,
  109. }).Entity;
  110. db.SaveChanges();
  111. }
  112. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  113. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  114. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  115. account.BalanceAmount += ProfitMoney;
  116. account.TotalAmount += ProfitMoney;
  117. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  118. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  119. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  120. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  121. {
  122. CreateDate = DateTime.Now,
  123. UpdateDate = DateTime.Now,
  124. UserId = UserId, //创客
  125. ProductType = BrandId,
  126. ChangeType = 1, //变动类型
  127. ChangeAmount = ProfitMoney, //变更金额
  128. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  129. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  130. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  131. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  132. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  133. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  134. Remark = ProfitType == 1 ? "直拓商户分润" : "品牌推广服务费",
  135. }).Entity;
  136. db.SaveChanges();
  137. RedisDbconn.Instance.Clear("UserAccount:" + UserId);
  138. RedisDbconn.Instance.Clear("UserAccount:Income:" + UserId + ":" + DateTime.Now.ToString("yyyyMM"));
  139. }
  140. tran.Commit();
  141. }
  142. catch (Exception ex)
  143. {
  144. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitType + "," + ProfitMoney + "\n" + ex.ToString(), "同步到余额异常");
  145. tran.Rollback();
  146. }
  147. function.WriteLog(index.ToString(), "同步分润数据");
  148. }
  149. db.Dispose();
  150. }
  151. //分润补贴
  152. private void DoSubsidyProfit(int BrandId, string date, int OpType = 0)
  153. {
  154. int OpTypeDo = OpType + 1;
  155. WebCMSEntities db = new WebCMSEntities();
  156. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  157. DataTable dt = OtherMySqlConn.dtable("select SubsidyUserId,sum(SubsidyProfitRate) from ProfitSubsidyDetail where Status=" + OpType + " and BrandId=" + BrandId + " and TradeMonth='" + date + "' GROUP BY SubsidyUserId");
  158. function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  159. int index = 0;
  160. foreach (DataRow dr in dt.Rows)
  161. {
  162. index += 1;
  163. int UserId = int.Parse(dr["SubsidyUserId"].ToString());
  164. decimal ProfitMoney = decimal.Parse(dr[1].ToString());
  165. OtherMySqlConn.op("update ProfitSubsidyDetail set Status=" + OpTypeDo + " where BrandId=" + BrandId + " and TradeMonth='" + date + "' and SubsidyUserId=" + UserId + "");
  166. var tran = db.Database.BeginTransaction();
  167. try
  168. {
  169. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  170. if (OpType == 0)
  171. {
  172. string IdBrand = UserId + "_" + BrandId;
  173. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  174. if (MachineData == null)
  175. {
  176. MachineData = db.UserMachineData.Add(new UserMachineData()
  177. {
  178. IdBrand = IdBrand,
  179. }).Entity;
  180. db.SaveChanges();
  181. }
  182. MachineData.OtherProfit += ProfitMoney;
  183. db.SaveChanges();
  184. }
  185. else if (OpType == 1)
  186. {
  187. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  188. if (account == null)
  189. {
  190. account = db.UserAccount.Add(new UserAccount()
  191. {
  192. Id = UserId,
  193. UserId = UserId,
  194. }).Entity;
  195. db.SaveChanges();
  196. }
  197. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  198. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  199. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  200. account.BalanceAmount += ProfitMoney;
  201. account.TotalAmount += ProfitMoney;
  202. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  203. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  204. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  205. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  206. {
  207. CreateDate = DateTime.Now,
  208. UpdateDate = DateTime.Now,
  209. UserId = UserId, //创客
  210. ProductType = BrandId,
  211. ChangeType = 111, //变动类型
  212. ChangeAmount = ProfitMoney, //变更金额
  213. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  214. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  215. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  216. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  217. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  218. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  219. Remark = "直拓商户补贴",
  220. }).Entity;
  221. db.SaveChanges();
  222. RedisDbconn.Instance.Clear("UserAccount:" + UserId);
  223. RedisDbconn.Instance.Clear("UserAccount:Income:" + UserId + ":" + DateTime.Now.ToString("yyyyMM"));
  224. }
  225. tran.Commit();
  226. }
  227. catch (Exception ex)
  228. {
  229. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到补贴异常");
  230. tran.Rollback();
  231. }
  232. function.WriteLog(index.ToString(), "同步分润数据");
  233. }
  234. db.Dispose();
  235. }
  236. }
  237. }