k94314517
2024-07-12 f911989f59778b11e9f60d400c3434f4dc6a18fb
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
package com.doumee.biz.zbom.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.doumee.biz.zbom.ZbomCRMService;
import com.doumee.biz.zbom.model.CrmCustomerInfoModel;
import com.doumee.biz.zbom.model.CrmCustomerSubmmitModel;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.core.utils.HttpsUtil;
import com.doumee.dao.business.CrmInterfaceLogMapper;
import com.doumee.dao.business.CustomerLogMapper;
import com.doumee.dao.business.model.CrmInterfaceLog;
import com.doumee.dao.business.model.CustomerLog;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
/**
 * 志邦CRM客户信息提交接口
 * @author 江蹄蹄
 * @date 2023/11/30 15:33
 */
@Service
public class ZbomCRMServiceImpl implements ZbomCRMService {
 
    @Autowired
    private CustomerLogMapper customerLogMapper;
    @Autowired
    private CrmInterfaceLogMapper crmInterfaceLogMapper;
 
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
 
 
    @Override
    @Async
    public void dealCustomerLogData(CustomerLog customerLog){
        CrmCustomerSubmmitModel entity = new CrmCustomerSubmmitModel();
        List<CrmCustomerInfoModel> cusInfo = new ArrayList();
        CrmCustomerInfoModel crmCustomerInfoModel = new CrmCustomerInfoModel();
        crmCustomerInfoModel.setId(customerLog.getId());
        crmCustomerInfoModel.setName(customerLog.getName());
        crmCustomerInfoModel.setPhone(customerLog.getPhone());
        crmCustomerInfoModel.setAreaname(customerLog.getAreaName());
        crmCustomerInfoModel.setAreacode(customerLog.getAreaCode());
        crmCustomerInfoModel.setOpenid(customerLog.getOpenid());
        crmCustomerInfoModel.setSource(Constants.CrmSources.SOURCE_ZBJX);
        crmCustomerInfoModel.setChannel(crmCustomerInfoModel.getSource()+"00");
        crmCustomerInfoModel.setDate(DateUtil.dateToString(customerLog.getCreateDate(),"yyyy/MM/dd HH:mm"));
        cusInfo.add(crmCustomerInfoModel);
        entity.setCusInfo(cusInfo);
        this.postDataToCrm(entity);
 
    }
 
 
 
    /**
     * 志邦客户信息提交接口
     * @param entity
     * @return
     */
    @Override
    public int postDataToCrm(CrmCustomerSubmmitModel entity )  {
        // TODO Auto-generated method stub
        int status = 2;
        if(entity==null || entity.getCusInfo()==null || entity.getCusInfo().size() ==0){
            return status;
        }
        String type = "postCusData";
        String appid = (String) redisTemplate.opsForValue().get(Constants.RedisKeys.ZBOM_CRM_API_KEY);
        String urlStr =  (String)redisTemplate.opsForValue().get(Constants.RedisKeys.ZBOM_CRM_API_URL) ;
        long _t = System.currentTimeMillis();
        String crmInfo="提交失败";
        if (StringUtils.isNotBlank(appid)||StringUtils.isNotBlank(urlStr)) {
            String param = JSONObject.toJSONString(entity);
            String result = null;
            int success =1;
            String url = urlStr;
            try {
                String token = DigestUtils.md5Hex(type + _t + appid);
                  url = urlStr + "?type=" + type + "&_t=" + _t + "&token=" + token;// 提交CRM地址
                  result = HttpsUtil.postJson(url,param);
                if (StringUtils.isNotBlank(result)) {
                    JSONObject r = JSONObject.parseObject(result.replace("(", "").replace(")", ""));
                    if (r != null && (StringUtils.equalsIgnoreCase(r.getString("code"), "1")
                            || StringUtils.equalsIgnoreCase(r.getString("code"), "2"))) {
                          status = 1;
                          crmInfo="提交成功";
                        success =0;
                    }else{
                        status = 2;
                        crmInfo="提交失败["+ result+"]";
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
                crmInfo+= e.getMessage();
            }finally {
                saveInterfaceLog(url,"志邦CRM客户留资信息提交",param,success,result);
            }
        }else{
            status = 2;
            crmInfo="提交失败crm配置有误,appid:["+ appid+"]"+"url:["+ urlStr+"]";
        }
        List<Long> idList = new ArrayList<>();
        for(CrmCustomerInfoModel info : entity.getCusInfo()){
            idList.add(info.getId());
        }
        // 如果提交成功
        CustomerLog d = new CustomerLog();
        d.setId(entity.getCusInfo().get(0).getId());
        d.setCrmStatus(Constants.ONE);
        customerLogMapper.update(null,new UpdateWrapper<CustomerLog>().lambda()
                .in(CustomerLog::getId,idList)
                .set(CustomerLog::getCrmStatus,Constants.ONE )
                .set(CustomerLog::getCrmInfo,crmInfo )
                .set(CustomerLog::getCrmDate,new Date() ));// 更新状态
        return status;// 默认失败
    }
 
    public  void  saveInterfaceLog(String url,String name,String  param,Integer success,String respone){
            if(crmInterfaceLogMapper ==null){
                return;
            }
            CrmInterfaceLog log = new CrmInterfaceLog();
            log.setCreateDate(new Date());
            log.setUrl(url);
            log.setEditDate(log.getCreateDate());
            log.setPlat(Constants.ZERO);
            log.setName(name);
            log.setIsdeleted(Constants.ZERO);
            log.setRequest(param);
            log.setType(Constants.ONE);
            log.setSuccess(success);
            log.setRepose(respone);
            crmInterfaceLogMapper.insert(log);
    }
}