xinyb
2024-07-18 80430d52f7716e3eb3e517dc96f94c4a6f9657bf
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
package com.yc.sdk.shopping.service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
import org.springframework.context.annotation.Scope;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;
 
import com.yc.action.grid.GridUtils;
import com.yc.sdk.shopping.entity.CountryEntity;
import com.yc.sdk.shopping.entity.RegionEntity;
import com.yc.service.BaseService;
 
@Service("CountryImpl")
@Scope("prototype")
public class CountryImpl extends BaseService implements CountryIfc {
 
    @Override
    public List<CountryEntity> getCountrys() {
        String sql = " set nocount on ; \n"
                + " select CountryId,CountryName,ISOCode2,ISOCode3,AddressFormat,PostcodeRequired \n"
                + " from t714030  \n"
                + " where isnull(Status,0) = 1 order by CountryName ; \n" ;
        List<Map<String,Object>> list = null ;
        try {
            list = this.jdbcTemplate.queryForList(sql) ;
        }catch(DataAccessException e ) {
            if (e instanceof EmptyResultDataAccessException){
                return null ;
            }else {
                //e.printStackTrace();
                 throw e;
            }
        }catch(Exception e){
            //e.printStackTrace();
            throw e;
        }
 
        List<CountryEntity> countryList = new ArrayList<CountryEntity>() ;
        for (int i = 0 ; list != null && i < list.size();i++) {
            Map<String,Object> map = list.get(i) ;
            CountryEntity countryEntity = getCountryEntity(map);
            countryList.add(countryEntity);
        }
        return countryList;
    }
 
    @Override
    public CountryEntity getCountry(Integer countryId) {
        String sql = " set nocount on ; \n"
                + " select CountryId,CountryName,ISOCode2,ISOCode3,AddressFormat,PostcodeRequired \n"
                + " from t714030  \n"
                + " where CountryId = "+countryId+" and isnull(Status,0) = 1 \n "
                + " order by CountryName ; \n" ;
        Map<String,Object> map = null ; 
        try{
            map =  this.jdbcTemplate.queryForMap(sql) ;
        }catch(DataAccessException e ) {
            if (e instanceof EmptyResultDataAccessException){
                return null ;
            }else {
                //e.printStackTrace();
                 throw e;
            }
        }catch(Exception e){
            //e.printStackTrace();
            throw e;
        }
    
        return getCountryEntity(map);
    }
 
    public CountryEntity getCountryEntity(Map<String,Object> map) {
        
        CountryEntity countryEntity = new CountryEntity() ;
        countryEntity.setCountryId((Integer)map.get("CountryId") == null ? null: (Integer)map.get("CountryId"));
        countryEntity.setCountryName((String)map.get("CountryName") == null ? "": (String)map.get("CountryName"));
        countryEntity.setIsoCode2((String)map.get("ISOCode2") == null ? "": (String)map.get("ISOCode2"));
        countryEntity.setIsoCode3((String)map.get("ISOCode3") == null ? "": (String)map.get("ISOCode3"));
        countryEntity.setAddressFormat((String)map.get("AddressFormat") == null ? "": (String)map.get("AddressFormat"));
        countryEntity.setPostcodeRequired((Integer)map.get("PostcodeRequired") == null ? 0: (Integer)map.get("PostcodeRequired"));
        return countryEntity ;
    
    }
 
    @Override
    public boolean hasShopCcCode(String areaId) {
        String sql = " set nocount on ; \n"
                + " if exists( select top 1 1 from t110601 a \n"
                + "       where ISNULL(a.isIndependentAccountAbility,0) = 1 \n"
                + "       and a.AreaId in (select AreaId from f110202("+GridUtils.prossSqlParm(areaId)+",'') )\n"
                + "       and isnull(a.ShopCcCode,'') <> '' ) \n"
                + "    select 1 \n"
                + " else \n"
                + "   select 0  \n" ;
        try{
            Integer ret =  this.jdbcTemplate.queryForObject(sql,Integer.class) ;
            if (ret != null && ret.intValue() == 1 ) {
                return true ;
            }else {
                return false ;
            }
        }catch(DataAccessException e ) {
            if (e instanceof EmptyResultDataAccessException){
                return false ;
            }else {
            //    e.printStackTrace();
                 throw e;
            }
        }catch(Exception e){
            //e.printStackTrace();
            throw e;
        }
    }
    
