Просмотр исходного кода

Merge branch 'fix-创客机具数量问题' into develop

lichunlei 3 лет назад
Родитель
Сommit
bc779e4acc

+ 10 - 0
AppStart/ExcelHelper.cs

@@ -778,6 +778,7 @@ namespace MySystem
                                     {
                                         Size = list.Rows.Count - DoCount;
                                     }
+                                    List<string> opData = new List<string>();
                                     for (int i = DoCount; i < DoCount + Size; i++)
                                     {
                                         DataRow dr = list.Rows[i];
@@ -821,6 +822,10 @@ namespace MySystem
                                                     if (machine.Id > 0)
                                                     {
                                                         function.WriteLog(DateTime.Now.ToString() + "\n" + Newtonsoft.Json.JsonConvert.SerializeObject(machine), "机具回仓库日志");
+                                                        if(!opData.Contains(machine.BuyUserId + ":" + machine.BrandId))
+                                                        {
+                                                            opData.Add(machine.BuyUserId + ":" + machine.BrandId);
+                                                        }
                                                         machine.StoreId = tostorefor.StoreId;
                                                         machine.BuyUserId = 0;
                                                         machine.UserId = 0;
@@ -955,6 +960,11 @@ namespace MySystem
                                         db.SaveChanges();
                                     }
                                     db.SaveChanges();
+                                    foreach(string sub in opData)
+                                    {
+                                        string[] datalist = sub.Split(":");
+                                        PublicFunction.SycnMachineCount(int.Parse(datalist[0]), int.Parse(datalist[1]));
+                                    }
                                     tran.Commit();
                                     RedisDbconn.Instance.Set("CheckImport:" + checkKey, "success|" + SuccessCount);
                                     RedisDbconn.Instance.SetExpire("CheckImport:" + checkKey, 60000);

+ 42 - 13
AppStart/PublicFunction.cs

@@ -419,22 +419,39 @@ namespace MySystem
 
         public static void SycnMachineCount(int Id = 0, int BrandId = 0)
         {
-            WebCMSEntities dbnew = new WebCMSEntities();
-            string IdBrand = Id + "_" + BrandId;
-            UserMachineData machineData = dbnew.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
-            if (machineData == null)
+            RedisDbconn.Instance.AddList("SycnUserMachineCountQueue", "{\"UserId\":\"" + Id + "\",\"BrandId\":\"" + BrandId + "\"}");
+        }
+
+        #endregion
+
+        #region 统计机具绑定数据
+
+        public static void StatUserMachineData(int UserId, int BrandId, int Count)
+        {
+            using (WebCMSEntities db = new WebCMSEntities())
             {
-                machineData = dbnew.UserMachineData.Add(new UserMachineData()
+                string IdBrand = UserId + "_" + BrandId;
+                UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
+                if (MachineData == null)
                 {
-                    IdBrand = IdBrand
-                }).Entity;
-                dbnew.SaveChanges();
+                    MachineData = db.UserMachineData.Add(new UserMachineData()
+                    {
+                        IdBrand = IdBrand,
+                    }).Entity;
+                    db.SaveChanges();
+                }
+                if(Count < 0)
+                {
+                    MachineData.TotalMachineCount -= Math.Abs(Count);
+                    MachineData.UnBindCount -= Math.Abs(Count);
+                }
+                else
+                {
+                    MachineData.TotalMachineCount += Count;
+                    MachineData.UnBindCount += Count;
+                }
+                db.SaveChanges();
             }
-            machineData.BindCount = dbnew.PosMachinesTwo.Count(m => m.Status > -1 && m.BuyUserId == Id && m.BrandId == BrandId && m.BindingState == 1);
-            machineData.UnBindCount = dbnew.PosMachinesTwo.Count(m => m.Status > -1 && m.BuyUserId == Id && m.BrandId == BrandId && m.BindingState == 0);
-            machineData.TotalMachineCount = machineData.BindCount + machineData.UnBindCount;
-            dbnew.SaveChanges();
-            dbnew.Dispose();
         }
 
         #endregion
@@ -448,6 +465,17 @@ namespace MySystem
             {
                 prepos.Status = -1;
                 prepos.ApplyFlag = 0;
+                SmallStoreHouse sstore = db.SmallStoreHouse.FirstOrDefault(m => m.UserId == prepos.ToUserId);
+                if(sstore != null)
+                {
+                    sstore.LaveNum += 1;
+                    sstore.TotalNum -= 1;
+                }
+                StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.UserId == prepos.FromStoreId);
+                if(store != null)
+                {
+                    store.LaveNum += 1;
+                }
                 // db.SaveChanges();
             }
             MachineApply apply = db.MachineApply.FirstOrDefault(m => m.SwapSnExpand.Contains(PosSn));
