Пример #1
0
        /// <inheritdoc />
        public virtual SqlQuery GetUpdate(Expression <Func <TEntity, bool> > predicate, TEntity entity, object userId)
        {
            var query = new SqlQuery(entity);

            var cacheKey = _sqlCacheKey + "GetUpdateByExpression";

            if (StringCache.ContainsKey(cacheKey))
            {
                query.SqlBuilder.Append(StringCache[cacheKey]);
            }
            else
            {
                var properties = SqlProperties.Where(p => !KeySqlProperties.Any(k => k.Name.Equals(p.Name, StringComparison.OrdinalIgnoreCase)));
                query.SqlBuilder.Append($"UPDATE {TableName} SET {string.Join(", ", properties.Select(p => $"{GetPropertyName(p.Name)} = @{p.Name}"))} ");

                StringCache.TryAdd(cacheKey, query.GetSql());
            }

            AppendWherePredicateQuery(query, predicate);

            //处理公共字段
            var commonValues = Config.UpdateCommonProperty(userId);

            foreach (var common in commonValues)
            {
                if (query.Param.Keys.Contains(common.Key))
                {
                    query.Param[common.Key] = common.Value;
                }
                else
                {
                    query.Param.Add(common.Key, common.Value);
                }
            }

            return(query);
        }