zhaojs
2023-09-15 fc13938ff90213060532d99a600dea4a84456885
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
using AlibabaSDK.Api;
using AlibabaSDK.Domain;
using AlibabaSDK.Request;
using CommonUtil;
using DbAccess;
using Operater.DAL;
using Operater.DbModel;
using Operater.DTO.TopMessage;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using YouZanSDKStandard.Api;
using YouZanSDKStandard.Api.Request;
 
namespace TradeInterflow.Handle
{
    public class AliTradeHandle
    {
        private static string _logName = "1688订单处理error";
        public static ISysSqlHelper dbMain = SysSqlHelper.CreateSqlHelper("db_main");
        public static void HandleExecute(string message)
        {
            try
            {
                BaseAliMessage aliMsg = JSONUtil.JsonToObject<BaseAliMessage>(message);
                switch (aliMsg.type)
                {
                    case "ORDER_BUYER_VIEW_BUYER_MAKE":
                        Console.WriteLine("1688创建订单");
                        CreateTradeMsg(aliMsg.data.ToString());
                        break;
                    case "ORDER_BATCH_PAY":
                        Console.WriteLine("1688订单批量支付状态同步消息");
                        BathPayMsg(aliMsg.data.ToString());
                        break;
                    case "ORDER_BUYER_VIEW_ORDER_PAY":
                        Console.WriteLine("1688交易付款(买家视角)");
                        OrderPay(aliMsg.data.ToString());
                        break;
                    case "ORDER_BUYER_VIEW_ANNOUNCE_SENDGOODS":
                        Console.WriteLine("1688订单发货(买家视角");
                        SendGoods(aliMsg.data.ToString());
                        break;
                    case "ORDER_BUYER_VIEW_ORDER_SELLER_CLOSE":
                        Console.WriteLine("1688卖家关闭订单(买家视角)");
                        OrderSaleCnacle(aliMsg.data.ToString());
                        break;
                    case "ORDER_BUYER_VIEW_ORDER_PRICE_MODIFY":
                        Console.WriteLine("1688修改订单价格(买家视角)");
                        PriceModify(aliMsg.data.ToString());
                        break;
                    case "ORDER_BUYER_VIEW_ORDER_SELLER_MODIFY_ADRESS":
                        Console.WriteLine("商家修改订单地址(买家视角)");
                        AddressModify(aliMsg.data.ToString());
                        break;
                    case "LOGISTICS_MAIL_NO_CHANGE":
                        Console.WriteLine("物流单号修改消息");
                        LogisticsChange(aliMsg.data.ToString());
                        break;
                    case "ORDER_BUYER_VIEW_PART_PART_SENDGOODS":
                        Console.WriteLine("1688订单部分发货(买家视角");
                        SendGoods(aliMsg.data.ToString());
                        break;
                    case "LOGISTICS_BUYER_VIEW_TRACE":
                        Console.WriteLine("1688物流单状态变更(买家视角)");
                        LogisticsSend(aliMsg.data.ToString());
                        break;
                    case "ORDER_BUYER_VIEW_ORDER_SUCCESS":
                        Console.WriteLine("1688交易成功(买家视角)");
                        TradeStatusChange(aliMsg.data.ToString(), "交易成功");
                        break;
                    case "ORDER_BUYER_VIEW_ORDER_BUYER_CLOSE":
                        Console.WriteLine("1688买家关闭订单(买家视角)");
                        TradeStatusChange(aliMsg.data.ToString(), "交易关闭");
                        break;
                    case "ORDER_BUYER_VIEW_ORDER_COMFIRM_RECEIVEGOODS":
                        Console.WriteLine("1688订单确认收货(买家视角)");
                        TradeStatusChange(aliMsg.data.ToString(), "确认收货");
                        break;
                    case "ORDER_BUYER_VIEW_ORDER_BUYER_REFUND_IN_SALES":
                        Console.WriteLine("1688商家退款消息");
                        TradeRefundStatus(aliMsg.data.ToString());
                        break;
                }
 
            }
            catch (Exception e)
            {
                LogUtil.Info("主线程错误:" + e.ToString(), _logName);
                Console.WriteLine("主线程错误:" + e.ToString());
            }
        }
 
