wanshenmean
2024-09-13 319e8729b47c96e3a3717c5a40cd5df867d65ce5
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
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.Const;
using WIDESEA_Core.DB.Models;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Tenants;
 
namespace WIDESEA_Core.AOP
{
    public static class SqlSugarAop
    {
        public static void DataExecuting(object oldValue, DataFilterModel entityInfo)
        {
            if (entityInfo.EntityValue is BaseEntity baseEntity)
            {
                // 新增操作
                if (entityInfo.OperationType == DataFilterType.InsertByObject)
                {
                    if (entityInfo.PropertyName == nameof(BaseEntity.CreateDate))
                    {
                        baseEntity.CreateDate = DateTime.Now;
                    }
                }
 
                if (entityInfo.OperationType == DataFilterType.UpdateByObject)
                {
                    baseEntity.ModifyDate = DateTime.Now;
                }
 
 
                if (App.User?.UserId > 0)
                {
                    switch (entityInfo.OperationType)
                    {
                        case DataFilterType.UpdateByObject:
                            baseEntity.Modifier = App.User.UserName;
                            break;
                        case DataFilterType.InsertByObject:
                                baseEntity.Creater = App.User.UserName;
                            break;
                    }
                }
            }
        }
 
        private static string GetWholeSql(SugarParameter[] paramArr, string sql)
        {
            foreach (var param in paramArr)
            {
                sql = sql.Replace(param.ParameterName, $@"'{param.Value.ObjToString()}'");
            }
 
            return sql;
        }
 
        private static string GetParas(SugarParameter[] pars)
        {
            string key = "【SQL参数】:";
            foreach (var param in pars)
            {
                key += $"{param.ParameterName}:{param.Value}\n";
            }
 
            return key;
        }
    }
}