Commit f12427d9 by DESKTOP-732ATD8\BLT

增加功能:Shopify 广告费同步任务

parent c4c37a35
<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.SyncShopifyFee/Bailun.DC.SyncShopifyFee.csproj Bailun.DC.SyncShopifyFee/
COPY Bailun.DC.Common/Bailun.DC.Common.csproj Bailun.DC.Common/
RUN dotnet restore Bailun.DC.SyncShopifyFee/Bailun.DC.SyncShopifyFee.csproj
COPY . .
WORKDIR /src/Bailun.DC.SyncShopifyFee
RUN dotnet build Bailun.DC.SyncShopifyFee.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish Bailun.DC.SyncShopifyFee.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Bailun.DC.SyncShopifyFee.dll"]
using System;
namespace Bailun.DC.SyncShopifyFee.Models
{
public class dc_base_finance_shopify_fee : response_shopify_fee
{
public int id { get; set; }
public DateTime gmt_create { get; set; }
public DateTime gmt_modified { get; set; }
public int status { get; set; }
}
}
using System.Collections.Generic;
namespace Bailun.DC.SyncShopifyFee.Models
{
public class response_result
{
public bool success { get; set; }
public bool has_damin { get; set; }
public string message { get; set; }
public IList<response_shopify_fee> data { get; set; }
}
}
using Newtonsoft.Json;
using System;
namespace Bailun.DC.SyncShopifyFee.Models
{
public class response_shopify_fee
{
public string data_id { get; set; }
public string ad_id { get; set; }
[JsonProperty("_ts")]
public DateTime ts { get; set; }
public DateTime? bdate { get; set; }
public DateTime? edate { get; set; }
public decimal? fee { get; set; }
public string account_currency { get; set; }
public decimal? cny_exchange_rate { get; set; }
}
}
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading.Tasks;
namespace Bailun.DC.SyncShopifyFee
{
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();
}
//测试
//static void Main(string[] args)
//{
// Console.WriteLine("开始启动 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
// DateTime? btime = new DateTime(2021, 8, 1);
// DateTime? etime = DateTime.Today;
// try
// {
// var _service = new Services();
// _service.Init(btime, etime);
// }
// catch (Exception ex)
// {
// Console.WriteLine(ex.Message);
// }
// Console.WriteLine("任务运行完成 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
//}
}
}
using Bailun.DC.SyncShopifyFee.Models;
using Dapper;
using Microsoft.Extensions.Hosting;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Bailun.DC.SyncShopifyFee
{
public class Services : BackgroundService
{
string url = "http://scrm.bailuntec.com/ScrmApi/api/ApiGetAdFeeLog"; //?btime=2021-10-01&etime=2021-10-03&page=1&rows=100
private Timer _timer;
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
//每60分钟执行一次任务
_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(60));
return Task.CompletedTask;
}
private void DoWork(object state)
{
try
{
Console.WriteLine("开始启动 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
Init();
Console.WriteLine("任务运行完成 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public Services()
{
Dapper.SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
}
public void Init(DateTime? btime = null, DateTime? etime = null)
{
var page = 1;
var pagesize = 1000;
var count = 0;
if (!btime.HasValue)
{
btime = DateTime.Now.AddHours(-2);
}
if (!etime.HasValue)
{
etime = DateTime.Today.AddDays(1);
}
while (page == 1 || (page > 1 && count > 0))
{
try
{
var result = Common.HttpHelper.NetHelper.Request($"{url}?btime={btime.Value.ToString("yyyy-MM-dd HH:00:00")}&etime={etime.Value.ToString("yyyy-MM-dd")}&page={page}&rows={pagesize}");
if (!string.IsNullOrEmpty(result))
{
var json = JsonConvert.DeserializeObject<response_result>(result);
count = json.data.Count;
if (json.success && count > 0)
{
//保存数据
Save(json.data);
page++;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Thread.Sleep(60 * 1000);
}
}
}
public void Save(IList<response_shopify_fee> list)
{
using (var db = new MySqlConnection(Common.GlobalConfig.ConnectionString))
{
if (db.State == System.Data.ConnectionState.Closed)
{
db.Open();
}
foreach (var item in list)
{
var m = JsonConvert.DeserializeObject<dc_base_finance_shopify_fee>(JsonConvert.SerializeObject(item));
m.gmt_modified = DateTime.Now;
int count = db.Execute("update dc_base_finance_shopify_fee set ad_id = @ad_id, ts = @ts, bdate = @bdate, edate = @edate, fee = @fee, account_currency = @account_currency, cny_exchange_rate = @cny_exchange_rate,gmt_modified = @gmt_modified where data_id=@data_id", m);
if (count == 0)
{
m.gmt_create = DateTime.Now;
db.Insert(m);
}
}
}
}
}
}
......@@ -95,6 +95,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bailun.DC.ExportTask", "Bai
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bailun.DC.SyncMonthStockWeighting", "Bailun.DC.SyncMonthStockWeighting\Bailun.DC.SyncMonthStockWeighting.csproj", "{66D159A3-63A6-4900-AB21-C04ADE5A3D01}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bailun.DC.SyncShopifyFee", "Bailun.DC.SyncShopifyFee\Bailun.DC.SyncShopifyFee.csproj", "{9A19AF60-5891-4C35-A021-2DE58415373D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -281,6 +283,10 @@ Global
{66D159A3-63A6-4900-AB21-C04ADE5A3D01}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66D159A3-63A6-4900-AB21-C04ADE5A3D01}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66D159A3-63A6-4900-AB21-C04ADE5A3D01}.Release|Any CPU.Build.0 = Release|Any CPU
{9A19AF60-5891-4C35-A021-2DE58415373D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9A19AF60-5891-4C35-A021-2DE58415373D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9A19AF60-5891-4C35-A021-2DE58415373D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9A19AF60-5891-4C35-A021-2DE58415373D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -324,6 +330,7 @@ Global
{DEAD70F9-01C4-4178-AB0B-1C4874AA7A61} = {AE2CE86A-8538-4142-920F-684DCF47C064}
{C4C4990D-14DC-4B76-A7EB-6F3FC5BE8B94} = {AE2CE86A-8538-4142-920F-684DCF47C064}
{66D159A3-63A6-4900-AB21-C04ADE5A3D01} = {AE2CE86A-8538-4142-920F-684DCF47C064}
{9A19AF60-5891-4C35-A021-2DE58415373D} = {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