        #region 1688退款状态
        private static void TradeRefundStatus(string message)
        {
            try
            {
                TradeRefundStatusMsg msg = JSONUtil.JsonToObject<TradeRefundStatusMsg>(message);
                YzRefund yzRefund = new YzRefund()
                {
                    AliRefundStatus = msg.currentStatus,
                    AliRefundAction = msg.refundAction
                };
                if (msg.refundAction == "SELLER_AGREE_REFUND")
                {
                    yzRefund.RelationYzRefund = 2;
                    new YzRefundDAL().Update(yzRefund, i => new { i.AliRefundStatus, i.AliRefundAction, i.RelationYzRefund }, t => t.AliRefundId == msg.refundId);
                }
                else
                {
                    new YzRefundDAL().Update(yzRefund, i => new { i.AliRefundStatus, i.AliRefundAction }, t => t.AliRefundId == msg.refundId);
                }
 
            }
            catch (Exception e)
            {
                LogUtil.Info("主线程错误:" + e.ToString(), "1688退款状态");
                Console.WriteLine("主线程错误:" + e.ToString());
            }
        }
 
        #endregion
 
        #region 1688订单状态改变
        private static void TradeStatusChange(string message, string statusStr)
        {
            try
            {
                TradeSuccessMsg successMsg = JSONUtil.JsonToObject<TradeSuccessMsg>(message);
                ChangeTradeStatus(successMsg.orderId, successMsg.currentStatus, statusStr);
            }
            catch (Exception e)
            {
                LogUtil.Info("主线程错误:" + e.ToString(), "1688订单状态改变");
                Console.WriteLine("主线程错误:" + e.ToString());
            }
        }
 
        #endregion
 
        #region 物流单状态变更(买家视角)
        private static void LogisticsSend(string message)
        {
            try
            {
                LogisticsSendMsg logisticsSendMsg = JSONUtil.JsonToObject<LogisticsSendMsg>(message);
                foreach (var trd in logisticsSendMsg.OrderLogisticsTracingModel.orderLogsItems)
                {
                    AliLogistics aliLogistics = new AliLogistics()
                    {
                        AliOid = trd.orderEntryId.ToString(),
                        AliTid = trd.orderId.ToString(),
                        CreateTime = DateTime.Now,
                        LogisticsId = logisticsSendMsg.OrderLogisticsTracingModel.logisticsId,
                        CpCode = logisticsSendMsg.OrderLogisticsTracingModel.cpCode,
                        MailNo = logisticsSendMsg.OrderLogisticsTracingModel.mailNo,
                        StatusChanged = logisticsSendMsg.OrderLogisticsTracingModel.statusChanged
                    };
                    var has = new AliLogisticsDAL().GetById(trd.orderEntryId.ToString());
                    if (has.IsNull() || has.AliOid.IsNullOrEmpty())
                    {//新增
                        new AliLogisticsDAL().Insert(aliLogistics);
                    }
                    else
                    {//修改
                        new AliLogisticsDAL().Update(aliLogistics, i => new { i.LogisticsId, i.CpCode, i.MailNo, i.StatusChanged }, t => t.AliOid == trd.orderEntryId.ToString());
                    }
                    if (logisticsSendMsg.OrderLogisticsTracingModel.statusChanged == "CONSIGN")
                    {//发货消息,修改有赞子订单发货状态
                        //var orderRelate = new TradeCreateRelationDAL().ListGet(t => t.AliOid == trd.orderEntryId.ToString());
                        //if (orderRelate.IsNull() || orderRelate.Count == 0)
                        //{
                        //    LogUtil.Info($"1688子订单:{trd.orderEntryId.ToString()},未找到对应有赞子订单信息", "物流单状态变更");
                        //    continue;
                        //}
                        YzOrderinfo yzOrderinfo = new YzOrderinfo()
                        {
                            AliGoodsStatus = 1
                        };
                        new YzOrderinfoDAL().Update(yzOrderinfo, i => new { i.AliGoodsStatus }, t => t.AliOid == trd.orderEntryId.ToString());
                    }
                }
            }
            catch (Exception e)
            {
                LogUtil.Info("主线程错误:" + e.ToString(), "物流单状态变更");
                Console.WriteLine("物流单状态变更主线程错误:" + e.ToString());
            }
        }
 
