SycnProfitServiceV2.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. DataTable dt = OtherMySqlConn.dtable("select UserId,DirectFlag,ProfitAmount from ProfitRecord where BrandId=" + BrandId + " and SeoTitle='" + date + "' and Version=0 and UserId>0");
  101. function.WriteLog("分润:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  102. int index = 0;
  103. foreach (DataRow dr in dt.Rows)
  104. {
  105. index += 1;
  106. int UserId = int.Parse(dr["UserId"].ToString());
  107. int DirectFlag = int.Parse(dr["DirectFlag"].ToString());
  108. decimal ProfitAmount = decimal.Parse(dr["ProfitAmount"].ToString());
  109. var tran = db.Database.BeginTransaction();
  110. try
  111. {
  112. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  113. if (account == null)
  114. {
  115. account = db.UserAccount.Add(new UserAccount()
  116. {
  117. Id = UserId,
  118. UserId = UserId,
  119. }).Entity;
  120. db.SaveChanges();
  121. }
  122. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  123. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  124. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  125. account.BalanceAmount += ProfitAmount;
  126. account.TotalAmount += ProfitAmount;
  127. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  128. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  129. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  130. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  131. {
  132. CreateDate = DateTime.Now,
  133. UpdateDate = DateTime.Now,
  134. UserId = UserId, //创客
  135. ProductType = BrandId,
  136. ChangeType = 1, //变动类型
  137. ChangeAmount = ProfitAmount, //变更金额
  138. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  139. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  140. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  141. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  142. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  143. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  144. Remark = DirectFlag == 1 ? "直拓商户分润" : "品牌推广服务费",
  145. }).Entity;
  146. db.SaveChanges();
  147. tran.Commit();
  148. }
  149. catch (Exception ex)
  150. {
  151. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitAmount + "\n" + ex.ToString(), "同步到余额异常");
  152. tran.Rollback();
  153. }
  154. function.WriteLog(index.ToString(), "同步分润数据");
  155. }
  156. OtherMySqlConn.dtable("update ProfitRecord set Version=1 where BrandId=" + BrandId + " and SeoTitle='" + date + "' and Version=0 and UserId>0");
  157. db.Dispose();
  158. }
  159. //分润补贴
  160. private void DoSubsidyProfit(int BrandId, string date)
  161. {
  162. WebCMSEntities db = new WebCMSEntities();
  163. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  164. DataTable dt = OtherMySqlConn.dtable("select SubsidyUserId,sum(SubsidyProfitRate) from ProfitSubsidyDetail where Status=0 and BrandId=" + BrandId + " and TradeMonth='" + date + "' GROUP BY SubsidyUserId");
  165. function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步补贴数据");
  166. int index = 0;
  167. foreach (DataRow dr in dt.Rows)
  168. {
  169. index += 1;
  170. int UserId = int.Parse(dr["SubsidyUserId"].ToString());
  171. decimal ProfitMoney = decimal.Parse(dr[1].ToString());
  172. try
  173. {
  174. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  175. string IdBrand = UserId + "_" + BrandId;
  176. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  177. if (MachineData == null)
  178. {
  179. MachineData = db.UserMachineData.Add(new UserMachineData()
  180. {
  181. IdBrand = IdBrand,
  182. }).Entity;
  183. db.SaveChanges();
  184. }
  185. MachineData.OtherProfit += ProfitMoney;
  186. db.SaveChanges();
  187. }
  188. catch (Exception ex)
  189. {
  190. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到补贴异常");
  191. }
  192. function.WriteLog(index.ToString(), "同步补贴数据");
  193. }
  194. OtherMySqlConn.dtable("update ProfitSubsidyDetail set Status=1 where Status=0 and BrandId=" + BrandId + " and TradeMonth='" + date + "'");
  195. db.Dispose();
  196. }
  197. private void DoSubsidyProfit2(int BrandId, string date)
  198. {
  199. WebCMSEntities db = new WebCMSEntities();
  200. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  201. DataTable dt = OtherMySqlConn.dtable("select SubsidyUserId,sum(SubsidyProfitRate) from ProfitSubsidyDetail where Status=1 and BrandId=" + BrandId + " and TradeMonth='" + date + "' GROUP BY SubsidyUserId");
  202. function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步补贴数据");
  203. int index = 0;
  204. foreach (DataRow dr in dt.Rows)
  205. {
  206. index += 1;
  207. int UserId = int.Parse(dr["SubsidyUserId"].ToString());
  208. decimal ProfitMoney = decimal.Parse(dr[1].ToString());
  209. var tran = db.Database.BeginTransaction();
  210. try
  211. {
  212. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  213. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  214. if (account == null)
  215. {
  216. account = db.UserAccount.Add(new UserAccount()
  217. {
  218. Id = UserId,
  219. UserId = UserId,
  220. }).Entity;
  221. db.SaveChanges();
  222. }
  223. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  224. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  225. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  226. account.BalanceAmount += ProfitMoney;
  227. account.TotalAmount += ProfitMoney;
  228. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  229. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  230. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  231. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  232. {
  233. CreateDate = DateTime.Now,
  234. UpdateDate = DateTime.Now,
  235. UserId = UserId, //创客
  236. ProductType = BrandId,
  237. ChangeType = 111, //变动类型
  238. ChangeAmount = ProfitMoney, //变更金额
  239. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  240. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  241. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  242. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  243. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  244. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  245. Remark = "直拓商户补贴",
  246. }).Entity;
  247. db.SaveChanges();
  248. tran.Commit();
  249. }
  250. catch (Exception ex)
  251. {
  252. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到补贴异常");
  253. tran.Rollback();
  254. }
  255. function.WriteLog(index.ToString(), "同步补贴数据");
  256. }
  257. OtherMySqlConn.dtable("update ProfitSubsidyDetail set Status=2 where Status=1 and BrandId=" + BrandId + " and TradeMonth='" + date + "'");
  258. db.Dispose();
  259. }
  260. }
  261. }