zoukankan      html  css  js  c++  java
  • 带有历史数据置顶的id列表查询

            public async Task<Tuple<List<H_Port>, int>> GetList(CommonPortSearchRequest request)
            {
    
                StringBuilder sbSql = new StringBuilder();
                StringBuilder sbCount = new StringBuilder();
                DynamicParameters paramters = new DynamicParameters();
                string strWhere = GetParameters(request, paramters);
                List<H_Port> retVals = new List<H_Port>();
    
                if (request.PortIds?.Count > 0)
                {
                    paramters.Add("@Ids", request.PortIds);
                    string strIds = string.Join(",", request.PortIds.Select(t => $"'{t.ToString()}'"));
                    sbSql.Append(" SELECT p.* from h_ports p where  p.IsDeleted = false  ");
                    sbSql.Append(strWhere);
                    sbSql.Append($" AND p.id in @Ids ORDER BY FIELD(p.id,{strIds}) ");
                    var list = (await ReadConnection.QueryAsync<H_Port>(sbSql.ToString(), paramters))?.ToList();
                    int startRow = request.Skip;
                    if (list.Count > startRow)
                    {
                        retVals.AddRange(list.Skip(startRow).Take(request.Count));
                        //取剩余数据
                        int remainCount = request.Count - retVals.Count;
                        if (remainCount > 0)
                        {
                            sbSql.Clear();
                            sbSql.Append(" SELECT p.* from h_ports p where  p.IsDeleted = false  ");
                            sbSql.Append(strWhere);
                            sbSql.Append($" AND p.id not in @Ids order by p.CreationTime desc limit 0,{remainCount} ");
                            list = (await ReadConnection.QueryAsync<H_Port>(sbSql.ToString(), paramters))?.ToList();
                            retVals.AddRange(list);
                        }
                    }
                    //如果相等就跳过这些数据
                    else if (list.Count == startRow)
                    {
                        sbSql.Clear();
                        sbSql.Append(" SELECT p.* from h_ports p where  p.IsDeleted = false  ");
                        sbSql.Append(strWhere);
                        sbSql.Append($" AND p.id not in @Ids order by p.CreationTime desc limit 0,{request.Count} ");
                        list = (await ReadConnection.QueryAsync<H_Port>(sbSql.ToString(), paramters))?.ToList();
                        retVals.AddRange(list);
                    }
                    else
                    {
                        int inCount = list.Count;
                        int needSkip = startRow - inCount;
                        sbSql.Clear();
                        sbSql.Append(" SELECT p.* from h_ports p where  p.IsDeleted = false  ");
                        sbSql.Append(strWhere);
                        sbSql.Append($" AND p.id not in @Ids order by p.CreationTime desc limit {needSkip},{request.Count} ");
                        list = (await ReadConnection.QueryAsync<H_Port>(sbSql.ToString(), paramters))?.ToList();
                        retVals.AddRange(list);
                    }               
                }
                else
                {
                    sbSql.Append(" SELECT p.* FROM h_ports p WHERE p.IsDeleted = false ");
                    sbSql.Append(strWhere);
                    sbSql.Append(" ORDER BY p.CreationTime DESC ");
                    //分页
                    if (request?.Count > 0)
                    {
                        sbSql.Append(" LIMIT " + request.Skip + "," + request.Count);
                    }
                    if (retVals.Count <= 0)
                    {
                        retVals.AddRange(await ReadConnection.QueryAsync<H_Port>(sbSql.ToString(), paramters));
                    }
                }
                sbCount.Append(" SELECT Count(p.Id) FROM h_ports p WHERE p.IsDeleted = false ");
                sbCount.Append(strWhere);    
               
                int count = 0;
                if (request?.Count > 0)
                {
                    count = await ReadConnection.QueryFirstOrDefaultAsync<int>(sbCount.ToString(), paramters);
                }
                return new Tuple<List<H_Port>, int>(retVals, count);
            }
  • 相关阅读:
    Selenium操作之滚动条
    IntelliJ IDEA代码编码区提示库源不匹配字节码解决办法
    自动化测试浅谈
    json-lib解析json之二维JSONArray
    Java 多态
    Java0基础教程——java的安装
    LAYUI弹出层详解
    ajax实现动态URL
    js serialize()
    TP5.1接入支付宝实现网页/APP支付完整请求回调流程(沙箱环境)
  • 原文地址:https://www.cnblogs.com/yiyanwei/p/14816353.html
Copyright © 2011-2022 走看看