Преглед изворни кода

完成分润补贴展示和到账优化

lichunlei пре 3 година
родитељ
комит
e2367d2515
1 измењених фајлова са 63 додато и 49 уклоњено
  1. 63 49
      AppStart/Helper/SycnProfitServiceV2.cs

+ 63 - 49
AppStart/Helper/SycnProfitServiceV2.cs

@@ -182,6 +182,8 @@ namespace MySystem
             WebCMSEntities db = new WebCMSEntities();
             OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
             
+            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");
+
             DataTable dt = OtherMySqlConn.dtable("select SubsidyUserId,sum(SubsidyProfitRate) from ProfitSubsidyDetail where Status=0 and BrandId=" + BrandId + " and TradeMonth='" + date + "' GROUP BY SubsidyUserId");
             function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步补贴数据");
             int index = 0;
@@ -221,64 +223,76 @@ namespace MySystem
         {
             WebCMSEntities db = new WebCMSEntities();
             OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
-            
-            DataTable dt = OtherMySqlConn.dtable("select SubsidyUserId,sum(SubsidyProfitRate) from ProfitSubsidyDetail where Status=1 and BrandId=" + BrandId + " and TradeMonth='" + date + "' GROUP BY SubsidyUserId");
-            function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步补贴数据");
-            int index = 0;
-            foreach (DataRow dr in dt.Rows)
+            int startid = 0;
+            bool op = true;
+            while(op)
             {
-                index += 1;
-                int UserId = int.Parse(dr["SubsidyUserId"].ToString());
-                decimal ProfitMoney = decimal.Parse(dr[1].ToString());
-                var tran = db.Database.BeginTransaction();
-                try
+                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");
+                if(dt.Rows.Count > 0)
                 {
-                    Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
-                    UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
-                    if (account == null)
+                    function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步补贴数据");
+                    int index = 0;
+                    foreach (DataRow dr in dt.Rows)
                     {
-                        account = db.UserAccount.Add(new UserAccount()
+                        index += 1;
+                        int UserId = int.Parse(dr["UserId"].ToString());
+                        decimal ProfitMoney = decimal.Parse(dr["ProfitAmount"].ToString());
+                        var tran = db.Database.BeginTransaction();
+                        try
                         {
-                            Id = UserId,
-                            UserId = UserId,
-                        }).Entity;
-                        db.SaveChanges();
+                            Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
+                            UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
+                            if (account == null)
+                            {
+                                account = db.UserAccount.Add(new UserAccount()
+                                {
+                                    Id = UserId,
+                                    UserId = UserId,
+                                }).Entity;
+                                db.SaveChanges();
+                            }
+                            decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
+                            decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
+                            decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
+                            account.BalanceAmount += ProfitMoney;
+                            account.TotalAmount += ProfitMoney;
+                            decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
+                            decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
+                            decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
+                            UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
+                            {
+                                CreateDate = DateTime.Now,
+                                UpdateDate = DateTime.Now,
+                                UserId = UserId, //创客
+                                ProductType = BrandId,
+                                ChangeType = 111, //变动类型
+                                ChangeAmount = ProfitMoney, //变更金额
+                                BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
+                                AfterTotalAmount = AfterTotalAmount, //变更后总金额
+                                BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
+                                AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
+                                BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
+                                AfterBalanceAmount = AfterBalanceAmount, //变更后余额
+                                Remark = "直拓商户补贴",
+                            }).Entity;
+                            db.SaveChanges();
+                            tran.Commit();
+                        }
+                        catch (Exception ex)
+                        {
+                            function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到补贴异常");
+                            tran.Rollback();
+                        }
+                        startid = int.Parse(dr["Id"].ToString());
+                        function.WriteLog(index.ToString(), "同步补贴数据");
                     }
-                    decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
-                    decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
-                    decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
-                    account.BalanceAmount += ProfitMoney;
-                    account.TotalAmount += ProfitMoney;
-                    decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
-                    decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
-                    decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
-                    UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
-                    {
-                        CreateDate = DateTime.Now,
-                        UpdateDate = DateTime.Now,
-                        UserId = UserId, //创客
-                        ProductType = BrandId,
-                        ChangeType = 111, //变动类型
-                        ChangeAmount = ProfitMoney, //变更金额
-                        BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
-                        AfterTotalAmount = AfterTotalAmount, //变更后总金额
-                        BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
-                        AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
-                        BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
-                        AfterBalanceAmount = AfterBalanceAmount, //变更后余额
-                        Remark = "直拓商户补贴",
-                    }).Entity;
-                    db.SaveChanges();
-                    tran.Commit();
                 }
-                catch (Exception ex)
+                else
                 {
-                    function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到补贴异常");
-                    tran.Rollback();
+                    op = false;
                 }
-                function.WriteLog(index.ToString(), "同步补贴数据");
             }
-            OtherMySqlConn.dtable("update ProfitSubsidyDetail set Status=2 where Status=1 and BrandId=" + BrandId + " and TradeMonth='" + date + "'");
+            OtherMySqlConn.dtable("update SubsidyRecord set Version=1 where BrandId=" + BrandId + " and SeoTitle='" + date + "' and Version=0 and UserId>0");
                 
             db.Dispose();
         }