cdk
2 天以前 38339731265cb8ffef7954fb90874036fd46f09d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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;
    }
 
}