From 6e78892c440406c94fa1190d8daa04480620f13a Mon Sep 17 00:00:00 2001
From: Ben Lin <maobin001@msn.com>
Date: 星期六, 28 十二月 2024 01:31:02 +0800
Subject: [PATCH] 通用导入Excel更新

---
 Tiger.Api/Autofac/AutoFacContianer.cs                       |    5 +
 Tiger.Business.MES/BIZ/ImportWoPlan.cs                      |  123 ++++++++++++++++++++++++++++++
 Tiger.IBusiness.MES/BIZ/IImportWoPlan.cs                    |   17 ++++
 Tiger.IBusiness/Common/ICommonImportExcel.cs                |   14 +++
 Tiger.Business/Common/ImportExcel.cs                        |   12 +-
 Tiger.IBusiness/Common/IImportExcel.cs                      |    4 
 Tiger.Model.Net/Entitys/ImportEntity.cs                     |   12 ++
 Tiger.Controllers.System/Controllers/Base/BaseController.cs |   10 +-
 8 files changed, 184 insertions(+), 13 deletions(-)

diff --git a/Tiger.Api/Autofac/AutoFacContianer.cs b/Tiger.Api/Autofac/AutoFacContianer.cs
index 3ce35cd..cdd8036 100644
--- a/Tiger.Api/Autofac/AutoFacContianer.cs
+++ b/Tiger.Api/Autofac/AutoFacContianer.cs
@@ -107,5 +107,10 @@
         {
             return AutoFacContainer.Instance.Resolve<T>();
         }
+
+        public static dynamic Resolve(string typeFullName)
+        {
+            return AutoFacContainer.Instance.Resolve(System.Type.GetType(typeFullName, true));
+        }
     }
 }
diff --git a/Tiger.Business.MES/BIZ/ImportWoPlan.cs b/Tiger.Business.MES/BIZ/ImportWoPlan.cs
new file mode 100644
index 0000000..215373d
--- /dev/null
+++ b/Tiger.Business.MES/BIZ/ImportWoPlan.cs
@@ -0,0 +1,123 @@
+锘縰sing Apache.NMS.ActiveMQ.Commands;
+using Rhea.Common;
+using System;
+using System.Collections.Generic;
+using Newtonsoft.Json;
+using System.Diagnostics;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Tiger.IBusiness;
+using Tiger.Model.Minsun;
+using Tiger.Model;
+using Tiger.Model.Entitys.MES.U9C;
+using System.Reflection;
+using Dm.filter;
+using System.Dynamic;
+
+namespace Tiger.Business
+{
+    /// <summary>
+    /// 宸ュ崟璁″垝瀵煎叆Excel
+    /// </summary>
+    public class ImportWoPlan : IImportWoPlan
+    {
+        /// <summary>
+        /// 瀵煎叆
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="newEntity"></param>
+        /// <param name="jsonStr"></param>
+        /// <returns></returns>
+        public async Task<ApiAction> Import<T>(T newEntity, ImportInput input) where T : class, new()
+        {
+            var result = new ApiAction();
+            try
+            {
+
+                List<T> list = JsonConvert.DeserializeObject<List<T>>(input.EntityJson);
+                DbClient db = Biz.Db;
+                if (list.Any())
+                {
+                    if (list.Count > 100)
+                    {
+                        db.Utilities.PageEach(list, 100, pageList =>
+                        {
+                            var y = db.Storageable(pageList)
+                                   .ToStorage();
+                            y.AsInsertable.ExecuteCommand();
+                            y.AsUpdateable.ExecuteCommand();
+                        });
+                    }
+                    else
+                    {
+                        var s = db.Storageable(list)
+                               .ToStorage();
+                        s.AsInsertable.ExecuteCommand();
+                        s.AsUpdateable.ExecuteCommand();
+                    }
+                }
+
+
+            }
+            catch (Exception ex)
+            {
+                result.CatchException(ex, $"楠岃瘉瀵煎叆寮傚父");
+            }
+            return result;
+        }
+
+        /// <summary>
+        /// 瀵煎叆Excel鍓嶉獙璇�
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="newEntity"></param>
+        /// <param name="input"></param>
+        /// <returns></returns>
+        public async Task<ApiAction<List<T>>> ValidateTableImport<T>(T newEntity, ImportValidateInput input) {
+            var result = new ApiAction<List<T>>();
+            try
+            {
+                List<T> list = JsonConvert.DeserializeObject<List<T>>(input.EntityJson);
+                List<string> chkColumns = JsonConvert.DeserializeObject<List<string>>(input.CheckJson);
+                List<string> wheres = JsonConvert.DeserializeObject<List<string>>(input.where); //鐩墠鍙湁=锛屼互鍚庡彲浠ュ鍔犲叾浠栫殑鏌ヨ鏉′欢
+                Type entityType = typeof(T); //T娉涘瀷 
+                //閫氳繃灞炴�у悕鑾峰彇灞炴�у��  鈥淰ALIDATION_TYPE鈥濆睘鎬у悕绉�
+                PropertyInfo typeInfo = entityType.GetProperty("VALIDATION_TYPE");
+                //閫氳繃灞炴�у悕鑾峰彇灞炴�у��  鈥淰ALIDATION_RESULT鈥濆睘鎬у悕绉�
+                PropertyInfo retInfo = entityType.GetProperty("VALIDATION_RESULT");
+                foreach (var item in list)
+                {
+                    string where = " 1=1 ";
+                    typeInfo.SetValue(item, "鏂板");
+                    retInfo.SetValue(item, "");
+                    foreach (var col in chkColumns)
+                    {
+                        PropertyInfo colInfo = entityType.GetProperty(col);
+                        if (!colInfo.IsNullOrEmpty() && colInfo.GetValue(item).IsNullOrEmpty()) {
+                            typeInfo.SetValue(item, "鏁版嵁寮傚父");
+                            retInfo.SetValue(item, "鍊间负绌烘垨涓嶅瓨鍦�");
+                        }
+                    }
+                    foreach (var w in wheres)
+                    {
+                        PropertyInfo wInfo = entityType.GetProperty(w);
+                        where += $"AND {w} = '{wInfo.GetValue(item)}'";
+                    }
+                    if (await Biz.Db.Queryable<T>().Where(where).AnyAsync())
+                    {
+                        typeInfo.SetValue(item, "淇敼");
+                        retInfo.SetValue(item, "鏁版嵁宸茬粡瀛樺湪锛屼娇鐢ㄦ洿鏂�");
+                    }
+                }
+                result.Data = list;
+            }
+            catch (Exception ex)
+            {
+                result.CatchException(ex, $"楠岃瘉瀵煎叆寮傚父");
+            }
+            return result;
+        }
+    }
+}
diff --git a/Tiger.Business/Common/ImportExcel.cs b/Tiger.Business/Common/ImportExcel.cs
index bd02b51..1300ea9 100644
--- a/Tiger.Business/Common/ImportExcel.cs
+++ b/Tiger.Business/Common/ImportExcel.cs
@@ -21,22 +21,22 @@
     /// <summary>
     /// 閫氱敤瀵煎叆Excel
     /// </summary>
