Air
2024-11-04 e91602b03701f11d87be2702fa37e0121c80d99f
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
package com.nova.sankuai.controller.v1;
 
import com.alibaba.fastjson.JSONObject;
import com.nova.sankuai.domain.api.baidu.BaiduDto;
import com.nova.sankuai.domain.api.baidu.BaiduWrapperDto;
import com.nova.sankuai.domain.entity.ChannelLog;
import com.nova.sankuai.domain.entity.Customer;
import com.nova.sankuai.domain.enums.SourceTypeEnum;
import com.nova.sankuai.infra.constants.ResultCode;
import com.nova.sankuai.infra.utils.ResultJson;
import com.nova.sankuai.service.IChannelInfoService;
import com.nova.sankuai.service.IChannelLogService;
import com.nova.sankuai.service.ICustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * @author weikangdi
 */
@Api(tags = "百度对接")
@RestController
@RequestMapping(value = "/api/v1/baidu")
@Slf4j
@AllArgsConstructor
public class BaiduController {
 
    private final ICustomerService customerService;
 
    private final IChannelLogService channelLogService;
 
    private final IChannelInfoService channelInfoService;
 
 
    @PostMapping(value = "/customerImport")
    @ApiOperation(value = "获取百度推广数据")
    public ResultJson customerImport(@RequestBody BaiduWrapperDto obj) {
        String content = obj.getData().toString();
        String appId = obj.getAppid().toString();
        ResultJson checkResult = channelInfoService.checkChannel(appId, content);
        if (checkResult != null) {
            return checkResult;
        }
        log.info("百度推广数据:" + content);
        String reg = "\\{([a-zA-Z]+)\\s*=\\s*([^={}]*?),\\s*([a-zA-Z]+)\\s*=\\s*(.*?),\\s*([a-zA-Z]+)\\s*=\\s*(.*?)}";
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(content);
        StringBuilder sb = new StringBuilder();
        sb.append("{'");
        while (m.find()) {
            String group = m.group().split("type=")[1];
            sb.append(group.split(",")[0] + "':'");
            sb.append(group.split("value=")[1].replace("}", "") + "','");
        }
        String jsonStr = sb.substring(0, sb.lastIndexOf(",")) + "}";
        BaiduDto baiduDto = null;
        try {
            baiduDto = JSONObject.parseObject(jsonStr, BaiduDto.class);
            Customer customer = customerService.addBaiDuCustomer(baiduDto);
            channelLogService.createImportLog(SourceTypeEnum.BaiDu.getCode(), ChannelLog.SUCCESS, customer.getId(), jsonStr);
            return ResultJson.success();
        } catch (Exception e) {
            log.error("Baidu数据失败");
            log.error("获取到的百度推送数据:" + content);
            channelLogService.createImportLog(SourceTypeEnum.BaiDu.getCode(), ChannelLog.FAIL, null, jsonStr);
            return ResultJson.failure(ResultCode.BAD_REQUEST);
        }
    }
}