Commit 09cffc0f by 泽锋 李

新增自动刷新周转建议的任务

parent c722a891
...@@ -37,6 +37,28 @@ namespace ShortagePush ...@@ -37,6 +37,28 @@ namespace ShortagePush
}); });
// 每10分钟刷一次周转数据
Task.Factory.StartNew(() => {
while (true)
{
try
{
System.Console.WriteLine($"开始刷新周转建议,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
new SkuAutoTurnServices().ResetPurchaseAdvise();
System.Console.WriteLine($"结束刷新周转建议,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Thread.Sleep(30 * 1000);
}
});
return Task.CompletedTask; return Task.CompletedTask;
} }
......
...@@ -45,6 +45,7 @@ namespace AutoGeneratePurchaseAdvise ...@@ -45,6 +45,7 @@ namespace AutoGeneratePurchaseAdvise
//PurchaseAdviseServices.Generate(DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"))); //PurchaseAdviseServices.Generate(DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00")));
//PurchaseAdviseServices.Generate(DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"))); //PurchaseAdviseServices.Generate(DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00")));
//PurchaseAdviseServices.AutoPushBuySys(4); //PurchaseAdviseServices.AutoPushBuySys(4);
new SkuAutoTurnServices().ResetPurchaseAdvise();
} }
catch (Exception ex) catch (Exception ex)
{ {
......
...@@ -10,6 +10,7 @@ using AutoTurnOver.DB.Base; ...@@ -10,6 +10,7 @@ using AutoTurnOver.DB.Base;
using AutoTurnOver.Models.stock; using AutoTurnOver.Models.stock;
using AutoTurnOver.Utility; using AutoTurnOver.Utility;
using AutoTurnOver.Models.Report; using AutoTurnOver.Models.Report;
using System.Threading;
namespace AutoTurnOver.DB namespace AutoTurnOver.DB
{ {
...@@ -1072,65 +1073,17 @@ from dc_auto_turnover where gmt_modified>=@btime and gmt_modified<=@etime ...@@ -1072,65 +1073,17 @@ from dc_auto_turnover where gmt_modified>=@btime and gmt_modified<=@etime
}); });
} }
public static void ResetPurchaseAdvise()
{
var task_name = "SResetPurchaseAdvise_v1";
var conn = _connection;
// 查询最后一次成功抓取的记录
var last_task_synchro_log = conn.QuerySingleOrDefault<task_synchro_log>(" select * from task_synchro_log where task_name=@task_name and status=1 order by end_time desc limit 1 ", new { task_name = task_name });
var new_task_synchro_log = new task_synchro_log
{
create_date = DateTime.Now,
end_time = DateTime.Now.AddMinutes(-20),
status = 0,
task_name = task_name
};
if (last_task_synchro_log != null) public static void WaitTurnoverQueueTask()
{ {
new_task_synchro_log.start_time = last_task_synchro_log.end_time.AddMinutes(-1); // 每秒检查一次
} Thread.Sleep(1 * 1000);
else var count = _connection.QueryFirstOrDefault<int>(" select count(1) from dc_base_queue where consume_date is null ", commandTimeout: 0);
if (count > 0)
{ {
new_task_synchro_log.start_time = DateTime.Now.AddMinutes(-60); WaitTurnoverQueueTask();
} }
new_task_synchro_log.id = conn.Insert(new_task_synchro_log) ?? 0;
int count = 0;
var page = 1;
var rows = 1000;
while (true)
{
var datas = _connection.Query<dc_base_purchase_details>($" select * from dc_base_purchase_details where update_time>=@btime and update_time<=@etime limit {(page-1)*rows},{(rows)} ",new {
btime = new_task_synchro_log.start_time,
etime = new_task_synchro_log.end_time
}).ToList();
if (datas == null || datas.Count <= 0)
{
break;
}
page++;
foreach (var item in datas)
{
//添加到队列,重新刷数据
ResetPurchaseAdviseSingle(item.bailun_sku,item.warehouse_code);
count++;
}
}
// 清理已经删除的数据
_connection.Execute(" update dc_auto_purchase_advise_detailed set status=-1 where bailun_sku_warehouse_code in @bailun_sku_warehouse_codes ",new { },commandTimeout:0);
new_task_synchro_log.count = count;
new_task_synchro_log.status = 1;
conn.Update(new_task_synchro_log);
} }
} }
......
...@@ -46,7 +46,7 @@ namespace AutoTurnOver.DB ...@@ -46,7 +46,7 @@ namespace AutoTurnOver.DB
{ {
Console.WriteLine("查询需要切换的数据"); Console.WriteLine("查询需要切换的数据");
// 查询需要切换的数据 // 查询需要切换的数据
List<replace_logistics_outofstock_dto> ouDatas = GetOutOfStock(bailun_sku, warehouse_code); List<replace_logistics_outofstock_dto> ouDatas = GetOutOfStock(date,bailun_sku, warehouse_code);
Console.WriteLine($" 查询需要切换的数据 :共:{ouDatas.Count} 条 "); Console.WriteLine($" 查询需要切换的数据 :共:{ouDatas.Count} 条 ");
int count = 1; int count = 1;
// 寻找有没有满足条件的物流方案 // 寻找有没有满足条件的物流方案
...@@ -147,7 +147,7 @@ namespace AutoTurnOver.DB ...@@ -147,7 +147,7 @@ namespace AutoTurnOver.DB
/// 获取供应链内有缺货的数据 /// 获取供应链内有缺货的数据
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static List<replace_logistics_outofstock_dto> GetOutOfStock(string bailun_sku = null, string warehouse_code = null) public static List<replace_logistics_outofstock_dto> GetOutOfStock(DateTime date, string bailun_sku = null, string warehouse_code = null)
{ {
var now = DateTime.Now; var now = DateTime.Now;
List<replace_logistics_outofstock_dto> datas = new List<replace_logistics_outofstock_dto>(); List<replace_logistics_outofstock_dto> datas = new List<replace_logistics_outofstock_dto>();
...@@ -169,8 +169,10 @@ from dc_auto_forecast_fluctuation as t1 ...@@ -169,8 +169,10 @@ from dc_auto_forecast_fluctuation as t1
left join dc_base_warehouse as t2 on t1.warehouse_code =t2.warehouse_code left join dc_base_warehouse as t2 on t1.warehouse_code =t2.warehouse_code
left join dc_auto_turnover as t3 on t1.warehouse_code =t3.warehouse_code and t1.bailun_sku = t3.bailun_sku left join dc_auto_turnover as t3 on t1.warehouse_code =t3.warehouse_code and t1.bailun_sku = t3.bailun_sku
where t1.type=2 and t2.hq_type in ('第三方仓库','FBA仓') and t1.start_time>=now() and t1.start_time<=DATE_ADD(now(),INTERVAL t3.turnover_days day) where t1.type=2 and t2.hq_type in ('第三方仓库','FBA仓') and t1.start_time>=now() and t1.start_time<=DATE_ADD(now(),INTERVAL t3.turnover_days day)
and t1.gmt_modified>=@btime
"; ";
DynamicParameters parameters = new DynamicParameters();
parameters.Add("btime",date);
if (!string.IsNullOrWhiteSpace(bailun_sku)) if (!string.IsNullOrWhiteSpace(bailun_sku))
{ {
sql += $" and t1.bailun_sku='{bailun_sku}' "; sql += $" and t1.bailun_sku='{bailun_sku}' ";
...@@ -180,7 +182,7 @@ where t1.type=2 and t2.hq_type in ('第三方仓库','FBA仓') and t1.start_time ...@@ -180,7 +182,7 @@ where t1.type=2 and t2.hq_type in ('第三方仓库','FBA仓') and t1.start_time
sql += $" and t1.warehouse_code='{warehouse_code}' "; sql += $" and t1.warehouse_code='{warehouse_code}' ";
} }
sql += " order by t1.start_time desc ;"; sql += " order by t1.start_time desc ;";
List<replace_logistics_outofstock_dto> ouDatas = _connection.Query<replace_logistics_outofstock_dto>(sql).ToList(); List<replace_logistics_outofstock_dto> ouDatas = _connection.Query<replace_logistics_outofstock_dto>(sql, parameters).ToList();
foreach (var item in ouDatas) foreach (var item in ouDatas)
{ {
// 如果采购都没到货就已经缺货了,调拨天数切换已经没有意义了 // 如果采购都没到货就已经缺货了,调拨天数切换已经没有意义了
......
...@@ -7,6 +7,9 @@ using System.IO; ...@@ -7,6 +7,9 @@ using System.IO;
using System.Data; using System.Data;
using AutoTurnOver.Utility; using AutoTurnOver.Utility;
using System.Threading.Tasks; using System.Threading.Tasks;
using AutoTurnOver.DB;
using Dapper;
using AutoTurnOver.Models.Report;
namespace AutoTurnOver.Services namespace AutoTurnOver.Services
{ {
...@@ -261,7 +264,7 @@ namespace AutoTurnOver.Services ...@@ -261,7 +264,7 @@ namespace AutoTurnOver.Services
#endregion #endregion
public dc_auto_turnover GetModel(string sku, string wCode) public AutoTurnOver.Models.dc_auto_turnover GetModel(string sku, string wCode)
{ {
return DB.dc_auto_turnover.GetModel(sku, wCode); return DB.dc_auto_turnover.GetModel(sku, wCode);
} }
...@@ -454,5 +457,87 @@ namespace AutoTurnOver.Services ...@@ -454,5 +457,87 @@ namespace AutoTurnOver.Services
{ {
return DB.dc_auto_turnover.GetHistoryDataNodes(sku, warehousecode); return DB.dc_auto_turnover.GetHistoryDataNodes(sku, warehousecode);
} }
public void ResetPurchaseAdvise()
{
var conn = MyMySqlConnection._connection;
// 如果遇到出周转建议的时候,则停止
var last_data = conn.QuerySingleOrDefault<DateTime>(" select MAX(create_time) from dc_auto_purchase_advise ");
if((DateTime.Now- last_data).TotalMinutes <= 20)
{
return;
}
var task_name = "ResetPurchaseAdvise_v1";
// 查询最后一次成功抓取的记录
var last_task_synchro_log = conn.QuerySingleOrDefault<task_synchro_log>(" select * from task_synchro_log where task_name=@task_name and status=1 order by end_time desc limit 1 ", new { task_name = task_name });
var new_task_synchro_log = new task_synchro_log
{
create_date = DateTime.Now,
end_time = DateTime.Now.AddMinutes(-20),
status = 0,
task_name = task_name
};
if (last_task_synchro_log != null)
{
new_task_synchro_log.start_time = last_task_synchro_log.end_time.AddMinutes(-1);
}
else
{
new_task_synchro_log.start_time = DateTime.Now.AddMinutes(-60);
}
new_task_synchro_log.id = conn.Insert(new_task_synchro_log) ?? 0;
int count = 0;
var bailun_sku_warehouse_codes = new List<string>();
var page = 1;
var rows = 1000;
var btime = DateTime.Now;
while (true)
{
var datas = conn.Query<dc_base_purchase_details>($" select * from dc_base_purchase_details where update_time>=@btime and update_time<=@etime limit {(page - 1) * rows},{(rows)} ", new
{
btime = new_task_synchro_log.start_time,
etime = new_task_synchro_log.end_time
}).ToList();
if (datas == null || datas.Count <= 0)
{
break;
}
page++;
foreach (var item in datas)
{
bailun_sku_warehouse_codes.Add($"{item.bailun_sku}{item.warehouse_code}");
//添加到队列,重新刷数据
AutoTurnOver.DB.dc_auto_turnover.ResetPurchaseAdviseSingle(item.bailun_sku, item.warehouse_code);
count++;
}
}
if(bailun_sku_warehouse_codes!=null && bailun_sku_warehouse_codes.Count >= 1)
{
// 等待周转跑完
AutoTurnOver.DB.dc_auto_turnover.WaitTurnoverQueueTask();
// 清理已经删除的数据
conn.Execute(" update dc_auto_purchase_advise_detailed set status=-1 where bailun_sku_warehouse_code in @bailun_sku_warehouse_codes ", new
{
bailun_sku_warehouse_codes = bailun_sku_warehouse_codes
}, commandTimeout: 0);
// 出周转建议
PurchaseAdviseServices.Generate(btime);
}
new_task_synchro_log.count = count;
new_task_synchro_log.status = 1;
conn.Update(new_task_synchro_log);
}
} }
} }
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