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 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 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().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); } }