Forráskód Böngészése

1、修复副规格不更新问题

MY 1 hónapja
szülő
commit
b6af3c3a0d

+ 1 - 1
JLHHJSvr/Excutor/ReCalculateBedNetNoAuditExcutor.cs

@@ -52,7 +52,7 @@ namespace JLHHJSvr.Excutor
 
                     if (bednet.deptid <= 0) throw new LJCommonException("存在报价单部门id错误,不能重算!");
 
-                    var dept = helper.Cache.GetData<int,u_dept,DeptMapping>(bednet.deptid.Value);
+                    var dept = helper.Cache.GetData<u_dept, DeptMapping>(bednet.deptid.Value);
 
                     if (dept.pricelistid <= 0) throw new LJCommonException("存在报价单部门pricelistid错误,不能重算!");
 

+ 1 - 1
JLHHJSvr/Excutor/ReCalculateERPCostExcutor.cs

@@ -56,7 +56,7 @@ namespace JLHHJSvr.Excutor
                     //if (mattress.flag == 1) throw new LJCommonException("存在报价单已审核,不能重算!");
                     if (mattress.deptid <= 0) throw new LJCommonException("存在报价单部门id错误,不能重算!");
 
-                    var dept = helper.Cache.GetData<int,u_dept,DeptMapping>(mattress.deptid.Value);
+                    var dept = helper.Cache.GetData<u_dept,DeptMapping>(mattress.deptid.Value);
                     if (dept.pricelistid <= 0) throw new LJCommonException("存在报价单部门pricelistid错误,不能重算!");
                 }
                 

+ 1 - 1
JLHHJSvr/Excutor/ReCalculateNoAuditExcutor.cs

@@ -52,7 +52,7 @@ namespace JLHHJSvr.Excutor
 
                     if (mattress.deptid <= 0) throw new LJCommonException("存在报价单部门id错误,不能重算!");
 
-                    var dept = helper.Cache.GetData<int,u_dept,DeptMapping>(mattress.deptid.Value);
+                    var dept = helper.Cache.GetData<u_dept,DeptMapping>(mattress.deptid.Value);
 
                     if (dept.pricelistid <= 0) throw new LJCommonException("存在报价单部门pricelistid错误,不能重算!");
 

+ 9 - 9
JLHHJSvr/Helper/BedNetHelper.cs

