using DTO;
|
using IServices;
|
using Microsoft.EntityFrameworkCore;
|
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Logging;
|
using Newtonsoft.Json.Linq;
|
using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Linq;
|
using System.Net.Http;
|
using System.Net.Http.Headers;
|
using System.Threading;
|
using System.Threading.Tasks;
|
using System.Transactions;
|
using zhengcaioa.IService;
|
using zhengcaioa.Models;
|
|
namespace zhengcaioa.Timer
|
{
|
public class TimerGetShopUsersService : BackgroundService
|
{
|
private readonly ILogger _logger;
|
private readonly IConfiguration _configuration;
|
private readonly ILiaotianService _liaotianService;
|
private readonly IHttpClientFactory _clientFactory;
|
private readonly IDtChannelArticleNewsService _dtChannelArticleNewsService;
|
|
public TimerGetShopUsersService(ILogger<TimerGetShopUsersService> logger, IServiceScopeFactory factory)
|
{
|
_logger = logger;
|
_configuration = factory.CreateScope().ServiceProvider.GetRequiredService<IConfiguration>();
|
_liaotianService = factory.CreateScope().ServiceProvider.GetRequiredService<ILiaotianService>();
|
_clientFactory = factory.CreateScope().ServiceProvider.GetRequiredService<IHttpClientFactory>();
|
_dtChannelArticleNewsService = factory.CreateScope().ServiceProvider.GetRequiredService<IDtChannelArticleNewsService>();
|
}
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
{
|
_logger.LogInformation("TimerGetShopUsersService is starting.");
|
|
//stoppingToken.Register(() => File.Create($"E:\\dotnetCore\\Practice\\Practice\\{DateTime.Now.Millisecond}.txt"));
|
|
DateTime datenow = DateTime.Now;
|
while (!stoppingToken.IsCancellationRequested)
|
{
|
_logger.LogInformation("TimerGetShopUsersService 开始执行");
|
_logger.LogInformation(datenow.ToString("yyyy-MM-dd HH:mm:ss"));
|
//每个小时计算一次
|
if (datenow.Hour <= DateTime.Now.Hour)
|
//if (true)
|
{
|
|
try
|
{
|
|
await this.huoquhuiyuanAsync();
|
|
}
|
catch (Exception ex)
|
{
|
_logger.LogInformation(ex.ToString());
|
}
|
|
datenow = DateTime.Now.AddHours(1);
|
}
|
|
|
await Task.Delay(TimeSpan.FromMinutes(10), stoppingToken);
|
|
_logger.LogInformation("TimerGetShopUsersService 继续执行");
|
}
|
|
_logger.LogInformation("TimerGetShopUsersService background task is stopping.");
|
}
|
|
public override void Dispose()
|
{
|
base.Dispose();
|
}
|
public void ceshi()
|
{
|
DtChannelArticleNewsDTOSearch searchEntity = new DtChannelArticleNewsDTOSearch();
|
searchEntity.EndTime = DateTime.Parse("2021-07-15 12:06:30.893");
|
searchEntity.AddTime = DateTime.Parse("2021-07-01 12:06:30.893");
|
searchEntity.UserName = "yangjiameng";
|
searchEntity.CategoryId = 85;
|
var dtChannelArticleNewsDTOs = _dtChannelArticleNewsService.GetList(searchEntity);
|
|
_logger.LogInformation("dtChannelArticleNewsDTOs:"+ dtChannelArticleNewsDTOs.Count);
|
}
|
|
public async Task huoquhuiyuanAsync()
|
{
|
string Shuchengurl = _configuration.GetSection("Shuchengurl").Value;
|
string GetShopUsers = _configuration.GetSection("GetShopUsers").Value;
|
|
|
string huiyuanurl = _configuration.GetSection("huiyuanurl").Value;
|
string GetDateFirstOrderUser = _configuration.GetSection("GetDateFirstOrderUser").Value;
|
string GetUserinfoAndRule = _configuration.GetSection("GetUserinfoAndRule").Value;
|
|
ResultEntity resultEntity = new ResultEntity();
|
resultEntity.Result = false;
|
|
|
//书城
|
string result = string.Empty;
|
Uri getUrl = new Uri(Shuchengurl+ GetShopUsers+ "?start=2021-07-1&end=2021-07-12");
|
|
var httpClient = _clientFactory.CreateClient();
|
httpClient.Timeout = new TimeSpan(0, 0, 60);
|
var Result = await httpClient.GetAsync(getUrl);
|
result = Result.Content.ReadAsStringAsync().Result;
|
|
|
_logger.LogInformation("result:" + result);
|
|
//会员
|
|
string result1 = string.Empty;
|
Uri postUrl = new Uri(huiyuanurl + GetDateFirstOrderUser);
|
|
JObject questions = new JObject();
|
questions.Add("start", "2021-07-12 12:36:30");
|
questions.Add("end", "2021-07-14 12:36:30");
|
|
string requestJson = questions.ToString();
|
|
using (HttpContent httpContent = new StringContent(requestJson))
|
{
|
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
|
var httpClient1 = _clientFactory.CreateClient();
|
httpClient1.Timeout = new TimeSpan(0, 0, 60);
|
var Result1 = await httpClient1.PostAsync(postUrl, httpContent);
|
result1 = Result1.Content.ReadAsStringAsync().Result;
|
}
|
|
_logger.LogInformation("result1:" + result1);
|
|
|
|
|
string result2 = string.Empty;
|
Uri postUrl2 = new Uri(huiyuanurl + GetUserinfoAndRule);
|
|
JObject questions2 = new JObject();
|
questions2.Add("UserID", "248768ac-d083-462a-5bb4-08d940e7bc81");
|
|
string requestJson2 = questions2.ToString();
|
|
using (HttpContent httpContent = new StringContent(requestJson2))
|
{
|
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
|
var httpClient2 = _clientFactory.CreateClient();
|
httpClient2.Timeout = new TimeSpan(0, 0, 60);
|
var Result2 = await httpClient2.PostAsync(postUrl2, httpContent);
|
result2 = Result2.Content.ReadAsStringAsync().Result;
|
}
|
|
_logger.LogInformation("result2:" + result2);
|
|
|
|
using (TransactionScope scope = new TransactionScope())
|
{
|
//获取最后更新时间
|
DateTime start = DateTime.Now.AddYears(-10);
|
DateTime end = DateTime.Now;
|
|
var ssss = _liaotianService.GetSysUpdatetime("getCustomers");
|
|
if (ssss != null)
|
{
|
start = ssss.Updatetime;
|
}
|
_logger.LogInformation("start:" + start);
|
_logger.LogInformation("end:" + end);
|
|
|
//获取客户并且保存到合作客户
|
|
|
|
|
//保存最后更新时间
|
if (ssss != null)
|
{
|
ssss.Updatetime = end;
|
}
|
else
|
{
|
ssss = new SysUpdatetime();
|
ssss.Updatetime = end;
|
ssss.Updatename = "getCustomers";
|
}
|
resultEntity = _liaotianService.saveSysUpdatetime(ssss);
|
if (!resultEntity.Result)
|
{
|
throw new Exception("保存更新时间出错!");
|
}
|
|
scope.Complete();
|
}
|
|
|
|
|
|
|
|
}
|
}
|
}
|