-    public class ImportExcel : IImportExcel
+    public class ImportExcel : ICommonImportExcel
     {
         /// <summary>
         /// 瀵煎叆
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="newEntity"></param>
-        /// <param name="jsonStr"></param>
+        /// <param name="input"></param>
         /// <returns></returns>
-        public async Task<ApiAction> Import<T>(T newEntity, string jsonStr) where T : class, new()
+        public async Task<ApiAction> Import<T>(T newEntity, ImportInput input) where T : class, new()
         {
             var result = new ApiAction();
             try
             {
 
-                List<T> list = JsonConvert.DeserializeObject<List<T>>(jsonStr);
+                List<T> list = JsonConvert.DeserializeObject<List<T>>(input.EntityJson);
                 DbClient db = Biz.Db;
                 if (list.Any())
                 {
@@ -75,13 +75,13 @@
         /// <param name="newEntity"></param>
         /// <param name="input"></param>
         /// <returns></returns>
-        public async Task<ApiAction<List<T>>> ValidateTableImport<T>(T newEntity, ImportEntityValidate input) {
+        public async Task<ApiAction<List<T>>> ValidateTableImport<T>(T newEntity, ImportValidateInput input) {
             var result = new ApiAction<List<T>>();
             try
             {
                 List<T> list = JsonConvert.DeserializeObject<List<T>>(input.EntityJson);
                 List<string> chkColumns = JsonConvert.DeserializeObject<List<string>>(input.CheckJson);
-                List<string> wheres = JsonConvert.DeserializeObject<List<string>>(input.where);
+                List<string> wheres = JsonConvert.DeserializeObject<List<string>>(input.where); //鐩墠鍙湁=锛屼互鍚庡彲浠ュ鍔犲叾浠栫殑鏌ヨ鏉′欢
                 Type entityType = typeof(T); //T娉涘瀷 
                 //閫氳繃灞炴�у悕鑾峰彇灞炴�у��  鈥淰ALIDATION_TYPE鈥濆睘鎬у悕绉�
                 PropertyInfo typeInfo = entityType.GetProperty("VALIDATION_TYPE");
diff --git a/Tiger.Controllers.System/Controllers/Base/BaseController.cs b/Tiger.Controllers.System/Controllers/Base/BaseController.cs
index c544d3f..8f3399b 100644
--- a/Tiger.Controllers.System/Controllers/Base/BaseController.cs
+++ b/Tiger.Controllers.System/Controllers/Base/BaseController.cs
@@ -577,12 +577,13 @@
 
         #region 瀵煎叆Excel
         [HttpPost]
-        public async Task<IActionResult> ImportExcel([FromBody] ApiAction action)
+        public async Task<IActionResult> ImportExcel([FromBody] ApiAction<ImportInput> action)
         {
             ApiAction response;
             try
             {
-                response = action.GetResponse(await DI.Resolve<IImportExcel>().Import(action.NewDataEntity(),action.Data?.ToString()));
+                IImportExcel import = DI.Resolve(action.Data.typeFullName) as IImportExcel;
+                response = action.GetResponse(await import.Import(action.NewDataEntity(),action.Data));
             }
             catch (System.Exception ex)
             {
@@ -597,12 +598,13 @@
         /// <param name="action"></param>
         /// <returns></returns>
         [HttpPost]
-        public async Task<IActionResult> ValidateTableImport([FromBody] ApiAction<ImportEntityValidate> action)
+        public async Task<IActionResult> ValidateTableImport([FromBody] ApiAction<ImportValidateInput> action)
         {
             ApiAction response;
             try
             {
-                response = action.GetResponse(await DI.Resolve<IImportExcel>().ValidateTableImport(action.NewDataEntity(), action.Data));
+                IImportExcel import = DI.Resolve(action.Data.typeFullName) as IImportExcel;
+                response = action.GetResponse(await import.ValidateTableImport(action.NewDataEntity(), action.Data));
             }
             catch (System.Exception ex)
             {
diff --git a/Tiger.IBusiness.MES/BIZ/IImportWoPlan.cs b/Tiger.IBusiness.MES/BIZ/IImportWoPlan.cs
new file mode 100644
index 0000000..27bfe82
--- /dev/null
+++ b/Tiger.IBusiness.MES/BIZ/IImportWoPlan.cs
@@ -0,0 +1,17 @@
+锘縰sing Rhea.Common;
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Tiger.Model;
+using Tiger.Model.Entitys.MES.BizMesWo;
+using Tiger.Model.Entitys.MES.BizMesWoBatch;
+
+namespace Tiger.IBusiness
+{
+    public interface IImportWoPlan: IImportExcel
+    {
+    }
+}
diff --git a/Tiger.IBusiness/Common/ICommonImportExcel.cs b/Tiger.IBusiness/Common/ICommonImportExcel.cs
new file mode 100644
index 0000000..ee5d9ce
--- /dev/null
+++ b/Tiger.IBusiness/Common/ICommonImportExcel.cs
@@ -0,0 +1,14 @@
+锘縰sing Rhea.Common;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Tiger.Model;
+
+namespace Tiger.IBusiness
+{
+    public interface ICommonImportExcel: IImportExcel
+    {
+    }
+}
diff --git a/Tiger.IBusiness/Common/IImportExcel.cs b/Tiger.IBusiness/Common/IImportExcel.cs
index ef4affd..2ba96f0 100644
--- a/Tiger.IBusiness/Common/IImportExcel.cs
+++ b/Tiger.IBusiness/Common/IImportExcel.cs
@@ -10,7 +10,7 @@
 {
     public interface IImportExcel
     {
-        public Task<ApiAction> Import<T>(T newEntity, string jsonStr) where T : class, new();
-        public Task<ApiAction<List<T>>> ValidateTableImport<T>(T newEntity, ImportEntityValidate input);
+        public Task<ApiAction> Import<T>(T newEntity, ImportInput input) where T : class, new();
+        public Task<ApiAction<List<T>>> ValidateTableImport<T>(T newEntity, ImportValidateInput input);
     }
 }
diff --git a/Tiger.Model.Net/Entitys/ImportEntity.cs b/Tiger.Model.Net/Entitys/ImportEntity.cs
index 5bc075c..7e9d82d 100644
--- a/Tiger.Model.Net/Entitys/ImportEntity.cs
+++ b/Tiger.Model.Net/Entitys/ImportEntity.cs
@@ -13,7 +13,17 @@
         public string[] PARAMS { get; set; }
     }
 
-    public class ImportEntityValidate
+    public class BaseImportInput
+    {
+        public string typeFullName { get; set; }
+    }
+
+    public class ImportInput : BaseImportInput
+    {
+        public string EntityJson { get; set; }
+    }
+
+    public class ImportValidateInput: BaseImportInput
     {
         public string EntityJson { get; set; }
         public string CheckJson { get; set; }

--
Gitblit v1.9.3