johnswang
2022-01-15 22907b8763fc073011872b637dc23023ef7fb4f4
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
package com.yc.sdk.shopping.action.jiazhuang;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
 
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.yc.action.BaseAction;
import com.yc.entity.DataSourceEntity;
import com.yc.exception.ApplicationException;
import com.yc.multiData.MultiDataSource;
import com.yc.multiData.SpObserver;
import com.yc.sdk.miniapp.entity.LiveGoodsEntity;
import com.yc.sdk.miniapp.entity.LiveReplayVideoEntity;
import com.yc.sdk.miniapp.entity.LiveRoomEntity;
import com.yc.sdk.miniapp.entity.LiveRoomEntity.RoomInfo;
import com.yc.sdk.miniapp.service.MaLiveIfc;
import com.yc.sdk.shopping.action.api.LiveRoomVideo;
import com.yc.sdk.shopping.action.api.ShopCcCode;
import com.yc.sdk.shopping.entity.CreateLiveRoomParameterStep1Entity;
import com.yc.sdk.shopping.entity.CreateLiveRoomParameterStep2Entity;
import com.yc.sdk.shopping.entity.CreateLiveRoomParameterStep3Entity;
import com.yc.sdk.shopping.entity.CreateLiveRoomResultEntity;
import com.yc.sdk.shopping.entity.PanicBuyingEntity;
import com.yc.sdk.shopping.entity.SettingEntity;
import com.yc.sdk.shopping.entity.ShopCcCodeEntity;
import com.yc.sdk.shopping.service.SettingIfc;
import com.yc.sdk.shopping.service.panicBuying.PanicBuyingIfc;
import com.yc.sdk.shopping.util.SettingKey;
import com.yc.sdk.shopping.util.StringLengthUtil;
import com.yc.utils.SessionKey;
 
import me.chanjar.weixin.common.error.WxErrorException;
 
import com.yc.factory.FactoryBean;
 
@Controller
@Scope("prototype")
@RequestMapping("/shopping/panicBuyingLive/")
public class PanicBuyingLive   extends BaseAction{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    MaLiveIfc maLiveIfc ;
    
    @Autowired
     SettingIfc settingIfc;
    @Autowired
    PanicBuyingIfc panicBuyingIfc;
    
 
    