    @Override
    public List<RegionEntity> getRegionsByCountryId(Integer countryId) {
        String sql = " set nocount on ; \n"
                + " select CountryId, ZoneId,EnAreaName,AreaId,AreaName,Status \n"
                + " from t110202  \n"
                + " where CountryId = "+countryId+" and isnull(Status,0) = 1 and isnull(ParentRowid,'') = '' \n "
                + " order by AreaName asc ; \n" ;
        List<Map<String,Object>> list = null ;
        //Map<String,Object> map = null ; 
        List<RegionEntity> regionList = new ArrayList<RegionEntity>() ;
        try{
            list =  this.jdbcTemplate.queryForList(sql) ;
            if (list == null ) return null ;
            
            for (Map<String,Object> map:list) {
                RegionEntity  regionEntity = new RegionEntity() ;
                regionEntity.setCountryId(countryId);
                regionEntity.setZoneId(map.get("ZoneId") == null ? 0 : (Integer)map.get("ZoneId"));
                regionEntity.setCode(map.get("EnAreaName") == null ? "" : (String)map.get("EnAreaName"));
                regionEntity.setAreaId(map.get("AreaId") == null ? "" : (String)map.get("AreaId"));
                regionEntity.setName(map.get("AreaName") == null ? "" : (String)map.get("AreaName"));
                regionEntity.setStatus(map.get("Status") == null ? 0 : (Integer)map.get("Status"));
                
                regionList.add(regionEntity);
            }
        }catch(DataAccessException e ) {
            if (e instanceof EmptyResultDataAccessException){
                return null ;
            }else {
            //    e.printStackTrace();
                 throw e;
            }
        }catch(Exception e){
            //e.printStackTrace();
            throw e;
        }
    
        return regionList;
    }
    
    @Override
    public List<RegionEntity> getRegionsByZoneId(Integer zoneId) {
        
        String sql = " set nocount on ; \n"
                + " select CountryId, ZoneId,EnAreaName,AreaId,AreaName,Status \n"
                + " from t110202  \n"
                + " where isnull(ParentRowid,'') in (select Rowid from t110202 where ZoneId =  "+zoneId+" ) \n "
                + "  and isnull(Status,0) = 1  \n "
                + " order by AreaName asc ; \n" ;
        List<Map<String,Object>> list = null ;
        //Map<String,Object> map = null ; 
        List<RegionEntity> regionList = new ArrayList<RegionEntity>() ;
        try{
            list =  this.jdbcTemplate.queryForList(sql) ;
            if (list == null ) return null ;
            
            for (Map<String,Object> map:list) {
                RegionEntity  regionEntity = new RegionEntity() ;
                regionEntity.setCountryId(null);
                regionEntity.setZoneId(map.get("ZoneId") == null ? 0 : (Integer)map.get("ZoneId"));
                regionEntity.setCode(map.get("EnAreaName") == null ? "" : (String)map.get("EnAreaName"));
                regionEntity.setAreaId(map.get("AreaId") == null ? "" : (String)map.get("AreaId"));
                regionEntity.setName(map.get("AreaName") == null ? "" : (String)map.get("AreaName"));
                regionEntity.setStatus(map.get("Status") == null ? 0 : (Integer)map.get("Status"));
                
                regionList.add(regionEntity);
            }
        }catch(DataAccessException e ) {
            if (e instanceof EmptyResultDataAccessException){
                return null ;
            }else {
                //e.printStackTrace();
                 throw e;
            }
        }catch(Exception e){
            //e.printStackTrace();
            throw e;
        }
    
        return regionList;
    }
    
