using Furion;
using Furion.DataEncryption;
using Furion.Logging;
using Furion.RemoteRequest.Extensions;
using Furion.TaskScheduler;
using Microsoft.Extensions.Logging;
using Myshipping.Application.Entity;
using Myshipping.Core;
using Myshipping.Core.Entity;
using Myshipping.Core.Helper;
using Myshipping.Core.Service;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application.Job
{
///
/// 和川数据与东胜比对
///
public class HCDataCompareWorker : ISpareTimeWorker
{
[SpareTime(60000, "HCDataCompareWorker", Description = "和川数据与东胜比对", DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)]
public async void CompareData(SpareTimer timer, long count)
{
Log.Information($"HCDataCompareWorker {DateTime.Now}");
var tenantId = 349708986646597L;
var _sugerClient = App.GetService();
_sugerClient.QueryFilter.Clear();
var _repBooking = App.GetService>();
var _repCtn = App.GetService>();
var _cache = App.GetService();
var _log = App.GetService>();
var jsonUrl = _cache.GetAllDictData().Result.FirstOrDefault(x => x.TypeCode == "url_set" && x.Code == "hc_ds_compare_url").Value;
if (!jsonUrl.EndsWith("/"))
{
jsonUrl += "/";
}
//找到主单未开船数据
var listZhu = _repBooking.AsQueryable()
.Filter(null, true)
.Where(x => x.IsDeleted == false
&& x.TenantId == tenantId
&& !x.ATD.HasValue
&& (x.ParentId == null || x.ParentId == 0))
.Select(x => new HCDataCompareSendModel
{
Id = x.Id,
MBLNO = x.MBLNO
})
.ToList();
//获取分单,构建数据对象
var zhuIdList = listZhu.Select(x => x.Id).ToList();
var listFen = _repBooking.AsQueryable()
.Where(x => x.IsDeleted == false
&& x.TenantId == tenantId
&& x.ParentId != null
&& zhuIdList.Contains(x.ParentId.Value))
.Select(x => new
{
x.Id,
x.MBLNO,
x.ParentId,
x.VERSION
})
.ToList();
foreach (var item in listZhu)
{
item.Sub = listFen
.Where(x => x.ParentId == item.Id)
.Select(x => new HCDataCompareSendSubModel
{
Id = x.Id,
MBLNO = x.MBLNO,
VERSION = x.VERSION
})
.ToList();
}
//找到昨天至今删除的数据
var startTime = DateTime.Today.AddDays(-1);
var listDel = _repBooking.AsQueryable()
.Filter(null, true)
.Where(x => x.IsDeleted == true
&& x.TenantId == tenantId
&& x.CreatedTime > startTime)
.Select(x => new HCDataCompareSendModel
{
Id = x.Id,
MBLNO = x.MBLNO,
VERSION = x.VERSION
})
.ToList();
var sendModel = new
{
Normal = listZhu,
Deleted = listDel,
};
_log.LogInformation($"发送待比较数据:{JsonConvert.SerializeObject(sendModel)}");
var rtn = await jsonUrl
.SetBody(sendModel)
.PostAsStringAsync();
_log.LogInformation($"调用比对返回结果:{rtn}");
StringBuilder sbMessage = new StringBuilder();
var jobjRtn = JObject.Parse(rtn);
if (jobjRtn.GetIntValue("Code") == 200)
{
var data = jobjRtn.GetJObjectValue("Data");
if (data != null)
{
var normal = data.GetJArrayValue("Normal");
if (normal != null)
{
sbMessage.AppendLine("正常业务:");
foreach (JObject item in normal)
{
sbMessage.AppendLine($"{item.GetStringValue("MBLNO")}(ID:{item.GetStringValue("Id")}) 异常信息:{item.GetStringValue("Result")}");
}
}
var deleted = data.GetJArrayValue("Deleted");
if (deleted != null)
{
sbMessage.AppendLine("已删除业务:");
foreach (JObject item in deleted)
{
sbMessage.AppendLine($"{item.GetStringValue("MBLNO")}(ID:{item.GetStringValue("Id")}) 异常信息:{item.GetStringValue("Result")}");
}
}
if (sbMessage.Length > 0)
{
//推送钉钉消息
DingTalkGroupHelper.SendDingTalkGroupMessage("hechuanCompareNotify", "比对异常结果", sbMessage.ToString());
}
}
}
else
{
var msg = jobjRtn.GetStringValue("Message");
_log.LogError($"和川数据比对失败:{msg}");
//推送钉钉消息
DingTalkGroupHelper.SendDingTalkGroupMessage("hechuanCompareNotify", "数据比对未能完成", msg);
}
}
}
//发送比对模型对象
public class HCDataCompareSendModel
{
public long Id { get; set; }
public string MBLNO { get; set; }
public string VERSION { get; set; }
public List Sub { get; set; }
}
public class HCDataCompareSendSubModel
{
public long Id { get; set; }
public string MBLNO { get; set; }
public string VERSION { get; set; }
}
}