SycnProfitService.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. // if(OpType == 0)
  37. // {
  38. // DoTradeProfit(BrandId, date, SysUserName, OpType);
  39. // }
  40. // else if(OpType == 1)
  41. // {
  42. // DoTradeProfit2(BrandId, date, SysUserName, OpType);
  43. // }
  44. DoTradeProfit(BrandId, date, SysUserName, OpType);
  45. DoSubsidyProfit(BrandId, date, OpType);
  46. function.WriteLog(DateTime.Now.ToString() + "\r\n\r\n", "同步分润数据");
  47. }
  48. catch (Exception ex)
  49. {
  50. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "同步分润数据异常");
  51. }
  52. }
  53. else
  54. {
  55. Thread.Sleep(60000);
  56. }
  57. }
  58. }
  59. private void DoTradeProfit(int BrandId, string date, string SysUserName, int OpType = 0)
  60. {
  61. int OpTypeDo = OpType + 1;
  62. WebCMSEntities db = new WebCMSEntities();
  63. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  64. 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");
  65. function.WriteLog("分润:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  66. int index = 0;
  67. foreach (DataRow dr in dt.Rows)
  68. {
  69. index += 1;
  70. int UserId = int.Parse(dr["UserId"].ToString());
  71. ulong ProfitType = ulong.Parse(dr["ProfitType"].ToString());
  72. OtherMySqlConn.op("update ProfitRewardRecord set CheckStatus=" + OpTypeDo + " where BrandId=" + BrandId + " and TradeMonth='" + date + "' and UserId=" + UserId + " and ProfitType=" + ProfitType);
  73. decimal ProfitMoney = decimal.Parse(dr[2].ToString());
  74. var tran = db.Database.BeginTransaction();
  75. try
  76. {
  77. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  78. if (OpType == 0)
  79. {
  80. ProfitRecord profit = db.ProfitRecord.FirstOrDefault(m => m.UserId == UserId && m.BrandId == BrandId && m.SeoTitle == date);
  81. if (profit == null)
  82. {
  83. profit = db.ProfitRecord.Add(new ProfitRecord()
  84. {
  85. CreateDate = DateTime.Now,
  86. CreateMan = SysUserName,
  87. SeoTitle = date,
  88. ParentNav = user.ParentNav,
  89. BrandId = BrandId,
  90. UserId = UserId,
  91. }).Entity;
  92. db.SaveChanges();
  93. }
  94. profit.ProfitAmount += ProfitMoney;
  95. string IdBrand = UserId + "_" + BrandId;
  96. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  97. if (MachineData == null)
  98. {
  99. MachineData = db.UserMachineData.Add(new UserMachineData()
  100. {
  101. IdBrand = IdBrand,
  102. }).Entity;
  103. db.SaveChanges();
  104. }
  105. MachineData.TradeProfit += ProfitMoney;
  106. db.SaveChanges();
  107. }
  108. else if (OpType == 1)
  109. {
  110. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  111. if (account == null)
  112. {
  113. account = db.UserAccount.Add(new UserAccount()
  114. {
  115. Id = UserId,
  116. UserId = UserId,
  117. }).Entity;
  118. db.SaveChanges();
  119. }
  120. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  121. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  122. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  123. account.BalanceAmount += ProfitMoney;
  124. account.TotalAmount += ProfitMoney;
  125. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  126. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  127. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  128. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  129. {
  130. CreateDate = DateTime.Now,
  131. UpdateDate = DateTime.Now,
  132. UserId = UserId, //创客
  133. ProductType = BrandId,
  134. ChangeType = 1, //变动类型
  135. ChangeAmount = ProfitMoney, //变更金额
  136. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  137. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  138. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  139. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  140. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  141. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  142. Remark = ProfitType == 1 ? "直拓商户分润" : "品牌推广服务费",
  143. }).Entity;
  144. db.SaveChanges();
  145. // RedisDbconn.Instance.Clear("UserAccount:" + UserId);
  146. // RedisDbconn.Instance.Clear("UserAccount:Income:" + UserId + ":" + DateTime.Now.ToString("yyyyMM"));
  147. }
  148. tran.Commit();
  149. }
  150. catch (Exception ex)
  151. {
  152. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitType + "," + ProfitMoney + "\n" + ex.ToString(), "同步到余额异常");
  153. tran.Rollback();
  154. }
  155. function.WriteLog(index.ToString(), "同步分润数据");
  156. }
  157. db.Dispose();
  158. }
  159. private void DoTradeProfit2(int BrandId, string date, string SysUserName, int OpType = 0)
  160. {
  161. WebCMSEntities db = new WebCMSEntities();
  162. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  163. DataTable dt = OtherMySqlConn.dtable("select UserId,ProfitAmount from ProfitRecord where BrandId=" + BrandId + " and SeoTitle='" + date + "' and UserId>0 order by Id");
  164. function.WriteLog("分润:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  165. int index = 0;
  166. foreach (DataRow dr in dt.Rows)
  167. {
  168. index += 1;
  169. int UserId = int.Parse(dr["UserId"].ToString());
  170. decimal ProfitAmount = decimal.Parse(dr["ProfitAmount"].ToString());
  171. var tran = db.Database.BeginTransaction();
  172. try
  173. {
  174. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  175. if (account == null)
  176. {
  177. account = db.UserAccount.Add(new UserAccount()
  178. {
  179. Id = UserId,
  180. UserId = UserId,
  181. }).Entity;
  182. db.SaveChanges();
  183. }
  184. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  185. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  186. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  187. account.BalanceAmount += ProfitAmount;
  188. account.TotalAmount += ProfitAmount;
  189. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  190. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  191. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  192. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  193. {
  194. CreateDate = DateTime.Now,
  195. UpdateDate = DateTime.Now,
  196. UserId = UserId, //创客
  197. ProductType = BrandId,
  198. ChangeType = 1, //变动类型
  199. ChangeAmount = ProfitAmount, //变更金额
  200. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  201. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  202. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  203. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  204. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  205. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  206. // Remark = ProfitType == 1 ? "直拓商户分润" : "品牌推广服务费",
  207. }).Entity;
  208. db.SaveChanges();
  209. tran.Commit();
  210. }
  211. catch (Exception ex)
  212. {
  213. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitAmount + "\n" + ex.ToString(), "同步到余额异常");
  214. tran.Rollback();
  215. }
  216. function.WriteLog(index.ToString(), "同步分润数据");
  217. }
  218. db.Dispose();
  219. }
  220. //分润补贴
  221. private void DoSubsidyProfit(int BrandId, string date, int OpType = 0)
  222. {
  223. int OpTypeDo = OpType + 1;
  224. WebCMSEntities db = new WebCMSEntities();
  225. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  226. DataTable dt = OtherMySqlConn.dtable("select SubsidyUserId,sum(SubsidyProfitRate) from ProfitSubsidyDetail where Status=" + OpType + " and BrandId=" + BrandId + " and TradeMonth='" + date + "' GROUP BY SubsidyUserId");
  227. function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  228. int index = 0;
  229. foreach (DataRow dr in dt.Rows)
  230. {
  231. index += 1;
  232. int UserId = int.Parse(dr["SubsidyUserId"].ToString());
  233. decimal ProfitMoney = decimal.Parse(dr[1].ToString());
  234. OtherMySqlConn.op("update ProfitSubsidyDetail set Status=" + OpTypeDo + " where BrandId=" + BrandId + " and TradeMonth='" + date + "' and SubsidyUserId=" + UserId + "");
  235. var tran = db.Database.BeginTransaction();
  236. try
  237. {
  238. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  239. if (OpType == 0)
  240. {
  241. string IdBrand = UserId + "_" + BrandId;
  242. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  243. if (MachineData == null)
  244. {
  245. MachineData = db.UserMachineData.Add(new UserMachineData()
  246. {
  247. IdBrand = IdBrand,
  248. }).Entity;
  249. db.SaveChanges();
  250. }
  251. MachineData.OtherProfit += ProfitMoney;
  252. db.SaveChanges();
  253. }
  254. else if (OpType == 1)
  255. {
  256. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  257. if (account == null)
  258. {
  259. account = db.UserAccount.Add(new UserAccount()
  260. {
  261. Id = UserId,
  262. UserId = UserId,
  263. }).Entity;
  264. db.SaveChanges();
  265. }
  266. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  267. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  268. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  269. account.BalanceAmount += ProfitMoney;
  270. account.TotalAmount += ProfitMoney;
  271. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  272. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  273. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  274. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  275. {
  276. CreateDate = DateTime.Now,
  277. UpdateDate = DateTime.Now,
  278. UserId = UserId, //创客
  279. ProductType = BrandId,
  280. ChangeType = 111, //变动类型
  281. ChangeAmount = ProfitMoney, //变更金额
  282. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  283. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  284. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  285. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  286. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  287. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  288. Remark = "直拓商户补贴",
  289. }).Entity;
  290. db.SaveChanges();
  291. // RedisDbconn.Instance.Clear("UserAccount:" + UserId);
  292. // RedisDbconn.Instance.Clear("UserAccount:Income:" + UserId + ":" + DateTime.Now.ToString("yyyyMM"));
  293. }
  294. tran.Commit();
  295. }
  296. catch (Exception ex)
  297. {
  298. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到补贴异常");
  299. tran.Rollback();
  300. }
  301. function.WriteLog(index.ToString(), "同步分润数据");
  302. }
  303. db.Dispose();
  304. }
  305. }
  306. }