        #endregion
 
        #region 1688物流单号修改
        private static void LogisticsChange(string message)
        {
            try
            {
                LogisticsChangeMsg msg = JSONUtil.JsonToObject<LogisticsChangeMsg>(message);
                //获取订单关系
                string reMsg = GetYzTid(msg.MailNoChangeModel.orderLogsItems[0].orderId);
                string Msg = $"1688物流单号修改-1688订单{msg.MailNoChangeModel.orderLogsItems[0].orderId},对应有赞订单:{reMsg}";
                Msg += $",之前的运单号:{msg.MailNoChangeModel.oldMailNo},更改后的运单号:{msg.MailNoChangeModel.newMailNo}";
                MessageNotify messageNotify = new MessageNotify()
                {
                    Msg = Msg,
                    Mark = msg.MailNoChangeModel.orderLogsItems[0].orderId,
                    MsgChannel = "1688"
                };
                CommonHandle.RecorderMessage(messageNotify);
            }
            catch (Exception e)
            {
                LogUtil.Info("主线程错误:" + e.ToString(), "1688物流单号修改");
                Console.WriteLine("1688物流单号修改主线程错误:" + e.ToString());
            }
        }
 
        #endregion
 
        #region 1688商家修改订单地址
        private static void AddressModify(string message)
        {
            try
            {
                AddressModifyMsg ModifyMsg = JSONUtil.JsonToObject<AddressModifyMsg>(message);
                //获取订单关系
                string msg = GetYzTid(ModifyMsg.orderId);
 
                MessageNotify messageNotify = new MessageNotify()
                {
                    Msg = $"1688商家修改订单地址-1688订单{ModifyMsg.orderId},对应有赞订单:{msg}",
                    Mark = ModifyMsg.orderId,
                    MsgChannel = "1688"
                };
                CommonHandle.RecorderMessage(messageNotify);
                //从接口获取订单详情
                AlibabaTradeDetailDomain topTradeInfo = CommonHandle.GetAliTradeDetail(ModifyMsg.orderId);
                if (topTradeInfo.IsNull())
                {
                    return;
                }
                AliTradeinfo aliTradeinfo = new AliTradeinfo()
                {
                    Province = topTradeInfo.nativeLogistics.province,
                    City = topTradeInfo.nativeLogistics.city,
                    Area = topTradeInfo.nativeLogistics.area,
                    Address = topTradeInfo.nativeLogistics.address,
                    Mobile = topTradeInfo.nativeLogistics.mobile,
                    Town = topTradeInfo.nativeLogistics.town
                };
                //修改1688订单收货人信息
                new AliTradeinfoDAL().Update(aliTradeinfo, i => new { i.Province, i.City, i.Area, i.Address, i.Mobile, i.Town }, t => t.Tid == ModifyMsg.orderId);
            }
            catch (Exception e)
            {
                LogUtil.Info("主线程错误:" + e.ToString(), "1688修改订单价格");
                Console.WriteLine("1688修改订单价格主线程错误:" + e.ToString());
            }
        }
        #endregion
 
