wankeda
2025-04-15 9bae33c85d698987a6c9cf8ba8edbe9497b101dc
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
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Core.Attributes;
using WIDESEAWCS_Core.Const;
using WIDESEAWCS_Core.DB;
using WIDESEAWCS_Core.DB.Models;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.HttpContextUser;
using WIDESEAWCS_Core.Seed;
using WIDESEAWCS_Core.Tenants;
 
namespace WIDESEAWCS_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;
                    }
 
                    PropertyInfo propertyInfo = entityInfo.EntityColumnInfo.PropertyInfo;
                    SequenceAttribute? sequenceAttirbute = propertyInfo.GetCustomAttribute<SequenceAttribute>();
                    if (sequenceAttirbute != null)
                    {
                        if (propertyInfo.GetValue(entityInfo.EntityValue)?.ObjToInt() <= 0)
                        {
                            SqlSugarClient sugarClient = new SqlSugarClient(new ConnectionConfig
                            {
                                ConfigId = MainDb.CurrentDbConnId,
                                ConnectionString = DBContext.GetMainConnectionDb().Connection,
                                IsAutoCloseConnection = true,
                                DbType = DBContext.DbType,
                            });
                            int count = sugarClient.Ado.GetScalar($"SELECT COUNT(*) FROM sys.sequences WHERE name = '{sequenceAttirbute.SequenceName}'").ObjToInt();
                            if (count == 0)
                            {
                                string sql = $"CREATE SEQUENCE {sequenceAttirbute.SequenceName} AS [int] START WITH {sequenceAttirbute.StartWith} INCREMENT BY {sequenceAttirbute.Increment} MINVALUE {sequenceAttirbute.SeqMinValue} MAXVALUE {sequenceAttirbute.SeqMaxValue} {(sequenceAttirbute.IsCycle ? "CYCLE" : "")} CACHE";
                                sugarClient.Ado.ExecuteCommand(sql);
                            }
                            int seq = sugarClient.Ado.GetScalar($"SELECT NEXT VALUE FOR {sequenceAttirbute.SequenceName}").ObjToInt();
                            propertyInfo.SetValue(entityInfo.EntityValue, seq, null);
                        }
                    }
                }
 
                else if (entityInfo.OperationType == DataFilterType.UpdateByObject)
                {
                    baseEntity.ModifyDate = DateTime.Now;
                }
 
                try
                {
                    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;
                        }
                    }
                    else
                    {
                        switch (entityInfo.OperationType)
                        {
                            case DataFilterType.UpdateByObject:
                                baseEntity.Modifier = "System";
                                break;
                            case DataFilterType.InsertByObject:
                                baseEntity.Creater = "System";
                                break;
                        }
                    }
                }
                catch (NullReferenceException)
                {
                    switch (entityInfo.OperationType)
                    {
                        case DataFilterType.UpdateByObject:
                            baseEntity.Modifier = "System";
                            break;
                        case DataFilterType.InsertByObject:
                            baseEntity.Creater = "System";
                            break;
                    }
                }
            }
 
            if (entityInfo.EntityValue is IBaseHistoryEntity baseHistoryEntity)
            {
                if (entityInfo.OperationType == DataFilterType.InsertByObject && entityInfo.PropertyName == nameof(IBaseHistoryEntity.InsertTime))
                {
                    baseHistoryEntity.InsertTime = DateTime.Now;
                }
            }
        }
    }
}