Commit 0624bf84 by lizefeng

新增库存差异计算服务

parent 6f8ceb22
using AutoTurnOver.Models;
using AutoTurnOver.Utility;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace AutoTurnOver.DB
{
public class dc_ana_deviation_dao : connectionHelper
{
/// <summary>
/// 分析差异数据
/// </summary>
/// <param name="taskDto"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public static Task<int> AnaDeviation(t_task_queue taskDto)
{
throw new NotImplementedException();
}
/// <summary>
/// 添加分析任务
/// </summary>
public static void PushAnaTask()
{
// 查询所有需要分析的sku
var skus = _connection.Query<int>(" select id from dc_ana_deviation_sku ").ToList();
foreach (var item in skus)
{
RabbitMQHelper.EnqueneMsg("aims:deviation:input",new t_task_queue {id = item.ToString(),create_time= DateTime.Now });
}
}
}
}
...@@ -14,6 +14,6 @@ namespace AutoTurnOver.DB ...@@ -14,6 +14,6 @@ namespace AutoTurnOver.DB
public string platform { get; set; } public string platform { get; set; }
public string site { get; set; } public string site { get; set; }
public string account { get; set; } public string account { get; set; }
public string product { get; set; } public string project { get; set; }
} }
} }
using System;
using System.Collections.Generic;
using System.Text;
namespace AutoTurnOver.Models
{
public class t_task_queue
{
public string id { get; set; }
public DateTime create_time { get; set; }
}
}
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Qiniu.SDK" Version="8.0.0" /> <PackageReference Include="Qiniu.SDK" Version="8.0.0" />
<PackageReference Include="StackExchange.Redis" Version="2.1.58" /> <PackageReference Include="StackExchange.Redis" Version="2.1.58" />
<PackageReference Include="RabbitMQ.Client" Version="6.4.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.1.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>
\ No newline at end of file
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using RabbitMQ.Client;
namespace AutoTurnOver.Utility
{
public class RabbitWorkerBase<T> : BackgroundService where T : class
{
private List<IModel> _channelList = new List<IModel>();
private string _taskName;
private string _queueName;
private int _exampleNumber;
private Timer _timer;
private bool _isZip;
public RabbitWorkerBase(string taskName, string queueName, int exampleNumber, bool isZip = false)
{
_taskName = taskName;
_queueName = queueName;
_exampleNumber = exampleNumber;
_isZip = isZip;
}
private void TimerDoWork(object state)
{
int changeExampleNumber = 2;
//for (int i = 0; i < _channelList.Count; i++)
//{
// var channel = _channelList[0];
// channel.Close();
// channel.Dispose();
// _channelList.Remove(channel);
//}
while (_channelList.Count >= 1)
{
var channel = _channelList[0];
channel.Close();
channel.Dispose();
_channelList.Remove(channel);
}
for (int i = 0; i < changeExampleNumber; i++)
{
var channel = RabbitMQHelper.GetChannel();
RabbitMQHelper.Receive<T>("", _queueName, channel, (taskDto) => DoWorkAsync(taskDto).GetAwaiter().GetResult(), _isZip);
_channelList.Add(channel);
}
}
protected virtual Task DoWork(T taskDto)
{
return Task.CompletedTask;
}
private async Task DoWorkAsync(T taskDto)
{
try
{
await DoWork(taskDto);
}
catch (Exception ex)
{
Console.WriteLine($"异常:{ex.Message},详细{ex.StackTrace}");
}
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
Console.WriteLine($"{_taskName}已启动");
for (int i = 0; i < _exampleNumber; i++)
{
var channel = RabbitMQHelper.GetChannel();
RabbitMQHelper.Receive<T>("", _queueName, channel, (taskDto) => DoWorkAsync(taskDto).GetAwaiter().GetResult(), _isZip);
_channelList.Add(channel);
}
_timer = new Timer(TimerDoWork, null, TimeSpan.FromMinutes(120), TimeSpan.FromMinutes(120));
return Task.CompletedTask;
}
public override void Dispose()
{
base.Dispose();
_timer?.Dispose();
}
}
}
using AutoTurnOver.DB;
using AutoTurnOver.Models;
using AutoTurnOver.Utility;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace ResetOutofstock
{
class DeviationRabbitBackgroundService : RabbitWorkerBase<t_task_queue>
{
public DeviationRabbitBackgroundService() : base("差异分析服务", "aims:deviation:input", 10)
{
}
protected override async Task DoWork(t_task_queue taskDto)
{
try
{
int hit = await dc_ana_deviation_dao.AnaDeviation(taskDto);
Console.WriteLine($"差异分析服务 完成 {hit} ");
}
catch (Exception e)
{
RabbitMQHelper.EnqueneMsg("aims:deviation:input", taskDto);
Console.WriteLine($"差异分析服务 数据异常,异常原因为:{e.Message},异常堆栈为:{e.StackTrace}");
}
}
}
}
...@@ -50,6 +50,7 @@ namespace ResetOutofstock ...@@ -50,6 +50,7 @@ namespace ResetOutofstock
//report_invest_return_dao.ShareAdFee(); //report_invest_return_dao.ShareAdFee();
//report_invest_return_dao.SynchBtmOrderRefund(); //report_invest_return_dao.SynchBtmOrderRefund();
//report_invest_return_dao.CalculationStockScore("962073701"); //report_invest_return_dao.CalculationStockScore("962073701");
//dc_ana_deviation_dao.PushAnaTask();
} }
catch (Exception ex) catch (Exception ex)
{ {
...@@ -64,6 +65,7 @@ namespace ResetOutofstock ...@@ -64,6 +65,7 @@ namespace ResetOutofstock
services.AddHostedService<CaseFlowBackgrounService>(); services.AddHostedService<CaseFlowBackgrounService>();
services.AddHostedService<StockBackgrounService>(); services.AddHostedService<StockBackgrounService>();
services.AddHostedService<ReportFinanceBackgrounService>(); services.AddHostedService<ReportFinanceBackgrounService>();
services.AddHostedService<DeviationRabbitBackgroundService>();
}); });
await builder.RunConsoleAsync(); await builder.RunConsoleAsync();
......
...@@ -19,7 +19,7 @@ namespace ResetOutofstock ...@@ -19,7 +19,7 @@ namespace ResetOutofstock
return Task.CompletedTask; return Task.CompletedTask;
} }
private void DoWork(object state) private async void DoWork(object state)
{ {
var now = DateTime.Now; var now = DateTime.Now;
...@@ -71,6 +71,18 @@ namespace ResetOutofstock ...@@ -71,6 +71,18 @@ namespace ResetOutofstock
//dc_report_finance_dao.CalculationAccounts(DateTime.Now.AddDays(-90)); //dc_report_finance_dao.CalculationAccounts(DateTime.Now.AddDays(-90));
Console.WriteLine($"结束 财报应首付账款,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); Console.WriteLine($"结束 财报应首付账款,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
} }
if (now.Hour == 12 && now.Minute == 30)
{
Console.WriteLine($"开始 计算库存差异 ,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
dc_ana_deviation_dao.PushAnaTask();
Console.WriteLine($"结束 计算库存差异,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
}
if (now.Hour == 23 && now.Minute == 30)
{
Console.WriteLine($"开始 计算库存差异 ,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
dc_ana_deviation_dao.PushAnaTask();
Console.WriteLine($"结束 计算库存差异,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
}
if (now.Hour == 23 && now.Minute == 50) if (now.Hour == 23 && now.Minute == 50)
{ {
Console.WriteLine($"开始 备份投资回报分析表 ,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); Console.WriteLine($"开始 备份投资回报分析表 ,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
......
...@@ -53,5 +53,11 @@ ...@@ -53,5 +53,11 @@
"btm-sys": { "btm-sys": {
"ad-fee": "http://btm.bailuntec.com/ScrmApi/api/ApiGetAdFeeDayFull", "ad-fee": "http://btm.bailuntec.com/ScrmApi/api/ApiGetAdFeeDayFull",
"order-refund": "http://btm.bailuntec.com/ScrmApi/api/ApiGetOrderRefundLog" "order-refund": "http://btm.bailuntec.com/ScrmApi/api/ApiGetOrderRefundLog"
},
"RabbitMQ": {
"Host": "rabbitmq.bailuntec.com",
"Port": "6783",
"UserName": "bailun",
"Password": "bailun2022"
} }
} }
...@@ -54,5 +54,11 @@ ...@@ -54,5 +54,11 @@
"btm-sys": { "btm-sys": {
"ad-fee": "http://btm.bailuntec.com/ScrmApi/api/ApiGetAdFeeDayFull", "ad-fee": "http://btm.bailuntec.com/ScrmApi/api/ApiGetAdFeeDayFull",
"order-refund": "http://btm.bailuntec.com/ScrmApi/api/ApiGetOrderRefundLog" "order-refund": "http://btm.bailuntec.com/ScrmApi/api/ApiGetOrderRefundLog"
},
"RabbitMQ": {
"Host": "10.0.6.36",
"UserName": "bailun",
"Password": "bailun2022",
"Port": "6783"
} }
} }
...@@ -53,6 +53,12 @@ ...@@ -53,6 +53,12 @@
"btm-sys": { "btm-sys": {
"ad-fee": "http://btm.bailuntec.com/ScrmApi/api/ApiGetAdFeeDayFull", "ad-fee": "http://btm.bailuntec.com/ScrmApi/api/ApiGetAdFeeDayFull",
"order-refund": "http://btm.bailuntec.com/ScrmApi/api/ApiGetOrderRefundLog" "order-refund": "http://btm.bailuntec.com/ScrmApi/api/ApiGetOrderRefundLog"
},
"RabbitMQ": {
"Host": "10.0.6.36",
"UserName": "bailun",
"Password": "bailun2022",
"Port": "6783"
} }
} }
\ 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