Commit 755531ef by lizefeng

新增一个查询试试缺货的接口

parent f77045e2
using AutoTurnOver.Models.Base;
using Dapper;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace AutoTurnOver.DB.Base
{
public static class DbHelper
{
public static Page<T> Page<T>(this MySqlConnection conn, string sql, page_search_dto search_data, object param = null, int? commandTimeout = null)
{
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();//开始计时
var result_data = new Page<T>();
var item_sql = sql;
if (!string.IsNullOrWhiteSpace(search_data.sidx) && !string.IsNullOrWhiteSpace(search_data.sord) && !item_sql.Contains("order by"))
{
item_sql += $" order by {search_data.sidx} {search_data.sord} ";
}
item_sql += $" limit {(search_data.page - 1) * search_data.rows},{search_data.rows} ";
result_data.Items = (connectionHelper._connection.Query<T>(item_sql, param, commandTimeout: commandTimeout)).AsList();
var original_sql = sql;
while (true)
{
Match match = Regex.Match(sql, @"select[\s\S]+from");
if (match.Success)
{
if (match.Groups.Count > 1)
{
throw new Exception(" 分页语句解析异常 ");
}
if (SubstringCount(match.Groups[0].Value, "from") > 1)
{
int remove_Index = match.Groups[0].Value.LastIndexOf("from");
sql = match.Groups[0].Value.Remove(remove_Index);
}
else
{
sql = original_sql.Replace(match.Groups[0].Value, " select count(1) from ");
break;
}
}
else
{
throw new Exception(" 分页语句解析异常 ");
}
}
result_data.TotalItems = connectionHelper._connection.QueryFirstOrDefault<int?>(sql, param, commandTimeout: commandTimeout) ?? 0;
result_data.CurrentPage = search_data.page;
result_data.TotalPages = (int)Math.Ceiling(result_data.TotalItems * 1.0 / search_data.rows);
watch.Stop();
result_data.CostTime = watch.ElapsedMilliseconds;
return result_data;
}
public static async Task<Page<T>> PageAsync<T>(this MySqlConnection conn, string sql, page_search_dto search_data, object param = null, int? commandTimeout = null)
{
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();//开始计时
var result_data = new Page<T>();
var item_sql = sql;
if (!string.IsNullOrWhiteSpace(search_data.sidx) && !string.IsNullOrWhiteSpace(search_data.sord) && !item_sql.Contains("order by"))
{
item_sql += $" order by {search_data.sidx} {search_data.sord} ";
}
item_sql += $" limit {(search_data.page - 1) * search_data.rows},{search_data.rows} ";
result_data.Items = (await connectionHelper._connection.QueryAsync<T>(item_sql, param, commandTimeout: commandTimeout)).AsList();
var original_sql = sql;
while (true)
{
Match match = Regex.Match(sql, @"select[\s\S]+from");
if (match.Success)
{
if (match.Groups.Count > 1)
{
throw new Exception(" 分页语句解析异常 ");
}
if (SubstringCount(match.Groups[0].Value, "from") > 1)
{
int remove_Index = match.Groups[0].Value.LastIndexOf("from");
sql = match.Groups[0].Value.Remove(remove_Index);
}
else
{
sql = original_sql.Replace(match.Groups[0].Value, " select count(1) from ");
break;
}
}
else
{
throw new Exception(" 分页语句解析异常 ");
}
}
result_data.TotalItems = await connectionHelper._connection.QueryFirstOrDefaultAsync<int?>(sql, param, commandTimeout: commandTimeout) ?? 0;
result_data.CurrentPage = search_data.page;
result_data.TotalPages = (int)Math.Ceiling(result_data.TotalItems * 1.0 / search_data.rows);
watch.Stop();
result_data.CostTime = watch.ElapsedMilliseconds;
return result_data;
}
/// <summary>
/// 计算字符串中子串出现的次数
/// </summary>
/// <param name="str">字符串</param>
/// <param name="substring">子串</param>
/// <returns>出现的次数</returns>
static int SubstringCount(string str, string substring)
{
if (str.Contains(substring))
{
string strReplaced = str.Replace(substring, "");
return (str.Length - strReplaced.Length) / substring.Length;
}
return 0;
}
}
}
using AutoTurnOver.DB.Base;
using AutoTurnOver.Models;
using AutoTurnOver.Models.Base;
using Dapper;
using System;
using System.Collections.Generic;
using System.Text;
namespace AutoTurnOver.DB
{
public class api_dao : connectionHelper
{
/// <summary>
/// 获取缺货数据
/// </summary>
/// <param name="search"></param>
/// <returns></returns>
public static Page<dc_quantity_out_stock_dto> GetOutStockPage(dc_quantity_out_stock_search_dto search)
{
string sql = " select * from dc_mid_transit as t1 where 1=1 ";
DynamicParameters parameters = new DynamicParameters();
if (search != null)
{
if (search.btime != null)
{
sql += " and t1.gmt_out_stock_modified>=@btime ";
parameters.Add("btime",search.btime);
}
if (search.etime != null)
{
sql += " and t1.gmt_out_stock_modified<=@etime ";
parameters.Add("etime", search.etime);
}
}
return _connection.Page<dc_quantity_out_stock_dto>(sql, search, parameters);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace AutoTurnOver.Models.Base
{
public class Page<T>
{
/// <summary>
/// The current page number contained in this page of result set
/// </summary>
public long CurrentPage { get; set; }
/// <summary>
/// The total number of pages in the full result set
/// </summary>
public long TotalPages { get; set; }
/// <summary>
/// The total number of records in the full result set
/// </summary>
public long TotalItems { get; set; }
/// <summary>
/// The number of items per page
/// </summary>
public long ItemsPerPage { get; set; }
/// <summary>
/// The actual records on this page
/// </summary>
public List<T> Items { get; set; }
/// <summary>
/// User property to hold anything.
/// </summary>
public object Context { get; set; }
public long CostTime { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace AutoTurnOver.Models.Base
{
public class page_search_dto
{
public int page { get; set; }
public int rows { get; set; }
public string sord { get; set; }
public string sidx { get; set; }
}
}
using AutoTurnOver.Models.Base;
using System;
using System.Collections.Generic;
using System.Text;
namespace AutoTurnOver.Models
{
public class dc_quantity_out_stock_dto
{
public int id { get; set; }
public string bailun_sku { get; set; }
public string warehouse_code { get; set; }
public decimal quantity_out_stock { get; set; }
public DateTime gmt_out_stock_modified { get; set; }
}
public class dc_quantity_out_stock_search_dto : page_search_dto
{
public DateTime? btime { get; set; }
public DateTime? etime { get; set; }
}
}
......@@ -9,6 +9,7 @@ using System.Linq;
using AutoTurnOver.Models;
using AutoTurnOver.DB;
using System.Text.RegularExpressions;
using AutoTurnOver.Models.Base;
namespace AutoTurnOver.Services
{
......@@ -707,5 +708,15 @@ namespace AutoTurnOver.Services
return datas;
}
/// <summary>
/// 获取缺货数据
/// </summary>
/// <param name="search"></param>
/// <returns></returns>
public static Page<dc_quantity_out_stock_dto> GetOutStockPage(dc_quantity_out_stock_search_dto search)
{
return api_dao.GetOutStockPage(search);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoTurnOver.Common;
using AutoTurnOver.Models;
using AutoTurnOver.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace AutoTurnOver.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ApiController : ControllerBase
{
[HttpPost("GetPurchaseSkuPage")]
public ActionResult<RequestResultDto> GetOutStockPage([FromBody] dc_quantity_out_stock_search_dto search)
{
return new RequestResultDto { success = true, data = ApiServices.GetOutStockPage(search) };
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment