买家购物管理

This commit is contained in:
2026-06-05 22:20:00 +08:00
parent dbf7c4f867
commit 1a44a5befb
6 changed files with 538 additions and 0 deletions

View File

@@ -0,0 +1,298 @@
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.entity.LbBuyerShopping;
import com.rj.mapper.LbBuyerShoppingMapper;
import com.rj.service.ILbBuyerShoppingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
/**
* 买方购物表 lb_buyer_shopping 服务实现
*/
@Slf4j
@Service
public class LbBuyerShoppingServiceImpl
extends ServiceImpl<LbBuyerShoppingMapper, LbBuyerShopping>
implements ILbBuyerShoppingService {
private static final DateTimeFormatter DATE_TIME_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Override
public Map<String, Object> add(LbBuyerShopping entity) {
Map<String, Object> result = new HashMap<>();
try {
if (entity.getId() == null) {
result.put("success", false);
result.put("message", "id不能为空");
return result;
}
trimStringFields(entity);
if (this.getById(entity.getId()) != null) {
result.put("success", false);
result.put("message", "该订单 id 已存在");
return result;
}
applyDefaults(entity);
if (entity.getCreatedAt() == null) {
entity.setCreatedAt(LocalDateTime.now());
}
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 (DuplicateKeyException e) {
log.warn("买方购物新增重复键, id={}", entity.getId());
result.put("success", false);
result.put("message", "该订单 id 已存在");
return result;
} catch (Exception e) {
log.error("买方购物新增异常", e);
result.put("success", false);
result.put("message", "新增异常:" + e.getMessage());
return result;
}
}
@Override
public Map<String, Object> update(LbBuyerShopping entity) {
Map<String, Object> result = new HashMap<>();
try {
if (entity.getId() == null) {
result.put("success", false);
result.put("message", "id不能为空");
return result;
}
LbBuyerShopping existing = this.getById(entity.getId());
if (existing == null) {
result.put("success", false);
result.put("message", "记录不存在");
return result;
}
trimStringFields(entity);
if (entity.getTotalMoney() == null) {
entity.setTotalMoney(existing.getTotalMoney());
}
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) {
log.error("买方购物编辑异常", 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 {
if (id == null) {
result.put("success", false);
result.put("message", "id不能为空");
return result;
}
boolean ok = this.removeById(id);
result.put("success", ok);
result.put("message", ok ? "删除成功" : "删除失败");
return result;
} catch (Exception e) {
log.error("买方购物删除异常", e);
result.put("success", false);
result.put("message", "删除异常:" + e.getMessage());
return result;
}
}
@Override
public Map<String, Object> pageQuery(Integer current,
Integer size,
Long sellerId,
Long merchandiseId,
Long buyerId,
String orderSn,
String sellerNickname,
String sellerMobile,
String buyerNickname,
String buyerMobile,
BigDecimal totalMoneyMin,
BigDecimal totalMoneyMax,
String createdAtStart,
String createdAtEnd,
String buyTimeStart,
String buyTimeEnd,
String confirmTimeStart,
String confirmTimeEnd) {
Map<String, Object> result = new HashMap<>();
try {
if (current == null || current < 1) {
current = 1;
}
if (size == null || size < 1) {
size = 10;
}
LambdaQueryWrapper<LbBuyerShopping> queryWrapper = new LambdaQueryWrapper<>();
if (sellerId != null) {
queryWrapper.eq(LbBuyerShopping::getSellerId, sellerId);
}
if (merchandiseId != null) {
queryWrapper.eq(LbBuyerShopping::getMerchandiseId, merchandiseId);
}
if (buyerId != null) {
queryWrapper.eq(LbBuyerShopping::getBuyerId, buyerId);
}
if (orderSn != null && !orderSn.trim().isEmpty()) {
queryWrapper.like(LbBuyerShopping::getOrderSn, orderSn.trim());
}
if (sellerNickname != null && !sellerNickname.trim().isEmpty()) {
queryWrapper.like(LbBuyerShopping::getSellerNickname, sellerNickname.trim());
}
if (sellerMobile != null && !sellerMobile.trim().isEmpty()) {
queryWrapper.like(LbBuyerShopping::getSellerMobile, sellerMobile.trim());
}
if (buyerNickname != null && !buyerNickname.trim().isEmpty()) {
queryWrapper.like(LbBuyerShopping::getBuyerNickname, buyerNickname.trim());
}
if (buyerMobile != null && !buyerMobile.trim().isEmpty()) {
queryWrapper.like(LbBuyerShopping::getBuyerMobile, buyerMobile.trim());
}
if (totalMoneyMin != null) {
queryWrapper.ge(LbBuyerShopping::getTotalMoney, totalMoneyMin);
}
if (totalMoneyMax != null) {
queryWrapper.le(LbBuyerShopping::getTotalMoney, totalMoneyMax);
}
LocalDateTime createdStart = parseDateTime(createdAtStart);
if (createdAtStart != null && !createdAtStart.trim().isEmpty() && createdStart == null) {
result.put("success", false);
result.put("message", "createdAtStart 格式错误,请使用 yyyy-MM-dd HH:mm:ss");
return result;
}
LocalDateTime createdEnd = parseDateTime(createdAtEnd);
if (createdAtEnd != null && !createdAtEnd.trim().isEmpty() && createdEnd == null) {
result.put("success", false);
result.put("message", "createdAtEnd 格式错误,请使用 yyyy-MM-dd HH:mm:ss");
return result;
}
if (createdStart != null) {
queryWrapper.ge(LbBuyerShopping::getCreatedAt, createdStart);
}
if (createdEnd != null) {
queryWrapper.le(LbBuyerShopping::getCreatedAt, createdEnd);
}
LocalDateTime buyStart = parseDateTime(buyTimeStart);
if (buyTimeStart != null && !buyTimeStart.trim().isEmpty() && buyStart == null) {
result.put("success", false);
result.put("message", "buyTimeStart 格式错误,请使用 yyyy-MM-dd HH:mm:ss");
return result;
}
LocalDateTime buyEnd = parseDateTime(buyTimeEnd);
if (buyTimeEnd != null && !buyTimeEnd.trim().isEmpty() && buyEnd == null) {
result.put("success", false);
result.put("message", "buyTimeEnd 格式错误,请使用 yyyy-MM-dd HH:mm:ss");
return result;
}
if (buyStart != null) {
queryWrapper.ge(LbBuyerShopping::getBuyTime, buyStart);
}
if (buyEnd != null) {
queryWrapper.le(LbBuyerShopping::getBuyTime, buyEnd);
}
LocalDateTime confirmStart = parseDateTime(confirmTimeStart);
if (confirmTimeStart != null && !confirmTimeStart.trim().isEmpty() && confirmStart == null) {
result.put("success", false);
result.put("message", "confirmTimeStart 格式错误,请使用 yyyy-MM-dd HH:mm:ss");
return result;
}
LocalDateTime confirmEnd = parseDateTime(confirmTimeEnd);
if (confirmTimeEnd != null && !confirmTimeEnd.trim().isEmpty() && confirmEnd == null) {
result.put("success", false);
result.put("message", "confirmTimeEnd 格式错误,请使用 yyyy-MM-dd HH:mm:ss");
return result;
}
if (confirmStart != null) {
queryWrapper.ge(LbBuyerShopping::getConfirmTime, confirmStart);
}
if (confirmEnd != null) {
queryWrapper.le(LbBuyerShopping::getConfirmTime, confirmEnd);
}
queryWrapper.orderByDesc(LbBuyerShopping::getBuyTime)
.orderByDesc(LbBuyerShopping::getCreatedAt);
Page<LbBuyerShopping> page = this.page(new Page<>(current, size), queryWrapper);
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) {
log.error("买方购物分页查询异常", e);
result.put("success", false);
result.put("message", "查询异常:" + e.getMessage());
return result;
}
}
private void applyDefaults(LbBuyerShopping entity) {
if (entity.getTotalMoney() == null) {
entity.setTotalMoney(BigDecimal.ZERO);
}
}
private void trimStringFields(LbBuyerShopping entity) {
if (entity.getOrderSn() != null) {
entity.setOrderSn(entity.getOrderSn().trim());
}
if (entity.getSellerNickname() != null) {
entity.setSellerNickname(entity.getSellerNickname().trim());
}
if (entity.getSellerMobile() != null) {
entity.setSellerMobile(entity.getSellerMobile().trim());
}
if (entity.getBuyerNickname() != null) {
entity.setBuyerNickname(entity.getBuyerNickname().trim());
}
if (entity.getBuyerMobile() != null) {
entity.setBuyerMobile(entity.getBuyerMobile().trim());
}
}
private LocalDateTime parseDateTime(String text) {
if (text == null || text.trim().isEmpty()) {
return null;
}
try {
return LocalDateTime.parse(text.trim(), DATE_TIME_FORMATTER);
} catch (Exception e) {
return null;
}
}
}