username@email.com
2024-09-09 cc170291673472d3cda8d7ea77f6bd3a3b5dbb83
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/***********************************************************************
 *            Project: baifenBinfa
 *        ProjectName: 百分兵法管理系统                               
 *                Web: http://chuanyin.com                     
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 202403/02   
 *        Description: 暂无
 ***********************************************************************/
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.Caching.AutoMate.RedisCache;
using CoreCms.Net.Configuration;
using CoreCms.Net.DTO;
using CoreCms.Net.IRepository;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.IServices;
using CoreCms.Net.Loging;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.baifenbingfa.DistributionSendOder;
using CoreCms.Net.Model.ViewModels.Api;
using CoreCms.Net.Model.ViewModels.DTO;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Utility.Helper;
using Essensoft.Paylink.Alipay.Domain;
using Flurl.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinExpressDeliveryOpenMessageGetDeliveryListResponse.Types;
 
 
namespace CoreCms.Net.Services
{
    /// <summary>
    /// 发货单表 接口实现
    /// </summary>
    public class CoreCmsBillDeliveryServices : BaseServices<CoreCmsBillDelivery>, ICoreCmsBillDeliveryServices
    {
        private readonly ICoreCmsBillDeliveryRepository _dal;
        private readonly ICoreCmsStoreServices _storeServices;
        private readonly ICoreCmsBillDeliveryItemServices _billDeliveryItemServices;
        private readonly ICoreCmsOrderLogServices _orderLogServices;
        private readonly ICoreCmsSettingServices _settingServices;
        private readonly IUnitOfWork _unitOfWork;
        private readonly IServiceProvider _serviceProvider;
        private readonly IRedisOperationRepository _redisOperationRepository;
        private readonly ICoreCmsDistributionRepository _coreCmsDistributionRepository;
 
