购物平台项目总结

判断非空

集合非空

CollectionUtils.isEmpty(productDOS)

公共生成id的工具

String id = UUIDUtils.getUUID();
这个类是自己创建的
package com.youkeda.dewu.util;

import java.util.UUID;


public class UUIDUtils {
    public static final String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }
}

自动生成一个32位(36位去掉4位空格)的id

API如何传入不带注解的参数

@ResponseBody
@GetMapping("/page")
public Result<Paging<Product>> pageQuery(BasePageParam param) {

    Result<Paging<Product>> result = new Result<>();

    result.setSuccess(true);
    result.setData(productService.pageQueryProduct(param));
    return result;
}

dao包

常见方法分析

  • 单个对象插入

  • 多对象插入(xml文件利用foreach)

  • 一般就是根据主键删除

  • 一般就是传新的对象

  • 根据主键查询

  • 根据主键的List查询(查询多个对象)

用foreach
  • 查询所有

  • 根据其他属性查询

  • 模糊查询(利用CONCAT拼接)

  • 对固定时间段的查询

利用
  • 分页模型查询

利用limit
<bind name="currentIndex" value="pagination * pageSize"/> limit #{currentIndex},#{pageSize}
  • 返回记录总数

select count(1)

多参数查询

利用@Param就不需要声明变量类型了

service包

UserService

register方法

public Result<User> register(String userName, String pwd) 
1判断变量是否为空
if (StringUtils.isEmpty(userName)) {
    result.setCode("600");
    result.setMessage("用户名不能为空");
    return result;
}
if (StringUtils.isEmpty(pwd)) {
    result.setCode("601");
    result.setMessage("密码不能为空");
    return result;
}
2查找是否已存在该对象
UserDO userDO = userDAO.findByUserName(userName);
3判断该对象是否已存在
if (userDO != null) {
    result.setCode("602");
    result.setMessage("用户名已经存在");
    return result;
}
4对密码加密
// 密码加自定义盐值,确保密码安全
String saltPwd = pwd + "_ykd2050";
// 生成md5值,并转大写字母
String md5Pwd = DigestUtils.md5Hex(saltPwd).toUpperCase();
5封装对象
UserDO userDO1 = new UserDO();
userDO1.setUserName(userName);
// 初始昵称等于用户名
userDO1.setNickName(userName);
userDO1.setPwd(md5Pwd);

userDAO.add(userDO1);

result.setSuccess(true);

result.setData(userDO1.toModel());

return result;

login方法

public Result<User> login(String userName, String pwd)
1判断参数
if (StringUtils.isEmpty(userName)) {
    result.setCode("600");
    result.setMessage("用户名不能为空");
    return result;
}
if (StringUtils.isEmpty(pwd)) {
    result.setCode("601");
    result.setMessage("密码不能为空");
    return result;
}

UserDO userDO = userDAO.findByUserName(userName);
if (userDO == null) {
    result.setCode("602");
    result.setMessage("用户名不存在");
    return result;
}
2获得密码
// 密码加自定义盐值,确保密码安全
String saltPwd = pwd + "_ykd2050";
// 生成md5值,并转大写字母
String md5Pwd = DigestUtils.md5Hex(saltPwd).toUpperCase();
3验证
if (!StringUtils.equals(md5Pwd, userDO.getPwd())) {
    result.setCode("603");
    result.setMessage("密码不对");
    return result;
}
4封装数据
result.setSuccess(true);

result.setData(userDO.toModel());

return result;

ProductService

pageQueryProduct

public Paging<Product> pageQueryProduct(BasePageParam param)
1判断参数
if (param == null) {
    return result;
}

if (param.getPagination() < 0) {
    param.setPagination(0);
}

if (param.getPageSize() < 0) {
    param.setPageSize(10);
}
2查询总数
//查询数据总数
int counts = productDAO.selectAllCounts();
3判断总数是否合理
if (counts < 0) {
    return result;
}
4获得总页数
int totalPage = (int)Math.ceil(counts / (param.getPageSize() * 1.0));
5获得数据
List<ProductDO> productDOS = productDAO.pageQuery(param);
List<Product> productList = new ArrayList<>();
if (!CollectionUtils.isEmpty(productDOS)) {
    for (ProductDO productDO : productDOS) {
        productList.add(productDO.convertToModel());
    }
}
6封装数据
result.setTotalCount(counts);
result.setPageSize(param.getPageSize());
result.setPageNum(param.getPagination());
result.setTotalPage(totalPage);
result.setData(productList);O

