服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2024-12-28 9f487ef04f1cb9e1086d4e72346e3e2237ffd5c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using 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.IBusiness.MES;
using Tiger.Model.Entitys.MES.U9C;
using System.Reflection;
using Dm.filter;
using System.Dynamic;
 
namespace Tiger.Business.MES
{
    /// <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泛型 
                //通过属性名获取属性值  “VALIDATION_TYPE”属性名称
                PropertyInfo typeInfo = entityType.GetProperty("VALIDATION_TYPE");
                //通过属性名获取属性值  “VALIDATION_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;
        }
    }
}