commit
fc68ececc7
54 changed files with 1114 additions and 366 deletions
@ -0,0 +1,16 @@ |
||||
package com.huoran.common.constant; |
||||
|
||||
/** |
||||
* @author TRACK |
||||
*/ |
||||
public class Constant { |
||||
|
||||
/** |
||||
* 句号(英文符号) |
||||
*/ |
||||
public static final String PERIOD = "."; |
||||
/** |
||||
* 逗号 |
||||
*/ |
||||
public static final String COMMA = ","; |
||||
} |
@ -0,0 +1,38 @@ |
||||
package com.huoran.common.constant; |
||||
|
||||
/** |
||||
* @author 菠萝凤梨 |
||||
* @date 2022/3/28 14:32 |
||||
*/ |
||||
public interface OauthCacheNames { |
||||
|
||||
/** |
||||
* oauth 授权相关key |
||||
*/ |
||||
String OAUTH_PREFIX = "huoran_oauth:"; |
||||
|
||||
/** |
||||
* token 授权相关key |
||||
*/ |
||||
String OAUTH_TOKEN_PREFIX = OAUTH_PREFIX + "token:"; |
||||
|
||||
/** |
||||
* 保存token 缓存使用key |
||||
*/ |
||||
String ACCESS = OAUTH_TOKEN_PREFIX + "access:"; |
||||
|
||||
/** |
||||
* 刷新token 缓存使用key |
||||
*/ |
||||
String REFRESH_TO_ACCESS = OAUTH_TOKEN_PREFIX + "refresh_to_access:"; |
||||
|
||||
/** |
||||
* 根据uid获取保存的token key缓存使用的key |
||||
*/ |
||||
String UID_TO_ACCESS = OAUTH_TOKEN_PREFIX + "uid_to_access:"; |
||||
|
||||
/** |
||||
* 保存token的用户信息使用的key |
||||
*/ |
||||
String USER_INFO = OAUTH_TOKEN_PREFIX + "user_info:"; |
||||
} |
@ -0,0 +1,11 @@ |
||||
package com.huoran.common.entity; |
||||
|
||||
import lombok.Data; |
||||
|
||||
@Data |
||||
public class CurrentInfo { |
||||
// 当前用户信息
|
||||
private String accountId; |
||||
private Integer shopId; |
||||
private Integer platformId; |
||||
} |
@ -0,0 +1,32 @@ |
||||
/* |
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||
* |
||||
* https://www.mall4j.com/
|
||||
* |
||||
* 未经允许,不可做商业用途! |
||||
* |
||||
* 版权所有,侵权必究! |
||||
*/ |
||||
package com.huoran.common.utils; |
||||
|
||||
|
||||
import com.huoran.common.entity.CurrentInfo; |
||||
import lombok.experimental.UtilityClass; |
||||
|
||||
/** |
||||
* @author LGH |
||||
*/ |
||||
@UtilityClass |
||||
public class SecurityUtils { |
||||
|
||||
/** |
||||
* 获取用户信息 |
||||
*/ |
||||
public CurrentInfo getCurrentInfo() { |
||||
CurrentInfo currentInfo = new CurrentInfo(); |
||||
currentInfo.setAccountId(TokenUtils.getIdByJwtToken(HttpContextUtils.getHttpServletRequest())); |
||||
currentInfo.setShopId(TokenUtils.getShopIdByJwtToken(HttpContextUtils.getHttpServletRequest())); |
||||
currentInfo.setPlatformId(TokenUtils.getPlatformIdByJwtToken(HttpContextUtils.getHttpServletRequest())); |
||||
return currentInfo; |
||||
} |
||||
} |
@ -1,113 +0,0 @@ |
||||
package com.huoran.nakadai.entity; |
||||
|
||||
import com.baomidou.mybatisplus.annotation.*; |
||||
import com.fasterxml.jackson.annotation.JsonFormat; |
||||
import lombok.Data; |
||||
|
||||
import java.io.Serializable; |
||||
import java.util.Date; |
||||
|
||||
/** |
||||
* <p> |
||||
* 各个平台账号表 |
||||
* </p> |
||||
* |
||||
* @author huoran |
||||
* @since 2021-06-28 |
||||
*/ |
||||
@Data |
||||
@TableName("hr_user_account") |
||||
public class HrUserAccount implements Serializable { |
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
/** |
||||
* 主键id(平台用户id) |
||||
*/ |
||||
@TableId(value = "id", type = IdType.AUTO) |
||||
private Integer id; |
||||
|
||||
/** |
||||
* 用户表id |
||||
*/ |
||||
private Integer userId; |
||||
|
||||
private Integer customerId; |
||||
|
||||
/** |
||||
* 账号 |
||||
*/ |
||||
private String account; |
||||
|
||||
/** |
||||
* 密码 |
||||
*/ |
||||
private String password; |
||||
|
||||
/** |
||||
* 工号 |
||||
*/ |
||||
private String workNumber; |
||||
|
||||
/** |
||||
* 绑定的角色id |
||||
*/ |
||||
private String roleId; |
||||
|
||||
/** |
||||
* 绑定院校id |
||||
*/ |
||||
private Integer schoolId; |
||||
|
||||
@TableField(exist = false) |
||||
private String schoolName; |
||||
|
||||
/** |
||||
* 所属平台:1->职站 2->数据平台 |
||||
*/ |
||||
private String platformId; |
||||
|
||||
@TableField(exist = false) |
||||
private String platformName; |
||||
|
||||
/** |
||||
* 平台(系统)绑定时间 |
||||
*/ |
||||
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") |
||||
private Date platformBindTime; |
||||
|
||||
/** |
||||
* 平台端区分:0->教师端 1->学生端 |
||||
*/ |
||||
private Integer type; |
||||
|
||||
@TableField(exist = false) |
||||
private String typeName; |
||||
|
||||
/** |
||||
* 是否删除(0、未删除 1、已删除) |
||||
*/ |
||||
@TableLogic |
||||
private Integer isDel; |
||||
|
||||
/** |
||||
* 是否禁用(0->禁用,1->启用) |
||||
*/ |
||||
private Integer isEnable; |
||||
|
||||
|
||||
/** |
||||
* 创建时间 |
||||
*/ |
||||
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") |
||||
@TableField(fill = FieldFill.INSERT) |
||||
private Date createTime; |
||||
|
||||
/** |
||||
* 修改时间 |
||||
*/ |
||||
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") |
||||
@TableField(fill = FieldFill.INSERT_UPDATE) |
||||
private Date updateTime; |
||||
|
||||
} |
@ -1,90 +0,0 @@ |
||||
package com.huoran.nakadai.entity; |
||||
|
||||
import com.baomidou.mybatisplus.annotation.*; |
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Data; |
||||
|
||||
import java.io.Serializable; |
||||
import java.util.Date; |
||||
|
||||
/** |
||||
* @描述:各个平台账号表 |
||||
* @作者: Rong |
||||
* @日期: 2021-08-25 |
||||
*/ |
||||
@Data |
||||
@ApiModel(value = "各个平台账号表") |
||||
@TableName("hr_user_account") |
||||
public class UserAccount implements Serializable { |
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
@TableId(type = IdType.AUTO) |
||||
@ApiModelProperty(value = "主键id(平台用户id)") |
||||
private Integer id; |
||||
|
||||
@ApiModelProperty(value = "用户表id") |
||||
private Integer userId; |
||||
|
||||
@ApiModelProperty(value = "客户表id") |
||||
private Integer customerId; |
||||
|
||||
@ApiModelProperty(value = "账号") |
||||
private String account; |
||||
|
||||
@ApiModelProperty("手机号(平台唯一标识)") |
||||
private String phone; |
||||
|
||||
@ApiModelProperty(value = "密码") |
||||
private String password; |
||||
|
||||
@ApiModelProperty(value = "工号") |
||||
private String workNumber; |
||||
|
||||
@ApiModelProperty(value = "绑定的角色id(0为数据平台普通用户)") |
||||
private String roleId; |
||||
|
||||
@ApiModelProperty(value = "绑定院校id") |
||||
private Integer schoolId; |
||||
|
||||
@ApiModelProperty(value = "所属平台id") |
||||
private String platformId; |
||||
|
||||
@ApiModelProperty(value = "小程序id") |
||||
private String appOpenId; |
||||
|
||||
@ApiModelProperty(value = "开放平台与公众平台唯一id") |
||||
private String unionid; |
||||
|
||||
@ApiModelProperty(value = "平台(系统)绑定时间") |
||||
private Date platformBindTime; |
||||
|
||||
@ApiModelProperty(value = "手机绑定时间") |
||||
private Date phoneBindingTime; |
||||
|
||||
@ApiModelProperty(value = "平台端区分:0->教师端 1->学生端 2->无端") |
||||
private Integer type; |
||||
|
||||
@ApiModelProperty(value = "是否删除(0、未删除 1、已删除)") |
||||
private Integer isDel; |
||||
|
||||
@ApiModelProperty(value = "是否禁用(0->禁用,1->启用)") |
||||
private Boolean isEnable; |
||||
|
||||
@ApiModelProperty(value = "创建时间") |
||||
@TableField(fill = FieldFill.INSERT) |
||||
private Date createTime; |
||||
|
||||
@ApiModelProperty(value = "更新时间") |
||||
@TableField(fill = FieldFill.INSERT_UPDATE) |
||||
private Date updateTime; |
||||
|
||||
@ApiModelProperty(value = "班级id") |
||||
@TableField(exist = false) |
||||
private String classId; |
||||
|
||||
@ApiModelProperty(value = "班级id") |
||||
@TableField(exist = false) |
||||
private String userName; |
||||
} |
@ -0,0 +1,38 @@ |
||||
/* |
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||
* |
||||
* https://www.mall4j.com/
|
||||
* |
||||
* 未经允许,不可做商业用途! |
||||
* |
||||
* 版权所有,侵权必究! |
||||
*/ |
||||
|
||||
package com.huoran.nakadai.entity.order; |
||||
|
||||
/** |
||||
* 提交订单事件先后顺序 |
||||
* @author LGH |
||||
*/ |
||||
public interface ConfirmOrderOrder { |
||||
|
||||
/** |
||||
* 没有任何活动时的顺序 |
||||
*/ |
||||
int DEFAULT = 0; |
||||
|
||||
/** |
||||
* 满减,排在DEFAULT后面 |
||||
*/ |
||||
int DISCOUNT = 100; |
||||
|
||||
/** |
||||
* 优惠券,排在DISCOUNT后面 |
||||
*/ |
||||
int COUPON = 200; |
||||
|
||||
/** |
||||
* 分销,排在COUPON后面 |
||||
*/ |
||||
int DISTRIBUTION = 300; |
||||
} |
@ -0,0 +1,28 @@ |
||||
/* |
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||
* |
||||
* https://www.mall4j.com/
|
||||
* |
||||
* 未经允许,不可做商业用途! |
||||
* |
||||
* 版权所有,侵权必究! |
||||
*/ |
||||
|
||||
package com.huoran.nakadai.entity.order; |
||||
|
||||
/** |
||||
* 购物车事件先后顺序 |
||||
* @author LGH |
||||
*/ |
||||
public interface ShopCartEventOrder { |
||||
|
||||
/** |
||||
* 没有任何活动时的顺序 |
||||
*/ |
||||
int DEFAULT = 0; |
||||
|
||||
/** |
||||
* 满减活动的组装顺序,排在DEFAULT后面 |
||||
*/ |
||||
int DISCOUNT = 100; |
||||
} |
@ -0,0 +1,33 @@ |
||||
/* |
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||
* |
||||
* https://www.mall4j.com/
|
||||
* |
||||
* 未经允许,不可做商业用途! |
||||
* |
||||
* 版权所有,侵权必究! |
||||
*/ |
||||
|
||||
package com.huoran.nakadai.entity.order; |
||||
|
||||
/** |
||||
* 提交订单事件先后顺序 |
||||
* @author LGH |
||||
*/ |
||||
public interface SubmitOrderOrder { |
||||
|
||||
/** |
||||
* 没有任何活动时的顺序 |
||||
*/ |
||||
int DEFAULT = 0; |
||||
|
||||
/** |
||||
* 优惠券,排在DEFAULT后面 |
||||
*/ |
||||
int DISCOUNT = 100; |
||||
|
||||
/** |
||||
* 优惠券,排在DEFAULT后面 |
||||
*/ |
||||
int COUPON = 200; |
||||
} |
@ -0,0 +1,26 @@ |
||||
/* |
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||
* |
||||
* https://www.mall4j.com/
|
||||
* |
||||
* 未经允许,不可做商业用途! |
||||
* |
||||
* 版权所有,侵权必究! |
||||
*/ |
||||
|
||||
package com.huoran.nakadai.event; |
||||
|
||||
import com.huoran.nakadai.entity.model.Order; |
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* 取消订单的事件 |
||||
* @author |
||||
*/ |
||||
@Data |
||||
@AllArgsConstructor |
||||
public class CancelOrderEvent { |
||||
|
||||
private Order order; |
||||
} |
@ -0,0 +1,44 @@ |
||||
/* |
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||
* |
||||
* https://www.mall4j.com/
|
||||
* |
||||
* 未经允许,不可做商业用途! |
||||
* |
||||
* 版权所有,侵权必究! |
||||
*/ |
||||
|
||||
package com.huoran.nakadai.event; |
||||
|
||||
|
||||
import com.huoran.nakadai.entity.app.dto.ShopCartItemDto; |
||||
import com.huoran.nakadai.entity.app.dto.ShopCartOrderDto; |
||||
import com.huoran.nakadai.entity.app.param.OrderParam; |
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Data; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 确认订单时的事件 |
||||
* @author LGH |
||||
*/ |
||||
@Data |
||||
@AllArgsConstructor |
||||
public class ConfirmOrderEvent { |
||||
|
||||
/** |
||||
* 购物车已经组装好的店铺订单信息 |
||||
*/ |
||||
private ShopCartOrderDto shopCartOrderDto; |
||||
|
||||
/** |
||||
* 下单请求的参数 |
||||
*/ |
||||
private OrderParam orderParam; |
||||
|
||||
/** |
||||
* 店铺中的所有商品项 |
||||
*/ |
||||
private List<ShopCartItemDto> shopCartItems; |
||||
} |
@ -0,0 +1,28 @@ |
||||
/* |
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||
* |
||||
* https://www.mall4j.com/
|
||||
* |
||||
* 未经允许,不可做商业用途! |
||||
* |
||||
* 版权所有,侵权必究! |
||||
*/ |
||||
|
||||
package com.huoran.nakadai.event; |
||||
|
||||
import com.huoran.nakadai.entity.model.Order; |
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Data; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 订单付款成功的事件 |
||||
* @author |
||||
*/ |
||||
@Data |
||||
@AllArgsConstructor |
||||
public class PaySuccessOrderEvent { |
||||
|
||||
private List<Order> orders; |
||||
} |
@ -0,0 +1,27 @@ |
||||
/* |
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||
* |
||||
* https://www.mall4j.com/
|
||||
* |
||||
* 未经允许,不可做商业用途! |
||||
* |
||||
* 版权所有,侵权必究! |
||||
*/ |
||||
|
||||
package com.huoran.nakadai.event; |
||||
|
||||
import com.huoran.nakadai.entity.model.Order; |
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Data; |
||||
|
||||
|
||||
/** |
||||
* 确认收货的事件 |
||||
* @author |
||||
*/ |
||||
@Data |
||||
@AllArgsConstructor |
||||
public class ReceiptOrderEvent { |
||||
|
||||
private Order order; |
||||
} |
@ -0,0 +1,38 @@ |
||||
/* |
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||
* |
||||
* https://www.mall4j.com/
|
||||
* |
||||
* 未经允许,不可做商业用途! |
||||
* |
||||
* 版权所有,侵权必究! |
||||
*/ |
||||
|
||||
package com.huoran.nakadai.event; |
||||
|
||||
|
||||
import com.huoran.nakadai.entity.app.dto.ShopCartDto; |
||||
import com.huoran.nakadai.entity.app.dto.ShopCartItemDto; |
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Data; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 购物车商品发生改变时的事件 |
||||
* @author LGH |
||||
*/ |
||||
@Data |
||||
@AllArgsConstructor |
||||
public class ShopCartEvent { |
||||
|
||||
/** |
||||
* 将要组装的单个店铺的店铺信息 |
||||
*/ |
||||
private ShopCartDto shopCartDto; |
||||
|
||||
/** |
||||
* 该店铺下的所有商品信息 |
||||
*/ |
||||
private List<ShopCartItemDto> shopCartItemDtoList; |
||||
} |
@ -0,0 +1,34 @@ |
||||
/* |
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||
* |
||||
* https://www.mall4j.com/
|
||||
* |
||||
* 未经允许,不可做商业用途! |
||||
* |
||||
* 版权所有,侵权必究! |
||||
*/ |
||||
|
||||
package com.huoran.nakadai.event; |
||||
|
||||
import com.huoran.nakadai.entity.app.dto.ShopCartOrderMergerDto; |
||||
import com.huoran.nakadai.entity.model.Order; |
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Data; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 提交订单时的事件 |
||||
* @author LGH |
||||
*/ |
||||
@Data |
||||
@AllArgsConstructor |
||||
public class SubmitOrderEvent { |
||||
/** |
||||
* 完整的订单信息 |
||||
*/ |
||||
private final ShopCartOrderMergerDto mergerOrder; |
||||
|
||||
private List<Order> orders; |
||||
|
||||
} |
@ -0,0 +1,99 @@ |
||||
/* |
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||
* |
||||
* https://www.mall4j.com/
|
||||
* |
||||
* 未经允许,不可做商业用途! |
||||
* |
||||
* 版权所有,侵权必究! |
||||
*/ |
||||
|
||||
package com.huoran.nakadai.listener; |
||||
|
||||
|
||||
import com.huoran.common.exception.YamiShopBindException; |
||||
import com.huoran.common.utils.SecurityUtils; |
||||
import com.huoran.common.utils.TokenUtils; |
||||
import com.huoran.nakadai.entity.app.dto.ShopCartItemDto; |
||||
import com.huoran.nakadai.entity.app.dto.ShopCartOrderDto; |
||||
import com.huoran.nakadai.entity.app.param.OrderParam; |
||||
import com.huoran.nakadai.entity.model.Product; |
||||
import com.huoran.nakadai.entity.model.Sku; |
||||
import com.huoran.nakadai.entity.order.ConfirmOrderOrder; |
||||
import com.huoran.nakadai.event.ConfirmOrderEvent; |
||||
import com.huoran.nakadai.service.ProductService; |
||||
import com.huoran.nakadai.service.SkuService; |
||||
import com.huoran.nakadai.utils.Arith; |
||||
import lombok.AllArgsConstructor; |
||||
import org.springframework.context.event.EventListener; |
||||
import org.springframework.core.annotation.Order; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
/** |
||||
* 确认订单信息时的默认操作 |
||||
* @author LGH |
||||
*/ |
||||
@Component("defaultConfirmOrderListener") |
||||
@AllArgsConstructor |
||||
public class ConfirmOrderListener { |
||||
|
||||
// private final UserAddrService userAddrService;
|
||||
//
|
||||
// private final TransportManagerService transportManagerService;
|
||||
|
||||
private final ProductService productService; |
||||
|
||||
private final SkuService skuService; |
||||
|
||||
/** |
||||
* 计算订单金额 |
||||
*/ |
||||
@EventListener(ConfirmOrderEvent.class) |
||||
@Order(ConfirmOrderOrder.DEFAULT) |
||||
public void defaultConfirmOrderEvent(ConfirmOrderEvent event) { |
||||
|
||||
|
||||
ShopCartOrderDto shopCartOrderDto = event.getShopCartOrderDto(); |
||||
|
||||
OrderParam orderParam = event.getOrderParam(); |
||||
String userId = SecurityUtils.getCurrentInfo().getAccountId(); |
||||
|
||||
// 订单的地址信息
|
||||
// UserAddr userAddr = userAddrService.getUserAddrByUserId(orderParam.getAddrId(), userId);
|
||||
|
||||
double total = 0.0; |
||||
|
||||
int totalCount = 0; |
||||
|
||||
double transfee = 0.0; |
||||
|
||||
for (ShopCartItemDto shopCartItem : event.getShopCartItems()) { |
||||
// 获取商品信息
|
||||
Product product = productService.getProductByProdId(shopCartItem.getProdId()); |
||||
// 获取sku信息
|
||||
Sku sku = skuService.getSkuBySkuId(shopCartItem.getSkuId()); |
||||
if (product == null || sku == null) { |
||||
throw new YamiShopBindException("购物车包含无法识别的商品"); |
||||
} |
||||
if (product.getStatus() != 1 || sku.getStatus() != 1) { |
||||
throw new YamiShopBindException("商品[" + sku.getProdName() + "]已下架"); |
||||
} |
||||
|
||||
totalCount = shopCartItem.getProdCount() + totalCount; |
||||
total = Arith.add(shopCartItem.getProductTotalAmount(), total); |
||||
// 用户地址如果为空,则表示该用户从未设置过任何地址相关信息
|
||||
// if (userAddr != null) {
|
||||
// // 每个产品的运费相加
|
||||
// transfee = Arith.add(transfee, transportManagerService.calculateTransfee(shopCartItem, userAddr));
|
||||
// }
|
||||
|
||||
shopCartItem.setActualTotal(shopCartItem.getProductTotalAmount()); |
||||
shopCartOrderDto.setActualTotal(Arith.add(total, transfee)); |
||||
shopCartOrderDto.setTotal(total); |
||||
shopCartOrderDto.setTotalCount(totalCount); |
||||
shopCartOrderDto.setTransfee(transfee); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,54 @@ |
||||
/* |
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||
* |
||||
* https://www.mall4j.com/
|
||||
* |
||||
* 未经允许,不可做商业用途! |
||||
* |
||||
* 版权所有,侵权必究! |
||||
*/ |
||||
|
||||
package com.huoran.nakadai.listener; |
||||
|
||||
import com.google.common.collect.Lists; |
||||
|
||||
import com.huoran.nakadai.entity.app.dto.ShopCartDto; |
||||
import com.huoran.nakadai.entity.app.dto.ShopCartItemDiscountDto; |
||||
import com.huoran.nakadai.entity.app.dto.ShopCartItemDto; |
||||
import com.huoran.nakadai.entity.order.ShopCartEventOrder; |
||||
import com.huoran.nakadai.event.ShopCartEvent; |
||||
import org.springframework.context.event.EventListener; |
||||
import org.springframework.core.annotation.Order; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 默认的购物车链进行组装时的操作 |
||||
* @author LGH |
||||
*/ |
||||
@Component("defaultShopCartListener") |
||||
public class ShopCartListener { |
||||
|
||||
/** |
||||
* 将店铺下的所有商品归属到该店铺的购物车当中 |
||||
* @param event#getShopCart() 购物车 |
||||
* @param event#shopCartItemDtoList 该购物车的商品 |
||||
* @return 是否继续组装 |
||||
*/ |
||||
@EventListener(ShopCartEvent.class) |
||||
@Order(ShopCartEventOrder.DEFAULT) |
||||
public void defaultShopCartEvent(ShopCartEvent event) { |
||||
ShopCartDto shopCart = event.getShopCartDto(); |
||||
List<ShopCartItemDto> shopCartItemDtoList = event.getShopCartItemDtoList(); |
||||
// 对数据进行组装
|
||||
List<ShopCartItemDiscountDto> shopCartItemDiscountDtoList = Lists.newArrayList(); |
||||
ShopCartItemDiscountDto shopCartItemDiscountDto = new ShopCartItemDiscountDto(); |
||||
|
||||
shopCartItemDiscountDto.setShopCartItems(shopCartItemDtoList); |
||||
shopCartItemDiscountDtoList.add(shopCartItemDiscountDto); |
||||
|
||||
shopCart.setShopCartItemDiscounts(shopCartItemDiscountDtoList); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,304 @@ |
||||
/* |
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||
* |
||||
* https://www.mall4j.com/
|
||||
* |
||||
* 未经允许,不可做商业用途! |
||||
* |
||||
* 版权所有,侵权必究! |
||||
*/ |
||||
|
||||
package com.huoran.nakadai.listener; |
||||
|
||||
import cn.hutool.core.bean.BeanUtil; |
||||
import cn.hutool.core.lang.Snowflake; |
||||
import cn.hutool.core.util.StrUtil; |
||||
|
||||
import com.huoran.common.constant.Constant; |
||||
import com.huoran.common.exception.YamiShopBindException; |
||||
import com.huoran.common.utils.SecurityUtils; |
||||
import com.huoran.common.utils.TokenUtils; |
||||
import com.huoran.nakadai.entity.app.dto.ShopCartItemDiscountDto; |
||||
import com.huoran.nakadai.entity.app.dto.ShopCartItemDto; |
||||
import com.huoran.nakadai.entity.app.dto.ShopCartOrderDto; |
||||
import com.huoran.nakadai.entity.app.dto.ShopCartOrderMergerDto; |
||||
import com.huoran.nakadai.entity.enums.OrderStatus; |
||||
import com.huoran.nakadai.entity.model.*; |
||||
import com.huoran.nakadai.entity.order.SubmitOrderOrder; |
||||
import com.huoran.nakadai.event.SubmitOrderEvent; |
||||
import com.huoran.nakadai.mapper.*; |
||||
import com.huoran.nakadai.service.ProductService; |
||||
import com.huoran.nakadai.service.SkuService; |
||||
import com.huoran.nakadai.utils.Arith; |
||||
import lombok.AllArgsConstructor; |
||||
import org.springframework.context.event.EventListener; |
||||
import org.springframework.core.annotation.Order; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import java.util.*; |
||||
|
||||
/** |
||||
* 确认订单信息时的默认操作 |
||||
* |
||||
* @author LGH |
||||
*/ |
||||
@Component("defaultSubmitOrderListener") |
||||
@AllArgsConstructor |
||||
public class SubmitOrderListener { |
||||
|
||||
|
||||
|
||||
|
||||
// private final UserAddrOrderService userAddrOrderService;
|
||||
|
||||
private final ProductService productService; |
||||
|
||||
private final SkuService skuService; |
||||
|
||||
private final Snowflake snowflake; |
||||
|
||||
private final OrderItemMapper orderItemMapper; |
||||
|
||||
private final SkuMapper skuMapper; |
||||
|
||||
private final ProductMapper productMapper; |
||||
|
||||
private final OrderMapper orderMapper; |
||||
|
||||
private final OrderSettlementMapper orderSettlementMapper; |
||||
|
||||
private final BasketMapper basketMapper; |
||||
|
||||
/** |
||||
* 计算订单金额 |
||||
*/ |
||||
@EventListener(SubmitOrderEvent.class) |
||||
@Order(SubmitOrderOrder.DEFAULT) |
||||
public void defaultSubmitOrderListener(SubmitOrderEvent event) { |
||||
Date now = new Date(); |
||||
String userId = SecurityUtils.getCurrentInfo().getAccountId(); |
||||
ShopCartOrderMergerDto mergerOrder = event.getMergerOrder(); |
||||
|
||||
// 订单商品参数
|
||||
List<ShopCartOrderDto> shopCartOrders = mergerOrder.getShopCartOrders(); |
||||
|
||||
List<Integer> basketIds = new ArrayList<>(); |
||||
// 商品skuId为key 需要更新的sku为value的map
|
||||
Map<Integer, Sku> skuStocksMap = new HashMap<>(16); |
||||
// 商品productId为key 需要更新的product为value的map
|
||||
Map<Integer, Product> prodStocksMap = new HashMap<>(16); |
||||
|
||||
// 把订单地址保存到数据库
|
||||
// UserAddrOrder userAddrOrder = BeanUtil.copyProperties(mergerOrder.getUserAddr(), UserAddrOrder.class);
|
||||
// if (userAddrOrder == null) {
|
||||
// throw new YamiShopBindException("请填写收货地址");
|
||||
// }
|
||||
// userAddrOrder.setUserId(userId);
|
||||
// userAddrOrder.setCreateTime(now);
|
||||
// userAddrOrderService.save(userAddrOrder);
|
||||
|
||||
// 订单地址id
|
||||
// Long addrOrderId = userAddrOrder.getAddrOrderId();
|
||||
|
||||
|
||||
// 每个店铺生成一个订单
|
||||
for (ShopCartOrderDto shopCartOrderDto : shopCartOrders) { |
||||
createOrder(event, now, userId, basketIds, skuStocksMap, prodStocksMap, 0L, shopCartOrderDto); |
||||
|
||||
} |
||||
|
||||
// 删除购物车的商品信息
|
||||
if (!basketIds.isEmpty()) { |
||||
basketMapper.deleteShopCartItemsByBasketIds(userId, basketIds); |
||||
|
||||
} |
||||
|
||||
|
||||
// 更新sku库存
|
||||
skuStocksMap.forEach((key, sku) -> { |
||||
|
||||
if (skuMapper.updateStocks(sku) == 0) { |
||||
skuService.removeSkuCacheBySkuId(key, sku.getProdId()); |
||||
throw new YamiShopBindException("商品:[" + sku.getProdName() + "]库存不足"); |
||||
} |
||||
}); |
||||
|
||||
// 更新商品库存
|
||||
prodStocksMap.forEach((prodId, prod) -> { |
||||
|
||||
if (productMapper.updateStocks(prod) == 0) { |
||||
productService.removeProductCacheByProdId(prodId); |
||||
throw new YamiShopBindException("商品:[" + prod.getProdName() + "]库存不足"); |
||||
} |
||||
}); |
||||
|
||||
} |
||||
|
||||
private void createOrder(SubmitOrderEvent event, Date now, String userId, List<Integer> basketIds, Map<Integer, Sku> skuStocksMap, Map<Integer, Product> prodStocksMap, Long addrOrderId, ShopCartOrderDto shopCartOrderDto) { |
||||
// 使用雪花算法生成的订单号
|
||||
String orderNumber = String.valueOf(snowflake.nextId()); |
||||
shopCartOrderDto.setOrderNumber(orderNumber); |
||||
|
||||
Integer shopId = shopCartOrderDto.getShopId(); |
||||
|
||||
// 订单商品名称
|
||||
StringBuilder orderProdName = new StringBuilder(100); |
||||
|
||||
List<OrderItem> orderItems = new ArrayList<>(); |
||||
|
||||
List<ShopCartItemDiscountDto> shopCartItemDiscounts = shopCartOrderDto.getShopCartItemDiscounts(); |
||||
for (ShopCartItemDiscountDto shopCartItemDiscount : shopCartItemDiscounts) { |
||||
List<ShopCartItemDto> shopCartItems = shopCartItemDiscount.getShopCartItems(); |
||||
for (ShopCartItemDto shopCartItem : shopCartItems) { |
||||
Sku sku = checkAndGetSku(shopCartItem.getSkuId(), shopCartItem, skuStocksMap); |
||||
Product product = checkAndGetProd(shopCartItem.getProdId(), shopCartItem, prodStocksMap); |
||||
|
||||
OrderItem orderItem = getOrderItem(now, userId, orderNumber, shopId, orderProdName, shopCartItem, sku, product); |
||||
|
||||
orderItems.add(orderItem); |
||||
|
||||
if (shopCartItem.getBasketId() != null && shopCartItem.getBasketId() != 0) { |
||||
basketIds.add(shopCartItem.getBasketId()); |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
orderProdName.subSequence(0, Math.min(orderProdName.length() - 1, 100)); |
||||
if (orderProdName.lastIndexOf(Constant.COMMA) == orderProdName.length() - 1) { |
||||
orderProdName.deleteCharAt(orderProdName.length() - 1); |
||||
} |
||||
|
||||
|
||||
// 订单信息
|
||||
com.huoran.nakadai.entity.model.Order order = getOrder(now, userId, addrOrderId, shopCartOrderDto, orderNumber, shopId, orderProdName, orderItems); |
||||
event.getOrders().add(order); |
||||
// 插入订单结算表
|
||||
OrderSettlement orderSettlement = new OrderSettlement(); |
||||
orderSettlement.setUserId(userId); |
||||
orderSettlement.setIsClearing(0); |
||||
orderSettlement.setCreateTime(now); |
||||
orderSettlement.setOrderNumber(orderNumber); |
||||
orderSettlement.setPayAmount(order.getActualTotal()); |
||||
orderSettlement.setPayStatus(0); |
||||
orderSettlement.setVersion(0); |
||||
orderSettlementMapper.insert(orderSettlement); |
||||
} |
||||
|
||||
private com.huoran.nakadai.entity.model.Order getOrder(Date now, String userId, Long addrOrderId, ShopCartOrderDto shopCartOrderDto, String orderNumber, Integer shopId, StringBuilder orderProdName, List<OrderItem> orderItems) { |
||||
com.huoran.nakadai.entity.model.Order order = new com.huoran.nakadai.entity.model.Order(); |
||||
|
||||
order.setShopId(shopId); |
||||
order.setOrderNumber(orderNumber); |
||||
// 订单商品名称
|
||||
order.setProdName(orderProdName.toString()); |
||||
// 用户id
|
||||
order.setUserId(userId); |
||||
// 商品总额
|
||||
order.setTotal(shopCartOrderDto.getTotal()); |
||||
// 实际总额
|
||||
order.setActualTotal(shopCartOrderDto.getActualTotal()); |
||||
order.setStatus(OrderStatus.UNPAY.value()); |
||||
order.setUpdateTime(now); |
||||
order.setCreateTime(now); |
||||
order.setIsPayed(0); |
||||
order.setDeleteStatus(0); |
||||
order.setProductNums(shopCartOrderDto.getTotalCount()); |
||||
order.setAddrOrderId(addrOrderId); |
||||
order.setReduceAmount(Arith.sub(Arith.add(shopCartOrderDto.getTotal(), shopCartOrderDto.getTransfee()), shopCartOrderDto.getActualTotal())); |
||||
order.setFreightAmount(shopCartOrderDto.getTransfee()); |
||||
order.setRemarks(shopCartOrderDto.getRemarks()); |
||||
|
||||
order.setOrderItems(orderItems); |
||||
return order; |
||||
} |
||||
|
||||
private OrderItem getOrderItem(Date now, String userId, String orderNumber, Integer shopId, StringBuilder orderProdName, ShopCartItemDto shopCartItem, Sku sku, Product product) { |
||||
OrderItem orderItem = new OrderItem(); |
||||
orderItem.setShopId(shopId); |
||||
orderItem.setOrderNumber(orderNumber); |
||||
orderItem.setProdId(sku.getProdId()); |
||||
orderItem.setSkuId(sku.getSkuId()); |
||||
orderItem.setSkuName(sku.getSkuName()); |
||||
orderItem.setProdCount(shopCartItem.getProdCount()); |
||||
orderItem.setProdName(sku.getProdName()); |
||||
orderItem.setPic(StrUtil.isBlank(sku.getPic()) ? product.getPic() : sku.getPic()); |
||||
orderItem.setPrice(shopCartItem.getPrice()); |
||||
orderItem.setUserId(userId); |
||||
orderItem.setProductTotalAmount(shopCartItem.getProductTotalAmount()); |
||||
orderItem.setRecTime(now); |
||||
orderItem.setCommSts(0); |
||||
orderItem.setBasketDate(shopCartItem.getBasketDate()); |
||||
orderProdName.append(orderItem.getProdName()).append(","); |
||||
//推广员卡号
|
||||
orderItem.setDistributionCardNo(shopCartItem.getDistributionCardNo()); |
||||
return orderItem; |
||||
} |
||||
|
||||
@SuppressWarnings({"Duplicates"}) |
||||
private Product checkAndGetProd(Integer prodId, ShopCartItemDto shopCartItem, Map<Integer, Product> prodStocksMap) { |
||||
Product product = productService.getProductByProdId(prodId); |
||||
if (product == null) { |
||||
throw new YamiShopBindException("购物车包含无法识别的商品"); |
||||
} |
||||
|
||||
if (product.getStatus() != 1) { |
||||
throw new YamiShopBindException("商品[" + product.getProdName() + "]已下架"); |
||||
} |
||||
|
||||
// 商品需要改变的库存
|
||||
Product mapProduct = prodStocksMap.get(prodId); |
||||
|
||||
if (mapProduct == null) { |
||||
mapProduct = new Product(); |
||||
mapProduct.setTotalStocks(0); |
||||
mapProduct.setProdId(prodId); |
||||
mapProduct.setProdName(product.getProdName()); |
||||
|
||||
} |
||||
|
||||
if (product.getTotalStocks() != -1) { |
||||
mapProduct.setTotalStocks(mapProduct.getTotalStocks() + shopCartItem.getProdCount()); |
||||
prodStocksMap.put(product.getProdId(), mapProduct); |
||||
} |
||||
|
||||
// -1为无限库存
|
||||
if (product.getTotalStocks() != -1 && mapProduct.getTotalStocks() > product.getTotalStocks()) { |
||||
throw new YamiShopBindException("商品:[" + product.getProdName() + "]库存不足"); |
||||
} |
||||
|
||||
return product; |
||||
} |
||||
|
||||
@SuppressWarnings({"Duplicates"}) |
||||
private Sku checkAndGetSku(Integer skuId, ShopCartItemDto shopCartItem, Map<Integer, Sku> skuStocksMap) { |
||||
// 获取sku信息
|
||||
Sku sku = skuService.getSkuBySkuId(skuId); |
||||
if (sku == null) { |
||||
throw new YamiShopBindException("购物车包含无法识别的商品"); |
||||
} |
||||
|
||||
if (sku.getStatus() != 1) { |
||||
throw new YamiShopBindException("商品[" + sku.getProdName() + "]已下架"); |
||||
} |
||||
// -1为无限库存
|
||||
if (sku.getStocks() != -1 && shopCartItem.getProdCount() > sku.getStocks()) { |
||||
throw new YamiShopBindException("商品:[" + sku.getProdName() + "]库存不足"); |
||||
} |
||||
|
||||
if (sku.getStocks() != -1) { |
||||
Sku mapSku = new Sku(); |
||||
mapSku.setProdId(sku.getProdId()); |
||||
// 这里的库存是改变的库存
|
||||
mapSku.setStocks(shopCartItem.getProdCount()); |
||||
mapSku.setSkuId(sku.getSkuId()); |
||||
mapSku.setProdName(sku.getProdName()); |
||||
skuStocksMap.put(sku.getSkuId(), mapSku); |
||||
} |
||||
return sku; |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue