因为数据库连接问题导致的插入数据库异常
This commit is contained in:
148
src/main/java/com/rj/service/HxrAdminOrderSelectService.java
Normal file
148
src/main/java/com/rj/service/HxrAdminOrderSelectService.java
Normal file
@@ -0,0 +1,148 @@
|
||||
package com.rj.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||
import com.rj.config.HxrAdminProperties;
|
||||
import com.rj.dto.hxr.HxrOrderRow;
|
||||
import com.rj.dto.hxr.HxrOrderSelectResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 调用 hxrd 后台订单列表接口并解析 JSON。
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class HxrAdminOrderSelectService {
|
||||
|
||||
private static final ObjectMapper JSON = new ObjectMapper()
|
||||
.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
|
||||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
|
||||
private final HxrAdminProperties properties;
|
||||
|
||||
/**
|
||||
* 拉取订单列表;配置未就绪或 HTTP 非 2xx 时返回 empty。
|
||||
* <p>使用 {@link HxrAdminProperties#getOrderSelectUrl()} 原样请求(与定时任务一致)。
|
||||
*/
|
||||
public Optional<HxrOrderSelectResponse> fetchOrderSelect() throws Exception {
|
||||
return fetchOrderSelectInternal(properties.getOrderSelectUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* 按购买时间区间、转卖状态等条件拉取订单列表(在配置的 {@code orderSelectUrl} 上覆盖查询参数)。
|
||||
*
|
||||
* @param buyTimeStart 购买时间起,对应 {@code buy_time[0]},可为 null 或空串表示不限制
|
||||
* @param buyTimeEnd 购买时间止,对应 {@code buy_time[1]}
|
||||
* @param isResell 转卖筛选,对应 {@code is_resell};为 null 时不改写该参数
|
||||
*/
|
||||
public Optional<HxrOrderSelectResponse> fetchOrderSelect(
|
||||
String buyTimeStart, String buyTimeEnd, Integer isResell) throws Exception {
|
||||
return fetchOrderSelectInternal(buildOrderSelectUrl(buyTimeStart, buyTimeEnd, isResell));
|
||||
}
|
||||
|
||||
private Optional<HxrOrderSelectResponse> fetchOrderSelectInternal(String requestUri) throws Exception {
|
||||
String cookieHeader = resolveCookieHeader();
|
||||
if (cookieHeader == null || cookieHeader.isBlank()) {
|
||||
log.warn("hxr.admin 未配置 cookie 或 phpsid,跳过拉取");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
HttpClient client = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(35))
|
||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||
.build();
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(requestUri))
|
||||
.timeout(Duration.ofSeconds(160))
|
||||
.header("Accept", "application/json, text/javascript, */*; q=0.01")
|
||||
.header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
|
||||
.header("Cookie", cookieHeader)
|
||||
.header("Priority", "u=1, i")
|
||||
.header("Referer", "https://hxrdhoutai.hxrdsm.cn/app/admin/order/index")
|
||||
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36 Edg/148.0.0.0")
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
int status = response.statusCode();
|
||||
if (status < 200 || status >= 300) {
|
||||
log.warn("hxr admin order/select HTTP {} bodyPrefix={}", status, abbreviate(response.body(), 500));
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
HxrOrderSelectResponse body = JSON.readValue(response.body(), HxrOrderSelectResponse.class);
|
||||
if (body.code() != 0) {
|
||||
log.warn("hxr admin order/select api code={} msg={}", body.code(), body.msg());
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(body);
|
||||
}
|
||||
|
||||
/**
|
||||
* 在 {@link HxrAdminProperties#getOrderSelectUrl()} 上覆盖 {@code buy_time[0]}、{@code buy_time[1]}、{@code is_resell}。
|
||||
*/
|
||||
private String buildOrderSelectUrl(String buyTimeStart, String buyTimeEnd, Integer isResell) {
|
||||
UriComponentsBuilder b = UriComponentsBuilder.fromUriString(properties.getOrderSelectUrl());
|
||||
b.replaceQueryParam("buy_time[0]", buyTimeStart == null ? "" : buyTimeStart);
|
||||
b.replaceQueryParam("buy_time[1]", buyTimeEnd == null ? "" : buyTimeEnd);
|
||||
if (isResell != null) {
|
||||
b.replaceQueryParam("is_resell", isResell);
|
||||
}
|
||||
return b.build().encode().toUriString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 拉取并逐条打日志(一行一条 JSON)。
|
||||
*/
|
||||
public void fetchAndLogEachOrder() throws Exception {
|
||||
Optional<HxrOrderSelectResponse> opt = fetchOrderSelect();
|
||||
if (opt.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
HxrOrderSelectResponse body = opt.get();
|
||||
log.info("未寄卖 order/select ok count={} allMoney={}", body.count(), body.allMoney());
|
||||
for (HxrOrderRow row : body.data()) {
|
||||
log.info("hxr order row {}", JSON.writeValueAsString(row));
|
||||
}
|
||||
}
|
||||
|
||||
private String resolveCookieHeader() {
|
||||
String c = firstNonBlank(properties.getCookie());
|
||||
if (c != null) {
|
||||
return c;
|
||||
}
|
||||
String id = firstNonBlank(properties.getPhpsid());
|
||||
if (id != null) {
|
||||
return "PHPSID=" + id;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private static String firstNonBlank(String s) {
|
||||
if (s == null) {
|
||||
return null;
|
||||
}
|
||||
String t = s.trim();
|
||||
return t.isEmpty() ? null : t;
|
||||
}
|
||||
|
||||
private static String abbreviate(String s, int maxLen) {
|
||||
if (s == null) {
|
||||
return "";
|
||||
}
|
||||
return s.length() <= maxLen ? s : s.substring(0, maxLen) + "...";
|
||||
}
|
||||
}
|
||||
31
src/main/java/com/rj/service/ILbOrderRowService.java
Normal file
31
src/main/java/com/rj/service/ILbOrderRowService.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.rj.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.rj.entity.LbOrderRow;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface ILbOrderRowService extends IService<LbOrderRow> {
|
||||
|
||||
Map<String, Object> add(LbOrderRow entity);
|
||||
|
||||
Map<String, Object> update(LbOrderRow entity);
|
||||
|
||||
Map<String, Object> deleteById(Long id);
|
||||
|
||||
Map<String, Object> pageQuery(
|
||||
Integer current,
|
||||
Integer size,
|
||||
String tenantId,
|
||||
String orderSn,
|
||||
Long buyerId,
|
||||
Long sellerId,
|
||||
Integer status,
|
||||
Long merchandiseId);
|
||||
|
||||
/**
|
||||
* 按条件调用 hxrd 后台订单列表接口拉取数据,并 upsert 到 {@code lb_order_row}。
|
||||
*/
|
||||
Map<String, Object> syncFromHxrAdmin(
|
||||
String buyTimeStart, String buyTimeEnd, String tenantId, Integer isResell);
|
||||
}
|
||||
273
src/main/java/com/rj/service/impl/LbOrderRowServiceImpl.java
Normal file
273
src/main/java/com/rj/service/impl/LbOrderRowServiceImpl.java
Normal file
@@ -0,0 +1,273 @@
|
||||
package com.rj.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.rj.dto.hxr.HxrOrderRow;
|
||||
import com.rj.dto.hxr.HxrOrderSelectResponse;
|
||||
import com.rj.entity.LbOrderRow;
|
||||
import com.rj.mapper.LbOrderRowMapper;
|
||||
import com.rj.service.HxrAdminOrderSelectService;
|
||||
import com.rj.service.ILbOrderRowService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrderRow> implements ILbOrderRowService {
|
||||
|
||||
@Autowired
|
||||
private HxrAdminOrderSelectService hxrAdminOrderSelectService;
|
||||
|
||||
@Override
|
||||
public Map<String, Object> add(LbOrderRow entity) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
if (entity.getId() == null) {
|
||||
result.put("success", false);
|
||||
result.put("message", "id(订单 id)不能为空");
|
||||
return result;
|
||||
}
|
||||
if (entity.getTenantId() == null || entity.getTenantId().trim().isEmpty()) {
|
||||
result.put("success", false);
|
||||
result.put("message", "tenantId不能为空");
|
||||
return result;
|
||||
}
|
||||
if (entity.getSellerId() == null) {
|
||||
result.put("success", false);
|
||||
result.put("message", "sellerId不能为空");
|
||||
return result;
|
||||
}
|
||||
if (entity.getBuyerId() == null) {
|
||||
result.put("success", false);
|
||||
result.put("message", "buyerId不能为空");
|
||||
return result;
|
||||
}
|
||||
if (entity.getOrderSn() == null || entity.getOrderSn().trim().isEmpty()) {
|
||||
result.put("success", false);
|
||||
result.put("message", "orderSn不能为空");
|
||||
return result;
|
||||
}
|
||||
if (entity.getMerchandiseId() == null) {
|
||||
result.put("success", false);
|
||||
result.put("message", "merchandiseId不能为空");
|
||||
return result;
|
||||
}
|
||||
if (this.getById(entity.getId()) != null) {
|
||||
result.put("success", false);
|
||||
result.put("message", "该订单 id 已存在");
|
||||
return result;
|
||||
}
|
||||
|
||||
entity.setTenantId(entity.getTenantId().trim());
|
||||
entity.setOrderSn(entity.getOrderSn().trim());
|
||||
if (entity.getStatus() == null) {
|
||||
entity.setStatus(0);
|
||||
}
|
||||
if (entity.getIsResell() == null) {
|
||||
entity.setIsResell(0);
|
||||
}
|
||||
if (entity.getIsShow() == null) {
|
||||
entity.setIsShow(0);
|
||||
}
|
||||
|
||||
boolean ok = this.save(entity);
|
||||
result.put("success", ok);
|
||||
result.put("message", ok ? "新增成功" : "新增失败");
|
||||
if (ok) {
|
||||
result.put("data", this.getById(entity.getId()));
|
||||
}
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
result.put("success", false);
|
||||
result.put("message", "新增异常:" + e.getMessage());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> update(LbOrderRow entity) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
if (entity.getId() == null) {
|
||||
result.put("success", false);
|
||||
result.put("message", "id不能为空");
|
||||
return result;
|
||||
}
|
||||
if (this.getById(entity.getId()) == null) {
|
||||
result.put("success", false);
|
||||
result.put("message", "记录不存在");
|
||||
return result;
|
||||
}
|
||||
boolean ok = this.updateById(entity);
|
||||
result.put("success", ok);
|
||||
result.put("message", ok ? "编辑成功" : "编辑失败");
|
||||
if (ok) {
|
||||
result.put("data", this.getById(entity.getId()));
|
||||
}
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
result.put("success", false);
|
||||
result.put("message", "编辑异常:" + e.getMessage());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> deleteById(Long id) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
boolean ok = this.removeById(id);
|
||||
result.put("success", ok);
|
||||
result.put("message", ok ? "删除成功" : "删除失败");
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
result.put("success", false);
|
||||
result.put("message", "删除异常:" + e.getMessage());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> pageQuery(
|
||||
Integer current,
|
||||
Integer size,
|
||||
String isResell,
|
||||
String orderSn,
|
||||
Long buyerId,
|
||||
Long sellerId,
|
||||
Integer status,
|
||||
Long merchandiseId) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
if (current == null || current < 1) {
|
||||
current = 1;
|
||||
}
|
||||
if (size == null || size < 1) {
|
||||
size = 10;
|
||||
}
|
||||
|
||||
LambdaQueryWrapper<LbOrderRow> w = new LambdaQueryWrapper<>();
|
||||
if (isResell != null && !isResell.trim().isEmpty()) {
|
||||
w.eq(LbOrderRow::getIsResell, isResell.trim());
|
||||
}
|
||||
if (orderSn != null && !orderSn.trim().isEmpty()) {
|
||||
w.like(LbOrderRow::getOrderSn, orderSn.trim());
|
||||
}
|
||||
if (buyerId != null) {
|
||||
w.eq(LbOrderRow::getBuyerId, buyerId);
|
||||
}
|
||||
if (sellerId != null) {
|
||||
w.eq(LbOrderRow::getSellerId, sellerId);
|
||||
}
|
||||
if (status != null) {
|
||||
w.eq(LbOrderRow::getStatus, status);
|
||||
}
|
||||
if (merchandiseId != null) {
|
||||
w.eq(LbOrderRow::getMerchandiseId, merchandiseId);
|
||||
}
|
||||
w.orderByDesc(LbOrderRow::getId);
|
||||
|
||||
Page<LbOrderRow> page = this.page(new Page<>(current, size), w);
|
||||
result.put("success", true);
|
||||
result.put("message", "查询成功");
|
||||
result.put("data", page.getRecords());
|
||||
result.put("total", page.getTotal());
|
||||
result.put("current", page.getCurrent());
|
||||
result.put("size", page.getSize());
|
||||
result.put("pages", page.getPages());
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
result.put("success", false);
|
||||
result.put("message", "查询异常:" + e.getMessage());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Map<String, Object> syncFromHxrAdmin(
|
||||
String buyTimeStart, String buyTimeEnd, String tenantId, Integer isResell) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
if (tenantId == null || tenantId.trim().isEmpty()) {
|
||||
result.put("success", false);
|
||||
result.put("message", "tenantId不能为空");
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Optional<HxrOrderSelectResponse> opt =
|
||||
hxrAdminOrderSelectService.fetchOrderSelect(buyTimeStart, buyTimeEnd, isResell);
|
||||
if (opt.isEmpty()) {
|
||||
result.put("success", false);
|
||||
result.put("message", "未拉取到订单(请检查 hxr.admin cookie/phpsid、网络或后台返回)");
|
||||
result.put("synced", 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
HxrOrderSelectResponse body = opt.get();
|
||||
List<HxrOrderRow> rows = body.data();
|
||||
if (rows == null || rows.isEmpty()) {
|
||||
result.put("success", true);
|
||||
result.put("message", "接口成功,本页无订单数据");
|
||||
result.put("synced", 0);
|
||||
result.put("remoteCount", body.count());
|
||||
return result;
|
||||
}
|
||||
|
||||
String tid = tenantId.trim();
|
||||
List<LbOrderRow> entities = new ArrayList<>(rows.size());
|
||||
for (HxrOrderRow row : rows) {
|
||||
entities.add(toLbOrderRow(row, tid));
|
||||
}
|
||||
|
||||
boolean ok = this.saveOrUpdateBatch(entities);
|
||||
result.put("success", ok);
|
||||
result.put("message", ok ? "同步完成" : "批量保存失败");
|
||||
result.put("synced", ok ? entities.size() : 0);
|
||||
result.put("remoteCount", body.count());
|
||||
result.put("allMoney", body.allMoney());
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
result.put("success", false);
|
||||
result.put("message", "同步异常:" + e.getMessage());
|
||||
result.put("synced", 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private static LbOrderRow toLbOrderRow(HxrOrderRow row, String tenantId) {
|
||||
LbOrderRow e = new LbOrderRow();
|
||||
e.setId(row.id());
|
||||
e.setOldId(row.oldId());
|
||||
e.setTenantId(tenantId);
|
||||
e.setSellerId(row.sellerId());
|
||||
e.setBuyerId(row.buyerId());
|
||||
e.setOrderSn(row.orderSn());
|
||||
e.setTotalMoney(row.totalMoney());
|
||||
e.setPayTime(row.payTime());
|
||||
e.setPayImg(row.payImg());
|
||||
e.setStatus(row.status());
|
||||
e.setIsResell(row.isResell());
|
||||
e.setIsShow(row.isShow());
|
||||
e.setConsignee(row.consignee());
|
||||
e.setPhone(row.phone());
|
||||
e.setProvince(row.province());
|
||||
e.setCity(row.city());
|
||||
e.setArea(row.area());
|
||||
e.setAddress(row.address());
|
||||
e.setMerchandiseId(row.merchandiseId());
|
||||
e.setConfirmTime(row.confirmTime());
|
||||
e.setBuyTime(row.buyTime());
|
||||
e.setCreatedAt(row.createdAt());
|
||||
e.setUpdatedAt(row.updatedAt());
|
||||
return e;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user