        #region 1688修改订单价格
        private static void PriceModify(string message)
        {
            try
            {
                PriceModifyMsg priceModifyMsg = JSONUtil.JsonToObject<PriceModifyMsg>(message);
                //获取订单关系
                string msg = GetYzTid(priceModifyMsg.orderId);
                MessageNotify messageNotify = new MessageNotify()
                {
                    Msg = $"1688商家修改订单价格-1688订单{priceModifyMsg.orderId},对应有赞订单:{msg}",
                    Mark = priceModifyMsg.orderId,
                    MsgChannel = "1688"
                };
                CommonHandle.RecorderMessage(messageNotify);
                //先删除原1688订单数据
                new AliTradeinfoDAL().Delete(priceModifyMsg.orderId);
                new AliOrderinfoDAL().Delete(t => t.Tid == priceModifyMsg.orderId);
                //重新获取新数据
                TrdeInfoHandle(priceModifyMsg.orderId);
            }
            catch (Exception e)
            {
                LogUtil.Info("主线程错误:" + e.ToString(), "1688修改订单价格");
                Console.WriteLine("1688修改订单价格主线程错误:" + e.ToString());
            }
        }
 
        #endregion
 
 
        #region 1688卖家关闭订单
        private static void OrderSaleCnacle(string message)
        {
            try
            {
                OrderSaleCnacleMsg orderSaleCnacleMsg = JSONUtil.JsonToObject<OrderSaleCnacleMsg>(message);
                ChangeTradeStatus(orderSaleCnacleMsg.orderId, orderSaleCnacleMsg.currentStatus, "交易取消");
                //获取订单关系
              //  string msg = GetYzTid(orderSaleCnacleMsg.orderId);
                //MessageNotify messageNotify = new MessageNotify()
                //{
                //    Msg = $"发货失败-1688订单{orderSaleCnacleMsg.orderId}-商家取消了交易,对应有赞订单:【{msg}】",
                //    Mark = orderSaleCnacleMsg.orderId,
                //    MsgChannel = "1688"
                //};
                //CommonHandle.RecorderMessage(messageNotify);
            }
            catch (Exception e)
            {
                LogUtil.Info("主线程错误:" + e.ToString(), "1688卖家关闭订单");
                Console.WriteLine("主线程错误:" + e.ToString());
            }
        }
 
        #endregion
 
        #region 修改订单状态
        private static void ChangeTradeStatus(string orderId, string currentStatus, string statusStr)
        {
            try
            {
                if (TrdeInfoHandle(orderId))
                {
                    AliTradeinfo aliTradeinfo = new AliTradeinfo()
                    {
                        TradeStatus = currentStatus
                    };
                    new AliTradeinfoDAL().UseTran(() =>
                    {//修改主子订单状态
                        new AliTradeinfoDAL().Update(aliTradeinfo, i => new { i.TradeStatus }, t => t.Tid == orderId);
                        AliOrderinfo aliOrderinfo = new AliOrderinfo()
                        {
                            OrderStatus = currentStatus,
                            StatusStr = statusStr
                        };
                        new AliOrderinfoDAL().Update(aliOrderinfo, i => new { i.OrderStatus, i.StatusStr }, t => t.Tid == orderId);
                    });
                    //修改退款表里1688订单状态
                    YzRefund yzRefund = new YzRefund()
                    {
                        AliTradeStatus = currentStatus
                    };
                    new YzRefundDAL().Update(yzRefund, i => new { i.AliTradeStatus }, t => t.AliTid == orderId);
                }
            }
            catch (Exception e)
            {
                LogUtil.Info("主线程错误:" + e.ToString(), "修改订单状态");
                Console.WriteLine("主线程错误:" + e.ToString());
            }
        }
 
