SycnProfitService.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 add = 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. PublicFunction.SplitUserAccountRecord(add);
  138. }
  139. tran.Commit();
  140. }
  141. catch (Exception ex)
  142. {
  143. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitType + "," + ProfitMoney + "\n" + ex.ToString(), "同步到余额异常");
  144. tran.Rollback();
  145. }
  146. function.WriteLog(index.ToString(), "同步分润数据");
  147. }
  148. db.Dispose();
  149. }
  150. private void DoTradeProfit2(int BrandId, string date, string SysUserName, int OpType = 0)
  151. {
  152. WebCMSEntities db = new WebCMSEntities();
  153. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  154. DataTable dt = OtherMySqlConn.dtable("select UserId,ProfitAmount from ProfitRecord where BrandId=" + BrandId + " and SeoTitle='" + date + "' and UserId>0 order by Id");
  155. function.WriteLog("分润:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  156. int index = 0;
  157. foreach (DataRow dr in dt.Rows)
  158. {
  159. index += 1;
  160. int UserId = int.Parse(dr["UserId"].ToString());
  161. decimal ProfitAmount = decimal.Parse(dr["ProfitAmount"].ToString());
  162. var tran = db.Database.BeginTransaction();
  163. try
  164. {
  165. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  166. if (account == null)
  167. {
  168. account = db.UserAccount.Add(new UserAccount()
  169. {
  170. Id = UserId,
  171. UserId = UserId,
  172. }).Entity;
  173. db.SaveChanges();
  174. }
  175. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  176. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  177. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  178. account.BalanceAmount += ProfitAmount;
  179. account.TotalAmount += ProfitAmount;
  180. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  181. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  182. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  183. UserAccountRecord add = db.UserAccountRecord.Add(new UserAccountRecord()
  184. {
  185. CreateDate = DateTime.Now,
  186. UpdateDate = DateTime.Now,
  187. UserId = UserId, //创客
  188. ProductType = BrandId,
  189. ChangeType = 1, //变动类型
  190. ChangeAmount = ProfitAmount, //变更金额
  191. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  192. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  193. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  194. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  195. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  196. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  197. }).Entity;
  198. db.SaveChanges();
  199. tran.Commit();
  200. PublicFunction.SplitUserAccountRecord(add);
  201. }
  202. catch (Exception ex)
  203. {
  204. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitAmount + "\n" + ex.ToString(), "同步到余额异常");
  205. tran.Rollback();
  206. }
  207. function.WriteLog(index.ToString(), "同步分润数据");
  208. }
  209. db.Dispose();
  210. }
  211. //分润补贴
  212. private void DoSubsidyProfit(int BrandId, string date, int OpType = 0)
  213. {
  214. int OpTypeDo = OpType + 1;
  215. WebCMSEntities db = new WebCMSEntities();
  216. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  217. DataTable dt = OtherMySqlConn.dtable("select SubsidyUserId,sum(SubsidyProfitRate) from ProfitSubsidyDetail where Status=" + OpType + " and BrandId=" + BrandId + " and TradeMonth='" + date + "' GROUP BY SubsidyUserId");
  218. function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  219. int index = 0;
  220. foreach (DataRow dr in dt.Rows)
  221. {
  222. index += 1;
  223. int UserId = int.Parse(dr["SubsidyUserId"].ToString());
  224. decimal ProfitMoney = decimal.Parse(dr[1].ToString());
  225. OtherMySqlConn.op("update ProfitSubsidyDetail set Status=" + OpTypeDo + " where BrandId=" + BrandId + " and TradeMonth='" + date + "' and SubsidyUserId=" + UserId + "");
  226. var tran = db.Database.BeginTransaction();
  227. try
  228. {
  229. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  230. if (OpType == 0)
  231. {
  232. string IdBrand = UserId + "_" + BrandId;
  233. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  234. if (MachineData == null)
  235. {
  236. MachineData = db.UserMachineData.Add(new UserMachineData()
  237. {
  238. IdBrand = IdBrand,
  239. }).Entity;
  240. db.SaveChanges();
  241. }
  242. MachineData.OtherProfit += ProfitMoney;
  243. db.SaveChanges();
  244. }
  245. else if (OpType == 1)
  246. {
  247. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  248. if (account == null)
  249. {
  250. account = db.UserAccount.Add(new UserAccount()
  251. {
  252. Id = UserId,
  253. UserId = UserId,
  254. }).Entity;
  255. db.SaveChanges();
  256. }
  257. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  258. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  259. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  260. account.BalanceAmount += ProfitMoney;
  261. account.TotalAmount += ProfitMoney;
  262. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  263. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  264. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  265. UserAccountRecord add = db.UserAccountRecord.Add(new UserAccountRecord()
  266. {
  267. CreateDate = DateTime.Now,
  268. UpdateDate = DateTime.Now,
  269. UserId = UserId, //创客
  270. ProductType = BrandId,
  271. ChangeType = 111, //变动类型
  272. ChangeAmount = ProfitMoney, //变更金额
  273. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  274. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  275. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  276. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  277. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  278. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  279. Remark = "直拓商户补贴",
  280. }).Entity;
  281. db.SaveChanges();
  282. PublicFunction.SplitUserAccountRecord(add);
  283. }
  284. tran.Commit();
  285. }
  286. catch (Exception ex)
  287. {
  288. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到补贴异常");
  289. tran.Rollback();
  290. }
  291. function.WriteLog(index.ToString(), "同步分润数据");
  292. }
  293. db.Dispose();
  294. }
  295. }
  296. }