欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
-------------------------------生成实体类-------------------------------
declare @TableName sysname = 'DictReCheckItem'  
declare @TableNameL sysname = 'DictReCheckItem'
declare @IsCreateControllerMethods  sysname =0
declare @Result varchar(max) = '
/// <summary>
/// ' + @TableName +
 
'a
/// </summary>
public class ' + @TableName + '
{'
    select @Result = @Result + '
    public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }'
from
(
SELECT
replace(col.name, ' ', '_') ColumnName,
column_id ColumnId,
prop.value ColName,
case typ.name
when 'bigint' then 'long'
when 'binary' then 'byte[]'
when 'bit' then 'bool'
when 'char' then 'string'
when 'date' then 'DateTime'
when 'datetime' then 'DateTime'
when 'datetime2' then 'DateTime'
when 'datetimeoffset' then 'DateTimeOffset'
when 'decimal' then 'decimal'
when 'float' then 'float'
when 'image' then 'byte[]'
when 'int' then 'int'
when 'money' then 'decimal'
when 'nchar' then 'char'
when 'ntext' then 'string'
when 'numeric' then 'decimal'
when 'nvarchar' then 'string'
when 'real' then 'double'
when 'smalldatetime' then 'DateTime'
when 'smallint' then 'short'
when 'smallmoney' then 'decimal'
when 'text' then 'string'
when 'time' then 'TimeSpan'
when 'timestamp' then 'DateTime'
when 'tinyint' then 'byte'
when 'uniqueidentifier' then 'Guid'
when 'varbinary' then 'byte[]'
when 'varchar' then 'string'
else 'UNKNOWN_' + typ.name
end ColumnType,
case
when col.is_nullable = 1 and typ.name in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier')
then '?'
else ''
end NullableSign
from sys.columns col
join sys.types typ on
col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
LEFT JOIN sys.extended_properties prop ON col.object_id = prop.major_id AND col.column_id = prop.minor_id
where object_id = object_id(@TableName)
) t
--order by ColumnId
 
set @Result = @Result + '
}'
 
print @Result
 
print''
print 'public class ' + @TableName + 'Params'
print'{'
print ' public string BeginDate { get; set; }'
print ' public string EndDate { get; set; }'
print '}'
 
-------------------------------生成实体类end-------------------------------
print ''
--IDAL
--public partial interface IDictQualityItemsDal : IBaseDal<DictQualityItems>
print 'public partial interface I'+@TableName +'Dal : IBaseDal<'+ @TableName +'>'
print'{'
print'  bool Inserts(List<'+@TableName+'> objs);'
print'  bool DeleteByIds(List<string> ids);'
print'  bool UpdateIsEnable(List<string> ids, string isEnable);'
print'  bool Updates(List<'+@TableName+'> objs);'
print '}'
print''
    --bool Inserts(List<DictQualityItems> objs);
    --bool DeleteByIds(List<string> ids);
    --bool UpdateIsEnable(List<string> ids, string isEnable);
     
 
--DAL
--public partial class DictQualityItemsDal : BaseDal<DictQualityItems>, IDictQualityItemsDal
print 'public partial class '+@TableName +'Dal : BaseDal<'+ @TableName +'>,I'+@TableName+'Dal'
print'{'
--add
print'  public bool Inserts(List<'   + @TableName +'> objs)'
print'  {' 
print'   bool value = false;'  
print'   try'  
print'   {'
--print'        new BaseDal<'+   @TableName+'>().Db.UseTransaction(() =>'
print'      this.Db.UseTransaction(() =>'
print'      {' 
print'          foreach ('+@TableName+' temp in objs)'
print'          {' 
print'              temp.Id = Guid.NewGuid().ToString("N");'   
print'              temp.CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");'   
print'              this.Add(temp);'   
print'          }' 
print'      });'   
print'  '  
print'      value = true;' 
print'    }'   
print'    catch (Exception) { throw; }'
print'  '  
print'    return value;'   
print'  }' 
print'  '  
 