        #endregion
 
 
        #region 1688订单发货
        private static void SendGoods(string message)
        {
            try
            {
                SendGoodsMsg sendGoodsMsg = JSONUtil.JsonToObject<SendGoodsMsg>(message);
                ChangeTradeStatus(sendGoodsMsg.orderId, sendGoodsMsg.currentStatus, "等待买家收货");
                //修改有赞子订单1688订单发货状态
                YzOrderinfo yzOrderinfo = new YzOrderinfo()
                {
                    AliGoodsStatus = 1
                };
                new YzOrderinfoDAL().Update(yzOrderinfo, i => new { i.AliGoodsStatus }, t => t.AliTid == sendGoodsMsg.orderId);
                #region 可调用AliTradeFlowService.AliTradeSendGoods服务
                ////有赞订单发货
                ////获取订单关系
                //var tradeRelat = new TradeRelationDAL().GetById(sendGoodsMsg.orderId);
                //if (tradeRelat.IsNull() || tradeRelat.AliTid.IsNullOrEmpty())
                //{//异常
                //    MessageNotify messageNotify = new MessageNotify()
                //    {
                //        Msg = $"发货失败-订单{sendGoodsMsg.orderId}没有找到对应有赞的订单关系",
                //        Mark = sendGoodsMsg.orderId,
                //        MsgChannel = "1688"
                //    };
                //    CommonHandle.RecorderMessage(messageNotify);
                //    return;
                //}
                ////获取关联的子订单
                //var orderList = new OrderRelationDAL().ListGet(t => t.AliTid == sendGoodsMsg.orderId);
                //if (orderList.IsNull() || orderList.Count == 0)
                //{
                //    MessageNotify messageNotify = new MessageNotify()
                //    {
                //        Msg = $"发货失败-订单{sendGoodsMsg.orderId}没有找到子订单信息",
                //        Mark = sendGoodsMsg.orderId,
                //        MsgChannel = "1688"
                //    };
                //    CommonHandle.RecorderMessage(messageNotify);
                //    return;
                //}
                ////获取1688物流信息
                //AlibabaGetLogisticsInfosRequest alibabaGetLogisticsInfosRequest = new AlibabaGetLogisticsInfosRequest()
                //{
                //    orderId = long.Parse(sendGoodsMsg.orderId),
                //    webSite = "1688",
                //    fields = "company,name,sender,receiver,sendgood"
                //};
                //var alibabaGetLogisticsInfosRes = AliabaClient.Execute(alibabaGetLogisticsInfosRequest, GetAliToken());
                //if (alibabaGetLogisticsInfosRes.IsNull() || !alibabaGetLogisticsInfosRes.Succes || alibabaGetLogisticsInfosRes.result.IsNull())
                //{//获取1688物流信息失败
                //    MessageNotify messageNotify = new MessageNotify()
                //    {
                //        Msg = $"发货失败-订单{sendGoodsMsg.orderId}获取1688物流信息失败:" + (alibabaGetLogisticsInfosRes.IsNull() ? "null" : alibabaGetLogisticsInfosRes.ErrorMsg),
                //        Mark = sendGoodsMsg.orderId,
                //        MsgChannel = "1688"
                //    };
                //    CommonHandle.RecorderMessage(messageNotify);
                //    return;
                //}
                ////获取1688与有赞的快递公司关联关系
                //var expressId = new AliExpressDAL().GetById(alibabaGetLogisticsInfosRes.result.logisticsCompanyId);
                //string outStype = expressId.YzId.ToString();
 
                ////有赞发货
 
                ////根据1688子订单获取有赞子订单数据
                //var yzOids = orderList.Where(t => alibabaGetLogisticsInfosRes.result.orderEntryIds.Contains(t.AlisOid)).ToList();
                //if (yzOids.IsNull() || yzOids.Count == 0 || yzOids.Count != alibabaGetLogisticsInfosRes.result.orderEntryIds.Split(',').Length)
                //{
                //    MessageNotify messageNotify = new MessageNotify()
                //    {
                //        Msg = $"发货失败-1688订单{sendGoodsMsg.orderId}获取有赞子订单失败,1688子订单:" + alibabaGetLogisticsInfosRes.result.orderEntryIds,
                //        Mark = sendGoodsMsg.orderId,
                //        MsgChannel = "1688"
                //    };
                //    CommonHandle.RecorderMessage(messageNotify);
                //    return;
                //}
                //var oidArr = yzOids.Select(t => t.YzOid).ToArray();
                //string oids = string.Join(',', oidArr);
                //YouZanLogisticsConfirmRequest yzRequest = new YouZanLogisticsConfirmRequest()
                //{
                //    tid = tradeRelat.YzTid,
                //    out_stype = outStype,
                //    out_sid = alibabaGetLogisticsInfosRes.result.logisticsBillNo,
                //    is_no_express = 0,
                //    oids = oids
                //};
                //var yzResponse = YouZanClient.Execute(yzRequest, GetYzToken());
                //if (yzResponse.IsNull() || !yzResponse.success)
                //{
                //    MessageNotify messageNotify = new MessageNotify()
                //    {
                //        Msg = $"发货失败-1688订单{sendGoodsMsg.orderId}有赞发货失败:" + (yzResponse.IsNull() ? "null" : yzResponse.message),
                //        Mark = sendGoodsMsg.orderId,
                //        MsgChannel = "yz"
                //    };
                //    CommonHandle.RecorderMessage(messageNotify);
                //    return;
                //}
                //LogUtil.Info($"1688订单{sendGoodsMsg.orderId}发货成功:{JSONUtil.ObjectToJson(yzRequest)}", "发货日志");
                //Console.WriteLine("发货成功");
                #endregion
            }
            catch (Exception e)
            {
                LogUtil.Info("主线程错误:" + e.ToString(), "1688订单发货");
                Console.WriteLine("主线程错误:" + e.ToString());
            }
        }
 
