jiangping
2024-07-12 3faf877e3ca03028d60bba265249a543e8f9b533
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
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.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.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;
 
    /**
     * 志邦客户信息提交接口
     * @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);
    }
}