@@ -177,7 +177,7 @@ namespace JLHHJSvr.Helper
 
             foreach (var mx in mxlist)
             {
-                var spring = Cache.GetData<int,u_spring,SpringMapping>(mx.springid.Value);
+                var spring = Cache.GetData<u_spring,SpringMapping>(mx.springid.Value);
 
                 // 处理相同公式部分
                 InitMxReplaceMents(bednet, mx, spring);
@@ -1133,7 +1133,7 @@ namespace JLHHJSvr.Helper
 
         private void InitBedNet(u_bednet bednet, Boolean isCalBed = true)
         {
-            var dept = Cache.GetData<int,u_dept,DeptMapping>(bednet.deptid.Value);
+            var dept = Cache.GetData<u_dept,DeptMapping>(bednet.deptid.Value);
 
             //var profirate = new u_factory_profitrate() { deptid = bednet.deptid, bednettypeid_mattresstypeid = bednet.bednettypeid, bednet_or_mattress = 0 };
             //DbSqlHelper.SelectOne(cmd, profirate, "profitrate");
@@ -1399,7 +1399,7 @@ namespace JLHHJSvr.Helper
             AddKeyValue("边铁条数", mx.side_iron_qty);
             AddKeyValue("条数", mx.side_iron_qty);
 
-            var spring = Cache.GetData<int,u_spring,SpringMapping>(mx2.springid.Value);
+            var spring = Cache.GetData<u_spring,SpringMapping>(mx2.springid.Value);
 
             spring.gram_weight /= 1000;
             if (spring.gram_weight > 0)
@@ -1444,7 +1444,7 @@ namespace JLHHJSvr.Helper
         /// <param name="mx"></param>
         private void CalSpringMtrlCol2(u_bednet bednet, u_bednetmx mx, List<u_bednetmx_spring> springList, string type)
         {
-            var spring = Cache.GetData<int,u_spring,SpringMapping>(mx.springid.Value);
+            var spring = Cache.GetData<u_spring, SpringMapping>(mx.springid.Value);
 
             #region 计算 底面无纺布 开始
             //计算 上下无纺布方数 =(B4+C4)*(B4+D4)/10000
@@ -1467,7 +1467,7 @@ namespace JLHHJSvr.Helper
                 {
                     _springid = springList[0].springid;
                 }
-                var _spring = Cache.GetData<int,u_spring,SpringMapping>(_springid.Value);
+                var _spring = Cache.GetData<u_spring, SpringMapping>(_springid.Value);
 
                 spring.center_diameter = _spring.center_diameter == null ? 0 : _spring.center_diameter;
 
@@ -1659,7 +1659,7 @@ namespace JLHHJSvr.Helper
             var spring = new u_spring() { springid = springId };
             if (line_diameter == 0)
             {
-                spring = Cache.GetData<int,u_spring,SpringMapping>(springId.Value);
+                spring = Cache.GetData<u_spring, SpringMapping>(springId.Value);
                 AddKeyValue(springName + "弹簧重/个", spring.gram_weight / 1000);
                 AddKeyValue(springName + "弹簧重", spring.gram_weight / 1000);
             } 
@@ -2479,7 +2479,7 @@ namespace JLHHJSvr.Helper
             var mxList = GetBedNetMxList(bednetid);
             var springList = GetBedNetSpringList(bednetid);
             
-            var dept = Cache.GetData<int,u_dept,DeptMapping>(bednet.deptid.Value); ;
+            var dept = Cache.GetData<u_dept, DeptMapping>(bednet.deptid.Value); ;
 
             if(bednet.deptid == null || bednet.deptid.Value <= 0)
             {
@@ -2915,7 +2915,7 @@ namespace JLHHJSvr.Helper
         {
             //var spring = new u_spring() { springid = springid };
             //DbSqlHelper.SelectOne(cmd, spring, "line_diameter,gram_weight,height,center_diameter,cyclenum,caliber");
-            var spring = Cache.GetData<int,u_spring,SpringMapping>(springid);
+            var spring = Cache.GetData<u_spring, SpringMapping>(springid);
             return spring.name;
         }
 
@@ -2953,7 +2953,7 @@ namespace JLHHJSvr.Helper
                     //多区
                     _springid = springList[0].springid;
                 }
-                var spring = Cache.GetData<int,u_spring,SpringMapping>(_springid.Value);
+                var spring = Cache.GetData<u_spring, SpringMapping>(_springid.Value);
 
                 // 处理相同公式部分
                 InitMxReplaceMents(bednet, mx, spring);

+ 23 - 56
JLHHJSvr/Helper/CacheHelper.cs

@@ -7,6 +7,8 @@ using System;
 using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.Data.SqlClient;
+using System.Linq;
+using System.Reflection;
 namespace JLHHJSvr.Helper
 {
     internal sealed class CacheHelper : HelperBase
@@ -40,70 +42,35 @@ namespace JLHHJSvr.Helper
 
         #region 获取数据
         private static readonly Dictionary<Type, object> _cacheServiceRegistry = new Dictionary<Type, object>();
-        public TEntity GetData<TKey, TEntity, TMapping>(TKey key) where TEntity: class,new() where TMapping : ICacheMapping<TKey, TEntity>, new()
+        
+        public TEntity GetData<TEntity,TMapping>(object key)
+            where TEntity : class, new() 
+            where TMapping : new()
         {
-            var entityType = typeof(TEntity);
-            if (!_cacheServiceRegistry.TryGetValue(entityType, out var serviceObj))
-            {
-                var cacheService = new CacheService<TKey, TEntity>(new GenericLoader<TKey, TEntity>(new TMapping()));
-                _cacheServiceRegistry.Add(entityType, cacheService);
-                serviceObj = cacheService;
-            }
+            var mappingType = typeof(TMapping);
 
-            var service = (CacheService<TKey, TEntity>)serviceObj;
-            return service.Get(cmd,key);
-        }
-        #endregion
-    }
+            var iface = mappingType.GetInterfaces()
+                .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICacheMapping<,>));
+            if (iface == null) throw new InvalidOperationException($"{mappingType.Name} must implement ICacheMapping<TKey, TValue>");
 
-    #region 核心
-    public interface ICacheLoader<TKey, TValue> where TValue : class, new()
-    {
-        TValue Load(SqlCommand cmd, TKey key);
-    }
-    public interface ICacheMapping<TKey, TValue> where TValue : class, new()
-    {
-        TValue GetInstance(SqlCommand cmd, TKey key);
-    }
-    public class CacheService<TKey, TValue> where TValue : class, new()
-    {
-        private readonly LJCache<TKey, TValue> _cache;
-        private readonly ICacheLoader<TKey, TValue> _loader;
+            var genericArgs = iface.GetGenericArguments();
+            var keyType = genericArgs[0];
 
-        public CacheService(ICacheLoader<TKey, TValue> loader, int cacheMinutes)
-        {
-            _loader = loader;
-            _cache = new LJCache<TKey, TValue> { DefaultAddMinutes = cacheMinutes };
-        }
+            // 找到私有的强类型实现方法并用 MakeGenericMethod 构造
+            var method = typeof(CacheHelper).GetMethod(nameof(GetDataInternal), BindingFlags.Instance | BindingFlags.NonPublic);
+            var gm = method.MakeGenericMethod(keyType, typeof(TEntity), mappingType);
 
-        public CacheService(ICacheLoader<TKey, TValue> loader)
-        {
-            _loader = loader;
-            _cache = new LJCache<TKey, TValue> { DefaultAddMinutes = 120 };
+            // 调用并返回(Invoke 返回 object)
+            return (TEntity) gm.Invoke(this, new object[] { key });
         }
 
-        public TValue Get(SqlCommand cmd,TKey key)
+        private TValue GetDataInternal<TKey, TValue, TMapping>(object key)
+            where TMapping : ICacheMapping<TKey, TValue>, new()
+            where TValue : class, new()
         {
-            if (!_cache.TryGetValue(key, out TValue value))
-            {
-                value = _loader.Load(cmd, key);
-                _cache.Add(key, value);
-            }
-            return ObjectHelper.DeepCopy(value);
-        }
-    }
-
-    public class GenericLoader<TKey, TValue> : ICacheLoader<TKey, TValue> where TValue : class, new()
-    {
-        private readonly ICacheMapping<TKey, TValue> _mapping;
-        public GenericLoader(ICacheMapping<TKey, TValue> mapping)
-        {
-            _mapping = mapping;
-        }
-        public TValue Load(SqlCommand cmd, TKey id)
-        {
-            return _mapping.GetInstance(cmd, id);
+            var mapping = new TMapping();
+            return mapping.GetInstance(cmd, (TKey)key);
         }
+        #endregion
     }
-    #endregion
 }

