123
ningshuxia
2023-04-13 eb32b4263f6901bb7ac1915b400e4fe28db20ef0
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
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
 
namespace IStation.DAL
{
    /// <summary>
    /// 测点映射
    /// </summary>
    public partial class MonitorPointMapping : CorpDAL_Sorter<Entity.MonitorPointMapping>
    {
        /// <summary>
        /// 
        /// </summary>
        public override ConnectionConfig ConnectionConfig
        {
            get { return ConfigHelper.DefaultConnectionConfig; }
        }
 
        /// <summary>
        /// 通过 MonitorPointID 获取
        /// </summary>
        public List<Entity.MonitorPointMapping> GetByMonitorPointID(long CorpID, long MonitorPointID)
        {
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                return db.Queryable<Entity.MonitorPointMapping>()
                    .Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID).ToList();
            }
        }
 
        /// <summary>
        /// 通过 MonitorPointIds 获取
        /// </summary>
        public List<Entity.MonitorPointMapping> GetByMonitorPointIds(long CorpID, List<long> MonitorPointIds)
        {
            if (MonitorPointIds == null || MonitorPointIds.Count < 1)
                return default;
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                return db.Queryable<Entity.MonitorPointMapping>()
                    .Where(x => x.CorpID == CorpID && MonitorPointIds.Contains(x.MonitorPointID)).ToList();
            }
        }
 
        /// <summary>
        /// 通过 ObjectType 和 ObjectID 获取
        /// </summary>
        public List<Entity.MonitorPointMapping> GetByObjectTypeAndObjectID(long CorpID, string ObjectType, long ObjectID)
        {
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                return db.Queryable<Entity.MonitorPointMapping>()
                    .Where(x => x.CorpID == CorpID && x.ObjectType == ObjectType && x.ObjectID == ObjectID).ToList();
            }
        }
 
        /// <summary>
        /// 更新 AccordParas
        /// </summary>
        public bool UpdateAccordParas(long CorpID, long ID, string AccordParas)
        {
            using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
            {
                return db.Updateable<Entity.MonitorPointMapping>()
                    .SetColumns(x => x.AccordParas == AccordParas)
                    .Where(x => x.CorpID == CorpID && x.ID == ID)
                    .ExecuteCommand() > 0;
            }
        }
 
        /// <summary>
        /// 通过 MonitorPointID 删除
        /// </summary>
        public bool DeleteByMonitorPointID(long CorpID, long MonitorPointID)
        {
            using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
            {
                return db.Deleteable<Entity.MonitorPointMapping>()
                    .Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID)
                    .ExecuteCommand() > 0;
            }
        }
 
        /// <summary>
        /// 通过 ObjectType 和 ObjectID 删除
        /// </summary>
        public bool DeleteByObjectTypeAndObjectID(long CorpID, string ObjectType, long ObjectID)
        {
            using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
            {
                return db.Deleteable<Entity.MonitorPointMapping>()
                    .Where(x => x.CorpID == CorpID && x.ObjectType == ObjectType && x.ObjectID == ObjectID)
                    .ExecuteCommand() > 0;
            }
        }
 
        /// <summary>
        /// 通过 ObjectType 和 ObjectID 设置
        /// </summary>
        public bool SetOfObject(long CorpID, string ObjectType, long ObjectID, List<Entity.MonitorPointMapping> list)
        {
            if (list == null || list.Count() < 1)
            {
                using (var db = new SqlSugarClient(ConnectionConfig))
                {
                    return db.Deleteable<Entity.MonitorPointMapping>()
                        .Where(x => x.CorpID == CorpID && x.ObjectType == ObjectType && x.ObjectID == ObjectID)
                        .ExecuteCommand() > 0;
                }
            }
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                try
                {
                    db.BeginTran();
                    var add_list = list.Where(x => x.ID < 1).ToList();
                    var update_list = list.Where(x => x.ID > 0).ToList();
                    var ids_exist = update_list.Select(x => x.ID).Distinct().ToList();
                    db.Deleteable<Entity.MonitorPointMapping>()
                        .Where(x => x.CorpID == CorpID && x.ObjectType == ObjectType && x.ObjectID == ObjectID && !ids_exist.Contains(x.ID))
                        .ExecuteCommand();
                    if (add_list.Count > 0)
                    {
                        var result = db.Insertable(add_list).ExecuteReturnSnowflakeIdList().Count > 0;
                        if (!result)
                        {
                            db.RollbackTran();
                            return default;
                        }
                    }
                    if (update_list.Count > 0)
                    {
                        var result = db.Updateable(update_list).ExecuteCommand() > 0;
                        if (!result)
                        {
                            db.RollbackTran();
                            return default;
                        }
                    }
                    db.CommitTran();
                    return true;
                }
                catch
                {
                    db.RollbackTran();
                    throw;
                }
 
            }
        }
 
 
    }
}