    @Override
    public RegionEntity getRegionsByAreaName(String areaName) {
        
        String sql = " set nocount on ; \n"
                + " declare @areaid varchar(50) , @areaname varchar(50) = "+GridUtils.prossSqlParm(areaName)+" \n" 
                + " declare @found int = 0 ,@parentrowid varchar(20) \n" 
                + " declare @FullAreaName varchar(500) ,@ZoneId int, \n"
                + "     @ParentName varchar(50),@ParentZoneId int,@ParentAreaId varchar(50) \n" 
                + " select top 1 @areaid = a.areaid,@ZoneId = a.ZoneId,@areaname = a.AreaName,\n"
                + "    @parentrowid = a.parentrowid,@FullAreaName = a.AreaName, \n"
                + "    @ParentName = b.AreaName,@ParentZoneId = b.ZoneId,@ParentAreaId = b.AreaId \n" 
                + " from t110202 a left join t110202 b on a.parentrowid = b.rowid \n"
                + "where a.areaname like '%' + isnull(@areaname,'') + '%' \n" 
                + " if @@ROWCOUNT > 0 set @found = 1  else set @found = 0\n" 
                + " while ISNULL(@found,0) = 1\n" 
                + " begin\n" 
                + "      select @FullAreaName = ISNULL(areaname,'') + ISNULL(@FullAreaName,'') ,\n" 
                + "          @parentrowid = a.parentrowid\n" 
                + "      from t110202 a \n" 
                + "      where rowid = @parentrowid \n" 
                + "      if @@ROWCOUNT > 0 set @found = 1  else set @found = 0\n" 
                + " end \n" 
                + " select @ZoneId as ZoneId,@areaid as AreaId,@areaname as AreaName,@FullAreaName as FullAreaName,\n"
                + "   @ParentName as ParentName,@ParentZoneId as ParentZoneId ,@ParentAreaId as ParentAreaId \n" ;
        Map<String,Object> map = null ;
        //Map<String,Object> map = null ; 
        
        try{
            map =  this.jdbcTemplate.queryForMap(sql) ;
            if (map != null) {
                RegionEntity  regionEntity = new RegionEntity() ;
                regionEntity.setCountryId(null);
                regionEntity.setZoneId(map.get("ZoneId") == null ? 0 : (Integer)map.get("ZoneId"));
                regionEntity.setCode(map.get("EnAreaName") == null ? "" : (String)map.get("EnAreaName"));
                regionEntity.setAreaId(map.get("AreaId") == null ? "" : (String)map.get("AreaId"));
                regionEntity.setName(map.get("AreaName") == null ? "" : (String)map.get("AreaName"));
                regionEntity.setStatus(map.get("Status") == null ? 0 : (Integer)map.get("Status"));
                regionEntity.setFullAreaName(map.get("FullAreaName") == null ? "" : (String)map.get("FullAreaName"));
                regionEntity.setParentName(map.get("ParentName") == null ? "" : (String)map.get("ParentName"));
                regionEntity.setParentZoneId(map.get("ParentZoneId") == null ? 0 : (Integer)map.get("ParentZoneId"));
                regionEntity.setParentAreaId(map.get("ParentAreaId") == null ? "" : (String)map.get("ParentAreaId"));
                
                return regionEntity ;
            }
        }catch(DataAccessException e ) {
            if (e instanceof EmptyResultDataAccessException){
                return null ;
            }else {
                //e.printStackTrace();
                 throw e;
            }
        }catch(Exception e){
            //e.printStackTrace();
            throw e;
        }
    
        return null;
    }
    
