| | |
| | | package com.doumee.biz.zbom.impl; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alibaba.fastjson.TypeReference; |
| | | 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.biz.zbom.model.crm.CRMConstants; |
| | | import com.doumee.biz.zbom.model.crm.CrmCustomerInfoRequest; |
| | | import com.doumee.biz.zbom.model.crm.CrmCustomerListRequest; |
| | | import com.doumee.biz.zbom.model.crm.CrmDaogouBindListRequest; |
| | | import com.doumee.biz.zbom.model.crm.response.CRMBaseResponse; |
| | | import com.doumee.biz.zbom.model.crm.response.CRMCustomerListResponse; |
| | | import com.doumee.biz.zbom.model.crm.response.CRMDaogouBindListResponse; |
| | | import com.doumee.biz.zbom.model.zhongtai.ZTConstants; |
| | | import com.doumee.biz.zbom.model.zhongtai.response.ZTBaseResponse; |
| | | 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 lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.codec.digest.DigestUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | import org.springframework.scheduling.annotation.Async; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.InputStreamReader; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.MalformedURLException; |
| | | import java.net.URL; |
| | | import java.net.URLEncoder; |
| | | import java.util.*; |
| | | |
| | | /** |
| | |
| | | * @date 2023/11/30 15:33 |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | public class ZbomCRMServiceImpl implements ZbomCRMService { |
| | | |
| | | @Autowired |
| | |
| | | @Autowired |
| | | private RedisTemplate<String, Object> redisTemplate; |
| | | |
| | | |
| | | |
| | | /** |
| | | * 志邦客户信息提交接口 |
| | | * @param entity |
| | | * @param customerLog |
| | | * @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 token = DigestUtils.md5Hex(type + _t + appid); |
| | | String url = urlStr + "?type=" + type + "&_t=" + _t + "&token=" + token;// 提交CRM地址 |
| | | String param = JSONObject.toJSONString(entity); |
| | | int success =1; |
| | | String result = null; |
| | | try { |
| | | 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); |
| | | } |
| | | @Async |
| | | public int dealCustomerLogData(CustomerLog customerLog){ |
| | | CrmCustomerInfoRequest entity = new CrmCustomerInfoRequest(); |
| | | entity.setId(customerLog.getId()); |
| | | entity.setName(customerLog.getName()); |
| | | entity.setPhone(zbomEncode(customerLog.getPhone())); |
| | | entity.setAreaname(customerLog.getAreaName()); |
| | | entity.setAreacode(customerLog.getAreaCode()); |
| | | entity.setOpenid(customerLog.getOpenid()); |
| | | entity.setSource(CRMConstants.CRM_SOURCE.SOURCE_ZBJX); |
| | | entity.setChannel(CRMConstants.CRM_SOURCE.SOURCE_ZBJX+"00"); |
| | | entity.setDate(DateUtil.dateToString(customerLog.getCreateDate(),"yyyy/MM/dd HH:mm")); |
| | | int status ; |
| | | String crmInfo; |
| | | String param = JSONObject.toJSONString(entity); |
| | | CRMBaseResponse result = sendHttpRequest(CRMConstants.IntegerUrl.POST_CUS_DATA_URL,CRMConstants.IntegerName.POST_CUS_DATA_NAME,param); |
| | | if (result != null && (StringUtils.equalsIgnoreCase(result.getCode(), CRMConstants.CODE_SUCCESS))) { |
| | | status = 1; |
| | | crmInfo="提交成功"; |
| | | }else{ |
| | | status = 2; |
| | | crmInfo="提交失败crm配置有误,appid:["+ appid+"]"+"url:["+ urlStr+"]"; |
| | | crmInfo="提交失败["+ result!=null?result.getMsg():"接口请求失败!"+"]"; |
| | | } |
| | | 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) |
| | | .eq(CustomerLog::getId,entity.getId()) |
| | | .set(CustomerLog::getCrmStatus,Constants.ONE ) |
| | | .set(CustomerLog::getCrmInfo,crmInfo ) |
| | | .set(CustomerLog::getCrmDate,new Date() ));// 更新状态 |
| | | return status;// 默认失败 |
| | | } |
| | | /** |
| | | * 获取导购绑定列表 |
| | | * @param entity |
| | | * @return |
| | | */ |
| | | @Override |
| | | public CRMBaseResponse<List<CRMDaogouBindListResponse>> getDaogouBindList(CrmDaogouBindListRequest entity ){ |
| | | int status ; |
| | | String crmInfo; |
| | | String param = JSONObject.toJSONString(entity); |
| | | CRMBaseResponse<List<CRMDaogouBindListResponse>> result = sendHttpRequest(CRMConstants.IntegerUrl.DAOGOU_BIND_LIST_URL,CRMConstants.IntegerName.DAOGOU_BIND_LIST_NAME,param); |
| | | return result;// 默认失败 |
| | | } |
| | | |
| | | /** |
| | | * 获取客户列表 |
| | | * @param entity |
| | | * @return |
| | | */ |
| | | @Override |
| | | public CRMBaseResponse<List<CRMCustomerListResponse>> getCustomerList(CrmCustomerListRequest entity ){ |
| | | int status ; |
| | | String crmInfo; |
| | | String param = JSONObject.toJSONString(entity); |
| | | CRMBaseResponse<List<CRMCustomerListResponse>> result = sendHttpRequest(CRMConstants.IntegerUrl.CUS_BEFORE_PAGE_LIST_URL,CRMConstants.IntegerName.CUS_BEFORE_PAGE_LIST_NAME,param); |
| | | return result;// 默认失败 |
| | | } |
| | | |
| | | public <T> CRMBaseResponse<T> sendHttpRequest(String interfaceUrl, String name, String param){ |
| | | log.info("【"+name+"】================开始===="+ JSONObject.toJSONString(param)); |
| | | String appKey = (String) redisTemplate.opsForValue().get(Constants.RedisKeys.ZBOM_CRM_API_KEY); |
| | | String url = redisTemplate.opsForValue().get(Constants.RedisKeys.ZBOM_CRM_API_URL) + interfaceUrl ; |
| | | long _t = System.currentTimeMillis(); |
| | | if (StringUtils.isNotBlank(appKey)||StringUtils.isNotBlank(url)) { |
| | | String res = null; |
| | | int success = 0; |
| | | try { |
| | | url += "?timestamp=" + _t + "&sign=" + DigestUtils.md5Hex( _t + appKey); |
| | | Map<String,String> headers = new HashMap<>(); |
| | | String index = (String) redisTemplate.opsForValue().get(Constants.RedisKeys.ZBOM_ZHONGTAI_API_URL); |
| | | res = HttpsUtil.postJson(url,param); |
| | | TypeReference typeReference = |
| | | new TypeReference<CRMBaseResponse<T>>(){}; |
| | | CRMBaseResponse<T> result = JSONObject.parseObject(res, typeReference.getType()); |
| | | logResult(result,name); |
| | | if(StringUtils.equals(result.getCode(),CRMConstants.CODE_SUCCESS)){ |
| | | return result; |
| | | }else{ |
| | | success =1; |
| | | } |
| | | }catch (Exception e){ |
| | | success = 1; |
| | | log.error("【"+name+"】================失败===="+ JSONObject.toJSONString(param)); |
| | | }finally { |
| | | saveInterfaceLog(url,name,param,success,res); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | private static void logResult(CRMBaseResponse res,String name) { |
| | | if(StringUtils.equals(res.getCode(), CRMConstants.CODE_SUCCESS)){ |
| | | log.info("【CRM接口:"+name+"】================成功====\n"+res); |
| | | }else{ |
| | | log.error("【CRM接口:"+name+"】================失败====:\n"+ res); |
| | | } |
| | | } |
| | | 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.setRepose(respone); |
| | | crmInterfaceLogMapper.insert(log); |
| | | } |
| | | |
| | | /** |
| | | * 志邦字符串加密 |
| | | * @param str |
| | | * @return |
| | | */ |
| | | |
| | | @Override |
| | | public String zbomEncode(String str) { |
| | | StringBuffer sb = new StringBuffer(); |
| | | String result = null; |
| | | try { |
| | | // 创建StringBuffer对象用来操作字符串 |
| | | String urlStr = (String) redisTemplate.opsForValue().get(Constants.RedisKeys.ZBOM_ENCODE_URL); |
| | | sb = new StringBuffer( |
| | | urlStr + "?code=" + URLEncoder.encode(str, "UTF-8") + "&type=0&numstr=wy?"); |
| | | result = HttpsUtil.sendGet(sb.toString()); |
| | | |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | }finally { |
| | | saveInterfaceLog(sb.toString(),"志邦字符串加密接口",str, result==null?1:0,result ); |
| | | } |
| | | return result; |
| | | } |
| | | } |