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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
 
namespace IStation.DAL
{
    /// <summary>
    ///  附加属性映射
    /// </summary>
    public partial class SpecialPropertyMapping : CorpDAL<Entity.SpecialPropertyMapping>
    {
        /// <summary>
        /// 
        /// </summary>
        public override ConnectionConfig ConnectionConfig
        {
            get { return ConfigHelper.DefaultConnectionConfig; }
 
        }
 
        /// <summary>
        ///  通过 CatalogID 获取 
        /// </summary>
        public List<Entity.SpecialPropertyMapping> GetByCatalogID(long CorpID, long CatalogID)
        {
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                return db.Queryable<Entity.SpecialPropertyMapping>()
                    .Where(x => x.CorpID == CorpID && x.CatalogID == CatalogID).ToList();
            }
        }
 
        /// <summary>
        ///  通过 CatalogIds 获取 
        /// </summary>
        public List<Entity.SpecialPropertyMapping> GetByCatalogIds(long CorpID, List<long> CatalogIds)
        {
            if (CatalogIds == null || CatalogIds.Count() < 1)
                return default;
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                return db.Queryable<Entity.SpecialPropertyMapping>()
                    .Where(x => x.CorpID == CorpID && CatalogIds.Contains(x.CatalogID)).ToList();
            }
        }
 
        /// <summary>
        ///  通过 PropertyID 获取 
        /// </summary>
        public List<Entity.SpecialPropertyMapping> GetByPropertyID(long CorpID, long PropertyID)
        {
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                return db.Queryable<Entity.SpecialPropertyMapping>()
                    .Where(x => x.CorpID == CorpID && x.PropertyID == PropertyID).ToList();
            }
        }
 
        /// <summary>
        ///  通过 PropertyIds 获取 
        /// </summary>
        public List<Entity.SpecialPropertyMapping> GetByPropertyIds(long CorpID, List<long> PropertyIds)
        {
            if (PropertyIds == null || PropertyIds.Count() < 1)
                return default;
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                return db.Queryable<Entity.SpecialPropertyMapping>()
                    .Where(x => x.CorpID == CorpID && PropertyIds.Contains(x.PropertyID)).ToList();
            }
        }
 
        /// <summary>
        /// 通过 CatalogID 和 PropertyID 获取 
        /// </summary>
        public Entity.SpecialPropertyMapping GetByCatalogIDAndPropertyID(long CorpID, long CatalogID, long PropertyID)
        {
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                return db.Queryable<Entity.SpecialPropertyMapping>()
                    .First(x => x.CorpID == CorpID && x.CatalogID == CatalogID && x.PropertyID == PropertyID);
            }
        }
 
        /// <summary>
        /// 设置
        /// </summary>
        public bool Set(List<Entity.SpecialPropertyMappingSelected> 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.SpecialPropertyMapping>()
                            .Where(x => x.CorpID == item.CorpID && x.CatalogID == item.CatalogID && x.PropertyID == item.PropertyID).Count() > 0;
                        if (bol)//已存在
                        {
                            if (!item.Selected)//未选择
                            {
                                var result = db.Deleteable<Entity.SpecialPropertyMapping>()
                                    .Where(x => x.CorpID == item.CorpID && x.CatalogID == item.CatalogID && x.PropertyID == item.PropertyID).ExecuteCommandHasChange();
                                if (!result)
                                {
                                    db.RollbackTran();
                                    return false;
                                }
                            }
                        }
                        else//不存在
                        {
                            if (item.Selected)//选择
                            {
                                var entity = new Entity.SpecialPropertyMapping();
                                entity.CorpID = item.CorpID;
                                entity.CatalogID = item.CatalogID;
                                entity.PropertyID = item.PropertyID;
                                var id = db.Insertable(entity).ExecuteReturnSnowflakeId();
                                if (id < 1)
                                {
                                    db.RollbackTran();
                                    return false;
                                }
                            }
                        }
                    }
                    db.CommitTran();
                    return true;
                }
                catch
                {
                    db.RollbackTran();
                    throw;
                }
            }
        }
 
 
    }
}