xinyb_
2021-05-24 c8636e4ca03e7fe13902ff98687f7591706cc780
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
package com.yc.sdk.shopping.action;
 
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import com.yc.action.grid.QrCodeRunable;
import com.yc.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.yc.action.BaseAction;
import com.yc.entity.DataSourceEntity;
import com.yc.multiData.MultiDataSource;
import com.yc.multiData.SpObserver;
import com.yc.sdk.miniapp.action.MaServiceInit;
import com.yc.sdk.miniapp.service.MaUserIfc;
import com.yc.sdk.password.action.ChangePassword;
import com.yc.sdk.shopping.entity.AddressEntity;
import com.yc.sdk.shopping.entity.BalanceEntity;
import com.yc.sdk.shopping.entity.CltTypeEntity;
import com.yc.sdk.shopping.entity.CurrencyEntity;
import com.yc.sdk.shopping.entity.CustomerEntity;
import com.yc.sdk.shopping.entity.MemberInfoEntity;
import com.yc.sdk.shopping.entity.OtherStaffEntity;
import com.yc.sdk.shopping.entity.SettingEntity;
import com.yc.sdk.shopping.entity.SourceInfoEntity;
import com.yc.sdk.shopping.service.CurrencyIfc;
import com.yc.sdk.shopping.service.SettingIfc;
import com.yc.sdk.shopping.service.address.AddressIfc;
import com.yc.sdk.shopping.service.balance.BalanceIfc;
import com.yc.sdk.shopping.service.register.AccountIfc;
import com.yc.sdk.shopping.service.register.CltTypeIfc;
import com.yc.sdk.shopping.service.register.OtherStaffIfc;
import com.yc.sdk.shopping.service.register.SourceInfoIfc;
import com.yc.sdk.shopping.service.wxpay.WxPayIfc;
import com.yc.sdk.shopping.util.SettingKey;
import com.yc.sdk.shopping.util.StringReplaceUtil;
import com.yc.sdk.weixincp.action.CPUser;
import com.yc.sdk.weixincp.action.CpServiceInit;
import com.yc.sdk.weixincp.entity.MyWxCpUser;
import com.yc.sdk.weixincp.service.AppIfc;
import com.yc.sdk.weixincp.service.ERPUserIfc;
import com.yc.sdk.weixinmp.entity.MyWxMpUser;
import com.yc.sdk.weixinmp.service.MpUserIfc;
import com.yc.service.demo.DemoIfc;
import com.yc.utils.SessionKey;
 
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import me.chanjar.weixin.cp.api.WxCpService;
import redis.clients.jedis.Jedis;
//import me.chanjar.weixin.cp.config.WxCpJedisConfigStorage;
import redis.clients.jedis.JedisPool;
 
@Controller
@RequestMapping("/shopping/account.do")
public class Account extends BaseAction {
    @Autowired
    AccountIfc accountIfc;
    @Autowired
    SettingIfc settingIfc;
    @Autowired
    DemoIfc demo;
    @Autowired
    CPUser user;
    @SuppressWarnings("rawtypes")
    @Autowired
    RedisTemplate redisTemplate ;
    @Autowired
    MpUserIfc mpUserIfc;
    @Autowired
    AppIfc appIfc ;
    @Autowired
    JedisPool jedisPool;
    @Autowired
    AddressIfc addressIfc;
    @Autowired
    WxPayIfc wxPayIfc;
    @Autowired
    CurrencyIfc currencyIfc;
    @Autowired
    BalanceIfc balanceIfc ;
    @Autowired
    MaUserIfc maUserIfc ;
    @Autowired
    SourceInfoIfc sourceInfoIfc;  //客户来源下拉列表
    @Autowired
    CltTypeIfc cltTypeIfc;   //客户分类
    
    
    @Autowired
    OtherStaffIfc otherStaffIfc;  //获取外部人员列表
    @Autowired
    ERPUserIfc erpUserIfc ;
    
    protected DataSourceEntity corpEntity;
    
