dengjunjie
7 天以前 bc43fe34a67fa68bcfb3a51b79308fad0be11c08
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
using HslCommunication;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Helper;
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, IRepository<Dt_MedicineGoods>>, IMedicineGoodsServices
    {
        public MedicineGoodsServices(IRepository<Dt_MedicineGoods> BaseDal) : base(BaseDal)
        {
        }
        public IRepository<Dt_MedicineGoods> Repository => BaseDal;
 
        static string SearchDate = "2025-10-30 00:00:00";
 
        /// <summary>
        /// 获取药品基础信息同步接口
        /// </summary>
        /// <returns></returns>
        public WebResponseContent GetMedicineGoodsInfom()
        {
            var responseContent = new WebResponseContent();
            try
            {
 
                var url = "http://121.37.118.63:80/GYZ2/95fck/goodsInfo";
 
 
                //if (string.IsNullOrEmpty(SearchDate)) SearchDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                //请求参数
                //var requestData = new
                //{
                //    searchDate = "2009-01-10 20:45:16"  // 正确的格式
                //};
                if (string.IsNullOrEmpty(SearchDate))
                {
                    // 默认查询过去24小时的数据
                    SearchDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                }
                // 请求参数
                var requestData = new
                {
                    searchDate = SearchDate
                };
                SearchDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                // 发起请求
                var result = HttpHelper.Post(url, requestData.ToJsonString());
 
                // 反序列化
                var response = JsonConvert.DeserializeObject<UpstreamResponse<MedicationsInfo>>(result);
 
                if (response == null || response.resultCode != "0")
                {
                    // 调用异常接口
                    //SendErrorToUpstream(5, "", response.resultMsg ?? "上游接口返回失败", "");
                    return responseContent.Error(response?.resultMsg ?? "上游接口返回失败");
                }
                if (response.data.Count < 1)
                {
                    return responseContent.OK("无新药品数据");
                }
 
                //获取所有已存在的商品编号
                var existingGoodsNos = BaseDal.Db.Queryable<Dt_MaterielInfo>()
                    .Select(g => g.MaterielCode)
                    .ToList();
 
                // 收集所有要插入的实体
                var entityList = response.data.Where(Medicat => !existingGoodsNos.Contains(Medicat.goods_no)).Select(Medicat => new Dt_MaterielInfo
                {
                    MaterielCode = Medicat.goods_no,
                    MaterielErpType = Medicat.goods_type,
                    item = Medicat.goods_tym, //通用名
                    MaterielName = Medicat.goods_spm,//物料名称
                    Dosage_form = GetDosageForm(Medicat.dosage_form),//药剂
                    Barcode = Medicat.barcode,//条形吗
                    MaterielSpec = Medicat.model,
                    Factory = Medicat.factory,
                    MaterielUnit = Medicat.unit,//单位
                    MaterielSourceType = MaterielSourceTypeEnum.PurchasePart,
                    // 使用 ?? 运算符提供默认值
                    MaterielLength = (float?)Medicat.item_length,
                    MaterielWide = (float?)Medicat.item_width,
                    MaterielHeight = (float?)Medicat.item_hight,
                    MaterielVolume = (float?)Medicat.item_volumn,
                    MaterielWeight = (float?)Medicat.item_weight,
                    MaterielStoragecondition = float.TryParse(Medicat.storage_confition, out var val) ? val : null, //储存条件
                    Remark = Medicat.remark,
                    GoodStatusState = "未下发",
                    ModifyDate = Medicat.modify_date
                }).ToList();
                // 一次性批量插入数据库
                BaseDal.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
            {
                //把小件未下发的的信息和箱规为0,最低库存为0的过滤掉
                //var list = BaseDal.Db.Queryable<Dt_MaterielInfo>().Where(x => x.GoodStatusState == "未下发" && x.MaterielSourceType == MaterielSourceTypeEnum.SelfMadePart).ToList();
                var list = BaseDal.Db.Queryable<Dt_MaterielInfo>().Where(x => x.MaterielSourceType == MaterielSourceTypeEnum.SelfMadePart && x.BoxQty != 0 && x.MinQty != 0 && x.GoodStatusState == "未下发").ToList();
                if (list.Count < 1)
                {
                    return new WebResponseContent().OK("无未下发药品");
                }
                //找到了就将整个list发给下游系统
                foreach (var item in list)
                {
                    var medicineDTO = new ProductInfo
                    {
                        //用户code
                        customerCode = "905",
                        //物料类型
                        materialCode = item.MaterielErpType,
                        //产品编码
                        productCode = item.MaterielCode,
                        //产品名
                        productName = item.MaterielName,
                        //产品条码
                        productBarCode = item.Barcode,
 
                        //药剂
                        medicine = item.Dosage_form,//这里要调用方法,转化方法
 
                        //规格
                        productSpecifications = item.MaterielSpec,
                        //单位
                        unit = item.MaterielUnit,
                        //长
                        singleProductLongNum = (item.MaterielLength ?? 0).ToString(),
                        //宽
                        singleProductWideNum = (item.MaterielWide ?? 0).ToString(),
                        //高
                        singleProductHighNum = (item.MaterielHeight ?? 0).ToString(),
                        //重量
                        singleProductWeight = (item.MaterielWeight ?? 0).ToString(),
                        //体积
                        singleProductVolume = (item.MaterielVolume ?? 0).ToString(),
                        //是否取消 0是不删除,1删除
                        isDelete = "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")
                    {
                        //更新药品状态-已完成
                        BaseDal.Db.Updateable<Dt_MaterielInfo>()
                            .SetColumns(x => x.GoodStatusState == "下发完成")
                            .Where(x => x.MaterielCode == item.MaterielCode)
                            .ExecuteCommand();
                    }
                    else
                    {
                        //SendErrorToUpstream(5, medicineDTO.materialCode, resp?.msg ?? "WCS药品信息同步失败", "");
                        //return new WebResponseContent { Status = false, Message = "订单推送失败" };
                        continue;
                    }
 
                }
                return new WebResponseContent { Status = true, Message = "成功" };
            }
            catch (Exception ex)
            {
                Console.WriteLine("ProductSynchronous 异常:" + ex.Message);
                return new WebResponseContent { Status = false, Message = ex.Message };
            }
 
        }
 
        /// <summary>
        /// 同步删除删除商品信息 
        /// 药品编码
        /// </summary>
        /// <returns></returns>
        public WebResponseContent DeleteProduct(string goodsno)
        {
            try
            {
                var goods = BaseDal.Db.Queryable<Dt_MaterielInfo>().Where(x => x.MaterielCode == goodsno).First();
                if (goods == null)
                {
                    return new WebResponseContent
                    {
                        Status = false,
                        Message = $"商品编号 {goodsno} 不存在"
                    };
                }
                var medicineDTO = new ProductInfo
                {
                    //用户code
                    customerCode = "905",
                    //物料类型
                    materialCode = "YY",
                    //产品编码
                    productCode = goods.MaterielCode,
                    //产品名
                    productName = goods.MaterielName,
                    //产品条码
                    productBarCode = goods.MaterielCode,
 
                    //规格
                    productSpecifications = goods.MaterielSpec,
                    //单位
                    unit = goods.MaterielUnit,
                    //长
                    singleProductLongNum = goods.MaterielLength.ToString(),
                    //宽
                    singleProductWideNum = goods.MaterielWide.ToString(),
                    //高
                    singleProductHighNum = goods.MaterielHeight.ToString(),
                    //重量
                    singleProductWeight = goods.MaterielWeight.ToString(),
                    //体积
                    singleProductVolume = goods.MaterielVolume.ToString(),
                    //是否取消 0是不删除,1删除
                    isDelete = "1"
                };
                var url = "http://172.16.1.2:9357/file-admin/api/product/productSynchronous";
                //var url = "http://172.16.1.245: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")
                {
                    //我自己也删除
                    base.Db.Deleteable<Dt_MaterielInfo>().Where(x => x.MaterielCode == goodsno);
                    return new WebResponseContent { Status = true, Message = "删除成功" };
                }
                return new WebResponseContent { Status = false, Message = "删除失败请重试" };
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.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://121.37.118.63:80/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);
            }
        }
        /// <summary>
        /// 创建药剂字典
        /// </summary>
        private static readonly Dictionary<string, string> _dosageFormMap = new Dictionary<string, string>
        {
            {"001", "片剂"}, {"002", "片剂"}, {"003", "片剂"}, {"004", "片剂"}, {"005", "片剂"},
            {"006", "片剂"}, {"007", "片剂"}, {"008", "片剂"}, {"009", "片剂"}, {"010", "片剂"},
            {"011", "其它"}, {"012", "片剂"}, {"013", "其它"}, {"014", "片剂"}, {"015", "片剂"},
            {"016", "片剂"}, {"017", "片剂"}, {"018", "其它"}, {"019", "其它"}, {"020", "其它"},
            {"021", "针剂"}, {"022", "针剂"}, {"023", "针剂"}, {"024", "针剂"}, {"025", "中成药"},
            {"026", "中成药"}, {"027", "其它"}, {"028", "其它"}, {"029", "其它"}, {"030", "其它"},
            {"031", "其它"}, {"032", "其它"}, {"033", "其它"}, {"034", "中成药"}, {"035", "中成药"},
            {"036", "中成药"}, {"037", "中成药"}, {"038", "其它"}, {"039", "其它"}, {"040", "其它"},
            {"041", "中成药"}, {"042", "中成药"}, {"043", "中成药"}, {"044", "其它"}, {"045", "中成药"},
            {"046", "中成药"}, {"047", "中成药"}, {"048", "中成药"}, {"049", "中成药"}, {"050", "中成药"},
            {"051", "中成药"}, {"052", "中成药"}, {"053", "中成药"}, {"054", "中成药"}, {"055", "中成药"},
            {"056", "其它"}, {"057", "其它"}, {"058", "其它"}, {"059", "其它"}, {"060", "其它"},
            {"061", "其它"}, {"062", "其它"}, {"063", "其它"}, {"064", "其它"}, {"065", "其它"},
            {"066", "其它"}, {"067", "中成药"}, {"068", "中成药"}, {"069", "中成药"}, {"070", "其它"},
            {"071", "其它"}, {"072", "其它"}, {"073", "中成药"}, {"074", "其它"}, {"075", "其它"},
            {"076", "中成药"}, {"077", "其它"}, {"078", "其它"}, {"079", "中成药"}, {"080", "中成药"},
            {"081", "中成药"}, {"082", "中成药"}, {"083", "其它"}, {"084", "中成药"}, {"085", "其它"},
            {"086", "中成药"}, {"087", "其它"}, {"088", "其它"}, {"089", "中成药"}, {"090", "中成药"},
            {"091", "其它"}, {"092", "其它"}, {"093", "其它"}, {"094", "中成药"}, {"095", "其它"},
            {"096", "其它"}, {"097", "其它"}, {"098", "其它"}, {"099", "其它"}, {"100", "其它"},
            {"101", "其它"}, {"102", "其它"}, {"103", "其它"}, {"104", "其它"}, {"105", "其它"},
            {"106", "其它"}, {"107", "其它"}, {"108", "其它"}, {"109", "其它"}, {"110", "其它"},
            {"111", "其它"}, {"112", "其它"}, {"113", "其它"}, {"114", "其它"}, {"115", "其它"},
            {"116", "其它"}, {"117", "其他"}
        };
 
        /// <summary>
        /// 根据剂型编号获取剂型分类
        /// </summary>
        /// <param name="dosageCode">剂型编号(001-117)</param>
        /// <returns>剂型分类(片剂/针剂/中成药/其它/其他)</returns>
        public static string GetDosageForm(string dosageCode)
        {
            if (string.IsNullOrEmpty(dosageCode))
                return "未知";
 
            // 处理编号格式,确保是3位数字
            if (dosageCode.Length == 1)
                dosageCode = "00" + dosageCode;
            else if (dosageCode.Length == 2)
                dosageCode = "0" + dosageCode;
 
            if (_dosageFormMap.TryGetValue(dosageCode, out string form))
            {
                return form;
            }
 
            return "未知";
        }
 
 
    }
}