        private readonly IHttpContextUser _user;
 
 
        public CoreCmsBillDeliveryServices(
            IUnitOfWork unitOfWork,
            IServiceProvider serviceProvider
            , ICoreCmsBillDeliveryRepository dal
            , ICoreCmsStoreServices storeServices
            , ICoreCmsBillDeliveryItemServices billDeliveryItemServices
            , ICoreCmsOrderLogServices orderLogServices
            , ICoreCmsSettingServices settingServices
            , IRedisOperationRepository redisOperationRepository,
                ICoreCmsDistributionRepository coreCmsDistributionRepository
            , IHttpContextUser user)
        {
            this._dal = dal;
            base.BaseDal = dal;
            _unitOfWork = unitOfWork;
            _serviceProvider = serviceProvider;
            _storeServices = storeServices;
            _billDeliveryItemServices = billDeliveryItemServices;
            _orderLogServices = orderLogServices;
            _settingServices = settingServices;
            _redisOperationRepository = redisOperationRepository;
            _coreCmsDistributionRepository = coreCmsDistributionRepository;
            _user = user;
 
        }
 
 
        /// <summary>
        /// 批量发货,可以支持多个订单合并发货,单个订单拆分发货等。
        /// </summary>
        /// <param name="orderId">英文逗号分隔的订单号</param>
        /// <param name="logiCode">物流公司编码</param>
        /// <param name="logiNo">物流单号</param>
        /// <param name="items">发货明细</param>
        /// <param name="storeId">店铺收货地址</param>
        /// <param name="shipName">收货人姓名</param>
        /// <param name="shipMobile">收货人电话</param>
        /// <param name="shipAreaId">省市区id</param>
        /// <param name="shipAddress">收货地址</param>
        /// <param name="memo">发货描述</param>
        /// <param name="deliveryCompanyId">第三方对接物流编码</param>
        /// <param name="sendDistributionID">送货供应商ID</param>
        /// <returns></returns>
        public async Task<WebApiCallBack> BatchShip(string[] orderId, string logiCode, string logiNo, Dictionary<int, int> items, int storeId = 0, string shipName = "", string shipMobile = "", int shipAreaId = 0, string shipAddress = "", string memo = "", string deliveryCompanyId = "", int? sendDistributionID = null)
        {
            using var container = _serviceProvider.CreateScope();
            var jm = new WebApiCallBack();
            if(sendDistributionID > 0)
            {
                jm.msg = "经销商发货不支持批量发货";
                return jm;
            }
            var orderService = container.ServiceProvider.GetService<ICoreCmsOrderServices>();
            var stockServices = container.ServiceProvider.GetService<ICoreCmsStockServices>();
            //获取订单详情
            var dInfoResult = await orderService.GetOrderShipInfo(orderId);
            if (!dInfoResult.status)
            {
                return dInfoResult;
            }
            var dInfo = dInfoResult.data as AdminOrderShipResult;
            var orders = dInfo.orders;
 
            //校验门店自提和普通订单收货地址是否填写
            if (storeId != 0)
            {
                var storeModel = await _storeServices.QueryByIdAsync(storeId);
                if (storeModel == null)
                {
                    jm.msg = GlobalErrorCodeVars.Code10000;
                    jm.data = 1000;
                    jm.code = 1000;
                    return jm;
                }
                shipName = storeModel.storeName;
                shipMobile = storeModel.mobile;
                shipAreaId = storeModel.areaId;
                shipAddress = storeModel.address;
            }
            if (string.IsNullOrEmpty(shipName) || string.IsNullOrEmpty(shipMobile) || string.IsNullOrEmpty(shipAddress) || shipAreaId == 0)
            {
                jm.msg = "收货地址信息不全";
                jm.otherData = new
                {
                    shipName,
                    shipMobile,
                    shipAddress,
                    shipAreaId
                };
                return jm;
            }
            var billDelivery = new CoreCmsBillDelivery();
            billDelivery.orderId = string.Join(",", orderId);
            billDelivery.deliveryId = CommonHelper.GetSerialNumberType((int)GlobalEnumVars.SerialNumberType.发货单编号);
            billDelivery.logiCode = logiCode;
            billDelivery.logiNo = logiNo;
            billDelivery.shipAreaId = shipAreaId;
            billDelivery.shipAddress = shipAddress;
            billDelivery.shipName = shipName;
            billDelivery.shipMobile = shipMobile;
            billDelivery.status = (int)GlobalEnumVars.BillDeliveryStatus.Already;
            billDelivery.memo = memo;
            billDelivery.createTime = DateTime.Now;
            billDelivery.thirdPartylogiCode = deliveryCompanyId;
 
            //设置发货明细
            var bdRel = new List<CoreCmsBillDeliveryItem>();
 
            //校验发货内容
            var tNum = 0;
            foreach (var item in items)
            {
                var orderItem = dInfo.items.Find(p => p.productId == item.Key);
                if (orderItem == null)
                {
                    //发货的商品不在发货明细里,肯定有问题
                    jm.msg = GlobalErrorCodeVars.Code10000;
                    return jm;
                }
                //判断总发货数量
                tNum = tNum + item.Value;
 
                if ((orderItem.nums - orderItem.sendNums - (orderItem.reshipNums - orderItem.reshipedNums)) < item.Value)
                {
                    jm.msg = orderItem.name + "发超了";
                    return jm;
                }
 
                //构建发货单明细
                var bdItem = new CoreCmsBillDeliveryItem();
                bdItem.deliveryId = billDelivery.deliveryId;
                bdItem.productId = orderItem.productId;
                bdItem.goodsId = orderItem.goodsId;
                bdItem.bn = orderItem.bn;
                bdItem.sn = orderItem.sn;
                bdItem.weight = orderItem.weight;
                bdItem.name = orderItem.name;
                bdItem.addon = !string.IsNullOrEmpty(orderItem.addon) ? orderItem.addon : "";
                bdItem.nums = item.Value;
                bdRel.Add(bdItem);
            }
            if (tNum < 1)
            {
                jm.msg = "请至少发一件商品!";
                return jm;
            }
            //事务处理开始
 
 
            //插入发货单主体表
            await _dal.InsertAsync(billDelivery);
 
            //插入发货单明细表
            await _billDeliveryItemServices.InsertAsync(bdRel);
 
            //订单更新发货状态,发送各种消息
            foreach (var order in orders)
            {
                await OrderShip(order, items, billDelivery, storeId);
            }
 
            var stock = new CoreCmsStock
            {
                manager = 0,
                id = billDelivery.deliveryId,
                createTime = DateTime.Now,
                type = (int)GlobalEnumVars.StockType.DeliverGoods,
                memo = "订单发货操作,发货单号:" + billDelivery.deliveryId
            };
 
            await stockServices.InsertAsync(stock);
 
            jm.status = true;
            jm.msg = "发货成功";
 
 
            return jm;
        }
 
 
        /// <summary>
        ///     发货,单个订单发货
        /// </summary>
        /// <param name="orderId">英文逗号分隔的订单号</param>
        /// <param name="logiCode">物流公司编码</param>
        /// <param name="logiNo">物流单号</param>
        /// <param name="items">发货明细</param>
        /// <param name="storeId">店铺收货地址</param>
        /// <param name="shipName">收货人姓名</param>
        /// <param name="shipMobile">收货人电话</param>
        /// <param name="shipAreaId">省市区id</param>
        /// <param name="shipAddress">收货地址</param>
        /// <param name="memo">发货描述</param>
        /// <param name="deliveryCompanyId">第三方对接物流编码</param>
        /// <param name="sendDistributionID">经销商送货ID</param>
        /// <returns></returns>
        public async Task<WebApiCallBack> Ship(string orderId, string logiCode, string logiNo, Dictionary<int, int> items, int storeId = 0, string shipName = "", string shipMobile = "", int shipAreaId = 0, string shipAddress = "", string memo = "", string deliveryCompanyId = "",int? sendDistributionID=null)
        {
            using var container = _serviceProvider.CreateScope();
            var jm = new WebApiCallBack();
            
            var orderService = container.ServiceProvider.GetService<ICoreCmsOrderServices>();
            //获取订单详情
            var dInfoResult = await orderService.GetOrderShipInfo(orderId);
            if (!dInfoResult.status)
            {
                return dInfoResult;
            }
            var dInfo = dInfoResult.data as AdminOrderShipOneResult;
            var orderInfo = dInfo.orderInfo;
 
            //校验门店自提和普通订单收货地址是否填写
            if (storeId != 0)
            {
                var storeModel = await _storeServices.QueryByIdAsync(storeId);
                if (storeModel == null)
                {
                    jm.msg = GlobalErrorCodeVars.Code10000;
                    jm.data = 1000;
                    jm.code = 1000;
                    return jm;
                }
                shipName = storeModel.storeName;
                shipMobile = storeModel.mobile;
                shipAreaId = storeModel.areaId;
                shipAddress = storeModel.address;
            }
            if (string.IsNullOrEmpty(shipName) || string.IsNullOrEmpty(shipMobile) || string.IsNullOrEmpty(shipAddress) || shipAreaId == 0)
            {
                jm.msg = "收货地址信息不全";
                jm.otherData = new
                {
                    shipName,
                    shipMobile,
                    shipAddress,
                    shipAreaId
                };
                return jm;
            }
            var billDelivery = new CoreCmsBillDelivery();
            billDelivery.orderId = string.Join(",", orderId);
            billDelivery.deliveryId = CommonHelper.GetSerialNumberType((int)GlobalEnumVars.SerialNumberType.发货单编号);
            billDelivery.logiCode = logiCode;
            billDelivery.logiNo = logiNo;
            billDelivery.shipAreaId = shipAreaId;
            billDelivery.shipAddress = shipAddress;
            billDelivery.shipName = shipName;
            billDelivery.shipMobile = shipMobile;
            billDelivery.status = (int)GlobalEnumVars.BillDeliveryStatus.Already;
            billDelivery.memo = memo;
            billDelivery.createTime = DateTime.Now;
            billDelivery.thirdPartylogiCode = deliveryCompanyId;
         
            if(sendDistributionID>0)
            {
              var a=  _coreCmsDistributionRepository.QueryById(sendDistributionID);
                if(a==null)
                {
                    jm.msg = "经销商不存在";
                    return jm;
                }
                billDelivery.sendDistributionUserID= a.userId;  
                billDelivery.sendDistributionID = sendDistributionID;
                billDelivery.sendDistributionAccept = sendDistributionAcceptType.NoAccted;
                var mQ = new DistributionSendOder
                {
                   
                    sendDistributionAccept = sendDistributionAcceptType.NoAccted,
                    distributionID = (int)sendDistributionID,
                    sendMangerID = _user.ID,
                    deliveryID = billDelivery.deliveryId,
                     sendTime = DateTime.Now, 
                      userID= a.userId
 
 
                };
                //记录操作记录
                await _redisOperationRepository.ListLeftPushAsync(RedisMessageQueueKey.AddDistributionSendOderMQ, JsonConvert.SerializeObject(mQ));
            }
            //设置发货明细
            var bdRel = new List<CoreCmsBillDeliveryItem>();
 
            //校验发货内容
            var tNum = 0;
            foreach (var item in items)
            {
                var orderItem = dInfo.items.Find(p => p.productId == item.Key);
                if (orderItem == null)
                {
                    //发货的商品不在发货明细里,肯定有问题
                    jm.msg = GlobalErrorCodeVars.Code10000;
                    return jm;
                }
                //判断总发货数量
                tNum = tNum + item.Value;
 
                if ((orderItem.nums - orderItem.sendNums - (orderItem.reshipNums - orderItem.reshipedNums)) < item.Value)
                {
                    jm.msg = orderItem.name + "发超了";
                    return jm;
                }
 
                //构建发货单明细
                var bdItem = new CoreCmsBillDeliveryItem();
                bdItem.deliveryId = billDelivery.deliveryId;
                bdItem.orderId = orderItem.orderId;
                bdItem.productId = orderItem.productId;
                bdItem.goodsId = orderItem.goodsId;
                bdItem.bn = orderItem.bn;
                bdItem.sn = orderItem.sn;
                bdItem.weight = orderItem.weight;
                bdItem.name = orderItem.name;
                bdItem.addon = !string.IsNullOrEmpty(orderItem.addon) ? orderItem.addon : "";
                bdItem.nums = orderItem.nums;
                bdRel.Add(bdItem);
            }
            if (tNum < 1)
            {
                jm.msg = "请至少发生一件商品!";
                return jm;
            }
            //事务处理开始
 
 
            //插入发货单主体表
         await _dal.InsertAsync(billDelivery);
           
       
            
 
            //插入发货单明细表
            await _billDeliveryItemServices.InsertAsync(bdRel);
 
            //订单更新发货状态,发送各种消息
            await OrderShip(orderInfo, items, billDelivery, storeId);
 
            jm.status = true;
            jm.msg = "发货成功";
 
            return jm;
        }
 
 
 
