Esempio n. 1
0
 /// <summary>
 /// 根据属性索引数据
 /// </summary>
 /// <typeparam name="TValue"></typeparam>
 /// <param name="master"></param>
 /// <param name="joinAssTable"></param>
 /// <param name="fieldValue">关联字段的值</param>
 /// <returns></returns>
 private static object IndexEntityObj <TValue>(TValue master, JoinAssTable joinAssTable, object fieldValue)
     where TValue : IBaseEntity
 {
     PropertyInfo[] properties = master.GetType().GetProperties();
     foreach (var item in properties)
     {
         //找到相同属性
         if (item == joinAssTable.PropertyInfo)
         {
             return(master);
         }
         else
         {
             var masterData = item.GetValue(master);
             if (masterData != null)
             {
                 var masterType = masterData.GetType();
                 //实体类型
                 Type entityType;
                 if (ExpressionExtension.IsAnyBaseEntity(masterType, out entityType))
                 {
                     //list
                     if (masterType.FullName.Contains("System.Collections.Generic"))
                     {
                         foreach (var obj in masterData as IEnumerable <IBaseEntity> )
                         {
                             //从list中查找对应的属性
                             var listData = IndexEntityObj(obj, joinAssTable, fieldValue);
                             if (listData != null)
                             {
                                 //验证主外键数据是否关联
                                 if (obj.GetType().GetProperty(joinAssTable.RightAssName).GetValue(obj).Equals(fieldValue))
                                 {
                                     return(obj);
                                 }
                             }
                         }
                     }
                     else
                     {
                         return(IndexEntityObj((TValue)masterData, joinAssTable, fieldValue));
                     }
                 }
             }
         }
     }
     return(null);
 }
Esempio n. 2
0
        /// <summary>
        /// 字段处理
        /// </summary>
        /// <param name="masterSql"></param>
        /// <param name="joinAssTable"></param>
        /// <param name="joinEntity"></param>
        /// <returns></returns>
        private string FieldDetailWith(ref string masterSql, JoinAssTable joinAssTable, EntityObject joinEntity)
        {
            StringBuilder sqlBuilder = new StringBuilder();
            //表名称
            string joinTableName = joinEntity.AsName == joinEntity.Name ? providerOption.CombineFieldName(joinEntity.Name) : joinEntity.AsName;
            //查询的字段
            var fieldPairs = joinAssTable.SelectFieldPairs != null && joinAssTable.SelectFieldPairs.Any() ? joinAssTable.SelectFieldPairs : joinEntity.FieldPairs;

            foreach (string fieldValue in fieldPairs.Values)
            {
                if (masterSql.LastIndexOf(',') == masterSql.Length - 1 && sqlBuilder.Length == 0)
                {
                    sqlBuilder.Append($"{joinTableName}.");
                }
                else
                {
                    //首先添加表名称
                    sqlBuilder.Append($",{joinTableName}.");
                }
                //字段
                string field = providerOption.CombineFieldName(fieldValue);
                //字符出现的次数
                int repeatCount = masterSql.Split(new string[] { field }, StringSplitOptions.None).Length - 1;
                //添加字段
                sqlBuilder.Append(field);
                if (repeatCount > 0)
                {
                    sqlBuilder.Append($" AS {fieldValue}_{repeatCount}");
                    joinAssTable.MapperList.Add(fieldValue, $"{fieldValue}_{repeatCount}");
                }
                else
                {
                    joinAssTable.MapperList.Add(fieldValue, fieldValue);
                }
            }
            var joinEntityType = joinAssTable.IsDto == false ? joinEntity.Type : joinAssTable.DtoType;

            //重新注册实体映射
            SqlMapper.SetTypeMap(joinEntityType, new CustomPropertyTypeMap(joinEntityType,
                                                                           (type, column) =>
                                                                           type.GetPropertys(joinAssTable.MapperList.FirstOrDefault(x => x.Value.Equals(column)).Key)
                                                                           ), true);
            //设置sql字段
            masterSql += sqlBuilder;
            return(masterSql);
        }