ningshuxia
2022-12-12 e78f5936fee9ab4fff600515bb20a41a28f329c4
DAL/IStation.DAL.Basic/property/SpecialPropertyMapping.cs
@@ -7,7 +7,7 @@
namespace IStation.DAL
{
    /// <summary>
    ///
    ///  附加属性映射
    /// </summary>
    public partial class SpecialPropertyMapping : CorpDAL<Entity.SpecialPropertyMapping>
    {
@@ -20,7 +20,129 @@
        }
        /// <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;
                }
            }
        }
    }