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);
        }

 

posted on 2021-05-27 08:32  不知勿言  阅读(84)  评论(0编辑  收藏  举报