context: using System.Collections.Generic;
  using System.Text;
  using System.Threading.Tasks;
  using Dapper;
  using Datory;
  using Datory.Utils;
  using SSCMS.Core.Utils;
  using SSCMS.Models;
  using SSCMS.Utils;
  
  namespace SSCMS.Core.Repositories
  {
      public partial class ContentRepository
      {
          public string GetStlWhereString(int siteId, string group, string groupNot, string tags, bool isImageExists, bool isImage, bool isVideoExists, bool isVideo, bool isFileExists, bool isFile, bool isTopExists, bool isTop, bool isRecommendExists, bool isRecommend, bool isHotExists, bool isHot, bool isColorExists, bool isColor, string where)
          {
              var whereBuilder = new StringBuilder();
              whereBuilder.Append($" AND SiteId = {siteId} ");
  
              if (isImageExists)
              {
                  whereBuilder.Append(isImage
                      ? $" AND {nameof(Content.ImageUrl)} <> '' "
                      : $" AND {nameof(Content.ImageUrl)} = '' ");
              }
  
              if (isVideoExists)
              {
                  whereBuilder.Append(isVideo
                      ? $" AND {nameof(Content.VideoUrl)} <> '' "
                      : $" AND {nameof(Content.VideoUrl)} = '' ");
              }
  
              if (isFileExists)
              {
                  whereBuilder.Append(isFile
                      ? $" AND {nameof(Content.FileUrl)} <> '' "
                      : $" AND {nameof(Content.FileUrl)} = '' ");
              }
  
              if (isTopExists)
              {
                  whereBuilder.Append($" AND {nameof(Content.Top)} = {StringUtils.ToLower(isTop.ToString())} ");
              }
  
              if (isRecommendExists)
              {
                  whereBuilder.Append($" AND {nameof(Content.Recommend)} = {StringUtils.ToLower(isRecommend.ToString())} ");
              }
  
              if (isHotExists)
              {
                  whereBuilder.Append($" AND {nameof(Content.Hot)} = {StringUtils.ToLower(isHot.ToString())} ");
              }
  
              if (isColorExists)
              {
                  whereBuilder.Append($" AND {nameof(Content.Color)} = {StringUtils.ToLower(isColor.ToString())} ");
              }
  
              var database = _settingsManager.Database;
  
              if (!string.IsNullOrEmpty(group))
              {
                  group = group.Trim().Trim(',');
                  var groups = ListUtils.GetStringList(group);
                  if (groups.Count > 0)
                  {
                      whereBuilder.Append(" AND (");
                      foreach (var theGroup in groups)
                      {
                          var trimGroup = theGroup.Trim();
  
                          whereBuilder.Append(
                                  $" ({nameof(Content.GroupNames)} = '{Utilities.FilterSql(trimGroup)}' OR {DatabaseUtils.GetInStr(database, nameof(Content.GroupNames), trimGroup + ",")} OR {DatabaseUtils.GetInStr(database, nameof(Content.GroupNames), "," + trimGroup + ",")} OR {DatabaseUtils.GetInStr(database, nameof(Content.GroupNames), "," + trimGroup)}) OR ");
                      }
                      whereBuilder.Length -= 3;
                      whereBuilder.Append(") ");
                  }
              }
  
              if (!string.IsNullOrEmpty(groupNot))
              {
                  groupNot = groupNot.Trim().Trim(',');
                  var groupNots = ListUtils.GetStringList(groupNot);
                  if (groupNots.Count > 0)
                  {
                      whereBuilder.Append(" AND (");
                      foreach (var theGroupNot in groupNots)
                      {
                          var trimGroup = theGroupNot.Trim();
                          //whereBuilder.Append(
                          //    $" ({ContentAttribute.GroupNameCollection} <> '{trimGroup}' AND CHARINDEX('{trimGroup},',{ContentAttribute.GroupNameCollection}) = 0 AND CHARINDEX(',{trimGroup},',{ContentAttribute.GroupNameCollection}) = 0 AND CHARINDEX(',{trimGroup}',{ContentAttribute.GroupNameCollection}) = 0) AND ");
  
                          whereBuilder.Append(
                                  $" ({nameof(Content.GroupNames)} <> '{trimGroup}' AND {DatabaseUtils.GetNotInStr(database, nameof(Content.GroupNames), trimGroup + ",")} AND {DatabaseUtils.GetNotInStr(database, nameof(Content.GroupNames), "," + trimGroup + ",")} AND {DatabaseUtils.GetNotInStr(database, nameof(Content.GroupNames), "," + trimGroup)}) AND ");
                      }
                      whereBuilder.Length -= 4;
                      whereBuilder.Append(") ");
                  }
              }
  
              if (!string.IsNullOrEmpty(tags))
              {
                  tags = tags.Trim().Trim(',');
                  var tagNames = ListUtils.GetStringList(tags);
                  if (tagNames.Count > 0)
                  {
                      whereBuilder.Append(" AND (");
                      foreach (var tagName in tagNames)
                      {
                          whereBuilder.Append(
                              $" ({nameof(Content.TagNames)} = '{Utilities.FilterSql(tagName)}' OR {DatabaseUtils.GetInStr(database, nameof(Content.TagNames), tagName + ",")} OR {DatabaseUtils.GetInStr(database, nameof(Content.TagNames), "," + tagName + ",")} OR {DatabaseUtils.GetInStr(database, nameof(Content.TagNames), "," + tagName)}) OR ");
                      }
                      whereBuilder.Length -= 3;
                      whereBuilder.Append(") ");
                  }
              }
  
              if (!string.IsNullOrEmpty(where))
              {
                  whereBuilder.Append($" AND ({where}) ");
              }
  
              return whereBuilder.ToString();
          }
  
          public string GetStlWhereStringBySearch(string group, string groupNot, bool isImageExists, bool isImage, bool isVideoExists, bool isVideo, bool isFileExists, bool isFile, bool isTopExists, bool isTop, bool isRecommendExists, bool isRecommend, bool isHotExists, bool isHot, bool isColorExists, bool isColor, string where)
          {
              var whereBuilder = new StringBuilder();
  
              if (isImageExists)
              {
                  whereBuilder.Append(isImage
                      ? $" AND {nameof(Content.ImageUrl)} <> '' "
                      : $" AND {nameof(Content.ImageUrl)} = '' ");
              }
  
              if (isVideoExists)
              {
                  whereBuilder.Append(isVideo
                      ? $" AND {nameof(Content.VideoUrl)} <> '' "
                      : $" AND {nameof(Content.VideoUrl)} = '' ");
              }
  
              if (isFileExists)
              {
                  whereBuilder.Append(isFile
                      ? $" AND {nameof(Content.FileUrl)} <> '' "
                      : $" AND {nameof(Content.FileUrl)} = '' ");
              }
  
              if (isTopExists)
              {
                  whereBuilder.Append($" AND {nameof(Content.Top)} = {StringUtils.ToLower(isTop.ToString())} ");
              }
  
              if (isRecommendExists)
              {
                  whereBuilder.Append($" AND {nameof(Content.Recommend)} = {StringUtils.ToLower(isRecommend.ToString())} ");
              }
  
              if (isHotExists)
              {
                  whereBuilder.Append($" AND {nameof(Content.Hot)} = {StringUtils.ToLower(isHot.ToString())} ");
              }
  
              if (isColorExists)
              {
                  whereBuilder.Append($" AND {nameof(Content.Color)} = {StringUtils.ToLower(isColor.ToString())} ");
              }
  
              var database = _settingsManager.Database;
  
              if (!string.IsNullOrEmpty(group))
              {
                  group = group.Trim().Trim(',');
                  var groupArr = group.Split(',');
                  if (groupArr != null && groupArr.Length > 0)
                  {
                      whereBuilder.Append(" AND (");
                      foreach (var theGroup in groupArr)
                      {
                          var trimGroup = theGroup.Trim();
  
                          whereBuilder.Append(
                                  $" ({nameof(Content.GroupNames)} = '{Utilities.FilterSql(trimGroup)}' OR {DatabaseUtils.GetInStr(database, nameof(Content.GroupNames), trimGroup + ",")} OR {DatabaseUtils.GetInStr(database, nameof(Content.GroupNames), "," + trimGroup + ",")} OR {DatabaseUtils.GetInStr(database, nameof(Content.GroupNames), "," + trimGroup)}) OR ");
                      }
                      if (groupArr.Length > 0)
                      {
                          whereBuilder.Length -= 3;
                      }
                      whereBuilder.Append(") ");
                  }
              }
  
              if (!string.IsNullOrEmpty(groupNot))
              {
                  groupNot = groupNot.Trim().Trim(',');
                  var groupNotArr = groupNot.Split(',');
  
gt:                 if (groupNotArr != null && groupNotArr.Length > 0)