--del
print'public bool DeleteByIds(List<string> ids)' 
print'{'
print'  bool value = false;'   
print'  try'
print'  {' 
--print'        new BaseDal<'+   @TableName+'>().Db.UseTransaction(() =>'
print'      this.Db.UseTransaction(() =>'
print'      {' 
print'          foreach (string id in ids)'
print'          {'
print'              this.Delete<'+   @TableName+'>(o => o.Id.Equals(id));'
print'          }'
print'      });'   
print''
print'      value = true;' 
print'  }'
print'  catch (Exception) { throw; }'  
print''
print'  return value;'
print'}'
print''
--UpdateIsEnable
print'public bool UpdateIsEnable(List<string> ids, string isEnable)' 
print'{'
print'  bool value = false;'   
print'  try'
print'  {' 
print'      this.Db.UseTransaction(() =>'
print'      {' 
print'       foreach (string id in ids)'
print'       {'
print'          int count = this.Update<'+@TableName+'>('
print'          o => o.Id.ToString().Equals(id), o => new '+@TableName+'()'
print'          {'
print'              IsEnable = isEnable'
print'          });'   
print'      }'
print'    });' 
print'  }'
print'  catch (Exception)' 
print'  {'
print'      value = false;'
print'      throw;'
print'  }'
print''
print'  return value;'
print'}'
print''
--Updates
print'public bool Updates(List<' +@TableName+'> objs)'
print'{'   
print'  bool value = false;'   
print'  try'   
print'  {' 
--print'        new BaseDal<'+   @TableName+'>().Db.UseTransaction(() =>'
print'      this.Db.UseTransaction(() =>'
print'      {' 
print'          foreach ('+@TableName+' temp in objs)'
print'          {' 
print'          this.Update<'+@TableName+'>('
print'          o => o.Id.ToString().Equals(id), o => new '+@TableName+'()'
print'          {'
print'              IsEnable = temp.IsEnable,'
print'          });'   
print'      }'
print'  }'
print'  catch (Exception)' 
print'  {'
print'      value = false;'
print'      throw;'
print'  }'
print''
print'  return value;'
print' }'
--类结束
print'}'
print ''
 
 
--IService
 --public partial interface IDictReviewItemsService : IBaseService<DictReviewItems>
 --List<ViewReportInfo> GetViewReportInfo(ViewReportInfoParams mode);
