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
using Masuit.Tools;
using Newtonsoft.Json;
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_DTO.SquareCabin;
using WIDESEA_ISquareCabinRepository;
using WIDESEA_ISquareCabinServices;
using WIDESEA_Model.Models;
using WIDESEA_SquareCabinRepository;
using static WIDESEA_DTO.SquareCabin.OrderDto;
 
namespace WIDESEA_SquareCabinServices
{
    public class DeliveryOrderServices : ServiceBase<Dt_DeliveryOrder, IDeliveryOrderRepository>, IDeliveryOrderServices
    {
        public DeliveryOrderServices(IDeliveryOrderRepository BaseDal) : base(BaseDal)
        {
        }
 
   
 
 
 
        /// <summary>
        /// 获取上游出库单 0成功1失败
        /// </summary>
        /// <param name="searchDate"></param>
        /// <returns></returns>
        public WebResponseContent GetUpstreamOutOrder(DateTime searchDate)
        {
            var responseContent = new WebResponseContent();
            try
            {
                //请求地址
                var url = "http:/127.0.0.1:9000/GYZ2/95fck/outOrder";
                //请求参数
                var requestDate = new
                {
                    searchDate = searchDate.ToString("yyyy-MM-dd:mm:ss")
                };
 
                //发起请求
                var reslu = HttpHelper.Post(url, requestDate.ToJsonString());
                //反序列化
                var response = JsonConvert.DeserializeObject<UpstreamResponse<UpstramOutOrderInfo>>(reslu);
 
                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 outorder in response.data)
                {
                    try
                    {
                        //插入出库单
                        var OutOders = new Dt_DeliveryOrder
                        {
                            Out_no = outorder.out_no,
                            Out_type = outorder.out_type,
                            Client_no=outorder.client_no,
                            Account_time = outorder.account_time,
                        };
                        var outorderId = Db.Insertable(outorder).ExecuteReturnIdentity();//返回主键id
 
                        //插入出库单明细
                        var OutOrderDetails = outorder.details.Select(d => new Dt_DeliveryOrderDetail
                        {
                            DeliveryOrderId = outorderId,
                            Goods_no = d.goods_no,
                            Order_qty = d.out_qty,
                            Batch_num = d.batch_num,
                            Exp_date = d.exp_date
                        }).ToList();
                        Db.Insertable(OutOrderDetails).ExecuteCommand();
                    }
                    catch (Exception ex)
                    {
 
                        // 针对某条订单报错时,推送异常给上游
                        SendErrorToUpstream(3, outorder.out_no, ex.Message, "");
                        throw; // 抛出异常,让外层捕获回滚
                    }
                }
                Db.Ado.CommitTran();
                return responseContent.OK("同步入库单成功");
            }
            catch (Exception ex)
            {
                // 全局异常时,也推送异常给上游
                SendErrorToUpstream(3, "", ex.Message, "");
                Db.Ado.RollbackTran();
                return responseContent.Error("同步失败: " + ex.Message);
            }
        }
 
 
        /// <summary>
        /// 出库报完成接口
        /// </summary>
        /// <param name="out_no">出库单号</param>
        /// <returns></returns>
        public WebResponseContent CompleteOutOrder(string out_no)
        {
            var responseContent = new WebResponseContent();
            try
            {
                if (string.IsNullOrWhiteSpace(out_no))
                {
                    return responseContent.Error("入库单号不可以为空");
                }
                var url = " http://127.0.0.1:9090/GYZ2/95fck/outOrderOk";
                var reslut = HttpHelper.Post(url, new {out_no }.ToJsonString());
                var response = JsonConvert.DeserializeObject<UpstreamOrderResponse>(reslut);
                if (response.resultCode != "0")
                {
                    SendErrorToUpstream(4, "", "上游接口返回失败", "");
                    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/exceptionLog";
 
                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);
            }
        }
    }
}