@@ -456,6 +484,7 @@ namespace MySystem
                 apply.SwapSnExpand = apply.SwapSnExpand.Replace(PosSn, PosSn.Substring(0, 3) + "d" + PosSn.Substring(4));
                 // db.SaveChanges();
             }
+
         }
 
         #endregion

+ 58 - 0
AppStart/SycnUserMachineCountHelper.cs

@@ -0,0 +1,58 @@
+using System;
+using System.Linq;
+using System.Threading;
+using MySystem.Models;
+using LitJson;
+
+
+namespace MySystem
+{
+    public class SycnUserMachineCountHelper
+    {
+        public readonly static SycnUserMachineCountHelper Instance = new SycnUserMachineCountHelper();
+        private SycnUserMachineCountHelper()
+        {
+        }
+
+        public void Start()//启动
+        {
+            Thread thread = new Thread(StartDo);
+            thread.IsBackground = true;
+            thread.Start();
+        }
+
+        public void StartDo()
+        {
+            while (true)
+            {
+                string data = RedisDbconn.Instance.RPop<string>("SycnUserMachineCountQueue");
+                if (!string.IsNullOrEmpty(data))
+                {
+                    JsonData json = JsonMapper.ToObject(data);
+                    int UserId = int.Parse(json["UserId"].ToString());
+                    int BrandId = int.Parse(json["BrandId"].ToString());
+                    WebCMSEntities dbnew = new WebCMSEntities();
+                    string IdBrand = UserId + "_" + BrandId;
+                    UserMachineData machineData = dbnew.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
+                    if (machineData == null)
+                    {
+                        machineData = dbnew.UserMachineData.Add(new UserMachineData()
+                        {
+                            IdBrand = IdBrand
+                        }).Entity;
+                        dbnew.SaveChanges();
+                    }
+                    machineData.BindCount = dbnew.PosMachinesTwo.Count(m => m.Status > -1 && m.BuyUserId == UserId && m.BrandId == BrandId && m.BindingState == 1);
+                    machineData.UnBindCount = dbnew.PosMachinesTwo.Count(m => m.Status > -1 && m.BuyUserId == UserId && m.BrandId == BrandId && m.BindingState == 0);
+                    machineData.TotalMachineCount = machineData.BindCount + machineData.UnBindCount;
+                    dbnew.SaveChanges();
+                    dbnew.Dispose();
+                }
+                else
+                {
+                    Thread.Sleep(10000);
+                }
+            }
+        }
+    }
+}

+ 16 - 0
Areas/Admin/Controllers/MainServer/StoreHouseController.cs

@@ -919,6 +919,7 @@ namespace MySystem.Areas.Admin.Controllers
                 string error = "";
                 decimal amount = 0;
                 List<string> PosSnList = new List<string>();
+                List<string> opData = new List<string>();
                 for (int i = 1; i < list.Count; i++)
                 {
                     JsonData dr = list[i];
@@ -1045,6 +1046,11 @@ namespace MySystem.Areas.Admin.Controllers
                                     AfterOutNum = toStore.OutNum, //操作后出库数
                                 }).Entity;
                                 db.SaveChanges();
+                                
+                                if(!opData.Contains(posInfo.BuyUserId + ":" + posInfo.BrandId))
+                                {
+                                    opData.Add(posInfo.BuyUserId + ":" + posInfo.BrandId);
+                                }
 
                                 posInfo.StoreId = toStore.Id;
                                 posInfo.BuyUserId = 0;
@@ -1135,6 +1141,11 @@ namespace MySystem.Areas.Admin.Controllers
                                 }).Entity;
                                 db.SaveChanges();
 
+                                if(!opData.Contains(posInfo.BuyUserId + ":" + posInfo.BrandId))
+                                {
+                                    opData.Add(posInfo.BuyUserId + ":" + posInfo.BrandId);
+                                }
+
                                 posInfo.StoreId = toStore.Id;
                                 posInfo.BuyUserId = 0;
                                 posInfo.UserId = 0;
@@ -1174,6 +1185,11 @@ namespace MySystem.Areas.Admin.Controllers
                 {
                     return "Warning|" + error;
                 }
