package com.changgou.order.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fescar.spring.annotation.GlobalTransactional;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.order.config.RabbitMQConfig;
import com.changgou.order.dao.*;
import com.changgou.order.pojo.*;
import com.changgou.order.service.CartService;
import com.changgou.order.service.OrderService;
import com.changgou.pay.feign.PayFeign;
import com.changgou.util.IdWorker;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderMapper orderMapper;
/**
* 查询全部列表
* @return
*/
@Override
public List<Order> findAll() {
return orderMapper.selectAll();
}
/**
* 根据ID查询
* @param id
* @return
*/
@Override
public Order findById(String id){
return orderMapper.selectByPrimaryKey(id);
}
@Autowired
private CartService cartService;
@Autowired
private IdWorker idWorker;
@Autowired
private OrderItemMapper orderItemMapper;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private SkuFeign skuFeign;
@Autowired
private TaskMapper taskMapper;
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 增加
* @param order
*/
@Override
@GlobalTransactional(name = "order_add")
public String add(Order order){
//1.获取购物车的相关数据(redis)
Map cartMap = cartService.list(order.getUsername());
List<OrderItem> orderItemList = (List<OrderItem>) cartMap.get("orderItemList");
//2.统计计算:总金额,总数量
//3.填充订单数据并保存到tb_order
order.setTotalNum((Integer) cartMap.get("totalNum"));
order.setTotalMoney((Integer) cartMap.get("totalMoney"));
order.setPayMoney((Integer) cartMap.get("totalMoney"));
order.setCreateTime(new Date());
order.setUpdateTime(new Date());
order.setBuyerRate("0"); // 0:未评价 1:已评价
order.setSourceType("1"); //1:WEB
order.setOrderStatus("0"); //0:未完成 1:已完成 2:已退货
order.setPayStatus("0"); //0:未支付 1:已支付
order.setConsignStatus("0"); //0:未发货 1:已发货
String orderId = idWorker.nextId()+"";
order.setId(orderId);
orderMapper.insertSelective(order);
//4.填充订单项数据并保存到tb_order_item
for (OrderItem orderItem : orderItemList) {
orderItem.setId(idWorker.nextId()+"");
orderItem.setIsReturn("0"); //0:未退货 1:已退货
orderItem.setOrderId(orderId);
orderItemMapper.insertSelective(orderItem);
}
//扣减库存并增加销量
skuFeign.decrCount(order.getUsername());
//int i =1/0;
//添加任务数据
System.out.println("向订单数据库中的任务表去添加任务数据");
Task task = new Task();
task.setCreateTime(new Date());
task.setUpdateTime(new Date());
task.setMqExchange(RabbitMQConfig.EX_BUYING_ADDPOINTUSER);
task.setMqRoutingkey(RabbitMQConfig.CG_BUYING_ADDPOINT_KEY);
Map map = new HashMap();
map.put("username",order.getUsername());
map.put("orderId",orderId);
map.put("point",order.getPayMoney());
task.setRequestBody(JSON.toJSONString(map));
taskMapper.insertSelective(task);
//5.删除购物车数据(redis)
redisTemplate.delete("cart_"+order.getUsername());
//发送延迟消息
rabbitTemplate.convertAndSend("","queue.ordercreate",orderId);
return orderId;
}
/**
* 修改
* @param order
*/
@Override
public void update(Order order){
orderMapper.updateByPrimaryKey(order);
}
/**
* 删除
* @param id
*/
@Override
public void delete(String id){
orderMapper.deleteByPrimaryKey(id);
}
/**
* 条件查询
* @param searchMap
* @return
*/
@Override
public List<Order> findList(Map<String, Object> searchMap){
Example example = createExample(searchMap);
return orderMapper.selectByExample(example);
}
/**
* 分页查询
* @param page
* @param size
* @return
*/
@Override
public Page<Order> findPage(int page, int size){
PageHelper.startPage(page,size);
return (Page<Order>)orderMapper.selectAll();
}
/**
* 条件+分页查询
* @param searchMap 查询条件
* @param page 页码
* @param size 页大小
* @return 分页结果
*/
@Override
public Page<Order> findPage(Map<String,Object> searchMap, int page, int size){
PageHelper.startPage(page,size);
Example example = createExample(searchMap);
return (Page<Order>)orderMapper.selectByExample(example);
}
@Autowired
private OrderLogMapper orderLogMapper;
@Override
@Transactional
public void updatePayStatus(String orderId, String transactionId) {
//1.查询订单
Order order = orderMapper.selectByPrimaryKey(orderId);
if (order != null && "0".equals(order.getPayStatus())){
//2.修改订单的支付状态
order.setPayStatus("1");
order.setOrderStatus("1");
order.setUpdateTime(new Date());
order.setPayTime(new Date());
order.setTransactionId(transactionId); //微信返回的交易流水号
orderMapper.updateByPrimaryKeySelective(order);
//3.记录订单日志
OrderLog orderLog = new OrderLog();
orderLog.setId(idWorker.nextId()+"");
orderLog.setOperater("system");
orderLog.setOperateTime(new Date());
orderLog.setOrderStatus("1");
orderLog.setPayStatus("1");
orderLog.setRemarks("交易流水号:"+transactionId);
orderLog.setOrderId(orderId);
orderLogMapper.insert(orderLog);
}
}
@Autowired
private PayFeign payFeign;
@Override
@Transactional
public void closeOrder(String orderId) {
/**
* 1.根据订单id查询mysql中的订单信息,判断订单是否存在,判断订单的支付状态
* 2. 基于微信查询订单信息(微信)
* 2.1)如果当前订单的支付状态为已支付,则进行数据补偿(mysql)
* 2.2)如果当前订单的支付状态为未支付,则修改mysql中的订单信息,新增订单日志,恢复商品的库存,基于微信关闭订单
*/
System.out.println("关闭订单业务开启:"+orderId);
Order order = orderMapper.selectByPrimaryKey(orderId);
if (order == null){
throw new RuntimeException("订单不存在!");
}
if (!"0".equals(order.getPayStatus())){
System.out.println("当前订单不需要关闭");
return;
}
System.out.println("关闭订单校验通过:"+orderId);
//基于微信查询订单信息
Map wxQueryMap = (Map) payFeign.queryOrder(orderId).getData();
System.out.println("查询微信支付订单:"+wxQueryMap);
//如果订单的支付状态为已支付,进行数据补偿(mysql)
if ("SUCCESS".equals(wxQueryMap.get("trade_state"))){
this.u
没有合适的资源?快使用搜索试试~ 我知道了~
畅购是一个综合性的B2C平台, 基于ssm框架, 采用分布式系统架构。.zip
共2000个文件
png:529个
java:315个
js:308个
0 下载量 48 浏览量
2024-10-17
09:43:14
上传
评论
收藏 114.8MB ZIP 举报
温馨提示
计算机系毕业设计
资源推荐
资源详情
资源评论
收起资源包目录
畅购是一个综合性的B2C平台, 基于ssm框架, 采用分布式系统架构。.zip (2000个子文件)
AuthorizationServerConfig.class 7KB
Order.class 6KB
AuthServiceImpl.class 5KB
Spu.class 5KB
SeckillGoods.class 5KB
FescarAutoConfiguration.class 5KB
User.class 5KB
SkuInfo.class 5KB
WebSecurityConfig.class 4KB
Sku.class 4KB
ReturnOrder.class 4KB
SeckillOrder.class 4KB
ApplyTokenTest.class 4KB
OrderItem.class 4KB
SpuListener.class 3KB
RabbitMQConfig.class 3KB
ReturnOrderItem.class 3KB
AuthController.class 3KB
TaskHis.class 3KB
Task.class 3KB
UserDetailsServiceImpl.class 3KB
Address.class 3KB
BusinessListener.class 2KB
CreateJWTTest.class 2KB
CookieUtil.class 2KB
Ad.class 2KB
CustomUserAuthenticationConverter.class 2KB
FescarRMRequestFilter.class 2KB
Log.class 2KB
Log.class 2KB
Preferential.class 2KB
OrderLog.class 2KB
Pref.class 2KB
FescarRestInterceptor.class 2KB
ParseJwtTest.class 2KB
Category.class 2KB
LoginLog.class 2KB
Activity.class 2KB
OrderConfig.class 2KB
Para.class 2KB
Spec.class 2KB
Brand.class 2KB
Menu.class 1KB
Resource.class 1KB
Admin.class 1KB
ReturnCause.class 1KB
Template.class 1KB
Album.class 1KB
SkuFeign.class 1KB
OAuthApplication.class 1KB
UserJwt.class 1KB
AuthServiceImpl$1.class 1KB
FreightTemplate.class 1KB
PointLog.class 1KB
Cities.class 1KB
Areas.class 1KB
Goods.class 1KB
ApplyTokenTest$1.class 1KB
AuthToken.class 1KB
Role.class 973B
CategoryBrand.class 964B
Provinces.class 961B
PayFeign.class 950B
OrderFeign.class 899B
CanalApplication.class 801B
SecKillGoodsFeign.class 710B
CategoryFeign.class 670B
SpuFeign.class 663B
CartFeign.class 662B
SecKillOrderFeign.class 613B
UserFeign.class 569B
AddressFeign.class 492B
AuthService.class 348B
file.conf 1013B
file.conf 1013B
registry.conf 868B
registry.conf 868B
all.css 318KB
all.css 318KB
all.css 318KB
all.css 318KB
all.css 318KB
all.css 318KB
all.css 318KB
all.css 318KB
all.css 318KB
all.css 318KB
widget-jquery.autocomplete.css 16KB
widget-jquery.autocomplete.css 16KB
widget-jquery.autocomplete.css 16KB
widget-jquery.autocomplete.css 16KB
widget-jquery.autocomplete.css 16KB
widget-jquery.autocomplete.css 16KB
widget-jquery.autocomplete.css 16KB
widget-jquery.autocomplete.css 16KB
widget-jquery.autocomplete.css 16KB
widget-jquery.autocomplete.css 16KB
pages-JD-index.css 9KB
pages-JD-index.css 9KB
pages-JD-index.css 9KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
学术菜鸟小晨
- 粉丝: 1w+
- 资源: 5533
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功