CarSyncServiceImpl.java
17.7 KB
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
package com.bs.cardata.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.bs.cardata.config.CarSysConfig;
import com.bs.cardata.constant.ServiceConstants;
import com.bs.cardata.mapper.CarBrandMapper;
import com.bs.cardata.mapper.CarSeriesMapper;
import com.bs.cardata.mapper.CarTypeMapper;
import com.bs.cardata.entity.Carbrand;
import com.bs.cardata.entity.Carseries;
import com.bs.cardata.entity.Cartype;
import com.bs.cardata.utils.Pinyin4JUtil;
import com.bs.cardata.service.CarSyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Service("carSyncService")
public class CarSyncServiceImpl implements CarSyncService {
@Autowired
private CarBrandMapper carBrandMapper;
@Autowired
private CarSeriesMapper carSeriesMapper;
@Autowired
private CarTypeMapper carTypeMapper;
@Autowired
private CarSysConfig carSysConfig;
@Override
public Integer saveCarBrand(JSONObject ppJson, String childPP) {
Carbrand carBrand = null;
/*
* `name` varchar(255) DEFAULT NULL COMMENT '品牌名称', `logo` varchar(255)
* DEFAULT NULL COMMENT 'logo', `create_time` datetime DEFAULT NULL
* COMMENT '创建时间',
*/
try {
carBrand = carBrandMapper.getCarBrandInfo(ppJson.get("id").toString(), childPP);
if (carBrand == null) {
carBrand = createCarBrand(ppJson, childPP);
} else {
carBrand.setUpdateTime(new Date());
if ((carBrand.getParentId()==null || carBrand.getParentId().intValue()==10000000) && ppJson.getIntValue(ServiceConstants.carBrandParentIdKey)>0) {
carBrand.setParentId(ppJson.getIntValue(ServiceConstants.carBrandParentIdKey));
carBrandMapper.updateById(carBrand);
}
udpateCarbrandAndPhotoImg(carBrand, ppJson, childPP);
}
} catch (Exception e) {
e.printStackTrace();
}
return carBrand.getBid();
}
private void udpateCarbrandAndPhotoImg(Carbrand carBrand, JSONObject ppJson, String childPP) {
String ppName = ppJson.getString("name");
if (carBrand.getParentId().intValue()>0) {
ppName = childPP;
}
boolean update = false;
if (!carBrand.getBrand().equals(ppName)) {
update = true;
carBrand.setBrand(ppName);
}
if (StringUtils.isEmpty(carBrand.getIdentifyingPhoto()) && !StringUtils.isEmpty(ppJson.getString("upload_url"))) {
update = true;
carBrand.setIdentifyingPhoto(ppJson.getString("upload_url"));
}
if (update) {
carBrandMapper.updateById(carBrand);
}
}
private static final String CAR_SERIES_LOWER_PRICE = "lowerPrice";
private static final String CAR_SERIES_HIGHER_PRICE = "higherPrice";
@Override
public Integer saveCarSeries(JSONObject cxJson, Integer brandId) {
/*
* `id` int(11) NOT NULL COMMENT '主键id', `brand_id` int(11) DEFAULT NULL
* COMMENT '关联品牌id', `series_name` varchar(255) DEFAULT NULL COMMENT
* '车系名称', `price_interval_s` float(10,2) DEFAULT NULL COMMENT
* '价格区间-低位', `price_interval_b` float(10,2) DEFAULT NULL COMMENT
* '价格区间-高位', `create_time` datetime DEFAULT NULL COMMENT '创建时间',
*/
Carseries carseries = null;
try {
Map<String, Object> valueMap = new HashMap<String, Object>();
Map<String, Object> whereMap = new HashMap<String, Object>();
Date currentDate = new Date();
carseries = carSeriesMapper.getCarSeriesInfo(cxJson.get("id").toString());
if (carseries == null) {
carseries = new Carseries();
carseries.setBrandId(String.valueOf(brandId));
carseries.setBrandCode(String.valueOf(brandId));
carseries.setCarseries(cxJson.get("cx_name").toString());
carseries.setSeriesLetter(Pinyin4JUtil.getFirstLetter(cxJson.get("cx_name").toString()));
carseries.setCreateTime(currentDate);
carseries.setLastOperatorId(ServiceConstants.lastOperatorId);
carseries.setLastOperatorName(ServiceConstants.lastOperatorName);
carseries.setOperatorType(ServiceConstants.operatorType);
carseries.setAutohomeId(cxJson.get("id").toString());
carseries.setIsUsed(0);
carseries.setLowerPrice(cxJson.getDouble(CAR_SERIES_LOWER_PRICE));
carseries.setHigherPrice(cxJson.getDouble(CAR_SERIES_HIGHER_PRICE));
int res = carSeriesMapper.insert(carseries);
if (carseries.getCsid() != null) {
carseries.setUpdateTime(currentDate);
carseries.setCarseriesCode(String.valueOf(carseries.getCsid()));
carSeriesMapper.updateById(carseries);
}
} else {
boolean updateFlag = false;
if (!carseries.getCarseries().equals(cxJson.get("cx_name").toString())) {
carseries.setCarseries(cxJson.get("cx_name").toString());
carseries.setSeriesLetter(Pinyin4JUtil.getFirstLetter(cxJson.get("cx_name").toString()));
updateFlag = true;
}
if (!carseries.getBrandId().equals(String.valueOf(brandId))) {
carseries.setBrandId(String.valueOf(brandId));
carseries.setBrandCode(String.valueOf(brandId));
updateFlag = true;
}
boolean priceUpdate = valideUpdatePrice(carseries, cxJson);
if (priceUpdate) {
carseries.setLowerPrice(cxJson.getDouble(CAR_SERIES_LOWER_PRICE));
carseries.setHigherPrice(cxJson.getDouble(CAR_SERIES_HIGHER_PRICE));
updateFlag = true;
}
if (updateFlag){
carseries.setUpdateTime(currentDate);
carSeriesMapper.updateById(carseries);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return carseries.getCsid();
}
/**
* 判断是否要更新车系价格
* @param carseries
* @param cxJson
* @return
*/
private boolean valideUpdatePrice(Carseries carseries, JSONObject cxJson) {
double price = cxJson.getDouble(CAR_SERIES_LOWER_PRICE);
if (price<1.0) {
return false;
}
if (carseries==null || carseries.getLowerPrice()==null) {
return false;
}
if (carseries.getLowerPrice().doubleValue()==price) {
return false;
}
return true;
}
@Override
public void saveCarType(JSONObject trJson, JSONObject serTypeJson) {
Pattern p = Pattern.compile("^(\\-|\\+)?\\d+(\\.\\d+)?$");
JSONObject detailJson = trJson.getJSONObject("cxDetail");
if (detailJson == null || detailJson.isEmpty()) {
detailJson = new JSONObject();
}
JSONObject baseJson = detailJson.getJSONObject("基本参数");
JSONObject carJson = detailJson.getJSONObject("车身");
JSONObject engineJson = detailJson.getJSONObject("发动机");
JSONObject dctJson = detailJson.getJSONObject("变速箱");
if (baseJson == null || baseJson.isEmpty()) {
baseJson = new JSONObject();
}
if (carJson == null || carJson.isEmpty()) {
carJson = new JSONObject();
}
if (engineJson == null || engineJson.isEmpty()) {
engineJson = new JSONObject();
}
if (dctJson == null || dctJson.isEmpty()) {
dctJson = new JSONObject();
}
Date currentDate = new Date();
// 能源类型
Integer ennergyType = null;
String energyDesc = "";
// 整车质保
String qaInfo = "";
try {
energyDesc = baseJson.getString("能源类型");
qaInfo = baseJson.getString("整车质保");
} catch (Exception e) {
System.out.println("===exception"+baseJson.toString());
}
if (!StringUtils.isEmpty(energyDesc)) {
ennergyType = parseEnergyType(energyDesc);
}
String cxImageUrl = trJson.getString("cxImage");
Cartype carType = null;
try {
carType = carTypeMapper.getCarTypeInfo(Integer.parseInt(serTypeJson.get("serId").toString()), trJson.get("Id").toString());
if (carType == null) {
carType = new Cartype();
carType.setCarBrandId(Integer.parseInt(serTypeJson.get("brandId").toString()));
carType.setCarBrandName(serTypeJson.get("brandName").toString());
carType.setSeriesId(Integer.parseInt(serTypeJson.get("serId").toString()));
carType.setCarSeriesName(serTypeJson.get("serName").toString());
carType.setName(trJson.get("Name").toString());
if (baseJson.get("变速箱") != null && !StringUtils.isEmpty(baseJson.get("变速箱").toString()) && !"-".equals(baseJson.get("变速箱").toString())) {
carType.setGearbox(baseJson.get("变速箱").toString());
}
if (baseJson.get("级别") != null && !StringUtils.isEmpty(baseJson.get("级别").toString()) && !"-".equals(baseJson.get("级别").toString())) {
carType.setCarLevel(baseJson.get("级别").toString());
}
if (ennergyType!=null) {
carType.setEnergyType(ennergyType);
carType.setEnergyDesc(energyDesc);
} else {
carType.setEnergyType(0);
carType.setEnergyDesc("");
}
if (engineJson.get("排量(L)") != null && !StringUtils.isEmpty(engineJson.get("排量(L)").toString()) && !"-".equals(engineJson.get("排量(L)").toString())) {
Matcher m = p.matcher(engineJson.get("排量(L)").toString());
if (m.lookingAt()) {
carType.setDisplacement(engineJson.get("排量(L)").toString());
}
}
if (trJson.get("Price") != null && !StringUtils.isEmpty(trJson.get("Price"))) {
/*
* String gpM = trJson.get("Price").toString().replace("万",
* ""); Matcher m = p.matcher(gpM); if (m.lookingAt()) {
* map.put("guide_price", gpM); }
*/
if (trJson.get("Price").toString().contains("万") && trJson.get("Price").toString().contains("-")) {
String mpM = trJson.get("Price").toString().replace("万", "").split("-")[0];
Matcher m = p.matcher(mpM);
if (m.lookingAt()) {
carType.setGuidePrice(mpM);
}
} else {
carType.setGuidePrice(trJson.get("Price").toString());
}
}
if (trJson.get("Price2Sc") != null && !StringUtils.isEmpty(trJson.get("Price2Sc"))) {
if (trJson.get("Price2Sc").toString().contains("万") && trJson.get("Price2Sc").toString().contains("-")) {
String mpM = trJson.get("Price2Sc").toString().replace("万", "").split("-")[0];
Matcher m = p.matcher(mpM);
if (m.lookingAt()) {
carType.setSellPrice(mpM);
}
} else {
carType.setSellPrice(trJson.get("Price2Sc").toString());
}
}
// 整车质保
carType.setQaInfo(qaInfo);
carType.setLastOperatorId(ServiceConstants.lastOperatorId);
carType.setLastOperatorName(ServiceConstants.lastOperatorName);
carType.setOperatorType(ServiceConstants.operatorType);
carType.setAutohomeId(trJson.get("Id").toString());
carType.setStatus(0);
carType.setIdentifyingPhoto(cxImageUrl);
carType.setCreateTime(currentDate);
int res = carTypeMapper.insert(carType);
if (res > 0) {
carType.setCarTypeId(carType.getId());
carType.setUpdateTime(currentDate);
carTypeMapper.updateById(carType);
}
} else {
boolean updateFlag = false;
// String typeNameModifyStatus = PropertyConfigurer.getString("car.typename.modify.status");
String typeNameModifyStatus = carSysConfig.getTypeNameModify();
if (!carType.getName().equals(trJson.get("Name").toString())
&& "on".equals(typeNameModifyStatus)) {
carType.setName(trJson.get("Name").toString());
if (ennergyType!=null) {
carType.setEnergyType(ennergyType);
carType.setEnergyDesc(energyDesc);
}
updateFlag = true;
}
Integer brandIdUpdate = Integer.parseInt(serTypeJson.get("brandId").toString());
if (!carType.getCarBrandId().equals(brandIdUpdate)) {
carType.setCarBrandId(brandIdUpdate);
updateFlag = true;
}
if (carType.getEnergyType()==null) {
carType.setEnergyType(0);
updateFlag = true;
}
if (ennergyType!=null && carType.getEnergyType().intValue()!=ennergyType.intValue()) {
carType.setEnergyType(ennergyType);
carType.setEnergyDesc(energyDesc);
carType.setStatus(1);
updateFlag = true;
}
if (StringUtils.isEmpty(carType.getIdentifyingPhoto()) && !StringUtils.isEmpty(cxImageUrl)) {
carType.setIdentifyingPhoto(cxImageUrl);
updateFlag = true;
}
if (StringUtils.isEmpty(carType.getQaInfo()) && !StringUtils.isEmpty(qaInfo)) {
carType.setQaInfo(qaInfo);
updateFlag = true;
}
if (updateFlag) {
carType.setUpdateTime(currentDate);
carTypeMapper.updateById(carType);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private Integer parseEnergyType(String energy) {
/*
能源类型:
1 汽油;2 柴油;
10 48V轻混系统;11 插电式混合动力;12 油电混合;
20 增程式;21 纯电动;22 氢燃料电池
*/
Integer res = null;
if (energy.equals("汽油")) {
res = 1;
} else if (energy.equals("柴油")) {
res = 2;
} else if (energy.equals("48V轻混系统")) {
res = 10;
} else if (energy.equals("插电式混合动力")) {
res = 11;
} else if (energy.equals("油电混合")) {
res = 12;
} else if (energy.equals("增程式")) {
res = 20;
} else if (energy.equals("纯电动")) {
res = 21;
} else if (energy.equals("氢燃料电池")) {
res = 22;
} else if (!StringUtils.isEmpty(energy)) {
res = 90;
}
return res;
}
@Override
public Integer saveCarBrandForParent(JSONObject ppJson) {
// 查询父类品牌数据
Carbrand Carbrand = carBrandMapper.getCarBrandForParent(ppJson.get("id").toString());
// 没有插入新纪录
if (Carbrand==null) {
ppJson.put(ServiceConstants.carBrandParentIdKey, -2);
Carbrand = createCarBrand(ppJson, ppJson.getString("name"));
} else {
Carbrand.setUpdateTime(new Date());
udpateCarbrandAndPhotoImg(Carbrand, ppJson, null);
}
return Carbrand.getBid();
}
// 创建品牌信息
private Carbrand createCarBrand(JSONObject ppJson, String brandName) {
Carbrand carBrand = new Carbrand();
Date currentDate = new Date();
carBrand.setBrand(brandName);
carBrand.setIdentifyingPhoto(ppJson.getString("upload_url"));
carBrand.setBraLetter(Pinyin4JUtil.getFirstLetter(brandName));
carBrand.setCreateTime(currentDate);
carBrand.setLastOperatorId(String.valueOf(ServiceConstants.lastOperatorId));
carBrand.setLastOperatorName(ServiceConstants.lastOperatorName);
carBrand.setOperatorType(ServiceConstants.operatorType);
carBrand.setAutohomeId(ppJson.getString("id"));
carBrand.setIsUsed(0);
if (ppJson.getIntValue(ServiceConstants.carBrandParentIdKey)>0) {
carBrand.setParentId(ppJson.getIntValue(ServiceConstants.carBrandParentIdKey));
} else if (ppJson.getIntValue(ServiceConstants.carBrandParentIdKey)==-2){
carBrand.setParentId(ppJson.getIntValue(ServiceConstants.carBrandParentIdKey));
}
int res = carBrandMapper.insert(carBrand);
if (carBrand.getBid() != null) {
carBrand.setBrandCode(String.valueOf(carBrand.getBid()));
carBrand.setUpdateTime(currentDate);
carBrandMapper.updateById(carBrand);
}
return carBrand;
}
}