    @Override
    public RegionEntity getRegionsByAreaId(String areaId) {
        
        String sql = " set nocount on ; \n"
                + " declare @areaid varchar(50) = "+GridUtils.prossSqlParm(areaId)+" \n" + 
                " declare @found int = 0 ,@areaname varchar(50) ,@parentrowid varchar(20) \n" + 
                " declare @FullAreaName varchar(500) ,@ZoneId int \n" + 
                " select @ZoneId = ZoneId,@areaname = a.areaname,@parentrowid = a.parentrowid,@FullAreaName = a.areaname \n" + 
                " from t110202 a where a.areaid = @areaid\n" + 
                " if @@ROWCOUNT > 0 set @found = 1  else set @found = 0\n" +  
                " while ISNULL(@found,0) = 1\n" + 
                " begin\n" + 
                "    select @FullAreaName = ISNULL(areaname,'') + ISNULL(@FullAreaName,'') ,\n" + 
                "        @parentrowid = a.parentrowid\n" + 
                "    from t110202 a \n" + 
                "    where rowid = @parentrowid \n" + 
                "    if @@ROWCOUNT > 0 set @found = 1  else set @found = 0\n" + 
                " end \n" + 
                " select @ZoneId as ZoneId,@areaid as AreaId,@areaname as AreaName,@FullAreaName as FullAreaName\n" ;
        Map<String,Object> map = null ;
        //Map<String,Object> map = null ; 
        
        try{
            map =  this.jdbcTemplate.queryForMap(sql) ;
            if (map != null) {
                RegionEntity  regionEntity = new RegionEntity() ;
                regionEntity.setCountryId(null);
                regionEntity.setZoneId(map.get("ZoneId") == null ? 0 : (Integer)map.get("ZoneId"));
                regionEntity.setCode(map.get("EnAreaName") == null ? "" : (String)map.get("EnAreaName"));
                regionEntity.setAreaId(map.get("AreaId") == null ? "" : (String)map.get("AreaId"));
                regionEntity.setName(map.get("AreaName") == null ? "" : (String)map.get("AreaName"));
                regionEntity.setStatus(map.get("Status") == null ? 0 : (Integer)map.get("Status"));
                regionEntity.setFullAreaName(map.get("FullAreaName") == null ? "" : (String)map.get("FullAreaName"));
                return regionEntity ;
            }
        }catch(DataAccessException e ) {
            if (e instanceof EmptyResultDataAccessException){
                return null ;
            }else {
            //    e.printStackTrace();
                 throw e;
            }
        }catch(Exception e){
            //e.printStackTrace();
            throw e;
        }
    
        return null;
    }
    
    
    @Override
    public List<RegionEntity> getRegionsByHasShopCcCode(Integer zoneId) {
        
        String sql = " set nocount on ; \n"
                + " select CountryId, ZoneId,EnAreaName,AreaId,AreaName,Status \n"
                + " from t110202 a \n"
                + " where isnull(ParentRowid,'') in (select Rowid from t110202 where ZoneId =  "+zoneId+" ) \n "
                + "  and isnull(a.Status,0) = 1  \n "
                + " order by a.AreaName asc ; \n" ;
        List<Map<String,Object>> list = null ;
        //Map<String,Object> map = null ; 
        List<RegionEntity> regionList = new ArrayList<RegionEntity>() ;
        try{
            list =  this.jdbcTemplate.queryForList(sql) ;
            if (list == null ) return null ;
            for (Map<String,Object> map:list) {
                RegionEntity  regionEntity = new RegionEntity() ;
                regionEntity.setCountryId(null);
                regionEntity.setZoneId(map.get("ZoneId") == null ? 0 : (Integer)map.get("ZoneId"));
                regionEntity.setCode(map.get("EnAreaName") == null ? "" : (String)map.get("EnAreaName"));
                regionEntity.setAreaId(map.get("AreaId") == null ? "" : (String)map.get("AreaId"));
                regionEntity.setName(map.get("AreaName") == null ? "" : (String)map.get("AreaName"));
                regionEntity.setStatus(map.get("Status") == null ? 0 : (Integer)map.get("Status"));
                
                regionList.add(regionEntity);
            }
        }catch(DataAccessException e ) {
            if (e instanceof EmptyResultDataAccessException){
                return null ;
            }else {
            //    e.printStackTrace();
                 throw e;
            }
        }catch(Exception e){
            //e.printStackTrace();
            throw e;
        }
    
        return regionList;
    }
    
    
    @Override
    public RegionEntity getRegionsByShopCcCode(String cccode) {
        
        String sql = " set nocount on ; \n"
                + " declare @cccode varchar(50) = "+GridUtils.prossSqlParm(cccode)+",@AreaId varchar(50) \n"
                + " select @AreaId = a.AreaId from t110601 a where a.cccode = @cccode \n"
                + " select CountryId, ZoneId,EnAreaName,AreaId,AreaName,Status \n"
                + " from t110202 a \n"
                + " where a.AreaId = @AreaId \n "
                + "  and isnull(a.Status,0) = 1  \n "
                + " order by a.AreaName asc ; \n" ;
        Map<String,Object> map = null ;
 
        try{
            map =  this.jdbcTemplate.queryForMap(sql) ;
            if (map == null ) return null ;
            
            RegionEntity  regionEntity = new RegionEntity() ;
            regionEntity.setCountryId(null);
            regionEntity.setZoneId(map.get("ZoneId") == null ? 0 : (Integer)map.get("ZoneId"));
            regionEntity.setCode(map.get("EnAreaName") == null ? "" : (String)map.get("EnAreaName"));
            regionEntity.setAreaId(map.get("AreaId") == null ? "" : (String)map.get("AreaId"));
            regionEntity.setName(map.get("AreaName") == null ? "" : (String)map.get("AreaName"));
            regionEntity.setStatus(map.get("Status") == null ? 0 : (Integer)map.get("Status"));
            return regionEntity ;
        }catch(DataAccessException e ) {
            if (e instanceof EmptyResultDataAccessException){
                return null ;
            }else {
            //    e.printStackTrace();
                 throw e;
            }
        }catch(Exception e){
            //e.printStackTrace();
            throw e;
        }
 
    }
}