SycnProfitServiceV2.cs 20 KB

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