|
|
|
package com.qs.cost.module.service;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
import com.qs.cost.common.conf.MainServerProperties;
|
|
|
|
import com.qs.cost.common.consts.ApiUrlConst;
|
|
|
|
import com.qs.cost.common.dto.u8.U8CallbackVo;
|
|
|
|
import com.qs.cost.common.dto.HttpResult;
|
|
|
|
import com.qs.cost.common.dto.R;
|
|
|
|
import com.qs.cost.common.dto.u8.U8RequestBo;
|
|
|
|
import com.qs.cost.common.utils.*;
|
|
|
|
import com.qs.cost.module.domain.GetListEntity;
|
|
|
|
import com.qs.cost.module.domain.LzOrder;
|
|
|
|
import com.qs.cost.module.domain.LzResponseLog;
|
|
|
|
import com.qs.cost.module.domain.dto.U8API4SaleOrder;
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author YenHex
|
|
|
|
* @since 2022/7/6
|
|
|
|
*/
|
|
|
|
@Slf4j
|
|
|
|
@Service
|
|
|
|
@AllArgsConstructor
|
|
|
|
public class JiaJinService {
|
|
|
|
|
|
|
|
private final LzResponseLogService responseLogService;
|
|
|
|
private final LzOrderService lzOrderService;
|
|
|
|
private final MainServerProperties mainServerProperties;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 请求量子app获取订单
|
|
|
|
* @param page
|
|
|
|
*/
|
|
|
|
public void query(Integer page){
|
|
|
|
if(page==null){
|
|
|
|
page = 1;
|
|
|
|
}
|
|
|
|
Date now = new Date();
|
|
|
|
Date lastMonth = DateUtil.getAfterSomedate(now,-1, Calendar.MONTH);
|
|
|
|
String date = DateUtil.formatDate(now,DateUtil.FORMAT_SIMPLE_DATE2)+"-"+DateUtil.formatDate(lastMonth,DateUtil.FORMAT_SIMPLE_DATE2);
|
|
|
|
String pageSize = "10";
|
|
|
|
String params = "date="+date+"&page="+page+"&page_size="+pageSize;
|
|
|
|
String signParam = ApiUrlConst.lzyunli + params + ApiUrlConst.lzyunli;
|
|
|
|
String sign = SecureUtil.md5(signParam);
|
|
|
|
params = params+"&sign="+sign;
|
|
|
|
String url = "http://wsapp6.lzyunli.com/index.php/getOrder?"+params;
|
|
|
|
HttpResult httpResult = HttpUtil.get(url);
|
|
|
|
if(httpResult.getCode()==200){
|
|
|
|
String json = httpResult.getData();
|
|
|
|
GetListEntity getListEntity = JsonUtil.jsonToPojo(json,GetListEntity.class);
|
|
|
|
if(getListEntity==null){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//保存请求json
|
|
|
|
LzResponseLog responseLog = new LzResponseLog();
|
|
|
|
responseLog.setJson(json);
|
|
|
|
responseLogService.save(responseLog);
|
|
|
|
GetListEntity.OtherEntity otherEntity = getListEntity.getOther();
|
|
|
|
//解析请求json,并保存订单到数据库
|
|
|
|
for (GetListEntity.OrderEntity orderEntity : getListEntity.getList()) {
|
|
|
|
LzOrder lzOrder = new LzOrder();
|
|
|
|
lzOrder.setOrderNo(orderEntity.getOrderNo());
|
|
|
|
lzOrder.setOrderAmount(orderEntity.getOrderAmount());
|
|
|
|
lzOrder.setShippingFare(orderEntity.getShippingFare());
|
|
|
|
lzOrder.setConsignee(orderEntity.getConsignee());
|
|
|
|
lzOrder.setConsigneeMobile(orderEntity.getConsigneeMobile());
|
|
|
|
lzOrder.setProvinces(orderEntity.getProvinces());
|
|
|
|
lzOrder.setDetailedAddress(orderEntity.getDetailedAddress());
|
|
|
|
lzOrder.setSaleType(otherEntity.getSalesType());
|
|
|
|
lzOrder.setSaleDept(otherEntity.getSalesClass());
|
|
|
|
lzOrder.setSaleMan(otherEntity.getSalesMan());
|
|
|
|
String itemJson = orderEntity.getItems().toString();
|
|
|
|
lzOrder.setItems(itemJson);
|
|
|
|
lzOrder.setCheckStatus(0);
|
|
|
|
List<GetListEntity.ItemEntity> items = JsonUtil.jsonToList(itemJson,GetListEntity.ItemEntity.class);
|
|
|
|
if(items!=null&&items.size()>0){
|
|
|
|
for (GetListEntity.ItemEntity item : items) {
|
|
|
|
if(StringUtil.isEmpty(item.getInvCode())){
|
|
|
|
lzOrder.setCheckStatus(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
lzOrder.setCheckStatus(2);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
LocalDateTime orderCreateTime = LocalDateTime.parse(orderEntity.getCreateTime(),df);
|
|
|
|
lzOrder.setOrderCreateTime(orderCreateTime);
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.error(e.getMessage());
|
|
|
|
}
|
|
|
|
lzOrderService.save(lzOrder);
|
|
|
|
}
|
|
|
|
Integer pageCount = getListEntity.getPageCount();
|
|
|
|
if(pageCount!=null&&pageCount.equals(page)){
|
|
|
|
return;
|
|
|
|
}else {
|
|
|
|
query(page+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.out.println();
|
|
|
|
}
|
|
|
|
|
|
|
|
//@Scheduled(cron = "")
|
|
|
|
public void task(){
|
|
|
|
//获取未同步订单,调用U8服务
|
|
|
|
LambdaQueryWrapper<LzOrder> lqw = new LambdaQueryWrapper<>();
|
|
|
|
lqw.eq(LzOrder::getStatus,0);
|
|
|
|
lqw.eq(LzOrder::getCheckStatus,0);
|
|
|
|
List<LzOrder> orders = lzOrderService.list(lqw);
|
|
|
|
//DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
Date now = new Date();
|
|
|
|
for (LzOrder order : orders) {
|
|
|
|
String host = mainServerProperties.getHost();
|
|
|
|
String year = mainServerProperties.getYear();
|
|
|
|
String book = mainServerProperties.getBook();
|
|
|
|
//封装jsonBody
|
|
|
|
List<U8API4SaleOrder.OrderDetail> details = new ArrayList<>();
|
|
|
|
|
|
|
|
String itemJson = order.getItems();
|
|
|
|
List<GetListEntity.ItemEntity> items = JsonUtil.jsonToList(itemJson,GetListEntity.ItemEntity.class);
|
|
|
|
BigDecimal rate = BigDecimal.ONE;
|
|
|
|
for (GetListEntity.ItemEntity item : items) {
|
|
|
|
U8API4SaleOrder.OrderDetail orderDetail = new U8API4SaleOrder.OrderDetail();
|
|
|
|
//拿子项税率
|
|
|
|
rate = item.getTaxRate();
|
|
|
|
orderDetail.setInvCode(item.getInvCode());
|
|
|
|
orderDetail.setTaxRate(item.getTaxRate());
|
|
|
|
orderDetail.setQuantity(new Long(item.getQty()));
|
|
|
|
orderDetail.setHasRatePrice(item.getSalePrice());
|
|
|
|
orderDetail.setHasRateSumPrice(item.getSaleAmount());
|
|
|
|
orderDetail.setPreShipmentsDate(now);
|
|
|
|
orderDetail.setPreFinishedDate(now);
|
|
|
|
}
|
|
|
|
U8API4SaleOrder api4SaleOrder = U8API4SaleOrder.builder()
|
|
|
|
.addType(0)
|
|
|
|
.code(order.getOrderNo())
|
|
|
|
.orderDate(DateUtil.localDateTimeToDate(order.getOrderCreateTime()))
|
|
|
|
.bizType("普通销售")
|
|
|
|
.saleType("01")
|
|
|
|
//TODO 客户编码未定
|
|
|
|
.cusCode("")
|
|
|
|
.currencyType("人民币")
|
|
|
|
.depCode(order.getSaleDept())
|
|
|
|
.createUser(order.getSaleMan())
|
|
|
|
.preShipmentsDate(now)
|
|
|
|
.preFinishedDate(now)
|
|
|
|
.taxRate(rate)
|
|
|
|
.remark("")
|
|
|
|
.remarkFoot("")
|
|
|
|
.details(details)
|
|
|
|
.build();
|
|
|
|
String jsonBody = JsonUtil.objectToJson(Arrays.asList(api4SaleOrder));
|
|
|
|
//发起U8服务中台
|
|
|
|
U8RequestBo requestBo = new U8RequestBo();
|
|
|
|
requestBo.setPk(order.getOrderNo());
|
|
|
|
requestBo.setCmd("XSDDAdd");
|
|
|
|
requestBo.setYear(year);
|
|
|
|
requestBo.setBook(book);
|
|
|
|
requestBo.setJsonBody(jsonBody);
|
|
|
|
requestBo.setKeyId("");
|
|
|
|
requestBo.setSettingRetryTimes(2);
|
|
|
|
requestBo.setCallbackState(1);
|
|
|
|
requestBo.setCallbackHost( host + "/lzOrder/callback");
|
|
|
|
order.setStatus(1);
|
|
|
|
order.setUpdateTime(LocalDateTime.now());
|
|
|
|
}
|
|
|
|
lzOrderService.updateBatchById(orders);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* U8中台回调接口
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public R<?> callback(@RequestBody U8CallbackVo requestVo){
|
|
|
|
String orderNo = requestVo.getId();
|
|
|
|
LambdaQueryWrapper<LzOrder> lqw = new LambdaQueryWrapper<>();
|
|
|
|
lqw.eq(LzOrder::getOrderNo,orderNo);
|
|
|
|
LzOrder lzOrder = lzOrderService.getOne(lqw,false);
|
|
|
|
//解析返回结果
|
|
|
|
lzOrder.setStatus(2);
|
|
|
|
String response = requestVo.getRespContext();
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(response);
|
|
|
|
if(jsonObject.getString("code").equals("200")){
|
|
|
|
//发送量子App更新订单状态
|
|
|
|
String params = "orderNos="+orderNo;
|
|
|
|
String signParam = ApiUrlConst.lzyunli + params + ApiUrlConst.lzyunli;
|
|
|
|
String sign = SecureUtil.md5(signParam);
|
|
|
|
String url = "http://wsapp6.lzyunli.com/index.php/upOrder";
|
|
|
|
Map<String, Object> paramMap = new HashMap<>(10);
|
|
|
|
paramMap.put("orderNos",orderNo);
|
|
|
|
paramMap.put("sign",sign);
|
|
|
|
HttpResult httpResult = HttpUtil.post(url,paramMap);
|
|
|
|
if(httpResult.getCode()==200){
|
|
|
|
if(httpResult.getData().contains("true")){
|
|
|
|
lzOrder.setCheckStatus(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
lzOrder.setStatus(3);
|
|
|
|
}
|
|
|
|
lzOrderService.updateById(lzOrder);
|
|
|
|
return R.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|