+                foreach(string sub in opData)
+                {
+                    string[] datalist = sub.Split(":");
+                    PublicFunction.SycnMachineCount(int.Parse(datalist[0]), int.Parse(datalist[1]));
+                }
             }
             AddSysLog("0", "MachinesRejectStore", "Import");
             return "success";

+ 45 - 7
Areas/Admin/Controllers/MainServer/SysToolsController.cs

@@ -1419,6 +1419,10 @@ namespace MySystem.Areas.Admin.Controllers
             {
                 return "请输入新机具SN";
             }
+            if (string.IsNullOrEmpty(MerNo))
+            {
+                return "请输入商户编号";
+            }
             string[] OldSnList = OldSn.Split('\n');
             string[] NewSnList = NewSn.Split('\n');
             if (OldSnList.Length != NewSnList.Length)
@@ -1482,15 +1486,12 @@ namespace MySystem.Areas.Admin.Controllers
                 }
                 oldPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == oldpos.BrandId) ?? new KqProducts();
                 newPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == newpos.BrandId) ?? new KqProducts();
-                PosMerchantInfo merchant = new PosMerchantInfo();
-                if (!string.IsNullOrEmpty(MerNo))
-                {
-                    merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == MerNo) ?? new PosMerchantInfo();
-                }
-                else
+                PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == MerNo);
+                if (merchant == null)
                 {
-                    merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == oldpos.BindMerchantId) ?? new PosMerchantInfo();
+                    CustomerSqlConn.op("insert into PosMerchantInfo select * from PosMerchantInfoBak where KqMerNo='" + MerNo + "'", MysqlConn.connstr);
                 }
+                merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == MerNo) ?? new PosMerchantInfo();
                 newpos.BindMerchantId = merchant.Id;
                 newpos.BuyUserId = oldpos.BuyUserId;
                 newpos.UserId = oldpos.UserId;
@@ -1601,6 +1602,7 @@ namespace MySystem.Areas.Admin.Controllers
             string[] OldSnList;
             string[] NewSnList;
             decimal amount = 0;
+            List<string> opData = new List<string>();
             //创客坏机换新
             if (ChangeType == "0")
             {
@@ -1715,6 +1717,11 @@ namespace MySystem.Areas.Admin.Controllers
                     newpos.UserId = oldpos.UserId;
                     newpos.PreUserId = oldpos.PreUserId;
 
+                    if(!opData.Contains(oldpos.BuyUserId + ":" + oldpos.BrandId))
+                    {
+                        opData.Add(oldpos.BuyUserId + ":" + oldpos.BrandId);
+                    }
+
                     oldpos.StoreId = storeBack.Id;
                     oldpos.QueryCount = 0;
                     oldpos.UpdateDate = null;
@@ -1904,6 +1911,11 @@ namespace MySystem.Areas.Admin.Controllers
                     newpos.IsFirst = oldpos.IsFirst;
                     newpos.TransferTime = oldpos.TransferTime;
 
+                    if(!opData.Contains(oldpos.BuyUserId + ":" + oldpos.BrandId))
+                    {
+                        opData.Add(oldpos.BuyUserId + ":" + oldpos.BrandId);
+                    }
+
                     oldpos.StoreId = storeBack.Id;
                     oldpos.QueryCount = 0;
                     oldpos.UpdateDate = null;
@@ -2097,6 +2109,11 @@ namespace MySystem.Areas.Admin.Controllers
             //         }));
             //     }
             // }
+            foreach(string sub in opData)
+            {
+                string[] datalist = sub.Split(":");
+                PublicFunction.SycnMachineCount(int.Parse(datalist[0]), int.Parse(datalist[1]));
+            }
             return "success";
         }
         #endregion
@@ -4078,6 +4095,7 @@ namespace MySystem.Areas.Admin.Controllers
             string[] OldSnList;
             OldSnList = OldSn.Replace("\r", "").Split('\n');
             string error = "";
+            List<string> opData = new List<string>();
             for (int i = 0; i < OldSnList.Length; i++)
             {
                 string OldSnNum = OldSnList[i];
@@ -4088,6 +4106,10 @@ namespace MySystem.Areas.Admin.Controllers
                 {
                     error += "以下操作失败" + OldSnNum + ',' + "该机具不在坏机仓或不存在" + '\n';
                 }
+                if(!opData.Contains(oldpos.BuyUserId + ":" + oldpos.BrandId))
+                {
+                    opData.Add(oldpos.BuyUserId + ":" + oldpos.BrandId);
+                }
                 oldpos.Status = 1;
                 oldpos.QueryCount = 0;
                 oldpos.UpdateDate = null;
@@ -4135,6 +4157,11 @@ namespace MySystem.Areas.Admin.Controllers
                 return "Warning|" + error;
             }
             db.SaveChanges();