        /// <summary>
        /// 给订单发货
        /// </summary>
        /// <param name="orderInfo">订单信息</param>
        /// <param name="items">总的发货包裹内容</param>
        /// <param name="deliveryInfo">发货单信息</param>
        /// <param name="storeId">门店自提还是普通订单,0是普通订单,其他是门店自提</param>
        private async Task<bool> OrderShip(CoreCmsOrder orderInfo, Dictionary<int, int> items, CoreCmsBillDelivery deliveryInfo, int storeId = 0)
        {
            using var container = _serviceProvider.CreateScope();
            var orderService = container.ServiceProvider.GetService<ICoreCmsOrderServices>();
            var billLadingService = container.ServiceProvider.GetService<ICoreCmsBillLadingServices>();
            var messageCenterServices = container.ServiceProvider.GetService<ICoreCmsMessageCenterServices>();
            var logisticsServices = container.ServiceProvider.GetService<ICoreCmsLogisticsServices>();
            var stockLogServices = container.ServiceProvider.GetService<ICoreCmsStockLogServices>();
            var stockServices = container.ServiceProvider.GetService<ICoreCmsStockServices>();
 
            var stockLogs = new List<CoreCmsStockLog>();
 
            var itemList = new Dictionary<int, int>();
            foreach (var item in orderInfo.items)
            {
                if (items.ContainsKey(item.productId))
                {
                    var maxNum = item.nums - item.reshipNums - item.sendNums;
                    if (maxNum > 0) //如果此条订单明细需要发货的话
                    {
                        var sendNum = maxNum;
                        if (items[item.productId] > maxNum)
                        {
                            //足够发此条记录的话
                            itemList.Add(item.productId, maxNum);
                            items[item.productId] = items[item.productId] - maxNum;
                        }
                        else
                        {
                            //此条订单都发不满的情况下
                            itemList.Add(item.productId, items[item.productId]);
                            sendNum = items[item.productId];
                        }
 
                        var sLog = new CoreCmsStockLog
                        {
                            stockId = deliveryInfo.deliveryId,
                            productId = item.productId,
                            goodsId = item.goodsId,
                            nums = -sendNum,
                            sn = item.sn,
                            bn = item.bn,
                            goodsName = item.name,
                            spesDesc = item.addon,
                            createTime = DateTime.Now
                        };
                        stockLogs.Add(sLog);
                    }
                }
            }
            //如果有发货信息,就去给订单更新发货状态
            if (itemList.Keys.Count <= 0)
            {
                return false;
            }
            var res = await orderService.EditShipStatus(orderInfo.orderId, itemList);
 
            //如果是门店自提,生成提货单
            if (storeId != 0)
            {
                await billLadingService.AddData(orderInfo.orderId, storeId, orderInfo.shipName, orderInfo.shipMobile);
            }
 
            if (res.status == true)
            {
                //添加操作日志
                var log = new CoreCmsOrderLog();
                log.orderId = orderInfo.orderId;
                log.userId = orderInfo.userId;
                log.type = (int)GlobalEnumVars.OrderLogTypes.LOG_TYPE_SHIP;
                log.msg = "订单发货操作,发货单号:" + deliveryInfo.deliveryId;
                log.data = JsonConvert.SerializeObject(deliveryInfo);
                log.createTime = DateTime.Now;
                await _orderLogServices.InsertAsync(log);
 
                var logistics = await logisticsServices.QueryByClauseAsync(p => p.logiCode == deliveryInfo.logiCode);
                deliveryInfo.logiName = logistics != null ? logistics.logiName : deliveryInfo.logiCode;
                //添加库存出库日志
 
                if (stockLogs.Any())
                {
                    //var stock = new CoreCmsStock
                    //{
                    //    manager = 0,
                    //    id = deliveryInfo.deliveryId,
                    //    createTime = DateTime.Now,
                    //    type = (int)GlobalEnumVars.StockType.DeliverGoods,
                    //    memo = log.msg
                    //};
                    //await stockServices.InsertAsync(stock);
                    await stockLogServices.InsertAsync(stockLogs);
                }
 
                //发送消息
                await messageCenterServices.SendMessage(orderInfo.userId, GlobalEnumVars.PlatformMessageTypes.DeliveryNotice.ToString(), JObject.FromObject(deliveryInfo));
            }
            return true;
        }
 
 
        /// <summary>
        /// 物流信息查询根据快递编码和单号查询(快递100)未使用
        /// </summary>
        /// <param name="code">查询的快递公司的编码, 一律用小写字母(如:yuantong)</param>
        /// <param name="no">查询的快递单号, 单号的最大长度是32个字符</param>
        /// <returns></returns>
        public async Task<WebApiCallBack> GetLogistic(string code, string no)
        {
            var jm = new WebApiCallBack();
 
            //快递100分配给贵司的的公司编号, 请在企业管理后台查看
            var allConfigs = await _settingServices.GetConfigDictionaries();
            var customer = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.Kuaidi100Customer);
            //签名, 用于验证身份, 按param + key + customer 的顺序进行MD5加密(注意加密后字符串一定要转大写), 不需要加上“+”号, 如{“com”: “yuantong”, “num”: “500306190180”, “from”: “广东省深圳市”, “to”: “北京市朝阳区”}xxxxxxxxxxxxyyyyyyyyyyy yyyyyyyyyyyyyyyyyyyyy
            var key = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.Kuaidi100Key);
 