print 'public partial interface I'+@TableName +'Service : IBaseService<'+ @TableName +'>'
print'{'
print'  bool Inserts(List<'+@TableName+'> objs);'
print'  bool DeleteByIds(List<string> ids);'
print'  bool UpdateIsEnable(List<string> ids, string isEnable);'
print'  bool Updates(List<'+@TableName+'> objs);'
print'  List<'+@TableName+'> Get'+@TableName+'('+@TableName+'Params mode);'
print'  List<'+@TableName+'> Get'+@TableName+'('+@TableName+'Params mode,int pageSize, int pageIndex, out int totalCount);'
print '}'
print ''
 
 
--Service
--public partial class DictQualityItemsService : BaseService<DictQualityItems>, IDictQualityItemsService
--private IDictQualityItemsDal Dal = new DictQualityItemsDal();
--public override void SetCurrentDal()
--CurrentDal = new DictQualityItemsDal();
print 'public partial class '+@TableName +'Service : BaseService<'+ @TableName +'>,I'+@TableName+'Service'
print '{'
print ' private I'+ @TableName +'Dal Dal = new '+@TableName +'Dal();'
print ' public override void SetCurrentDal()'
print ' {'
print '     CurrentDal = new '+ @TableName +'Dal();'
print ' }'
print''
--add
print'public bool Inserts(List<'+ @TableName +'> objs)'
print'{'
print'  return Dal.Inserts(objs);'
print'}'
print''
--del
print'public bool DeleteByIds(List<string> ids)'
print'{'
print'  return Dal.DeleteByIds(ids);'
print'}'
print''
--UpdateIsEnable
print'public bool UpdateIsEnable(List<string> ids, string isEnable)'
print'{'
print'  return Dal.UpdateIsEnable(ids, isEnable);'
print'}'
print''
--Updates
print'public bool Updates(List<'+ @TableName +'> objs)'
print'{'
print'  return Dal.Updates(objs);'
print'}'
print''
--查询方法
print ' public List<'+@TableName+'> Get'+@TableName+'('+@TableName+'Params mode)'
print ' {'
print'          List<'+@TableName+'> list = new List<' + @TableName +'>();'    
print'          Expression<Func<'+@TableName+', bool>> where = o => o.Id == o.Id;'
print'          string beginDate = mode.BeginDate + " 00:00:00";'  
print'          string endDate = mode.EndDate + " 23:59:59";'  
print'          where = where.And(o => DateTime.Parse(o.AuditTime) >= DateTime.Parse(beginDate)'
print'          && DateTime.Parse(o.AuditTime) <= DateTime.Parse(endDate));'
print''
print'          if (!string.IsNullOrEmpty(mode.Organization))'
print'          where = where.And(o => o.Organization == mode.Organization);'
print''
print'          list = CurrentDal.GetEntities(where)'
print'          .OrderBy(o => o.Organization)'
print'          .ThenBy(o => o.ClassName)'
print'          .ToList();'
print''
print'          return list;'    
print ' }'
 
 
--分页查询方法
print''
print ' public List<'+@TableName+'> Get'+@TableName+'('+@TableName+'Params mode,int pageSize, int pageIndex, out int totalCount)'
print ' {'
print'          List<'+@TableName+'> result = new List<' + @TableName +'>();'      
print'          totalCount = 0;'       
print'          try'
print'          {'
print'              Expression<Func<'+@TableName+', bool>> where = o => o.Id == o.Id;'
print'              if (mode != null) where = GetWhere(mode);'
print'              totalCount = new BaseDal<'+@TableName+'>().Db.Query<'+@TableName+'>().Where(where).Count();'
print''
print'              result = new BaseDal<'+@TableName+'>().Db.Query<'+@TableName+'>().Where(where)'
print'              .OrderByDesc(o => o.OrganizationName)'
print'              .ThenByDesc(o => o.Modality)'
print'              .ThenBy(o => o.Status)'
print'              .Skip(pageSize * (pageIndex - 1))'
print'              .Take(pageSize)'
print'              .ToList();'
print'          }'
print'          catch (Exception ex)'
print'          {'
print'              LogHelper.ErrorFormat("'+@TableName+'Service Get'+@TableName+'-{0}", ex);'
print'          }'
print'          return result;'  
print ' }'
print''
--where
print'  private Expression<Func<'+@TableName+', bool>> GetWhere('+@TableName+'Params mode)'
print'  {'
print'      string beginDate = mode.BeginDate + " 00:00:00";'  
print'      string endDate = mode.EndDate + " 23:59:59";'  
print'      Expression<Func<'+@TableName+', bool>> where = o => o.Id == o.Id;'
print'      where = where.And(o => DateTime.Parse(o.AuditTime) >= DateTime.Parse(beginDate)'
print'      && DateTime.Parse(o.AuditTime) <= DateTime.Parse(endDate));'
print''
print'      if (!string.IsNullOrEmpty(mode.Organization))'
print'      where = where.And(o => o.Organization == mode.Organization);'
print''
print'      return where;'   
print'   }'
print '}'
 
--Controller
declare @Controller varchar(max) =
'
/// <summary>
/// ' + @TableName +
 
' 控制器
/// </summary>' + char(13)
+'[ApiController]' +char(13)
+'public class ' + @TableName + 'Controller : ControllerBase'+char(13)
+'{'+char(13)
+ ' #region 定义' + char(13)
+ ' /// <summary>
    ///  I' + @TableName + 'Service ' + @TableName + 'Service
    /// </summary>' + char(13)
+'  private I'+ @TableName + 'Service ' + @TableName + 'Service; '
+'
    /// <summary>
    ///  初始化服务信息
    /// </summary>' + char(13)
    +'  /// <param name="'+@TableNameL+'Service">I' + @TableName + 'Service ' + @TableNameL + 'Service</param>' + char(13)
    +'  public '+ @TableName + 'Controller(I' + @TableName+ 'Service ' + @TableNameL + 'service)'+ char(13)
    +'  {'+char(13)
    + '     '+@TableName+ 'Service = ' + @TableNameL+ 'Service;'+char(13)
    +'  }'+char(13)