+            foreach(string sub in opData)
+            {
+                string[] datalist = sub.Split(":");
+                PublicFunction.SycnMachineCount(int.Parse(datalist[0]), int.Parse(datalist[1]));
+            }
             return "success";
         }
         #endregion
@@ -4249,6 +4276,7 @@ namespace MySystem.Areas.Admin.Controllers
             var endTime = time.AddDays(20 - time.Day).AddDays(-60);
             StoreForCode storeForBack = new StoreForCode();
             StoreHouse BackStore = new StoreHouse();
+            List<string> opData = new List<string>();
             foreach (var item in PosSnList)
             {
                 MachineForSnNo machineForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == item) ?? new MachineForSnNo();
@@ -4390,6 +4418,11 @@ namespace MySystem.Areas.Admin.Controllers
                     var userMachineDatas = db.UserMachineData.FirstOrDefault(m => m.IdBrand == fInfo) ?? new UserMachineData();
                     userMachineDatas.TotalMachineCount -= 1;
                     userMachineDatas.UnBindCount -= 1;
+                    
+                    if(!opData.Contains(pos.BuyUserId + ":" + pos.BrandId))
+                    {
+                        opData.Add(pos.BuyUserId + ":" + pos.BrandId);
+                    }
 
                     pos.StoreId = BackStore.Id;//变更机具所属仓库,重置为仓库机
                     pos.Status = 1;
@@ -4471,6 +4504,11 @@ namespace MySystem.Areas.Admin.Controllers
                 }
             }
             db.SaveChanges();
+            foreach(string sub in opData)
+            {
+                string[] datalist = sub.Split(":");
+                PublicFunction.SycnMachineCount(int.Parse(datalist[0]), int.Parse(datalist[1]));
+            }
             return "success";
         }
         #endregion

+ 16 - 0
Areas/Admin/Controllers/OperateServer/StoreHouseOperateController.cs

@@ -644,6 +644,7 @@ namespace MySystem.Areas.Admin.Controllers
                 string error = "";
                 decimal amount = 0;
                 List<string> PosSnList = new List<string>();
+                List<string> opData = new List<string>();
                 for (int i = 1; i < list.Count; i++)
                 {
                     JsonData dr = list[i];
@@ -770,6 +771,11 @@ namespace MySystem.Areas.Admin.Controllers
                                 }).Entity;
                                 db.SaveChanges();
 
+                                if(!opData.Contains(posInfo.BuyUserId + ":" + posInfo.BrandId))
+                                {
+                                    opData.Add(posInfo.BuyUserId + ":" + posInfo.BrandId);
+                                }
+
                                 posInfo.StoreId = toStore.Id;
                                 posInfo.BuyUserId = 0;
                                 posInfo.UserId = 0;
@@ -858,6 +864,11 @@ namespace MySystem.Areas.Admin.Controllers
                                 }).Entity;
                                 db.SaveChanges();
 
+                                if(!opData.Contains(posInfo.BuyUserId + ":" + posInfo.BrandId))
+                                {
+                                    opData.Add(posInfo.BuyUserId + ":" + posInfo.BrandId);
+                                }
+
                                 posInfo.StoreId = toStore.Id;
                                 posInfo.BuyUserId = 0;
                                 posInfo.UserId = 0;
@@ -897,6 +908,11 @@ namespace MySystem.Areas.Admin.Controllers
                 {
                     return "Warning|" + error;
                 }
+                foreach(string sub in opData)
+                {
+                    string[] datalist = sub.Split(":");
+                    PublicFunction.SycnMachineCount(int.Parse(datalist[0]), int.Parse(datalist[1]));
+                }
             }
             AddSysLog("0", "MachinesRejectStore", "Import");
             return "success";

+ 1 - 1
Startup.cs

@@ -140,7 +140,7 @@ namespace MySystem
             SycnHelpProfitService.Instance.Start();
             ExcelHelper.Instance.Start();
             OpExcelHelper.Instance.Start();
-            TestHelper.Instance.Start();
+            SycnUserMachineCountHelper.Instance.Start(); //重置创客机具数量
         }