SycnProfitServiceV2.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 SycnProfitServiceV2
  11. {
  12. public readonly static SycnProfitServiceV2 Instance = new SycnProfitServiceV2();
  13. private SycnProfitServiceV2()
  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);
  39. DoSubsidyProfit(BrandId, date);
  40. }
  41. else if (OpType == 1)
  42. {
  43. DoTradeProfit2(BrandId, date, SysUserName);
  44. DoSubsidyProfit2(BrandId, date);
  45. }
  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)
  60. {
  61. WebCMSEntities db = new WebCMSEntities();
  62. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  63. bool op = true;
  64. while (op)
  65. {
  66. string ids = "";
  67. DataTable idsDt = OtherMySqlConn.dtable("select Id from ProfitRewardRecord where CheckStatus=0 and BrandId=" + BrandId + " and TradeMonth='" + date + "' order by Id limit 500");
  68. if (idsDt.Rows.Count > 0)
  69. {
  70. foreach (DataRow idsDr in idsDt.Rows)
  71. {
  72. ids += idsDr["Id"].ToString() + ",";
  73. }
  74. DataTable dt = OtherMySqlConn.dtable("select UserId,ProfitType,sum(CreditTradeProfit+DebitNonTradeCapProfit+DebitTradeCapProfit+QrCreditTradeProfit) from ProfitRewardRecord where ids in (" + ids.TrimEnd(',') + ") group by UserId,ProfitType");
  75. function.WriteLog("分润:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  76. int index = 0;
  77. foreach (DataRow dr in dt.Rows)
  78. {
  79. index += 1;
  80. int UserId = int.Parse(dr["UserId"].ToString());
  81. ulong ProfitType = ulong.Parse(dr["ProfitType"].ToString());
  82. decimal ProfitMoney = decimal.Parse(dr[2].ToString());
  83. var tran = db.Database.BeginTransaction();
  84. try
  85. {
  86. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  87. ProfitRecord profit = db.ProfitRecord.FirstOrDefault(m => m.UserId == UserId && m.BrandId == BrandId && m.SeoTitle == date);
  88. if (profit == null)
  89. {
  90. profit = db.ProfitRecord.Add(new ProfitRecord()
  91. {
  92. CreateDate = DateTime.Now,
  93. CreateMan = SysUserName,
  94. SeoTitle = date,
  95. ParentNav = user.ParentNav,
  96. BrandId = BrandId,
  97. UserId = UserId,
  98. DirectFlag = ProfitType,
  99. }).Entity;
  100. db.SaveChanges();
  101. }
  102. profit.ProfitAmount += ProfitMoney;
  103. string IdBrand = UserId + "_" + BrandId;
  104. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  105. if (MachineData == null)
  106. {
  107. MachineData = db.UserMachineData.Add(new UserMachineData()
  108. {
  109. IdBrand = IdBrand,
  110. }).Entity;
  111. db.SaveChanges();
  112. }
  113. MachineData.TradeProfit += ProfitMoney;
  114. db.SaveChanges();
  115. tran.Commit();
  116. }
  117. catch (Exception ex)
  118. {
  119. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitType + "," + ProfitMoney + "\n" + ex.ToString(), "同步到余额异常");
  120. tran.Rollback();
  121. }
  122. function.WriteLog(index.ToString(), "同步分润数据");
  123. }
  124. OtherMySqlConn.op("update ProfitRewardRecord set CheckStatus=1 where ids in (" + ids.TrimEnd(',') + ")");
  125. }
  126. else
  127. {
  128. op = false;
  129. }
  130. }
  131. db.Dispose();
  132. }
  133. private void DoTradeProfit2(int BrandId, string date, string SysUserName)
  134. {
  135. WebCMSEntities db = new WebCMSEntities();
  136. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  137. bool op = true;
  138. while (op)
  139. {
  140. DataTable idsDt = OtherMySqlConn.dtable("select Id from ProfitRecord where BrandId=" + BrandId + " and SeoTitle='" + date + "' and Version=0 and UserId>0 order by Id limit 500");
  141. if (idsDt.Rows.Count > 0)
  142. {
  143. string ids = "";
  144. foreach (DataRow idsDr in idsDt.Rows)
  145. {
  146. ids += idsDr["Id"].ToString() + ",";
  147. }
  148. DataTable dt = OtherMySqlConn.dtable("select UserId,DirectFlag,ProfitAmount from ProfitRecord where Id in (" + ids.TrimEnd(',') + ")");
  149. function.WriteLog("分润:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  150. int index = 0;
  151. foreach (DataRow dr in dt.Rows)
  152. {
  153. index += 1;
  154. int UserId = int.Parse(dr["UserId"].ToString());
  155. int DirectFlag = int.Parse(dr["DirectFlag"].ToString());
  156. decimal ProfitAmount = decimal.Parse(dr["ProfitAmount"].ToString());
  157. var tran = db.Database.BeginTransaction();
  158. try
  159. {
  160. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  161. if (account == null)
  162. {
  163. account = db.UserAccount.Add(new UserAccount()
  164. {
  165. Id = UserId,
  166. UserId = UserId,
  167. }).Entity;
  168. db.SaveChanges();
  169. }
  170. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  171. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  172. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  173. account.BalanceAmount += ProfitAmount;
  174. account.TotalAmount += ProfitAmount;
  175. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  176. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  177. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  178. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  179. {
  180. CreateDate = DateTime.Now,
  181. UpdateDate = DateTime.Now,
  182. UserId = UserId, //创客
  183. ProductType = BrandId,
  184. ChangeType = 1, //变动类型
  185. ChangeAmount = ProfitAmount, //变更金额
  186. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  187. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  188. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  189. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  190. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  191. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  192. Remark = DirectFlag == 1 ? "直拓商户分润" : "品牌推广服务费",
  193. }).Entity;
  194. db.SaveChanges();
  195. tran.Commit();
  196. }
  197. catch (Exception ex)
  198. {
  199. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitAmount + "\n" + ex.ToString(), "同步到余额异常");
  200. tran.Rollback();
  201. }
  202. function.WriteLog(index.ToString(), "同步分润数据");
  203. }
  204. OtherMySqlConn.dtable("update ProfitRecord set Version=1 where Id in (" + ids.TrimEnd(',') + ")");
  205. }
  206. else
  207. {
  208. op = false;
  209. }
  210. }
  211. db.Dispose();
  212. }
  213. //分润补贴
  214. private void DoSubsidyProfit(int BrandId, string date)
  215. {
  216. WebCMSEntities db = new WebCMSEntities();
  217. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  218. bool op = true;
  219. while (op)
  220. {
  221. DataTable idsDt = OtherMySqlConn.dtable("select Id from ProfitSubsidyDetail where Status=0 and BrandId=" + BrandId + " and TradeMonth='" + date + "' order by Id limit 500");
  222. if (idsDt.Rows.Count > 0)
  223. {
  224. string ids = "";
  225. foreach (DataRow idsDr in idsDt.Rows)
  226. {
  227. ids += idsDr["Id"].ToString() + ",";
  228. }
  229. DataTable dt = OtherMySqlConn.dtable("select SubsidyUserId,sum(SubsidyProfitRate) from ProfitSubsidyDetail where Id in (" + ids.TrimEnd(',') + ") GROUP BY SubsidyUserId");
  230. function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  231. int index = 0;
  232. foreach (DataRow dr in dt.Rows)
  233. {
  234. index += 1;
  235. int UserId = int.Parse(dr["SubsidyUserId"].ToString());
  236. decimal ProfitMoney = decimal.Parse(dr[1].ToString());
  237. var tran = db.Database.BeginTransaction();
  238. try
  239. {
  240. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  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. tran.Commit();
  254. }
  255. catch (Exception ex)
  256. {
  257. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到补贴异常");
  258. tran.Rollback();
  259. }
  260. function.WriteLog(index.ToString(), "同步分润数据");
  261. }
  262. OtherMySqlConn.dtable("update ProfitSubsidyDetail set Status=1 where Id in (" + ids.TrimEnd(',') + ")");
  263. }
  264. else
  265. {
  266. op = false;
  267. }
  268. }
  269. db.Dispose();
  270. }
  271. private void DoSubsidyProfit2(int BrandId, string date)
  272. {
  273. WebCMSEntities db = new WebCMSEntities();
  274. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  275. bool op = true;
  276. while (op)
  277. {
  278. DataTable idsDt = OtherMySqlConn.dtable("select Id from ProfitSubsidyDetail where Status=1 and BrandId=" + BrandId + " and TradeMonth='" + date + "' order by Id limit 500");
  279. if (idsDt.Rows.Count > 0)
  280. {
  281. string ids = "";
  282. foreach (DataRow idsDr in idsDt.Rows)
  283. {
  284. ids += idsDr["Id"].ToString() + ",";
  285. }
  286. DataTable dt = OtherMySqlConn.dtable("select SubsidyUserId,sum(SubsidyProfitRate) from ProfitSubsidyDetail where Id in (" + ids.TrimEnd(',') + ") GROUP BY SubsidyUserId");
  287. function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  288. int index = 0;
  289. foreach (DataRow dr in dt.Rows)
  290. {
  291. index += 1;
  292. int UserId = int.Parse(dr["SubsidyUserId"].ToString());
  293. decimal ProfitMoney = decimal.Parse(dr[1].ToString());
  294. var tran = db.Database.BeginTransaction();
  295. try
  296. {
  297. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  298. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  299. if (account == null)
  300. {
  301. account = db.UserAccount.Add(new UserAccount()
  302. {
  303. Id = UserId,
  304. UserId = UserId,
  305. }).Entity;
  306. db.SaveChanges();
  307. }
  308. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  309. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  310. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  311. account.BalanceAmount += ProfitMoney;
  312. account.TotalAmount += ProfitMoney;
  313. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  314. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  315. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  316. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  317. {
  318. CreateDate = DateTime.Now,
  319. UpdateDate = DateTime.Now,
  320. UserId = UserId, //创客
  321. ProductType = BrandId,
  322. ChangeType = 111, //变动类型
  323. ChangeAmount = ProfitMoney, //变更金额
  324. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  325. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  326. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  327. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  328. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  329. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  330. Remark = "直拓商户补贴",
  331. }).Entity;
  332. db.SaveChanges();
  333. tran.Commit();
  334. }
  335. catch (Exception ex)
  336. {
  337. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到补贴异常");
  338. tran.Rollback();
  339. }
  340. function.WriteLog(index.ToString(), "同步分润数据");
  341. }
  342. OtherMySqlConn.dtable("update ProfitSubsidyDetail set Status=2 where Id in (" + ids.TrimEnd(',') + ")");
  343. }
  344. else
  345. {
  346. op = false;
  347. }
  348. }
  349. db.Dispose();
  350. }
  351. }
  352. }