using Kingdee.CDP.WebApi.SDK;
|
using MES.Service.Models;
|
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc.Filters;
|
using NewPdaSqlServer.DB;
|
using NewPdaSqlServer.Dto;
|
using NewPdaSqlServer.entity;
|
using NewPdaSqlServer.util;
|
using Newtonsoft.Json;
|
using Newtonsoft.Json.Linq;
|
using SqlSugar;
|
using System.Text;
|
using System.Net.Http;
|
|
namespace MES.Service.service.Warehouse;
|
|
|
public class MesSuppScManager : Repository<MesSuppSc>
|
{
|
private const string SourceUrl = "https://qixizhaopin.modaniot.com:8443/wetchat/ToGSMesServlet?password=gsmes";
|
|
/// <summary>
|
/// 从指定网址读取 JSON 数据并保存到 MES_SUPPSCORE(主)和 MES_SUPPSCORE_DETAIL(子)表
|
/// </summary>
|
public bool FetchAndSaveFromUrl()
|
{
|
string json;
|
try
|
{
|
using var http = new HttpClient();
|
http.Timeout = TimeSpan.FromSeconds(30);
|
json = http.GetStringAsync(SourceUrl).Result;
|
}
|
catch (Exception ex)
|
{
|
throw new Exception($"获取远程数据失败: {ex.Message}", ex);
|
}
|
|
if (string.IsNullOrWhiteSpace(json))
|
throw new Exception("远程返回内容为空");
|
|
JToken root;
|
try
|
{
|
root = JToken.Parse(json);
|
}
|
catch (Exception ex)
|
{
|
throw new Exception($"解析 JSON 失败: {ex.Message}");
|
}
|
|
var data = root["data"] as JArray;
|
if (data == null || data.Count == 0)
|
throw new Exception("JSON 中未包含 data 或 data 为空");
|
|
var mains = new List<MesSuppSc>();
|
var details = new List<MesSuppScDetail>();
|
|
foreach (var item in data)
|
{
|
var gysmc = item["gysmc"]?.ToString();
|
var zf = item["zf"]?.ToString();
|
var xmdfStr = item["xmdf"]?.ToString();
|
|
decimal? zfDec = null;
|
if (!string.IsNullOrWhiteSpace(zf) && decimal.TryParse(zf, out var tmp))
|
zfDec = tmp;
|
|
var mainId = Guid.NewGuid();
|
mains.Add(new MesSuppSc
|
{
|
ID = mainId,
|
SuppDate = null, // 若 JSON 包含日期字段,可在此填充
|
SuppName = gysmc,
|
SuppNum = zfDec
|
});
|
|
// 解析子表 xmdf 字符串(xmdf 本身是一个 JSON 字符串)
|
if (!string.IsNullOrWhiteSpace(xmdfStr))
|
{
|
try
|
{
|
var subArr = JArray.Parse(xmdfStr);
|
foreach (var sub in subArr)
|
{
|
var xm1 = sub["xm1"]?.ToString();
|
var xm2 = sub["xm2"]?.ToString();
|
var dykhdf = sub["dykhdf"]?.ToString();
|
|
decimal? score = null;
|
if (!string.IsNullOrWhiteSpace(dykhdf) && decimal.TryParse(dykhdf, out var s))
|
score = s;
|
|
details.Add(new MesSuppScDetail
|
{
|
ID = Guid.NewGuid(),
|
PID = mainId,
|
XM1 = xm1,
|
XM2 = xm2,
|
DYKHDF = score
|
});
|
}
|
}
|
catch (Exception ex)
|
{
|
// 忽略单条解析错误,但记录异常信息
|
Console.WriteLine($"解析 xmdf 失败(供应商 {gysmc}): {ex.Message}");
|
}
|
}
|
}
|
|
// 写入数据库:事务内先删除旧数据再插入新数据
|
var result = UseTransaction(db =>
|
{
|
// 先清子表,再清主表
|
db.Deleteable<MesSuppScDetail>().ExecuteCommand();
|
db.Deleteable<MesSuppSc>().ExecuteCommand();
|
|
var mainInserted = 0;
|
if (mains.Count > 0)
|
mainInserted = db.Insertable(mains).ExecuteCommand();
|
|
var detailInserted = 0;
|
if (details.Count > 0)
|
detailInserted = db.Insertable(details).ExecuteCommand();
|
|
return (mainInserted + detailInserted) > 0 ? 1 : 0;
|
}) > 0;
|
|
return result;
|
}
|
|
}
|