SycnProfitServiceV2.cs 18 KB

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