ningshuxia
2022-12-12 4f1314cb69a47c22e52f1efdc4b4be6c1d55143d
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
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
 
namespace IStation.DAL
{
    /// <summary>
    /// 
    /// </summary>
    public partial class SpecialPropertyValue : CorpDAL<Entity.SpecialPropertyValue> 
    {
        /// <summary>
        /// 
        /// </summary>
        public override ConnectionConfig ConnectionConfig
        {
            get { return ConfigHelper.DefaultConnectionConfig; }
        }
 
        /// <summary>
        /// 通过 ObjectType 和 ObjectID 获取
        /// </summary>
        public List<Entity.SpecialPropertyValue> GetByObjectTypeAndObjectID(long CorpID, string ObjectType, long ObjectID)
        {
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                return db.Queryable<Entity.SpecialPropertyValue>()
                    .Where(x => x.CorpID == CorpID && x.ObjectType==IStation.ObjectType.Station&&x.ObjectID==ObjectID).ToList();
            }
        }
 
        /// <summary>
        /// 通过 Object 和 PropertyID 获取
        /// </summary>
        public Entity.SpecialPropertyValue GetObjectAndPropertyID(long CorpID, string ObjectType, long ObjectID, long PropertyID)
        {
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                return db.Queryable<Entity.SpecialPropertyValue>()
                    .First(x => x.CorpID == CorpID && x.ObjectType == IStation.ObjectType.Station && x.ObjectID == ObjectID&&x.PropertyID== PropertyID);
            }
        }
 
        /// <summary>
        /// 通过 PropertyID 获取
        /// </summary>
        public List<Entity.SpecialPropertyValue> GetByPropertyID(long CorpID, long PropertyID)
        {
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                return db.Queryable<Entity.SpecialPropertyValue>()
                    .Where(x => x.CorpID == CorpID && x.PropertyID==PropertyID).ToList();
            }
        }
 
        /// <summary>
        /// 通过 PropertyIds 获取
        /// </summary>
        public List<Entity.SpecialPropertyValue> GetByPropertyIds(long CorpID, List<long> PropertyIds)
        {
            if (PropertyIds == null || PropertyIds.Count < 1)
                return default;
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                return db.Queryable<Entity.SpecialPropertyValue>()
                    .Where(x => x.CorpID == CorpID && PropertyIds.Contains(x.PropertyID)).ToList();
            }
        }
 
        /// <summary>
        /// 设置
        /// </summary>
        public bool Set(List<Entity.SpecialPropertyValueSetter> list)
        {
            if (list == null || list.Count() < 1)
                return false;
            var corpIds = list.Select(x => x.CorpID).Distinct().ToList();
            if (corpIds.Count != 1)
                return false;
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                try
                {
                    db.BeginTran();
                    foreach (var item in list)
                    {
                        var bol = db.Queryable<Entity.SpecialPropertyValue>()
                            .Where(x => x.CorpID == item.CorpID && x.ObjectType == item.ObjectType && x.ObjectID==item.ObjectID && x.PropertyID == item.PropertyID).Count() > 0;
                        if (bol)//已存在
                        {
                            var result = db.Updateable<Entity.SpecialPropertyValue>()
                                                .SetColumns(x => x.PropertyValue== item.PropertyValue)
                                                .Where(x => x.CorpID == item.CorpID&&x.ObjectType==item.ObjectType&&x.ObjectID==item.ObjectID && x.PropertyID == item.PropertyID)
                                                .ExecuteCommand() > 0;
                            if (!result)
                            {
                                db.RollbackTran();
                                return false;
                            }
                        }
                        else//不存在
                        {
                            var entity = new Entity.SpecialPropertyValue();
                            entity.CorpID = item.CorpID;
                            entity.ObjectType = item.ObjectType;
                            entity.ObjectID = item.ObjectID;
                            entity.PropertyID = item.PropertyID;
                            entity.PropertyValue = item.PropertyValue;
                            var id = db.Insertable(entity).ExecuteReturnSnowflakeId();
                            if (id < 1)
                            {
                                db.RollbackTran();
                                return false;
                            }
                        }
                    }
                    db.CommitTran();
                    return true;
                }
                catch
                {
                    db.RollbackTran();
                    throw;
                }
            }
        }
 
 
 
 
    }
}