    /**
     * 按 jsp 电话注册,需要有注册页面交互 
     * @param request
     * @param response
     */
    @RequestMapping(params = "m=telreg")
    public void newCustomerByTel(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        String sessionId = session.getId();
        //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID);
 
        String hostUrl = SettingKey.getHostUrl(request);
        // 将微信corpid组装成url
        String wxQueryString = SettingKey.getQueryStringByWx(request);
        boolean isMoblieBrowser = SettingKey.isMoblieBrowser(request);
        
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        String reg = (request.getParameter("reg") == null ? "" : request.getParameter("reg"));
        CustomerEntity customerEntity = new CustomerEntity();
        SettingEntity settingEntity = null;
        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 = settingIfc.getSettingEntity();
            
            String telephone = request.getParameter("telephone") ;
            String password = request.getParameter("password") == null ? "" : request.getParameter("password");
            String confirm = request.getParameter("confirm") == null ? "" : request.getParameter("confirm");
            String code = request.getParameter("code") == null ? "" : request.getParameter("code");
            String agree = request.getParameter("agree");
            if (telephone == null || "".equals(telephone) || telephone.length() < 11) {
                errJson.addProperty("telephone", "客户电话必须在 1 至 11 字符之间!");
            }
            if (settingEntity.isShowPassword() ) {
                if (password == null || "".equals(password))
                    errJson.addProperty("password", "密码 必须输入!");
                if (confirm == null || "".equals(confirm))
                    errJson.addProperty("confirm", "确认密码 必须输入!");
                if (password != null && confirm != null && !confirm.equals(password)) {
                    errJson.addProperty("password", "2次输入的密码 必须相同!");
                    errJson.addProperty("confirm", "2次输入的密码 必须相同!");
                }
            }
            if (agree == null || "".equals(agree)){
                errJson.addProperty("warning", "警告:您未同意 隐私政策 条款 !");
            }
            
            //验证码 
            String code2 = "" ;
            try {
                code2 = (String) redisTemplate.opsForValue().get(VerificationCodes.getCodeKey( telephone));
            }catch(Exception e) {
                e.printStackTrace();
                errJson.addProperty(SettingKey.CLTTEL, e.getMessage());
                json.add("error", errJson);
                this.printJson(response, json.toString());
                return ;
            }
            if (code == null || "".equals(code2)||code2 == null || "".equals(code2)|| ! code.equals(code2)){
                errJson.addProperty("code", "验证码不正确!");
                json.add("error", errJson);
                //this.printJson(response, json.toString());
                //return ;
            }
            
            Set<Entry<String, JsonElement>> it = errJson.entrySet();
            if (!it.isEmpty() && it.size() != 0) {
                json.add("error", errJson);
                this.printJson(response, json.toString());
                return;
            }
    
            
            String openId = request.getParameter(SessionKey.WEIXIN_OPENID);  //从页面传递openid 
            if (openId == null || openId.equals("")) {
                openId = (String) session.getAttribute(SessionKey.WEIXIN_OPENID);   //从会话取openid
            }
            
            String hrCode = (String) session.getAttribute(SessionKey.HRCODE); // 是否ERP登录的用户
            // 如果是微信过来的,则要给 USERCODE 赋值
            String corpId = request.getParameter(SessionKey.WEIXIN_CORPID);
            String wx = request.getParameter(SessionKey.WEIXIN_FROM);
            if (corpId == null || "".equals(corpId)) {
                corpId = request.getParameter(SessionKey.WEIXIN_APPID);
            }
            customerEntity.setFromWx(wx);
            
            if (settingEntity.isShowPassword() ) {
                customerEntity.setPassword(ChangePassword.getEncryptPassword(password));
            }
            if (customerEntity.getPassword()==null||"".equals(customerEntity.getPassword())) {
                customerEntity.setPassword(ChangePassword.getEncryptPassword());   //给个缺省密码 
            }
            
            customerEntity.setTel(telephone);
            customerEntity.setOpenId(openId);
            customerEntity.setCltType(settingEntity.getCltType());   //新客户分类
            customerEntity.setReferralsType(settingEntity.getReferralsType());   //新客户推荐人所属
            customerEntity.setMemberLevel(settingEntity.getDefaultMemberLevel());   //新客户会员级别
            customerEntity.setCccode(settingEntity.getCcCode()) ;    //新客户所在部门
            customerEntity.setSourceInfo(settingEntity.getDefaultSourceInfo());
            // 保存到数据库
            customerEntity = accountIfc.saveNewCustomer(customerEntity, hrCode);
 
            // 1.首先判断是否微信过来的链接,如果是,则使用 CorpId 取数据源
            //if (corpId != null && !"".equals(corpId) && wx != null && "1".equals(wx)) { // 仅限于微信号企业版
                session.setAttribute(SessionKey.USERCODE, customerEntity.getCltCode());
                session.setAttribute(SessionKey.USERNAME, customerEntity.getCltName());
            //}
            
 
            session.setAttribute(SettingKey.CLTCODE, customerEntity.getCltCode());
            session.setAttribute(SettingKey.CLTNAME, customerEntity.getCltName());
            session.setAttribute(SettingKey.CLTTEL, customerEntity.getTel());
            session.setAttribute(SettingKey.CLTEMAIL, customerEntity.getEmail());
            session.setAttribute(SettingKey.COUNTRY, customerEntity.getCountryZoneId());
            session.setAttribute(SettingKey.PROVINCE, customerEntity.getProvinceZoneId());
            session.setAttribute(SettingKey.POSTCODE, customerEntity.getPostCode());
            
            // 给匿名购物车设置“主人”,这个“主人”就是已经登录的客户名字
            accountIfc.updateCartOwner(hrCode, sessionId, customerEntity.getCltCode(), customerEntity.getCltName(),openId);
            
        } 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();
        }
        
        //取第三方 service 
        WxCpService openWxCpService = null ;
        try {
            openWxCpService = CpServiceInit.getWxCpService(dataSourceEntity.getCorpId(),"addressbook") ;
        }catch (Exception e) {
            e.printStackTrace();
            errJson.addProperty("warning", (e.getCause()!=null?e.getCause().getMessage(): e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
        }
                
        try {
            // 同步微信企业号用户
            boolean isAddWxCpUser = false;
            if (settingEntity.isSyncWxCpUser()) {
                //DataSourceEntity corpEntity = MultiDataSource.getDataSourceMap(dbId);
                //DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
                SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());// 切换数据源
                if (corpEntity != null && corpEntity.getCorpId() != null && !"".equals(corpEntity.getCorpId())) {
                    //user.setInit(wxCpConfigStorage, wxCpService, corpEntity);
                    isAddWxCpUser = user.newUser(response, customerEntity.getCltCode(), customerEntity.getCltName(),openWxCpService);
                }
            } // 同步结束
    
    
            SettingKey.setCartHintMsg(session, "成功 您的账户已注册成功。");
            
            String redirect = (session.getAttribute(SettingKey.REDIRECT)==null?null:(String)session.getAttribute(SettingKey.REDIRECT));
            if (redirect != null && !"".equals(redirect)) {
                String str = (redirect.indexOf("?") > 0?"&":"?") ;
                if (!(redirect.indexOf(SessionKey.WEIXIN_FROM+"=") > 0 
                        && (redirect.indexOf(SessionKey.WEIXIN_CORPID+"=") > 0 
                            ||redirect.indexOf(SessionKey.WEIXIN_APPID+"=") > 0 ))) {
                    redirect += (wxQueryString == null||"".equals(wxQueryString)?"":str + wxQueryString) ;
                }
            }else if (reg != null && reg.equals("1")) {
                if (isAddWxCpUser)
                    redirect = hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "account/regsuccess.jsp?sync=1"
                            + (wxQueryString == null || "".equals(wxQueryString) ? "" : "&" + wxQueryString);
                else
                    redirect = hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "account/regsuccess.jsp"
                            + (wxQueryString == null || "".equals(wxQueryString) ? "" : "?" + wxQueryString);
                
            }
            if (redirect == null || "".equals(redirect)) {
                redirect = hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "account/account.jsp"
                        + (wxQueryString == null || "".equals(wxQueryString) ? "" : "?" + wxQueryString);
            }
            
            json.addProperty("redirect",redirect );
    
            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();
        }
        
    }
 
    @RequestMapping(params = "m=getProfile")
    public void getProfile(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID);
        String cltCode = (session.getAttribute(SettingKey.CLTCODE) == null ? "" : (String)session.getAttribute(SettingKey.CLTCODE)); 
        
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        
        JsonObject cltcodeJson = new JsonObject();
        
        SettingEntity settingEntity = null ;
        CustomerEntity customerEntity = null;
        List<SourceInfoEntity> sourceInfoList = null ;
        List<CltTypeEntity> cltTypeList = null ;
        List<OtherStaffEntity> otherStaffList = null;
        
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
            settingEntity = settingIfc.getSettingEntity() ;
 
            customerEntity = accountIfc.getLoginInfoByCltCode(cltCode,null,null) ;
            if (customerEntity == null)  {  
                cltcodeJson.addProperty("cltcode","");
                json.add("list", cltcodeJson);
                json.addProperty("cltcode","") ;
                errJson.addProperty("warning", "客户未登录");
                json.add("error", errJson);
                return ;
            }
 
            sourceInfoList = sourceInfoIfc.getSourceInfoEntity(-17000110);
            cltTypeList = cltTypeIfc.getCltTypeEntity();
            otherStaffList =  otherStaffIfc.getOtherStaffEntity(settingEntity.getReferralsType()) ;
            
            cltcodeJson.addProperty("cltcode", customerEntity.getCltCode());  //客户编号
            cltcodeJson.addProperty("cltname", customerEntity.getCltName());  //您的姓名
            cltcodeJson.addProperty("gender",customerEntity.getGender());   //称呼 1.先生/男士/帅哥  ,2.小姐/女士/美女
            cltcodeJson.addProperty("telephone", customerEntity.getTel());  //手机号码
            cltcodeJson.addProperty("email", customerEntity.getEmail());   //电子邮箱
            cltcodeJson.addProperty("sourceinfo", customerEntity.getSourceInfo());  //客户来源  SourceInfoList
            cltcodeJson.addProperty("clttype", customerEntity.getCltType());   //客户分类   SourceInfoList
            cltcodeJson.addProperty("referralstype", customerEntity.getReferralsType());  //推荐人所属  SourceInfoList
            cltcodeJson.addProperty("referralscode", customerEntity.getReferralsCode());   //推荐人  OtherStaffList
            cltcodeJson.addProperty("fax", customerEntity.getFax());   //传真号码
            
            json.add("list", cltcodeJson);
            
            
            JsonArray sourceInfoJsonArray = new JsonArray();
            for(int i = 0 ;sourceInfoList != null&&sourceInfoList.size()>0&& i < sourceInfoList.size();i++) {
                JsonObject sourceInfoJsonObject = new JsonObject();
                sourceInfoJsonObject.addProperty("InterValue", sourceInfoList.get(i).getInterValue());
                sourceInfoJsonObject.addProperty("ShowValue", sourceInfoList.get(i).getDictvalue());
                sourceInfoJsonArray.add(sourceInfoJsonObject);
            }
            json.add("SourceInfoList", sourceInfoJsonArray);
            
            JsonArray cltTypeJsonArray = new JsonArray();
            for(int i = 0 ;cltTypeList != null&&cltTypeList.size()>0&& i < cltTypeList.size();i++) {
                JsonObject cltTypeJsonObject = new JsonObject();
                cltTypeJsonObject.addProperty("InterValue", cltTypeList.get(i).getCltType() );
                cltTypeJsonObject.addProperty("ShowValue", cltTypeList.get(i).getCltType() );
                cltTypeJsonArray.add(cltTypeJsonObject);
            }
            json.add("CltTypeList", cltTypeJsonArray);
            
            JsonArray otherStaffListJsonArray = new JsonArray();
            for(int i = 0 ;otherStaffList != null&&otherStaffList.size()>0&& i < otherStaffList.size();i++) {
                JsonObject otherStaffListJsonObject = new JsonObject();
                otherStaffListJsonObject.addProperty("InterValue", otherStaffList.get(i).getCltCode() );
                otherStaffListJsonObject.addProperty("ShowValue", otherStaffList.get(i).getCltName() );
                otherStaffListJsonArray.add(otherStaffListJsonObject);
            }
            json.add("OtherStaffList", otherStaffListJsonArray);
            
            
            this.printJson(response, json.toString());
            return;
            
        } catch (DataAccessException e) {
            e.printStackTrace();
            errJson.addProperty("warning", (e.getCause()!=null?e.getCause().getMessage(): e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
        } catch (Exception 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(params = "m=telRegByAutoReg")
    public void newCustomerByTelByAutoReg(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        String sessionId = session.getId();
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        //String signature=null;
        //String rawData = null ;
        String encryptedData = request.getParameter("encryptedData");
        String iv = request.getParameter("iv") ; 
        
        //String telephone = request.getParameter("telephone") == null ? "" : request.getParameter("telephone");  //电话
//        if (telephone == null || "".equals(telephone) || telephone.length() < 11) {
//            errJson.addProperty("telephone", "客户电话必须在 1 至 11 字符之间!");
//        }
        
        String referralsCode = request.getParameter("ReferralsCode") ;   //传递过来的是企业微信的 userid 
        String referralsName = request.getParameter("ReferralsName") ;   //传递过来的是企业微信的 username
        
        Set<Entry<String, JsonElement>> it = errJson.entrySet();
        if (!it.isEmpty() && it.size() != 0) {
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
        }
        String openId = request.getParameter(SessionKey.WEIXIN_OPENID);  //从页面传递openid 
        if (openId == null || openId.equals("")) {
            openId = (String) session.getAttribute(SessionKey.WEIXIN_OPENID);   //从会话取openid
        }
        
        // 如果是微信过来的,则要给 USERCODE 赋值
        String corpId = request.getParameter(SessionKey.WEIXIN_CORPID);
        String wx = request.getParameter(SessionKey.WEIXIN_FROM);
        if (corpId == null || "".equals(corpId)) {
            corpId = request.getParameter(SessionKey.WEIXIN_APPID);
        }
        CustomerEntity customerEntity = new CustomerEntity();
        SettingEntity settingEntity = null;
        DataSourceEntity dataSourceEntity = null ;
        try {
            dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
        }catch (Exception e) {
            e.printStackTrace();
            errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage());
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }
        
        //第三方托管方式写法 
        WxMaService wxService = null;
        try {
            wxService = MaServiceInit.getWxMaServiceByOpenComponentByAppId(dataSourceEntity.getMiniAppId()) ;
            if (wxService==null) {
                errJson.addProperty("warning","在数据源中没有找到该小程序的 AppId 参数设置(WxMaService),可能传递 AppId 【"+ corpId +"】参数不正确!【/shopping/account.do?m=telRegByAutoReg】");
                json.add("error", errJson);
                this.printJson(response, json.toString());
                return;    
            }
        }catch (Exception e) {
            e.printStackTrace();
            errJson.addProperty("warning",e.getCause()!=null?e.getCause().getMessage(): e.getMessage()+"【/shopping/account.do?m=telRegByAutoReg】");
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;    
        }
        String hrCode = null ;
        if (wx !=null && "3".equals(wx)) {
            hrCode = request.getParameter(SettingKey.FROMUSERID) ;   //取名片用户,主要用来获取名片上的部门信息 ,使新客户所在的部门与 名片用户一致(即在同一个部门),保证新客户下达的订单也在同一个部门
        }
        if (hrCode == null || "".equals(hrCode)){
            hrCode = (String) session.getAttribute(SessionKey.HRCODE); // 是否ERP登录的用户
        }
        try (Jedis jedis = this.jedisPool.getResource()){
            SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());// 切换数据源
            settingEntity = settingIfc.getSettingEntity();
            String sessionKey = jedis.get(SettingKey.SESSIONKEY.concat(":"+dataSourceEntity.getMiniAppId()).concat(":"+openId));
            if (sessionKey == null || "".equals(sessionKey)) {
                   sessionKey = request.getParameter(SettingKey.SESSIONKEY) ;
               }
            if (sessionKey == null || "".equals(sessionKey)) {
                errJson.addProperty("warning", "获取会话信息sessionKey失败,导致无法解密获取的电话号码,可能原因是会话已过期,请强制退出微信然后再重试【/shopping/account.do?m=telRegByAutoReg】");
                json.add("error", errJson);
                this.printJson(response, json.toString());
                return;
            }
            //解密数据获取电话号码 
            WxMaPhoneNumberInfo wxMaPhoneNumberInfo = wxService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
            
            Integer province = settingEntity.getProvinceZoneId()==null?0:settingEntity.getProvinceZoneId() ;
            Integer city = settingEntity.getCityZoneId()==null?0:settingEntity.getCityZoneId() ;
            Integer county = settingEntity.getCountyZoneId()==null?0:settingEntity.getCountyZoneId() ;
            Integer zoneId = county;
            if (zoneId == null || zoneId.equals(0))
                zoneId = city;
            if (zoneId == null || zoneId.equals(0))
                zoneId = province;
            if (zoneId == null || zoneId.equals(0))
                zoneId = 0;
            
            
            customerEntity.setCltType(settingEntity.getCltType());
            customerEntity.setSourceInfo(settingEntity.getDefaultSourceInfo());
            customerEntity.setCountryZoneId(settingEntity.getCountryId()==null?0:settingEntity.getCountryId())  ;
            customerEntity.setProvinceZoneId(province);
            customerEntity.setCityZoneId(city);
            customerEntity.setCountyZoneId(county);
            customerEntity.setZoneId(zoneId);
            customerEntity.setTel(wxMaPhoneNumberInfo.getPhoneNumber());
            customerEntity.setFax(null);
            customerEntity.setEmail(null);
            customerEntity.setPropertyId(null);
            customerEntity.setBuilding(null);
            customerEntity.setUnit(null);
            customerEntity.setHouse(null);
            customerEntity.setOpenId(openId);
            
            
            customerEntity.setPostCode(null);
 
            String password = ChangePassword.getEncryptPassword() ;
            
            customerEntity.setPassword(password);
            
            
            MyWxMpUser wxMpUser = maUserIfc.getUser(openId) ;
            
            customerEntity.setOrganization(null);
            customerEntity.setAddress(null);
            customerEntity.setCccode(settingEntity.getCcCode());
            customerEntity.setWeiXinUserStatus(4);
            customerEntity.setGender(wxMpUser != null? wxMpUser.getSex():0);
            customerEntity.setCltType(settingEntity.getCltType());
            customerEntity.setReferralsType("企业微信");  //"企业微信" 是小程序的固定法写法, 不需要值 settingEntity.getReferralsType()
            customerEntity.setReferralsCode(referralsCode);
            customerEntity.setReferralsName(referralsName);
 
            customerEntity.setFromWx(wx);
            
            customerEntity.setOpenId(openId);
            customerEntity.setCltName(wxMpUser!=null?wxMpUser.getNickname():"");
            
            
            // 保存到数据库
            customerEntity = accountIfc.saveNewCustomer(customerEntity, hrCode);
            //--增加处理客户确认后生成二维码,线程处理 by danaus 2020/8/10 11:53
            ThreadPoolTaskScheduler threadPoolTaskScheduler = (ThreadPoolTaskScheduler) FactoryBean.getBean("threadPoolTaskScheduler");
            threadPoolTaskScheduler.execute(new QrCodeRunable(170001,16,customerEntity.getDocCode()
                    ,(String) request.getSession().getAttribute(SessionKey.HRCODE)
                    ,(String) request.getSession().getAttribute(SessionKey.HRNAME)
                    ,dataSourceEntity.getDbId()+"" ));
            //----------
            //将小程序拉取下来的电话号码保存起来
            maUserIfc.updateTelephone(wxMaPhoneNumberInfo.getPhoneNumber(), openId ) ;
            
            // 1.首先判断是否微信过来的链接,如果是,则使用 CorpId 取数据源
            if (corpId != null && !"".equals(corpId) && wx != null && "1".equals(wx)) { // 仅限于微信号企业版
                session.setAttribute(SessionKey.USERCODE, customerEntity.getCltCode());
                session.setAttribute(SessionKey.USERNAME, customerEntity.getCltName());
            }
 
            session.setAttribute(SettingKey.CLTCODE, customerEntity.getCltCode());
            session.setAttribute(SettingKey.CLTNAME, customerEntity.getCltName());
            session.setAttribute(SettingKey.CLTTEL, customerEntity.getTel());
            session.setAttribute(SettingKey.CLTEMAIL, customerEntity.getEmail());
            session.setAttribute(SettingKey.COUNTRY, customerEntity.getCountryZoneId());
            session.setAttribute(SettingKey.PROVINCE, customerEntity.getProvinceZoneId());
            session.setAttribute(SettingKey.POSTCODE, customerEntity.getPostCode());
 
            // 给匿名购物车设置“主人”,这个“主人”就是已经登录的客户名字
            accountIfc.updateCartOwner(hrCode, sessionId, customerEntity.getCltCode(), customerEntity.getCltName(),openId);
            json.addProperty(SettingKey.CLTCODE, customerEntity.getCltCode());
            json.addProperty(SettingKey.CLTNAME,customerEntity.getCltName()) ;
            json.addProperty(SettingKey.ReferralsType, customerEntity.getReferralsType());   //介绍人类别
            json.addProperty(SettingKey.ReferralsCode, customerEntity.getReferralsCode());   //介绍人编号
            json.addProperty(SettingKey.ReferralsName, customerEntity.getReferralsName());   //介绍人姓名 
            json.addProperty("Telephone", customerEntity.getTel());   //电话
            json.addProperty("TelephoneMask",StringReplaceUtil.getTelephoneMask(customerEntity.getTel()));   //电话
            
            json.addProperty("state", "success");
            this.printJson(response, json.toString());
            return;
            
        }catch (DataAccessException e) {
            e.printStackTrace();
            errJson.addProperty("warning",  (e.getCause()!=null?e.getCause().getMessage(): e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
        } catch (Exception 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();
        }
        
    }
    
    
    /**
     * 按 jsp 页面注册 
     * @param request
     * @param response
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(params = "m=new")
    public void newCustomer(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        String sessionId = session.getId();
        //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID);
 
        String hostUrl = SettingKey.getHostUrl(request);
        // 将微信corpid组装成url
        String wxQueryString = SettingKey.getQueryStringByWx(request);
        boolean isMoblieBrowser = SettingKey.isMoblieBrowser(request);
        
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
 
        String hrCode = (String) session.getAttribute(SessionKey.HRCODE); // 是否ERP登录的用户
 
        String reg = (request.getParameter("reg") == null ? "" : request.getParameter("reg"));
        // String sessionId = session.getId();
        // String userCode = (session.getAttribute(SessionKey.USERCODE) == null
        // ? "" : (String)session.getAttribute(SessionKey.USERCODE));
        String cltName = request.getParameter("cltname") == null ? "" : request.getParameter("cltname");//姓名
        String telephone = request.getParameter("telephone") == null ? "" : request.getParameter("telephone");  //电话
        String fax = request.getParameter("fax") == null ? "" : request.getParameter("fax");   //传真 
        String email = request.getParameter("email") == null ? "" : request.getParameter("email");
        String sourceInfo = request.getParameter("sourceinfo") == null ? "" : request.getParameter("sourceinfo");   //来源
 
        String referralsType = request.getParameter("referralstype") == null ? "" : request.getParameter("referralstype");   //介绍人类型
        String referralsCode = request.getParameter("referralscode") == null ? "" : request.getParameter("referralscode");   //介绍人代码
        
        String password = request.getParameter("password") == null ? "" : request.getParameter("password");   //密码
        String confirm = request.getParameter("confirm") == null ? "" : request.getParameter("confirm");    //确认密码
        String countryId = request.getParameter("country_id") == null ? "" : request.getParameter("country_id"); // 国家
        String province = request.getParameter("zone_id") == null ? "" : request.getParameter("zone_id"); // 省级
        String city = request.getParameter("city") == null ? "" : request.getParameter("city"); // 城市
        String county = request.getParameter("county") == null ? "" : request.getParameter("county"); // 区/县
        String propertyId = request.getParameter("propertyid") == null ? "" : request.getParameter("propertyid");   //小区 (用于程序拼装地址)
        String building = request.getParameter("building") == null ? "" : request.getParameter("building");   //第几栋 (用于程序拼装地址)
        String unit = request.getParameter("unit") == null ? "" : request.getParameter("unit");   //第几单元 (用于程序拼装地址)
        String house = request.getParameter("house") == null ? "" : request.getParameter("house");    //门牌号 (用于程序拼装地址)
        String postCode = request.getParameter("postcode") == null ? "" : request.getParameter("postcode");   //邮编
        String organization = request.getParameter("organization") == null ? "" : request.getParameter("organization");   //客户所属公司
        String address = request.getParameter("address") == null ? "" : request.getParameter("address");   //地址(客户自己填写的)
        String gender = request.getParameter("gender") == null ? "" : request.getParameter("gender");  //性别 
        String cltType = request.getParameter("clttype") == null ? "" : request.getParameter("clttype");   //客户类型 
        String agree = request.getParameter("agree");   //同意隐私条款 
        String code = request.getParameter("code") == null ? "" : request.getParameter("code");   //验证码 
        
        // 如果是微信过来的,则要给 USERCODE 赋值
        String corpId = request.getParameter(SessionKey.WEIXIN_CORPID);
        String wx = request.getParameter(SessionKey.WEIXIN_FROM);
        if (corpId == null || "".equals(corpId)) {
            corpId = request.getParameter(SessionKey.WEIXIN_APPID);
        }
 
        if (cltName == null || "".equals(cltName) || cltName.length() < 2) {
            errJson.addProperty("cltname", "客户姓名必须在2 至 32 字符之间!");
        }
        if (gender == null || "".equals(gender)) {
            errJson.addProperty("gender", "称呼须输入!");
        }
        
        //取第三方 service 
        WxCpService openWxCpService = null ;
        DataSourceEntity dataSourceEntity = null ;
        try {
            dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            openWxCpService = CpServiceInit.getWxCpService(dataSourceEntity.getCorpId(),"addressbook") ;
        }catch (Exception e) {
            e.printStackTrace();
            errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage());
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }
        
        
        CustomerEntity customerEntity = new CustomerEntity();
        SettingEntity settingEntity = null;
        try {
            
            SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());// 切换数据源
            settingEntity = settingIfc.getSettingEntity();
 
            if (settingEntity.isShowTel() && settingEntity.isRequiredTel()) {
                if (telephone == null || "".equals(telephone) || telephone.length() < 11) {
                    errJson.addProperty("telephone", "客户电话必须在 1 至 11 字符之间!");
                }
            }
            if (settingEntity.isShowCltType() && settingEntity.isRequiredCltType()) {
                if (cltType == null || "".equals(cltType))
                    errJson.addProperty("clttype", "客户分类 必须输入!");
            }
 
            if (settingEntity.isShowEmail() && settingEntity.isRequiredEmail()) {
                if (email == null || "".equals(email))
                    errJson.addProperty("email", "电子邮箱 必须输入!");
                if (email != null && !"".equals(email) && !SettingKey.emailFormat(email))
                    errJson.addProperty("email", "电子邮箱 格式错误,请重新输入!");
            }
            if (settingEntity.isShowSourceInfo() && settingEntity.isRequiredSourceInfo()) {
                if (sourceInfo == null || "".equals(sourceInfo))
                    errJson.addProperty("sourceinfo", "客户来源 必须输入!");
            }
            
            if (!settingEntity.isShowSourceInfo()) {
                sourceInfo = settingEntity.getDefaultSourceInfo();
            }
            
            if (settingEntity.isShowReferralsCode() && settingEntity.isRequiredReferralsCode()) {
                if (referralsType == null || "".equals(referralsType))
                    errJson.addProperty("referralstype", "推荐人所属 必须输入!");
            }
            
            if (settingEntity.isShowReferralsCode() && settingEntity.isRequiredReferralsCode()) {
                if (referralsCode == null || "".equals(referralsCode))
                    errJson.addProperty("referralscode", "推荐人 必须输入!");
            }
            
            if (settingEntity.isShowFax() && settingEntity.isRequiredFax()) {
                if (fax == null || "".equals(fax))
                    errJson.addProperty("fax", "传真号码 必须输入!");
            }
            if (settingEntity.isShowPassword() && settingEntity.isRequiredPassword()) {
                if (password == null || "".equals(password))
                    errJson.addProperty("password", "密码 必须输入!");
                if (confirm == null || "".equals(confirm))
                    errJson.addProperty("confirm", "确认密码 必须输入!");
                if (password != null && confirm != null && !confirm.equals(password)) {
                    errJson.addProperty("password", "2次输入的密码 必须相同!");
                    errJson.addProperty("confirm", "2次输入的密码 必须相同!");
                }
            }
            if (settingEntity.isShowOrganization() && settingEntity.isRequiredOrganization()) {
                if (organization == null || "".equals(organization))
                    errJson.addProperty("organization", "公司名称 必须输入!");
            }
            if (settingEntity.isShowCountryId() && settingEntity.isRequiredCountryId()) {
                if (countryId == null || "".equals(countryId))
                    errJson.addProperty("country_id", "国家 必须输入!");
            }
            if (settingEntity.isShowProvinceZoneId() && settingEntity.isRequiredProvinceZoneId()) {
                if (province == null || "".equals(province))
                    errJson.addProperty("zone_id", "省份 必须输入!");
            }
            if (settingEntity.isShowCityZoneId() && settingEntity.isRequiredCityZoneId()) {
                if (city == null || "".equals(city))
                    errJson.addProperty("city", "城市 必须输入!");
            }
            if (settingEntity.isShowCountyZoneId() && settingEntity.isRequiredCountyZoneId()) {
                if (county == null || "".equals(county))
                    errJson.addProperty("county", "区/县 必须输入!");
            }
            if (settingEntity.isShowPropertyId() && settingEntity.isRequiredPropertyId()) {
                if (propertyId == null || "".equals(propertyId))
                    errJson.addProperty("propertyid", "小区名称 必须输入!");
                if (building == null || "".equals(building))
                    errJson.addProperty("building", "楼号 必须输入!");
            }
            if (settingEntity.isShowAddress() && settingEntity.isRequiredAddress()) {
                if (address == null || "".equals(address))
                    errJson.addProperty("address", "地址 必须输入!");
            }
            if (settingEntity.isShowPostcode() && settingEntity.isRequiredPostcode()) {
                if (postCode == null || "".equals(postCode))
                    errJson.addProperty("postcode", "邮编 必须输入!");
            }
            
            
            boolean foundWarning = false ;
            if (settingEntity.getCcCode() == null || "".equals(settingEntity.getCcCode())) {
                foundWarning = true ;
                errJson.addProperty("warning", "请在 714001 功能号中预先设置【新客户所在部门】参数!");
            }
            
            if (!foundWarning && (agree == null || "".equals(agree))) {
                errJson.addProperty("warning", "警告:您未同意同意 隐私政策 条款 !");
            }
            
            Set<Entry<String, JsonElement>> it = errJson.entrySet();
            if (!it.isEmpty() && it.size() != 0) {
                json.add("error", errJson);
                this.printJson(response, json.toString());
                return;
            }
            
            //验证码 
            String code2 = "" ;
            if (settingEntity.isShowTel() && settingEntity.isRequiredTel()) {
                try {
                    code2 =  (String) redisTemplate.opsForValue().get(VerificationCodes.getCodeKey( telephone)) ;
                }catch(Exception e) {
                    e.printStackTrace();
                    errJson.addProperty(SettingKey.CLTTEL, e.getMessage());
                    json.add("error", errJson);
                    this.printJson(response, json.toString());
                    return ;
                }
                if (code == null || "".equals(code2)||code2 == null || "".equals(code2)|| ! code.equals(code2)){
                    errJson.addProperty("code", "验证码不正确!");
                    json.add("error", errJson);
                    this.printJson(response, json.toString());
                    return ;
                }
            }
            
            if ((province == null || "".equals(province))&& null != settingEntity.getProvinceZoneId()) {
                province = String.valueOf(settingEntity.getProvinceZoneId());
            }
            
            if (province == null || "".equals(province)) {
                province = "0" ;
            }
            if (city == null || "".equals(city)) {
                city = "0" ;
            }
            String zoneId = county;
            if (zoneId == null || "".equals(zoneId) || "0".equals(zoneId))
                zoneId = city;
            if (zoneId == null || "".equals(zoneId) || "0".equals(zoneId))
                zoneId = province;
            if (zoneId == null || "".equals(zoneId) || "0".equals(zoneId))
                zoneId = "0";
            if (propertyId == null || "".equals(propertyId))
                propertyId = "0";
            if (county == null || "".equals(county))
                county = "0";
            if (propertyId == null || "".equals(propertyId))
                propertyId = "0";
            if (countryId == null || "".equals(countryId)) {
                countryId = "0" ;
            }
 
            customerEntity.setCltName(cltName);
            customerEntity.setCltType(settingEntity.getCltType());
            customerEntity.setSourceInfo(sourceInfo);
            customerEntity.setCountryZoneId(countryId == null ? null : Integer.parseInt(countryId));
            customerEntity.setProvinceZoneId(province == null ? null : Integer.parseInt(province));
            customerEntity.setCityZoneId(city == null ? null : Integer.parseInt(city));
            customerEntity.setCountyZoneId(county == null ? null : Integer.parseInt(county));
            customerEntity.setZoneId(Integer.parseInt(zoneId));
            customerEntity.setTel(telephone);
            customerEntity.setFax(fax);
            customerEntity.setEmail(email);
            customerEntity.setPropertyId(Integer.parseInt(propertyId));
            customerEntity.setBuilding(building);
            customerEntity.setUnit(unit);
            customerEntity.setHouse(house);
 
            customerEntity.setPostCode(postCode);
 
            customerEntity.setPassword(ChangePassword.getEncryptPassword(password) );
            customerEntity.setOrganization(organization);
            customerEntity.setAddress(address);
            customerEntity.setCccode(settingEntity.getCcCode());
            customerEntity.setWeiXinUserStatus(4);
            customerEntity.setGender(Integer.parseInt(gender));
            customerEntity.setCltType(cltType);
            customerEntity.setReferralsType(referralsType);
            customerEntity.setReferralsCode(referralsCode);
 
            customerEntity.setFromWx(wx);
            String openId = request.getParameter(SessionKey.WEIXIN_OPENID);  //从页面传递openid 
            if (openId == null || openId.equals("")) {
                openId = (String) session.getAttribute(SessionKey.WEIXIN_OPENID);   //从会话取openid
            }
            customerEntity.setOpenId(openId);
 
            Integer telCount = accountIfc.getCltCodeCountByCltTel(telephone);
            if (telCount != null && telCount.intValue() > 0) {
                errJson.addProperty("telephone", "该电话已经注册,请使用另一个号码!");
                json.add("error", errJson);
                this.printJson(response, json.toString());
                return;
            }
 
            if (email != null && !"".equals(email)) {
                Integer emailCount = accountIfc.getCltCodeCountByEmail(email);
                if (emailCount != null && emailCount.intValue() > 0) {
                    errJson.addProperty("email", "该电子邮件地址已经注册,请使用另一个邮箱地址!");
                    json.add("error", errJson);
                    this.printJson(response, json.toString());
                    return;
                }
            }
            if (openId != null && !"".equals(openId)) {
                Integer emailCount = accountIfc.getCltCodeCountByOpenId(openId);
                if (emailCount != null && emailCount.intValue() > 0) {
                    errJson.addProperty("warning", "当前微信用户已经注册,请刷新页面!");
                    json.add("error", errJson);
                    this.printJson(response, json.toString());
                    return;
                }
            }
 
            // 保存到数据库
            customerEntity = accountIfc.saveNewCustomer(customerEntity, hrCode);
 
            // 1.首先判断是否微信过来的链接,如果是,则使用 CorpId 取数据源
            if (corpId != null && !"".equals(corpId) && wx != null && "1".equals(wx)) { // 仅限于微信号企业版
                session.setAttribute(SessionKey.USERCODE, customerEntity.getCltCode());
                session.setAttribute(SessionKey.USERNAME, customerEntity.getCltName());
            }
 
            session.setAttribute(SettingKey.CLTCODE, customerEntity.getCltCode());
            session.setAttribute(SettingKey.CLTNAME, customerEntity.getCltName());
            session.setAttribute(SettingKey.CLTTEL, customerEntity.getTel());
            session.setAttribute(SettingKey.CLTEMAIL, customerEntity.getEmail());
            session.setAttribute(SettingKey.COUNTRY, customerEntity.getCountryZoneId());
            session.setAttribute(SettingKey.PROVINCE, customerEntity.getProvinceZoneId());
            session.setAttribute(SettingKey.POSTCODE, customerEntity.getPostCode());
 
            // 给匿名购物车设置“主人”,这个“主人”就是已经登录的客户名字
            accountIfc.updateCartOwner(hrCode, sessionId, customerEntity.getCltCode(), customerEntity.getCltName(),openId);
 
        
            // 同步微信企业号用户
            boolean isAddWxCpUser = false;
            if (settingEntity.isSyncWxCpUser()) {
                //DataSourceEntity corpEntity = MultiDataSource.getDataSourceMap(dbId);
                //DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
                
                if (corpEntity != null && corpEntity.getCorpId() != null && !"".equals(corpEntity.getCorpId())) {
                    //user.setInit(wxCpConfigStorage, wxCpService, corpEntity);
                    isAddWxCpUser = user.newUser(response, customerEntity.getCltCode(), customerEntity.getCltName(),openWxCpService);
                }
            } // 同步结束
    
            String redirect = (session.getAttribute(SettingKey.REDIRECT)==null?null:(String)session.getAttribute(SettingKey.REDIRECT));
            if (redirect != null && !"".equals(redirect)) {
                String str = (redirect.indexOf("?") > 0?"&":"?") ;
                if (!(redirect.indexOf(SessionKey.WEIXIN_FROM+"=") > 0 
                        && (redirect.indexOf(SessionKey.WEIXIN_CORPID+"=") > 0 
                            ||redirect.indexOf(SessionKey.WEIXIN_APPID+"=") > 0 ))) {
                    redirect += (wxQueryString == null||"".equals(wxQueryString)?"":str + wxQueryString) ;
                }
                
                
                json.addProperty("redirect",redirect) ;
            }else if (reg != null && reg.equals("1")) {
                String url = "";
                if (isAddWxCpUser)
                    url = hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "account/regsuccess.jsp?sync=1"
                            + (wxQueryString == null || "".equals(wxQueryString) ? "" : "&" + wxQueryString);
                else
                    url = hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "account/regsuccess.jsp"
                            + (wxQueryString == null || "".equals(wxQueryString) ? "" : "?" + wxQueryString);
                json.addProperty("redirect", url);
                json.addProperty("status", "success");
            }
            
            //删除验证码
            redisTemplate.delete(VerificationCodes.getCodeKey( telephone)) ;
 
            
            this.printJson(response, json.toString());
            return;
        } catch (DataAccessException e) {
            e.printStackTrace();
            errJson.addProperty("warning",  (e.getCause()!=null?e.getCause().getMessage(): e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
        } catch (Exception 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();
        }
    }
    
    @RequestMapping(params = "m=accountInfo")
    public void accountInfo(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID);
        String cltCode = (session.getAttribute(SettingKey.CLTCODE) == null ? "" : (String)session.getAttribute(SettingKey.CLTCODE)); 
        String cltName = (session.getAttribute(SettingKey.CLTNAME) == null ? "" : (String)session.getAttribute(SettingKey.CLTNAME)) ;
        String userCode = (session.getAttribute(SessionKey.USERCODE) == null ? "" 
                : (String)session.getAttribute(SessionKey.USERCODE));
        String openId = (session.getAttribute(SessionKey.WEIXIN_OPENID) == null ? "" 
                : (String) session.getAttribute(SessionKey.WEIXIN_OPENID) ) ;
        String nickName = (session.getAttribute(SessionKey.WEIXIN_NICKNAME) == null?""
                :(String)session.getAttribute(SessionKey.WEIXIN_NICKNAME)) ;
        String headImgUrl = (session.getAttribute(SessionKey.WEIXIN_HEADIMGURL) == null?""
                :(String)session.getAttribute(SessionKey.WEIXIN_HEADIMGURL)) ;
        
        
        String memberLevelDesc = (session.getAttribute(SettingKey.MEMBERLEVELDESC) == null?"":(String)session.getAttribute(SettingKey.MEMBERLEVELDESC));
        String memberLevel = (session.getAttribute(SettingKey.MEMBERLEVEL) == null?"":(String)session.getAttribute(SettingKey.MEMBERLEVEL));
        String fromUserId = request.getParameter(SettingKey.FROMUSERID) ;
        
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        DecimalFormat df = new DecimalFormat("0.00") ;
        
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        SettingEntity settingEntity = null;
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());// 切换数据源
            settingEntity = settingIfc.getSettingEntity();
            MemberInfoEntity memberInfoEntity =wxPayIfc.getMemberInfoByMemberLevel(memberLevel);
            JsonObject userInfo = new JsonObject();        
            
            MyWxMpUser myWxMpUser= maUserIfc.getUser(openId) ;  //再次取数 
            int userCount = mpUserIfc.getUserCountByReferralsCode(openId);
            
            CurrencyEntity currencyEntity = currencyIfc.getUserCurrency(userCode);
            String currency = currencyEntity.getCurrency();
            
            BalanceEntity balanceEntity = balanceIfc.getBalance(currency, cltCode) ;
            
            String referralsName = (myWxMpUser!=null && myWxMpUser.getReferralsName()!=null&&!"".equals(myWxMpUser.getReferralsName())?myWxMpUser.getReferralsName(): (settingEntity.getMetaTitle()!=null&& !"".equals(settingEntity.getMetaTitle())?settingEntity.getMetaTitle():"无分享人"));
            userInfo.addProperty("CltCode", cltCode);   //客户编号
            userInfo.addProperty("CltName", cltName);   //客户名称
            userInfo.addProperty("NickName", nickName);  //昵称
            userInfo.addProperty("HeadImgUrl", headImgUrl);   //头像
            
            userInfo.addProperty("UserCount", userCount);    //我的关注
            
            //SubscribeTime 是unix timestamp时间戳 ,因此要转换为 日期时间 Date类型
            Date subscribeTime = (myWxMpUser!=null&&myWxMpUser.getSubscribeTime()!=null?new Date(myWxMpUser.getSubscribeTime() * 1000L):null) ;
            userInfo.addProperty("SubscribeTime",subscribeTime!=null?sdf.format(subscribeTime):"") ;   //关注时间
            
            userInfo.addProperty("MemberLevel", memberLevel);   //会员等级 
            userInfo.addProperty("MemberLevelDesc", memberLevelDesc);   //会员等描述 
            userInfo.addProperty("MemberDocCode", memberInfoEntity!=null?memberInfoEntity.getDocCode():"");   //显示 会员描述 链接用 
            userInfo.addProperty("AccumulationAmount",balanceEntity != null?df.format(balanceEntity.getAccumulationAmount()):"0" ) ;  //累计积分
            userInfo.addProperty("NotWithdrawAmount",balanceEntity != null?df.format(0.0-balanceEntity.getBalance()):"0");   //未提现积分
            userInfo.addProperty("WithdrawAmount",balanceEntity != null?df.format((0.0 - balanceEntity.getBalance()
                     * (1.0 - settingEntity.getWithdrawTaxRate()))):"0");  //可提现积分
            
            //取出 我的客服人员名称 
            MyWxCpUser myWxCpUser = erpUserIfc.getWorkAppUser( fromUserId) ;
            String fromUserName = (myWxCpUser != null?myWxCpUser.getName():"") ;
            userInfo.addProperty("FromUserId", fromUserId) ;
            userInfo.addProperty("FromUserName", fromUserName) ;
            if (settingEntity.isStartupLeagueShopCcCode()) {
                userInfo.addProperty("ReferralsName", fromUserName);   //介绍人
            }else {
                userInfo.addProperty("ReferralsName", referralsName);   //介绍人
            }
            
            json.add("UserInfo", userInfo);
            //errJson.addProperty("warning","");   //不要显示空白错误提示,会导致页面有空白提示 
            //json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
            
        } catch (DataAccessException e) {
            e.printStackTrace();
            errJson.addProperty("warning",(e.getCause()!=null?e.getCause().getMessage(): e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
        } catch (Exception 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();
        }
    }
    
    @RequestMapping(params = "m=updateprofile")
    public void updateProfile(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID);
 
        String hostUrl = SettingKey.getHostUrl(request);
        // 将微信corpid组装成url
        String wxQueryString = SettingKey.getQueryStringByWx(request);
        boolean isMoblieBrowser = SettingKey.isMoblieBrowser(request);
 
        String cltCode = (session.getAttribute(SettingKey.CLTCODE) == null ? ""
                : (String) session.getAttribute(SettingKey.CLTCODE));
        String cltName = request.getParameter("cltname") == null ? "" : request.getParameter("cltname");
        String telephone = request.getParameter("telephone") == null ? "" : request.getParameter("telephone");
        String fax = request.getParameter("fax") == null ? "" : request.getParameter("fax");
        String email = request.getParameter("email") == null ? "" : request.getParameter("email");
        String sourceInfo = request.getParameter("sourceinfo") == null ? "" : request.getParameter("sourceinfo");
        
        String referralsType = request.getParameter("referralstype") == null ? "" : request.getParameter("referralstype");
        String referralsCode = request.getParameter("referralscode") == null ? "" : request.getParameter("referralscode");
        String code = request.getParameter("code") == null ? "" : request.getParameter("code");
 
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        if (cltName == null || "".equals(cltName) || cltName.length() < 2) {
            errJson.addProperty("cltname", "客户姓名必须在2 至 32 字符之间!");
        }
 
        SettingEntity settingEntity = null;
        Integer ret = null;
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());
            settingEntity = settingIfc.getSettingEntity();
            if (settingEntity.isShowEmail() && settingEntity.isRequiredEmail()) {
                if (email == null || "".equals(email)) {
                    errJson.addProperty("email", "电子邮箱 必须输入!");
                }
                if (email != null && !"".equals(email) && !SettingKey.emailFormat(email)) {
                    errJson.addProperty("email", "电子邮箱 格式错误,请重新输入!");
                }
            }
            if (settingEntity.isShowSourceInfo() && settingEntity.isRequiredSourceInfo()) {
                if (sourceInfo == null || "".equals(sourceInfo)) {
                    errJson.addProperty("sourceinfo", "客户来源 必须输入!");
                }
            }
            if (settingEntity.isShowReferralsCode() && settingEntity.isRequiredReferralsCode()) {
                if (referralsType == null || "".equals(referralsType)) {
                    errJson.addProperty("referralstype", "推荐人所属 必须输入!");
                }
            }
            
            if (settingEntity.isShowReferralsCode() && settingEntity.isRequiredReferralsCode()) {
                if (referralsCode == null || "".equals(referralsCode)) {
                    errJson.addProperty("referralscode", "推荐人 必须输入!");
                }
            }
            if (settingEntity.isShowFax() && settingEntity.isRequiredFax()) {
                if (fax == null || "".equals(fax)) {
                    errJson.addProperty("fax", "传真号码 必须输入!");
                }
            }
            
            if (settingEntity.isShowTel() && settingEntity.isRequiredTel()) {
                if (telephone == null || "".equals(telephone)) {
                    errJson.addProperty("telephone", "手机号码 必须输入!");
                }
            }
 
            Set<Entry<String, JsonElement>> it = errJson.entrySet();
            if (!it.isEmpty() && it.size() != 0) {
                json.add("error", errJson);
                this.printJson(response, json.toString());
                return;
            }
            
            //验证码 
            String code2 = "" ;
            if (settingEntity.isShowTel() && settingEntity.isRequiredTel()) {
                try {
                    code2 = (String) redisTemplate.opsForValue().get(VerificationCodes.getCodeKey( telephone)) ;
                }catch(Exception e) {
                    e.printStackTrace();
                    errJson.addProperty(SettingKey.CLTTEL, e.getMessage());
                    json.add("error", errJson);
                    this.printJson(response, json.toString());
                    return ;
                }
                if (code == null || "".equals(code2)||code2 == null || "".equals(code2)|| ! code.equals(code2)){
                    errJson.addProperty("code", "验证码不正确!");
                    json.add("error", errJson);
                    this.printJson(response, json.toString());
                    return ;
                }
            }
 
            // 保存到数据库
            ret = accountIfc.updateCustomerProfile(cltCode, cltName, telephone, sourceInfo, referralsType, referralsCode, email, fax);
    
            if (ret != null && ret.equals(1)) {
 
                session.setAttribute(SettingKey.CLTNAME, cltName);
                session.setAttribute(SettingKey.CLTTEL, telephone);
                session.setAttribute(SettingKey.CLTEMAIL, email);
 
                SettingKey.setCartHintMsg(session, "成功 您的账户已更新。");
                json.addProperty("redirect", hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "account/account.jsp"
                        + (wxQueryString == null || "".equals(wxQueryString) ? "" : "?" + wxQueryString));
            }
            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();
        }
    
    }
 
    @RequestMapping(params = "m=updateaddress")
    public void updateAddress(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        // String dbId = (String)
        // session.getAttribute(SessionKey.SHOPPING_DBID);
 
        String hostUrl = SettingKey.getHostUrl(request);
        // 将微信corpid组装成url
        String wxQueryString = SettingKey.getQueryStringByWx(request);
        boolean isMoblieBrowser = SettingKey.isMoblieBrowser(request);
 
        String seq = (request.getParameter(SettingKey.PAYMENTADDRESS) == null 
                ||request.getParameter(SettingKey.PAYMENTADDRESS).equals("") ? "0" : request.getParameter(SettingKey.PAYMENTADDRESS));
        String ac = (request.getParameter("ac") == null ? "new" : request.getParameter("ac"));
        String returnTo= request.getParameter("returnto");  //返回到哪个页面,
 
        String cltCode = (session.getAttribute(SettingKey.CLTCODE) == null ? ""
                : (String) session.getAttribute(SettingKey.CLTCODE));
    
        String linkMan = request.getParameter("linkman") == null ? "" : request.getParameter("linkman");  //联系人 
        String telephone = request.getParameter("telephone") == null ? "" : request.getParameter("telephone");
        String organization = request.getParameter("organization") == null ? "" : request.getParameter("organization");
        String countryId = request.getParameter("country_id") == null ? "0" : request.getParameter("country_id"); // 国家
        String countryName = request.getParameter("countryname") == null ? "" : request.getParameter("countryname"); // 国家
        String provinceZoneId = request.getParameter("zone_id") == null ? "0" : request.getParameter("zone_id"); // 省级
        String provinceName = request.getParameter("provincename") == null ? "" : request.getParameter("provincename"); // 省级
        String cityZoneId = request.getParameter("city") == null ? "0" : request.getParameter("city"); // 城市
        String cityName = request.getParameter("cityname") == null ? "" : request.getParameter("cityname"); // 城市
        String countyZoneId = request.getParameter("county") == null ? "0" : request.getParameter("county"); // 区/县
        String countyName = request.getParameter("countyname") == null ? "" : request.getParameter("countyname"); // 区/县
        String propertyId = request.getParameter("propertyid") == null ? "0" : request.getParameter("propertyid"); // 小区
        String building = request.getParameter("building") == null ? "" : request.getParameter("building");
        String unit = request.getParameter("unit") == null ? "" : request.getParameter("unit");
        String house = request.getParameter("house") == null ? "" : request.getParameter("house");
        String postCode = request.getParameter("postcode") == null ? "" : request.getParameter("postcode");
 
        String addressName = request.getParameter("addressname") == null ? "" : request.getParameter("addressname");
        String address = request.getParameter("address") == null ? "" : request.getParameter("address");
        String street = request.getParameter("street") == null ? "" : request.getParameter("street");
        String longitude = request.getParameter("longitude") == null ? "" :request.getParameter("longitude") ;
        String latitude = request.getParameter("latitude") == null ? "" :request.getParameter("latitude") ;
 
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        if (cltCode == null || "".equals(cltCode)) {
            SettingKey.setCartHintMsg(session, "请先登录,再修改地址!");
            json.addProperty("redirect", hostUrl + "/shopping/account/login.jsp"
                    + (wxQueryString == null || "".equals(wxQueryString) ? "" : "?" + wxQueryString));
            return;
        }
 
        if (linkMan == null || "".equals(linkMan) || linkMan.length() < 2) {
            errJson.addProperty("linkman", "联系人姓名必须在2 至 32 字符之间!");
            errJson.addProperty("warning", "联系人姓名必须在2 至 32 字符之间!");
        }
 
        SettingEntity settingEntity = null;
        Integer ret = null;
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
            settingEntity = settingIfc.getSettingEntity();
            if (settingEntity.isShowOrganization() && settingEntity.isRequiredOrganization()) {
                if (organization == null || "".equals(organization)) {
                    errJson.addProperty("organization", "公司名称 必须输入!");
                    errJson.addProperty("warning","公司名称 必须输入!");
                }
            }
            if (settingEntity.isShowCountryId() && settingEntity.isRequiredCountryId()) {
                if (countryId == null || "".equals(countryId)) {
                    errJson.addProperty("country_id", "国家 必须输入!");
                    errJson.addProperty("warning", "国家 必须输入!");
                }
            }
            if (settingEntity.isShowProvinceZoneId() && settingEntity.isRequiredProvinceZoneId()) {
                if (provinceZoneId == null || "".equals(provinceZoneId)) {
                    errJson.addProperty("zone_id", "省份 必须输入!");
                    errJson.addProperty("warning", "省份 必须输入!");
                }
            }
            if (settingEntity.isShowCityZoneId() && settingEntity.isRequiredCityZoneId()) {
                if (cityZoneId == null || "".equals(cityZoneId)) {
                    errJson.addProperty("city", "城市 必须输入!");
                    errJson.addProperty("warning", "城市 必须输入!");
                }
            }
            if (settingEntity.isShowCountyZoneId() && settingEntity.isRequiredCountyZoneId()) {
                if (countyZoneId == null || "".equals(countyZoneId)) {
                    errJson.addProperty("county", "区/县 必须输入!");
                    errJson.addProperty("warning", "区/县 必须输入!");
                }
            }
            if (settingEntity.isShowPropertyId() && settingEntity.isRequiredPropertyId()) {
                if (propertyId == null || "".equals(propertyId)) {
                    errJson.addProperty("propertyid", "小区名称 必须输入!");
                    errJson.addProperty("warning", "小区名称 必须输入!");
                }
                if (building == null || "".equals(building)) {
                    errJson.addProperty("building", "楼号 必须输入!");
                    errJson.addProperty("warning", "楼号 必须输入!");
                }
            }
            if (settingEntity.isShowAddress() && settingEntity.isRequiredAddress()) {
                if (street == null || "".equals(street)) {
                    errJson.addProperty("address", "地址 必须输入!");
                    errJson.addProperty("warning", "地址 必须输入!");
                }
            }
            if (settingEntity.isShowPostcode() && settingEntity.isRequiredPostcode()) {
                if (postCode == null || "".equals(postCode)) {
                    errJson.addProperty("postcode", "邮编 必须输入!");
                    errJson.addProperty("warning", "邮编 必须输入!");
                }
            }
 
            Set<Entry<String, JsonElement>> it = errJson.entrySet();
            if (!it.isEmpty() && it.size() != 0) {
                json.add("error", errJson);
                this.printJson(response, json.toString());
                return;
            }
            if (countryId == null || "".equals(countryId)) {
                countryId = "0";
            }
            if (provinceZoneId == null || "".equals(provinceZoneId)) {
                provinceZoneId = "0";
            }
            if (cityZoneId == null || "".equals(cityZoneId)) {
                cityZoneId = "0";
            }
            if (countyZoneId == null || "".equals(countyZoneId)) {
                countyZoneId = "0";
            }
            if (propertyId == null || "".equals(propertyId)) {
                propertyId = "0";
            }
 
            AddressEntity addressEntity = new AddressEntity() ;
            addressEntity.setSeq(Integer.parseInt(seq));
            addressEntity.setCltCode(cltCode);
            addressEntity.setLinkMan(linkMan);
            addressEntity.setTelephone(telephone);
            addressEntity.setCountryId(Integer.parseInt(countryId));
            addressEntity.setCountryName(countryName);
            addressEntity.setProvinceZoneId(Integer.parseInt(provinceZoneId));
            addressEntity.setProvinceName(provinceName);
            addressEntity.setCityZoneId(Integer.parseInt(cityZoneId));
            addressEntity.setCityName(cityName);
            addressEntity.setCountyZoneId(Integer.parseInt(countyZoneId));
            addressEntity.setCountyName(countyName);
            addressEntity.setPropertyId(Integer.parseInt(propertyId));
            addressEntity.setBuilding(building);
            addressEntity.setUnit(unit);
            addressEntity.setHouse(house);
            addressEntity.setPostCode(postCode);
            addressEntity.setOrganization(organization);
            addressEntity.setAddressName(addressName);
            addressEntity.setAddress(address);
            addressEntity.setStreet(street);
            addressEntity.setLongitude(longitude);
            addressEntity.setLatitude(latitude);
            
            if (ac != null && "edit".equals(ac)) {
                // 更新 update
                ret = addressIfc.updateAddress(addressEntity);
            } else {
                // 新增 new
                ret = addressIfc.newAddress(addressEntity);
            }
            
            if (ret != null ) {
                SettingKey.setCartHintMsg(session, "成功 您的地址已更新。");
                String redirect = hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "account/address.jsp"
                        + (wxQueryString == null || "".equals(wxQueryString) ? "" : "?" + wxQueryString) ;
                if (returnTo!= null&& "checkoutnew".equals(returnTo)) {
                    redirect = hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "checkoutnew/address.jsp"
                            + (wxQueryString == null || "".equals(wxQueryString) ? "" : "?" + wxQueryString) ;
                }else if (returnTo!= null&& "confirm".equals(returnTo)) {
                    if (ret != null) {
                        session.setAttribute(SettingKey.PAYMENTADDRESS,ret.toString()) ;   //保存已经选择的地址
                    }
                    redirect = hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "checkoutnew/"
                            + (wxQueryString == null || "".equals(wxQueryString) ? "" : "?" + wxQueryString) ;
                }else if(returnTo!= null&&"membercheckout".equals(returnTo)){
                    String memberInfoDocCode=request.getParameter(SettingKey.DOCCODE)==null?"":request.getParameter(SettingKey.DOCCODE);
                    redirect = hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "checkoutnew/membercheckout.jsp?"
                            + SettingKey.DOCCODE + "=" + (memberInfoDocCode!=null?memberInfoDocCode:"")
                            + (wxQueryString == null || "".equals(wxQueryString) ? "" : "&" + wxQueryString) ;
                }
                
                json.addProperty("redirect", redirect);
            }
            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();
        }
 
    }
 
    @RequestMapping(params = "m=deladdress")
    public void delAddress(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID);
 
        String hostUrl = SettingKey.getHostUrl(request);
        // 将微信corpid组装成url
        String wxQueryString = SettingKey.getQueryStringByWx(request);
        boolean isMoblieBrowser = SettingKey.isMoblieBrowser(request);
 
        String seq = (request.getParameter(SettingKey.PAYMENTADDRESS) == null ? "0" : request.getParameter(SettingKey.PAYMENTADDRESS));
        String cltCode = (session.getAttribute(SettingKey.CLTCODE) == null ? ""
                : (String) session.getAttribute(SettingKey.CLTCODE));
        
        String returnTo= request.getParameter("returnto");  //返回到哪个页面,
 
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        if (cltCode == null || "".equals(cltCode)) {
            SettingKey.setCartHintMsg(session, "请先登录,再删除地址!");
            this.print(response,
                    "<script type=\"text/javascript\">window.location=\"" + hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "account/login.jsp"
                            + (wxQueryString == null || "".equals(wxQueryString) ? "" : "?" + wxQueryString) + "\";"
                            + "</script>");
            return;
        }
 
        Integer ret = null;
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());// 切换数据源
            // 保存到数据库
            ret = addressIfc.delCustomerAddress(Integer.parseInt(seq), cltCode);
            
            //如果删除的地址是已经选择的,则要把选择的标记清空 
            String selectedSeq = (String)session.getAttribute(SettingKey.PAYMENTADDRESS) ;   //已经选择的地址
            if (seq!= null && seq.equals(selectedSeq)) {
                session.removeAttribute(SettingKey.PAYMENTADDRESS);
            }
            
            if (ret != null && ret.equals(1)) {
                SettingKey.setCartHintMsg(session, "成功 您的地址已删除。");
                String redirect = hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "account/address.jsp"
                        + (wxQueryString == null || "".equals(wxQueryString) ? "" : "?" + wxQueryString);
                if (returnTo!= null && "checkoutnew".equals(returnTo)) {
                    redirect = hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "checkoutnew/address.jsp"
                            + (wxQueryString == null || "".equals(wxQueryString) ? "" : "?" + wxQueryString);
                }
                json.addProperty("redirect", redirect);
            }
            String redirect = "<script type=\"text/javascript\">window.location=\"" + hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "account/address.jsp"
                    + (wxQueryString == null || "".equals(wxQueryString) ? "" : "?" + wxQueryString) + "\";"
                    + "</script>";
            if (returnTo!= null && "checkoutnew".equals(returnTo)) {
                redirect = "<script type=\"text/javascript\">window.location=\"" + hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "checkoutnew/address.jsp"
                        + (wxQueryString == null || "".equals(wxQueryString) ? "" : "?" + wxQueryString) + "\";"
                        + "</script>";
            }
            this.print(response,redirect);
            return;
 
        } catch (DataAccessException e) {
            errJson.addProperty("warning", e.getCause().getMessage());
            json.add("error", errJson);
 
            SettingKey.setCartHintMsg(session, e.getCause().getMessage());
            // this.printJson(response, json.toString());
            
            String redirect = "<script type=\"text/javascript\">window.location=\"" + hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "account/address.jsp"
                    + (wxQueryString == null || "".equals(wxQueryString) ? "" : "?" + wxQueryString) + "\";"
                    + "</script>";
            if (returnTo!= null && "checkoutnew".equals(returnTo)) {
                redirect = "<script type=\"text/javascript\">window.location=\"" + hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "checkoutnew/address.jsp"
                        + (wxQueryString == null || "".equals(wxQueryString) ? "" : "&" + wxQueryString) + "\";"
                        + "</script>";
            }else if(returnTo!= null&&"membercheckout".equals(returnTo)){
                String memberInfoDocCode=request.getParameter(SettingKey.DOCCODE)==null?"":request.getParameter(SettingKey.DOCCODE);
                redirect = hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "checkoutnew/membercheckout.jsp?"
                        + SettingKey.DOCCODE + "=" + (memberInfoDocCode!=null?memberInfoDocCode:"")
                        + (wxQueryString == null || "".equals(wxQueryString) ? "" : "&" + wxQueryString) ;
            }
            this.print(response,redirect);
            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();
        }
    }
    
    @RequestMapping(params = "m=getAddressList")
    public void getAddressList(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session=request.getSession();
        //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID);
        String cltCode = (session.getAttribute(SettingKey.CLTCODE)==null?"":(String)session.getAttribute(SettingKey.CLTCODE));
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        
        String seq = session.getAttribute(SettingKey.PAYMENTADDRESS) == null?"0":(String) session.getAttribute(SettingKey.PAYMENTADDRESS) ;
        
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
            JsonArray addressListJsonArray = new JsonArray();
            List<AddressEntity> addressList = addressIfc.getAddress(cltCode);
            for (int i = 0 ; addressList != null && i < addressList.size();i++) {
                 AddressEntity addressEntity = addressList.get(i) ;
                 JsonObject addressEntityJsonObject = new JsonObject();
                 
                 addressEntityJsonObject.addProperty("Seq", addressEntity.getSeq());
                addressEntityJsonObject.addProperty("LinkMan", addressEntity.getLinkMan()!= null&& ! "".equals(addressEntity.getLinkMan())?addressEntity.getLinkMan(): addressEntity.getCltName());
                addressEntityJsonObject.addProperty("Telephone", addressEntity.getTelephone()!= null&& ! "".equals(addressEntity.getTelephone())?addressEntity.getTelephone()  : "");
                addressEntityJsonObject.addProperty("Organization", addressEntity.getOrganization());
                addressEntityJsonObject.addProperty("AddressName",addressEntity.getAddressName() );
                addressEntityJsonObject.addProperty("Address",addressEntity.getAddress() );
                addressEntityJsonObject.addProperty("Street",addressEntity.getStreet() );
                addressEntityJsonObject.addProperty("isSelected",seq.equals(addressEntity.getSeq().toString()));
                addressEntityJsonObject.addProperty("Longitude",addressEntity.getLongitude());
                addressEntityJsonObject.addProperty("Latitude",addressEntity.getLatitude());
                addressEntityJsonObject.addProperty("CountryId",addressEntity.getCountryId());
                addressEntityJsonObject.addProperty("CountryName",addressEntity.getCountryName());
                addressEntityJsonObject.addProperty("ProvinceZoneId",addressEntity.getProvinceZoneId());
                addressEntityJsonObject.addProperty("ProvinceName",addressEntity.getProvinceName());
                addressEntityJsonObject.addProperty("CityZoneId",addressEntity.getCityZoneId());
                addressEntityJsonObject.addProperty("CityName",addressEntity.getCityName());
                addressEntityJsonObject.addProperty("CountyZoneId",addressEntity.getCountyZoneId());
                addressEntityJsonObject.addProperty("CountyName",addressEntity.getCountyName());
                
                addressListJsonArray.add(addressEntityJsonObject);
            }
            
            json.add("list", addressListJsonArray);
            
            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();
        }
    }
    
    @RequestMapping(params = "m=getAddressBySeq")
    public void getAddressBySeq(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session=request.getSession();
        //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID);
        String cltCode = (session.getAttribute(SettingKey.CLTCODE)==null?"":(String)session.getAttribute(SettingKey.CLTCODE));
        String seq = (request.getParameter(SettingKey.PAYMENTADDRESS)== null?"0":request.getParameter(SettingKey.PAYMENTADDRESS)) ;
        String ac = (request.getParameter("ac")== null?"new":request.getParameter("ac")) ;
        
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        SettingEntity settingEntity = null;
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
            settingEntity = settingIfc.getSettingEntity();
            
            //JsonArray addressListJsonArray = new JsonArray();
            JsonObject addressJsonObject = new JsonObject();
            
            AddressEntity addressEntity = addressIfc.getAddressByAddressId(Integer.parseInt(seq), cltCode) ;
            Integer defaultCountryId = null ;  //缺省国家
            String defaultCountryName = null ;  //缺省国家
            Integer defaultProvinceZoneId = null ;  //缺省省份
            String defaultProvinceName = null ;  //缺省省份
            Integer defaultCityZoneId = null ;  //缺省城市
            String defaultCityName = null ;  //缺省城市
            Integer defaultCountyZoneId = null ; //缺省区/县
            String defaultCountyName = null ; //缺省区/县
            Integer defaultPropertyID = null ; //楼盘编号/小区名称
            if (  ac != null && "edit".equals(ac) && addressEntity != null )  {
                defaultCountryId = addressEntity.getCountryId() ;
                defaultCountryName = addressEntity.getCountryName();
                defaultProvinceZoneId = addressEntity.getProvinceZoneId();
                defaultProvinceName = addressEntity.getProvinceName();
                defaultCityZoneId = addressEntity.getCityZoneId();
                defaultCityName = addressEntity.getCityName();
                defaultCountyZoneId = addressEntity.getCountyZoneId();
                defaultCountyName = addressEntity.getCountyName();
                defaultPropertyID = addressEntity.getPropertyId() ; 
                
            } else { 
                defaultCountryId = settingEntity.getCountryId() ;
                defaultCountryName = settingEntity.getCountryName();
                defaultProvinceZoneId = settingEntity.getProvinceZoneId();
                defaultProvinceName = settingEntity.getProvinceName();
                defaultCityZoneId = settingEntity.getCityZoneId();
                defaultCityName = settingEntity.getCityName();
            }
            addressJsonObject.addProperty("seq", seq);   //seq
            addressJsonObject.addProperty("linkman", ac != null && "edit".equals(ac)?addressEntity.getLinkMan():"");  //联系人姓名
            addressJsonObject.addProperty("telephone", ac != null && "edit".equals(ac)?addressEntity.getTelephone():"" );  //联系人电话
            addressJsonObject.addProperty("organization", ac != null && "edit".equals(ac)?addressEntity.getOrganization():"");  //公司名称
            addressJsonObject.addProperty("country_id", defaultCountryId);   //国家
            addressJsonObject.addProperty("countryname", defaultCountryName);   //国家
            addressJsonObject.addProperty("zone_id", defaultProvinceZoneId);   //省份
            addressJsonObject.addProperty("provincename", defaultProvinceName);   //省份
            addressJsonObject.addProperty("city", defaultCityZoneId);   //城市
            addressJsonObject.addProperty("cityname", defaultCityName);   //城市
            addressJsonObject.addProperty("county", defaultCountyZoneId);   //区/县
            addressJsonObject.addProperty("countyname", defaultCountyName);   //区/县
            addressJsonObject.addProperty("propertyid",ac != null && "edit".equals(ac)? defaultPropertyID:0);   //小区ID
            addressJsonObject.addProperty("propertyname", ac != null && "edit".equals(ac)?addressEntity.getPropertyName():"");  //小区名称
            addressJsonObject.addProperty("building", ac != null && "edit".equals(ac)?addressEntity.getBuilding():"");  //楼号
            addressJsonObject.addProperty("unit", ac != null && "edit".equals(ac)?addressEntity.getUnit():"");   //单元号
            addressJsonObject.addProperty("house", ac != null && "edit".equals(ac)?addressEntity.getHouse():"");   //房屋号
            addressJsonObject.addProperty("street", ac != null && "edit".equals(ac)?addressEntity.getStreet():"");   //街道地址
            addressJsonObject.addProperty("addressname", ac != null && "edit".equals(ac)?addressEntity.getAddressName():"");  //地址
            addressJsonObject.addProperty("address", ac != null && "edit".equals(ac)?addressEntity.getAddress():"");  //地址
            addressJsonObject.addProperty("postcode", ac != null && "edit".equals(ac)?addressEntity.getPostCode():"");  //邮编
            addressJsonObject.addProperty("longitude", ac != null && "edit".equals(ac)?addressEntity.getLongitude():"");  //地理经度(x)
            addressJsonObject.addProperty("latitude", ac != null && "edit".equals(ac)?addressEntity.getLatitude():"");  //地理纬度(y)
            json.add("list", addressJsonObject);
            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();
        }
    }    
        
    
    
    @RequestMapping(params = "m=selectaddress")
    public void selectAddress(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID);
 
        String hostUrl = SettingKey.getHostUrl(request);
        String cltCode = (session.getAttribute(SettingKey.CLTCODE) == null ? ""
                : (String) session.getAttribute(SettingKey.CLTCODE));
        
        // 将微信corpid组装成url
        String wxQueryString = SettingKey.getQueryStringByWx(request);
        boolean isMoblieBrowser = SettingKey.isMoblieBrowser(request);
        String returnTo= request.getParameter("returnto");  //返回到哪个页面: checkoutnew   ,  account
        
        String seq = (request.getParameter(SettingKey.PAYMENTADDRESS) == null ? "0" : request.getParameter(SettingKey.PAYMENTADDRESS));
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        boolean found ;
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());// 切换数据源
            // 保存到数据库
            found = accountIfc.hasExistsAddress(cltCode,Integer.parseInt(seq));
            
            if (found) {
                if (seq != null) {
                    session.setAttribute(SettingKey.PAYMENTADDRESS,seq.toString()) ;   //保存已经选择的地址
                }
                String redirect =  hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "checkoutnew/"
                        + (wxQueryString == null || "".equals(wxQueryString) ? "" : "?" + wxQueryString);
                if (returnTo != null && "account".equals(returnTo)) {
                    redirect =  hostUrl +"/shopping/"+ (isMoblieBrowser?"mobile/":"") +"account/profile.jsp"+ (wxQueryString == null||"".equals(wxQueryString)?"":"?" + wxQueryString)  ;
                }else if(returnTo!= null&&"membercheckout".equals(returnTo)){
                    String memberInfoDocCode=request.getParameter(SettingKey.DOCCODE)==null?"":request.getParameter(SettingKey.DOCCODE);
                    redirect = hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "checkoutnew/membercheckout.jsp?"
                            + SettingKey.DOCCODE + "=" + (memberInfoDocCode!=null?memberInfoDocCode:"")
                            + (wxQueryString == null || "".equals(wxQueryString) ? "" : "&" + wxQueryString) ;
                }
                
                json.addProperty("redirect", redirect);
            } else {
                errJson.addProperty("warning", "地址不存在!");
                json.add("error", errJson);
            }
            
            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();
        }
        
    }
 
    @RequestMapping(params = "m=password")
    public void updatePassword(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID);
 
        String hostUrl = SettingKey.getHostUrl(request);
        // 将微信corpid组装成url
        String wxQueryString = SettingKey.getQueryStringByWx(request);
        boolean isMoblieBrowser = SettingKey.isMoblieBrowser(request);
 
        String cltCode = (session.getAttribute(SettingKey.CLTCODE) == null ? ""
                : (String) session.getAttribute(SettingKey.CLTCODE));
 
        String password = request.getParameter("password") == null ? "" : request.getParameter("password");
        String confirm = request.getParameter("confirm") == null ? "" : request.getParameter("confirm");
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        if (password == null || "".equals(password) || password.length() < 4)
            errJson.addProperty("password", "输入密码必须在4到20字符之间!");
        if (confirm == null || "".equals(confirm) || (password != null && confirm != null && !confirm.equals(password)))
            errJson.addProperty("confirm", "确认密码与输入密码不一致!");
        Set<Entry<String, JsonElement>> it = errJson.entrySet();
        if (!it.isEmpty() && it.size() != 0) {
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
        }
 
        Integer ret = null;
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());// 切换数据源
            // 保存到数据库
            ret = accountIfc.changePassword(cltCode, password);
            if (ret != null && ret.equals(1)) {
                SettingKey.setCartHintMsg(session, "成功 您的密码已更新。");
                json.addProperty("redirect", hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "account/account.jsp"
                        + (wxQueryString == null || "".equals(wxQueryString) ? "" : "?" + wxQueryString));
            }
            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();
        }
        
 
    }
}