+ char(13)+ '   #endregion' + char(13) + char(13)+char(13)
 
 
declare @Controller2 varchar(max) =
--查询
+'#region query
 /// <summary>
 /// 分页-根据指定条件查询信息
 /// -----------------'
 +@TableName+'Params 对象-----------------
 /// BeginDate--开始日期
 /// EndDate--结束日期
 /// -----------------'
 +@TableName+'Params 对象-----------------
 /// </summary>
 /// <param name="jsonData">'+@TableName+'Params</param>
 ///<param name="pageSize">int型,每页数量</param>
 ///<param name="pageIndex">int型,当前页数</param>
 /// <returns>查询结果信息</returns>
 [HttpGet]
 [Route("api/'+@TableName+'/Get'+@TableName+'")]
 public HttpResponseMessage Get'+@TableName+'(string jsonData, int pageSize, int pageIndex)
 {
     var res = new ProxyResult();
     res.Success = false;
     try
     {
         int totalCount = 0;'
         +@TableName+'Params mode = JsonHelper.Instance.Deserialize<'+@TableName+'Params>(jsonData);
         List<'+@TableName+'> list = '+@TableName+'Service.Get'+@TableName+'(mode, pageSize, pageIndex, out totalCount);
         res.JsonData = JsonHelper.Instance.Serialize(list);
         res.Success = true;
         res.DataCount = totalCount;
     }
     catch (Exception ex)
     {
         string input = "入参==" + jsonData + "\r\n";
         LogHelper.ErrorFormat("api/'+@TableName+'/Get'+@TableName+' 异常-{0}", input + ex.Message);
         res.ErrMessage = ex.Message;
     }
     HttpResponseMessage result = new HttpResponseMessage
     {
         Content = new StringContent(JsonHelper.Instance.Serialize(res),
         System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
     };//这里是去掉反斜杠再放回出去,json就只剩下双引号。
     return result;
 }
 
 #endregion'+ char(13)+ char(13)
--删除方法
+'  #region Delete
/// <summary>
 /// 根据ID删除字典信息
 /// </summary>
 /// <param name="ids">编号|数组类型</param>
 /// <returns></returns>
 [HttpGet]' + char(13)
+'[Route("api/'+@TableName+'/DeleteByIds")]'+ char(13)
+'public HttpResponseMessage DeleteByIds(List<string> ids)
{
    var res = new ProxyResult();
    res.Success = false;
    try
    {'
+'res.JsonData = JsonHelper.Instance.Serialize('+ @TableName + 'Service.DeleteByIds(ids));'
+'     res.Success = true;
 }
 catch (Exception ex)
 {'
 +'LogHelper.ErrorFormat("api/'+@TableName+'/DeleteByIds 异常-{0}", ex);'
 +'        res.ErrMessage = ex.Message;
    }
    HttpResponseMessage result = new HttpResponseMessage
    {
        Content = new StringContent(JsonHelper.Instance.Serialize(res),
        System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
    };//这里是去掉反斜杠再放回出去,json就只剩下双引号。
    return result;
}'+char(13) +char(13)
+'#endregion' +char(13) +char(13)
 
--修复更改
+'   #region Update'+char(13)
     +'   /// <summary>'+char(13)
     +'/// 修改 ' + @TableName +char(13)
+' /// </summary>
 /// <param name="jsdata">'+@TableName+'json</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/'+@TableName+'/Update")]
        public HttpResponseMessage Update([FromBody] object jsdata)
        {
            var res = new ProxyResult();
            res.Success = false;
            try
            {'
            +@TableName+' obj = JsonHelper.Instance.Deserialize<'+@TableName+'>(jsdata.ToString());
                res.JsonData = JsonHelper.Instance.Serialize('+@TableName+'Service.Update(obj));
                res.Success = true;
            }
            catch (Exception ex)
            {
                LogHelper.ErrorFormat("api/'+@TableName+'/Update 异常-{0}", ex.Message + ex.StackTrace);
                res.ErrMessage = ex.Message;
            }
            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(JsonHelper.Instance.Serialize(res),
                System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            };//这里是去掉反斜杠再放回出去,json就只剩下双引号。
            return result;
        }'+char(13) +char(13)
+'#endregion' +char(13) +char(13)
 
         
+char(13) +'}'+char(13)
 
print @Controller
print @Controller2
 
-- 注册服务代码
print('services.AddScoped<I' + @TableName + 'Service,' + @TableName + 'Service>();')

declare @Controller3 varchar(max) = ''

print @Controller3

  

posted on   sunwugang  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示