zjh
7 天以前 98958cdd70d3a63a562e9238ddecb9c6c6865e95
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using MES.Service.DB;
using MES.Service.Dto.service;
using MES.Service.Modes;
using Newtonsoft.Json;
using System.Text;
 
namespace MES.Service.service.Warehouse;
 
public class MesPalletBinding1Manager : Repository<MesPalletBinding1>
{
    /// <summary>
    /// 调用接口获取栈板绑定信息
    /// </summary>
    /// <param name="stackCode">栈板码</param>
    /// <returns>接口返回结果</returns>
    public async Task<GetTransferListResponse> GetTransferListByStackCodeAsync(
        MesPalletBinding palletBinding)
    {
        try
        {
            // API URL
            string apiUrl =
                "http://no2api.dream-maker.com/bu/storage/pallet/getTransferList";
 
            // 构建请求体
            var requestData = new { stackCode = palletBinding.StackCode };
            var jsonContent = JsonConvert.SerializeObject(requestData);
            var content = new StringContent(jsonContent, Encoding.UTF8,
                "application/json");
 
            // 发送HTTP请求
            using (var httpClient = new HttpClient())
            {
                var response = await httpClient.PostAsync(apiUrl, content);
                var responseContent =
                    await response.Content.ReadAsStringAsync();
 
                // 反序列化响应
                var result =
                    JsonConvert.DeserializeObject<GetTransferListResponse>(
                        responseContent);
                return result;
            }
        }
        catch (Exception ex)
        {
            throw new Exception($"调用接口获取栈板绑定信息失败: {ex.Message}");
        }
    }
 
    /// <summary>
        /// 将接口返回的数据插入到数据库中
        /// </summary>
        /// <param name="palletBinding">栈板绑定信息</param>
        /// <returns>插入记录数</returns>
        public int InsertPalletBindingData(MesPalletBinding palletBinding)
        {
            var response = GetTransferListByStackCodeAsync(palletBinding).Result;
 
            if (response?.Data?.SnList == null || string.IsNullOrEmpty(response.Data.StackCode))
            {
                return 0;
            }
 
            int insertedCount = 0;
            
            try
            {
                // 准备要插入的数据列表
                var bindingList = new List<MesPalletBinding1>();
                
                foreach (var snItem in response.Data.SnList)
                {
                    var binding = new MesPalletBinding1
                    {
                        Stackcode = response.Data.StackCode,
                        SnNo = snItem.SnNo,
                        TicketNo = snItem.TickeNo,
                        Mediumboxcode = snItem.MediumBoxCode,
                        CreateDate = DateTime.Now,
                        IsInbound = 0 // 默认未入库
                    };
                    
                    bindingList.Add(binding);
                }
                
                // 批量插入数据到数据库
                if (bindingList.Any())
                {
                    // 批量插入新数据
                    var insertedIds = Db.Insertable(bindingList).PageSize(1)
                        .IgnoreColumnsNull()
                        .ExecuteCommand();
                    insertedCount = insertedIds;
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"插入栈板绑定数据失败: {ex.Message}");
            }
            
            return insertedCount;
        }
 
        /// <summary>
        /// 检查栈板码在数据库中是否存在
        /// </summary>
        /// <param name="stackCode">栈板码</param>
        /// <returns>存在返回true,否则返回false</returns>
        public bool CheckStackCodeExists(string stackCode)
        {
            try
            {
                if (string.IsNullOrEmpty(stackCode))
                {
                    return false;
                }
 
                // 查询MES_PALLET_BINDING表中是否存在该栈板码的数据
                var exists = Db.Queryable<MesPalletBinding1>()
                    .Any(x => x.Stackcode == stackCode);
 
                return exists;
            }
            catch (Exception ex)
            {
                throw new Exception($"检查栈板码是否存在时出错: {ex.Message}");
            }
        }
 
        /// <summary>
        /// 将接口返回的数据插入到数据库中(带存在性检查)
        /// </summary>
        /// <param name="palletBinding">栈板绑定信息</param>
        /// <returns>操作结果</returns>
        public async Task<int> InsertPalletBindingDataWithCheckAsync(MesPalletBinding palletBinding)
        {
            // 先检查栈板码是否存在
            bool exists = CheckStackCodeExists(palletBinding.StackCode);
            return exists ? 1 :
                // 调用原来的插入方法
                InsertPalletBindingData(palletBinding);
        }
 
    /// <summary>
    /// 根据栈板码删除旧数据
    /// </summary>
    /// <param name="stackCode">栈板码</param>
    /// <returns>删除记录数</returns>
    private async Task<int> DeleteByStackCodeAsync(string stackCode)
    {
        try
        {
            var deleteResult = await Db.Deleteable<MesPalletBinding1>()
                .Where(x => x.Stackcode == stackCode)
                .ExecuteCommandAsync();
            return deleteResult;
        }
        catch (Exception ex)
        {
            // 记录错误但不抛出异常,允许插入操作继续
            Console.WriteLine($"删除旧数据时出错: {ex.Message}");
            return 0;
        }
    }
}