1
yanjinhui
2025-09-29 18ef8c37e6290ba6f47cbd5bbd26e56a682d767e
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using AngleSharp.Dom;
using Masuit.Tools;
using Newtonsoft.Json;
using SharpCompress.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
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 static WIDESEA_DTO.SquareCabin.OrderDto;
using static WIDESEA_DTO.SquareCabin.TowcsDto;
 
namespace WIDESEA_SquareCabinServices
{
    public class MedicineGoodsServices : ServiceBase<Dt_MedicineGoods, IMedicineGoodsRepository>, IMedicineGoodsServices
    {
        public MedicineGoodsServices(IMedicineGoodsRepository BaseDal) : base(BaseDal)
        {
        }
 
        /// <summary>
        /// 获取药品基础信息同步接口
        /// </summary>
        /// <returns></returns>
        public WebResponseContent GetMedicineGoodsInfom(DateTime searchDate)
        {
            var responseContent = new WebResponseContent();
            try
            {
              var url = "http://127.0.0.1:9090/GYZ2/95fck/goodsInfo";
 
                // 请求参数
                var requestData = new
                {
                    searchDate = searchDate.ToString("yyyy-MM-dd HH:mm:ss")
                };
 
                // 发起请求
                var result = HttpHelper.Post(url, requestData.ToJsonString());
 
                // 反序列化
                var response = JsonConvert.DeserializeObject<UpstreamResponse<MedicationsInfo>>(result);
 
                if (response.resultCode != "0")
                {
                    // 调用异常接口
                    SendErrorToUpstream(5, "", response.resultMsg ?? "上游接口返回失败", "");
                    return responseContent.Error(response.resultMsg ?? "上游接口返回失败");
                }
                if (response.data == null || !response.data.Any())
                {
                    return responseContent.OK("无新药品数据");
                }
 
                #region
                //foreach (var Medicat in response.data)
                //{
                //    var entity = new Dt_MedicineGoods
                //    {
                //        Goods_no = Medicat.goods_no,
                //        Goods_spm = Medicat.goods_spm,
                //        Model = Medicat.model,
                //        Factory = Medicat.factory,
                //        Unit = Medicat.unit,
                //        Item_Iength = Medicat.item_length,
                //        Item_weight = Medicat.item_weight,
                //        Item_hight = Medicat.item_hight,
                //        Item_volumn = Medicat.item_volumn,
                //        Storage_cond = Medicat.storage_cond,
                //        Remark = Medicat.remark,
                //        GoodStatus = "未下发",
                //        ModifyDate = Medicat.modify_date
                //    };
                //    AddData(entity);
                //}
                #endregion
                // 收集所有要插入的实体
                var entityList = response.data.Select(Medicat => new Dt_MedicineGoods
                {
                    Goods_no = Medicat.goods_no,
                    Goods_spm = Medicat.goods_spm,
                    Model = Medicat.model,
                    Factory = Medicat.factory,
                    Unit = Medicat.unit,
                    Item_Iength = Medicat.item_length,
                    Item_weight = Medicat.item_weight,
                    Item_hight = Medicat.item_hight,
                    Item_volumn = Medicat.item_volumn,
                    Storage_cond = Medicat.storage_cond,
                    Remark = Medicat.remark,
                    GoodStatus = "未下发",
                    ModifyDate = Medicat.modify_date
                }).ToList();
                // 一次性批量插入数据库
                Db.Insertable(entityList).ExecuteCommand();
 
                ProductSynchronous();
                return responseContent.OK("同步成功");
            }
            
            catch (Exception ex)
            {
                SendErrorToUpstream(5, "", ex.Message, "");
                return responseContent.Error(ex.Message);
            }
        }
 
 
        /// <summary>
        /// 发给下游wcs同步药品信息
        /// </summary>
        /// <returns></returns>
        public WebResponseContent ProductSynchronous()
        {
            try
            {
                //先找到所有未下发的药品
                var list = BaseDal.QueryData(x => x.GoodStatus == "未下发").ToList();
                if (list == null || !list.Any())
                {
                    return new WebResponseContent().OK("无未下发药品");
                }
                //找到了就将整个list发给下游系统
                foreach (var item in list)
                {
                    var medicineDTO = new ProductInfo
                    {
                        //用户code
                        customerCode = "905",
                        //物料类型
                        materialCode = "YY",
                        //产品编码
                        productCode= item.Goods_no,
                        //产品名
                        productName =item.Goods_spm ,
                        //产品条码
                        productBarCode= item.Goods_no,
 
                        //规格
                        productSpecifications=item.Model,
                        //单位
                        unit = item.Unit,
                        //长
                        singleProductLongNum = item.Item_Iength.ToString(),
                        //宽
                        singleProductWideNum = item.Item_weight.ToString(),
                        //高
                        singleProductHighNum = item.Item_hight.ToString(),
                        //重量
                        singleProductWeight = item.Item_weight.ToString(),
                        //体积
                        singleProductVolume = item.Item_volumn.ToString(),
                        //是否取消
                        Is_delete = "0"
                    };
                    var url = "http://172.16.1.2:9357/file-admin/api/product/productSynchronous";
                    var result = HttpHelper.Post(url, medicineDTO.ToJsonString());
                    var resp = JsonConvert.DeserializeObject<TowcsResponse<object>>(result);
 
                    if (resp!=null&&resp.code=="0")
                    {
                        //更新药品状态-已完成
                      Db.Updateable<Dt_MedicineGoods>()
                            .SetColumns(x => x.GoodStatus == "下发完成")
                            .Where(x => x.Goods_no == item.Goods_no)
                            .ExecuteCommand();  
                    }
                    else
                    {
                        SendErrorToUpstream(5, medicineDTO.materialCode, resp?.msg ?? "WCS药品信息同步失败", "");
                      
                        return new WebResponseContent { Status = false, Message = "订单推送失败" };
                    }
 
                }
                return new WebResponseContent { Status = true, Message = "成功" };
            }
            catch (Exception ex)
            {
                return new WebResponseContent { Status = false, Message = 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);
            }
        }
 
       
    }
}