            if (string.IsNullOrEmpty(customer) || string.IsNullOrEmpty(key))
            {
                jm.msg = "快递查询接口公司编码及授权key获取失败";
            }
 
            var param = new KuaiDi100ApiPostParam();
            param.com = code.ToLowerInvariant();
            param.num = no;
            param.phone = "";
            param.from = "";
            param.to = "";
            param.resultv2 = 1;
            var jsonParamData = JsonConvert.SerializeObject(param);
 
            //签名加密
            var str = jsonParamData + key + customer;
            var signStr = CommonHelper.Md5For32(str).ToUpper();
            //实时查询请求地址
            var postUrl = "http://poll.kuaidi100.com/poll/query.do";
 
            var postData = new
            {
                customer,
                param = jsonParamData,
                sign = signStr
            };
 
            var result = await postUrl.PostUrlEncodedAsync(postData).ReceiveJson<KuaiDi100ApiPostResult>();
            if (result.status == "200")
            {
                jm.status = true;
                jm.data = result;
            }
            else
            {
                jm.status = false;
                jm.msg = !string.IsNullOrEmpty(result.message) ? result.message : "暂无消息";
            }
 
            return jm;
        }
 
 
        /// <summary>
        /// 发货单统计7天统计
        /// </summary>
        /// <returns></returns>
        public async Task<List<StatisticsOut>> Statistics()
        {
            return await _dal.Statistics();
 
        }
 
    }
}