        #endregion
 
        #region 1688交易付款(买家视角)
 
        private static void OrderPay(string message)
        {
            try
            {
                OrderPayMsg orderPayMsg = JSONUtil.JsonToObject<OrderPayMsg>(message);
                ChangeTradeStatus(orderPayMsg.orderId, orderPayMsg.currentStatus, "等待卖家发货");
            }
            catch (Exception e)
            {
                LogUtil.Info("主线程错误:" + e.ToString(), "1688交易付款");
                Console.WriteLine("主线程错误:" + e.ToString());
            }
        }
 
        #endregion
 
        #region 批量支付状态同步消息
        private static void BathPayMsg(string message)
        {
            try
            {
                BathPayMsg msg = JSONUtil.JsonToObject<BathPayMsg>(message);
                foreach (var item in msg.batchPay)
                {
 
                }
            }
            catch (Exception e)
            {
                LogUtil.Info("主线程错误:" + e.ToString(), "批量支付状态同步消息");
                Console.WriteLine("主线程错误:" + e.ToString());
            }
        }
        #endregion
 
        #region 创建订单消息
        public static void CreateTradeMsg(string msg)
        {
            try
            {
                AliTradeCreateMsg aliTradeCreateMsg = JSONUtil.JsonToObject<AliTradeCreateMsg>(msg);
                //先处理订单数据
                TrdeInfoHandle(aliTradeCreateMsg.orderId.ToString());
                //if (TrdeInfoHandle(aliTradeCreateMsg.orderId.ToString()))
                //{
                //    //从数据库获取子订单数据,补全子订单关系表
                //    var orderList = new AliOrderinfoDAL().ListGet(t => t.Tid == aliTradeCreateMsg.orderId.ToString());
                //    if (orderList.IsNull() || orderList.Count == 0)
                //    {
                //        return;
                //    }
                //    foreach (var ord in orderList)
                //    {//补全子订单关系表中的1688子订单号
                //        OrderRelation orderRelation = new OrderRelation()
                //        {
                //            AlisOid = ord.Oid
                //        };
                //        var orderRe = new OrderRelationDAL().ListGet(t => t.AliTid == ord.Tid && t.AliSpecid == ord.SpecId);
                //        new OrderRelationDAL().Update(orderRelation, i => new { i.AlisOid }, t => t.AliTid == ord.Tid && t.AliSpecid == ord.SpecId);
                //        //补全有赞子订单表里的1688对应子订单id
                //        YzOrderinfo yzOrderinfo = new YzOrderinfo()
                //        {
                //            AliOid = ord.Oid
                //        };
                //        new YzOrderinfoDAL().Update(yzOrderinfo, i => new { i.AliOid }, t => t.Oid == orderRe[0].YzOid);
                //    }
                //}
            }
            catch (Exception e)
            {
                LogUtil.Info("主线程错误:" + e.ToString(), "1688创建订单消息");
                Console.WriteLine("主线程错误:" + e.ToString());
            }
        }
        #endregion
 
 
        #region 处理订单表数据
        private static bool TrdeInfoHandle(string tid)
        {
            try
            {
                //先检查1688订单表数据是否存在
                var dbTradeInfo = new AliTradeinfoDAL().GetById(tid);
                if (!dbTradeInfo.IsNull() && !dbTradeInfo.Tid.IsNullOrEmpty())
                {//数据库中已存在数据
                    return true;
                }
                //从接口获取订单详情
                AlibabaTradeDetailDomain topTradeInfo = CommonHandle.GetAliTradeDetail(tid);
                if (topTradeInfo.IsNull())
                {
                    return false;
                }
                //订单数据入库
                AliTradeinfo aliTradeinfo = new AliTradeinfo()
                {
                    Tid = topTradeInfo.baseInfo.id.ToString(),
                    PayTime = TimeSpanChange(topTradeInfo.baseInfo.payTime),
                    AllDeliveredTime = TimeSpanChange(topTradeInfo.baseInfo.allDeliveredTime),
                    Discount = topTradeInfo.baseInfo.discount / 100,
                    AlipayTradeId = topTradeInfo.baseInfo.alipayTradeId,
                    SumProductPayment = topTradeInfo.baseInfo.sumProductPayment,
                    BuyerFeedback = topTradeInfo.baseInfo.buyerFeedback,
                    BuyerLoginId = topTradeInfo.baseInfo.buyerLoginId,
                    ModifyTime = TimeSpanChange(topTradeInfo.baseInfo.modifyTime),
                    CloseReason = topTradeInfo.baseInfo.closeReason,
                    SellerAlipayId = topTradeInfo.baseInfo.sellerAlipayId,
                    CompleteTime = TimeSpanChange(topTradeInfo.baseInfo.completeTime),
                    SellerLoginid = topTradeInfo.baseInfo.sellerLoginId,
                    BuyerId = topTradeInfo.baseInfo.buyerID,
                    CloseOperateType = topTradeInfo.baseInfo.closeReason,
                    TotalAmount = topTradeInfo.baseInfo.totalAmount,
                    SellerId = topTradeInfo.baseInfo.sellerID,
                    ShippingFee = topTradeInfo.baseInfo.shippingFee,
                    BuyerUserid = topTradeInfo.baseInfo.buyerUserId.ToString(),
                    BuyerMemo = topTradeInfo.baseInfo.buyerMemo,
                    Refund = topTradeInfo.baseInfo.refund,
                    TradeStatus = topTradeInfo.baseInfo.status,
                    RefundPayment = topTradeInfo.baseInfo.refundPayment,
                    CouponFee = topTradeInfo.baseInfo.couponFee,
                    RefundStatus = topTradeInfo.baseInfo.refundStatus,
                    Remark = topTradeInfo.baseInfo.remark,
                    CloseRemark = topTradeInfo.baseInfo.closeRemark,
                    ReceivingTime = TimeSpanChange(topTradeInfo.baseInfo.receivingTime),
                    SellerUserid = topTradeInfo.baseInfo.sellerUserId.ToString(),
                    CreateTime = TimeSpanChange(topTradeInfo.baseInfo.createTime),
                    RefundId = topTradeInfo.baseInfo.refundId,
                    ContactPerson = topTradeInfo.nativeLogistics.contactPerson,
                    Province = topTradeInfo.nativeLogistics.province,
                    City = topTradeInfo.nativeLogistics.city,
                    Area = topTradeInfo.nativeLogistics.area,
                    Address = topTradeInfo.nativeLogistics.address,
                    Mobile = topTradeInfo.nativeLogistics.mobile,
                    Town = topTradeInfo.nativeLogistics.town
                };
                //组装子订单数据
                List<AliOrderinfo> aliOrderinfos = new List<AliOrderinfo>();
                foreach (var ord in topTradeInfo.productItems)
                {
                    AliOrderinfo order = new AliOrderinfo()
                    {
                        Oid = ord.subItemID.ToString(),
                        Tid = topTradeInfo.baseInfo.id.ToString(),
                        ItemAmount = ord.itemAmount,
                        Name = ord.name,
                        Price = ord.price,
                        ProductId = ord.productID.ToString(),
                        ProductImgUrl = ord.productImgUrl[0],
                        ProductSnapshotUrl = ord.productSnapshotUrl,
                        Quantity = ord.quantity,
                        Refund = ord.refund,
                        SkuId = ord.skuID.ToString(),
                        OrderStatus = ord.status,
                        Unit = ord.unit,
                        Weight = ord.weight,
                        WeightUnit = ord.weight_unit,
                        EntryDiscount = ord.entryDiscount / 100,
                        SpecId = ord.specId,
                        StatusStr = ord.statusStr,
                        RefundStatus = ord.refundStatus,
                        CloseReason = ord.closeReason,
                        LogisticsStatus = ord.logisticsStatus,
                        GmtCreate = TimeSpanChange(ord.gmtCreate),
                        GmtModified = TimeSpanChange(ord.gmtModified),
                        GmtCompleted = TimeSpanChange(ord.gmtCompleted),
                        RefundId = ord.refundId,
                        RefundIdForas = ord.refundIdForAs
                    };
                    order.Skuinfos = ord.skuInfos.IsNull() || ord.skuInfos.Count == 0 ? "" : JSONUtil.ObjectToJson(ord.skuInfos);
                    aliOrderinfos.Add(order);
                }
                //使用事务入库处理
                new AliTradeinfoDAL().UseTran(() =>
                {
                    new AliTradeinfoDAL().Insert(aliTradeinfo);
                    new AliOrderinfoDAL().InsertRange(aliOrderinfos);
                });
                return true;
            }
            catch (Exception e)
            {
                LogUtil.Info("处理订单表数据error:" + e.ToString(), _logName);
                Console.WriteLine("处理订单表数据error:" + e.ToString());
            }
            return false;
        }
 
        #endregion
 
 
 
 
        #region 1688时间格式转换
        private static DateTime? TimeSpanChange(string timset)
        {
            if (timset.IsNullOrEmpty())
            {
                return null;
            }
            string format = "yyyyMMddHHmmssfffzzz";
            return DateTime.ParseExact(timset, format, CultureInfo.InvariantCulture, DateTimeStyles.None);
        }
 
        #endregion
 
 
        #region 获取1688对应的有赞订单
        private static string GetYzTid(string tid)
        {
            var tradeRelat = new TradeCreateRelationDAL().ListGet(t => t.AliTid == tid);
            string msg = "";
            if (tradeRelat.IsNull() || tradeRelat.Count==0)
            {
                msg = "没有找到对应有赞的订单";
            }
            else
            {
                msg = tradeRelat[0].YzTid;
            }
            return msg;
        }
 
        #endregion
 
 
    }
}