feat(支持与O2系统进行数据同步):
parent
e6ee95f229
commit
ad298e6cc2
@ -0,0 +1,46 @@
|
||||
package com.currency.appengine.controller;
|
||||
|
||||
import com.currency.appengine.domain.CustomerReq;
|
||||
import com.currency.appengine.service.common.CommonServices;
|
||||
import com.currency.appengine.service.system.CustomerService;
|
||||
import com.currency.appengine.utils.Result;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 公开接口
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/open")
|
||||
public class OpenController {
|
||||
|
||||
@Autowired
|
||||
private CustomerService customerService;
|
||||
@Autowired
|
||||
private CommonServices commonServices;
|
||||
|
||||
@PostMapping("/add")
|
||||
public Result add(@RequestBody Map<String, Object> obj) {
|
||||
CustomerReq customerReq = new CustomerReq();
|
||||
customerReq.setCustomerName(obj.get("name").toString());
|
||||
if (Objects.nonNull(obj.get("remark"))) {
|
||||
customerReq.setRemarks(obj.get("remark").toString());
|
||||
}
|
||||
if (Objects.nonNull(obj.get("phone"))) {
|
||||
customerReq.setContactPhone(obj.get("phone").toString());
|
||||
}
|
||||
customerReq.setStatus(1);
|
||||
customerReq.setIsAuto(true);
|
||||
customerReq.setSettlementCurrency("CNY");
|
||||
customerReq.setTax("text2");
|
||||
customerReq.setIsTaxIncluded(1);
|
||||
customerReq.setCreateBy("超级管理员");
|
||||
return Result.suc(customerService.AsynCustomer(customerReq));
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.currency.appengine.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* 同步数据到o1
|
||||
*
|
||||
* @author zk
|
||||
* @date 2024/9/30
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@ToString
|
||||
@Builder
|
||||
public class AysnCustomer {
|
||||
/**
|
||||
* 客户名称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 手机号
|
||||
*/
|
||||
private String phone;
|
||||
/**
|
||||
* 邮箱
|
||||
*/
|
||||
private String email;
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
private String remark;
|
||||
/**
|
||||
* 创建时间(时间戳)
|
||||
*/
|
||||
// private Long createTime;
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package com.currency.appengine.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* 客户请求信息
|
||||
*
|
||||
* @author zk
|
||||
* @date 2024/10/23
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
@ToString
|
||||
public class CustomerReq {
|
||||
|
||||
private Long id;
|
||||
/**
|
||||
* 地址
|
||||
*/
|
||||
private String address;
|
||||
/**
|
||||
* 联系人
|
||||
*/
|
||||
private String contactPerson;
|
||||
/**
|
||||
* 联系方式
|
||||
*/
|
||||
private String contactPhone;
|
||||
/**
|
||||
* 联系人编码
|
||||
*/
|
||||
private String customerCode;
|
||||
/**
|
||||
* 联系人名称
|
||||
*/
|
||||
private String customerName;
|
||||
/**
|
||||
* 是否自动编码
|
||||
*/
|
||||
private Boolean isAuto;
|
||||
/**
|
||||
* 是否含税
|
||||
*/
|
||||
private Integer isTaxIncluded;
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
private String remarks;
|
||||
/**
|
||||
* 结算货币类型
|
||||
*/
|
||||
private String settlementCurrency;
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
private Integer status;
|
||||
/**
|
||||
* 税率
|
||||
*/
|
||||
private String tax;
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
private String createBy;
|
||||
/**
|
||||
* 创建人id
|
||||
*/
|
||||
private String userId;
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.currency.appengine.filter;//package cn.iocoder.yudao.module.open.filter;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
public class RepeatedlyReadFilter implements Filter {
|
||||
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||
ServletRequest requestWrapper = null;
|
||||
if(servletRequest instanceof HttpServletRequest) {
|
||||
requestWrapper = new RequestWrapper((HttpServletRequest) servletRequest);
|
||||
}
|
||||
if(requestWrapper == null) {
|
||||
filterChain.doFilter(servletRequest, servletResponse);
|
||||
} else {
|
||||
filterChain.doFilter(requestWrapper, servletResponse);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
package com.currency.appengine.filter;
|
||||
|
||||
import javax.servlet.ReadListener;
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletRequestWrapper;
|
||||
import java.io.*;
|
||||
|
||||
public class RequestWrapper extends HttpServletRequestWrapper {
|
||||
private final String body;
|
||||
|
||||
public RequestWrapper(HttpServletRequest request) {
|
||||
super(request);
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
BufferedReader bufferedReader = null;
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
inputStream = request.getInputStream();
|
||||
if (inputStream != null) {
|
||||
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
char[] charBuffer = new char[128];
|
||||
int bytesRead = -1;
|
||||
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
|
||||
stringBuilder.append(charBuffer, 0, bytesRead);
|
||||
}
|
||||
} else {
|
||||
stringBuilder.append("");
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (bufferedReader != null) {
|
||||
try {
|
||||
bufferedReader.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
body = stringBuilder.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServletInputStream getInputStream() throws IOException {
|
||||
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
|
||||
return new ServletInputStream() {
|
||||
@Override
|
||||
public boolean isFinished() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReady() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReadListener(ReadListener readListener) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
return byteArrayInputStream.read();
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public BufferedReader getReader() throws IOException {
|
||||
return new BufferedReader(new InputStreamReader(this.getInputStream()));
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return this.body;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.currency.appengine.mapper.system;
|
||||
|
||||
import com.currency.appengine.domain.CustomerReq;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Options;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
|
||||
/**
|
||||
* @author zk
|
||||
* @date 2024/10/23
|
||||
*/
|
||||
public interface CustomerMapper {
|
||||
|
||||
@Insert(
|
||||
"insert into mini_customer_info(address,create_by, contact_person, contact_phone, customer_name, is_tax_included, remarks, settlement_currency, status, tax) values(#{address},#{createBy}, #{contactPerson}, #{contactPhone}, #{customerName}, #{isTaxIncluded}, #{remarks}, #{settlementCurrency}, #{status}, #{tax});"
|
||||
)
|
||||
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
|
||||
int insertCustomer(CustomerReq customerReq);
|
||||
|
||||
@Select(
|
||||
"SELECT user_name FROM sys_user WHERE user_id = #{userId}"
|
||||
)
|
||||
String selectUserById(String userId);
|
||||
|
||||
@Update(
|
||||
"update mini_customer_info set customer_code = #{customerCode} where id = #{id}"
|
||||
)
|
||||
void updateCodeById(CustomerReq customerReq);
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.currency.appengine.service.system;
|
||||
|
||||
import com.currency.appengine.domain.CustomerReq;
|
||||
|
||||
/**
|
||||
* @author zk
|
||||
* @date 2024/10/23
|
||||
*/
|
||||
public interface CustomerService {
|
||||
/**
|
||||
* 添加客户信息
|
||||
*/
|
||||
int addCustomer(CustomerReq customerReq);
|
||||
/**
|
||||
* 同步客户信息
|
||||
*/
|
||||
int AsynCustomer(CustomerReq customerReq);
|
||||
}
|
@ -0,0 +1,109 @@
|
||||
package com.currency.appengine.service.system.imp;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.currency.appengine.domain.AysnCustomer;
|
||||
import com.currency.appengine.domain.CustomerReq;
|
||||
import com.currency.appengine.mapper.system.CustomerMapper;
|
||||
import com.currency.appengine.service.common.CommonServices;
|
||||
import com.currency.appengine.service.system.CustomerService;
|
||||
import com.currency.appengine.utils.SecurityUtils;
|
||||
import com.currency.appengine.utils.SignUtil;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author zk
|
||||
* @date 2024/10/23
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class CustomerServiceImpl implements CustomerService {
|
||||
|
||||
@Autowired
|
||||
private CustomerMapper customerMapper;
|
||||
@Autowired
|
||||
private CommonServices commonServices;
|
||||
|
||||
/**
|
||||
* 添加客户信息
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public int addCustomer(CustomerReq customerReq) {
|
||||
if (Boolean.TRUE.equals(customerReq.getIsAuto())){
|
||||
customerReq.setCustomerCode(commonServices.getCode("CustomerCode", null));
|
||||
}
|
||||
String user = customerMapper.selectUserById(customerReq.getUserId());
|
||||
customerReq.setCreateBy(user);
|
||||
int i = customerMapper.insertCustomer(customerReq);
|
||||
customerReq.setCustomerCode(String.valueOf(customerReq.getId()));
|
||||
customerMapper.updateCodeById(customerReq);
|
||||
try {
|
||||
aysnO2(customerReq);
|
||||
} catch (Exception e) {
|
||||
log.warn("同步客户信息异常", e);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步客户信息
|
||||
*
|
||||
* @param customerReq
|
||||
*/
|
||||
@Override
|
||||
public int AsynCustomer(CustomerReq customerReq) {
|
||||
int i = customerMapper.insertCustomer(customerReq);
|
||||
customerReq.setCustomerCode(String.valueOf(customerReq.getId()));
|
||||
customerMapper.updateCodeById(customerReq);
|
||||
return i;
|
||||
}
|
||||
/**
|
||||
* 同步到O2客户信息
|
||||
*/
|
||||
private static void aysnO2(CustomerReq req) {
|
||||
String property = SpringUtil.getProperty("o2.sync.enabled");
|
||||
Boolean res = Boolean.valueOf(property);
|
||||
if (Boolean.TRUE.equals(res)) {
|
||||
String accessKey = SpringUtil.getProperty("o2.sync.access-key");
|
||||
String secretKey = SpringUtil.getProperty("o2.sync.secret-key");
|
||||
String url = SpringUtil.getProperty("o2.sync.create-url");
|
||||
Map<String, String> map = new HashMap<>();
|
||||
AysnCustomer customer = new AysnCustomer();
|
||||
customer.setName(req.getCustomerName());
|
||||
customer.setPhone(req.getContactPhone());
|
||||
customer.setRemark(req.getRemarks());
|
||||
// customer.setCreateTime(crmCustomer.getCreateTime().getTime());
|
||||
Map<String, Object> toMap = BeanUtil.beanToMap(customer, false, true);
|
||||
long timestamp = System.currentTimeMillis();
|
||||
map.put("time-stamp", String.valueOf(timestamp));
|
||||
map.put("access-key", accessKey);
|
||||
map.put("tenant-id", "1");
|
||||
toMap.put("time-stamp", String.valueOf(timestamp));
|
||||
toMap.put("access-key", accessKey);
|
||||
String sign = SignUtil.getSign(toMap, secretKey);
|
||||
map.put("sign", sign);
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = HttpRequest.post(url)
|
||||
.addHeaders(map)
|
||||
.body(JSONUtil.toJsonStr(customer))
|
||||
.execute();
|
||||
} catch (Exception e) {
|
||||
log.warn("同步O2客户信息异常", e);
|
||||
return;
|
||||
}
|
||||
String body = response.body();
|
||||
if (response.isOk()) {
|
||||
log.info("同步O2成功");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue