服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2024-10-26 0295f0d93a3dd54bb6104f5e9a488f94596e756b
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using Tiger.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Rhea.Common;
using System.Net;
using System.Linq;
using Newtonsoft.Json;
using Tiger.IBusiness;
using Microsoft.AspNetCore.Http;
using Tiger.Model.Entitys.MES.BasLabelTemp;
using System.Security.Cryptography;
using Tiger.Model.Entitys.MES.Position;
 
namespace Tiger.Business.MES
{
    public partial class BasLabelTemp : ILabelTemplate
    {
        public async Task<ApiAction> DeleteLabelTemplate(string Id)
        {
            var result = new ApiAction();
            try
            {
                var _label = await Biz.Db.Queryable<BAS_LABEL_TEMP>().Where(x => x.ID == Id).FirstAsync();
                if (_label == null)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"标签模板不存在,不能删除!");
                    return result;
                }
                if (Biz.Db.Queryable<MES_PROD_ACTION>().Any(x => x.LABEL_CODE == _label.LABEL_CODE))
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"标签模板已经绑定到行为,不能删除!");
                    return result;
                }
                if (Biz.Db.Queryable<MES_WO_ACTION>().Any(x => x.LABEL_CODE == _label.LABEL_CODE))
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"标签模板已经绑定到行为,不能删除!");
                    return result;
                }
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    db.Deleteable<BAS_LABEL_TEMP>().Where(x => x.ID == Id).ExecuteCommand();
                    db.Deleteable<BAS_LABEL_VAR>().Where(x => x.LABEL_ID == Id).ExecuteCommand();
                    db.Deleteable<BAS_LABEL_VAR_WO>().Where(x => x.LABEL_ID == Id).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"删除标签模板异常");
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "删除标签模板异常");
            }
            return result;
        }
 
        /// <summary>
        /// 保存标签模板
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiAction> SaveLabelTemplate(BasLabelTempInput input)
        {
            var result = new ApiAction();
            try
            {
                BAS_LABEL_TEMP? prodTemp = null;
                BAS_LABEL_TEMP? prodCustTemp = null;
                var ltemp = await Biz.Db.Queryable<BAS_LABEL_TEMP>().Where(x => x.LABEL_CODE == input.Label.LABEL_CODE).FirstAsync();
                //如果传入的标签客户编码不为空
                if (!input.Label.CUST_CODE.IsNullOrEmpty())
                {
                    //如果查不到标签并且客户编码不为空
                    if (ltemp == null)
                    {
                        prodCustTemp = input.Label;
                        prodTemp = input.Label.Clone();
                        prodTemp.CUST_CODE = "";
                        prodTemp.LABEL_CODE = GenerateCodeName(input.Label.LABEL_CODE, "_L");
                    }
                    else
                    {
                        prodCustTemp = input.Label;
                        if ((ltemp.PROD_CODE != input.Label.PROD_CODE && ltemp.CUST_CODE == input.Label.CUST_CODE) ||
                            (ltemp.PROD_CODE != input.Label.PROD_CODE && ltemp.CUST_CODE != input.Label.CUST_CODE) ||
                            (ltemp.PROD_CODE == input.Label.PROD_CODE && ltemp.CUST_CODE != input.Label.CUST_CODE))
                        {
                            prodCustTemp.LABEL_CODE = GenerateCodeName(input.Label.LABEL_CODE, "_L");
                        }
                    }
                }
                else
                {
                    //如果客户编码为空
                    prodTemp = input.Label;
                    if (ltemp != null && (ltemp.PROD_CODE.IsNullOrEmpty() || ltemp.PROD_CODE != input.Label.PROD_CODE))
                    {
                        prodTemp.LABEL_CODE = GenerateCodeName(input.Label.LABEL_CODE, "_L");
                    }
                }
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    if (prodTemp != null)
                    {
                        var x = db.Storageable(prodTemp)
                           .WhereColumns(t => new { t.LABEL_CODE, t.PROD_CODE, t.CUST_CODE, t.GHOST_ROW })
                           .ToStorage();
                        x.AsInsertable.ExecuteCommand();
                        x.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                    }
                    if (prodCustTemp != null)
                    {
                        var y = db.Storageable(prodCustTemp)
                           .WhereColumns(t => new { t.LABEL_CODE, t.PROD_CODE, t.CUST_CODE, t.GHOST_ROW })
                           .ToStorage();
                        y.AsInsertable.ExecuteCommand();
                        y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                    }
                    var temp = db.Queryable<BAS_LABEL_TEMP>().Where(x =>
                    x.LABEL_CODE == input.Label.LABEL_CODE &&
                    x.PROD_CODE == input.Label.PROD_CODE &&
                    x.CUST_CODE == input.Label.CUST_CODE).First();
                    if (input.LabelVars != null && input.LabelVars.Count > 0)
                    {
                        if (temp != null && temp.ID != input.LabelVars[0].LABEL_ID)
                        {
                            foreach (var item in input.LabelVars)
                            {
                                item.LABEL_ID = temp.ID;
                            }
                        }
                        var z = db.Storageable(input.LabelVars)
                           .WhereColumns(t => new { t.LABEL_ID, t.VAR_NAME, t.GHOST_ROW })
                           .ToStorage();
                        z.AsInsertable.ExecuteCommand();
                        z.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                    }
                    if (input.LabelVarWos != null && input.LabelVarWos.Count > 0)
                    {
                        if (temp != null && temp.ID != input.LabelVarWos[0].LABEL_ID)
                        {
                            foreach (var item in input.LabelVarWos)
                            {
                                item.LABEL_ID = temp.ID;
                            }
                        }
                        db.Deleteable<BAS_LABEL_VAR_WO>(false).Where(x => x.LABEL_ID == input.LabelVarWos[0].LABEL_ID).ExecuteCommand();
                        var o = db.Storageable(input.LabelVarWos)
                           .WhereColumns(t => new { t.LABEL_ID, t.WORK_ORDER, t.VAR_NAME, t.GHOST_ROW })
                           .ToStorage();
                        o.AsInsertable.ExecuteCommand();
                        o.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                    }
                });
                if (!dbTran.IsSuccess)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"保存标签模板异常!");
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "保存标签模板异常");
            }
            return await Task.FromResult(result);
        }
 
        /// <summary>
        /// 保存标签模板变量
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiAction> SaveLabelTemplateVar(BasLabelTempInput input)
        {
            var result = new ApiAction();
            try
            {
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    if (input.LabelVars != null && input.LabelVars.Count > 0)
                    {
                        db.Deleteable<BAS_LABEL_VAR>(false).Where(x => x.LABEL_ID == input.LabelVars[0].LABEL_ID).ExecuteCommand();
                        var z = db.Storageable(input.LabelVars)
                           .WhereColumns(t => new { t.LABEL_ID, t.VAR_NAME, t.GHOST_ROW })
                           .ToStorage();
                        z.AsInsertable.ExecuteCommand();
                        z.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                    }
                    if (input.LabelVarWos != null && input.LabelVarWos.Count > 0)
                    {
                        db.Deleteable<BAS_LABEL_VAR_WO>(false).Where(x => x.LABEL_ID == input.LabelVarWos[0].LABEL_ID).ExecuteCommand();
                        var o = db.Storageable(input.LabelVarWos)
                           .WhereColumns(t => new { t.LABEL_ID, t.WORK_ORDER, t.VAR_NAME, t.GHOST_ROW })
                           .ToStorage();
                        o.AsInsertable.ExecuteCommand();
                        o.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                    }
                });
                if (!dbTran.IsSuccess)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"保存标签变量异常!");
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "保存标签变量异常");
            }
            return await Task.FromResult(result);
        }
 
        /// <summary>
        /// 根据分隔符生成Code名
        /// </summary>
        /// <param name="data"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        private string GenerateCodeName(string data, string separator)
        {
            string result = string.Empty;
            var strs = data.Split("_L");
            result = strs.Length > 1 ? data.Replace(strs[1], DateTime.Now.ToString("yyyyMMddhhmmss")) : $"{data}_L{DateTime.Now.ToString("yyyyMMddhhmmss")}";
            return result;
        }
 
    }
}