OrderService

updateProductPersonNumber

public Order updateProductPersonNumber(String orderNumber)
用于更新库存量和销量
1判断参数(这一步少了,方法里应该缺了)
2判断获得的订单对象
OrderDO orderDO = orderDAO.selectByOrderNumber(orderNumber);
if (orderDO == null) {
    return null;
}
3获得分布式锁
RLock transferLock = redisson.getLock("PURCHASE");
transferLock.lock();
4查询货物详情对象
ProductDetail productDetail = productDetailService.get(orderDO.getProductDetailId());
if (productDetail == null) {
    return null;
}
5减少其库存量并保存
productDetail.setStock(productDetail.getStock() - 1);
productDetailService.save(productDetail);
6获得产品对象,并增加其销售量,进行保存
Product product = productService.get(productDetail.getProductId());
product.setPurchaseNum(product.getPurchaseNum() + 1);
productService.save(product);
7解锁
catch (Exception e) {
    logger.error("", e);
} finally {
    transferLock.unlock();
}

queryRecentPaySuccess

public Paging<Order> queryRecentPaySuccess(QueryOrderParam queryOrderParam) 
查询最近的成功订单
1判断参数
if (queryOrderParam == null) {
    queryOrderParam = new QueryOrderParam();
}
2获得订单总数
int counts = orderDAO.selectCounts(queryOrderParam);

if (counts < 1) {
    result.setTotalCount(0);
    return result;
}
3查询相应的订单
List<OrderDO> orderDOS = orderDAO.pageQuery(queryOrderParam);
4获得订单中的用户和产品对象
for (OrderDO orderDO : orderDOS) {
    orders.add(orderDO.convertToModel());
    productDetailIds.add(orderDO.getProductDetailId());
    userIds.add(Long.parseLong(orderDO.getUserId()));
}
List<User> users = userService.queryUser(userIds);
        List<ProductDetail> productDetails = productDetailService.queryProductDetail(productDetailIds);
        Map<String, ProductDetail> productDetailMap = productDetails.stream().collect(
            Collectors.toMap(ProductDetail::getId, t -> t));
        Map<Long, User> userMap = users.stream().collect(Collectors.toMap(User::getId, t -> t));
5封装进订单的属性中
for (Order order : orders) {
    order.setProductDetail(productDetailMap.get(order.getProductDetailId()));
    order.setUser(userMap.get(Long.parseLong(order.getUserId())));
}
6封装结果
result.setData(orders);
result.setTotalCount(counts);
result.setPageNum(orders.size());
result.setPageSize(queryOrderParam.getPageSize());
result.setTotalPage(orders.size() / queryOrderParam.getPageNum());

createOrderNumber

private String createOrderNumber()
生成唯一的订单号
private String createOrderNumber() {
    LocalDateTime localDateTime = LocalDateTime.now();
    DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
    String date = dtf2.format(localDateTime);
    RAtomicLong aLong = redisson.getAtomicLong("myOrderPartNum" + date);
    aLong.expire(10, TimeUnit.SECONDS);
    long count = aLong.incrementAndGet();
    String orderId = date + count;
    return orderId;
}

PaymentRecordService

save
public PaymentRecord save(PaymentRecord paymentRecord)
  1. 判断参数

if (paymentRecord == null) {
    return null;
}
  1. 判断其Id是否存在,是更新还是添加

  1. 添加的话,就对其设置id

if (StringUtils.isEmpty(paymentRecord.getId())) {
    PaymentRecordDO paymentRecordDO = new PaymentRecordDO(paymentRecord);
    paymentRecordDO.setId(UUIDUtils.getUUID());
    int insertSize = paymentRecordDAO.insert(paymentRecordDO);
    if (insertSize < 1) {
        return null;
    }
    paymentRecord.setId(paymentRecordDO.getId());
    return paymentRecord;
} 
  1. 更新的话,就直接更改内容

PaymentRecordDO paymentRecordDO = new PaymentRecordDO(paymentRecord);
int updateSize = paymentRecordDAO.update(paymentRecordDO);
if (updateSize < 1) {
    return null;
}
return paymentRecord;

PayService

aliPay

public Result aliPay(String orderId, PaymentParam paymentParam)
1实例化客户端
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", this.aliPayAppId,
                                                    this.aliPayAppPrivateKey, "json", "UTF-8",
                                                    this.aliPayPublicKey, "RSA2");
