1
yanjinhui
2025-09-24 bd09f6b4fdb821c9fb63e4dd0e9b184d29e9f148
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using Masuit.Tools;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Helper;
using WIDESEA_ISquareCabinRepository;
using WIDESEA_ISquareCabinServices;
using WIDESEA_Model.Models;
using WIDESEA_SquareCabinRepository;
using static System.Net.WebRequestMethods;
using static WIDESEA_DTO.SquareCabin.OrderDto;
 
namespace WIDESEA_SquareCabinServices
{
    public class CabinOrderServices : ServiceBase<Dt_CabinOrder, ICabinOrderRepository>, ICabinOrderServices
    {
        public CabinOrderServices(ICabinOrderRepository BaseDal) : base(BaseDal)
        {
        }
 
        /// <summary>
        /// 获取上游系统的入库单
        /// </summary>
        /// <param name="searchDate"></param>
        /// <returns></returns>
        public WebResponseContent GetUpstreamOrder(DateTime searchDate)
        {
            var responseContent = new WebResponseContent();
 
            try
            {
                // 请求地址
                var url = "http://127.0.0.1:9090/GYZ2/95fck/inOrder";
 
                // 请求参数
                var requestData = new
                {
                    searchDate = searchDate.ToString("yyyy-MM-dd HH:mm:ss")
                };
 
                // 发起请求
                var result = HttpHelper.Post(url, requestData.ToJsonString());
 
          
                // 反序列化
                var response = JsonConvert.DeserializeObject<UpstreamResponse<UpstreamOrderInfo>>(result);
 
                if (response.resultCode != "0")
                {
                    // 调用异常接口
                    SendErrorToUpstream(1, "", response.resultMsg ?? "上游接口返回失败", "");
                    return responseContent.Error(response.resultMsg ?? "上游接口返回失败");
                }
 
                if (response.data == null || !response.data.Any())
                {
                    return responseContent.OK("无新入库单数据");
                }
 
                Db.Ado.BeginTran();
 
                foreach (var order in response.data)
                {
                    try
                    {
                        // 插入入库单表
                        var entityOrder = new Dt_CabinOrder
                        {
                            Order_no = order.order_no,
                            Order_type = order.order_type,
                            Supplier_no = order.supplier_no,
                            Account_tiem= order.account_time,
                        };
 
                        var orderId = Db.Insertable(entityOrder).ExecuteReturnIdentity(); //就是返回主键ID。
 
                        // 插入入库明细表
                        var detailEntities = order.details.Select(d => new Dt_CabinOrderDetail
                        {
                            OrderId = orderId,
                            Goods_no = d.goods_no,
                            Order_qty = d.order_qty,
                            Batch_num = d.batch_num,
                            Exp_date = d.exp_date,
                            Warehouse_no = d.warehouse_no,
                            Status = 0,
                        }).ToList();
 
                        Db.Insertable(detailEntities).ExecuteCommand();
                    }
                    catch (Exception innerEx)
                    {
                        // 针对某条订单报错时,推送异常给上游
                        SendErrorToUpstream(1, order.order_no, innerEx.Message, "");
                        throw; // 抛出异常,让外层捕获回滚
                    }
                }
 
                Db.Ado.CommitTran();
 
                return responseContent.OK("同步入库单成功");
            }
            catch (Exception ex)
            {
                // 全局异常时,也推送异常给上游
                SendErrorToUpstream(1, "", ex.Message, "");
                Db.Ado.RollbackTran();
                return responseContent.Error("同步失败: " + ex.Message);
            }
        }
 
        /// <summary>
        /// 入库报完成接口
        /// </summary>
        /// <param name="order_no">入库单号</param>
        /// <returns></returns>
        public WebResponseContent CompleteOrder(string order_no)
        {
            var responseContent = new WebResponseContent();
            try
            {
                if (string.IsNullOrWhiteSpace(order_no))
                {
                    return responseContent.Error("入库单号不可以为空");
                }
                var url = " http://127.0.0.1:9090/GYZ2/95fck/inOrderOk";
                var reslut=HttpHelper.Post(url, new {order_no }.ToJsonString());
                var response = JsonConvert.DeserializeObject<UpstreamOrderResponse>(reslut);
                if (response.resultCode != "0")
                {
                    SendErrorToUpstream(2, "", "上游接口返回失败", "");
                    return responseContent.Error(response.resultMsg ?? "上游接口返回失败");
                }
               return  responseContent.OK("操作成功");
            }
            
            catch (Exception ex)
            {
                SendErrorToUpstream(1, "", ex.Message, "");
                return responseContent.Error("同步失败: " + ex.Message);
            }
            
        }
 
        /// <summary>
        /// 推送异常信息给上游系统1.入库单接口;2.入库单报完成接口;3.出库单接口;4.出库报完成接口;5.药品基础信息同步接口;6.供应商信息接口;7.客户信息接口;8.库存
        /// </summary>
        public void SendErrorToUpstream(int type, string code, string message, string remark)
        {
            try
            {
                var url = "http://127.0.0.1:9090/GYZ2/95fck/lexceptionLogog";
 
                var requestData = new
                {
                    type = type.ToString(),
                    code = code,
                    message = message,
                    remark = remark
                };
 
                var result = HttpHelper.Post(url, requestData.ToJsonString());
                // 可以反序列化检查 resultCode 是否为0
            }
            catch (Exception e)
            {
                // 这里不要再抛异常了,避免死循环
                Console.WriteLine("异常接口推送失败:" + e.Message);
            }
        }
 
 
 
      
 
    }
}