Commit 206701a3 by jianshuqin

增加同步物流账单时间服务

parent b47e011b
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.35" />
<PackageReference Include="Dapper.SimpleCRUD" Version="2.2.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.7" />
<PackageReference Include="MySql.Data" Version="8.0.21" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Bailun.DC.Common\Bailun.DC.Common.csproj" />
</ItemGroup>
</Project>
FROM microsoft/dotnet:2.1-runtime AS base
WORKDIR /app
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY Bailun.DC.SyncPickBillTime/Bailun.DC.SyncPickBillTime.csproj Bailun.DC.SyncPickBillTime/
COPY Bailun.DC.Common/Bailun.DC.Common.csproj Bailun.DC.Common/
RUN dotnet restore Bailun.DC.SyncPickBillTime/Bailun.DC.SyncPickBillTime.csproj
COPY . .
WORKDIR /src/Bailun.DC.SyncPickBillTime
RUN dotnet build Bailun.DC.SyncPickBillTime.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish Bailun.DC.SyncPickBillTime.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Bailun.DC.SyncPickBillTime.dll"]
namespace Bailun.DC.SyncPickBillTime.Models
{
public class RequestDto
{
/// <summary>
/// 页码
/// </summary>
public int PageIndex { get; set; }
/// <summary>
/// 分页大小
/// </summary>
public int PageCount { get; set; }
/// <summary>
/// 配货单号
/// </summary>
public string AllocatecargoOrderCode { get; set; }
/// <summary>
/// 差异状态 未对账 = 0, 存在差异 = 1 , 验证通过 = 2
/// </summary>
public int[] DiffStateTypes { get; set; }
}
}
using System;
using System.Collections.Generic;
namespace Bailun.DC.SyncPickBillTime.Models
{
public class ResponseDto
{
public bool success { get; set; }
public bool unAuthorizedRequest { get; set; }
public string error { get; set; }
public ResultDto result { get; set; }
}
public class ResultDto
{
public IList<PickBillDto> result { get; set; }
public int pageIndex { get; set; }
public int totalPages { get; set; }
public int total { get; set; }
public bool hasPreViousPage { get; set; }
public bool hasNextPage { get; set; }
}
public class PickBillDto
{
public int id { get; set; }
/// <summary>
/// 配货单号
/// </summary>
public string allocatecargoOrderCode { get; set; }
/// <summary>
/// 跟踪号
/// </summary>
public string trackingNo { get; set; }
/// <summary>
/// 物流商计费重量
/// </summary>
public decimal? merchantOutputWeightKg { get; set; }
/// <summary>
/// 物流商金额
/// </summary>
public decimal? merchantShipmentCostCNY { get; set; }
/// <summary>
/// 物流商币种
/// </summary>
public string currencyType { get; set; }
/// <summary>
/// 物流商账单日期
/// </summary>
public DateTime? merchantReconciliationTime { get; set; }
}
}
using System;
namespace Bailun.DC.SyncPickBillTime.Models
{
public class dc_base_oms_pick_variances
{
public int id { get; set; }
public string bailun_order_id { get; set; }
public string pick_order_id { get; set; }
public decimal old_weight { get; set; }
public decimal old_cost { get; set; }
public decimal new_weight { get; set; }
public decimal new_cost { get; set; }
public decimal bill_weight { get; set; }
public decimal bill_cost { get; set; }
public DateTime bill_time { get; set; }
public decimal weight_difference { get; set; }
public decimal cost_difference { get; set; }
public DateTime gmt_create { get; set; }
public DateTime gmt_modified { get; set; }
}
}
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading.Tasks;
namespace Bailun.DC.SyncPickBillTime
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("启动新数据中心同步任务 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
var builder = new HostBuilder().ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Services>();
});
await builder.RunConsoleAsync();
}
}
}
using Bailun.DC.SyncPickBillTime.Models;
using Dapper;
using Microsoft.Extensions.Hosting;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Bailun.DC.SyncPickBillTime
{
public class Services : BackgroundService
{
#if DEBUG
string url = "http://wms.bailuntec.com/api/services/app/LogisticsReconciliationService/GetBaseDataList";
#else
string url = "http://wms-api.owms:5001/api/services/app/LogisticsReconciliationService/GetBaseDataList";
#endif
private Timer _timer;
public static bool IsWork = false;
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
//每5分钟执行一次任务
_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(5));
return Task.CompletedTask;
}
private void DoWork(object state)
{
if (!IsWork)
{
Console.WriteLine($"开始启动:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
IsWork = true;
try
{
Init();
}
catch (Exception ex)
{
Console.WriteLine(ex?.InnerException?.Message ?? ex.Message);
}
Console.WriteLine($"任务运行完成:{ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
IsWork = false;
}
}
public Services()
{
Dapper.SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
}
public void Init()
{
using (var db = new MySqlConnection(Common.GlobalConfig.ConnectionString))
{
if (db.State == System.Data.ConnectionState.Closed)
{
db.Open();
}
string sql = $"select * from dc_base_oms_pick_variances where bill_time <= @bill_time and bill_cost <> @bill_cost";
IList<dc_base_oms_pick_variances> list = db.Query<dc_base_oms_pick_variances>(sql, new { bill_time = DateTime.Parse("1991-01-01 00:00:00"), bill_cost = 0 }).ToList();
if (list?.Count > 0)
{
Console.WriteLine($"需要同步{list.Count}条数据");
RequestDto request = new RequestDto
{
PageCount = 1000,
PageIndex = 1,
AllocatecargoOrderCode = string.Join(",", list.Select(l => l.pick_order_id))
};
ResponseDto response = default(ResponseDto);
while (request.PageIndex == 1 || (response != null && response.result != null && (response.result.totalPages > response.result.pageIndex)))
{
string data = JsonConvert.SerializeObject(request);
var result = Common.HttpHelper.NetHelper.HttpPostJson(url, data);
request.PageIndex++;
if (!string.IsNullOrEmpty(result))
{
response = JsonConvert.DeserializeObject<ResponseDto>(result);
if (request.PageIndex == 1)
{
Console.WriteLine($"总共同步{response?.result?.total ?? 0}条数据");
}
if (response != null && response.success && response.result?.result?.Count > 0)
{
//保存数据
Save(response.result.result);
Console.WriteLine($"已同步{((response.result.pageIndex - 1) * request.PageCount) + response.result.result.Count}条数据");
}
}
}
}
}
}
private void Save(IList<PickBillDto> list)
{
if (list?.Count > 0)
{
dc_base_oms_pick_variances[] listEngity = list.Select(l => new dc_base_oms_pick_variances() { pick_order_id = l.allocatecargoOrderCode, bill_time = l.merchantReconciliationTime ?? DateTime.Parse("1991-01-01 00:00:00"), gmt_modified = DateTime.Now }).ToArray();
using (var db = new MySqlConnection(Common.GlobalConfig.ConnectionString))
{
if (db.State == System.Data.ConnectionState.Closed)
{
db.Open();
}
int count = db.Execute("update dc_base_oms_pick_variances set bill_time = @bill_time, gmt_modified = @gmt_modified where pick_order_id=@pick_order_id", listEngity);
}
}
}
}
}
......@@ -99,6 +99,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bailun.DC.SyncShopifyFee",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bailun.DC.SyncAmazonAdFee", "Bailun.DC.SyncAmazonAdFee\Bailun.DC.SyncAmazonAdFee.csproj", "{6FD93D1A-ABF6-4DE1-96A6-5D48BB756A04}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bailun.DC.SyncPickBillTime", "Bailun.DC.SyncPickBillTime\Bailun.DC.SyncPickBillTime.csproj", "{98D33EE3-BDB9-436F-BC26-0D9DCBDE0D90}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -293,6 +295,10 @@ Global
{6FD93D1A-ABF6-4DE1-96A6-5D48BB756A04}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6FD93D1A-ABF6-4DE1-96A6-5D48BB756A04}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6FD93D1A-ABF6-4DE1-96A6-5D48BB756A04}.Release|Any CPU.Build.0 = Release|Any CPU
{98D33EE3-BDB9-436F-BC26-0D9DCBDE0D90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{98D33EE3-BDB9-436F-BC26-0D9DCBDE0D90}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98D33EE3-BDB9-436F-BC26-0D9DCBDE0D90}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98D33EE3-BDB9-436F-BC26-0D9DCBDE0D90}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -338,6 +344,7 @@ Global
{66D159A3-63A6-4900-AB21-C04ADE5A3D01} = {AE2CE86A-8538-4142-920F-684DCF47C064}
{9A19AF60-5891-4C35-A021-2DE58415373D} = {AE2CE86A-8538-4142-920F-684DCF47C064}
{6FD93D1A-ABF6-4DE1-96A6-5D48BB756A04} = {AE2CE86A-8538-4142-920F-684DCF47C064}
{98D33EE3-BDB9-436F-BC26-0D9DCBDE0D90} = {AE2CE86A-8538-4142-920F-684DCF47C064}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6E53AF28-A282-4FB0-A769-EAEA9769C02A}
......
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