2创建API对应的request
AlipayTradeWapPayRequest request = getAlipayTradeWapPayRequest(orderId, paymentParam);
3调用SDK生成表单
response = alipayClient.pageExecute(request);
4更新支付记录
String channelId = response.getTradeNo();
//更新支付记录
updatePayRecord(null, channelId, PayType.ALIPAY.toString(), paymentParam.getOrderNumber(),
                PaymentStatus.PENDING);

alipayCallBack

public Result alipayCallBack(Map<String, String> mapParam)
支付回调
1取得response得到的各个参数
String status = mapParam.get("trade_status");
String orderNum = mapParam.get("out_trade_no");
String endTime = mapParam.get("gmt_close");
2根据订单号获得对应的订单
Order order = orderService.getByOrderNumber(orderNum);
3判断对应的订单是否为空
4更新订单状态
5更新支付流水
6更新库存和销量
if (order != null) {
    //交易成功
    if ("TRADE_SUCCESS".equals(status)) {
        // 更新订单状态
        orderService.updateOrderStatus(orderNum, OrderStatus.TRADE_PAID_SUCCESS);

        //更新支付流水
        PaymentRecordQueryParam queryParam = new PaymentRecordQueryParam();
        queryParam.setOrderNumber(orderNum);
        List<PaymentRecord> paymentRecords = paymentRecordService.query(queryParam);
        if (!CollectionUtils.isEmpty(paymentRecords)) {
            PaymentRecord paymentRecord = paymentRecords.get(0);
            paymentRecord.setPayStatus(PaymentStatus.SUCCESS);
            //更新支付流水状态
            paymentRecordService.updateStatus(paymentRecord);
        }
        orderService.updateProductPersonNumber(orderNum);
    }

    //交易关闭
    if ("TRADE_CLOSED".equals(status)) {
        // 更新订单状态
        orderService.updateOrderStatus(orderNum, OrderStatus.TRADE_PAID_FAILED);

        //更新支付流水
        PaymentRecordQueryParam queryParam = new PaymentRecordQueryParam();
        queryParam.setOrderNumber(orderNum);
        List<PaymentRecord> paymentRecords = paymentRecordService.query(queryParam);
        if (!CollectionUtils.isEmpty(paymentRecords)) {
            PaymentRecord paymentRecord = paymentRecords.get(0);
            paymentRecord.setPayStatus(PaymentStatus.FAILURE);
            //更新支付流水状态
            paymentRecordService.updateStatus(paymentRecord);
        }
    }
}

getAlipayTradeWapPayRequest

private AlipayTradeWapPayRequest getAlipayTradeWapPayRequest(String orderId, PaymentParam paymentParam)
封装request
1设置相应的非Map参数
request.setApiVersion("1.0");
request.setReturnUrl("");
request.setNotifyUrl("");
2设置Map参数
bizContentMap.put("out_trade_no", orderId);
bizContentMap.put("total_amount", paymentParam.getPayAmount());
bizContentMap.put("quit_url", "www.youkeda.com");
bizContentMap.put("product_code", "QUICK_WAP_WAY");
3转化Map
request.setBizContent(objectMapper.writeValueAsString(bizContentMap));

control包

PaymentController

alipaycallback

@PostMapping(path = "/alipaycallback")
void alipaycallback(HttpServletRequest request, HttpServletResponse response)
1获取Map
Map requestParams = request.getParameterMap();
2组装所需要的数据
for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
    String name = (String)iter.next();
    String[] values = (String[])requestParams.get(name);
    String valueStr = "";
    for (int i = 0; i < values.length; i++) {
        valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
    }
    //乱码解决,这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
    //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
    params.put(name, valueStr);
}
3调用回调接口
Result payResult = payService.alipayCallBack(params);

4打印是否成功

try {
    if (payResult.isSuccess()) {
        response.getWriter().print("success");
        response.getWriter().flush();
    }
} catch (IOException e) {
    logger.error("", e);
}

PaymentApi

payOrder

public Result payOrder(@RequestBody PaymentParam paymentParam) 
1判断id
if (StringUtils.isEmpty(paymentParam.getUserId())) {
    result.setSuccess(false);
    result.setMessage("userId is null");
    return result;
}

2判断金额

if (paymentParam.getPayAmount() <= 0) {
    result.setMessage("支付金额不能小于0");
    return result;
}

3组装消费流水记录

PaymentRecord paymentRecord = initPaymentRecord(paymentParam);
PaymentRecord saveResult = paymentRecordService.save(paymentRecord);

4执行支付行为

return payService.payOrder(paymentParam);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值