    /**
     * 获取我的直播间列信息(包含已经创建和未创建的,用于新建直接间)
     * @param request
     * @param response
     */
    @GetMapping("/getLiveRoomListForOpenId.do")
    public void getLiveRoomListForOpenId(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session=request.getSession();
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        //String roomId = request.getParameter("roomid") ;
        //String docCode = request.getParameter(SettingKey.DOCCODE) ;
        String hostUrl = SettingKey.getHostUrl(request);
        
        //String userCode = (String) session.getAttribute(SessionKey.HRCODE) ;
        String openId = (session.getAttribute(SessionKey.WEIXIN_OPENID) == null ? "": (String) session.getAttribute(SessionKey.WEIXIN_OPENID));
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
            
            //刷新房间及状态信息 ,不需要刷新,因为设置了定时刷新功能 
            //refreshLiveRoom(dataSourceEntity);
            
            //SettingEntity settingEntity = settingIfc.getSettingEntity(request) ;
            
            //取网店 shopcccode
            //ShopCcCodeEntity shopCcCodeEntity = ShopCcCode.getShopCcCode(settingEntity,request);
 
            //重新获取房间列表
            //直播计划
            LiveRoomEntity dataList = maLiveIfc.getLiveRoomListForOpenId(openId) ;
            List<RoomInfo>  liveList = dataList.getRoomInfo().stream().filter(s->s.getLiveStatus()==null||s.getLiveStatus().intValue()==101||s.getLiveStatus().intValue()==102).collect(Collectors.toList());
            JsonArray liveRoomListArray = new JsonArray();
            for (int i = 0 ; liveList!= null  && i < liveList.size();i++) {
                RoomInfo roomInfo = liveList.get(i) ;
                
                JsonObject jsonItem = getRoomJsonObject( roomInfo, hostUrl, dataSourceEntity.getDbId());
                liveRoomListArray.add(jsonItem) ;
            }
            json.add("liveList", liveRoomListArray);
            
            //历史直播
            List<RoomInfo>  historyList = dataList.getRoomInfo().stream().filter(s->s.getLiveStatus()!=null&&s.getLiveStatus().intValue()!=101&&s.getLiveStatus().intValue()!=102).collect(Collectors.toList());
            JsonArray historyRoomListArray = new JsonArray();
            for (int i = 0 ; historyList!= null  && i < historyList.size();i++) {
                RoomInfo roomInfo = historyList.get(i) ;
                
                JsonObject jsonItem = getRoomJsonObject( roomInfo, hostUrl, dataSourceEntity.getDbId());
                historyRoomListArray.add(jsonItem) ;
            }
            json.add("historyList", historyRoomListArray);
            
            
            json.addProperty("state", "success");            
            this.printJson(response, json.toString());
            return;
        }catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            e.printStackTrace();
            errJson.addProperty("warning",(e.getCause()!=null?e.getCause().getMessage(): e.getMessage())+"【/shopping/live/getLiveRoomListForMe.do】");
            json.add("error", errJson);
            this.printJson(response, json.toString());
        }finally {
            SpObserver.setDBtoInstance();
        }
    }
    
    /**
     * 获取直播间明细
     * @param request
     * @param response
     */
    @GetMapping("/getLiveRoomDetailByDocCode.do")
    public void getLiveRoomDetailByDocCode(HttpServletRequest request, HttpServletResponse response) {
        //HttpSession session=request.getSession();
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        //String roomId = request.getParameter("roomid") ;
        String docCode = request.getParameter(SettingKey.DOCCODE) ;
        String hostUrl = SettingKey.getHostUrl(request);
        
        //String userCode = (String) session.getAttribute(SessionKey.HRCODE) ;
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
            
            //刷新房间及状态信息 ,不需要刷新,因为设置了定时刷新功能 
            //refreshLiveRoom(dataSourceEntity);
            
            //SettingEntity settingEntity = settingIfc.getSettingEntity(request) ;
            
            //取网店 shopcccode
            //ShopCcCodeEntity shopCcCodeEntity = ShopCcCode.getShopCcCode(settingEntity,request);
 
            //重新获取房间列表
            LiveRoomEntity liveRoomList = maLiveIfc.getLiveRoom(docCode) ;
            JsonArray roomListArray = new JsonArray();
            for (int i = 0 ; liveRoomList!= null && liveRoomList.getRoomInfo() != null  && i < liveRoomList.getRoomInfo().size();i++) {
                RoomInfo roomInfo = liveRoomList.getRoomInfo().get(i) ;
                
                JsonObject jsonItem = getRoomJsonObject( roomInfo, hostUrl, dataSourceEntity.getDbId());
                roomListArray.add(jsonItem) ;
            }
            json.add("list", roomListArray);
            json.addProperty("state", "success");            
            this.printJson(response, json.toString());
            return;
        }catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            e.printStackTrace();
            errJson.addProperty("warning",(e.getCause()!=null?e.getCause().getMessage(): e.getMessage())+"【/shopping/live/getLiveRoomListForMe.do】");
            json.add("error", errJson);
            this.printJson(response, json.toString());
        }finally {
            SpObserver.setDBtoInstance();
        }
    }
    
 
    
    
    
    /**
     * 获取所有直播间信息(已经创建了的直播间)
     * @param request
     * @param response
     */
    @GetMapping("/getLiveRoomList.do")
    public void getLiveRoomList(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session=request.getSession();
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        String roomId = request.getParameter("roomid") ;
        String docCode = request.getParameter(SettingKey.DOCCODE) ;
        String hostUrl = SettingKey.getHostUrl(request);
        
        @Deprecated
        String isShowBannerPhotoOnHomePage = request.getParameter("isShowBannerPhotoOnHomePage") ;   //是否只显示带有首页广告的直播间信息: true , false  
        
        String userCode = (String) session.getAttribute(SessionKey.HRCODE) ;
        String isShowOwnerLiveRoom = request.getParameter("isShowOwnerLiveRoom") ;   //只显示自己的直播间 ,包括没有正式创建成功的,因为没创建成功的记录可以再次创建
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
            
            //刷新房间及状态信息 ,不需要刷新,因为设置了定时刷新功能 
            //refreshLiveRoom(dataSourceEntity);
            
            SettingEntity settingEntity = settingIfc.getSettingEntity(request) ;
            
            //取网店 shopcccode
            ShopCcCodeEntity shopCcCodeEntity = ShopCcCode.getShopCcCode(settingEntity,request);
 
            //重新获取房间列表
            LiveRoomEntity liveRoomList = maLiveIfc.getLiveRoomList(roomId==null?null:Integer.valueOf(roomId),shopCcCodeEntity.getShopCcCode(),
                    isShowBannerPhotoOnHomePage!=null&&"true".equalsIgnoreCase(isShowBannerPhotoOnHomePage),
                    isShowOwnerLiveRoom!=null&&"true".equalsIgnoreCase(isShowOwnerLiveRoom),
                    userCode,docCode) ;
            JsonArray roomListArray = new JsonArray();
            for (int i = 0 ; liveRoomList!= null && liveRoomList.getRoomInfo() != null  && i < liveRoomList.getRoomInfo().size();i++) {
                RoomInfo roomInfo = liveRoomList.getRoomInfo().get(i) ;
                if (isShowOwnerLiveRoom==null||!"true".equalsIgnoreCase(isShowOwnerLiveRoom) ) {
                    if (roomInfo.getId() == null&& (docCode==null||"".equals(docCode))) {
                        continue;
                    }
                }
                
                JsonObject jsonItem = getRoomJsonObject( roomInfo, hostUrl, dataSourceEntity.getDbId());
                roomListArray.add(jsonItem) ;
            }
            json.add("list", roomListArray);
            json.addProperty("state", "success");            
            this.printJson(response, json.toString());
            return;
        }catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            e.printStackTrace();
            errJson.addProperty("warning",(e.getCause()!=null?e.getCause().getMessage(): e.getMessage())+"【/shopping/live/getLiveRoomList.do】");
            json.add("error", errJson);
            this.printJson(response, json.toString());
        }finally {
            SpObserver.setDBtoInstance();
        }
    }
    
    
       /**
        * 我的商品库
        * @param request
        * @param response
        */
       @RequestMapping("/getGoodsListForMe.do")
       public void getGoodsListForMe(HttpServletRequest request, HttpServletResponse response) {
           HttpSession session = request.getSession();
           JsonObject json = new JsonObject();
           JsonObject errJson = new JsonObject();
           String hostUrl = SettingKey.getHostUrl(request);
           try {
               DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
               SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
               //SettingEntity settingEntity = settingIfc.getSettingEntity(request) ;
               
               String openId = (String)session.getAttribute(SessionKey.WEIXIN_OPENID);
               JsonArray jsonArray = new JsonArray();
               List<LiveGoodsEntity> list = maLiveIfc.getGoodsListForMe(openId) ;
               for (int i = 0; list!=null&& i < list.size(); i++) {
                   LiveGoodsEntity liveGoodsEntity = list.get(i) ;
                   jsonArray.add(LiveRoomVideo.getGoodsJsonObject( liveGoodsEntity, hostUrl, dataSourceEntity.getDbId()+"") );
               }
               json.add("list", jsonArray);
               this.printJson(response, json.toString());
               return ;
           } catch (DataAccessException e) {
               this.logger.error(e.getMessage(), e);
               e.printStackTrace();
               errJson.addProperty("warning",(e.getCause()!=null?e.getCause().getMessage(): e.getMessage())+"【/shopping/live/getGoodsListForMe.do】");
               json.add("error", errJson);
               this.printJson(response, json.toString());
               return ;
           }catch (Exception e) {
               this.logger.error(e.getMessage(), e);
               e.printStackTrace();
               errJson.addProperty("warning",(e.getCause()!=null?e.getCause().getMessage(): e.getMessage())+"【/shopping/live/getGoodsListForMe.do】");
               json.add("error", errJson);
               this.printJson(response, json.toString());
               return ;
           }finally {
               SpObserver.setDBtoInstance();
           }    
       }
       
       
    /**
     * 直播间商品列表
     * @param request
     * @param response
     */
    @RequestMapping("/goodsListInRoom.do")
    public void goodsListInRoom(HttpServletRequest request, HttpServletResponse response) {
        //HttpSession session = request.getSession();
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        //CallBackMessage callBackMessage=new CallBackMessage();
        String roomId = request.getParameter("roomid") ;
        //String goodsId = request.getParameter("goodsid") ;
        //String onSaleStr = request.getParameter("onSale") ;
        //String nickName = request.getParameter("nickName") ;
        //String goodsId = request.getParameter("goodsid") ;   
        if (roomId == null || "".equals(roomId) || "null".equals(roomId)) {
            errJson.addProperty("warning","必须指定 roomid 参数【/shopping/live/goodsListInRoom.do】");
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }
        
        String hostUrl = SettingKey.getHostUrl(request);
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
            
            List<LiveGoodsEntity> list = maLiveIfc.getGoodsListInRoom(Integer.parseInt(roomId));
            JsonArray goodsJsonArray = new JsonArray();
            for (int i = 0;list!=null&& i < list.size(); i++) {
                LiveGoodsEntity liveGoodsEntity = list.get(i) ;
                
                goodsJsonArray.add(LiveRoomVideo.getGoodsJsonObject( liveGoodsEntity, hostUrl, dataSourceEntity.getDbId()+"") );
                
            }
            json.add("list", goodsJsonArray);
 
            json.addProperty("state", "success");            
            this.printJson(response, json.toString());
        } catch (DataAccessException e) {
            this.logger.error(e.getMessage(), e);
            e.printStackTrace();
            errJson.addProperty("warning",(e.getCause()!=null?e.getCause().getMessage(): e.getMessage())+"【/shopping/live/goodsListInRoom.do】");
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            e.printStackTrace();
            errJson.addProperty("warning",(e.getCause()!=null?e.getCause().getMessage(): e.getMessage())+"【/shopping/live/goodsListInRoom.do】");
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }finally {
            SpObserver.setDBtoInstance();
        }
    }
    
    
 
    /**
     * 获取可导入直播间的商品
     * @param request
     * @param response
     */
    @RequestMapping("/getGoodsListForImportRoom.do")
    public void getGoodsListForImportRoom(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        String hostUrl = SettingKey.getHostUrl(request);
        String roomId = request.getParameter("roomid") ;
        if (roomId == null||"".equals(roomId)) {
            errJson.addProperty("warning","请传递 roomid 参数【/shopping/live/getGoodsListForImportRoom.do】");
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }
        
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
            //SettingEntity settingEntity = settingIfc.getSettingEntity(request) ;
            
            String openId = (String)session.getAttribute(SessionKey.WEIXIN_OPENID);
            JsonArray jsonArray = new JsonArray();
            List<LiveGoodsEntity> list = maLiveIfc.getGoodsListForImportRoom(Integer.parseInt(roomId),openId) ;
            for (int i = 0; list!=null&& i < list.size(); i++) {
                LiveGoodsEntity liveGoodsEntity = list.get(i) ;
                jsonArray.add(LiveRoomVideo.getGoodsJsonObject( liveGoodsEntity, hostUrl, dataSourceEntity.getDbId()+"") );
            }
            json.add("list", jsonArray);
            this.printJson(response, json.toString());
            return ;
        } catch (DataAccessException e) {
            this.logger.error(e.getMessage(), e);
            e.printStackTrace();
            errJson.addProperty("warning",(e.getCause()!=null?e.getCause().getMessage(): e.getMessage())+"【/shopping/live/getGoodsListForImportRoom.do】");
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            e.printStackTrace();
            errJson.addProperty("warning",(e.getCause()!=null?e.getCause().getMessage(): e.getMessage())+"【/shopping/live/getGoodsListForImportRoom.do】");
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }finally {
            SpObserver.setDBtoInstance();
        }    
    }
    
    
 
    /**
     * 获取可导入商品库的活动列表
     * @param request
     * @param response
     */
    @RequestMapping("/getPanicBuyingListForImportGoods.do")
    //@RequestMapping("/shopping/live/getPanicBuyingListForLiveGoods.do")
    public void getPanicBuyingListForImportGoods(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        JsonObject json = new JsonObject();  
        JsonObject errJson = new JsonObject();
        //String cltCode = (session.getAttribute(SettingKey.CLTCODE) == null?"": (String)session.getAttribute(SettingKey.CLTCODE)) ;
        String hostUrl = SettingKey.getHostUrl(request);
        String openId = (session.getAttribute(SessionKey.WEIXIN_OPENID) == null ? "": (String) session.getAttribute(SessionKey.WEIXIN_OPENID));
        
        DataSourceEntity dataSourceEntity ;
        try {
            dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
        }catch (DataAccessException e) {
            errJson.addProperty("warning", (e.getCause()!=null?e.getCause().getMessage(): e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
        }catch (Exception e ){
            errJson.addProperty("warning", (e.getCause()!=null?e.getCause().getMessage(): e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
        }
        
        try {
            SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());// 切换数据源
            //SettingEntity settingEntity = settingIfc.getSettingEntity();
            
            List<PanicBuyingEntity> dataList = panicBuyingIfc.getPanicBuyingListForImportGoods(openId) ;
            //草稿
            JsonArray draftListJsonArray = new JsonArray();
            for (int i = 0;dataList!=null&& i < dataList.size(); i++) {
                draftListJsonArray.add(PanicBuying.getPanicBuyingJsonObject( dataList.get(i), hostUrl, dataSourceEntity.getDbId()+"",openId));    
            }
            json.add("list", draftListJsonArray);
            
            json.addProperty("state", "success");
            this.printJson(response, json.toString());
            return;
        } catch (DataAccessException e) {
            errJson.addProperty("warning", (e.getCause()!=null?e.getCause().getMessage(): e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
        }catch (Exception e ){
            errJson.addProperty("warning", (e.getCause()!=null?e.getCause().getMessage(): e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
        } finally {
            SpObserver.setDBtoInstance();
        }    
    }
    
    public static JsonObject getRoomJsonObject(RoomInfo roomInfo,String hostUrl,Integer dbId) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
        JsonObject jsonItem = new JsonObject();
        
        jsonItem.addProperty("DocCode", roomInfo.getDocCode());
        jsonItem.addProperty("FormId", roomInfo.getFormId());
        jsonItem.addProperty("DocDate", roomInfo.getDocDate()==null?"":format2.format( roomInfo.getDocDate()));
        jsonItem.addProperty("EnterCode", roomInfo.getEnterCode());
        jsonItem.addProperty("EnterName", roomInfo.getEnterName());
        jsonItem.addProperty("EnterDate", roomInfo.getEnterDate()==null?"":format.format( roomInfo.getEnterDate()));
        jsonItem.addProperty("RoomId", roomInfo.getId());  //房间号
        jsonItem.addProperty("RoomName", roomInfo.getName());  //直播标题
        jsonItem.addProperty("CoverImg", roomInfo.getCoverImg());   //直播间背景墙
        jsonItem.addProperty("CoverImgUnid",roomInfo.getCoverImgUnid());  //直播间背景墙UNID
        jsonItem.addProperty("CoverImgUnidUrl", SettingKey.getUrl(hostUrl,  roomInfo.getCoverImgUnidUrl(), dbId+"",null));  //直播间背景墙URL
        jsonItem.addProperty("LiveStatus", roomInfo.getLiveStatus());  //直播状态:101 直播中,102 未开始,103 已结束 ,104 禁播,105 暂停中,106 异常,107 已过期
        jsonItem.addProperty("StartTime", roomInfo.getStartDateTime() == null ?"": format.format(roomInfo.getStartDateTime()));   //开播开始时间
        jsonItem.addProperty("EndTime", roomInfo.getEndDateTime() == null ? "" :format.format(roomInfo.getEndDateTime()));   //开播结束时间
        
        jsonItem.addProperty("AnchorWechat", roomInfo.getAnchorWechat());  //主播微信号
        jsonItem.addProperty("AnchorName", roomInfo.getAnchorName());   //主播昵称
        jsonItem.addProperty("AnchorImg", roomInfo.getAnchorImg());   //主播头像
        jsonItem.addProperty("AnchorImgUnid", roomInfo.getAnchorImgUnid());  //主播头像Unid
        jsonItem.addProperty("AnchorImgUnidUrl", SettingKey.getUrl(hostUrl,  roomInfo.getAnchorImgUnidUrl(), dbId+"",null));  //主播头像Url
        jsonItem.addProperty("SubAnchorWechat", roomInfo.getSubAnchorWechat());   //主播副号微信号
        jsonItem.addProperty("ShareImg", roomInfo.getShareImg());   //分享卡片封面
        jsonItem.addProperty("ShareImgUnid", roomInfo.getShareImgUnid());  //分享卡片封面Unid
        jsonItem.addProperty("ShareImgUnidUrl", SettingKey.getUrl(hostUrl,  roomInfo.getShareImgUnidUrl(), dbId+"",null));  //分享卡片封面Url
        jsonItem.addProperty("ShopCcCode", roomInfo.getShopCcCode());
        jsonItem.addProperty("LiveBalanceSecond", roomInfo.getLiveBalanceSecond().longValue()<0?0:roomInfo.getLiveBalanceSecond());   //直播到计时
        jsonItem.addProperty("isShowBannerPhotoOnHomePage", roomInfo.isShowBannerPhotoOnHomePage());
        
        jsonItem.addProperty("BannerPhoto", roomInfo.getBannerPhoto());   //广告图片Unid
        jsonItem.addProperty("BannerPhotoUrl", SettingKey.getUrl(hostUrl,  roomInfo.getBannerPhotoUrl(), dbId+"",null));  //广告图片Url
        jsonItem.addProperty("FeedsImg", roomInfo.getFeedsImgUnid());  //购物直播频道封面图
        jsonItem.addProperty("FeedsImgUnid", roomInfo.getFeedsImgUnid());  //购物直播频道封面图Unid
        jsonItem.addProperty("FeedsImgUnidUrl", SettingKey.getUrl(hostUrl,  roomInfo.getFeedsImgUnidUrl(), dbId+"",null));  //购物直播频道封面图Url
        jsonItem.addProperty("isFeedsPublic", roomInfo.getIsFeedsPublic().equals(1)?true:false);  //是否开启官方收录
        jsonItem.addProperty("ScreenType", roomInfo.getScreenType());  //0竖屏,1横屏、
        jsonItem.addProperty("Type", roomInfo.getType());  //直播间类型:0 手机直播, 1 推流
        jsonItem.addProperty("isCloseLike", roomInfo.getCloseLike().equals(1)?true:false);  //是否关闭点赞 
        jsonItem.addProperty("isCloseGoods", roomInfo.getCloseGoods().equals(1)?true:false);   //是否关闭货架
        jsonItem.addProperty("isCloseComment", roomInfo.getCloseComment().equals(1)?true:false);   //是否关闭评论
        jsonItem.addProperty("isCloseReplay", roomInfo.getCloseReplay().equals(1)?true:false);   //是否关闭回放
        jsonItem.addProperty("isCloseShare", roomInfo.getCloseShare().equals(1)?true:false);   //是否关闭分享
        jsonItem.addProperty("isCloseKf", roomInfo.getCloseKf().equals(1)?true:false);   //是否关闭客服
        
        JsonArray replayListArray = new JsonArray();
        MaLiveIfc maLiveIfc = (MaLiveIfc)FactoryBean.getBean("MaLiveImpl"); 
        LiveReplayVideoEntity replayList = maLiveIfc.getReplayVideo(roomInfo.getId()) ;
        for (int j =0; replayList != null && replayList.getLiveReplay()!= null && j < replayList.getLiveReplay().size();j++) {
            JsonObject replayItem = new JsonObject();
            replayItem.addProperty("ExpireTime", replayList.getLiveReplay().get(j).getExpireTime());   //回放过期时间
            replayItem.addProperty("CreateTime", replayList.getLiveReplay().get(j).getCreateTime());   //回放生成时间
            replayItem.addProperty("MediaUrl", replayList.getLiveReplay().get(j).getMediaUrl());    //回放URL
            replayListArray.add(replayItem); 
        }
        jsonItem.add("ReplayVideoList",replayListArray ) ;   //回放列表
        
        jsonItem.addProperty("SubscribeTimes", roomInfo.getSubscribeTimes());   //直播间订阅次数
        return jsonItem;
    }
    
    
    /**
     * 保存小程序提交过来的新建直播间信息 , 第一步
     * @param request
     * @param response
     */
    @RequestMapping("/saveRoomStep1.do")
    public void saveRoomStep1(@RequestBody CreateLiveRoomParameterStep1Entity createLiveRoomParameterStep1Entity, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session=request.getSession();
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        String userCode = (String) session.getAttribute(SessionKey.HRCODE) ;
        String userName = (String) session.getAttribute(SessionKey.HRNAME) ;
        String openId = (session.getAttribute(SessionKey.WEIXIN_OPENID) == null ? "": (String) session.getAttribute(SessionKey.WEIXIN_OPENID));
 
        if (createLiveRoomParameterStep1Entity.getName() == null||"".equals(createLiveRoomParameterStep1Entity.getName())) {
            errJson.addProperty("warning","必须传递直播间标题 name 【最短3个汉字,最长17个汉字】");
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }
        int len = StringLengthUtil.getWordCount(createLiveRoomParameterStep1Entity.getName());
        if (len < 6 || len > 34) {
            errJson.addProperty("warning","直播间标题最短3个汉字,最长17个汉字");
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }
        if (createLiveRoomParameterStep1Entity.getAnchorName() == null||"".equals(createLiveRoomParameterStep1Entity.getAnchorName())) {
            errJson.addProperty("warning","必须传递主播昵称 anchorName 【最短2个汉字,最长15个汉字】");
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }
        
        len = StringLengthUtil.getWordCount(createLiveRoomParameterStep1Entity.getAnchorName());
        if (len < 4 || len > 30) {
            errJson.addProperty("warning","主播昵称最短2个汉字,最长15个汉字");
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }
        
        if (createLiveRoomParameterStep1Entity.getAnchorWechat() == null||"".equals(createLiveRoomParameterStep1Entity.getAnchorWechat())) {
            errJson.addProperty("warning","必须传递主播微信号 anchorWechat 【如果未实名认证,需要先前往“小程序直播”小程序进行实名验证】");
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }
 
        if (createLiveRoomParameterStep1Entity.getShareImgUnid() == null||"".equals(createLiveRoomParameterStep1Entity.getShareImgUnid())) {
            errJson.addProperty("warning","必须上传分享卡片封面 ShareImgUnid 字段");
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }
        
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
            SettingEntity settingEntity = settingIfc.getSettingEntity(request) ;
            //取网店 shopcccode
            ShopCcCodeEntity shopCcCodeEntity = ShopCcCode.getShopCcCode(settingEntity,request);
            
            createLiveRoomParameterStep1Entity.setShopCcCode(shopCcCodeEntity.getShopCcCode());
            createLiveRoomParameterStep1Entity.setOpenId(openId);
            createLiveRoomParameterStep1Entity.setUserCode(userCode);
            createLiveRoomParameterStep1Entity.setUserName(userName);
            
            String newDocCode = maLiveIfc.createRoomStep1(createLiveRoomParameterStep1Entity);
            
            json.addProperty(SettingKey.DOCCODE, newDocCode);    
            json.addProperty("state", "success");    
            this.printJson(response, json.toString());
            return ;
            
        }catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            e.printStackTrace();
            errJson.addProperty("warning",(e.getCause()!=null?e.getCause().getMessage(): e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }finally {
            SpObserver.setDBtoInstance();
        }
    }
    
    /**
     * 保存小程序提交过来的新建直播间信息 , 第二步
     * @param request
     * @param response
     */
    @RequestMapping("/saveRoomStep2.do")
    public void saveRoomStep2(@RequestBody CreateLiveRoomParameterStep2Entity createLiveRoomParameterStep2Entity, HttpServletRequest request, HttpServletResponse response) {
        //HttpSession session=request.getSession();
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
             
            maLiveIfc.createRoomStep2(createLiveRoomParameterStep2Entity);
            
            json.addProperty(SettingKey.DOCCODE, createLiveRoomParameterStep2Entity.getDocCode());    
            json.addProperty("state", "success");    
            this.printJson(response, json.toString());
            return ;
            
        }catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            e.printStackTrace();
            errJson.addProperty("warning",(e.getCause()!=null?e.getCause().getMessage(): e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }finally {
            SpObserver.setDBtoInstance();
        }
    }
    
    
    /**
     * 保存小程序提交过来的新建直播间信息 , 第三步
     * @param request
     * @param response
     */
    @RequestMapping("/saveRoomStep3.do")
    public void saveRoomStep3(@RequestBody CreateLiveRoomParameterStep3Entity createLiveRoomParameterStep3Entity, HttpServletRequest request, HttpServletResponse response) {
        //HttpSession session=request.getSession();
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
            
            if (createLiveRoomParameterStep3Entity.getStartTime() == null||"".equals(createLiveRoomParameterStep3Entity.getStartTime())) {
                errJson.addProperty("warning","必须传递直播计划开始时间 startTime ,格式:yyyy-MM-dd HH:mm:ss");
                json.add("error", errJson);
                this.printJson(response, json.toString());
                return ;
            }
            if (createLiveRoomParameterStep3Entity.getEndTime() == null||"".equals(createLiveRoomParameterStep3Entity.getEndTime())) {
                errJson.addProperty("warning","必须传递直播计划结束时间 endTime ,格式:yyyy-MM-dd HH:mm:ss");
                json.add("error", errJson);
                this.printJson(response, json.toString());
                return ;
            }
            
            Date startDateTime = null,endDateTime = null;
            try {
                startDateTime = sdf.parse(createLiveRoomParameterStep3Entity.getStartTime()) ;
            } catch (ParseException e) {
                errJson.addProperty("warning","直播计划开始时间 startTime 不能转换为日期类型,请检查日期格式是否符合:yyyy-MM-dd HH:mm:ss ");
                json.add("error", errJson);
                this.printJson(response, json.toString());
                return ;
            }
            try {
                endDateTime = sdf.parse(createLiveRoomParameterStep3Entity.getEndTime()) ;
            } catch (ParseException e) {
                errJson.addProperty("warning","直播计划开始时间 endTime 不能转换为日期类型,请检查日期格式是否符合:yyyy-MM-dd HH:mm:ss ");
                json.add("error", errJson);
                this.printJson(response, json.toString());
                return ;
            }
            createLiveRoomParameterStep3Entity.setStartDateTime(startDateTime);
            createLiveRoomParameterStep3Entity.setEndDateTime(endDateTime);
            
            
            maLiveIfc.createRoomStep3(createLiveRoomParameterStep3Entity);
            
            //创建直播间
            CreateLiveRoomResultEntity createLiveRoomResultEntity = LiveRoomVideo.CreateLiveRoom( dataSourceEntity, createLiveRoomParameterStep3Entity.getDocCode(), request);
 
            json.addProperty("roomId", createLiveRoomResultEntity.getRoomId());    //当主播微信号没有在 “小程序直播“ 小程序实名认证 返回该字段
            json.addProperty("qrcodeUrl", createLiveRoomResultEntity.getQrCodeUrl());    //当主播微信号没有在 “小程序直播“ 小程序实名认证 返回该字段
            json.addProperty("qrcodeUrlUnid", createLiveRoomResultEntity.getQrCodeUrlUnid());    //当主播微信号没有在 “小程序直播“ 小程序实名认证 返回该字段
            json.addProperty(SettingKey.DOCCODE, createLiveRoomParameterStep3Entity.getDocCode());    
            json.addProperty("state", createLiveRoomResultEntity.getState());    
            this.printJson(response, json.toString());
            return ;
        } catch (ApplicationException e) {
            this.logger.error(e.getMessage(), e);
            //e.printStackTrace();   //ApplicationException 例外不要输出这句话
            errJson.addProperty("warning",(e.getCause()!=null?e.getCause().getMessage(): e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        } catch(WxErrorException e) {
            this.logger.error(e.getMessage(), e);
            e.printStackTrace();
            errJson.addProperty("warning","错误代码:" + e.getError().getErrorCode() + ",错误信息:" + e.getError().getErrorMsg());
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            e.printStackTrace();
            errJson.addProperty("warning",(e.getCause()!=null?e.getCause().getMessage(): e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }finally {
            SpObserver.setDBtoInstance();
        }
    }
}