+ 22 - 22
JLHHJSvr/Helper/MattressHelper.cs

@@ -395,7 +395,7 @@ namespace JLHHJSvr.Helper
         }
         private void InitMattress(u_mattress mattress)
         {
-            var dept = Cache.GetData<int,u_dept,DeptMapping>(mattress.deptid.Value);
+            var dept = Cache.GetData<u_dept, DeptMapping>(mattress.deptid.Value);
             //var profirate = new u_factory_profitrate() { deptid = mattress.deptid, bednettypeid_mattresstypeid = mattress.mattresstypeid, bednet_or_mattress = 1 };
             //DbSqlHelper.SelectOne(cmd, profirate, "profitrate");
             decimal profitrate = 0;
@@ -665,7 +665,7 @@ namespace JLHHJSvr.Helper
             AddFormulaKeyValue("底价", "if((【部门利润率】+【部门让利点数】/100)<> 0,【不含税出厂价】/(【部门利润率】+【部门让利点数】/ 100),0)");
             AddFormulaKeyValue("佣金", "【底价】/(1-(【佣金点数】-1))-【底价】");
 
-            var dept = Cache.GetData<int,u_dept,DeptMapping>(mattress.deptid.Value);
+            var dept = Cache.GetData<u_dept, DeptMapping>(mattress.deptid.Value);
             AddKeyValue("税金补偿", dept.taxes_rate);
 
             var bednetVarList = new List<u_bednet_var>();
@@ -1486,7 +1486,7 @@ namespace JLHHJSvr.Helper
             var mattressType = new u_mattress_type() { mattresstypeid = mattress.mattresstypeid };
             DbSqlHelper.SelectOne(cmd, mattressType, "hrcost_formula,biandaiqty,zhizao_amt,guanli_rate,typerate,dannum1_rate,dannum2_rate,dannum3_rate,dannum4_rate");
 
-            var dept = Cache.GetData<int,u_dept,DeptMapping>(mattress.deptid.Value);
+            var dept = Cache.GetData<u_dept, DeptMapping>(mattress.deptid.Value);
             decimal? com_profitrate_dannum = 0;
 
             decimal _rate = 1;
@@ -3037,7 +3037,7 @@ namespace JLHHJSvr.Helper
 
             var existing = GetMattressSubspecs(mattress.mattressid, "mattressid") ?? new List<u_mattress>();
 
-            var oldIdSet = new HashSet<int>(existing.Where(t => t.xd_flag != null && t.xd_flag == 0).Select(x => x.mattressid));
+            var oldIdSet = new HashSet<int>(existing.Select(x => x.mattressid));
             var newIdSet = new HashSet<int>(mattress.subspecsList.Where(x => x.mattressid != 0).Select(x => x.mattressid));
             var toUpdate = mattress.subspecsList.Where(x => oldIdSet.Contains(x.mattressid)).ToList();
             var toInsert = mattress.subspecsList.Where(x => x.mattressid == 0).ToList();
@@ -3090,24 +3090,24 @@ namespace JLHHJSvr.Helper
             clone.mattresscode = child.mattresscode;
             clone.mattressrelcode = child.mattressrelcode;
             clone.mattressname = child.mattressname;
-            clone.xd_flag = 0;
-            clone.xd_auditingdate = null;
-            clone.xd_auditingrep = string.Empty;
-            clone.flag = 0;
-            clone.auditingdate = null;
-            clone.auditingrep = string.Empty;
-            clone.js1_flag = 0;
-            clone.js1_auditingdate = null;
-            clone.js1_auditingrep = string.Empty;
-            clone.yw_flag = 0;
-            clone.yw_auditingdate = null;
-            clone.yw_auditingrep = string.Empty;
-            clone.js2_flag = 0;
-            clone.js2_auditingdate = null;
-            clone.js2_auditingrep = string.Empty;
-            clone.qr_flag = 0;
-            clone.qr_auditingdate = null;
-            clone.qr_auditingrep = string.Empty;
+            clone.xd_flag = child.xd_flag;
+            clone.xd_auditingdate = child.xd_auditingdate;
+            clone.xd_auditingrep = child.xd_auditingrep;
+            clone.flag = child.flag;
+            clone.auditingdate = child.auditingdate;
+            clone.auditingrep = child.auditingrep;
+            clone.js1_flag = child.js1_flag;
+            clone.js1_auditingdate = child.js1_auditingdate;
+            clone.js1_auditingrep = child.js1_auditingrep;
+            clone.yw_flag = child.yw_flag;
+            clone.yw_auditingdate = child.yw_auditingdate;
+            clone.yw_auditingrep = child.yw_auditingrep;
+            clone.js2_flag = child.js2_flag;
+            clone.js2_auditingdate = child.js2_auditingdate;
+            clone.js2_auditingrep = child.js2_auditingrep;
+            clone.qr_flag = child.qr_flag;
+            clone.qr_auditingdate = child.qr_auditingdate;
+            clone.qr_auditingrep = child.qr_auditingrep;
             clone.subspecsList = child.subspecsList;
             clone.mattress_width = child.mattress_width;
             clone.mattress_length = child.mattress_length;

+ 1 - 1
JLHHJSvr/Helper/SoftBedHelper.cs

@@ -683,7 +683,7 @@ namespace JLHHJSvr.Helper
 
 		private void InitSoftBed(u_softbed softbed)
 		{
-            var dept = Cache.GetData<int, u_dept, DeptMapping>(softbed.deptid);
+            var dept = Cache.GetData<u_dept, DeptMapping>(softbed.deptid);
 
 			//
             softbed.dept_profitrate = dept.profitrate;

+ 6 - 0
JLHHJSvr/JLHHJSvr.csproj

@@ -480,6 +480,11 @@
     <Compile Include="ImgHelper\ImgHelper.cs" />
     <Compile Include="LJFrameWork\FilePath\FilePathHelper.cs" />
     <Compile Include="LJFrameWork\LJException\LJException.cs" />
+    <Compile Include="LJFrameWork\ljlib.cache\Caching\CacheService.cs" />
+    <Compile Include="LJFrameWork\ljlib.cache\Caching\GenericLoader.cs" />
+    <Compile Include="LJFrameWork\ljlib.cache\Caching\ICacheLoader.cs" />
+    <Compile Include="LJFrameWork\ljlib.cache\Caching\ICacheMapping.cs" />
+    <Compile Include="LJFrameWork\ljlib.cache\Caching\ICacheService.cs" />
     <Compile Include="LJFrameWork\ljlib.cache\LJCache.cs" />
     <Compile Include="LJFrameWork\LJLib.Method\SortedListEx.cs" />
     <Compile Include="LJFrameWork\LJLib.SQLEX\LJTransactionScope.cs" />
@@ -615,6 +620,7 @@
     <Compile Include="StreamHelper\StreamHelper.cs" />
     <Compile Include="TextLog\ILogger.cs" />
     <Compile Include="TextLog\Logger.cs" />
+    <Compile Include="TextLog\ReuestLogger.cs" />
     <Compile Include="TextLog\SyncLogger.cs" />
     <Compile Include="Tools\AutoInit.cs" />
     <Compile Include="Tools\CalculateFormula.cs" />

+ 29 - 0
JLHHJSvr/LJFrameWork/ljlib.cache/Caching/CacheService.cs

@@ -0,0 +1,29 @@
+using DirectService.Tools;
+using System;
+using System.Data.SqlClient;
+
+namespace LJLib
+{
+    public class CacheService<TKey, TValue> : ICacheService<TKey, TValue>
+    where TValue : class, new()
+    {
+        private readonly LJCache<TKey, TValue> _cache;
+        private readonly ICacheLoader<TKey, TValue> _loader;
+
+        public CacheService(ICacheLoader<TKey, TValue> loader, int cacheMinutes = 120)
+        {
+            _loader = loader ?? throw new ArgumentNullException(nameof(loader));
+            _cache = new LJCache<TKey, TValue> { DefaultAddMinutes = cacheMinutes };
+        }
+
+        public TValue Get(SqlCommand cmd, TKey key)
+        {
+            if (!_cache.TryGetValue(key, out TValue value))
+            {
+                value = _loader.Load(cmd, key);
+                _cache.Add(key, value);
+            }
+            return ObjectHelper.DeepCopy(value);
+        }
+    }
+}

+ 26 - 0
JLHHJSvr/LJFrameWork/ljlib.cache/Caching/GenericLoader.cs

@@ -0,0 +1,26 @@
+using JLHHJSvr.Helper;
+using System;
+using System.Collections.Generic;
+using System.Data.SqlClient;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace LJLib
+{
+    public class GenericLoader<TKey, TValue> : ICacheLoader<TKey, TValue>
+    where TValue : class, new()
+    {
+        private readonly ICacheMapping<TKey, TValue> _mapping;
+
+        public GenericLoader(ICacheMapping<TKey, TValue> mapping)
+        {
+            _mapping = mapping ?? throw new ArgumentNullException(nameof(mapping));
+        }
+
+        public TValue Load(SqlCommand cmd, TKey id)
+        {
+            return _mapping.GetInstance(cmd, id);
+        }
+    }
+}

+ 14 - 0
JLHHJSvr/LJFrameWork/ljlib.cache/Caching/ICacheLoader.cs

@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Data.SqlClient;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace LJLib
+{
+    public interface ICacheLoader<TKey, TValue> where TValue : class, new()
+    {
+        TValue Load(SqlCommand cmd, TKey key);
+    }
+}

+ 14 - 0
JLHHJSvr/LJFrameWork/ljlib.cache/Caching/ICacheMapping.cs

@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Data.SqlClient;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace LJLib
+{
+    public interface ICacheMapping<TKey, TValue> where TValue : class, new()
+    {
+        TValue GetInstance(SqlCommand cmd, TKey key);
+    }
+}

+ 14 - 0
JLHHJSvr/LJFrameWork/ljlib.cache/Caching/ICacheService.cs

@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Data.SqlClient;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace LJLib
+{
+    public interface ICacheService<TKey, TValue> where TValue : class, new()
+    {
+        TValue Get(SqlCommand cmd, TKey key);
+    }
+}

+ 95 - 0
JLHHJSvr/TextLog/ReuestLogger.cs

@@ -0,0 +1,95 @@
+using LJLib.Tools.File;
+using System;
+using System.IO;
+
+namespace LJLib.TextLog
+{
+    internal class ReuestLogger : ILogger
+    {
+        private readonly string _logDir;
+        private readonly object _syncRoot = new object();
+
+        public ReuestLogger(string logDir = null)
+        {
+            // 默认目录为执行目录下的 log 文件夹
+            _logDir = logDir ?? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log");
+
+            if (!Directory.Exists(_logDir))
+                Directory.CreateDirectory(_logDir);
+        }
+
+        /// <summary>
+        /// 获取今天的日志文件全路径
+        /// </summary>
+        private string GetTodayLogFile()
+        {
+            string fileName = $"REQUEST_{DateTime.Now:yyyyMMdd}.log";
+            return Path.Combine(_logDir, fileName);
+        }
+
+        /// <summary>
+        /// 写入日志
+        /// </summary>
+        public void WriteLog(string msg)
+        {
+            try
+            {
+                lock (_syncRoot)
+                {
+                    string filePath = GetTodayLogFile();
+
+                    using (StreamWriter sw = new StreamWriter(filePath, true))
+                    {
+                        sw.WriteLine("====================================================================");
+                        sw.WriteLine($"时间: {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
+                        sw.WriteLine("--------------------------------------------------------------------");
+                        sw.WriteLine(msg);
+                        sw.WriteLine("====================================================================");
+                        sw.WriteLine();
+                    }
+
+                    // 清理过期日志(7天前的删除)
+                    CleanupOldLogs();
+                }
+            }
+            catch
+            {
+                // 这里可考虑写到系统事件日志或忽略
+            }
+        }
+
+        /// <summary>
+        /// 清理7天前的日志文件
+        /// </summary>
+        private void CleanupOldLogs()
+        {
+            try
+            {
+                var files = Directory.GetFiles(_logDir, "REQUEST_*.log");
+                DateTime threshold = DateTime.Now.AddDays(-7);
+
+                foreach (var file in files)
+                {
+                    string fileName = Path.GetFileNameWithoutExtension(file);
+                    // 格式: REQUEST_yyyyMMdd
+                    if (fileName.Length == "REQUEST_yyyyMMdd".Length &&
+                        DateTime.TryParseExact(fileName.Replace("REQUEST_", ""),
+                                               "yyyyMMdd",
+                                               null,
+                                               System.Globalization.DateTimeStyles.None,
+                                               out DateTime logDate))
+                    {
+                        if (logDate < threshold.Date)
+                        {
+                            File.Delete(file);
+                        }
+                    }
+                }
+            }
+            catch
+            {
+                // 忽略清理异常
+            }
+        }
+    }
+}