add 后台服务 和 部署sql 等
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
using Ewide.Nbzs.Entity;
|
||||
using Furion;
|
||||
using Furion.TaskScheduler;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -12,49 +15,191 @@ namespace Ewide.Nbzs.BackWorkerService
|
||||
{
|
||||
public class Worker : BackgroundService
|
||||
{
|
||||
public SqlSugarClient db;
|
||||
private readonly ILogger<Worker> _logger;
|
||||
|
||||
readonly SqlSugarProvider db_Product_Conn;
|
||||
readonly SqlSugarProvider db_Zlb_Conn;
|
||||
public Worker(ILogger<Worker> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
List<ConnectionConfig> connectConfigList = new()
|
||||
{
|
||||
new ConnectionConfig
|
||||
{
|
||||
ConnectionString = App.Configuration["SqlServerConnectionString:DefaultConnection"],
|
||||
DbType = DbType.SqlServer,
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
ConfigId = "0"
|
||||
}
|
||||
};
|
||||
db = new SqlSugarClient(connectConfigList);
|
||||
db_Product_Conn = DbManage.GetConnection("Product_Conn");
|
||||
db_Zlb_Conn = DbManage.GetConnection("Zlb_Conn");
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
ExecProcess();
|
||||
//var asd = db.Queryable<Ewide.NbzsZheliban.Entity.zjzwfwTickets>().ToList();
|
||||
var asd = db.Queryable<Ewide.NbzsZheliban.Entity.InvestigateTable>().ToList();
|
||||
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||
await Task.Delay(1000, stoppingToken);
|
||||
ExecPushProcess();
|
||||
_logger.LogInformation("已完成一次任务,Finish a Worker running at: {time}", DateTimeOffset.Now);
|
||||
await Task.Delay(1 * 60 * 1000, stoppingToken);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Ö´ÐÐÈÎÎñ ÿÃë
|
||||
/// </summary>
|
||||
private void ExecProcess()
|
||||
{
|
||||
Thread.Sleep(3000);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行推送任务
|
||||
/// </summary>
|
||||
private void ExecPushProcess()
|
||||
{
|
||||
//取出推送任务表数据
|
||||
var listPush = db_Product_Conn.Queryable<PushZlb>().Where(p => p.IsDeleted == 0).ToList();
|
||||
listPush.ForEach(p =>
|
||||
{
|
||||
if (p.IsAutoDo == 1)
|
||||
{
|
||||
//_logger.LogInformation("自动创建任务:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
////自动推
|
||||
//#region 自动
|
||||
//SpareTime.Do(() =>
|
||||
//{
|
||||
// if (p.IsError == 1)
|
||||
// {
|
||||
// _logger.LogInformation("自动任务停止:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
// return null;
|
||||
// }
|
||||
// else
|
||||
// return SpareTime.GetCronNextOccurrence(p.AutoDoTime);
|
||||
//}, (time, count) => { Push(p, count); }
|
||||
//);
|
||||
//#endregion
|
||||
|
||||
if (p.IsError == 1)
|
||||
{
|
||||
//_logger.LogInformation("自动任务停止:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
}
|
||||
else
|
||||
{
|
||||
Push(p);
|
||||
}
|
||||
}
|
||||
else if (p.IsAutoDo == 0 && p.IsProcessed == 0)
|
||||
{
|
||||
//_logger.LogInformation("手动创建任务:" + DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
//手动
|
||||
//SpareTime.DoOnce(1, (timer, count) => { Push(p); });
|
||||
Push(p);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
private void Push2(PushZlb pushZlb)
|
||||
{
|
||||
_logger.LogWarning("测试Push2");
|
||||
}
|
||||
|
||||
private List<PushResult> Push(PushZlb pushZlbObj)
|
||||
{
|
||||
_logger.LogInformation("进入Push方法():Id:{Id} ,项目ID:{PrjId} at: {time}", pushZlbObj.ID, pushZlbObj.PrjId, DateTimeOffset.Now);
|
||||
List<PushResult> list_result = new();
|
||||
try
|
||||
{
|
||||
var projectId = Guid.Parse(pushZlbObj.PrjId);
|
||||
var project = db_Product_Conn.Queryable<Projects>().Where(p => p.ID == projectId).First();
|
||||
if (project != null)
|
||||
{
|
||||
//删政策
|
||||
var delete_result = db_Zlb_Conn.Deleteable<PoliciesRegulations>().ExecuteCommandHasChange();
|
||||
list_result.Add(new PushResult { Action = "Delete", TableName = "PoliciesRegulations", IsSuccess = delete_result });
|
||||
//if (delete_result)
|
||||
//{
|
||||
var insert_result = db_Zlb_Conn.Insertable(db_Product_Conn.Queryable<PoliciesRegulations>().ToList()).ExecuteCommand();
|
||||
list_result.Add(new PushResult { Action = "Insert", TableName = "PoliciesRegulations", IsSuccess = insert_result > 0 });
|
||||
//}
|
||||
//删除非住宅协议
|
||||
delete_result = db_Zlb_Conn.Deleteable<NonResidentialAgreement>().Where(p => p.ProjectID == projectId).ExecuteCommandHasChange();
|
||||
list_result.Add(new PushResult { Action = "Delete", TableName = "NonResidentialAgreement", IsSuccess = delete_result });
|
||||
//if (delete_result)
|
||||
//{
|
||||
insert_result = db_Zlb_Conn.Insertable(db_Product_Conn.Queryable<NonResidentialAgreement>().Where(p => p.ProjectID == projectId).ToList()).ExecuteCommand();
|
||||
list_result.Add(new PushResult { Action = "Insert", TableName = "NonResidentialAgreement", IsSuccess = insert_result > 0 });
|
||||
//}
|
||||
//删除住宅协议
|
||||
delete_result = db_Zlb_Conn.Deleteable<ResidentialAgreement>().Where(p => p.ProjectID == projectId).ExecuteCommandHasChange();
|
||||
list_result.Add(new PushResult { Action = "Delete", TableName = "ResidentialAgreement", IsSuccess = delete_result });
|
||||
//if (delete_result)
|
||||
//{
|
||||
insert_result = db_Zlb_Conn.Insertable(db_Product_Conn.Queryable<ResidentialAgreement>().Where(p => p.ProjectID == projectId).ToList()).ExecuteCommand();
|
||||
list_result.Add(new PushResult { Action = "Insert", TableName = "ResidentialAgreement", IsSuccess = insert_result > 0 });
|
||||
//}
|
||||
//删除非住宅评估
|
||||
delete_result = db_Zlb_Conn.Deleteable<NonInvestigateTable_Assessment>().Where(p => p.ProjectId == projectId).ExecuteCommandHasChange();
|
||||
list_result.Add(new PushResult { Action = "Delete", TableName = "NonInvestigateTable_Assessment", IsSuccess = delete_result });
|
||||
//if (delete_result)
|
||||
//{
|
||||
insert_result = db_Zlb_Conn.Insertable(db_Product_Conn.Queryable<NonInvestigateTable_Assessment>().Where(p => p.ProjectId == projectId).ToList()).ExecuteCommand();
|
||||
list_result.Add(new PushResult { Action = "Insert", TableName = "NonInvestigateTable_Assessment", IsSuccess = insert_result > 0 });
|
||||
//}
|
||||
//删除住宅评估
|
||||
delete_result = db_Zlb_Conn.Deleteable<InvestigateTable_Assessment>().Where(p => p.ProjectId == projectId).ExecuteCommandHasChange();
|
||||
list_result.Add(new PushResult { Action = "Delete", TableName = "InvestigateTable_Assessment", IsSuccess = delete_result });
|
||||
//if (delete_result)
|
||||
//{
|
||||
insert_result = db_Zlb_Conn.Insertable(db_Product_Conn.Queryable<InvestigateTable_Assessment>().Where(p => p.ProjectId == projectId).ToList()).ExecuteCommand();
|
||||
list_result.Add(new PushResult { Action = "Insert", TableName = "InvestigateTable_Assessment", IsSuccess = insert_result > 0 });
|
||||
//}
|
||||
//删除非住宅调查表
|
||||
delete_result = db_Zlb_Conn.Deleteable<NonResidentialInvestigateTable>().Where(p => p.ProjectId == projectId).ExecuteCommandHasChange();
|
||||
list_result.Add(new PushResult { Action = "Delete", TableName = "NonResidentialInvestigateTable", IsSuccess = delete_result });
|
||||
//if (delete_result)
|
||||
//{
|
||||
insert_result = db_Zlb_Conn.Insertable(db_Product_Conn.Queryable<NonResidentialInvestigateTable>().Where(p => p.ProjectId == projectId).ToList()).ExecuteCommand();
|
||||
list_result.Add(new PushResult { Action = "Insert", TableName = "NonResidentialInvestigateTable", IsSuccess = insert_result > 0 });
|
||||
//}
|
||||
//删除住宅调查表
|
||||
delete_result = db_Zlb_Conn.Deleteable<InvestigateTable>().Where(p => p.ProjectId == projectId).ExecuteCommandHasChange();
|
||||
list_result.Add(new PushResult { Action = "Delete", TableName = "InvestigateTable", IsSuccess = delete_result });
|
||||
//if (delete_result)
|
||||
//{
|
||||
insert_result = db_Zlb_Conn.Insertable(db_Product_Conn.Queryable<InvestigateTable>().Where(p => p.ProjectId == projectId).ToList()).ExecuteCommand();
|
||||
list_result.Add(new PushResult { Action = "Insert", TableName = "InvestigateTable", IsSuccess = insert_result > 0 });
|
||||
//}
|
||||
//删项目
|
||||
delete_result = db_Zlb_Conn.Deleteable<Projects>().Where(p => p.ID == projectId).ExecuteCommandHasChange();
|
||||
list_result.Add(new PushResult { Action = "Delete", TableName = "Projects", IsSuccess = delete_result });
|
||||
//if (delete_result)
|
||||
//{
|
||||
insert_result = db_Zlb_Conn.Insertable(db_Product_Conn.Queryable<Projects>().Where(p => p.ID == projectId).ToList()).ExecuteCommand();
|
||||
list_result.Add(new PushResult { Action = "Insert", TableName = "Projects", IsSuccess = insert_result > 0 });
|
||||
//}
|
||||
}
|
||||
StringBuilder push_error_reason = new();
|
||||
//if (list_result.Any(r => !r.IsSuccess))
|
||||
//{
|
||||
// list_result.ForEach(r =>
|
||||
// {
|
||||
// push_error_reason.AppendFormat("{0} 表:[{1}] 结果:[{2}] [{3}]", r.Action, r.TableName, r.IsSuccess, r.FailReason);
|
||||
// });
|
||||
// pushZlbObj.IsProcessed = 1;
|
||||
// pushZlbObj.LastProcessTime = DateTime.Now;
|
||||
// pushZlbObj.ProcessCount = pushZlbObj.ProcessCount + 1;
|
||||
// pushZlbObj.IsError = 1;
|
||||
// pushZlbObj.ErrorReason = push_error_reason.ToString();
|
||||
// db_Product_Conn.Updateable(pushZlbObj).UpdateColumns(u => new { u.IsProcessed, u.LastProcessTime, u.ProcessCount, u.IsError, u.ErrorReason }).ExecuteCommand();
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
pushZlbObj.IsProcessed = 1;
|
||||
pushZlbObj.LastProcessTime = DateTime.Now;
|
||||
if (pushZlbObj.ProcessCount == null)
|
||||
pushZlbObj.ProcessCount = 0;
|
||||
pushZlbObj.ProcessCount++;
|
||||
pushZlbObj.IsError = 0;
|
||||
pushZlbObj.ErrorReason = push_error_reason.ToString();
|
||||
db_Product_Conn.Updateable(pushZlbObj).UpdateColumns(u => new { u.IsProcessed, u.LastProcessTime, u.ProcessCount, u.IsError, u.ErrorReason }).WhereColumns(u => new { u.ID }).ExecuteCommand();
|
||||
//}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message + ex.StackTrace);
|
||||
}
|
||||
_logger.LogInformation("离开Push方法():Id:{Id} ,项目ID:{PrjId} at: {time}", pushZlbObj.ID, pushZlbObj.PrjId, DateTimeOffset.Now);
|
||||
return list_result;
|
||||
}
|
||||
}
|
||||
public class PushResult
|
||||
{
|
||||
public string Action { get; set; }
|
||||
public bool IsSuccess { get; set; }
|
||||
public string TableName { get; set; }
|
||||
public string FailReason { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user