SendCrmService.java
4.9 KB
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
package com.huaheng.api.crm.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.api.crm.domain.CrmResponse;
import com.huaheng.api.crm.domain.CrmSend;
import com.huaheng.api.crm.domain.CrmSendHeader;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.OkHttpUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.domain.AjaxResultCrm;
import com.huaheng.framework.web.domain.AjaxResultWCS;
import com.huaheng.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.sn.domain.Sn;
import com.huaheng.pc.config.sn.service.SnService;
import com.huaheng.pc.shipment.kuaidiHeader.domain.KuaidiHeader;
import com.huaheng.pc.shipment.kuaidiHeader.domain.SendQiwei;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* 企微发送消息
*
* @author huaheng
* @date 2022-09-07
*/
@Service
public class SendCrmService {
public static final String crm_appid = "hh1995";
@Resource
private AddressService addressService;
@Resource
private SnService snService;
public String getCrmAddress() {
Address address = addressService.selectAddressByParam("crm", QuantityConstant.WAREHOUSE_KS);
if(address==null){
throw new ServiceException("请先配置企微地址");
}
return address.getUrl();
}
public String getCrmToken() {
String url= getCrmAddress();
url=url+"API/Gettoken";
String json="appid="+crm_appid;
String result = OkHttpUtils.bodyget(url,json, QuantityConstant.WAREHOUSE_KS);
if (StringUtils.isEmpty(result)) {
throw new ServiceException("获取crm登录失败");
}
//将result中反斜杠去掉
String cleanedStr = result.replace("\\", "");
result = cleanedStr.substring(1, cleanedStr.length() - 1);
JSONObject obj = JSONObject.parseObject(result);
String code=obj.getString("status");
if(StringUtils.isNotEmpty(code)&&!code.equals("200")){
throw new ServiceException(obj.getString("msg"));
}
CrmResponse crmResponse=obj.getObject("response",CrmResponse.class);
// CrmResponse crmResponse = (CrmResponse) ajaxResult.getData();
return crmResponse.getToken();
}
/**
* 查询适合的sn表数据,发送给crm
* @return
*/
public AjaxResult sendCrm() {
LambdaQueryWrapper<Sn> wrapper=new LambdaQueryWrapper<>();
wrapper.eq(Sn::getSyncCrm,1);
wrapper.ge(Sn::getLastUpdated, DateUtils.getNowPreDays("yyyy-MM-dd",5));
wrapper.orderByDesc(Sn::getId);
List<Sn> list=snService.list(wrapper);
if(list.size()==0){
return AjaxResult.success();
}
String token=getCrmToken();
CrmSend crmSend=new CrmSend();
crmSend.setToken( token);
for(Sn sn:list){
if(StringUtils.isEmpty(sn.getMocode())){
sn.setSyncCrm(0);
sn.setSyncIot(0);
snService.updateById(sn);
continue;
}
//查找出通单是否已忽略,已忽略才上床crm
String referCOde=sn.getReferCode();
if(StringUtils.isEmpty(referCOde)){
continue;
}
CrmSendHeader crmSendHeader=new CrmSendHeader();
crmSendHeader.setProID(sn.getMocode());
crmSendHeader.setSNCode(sn.getCode());
crmSendHeader.setMaterialID(sn.getMaterialCode());
crmSendHeader.setEndUser(sn.getFianlCustomerName());
crmSendHeader.setDealer(sn.getCustomerName());
crmSend.setHead(crmSendHeader);
String result=OkHttpUtils.bodypost(getCrmAddress()+"API/CreateEquip", JSON.toJSONString(crmSend), QuantityConstant.WAREHOUSE_KS,"SN传递给CRM");
if (StringUtils.isEmpty(result)) {
throw new ServiceException("获取crm登录失败");
}
//将result中反斜杠去掉
String cleanedStr = result.replace("\\r\\n", "");
cleanedStr = cleanedStr.replace("\\", "");
result = cleanedStr.substring(1, cleanedStr.length() - 1);
JSONObject obj = JSONObject.parseObject(result);
String code=obj.getString("status");
if(StringUtils.isNotEmpty(code)&&code.equals("200")){
//更改同步状态
sn.setSyncCrm(2);
snService.updateById(sn);
}
}
return AjaxResult.success();
}
}