package com.seecen.redis.controller;
import com.seecen.redis.aop.Log;
import com.seecen.redis.aop.LogType;
import com.seecen.redis.entity.TAdmin;
import com.seecen.redis.service.AdminService;
import lombok.extern.slf4j.Slf4j;
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.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;/**
* @author bigpeng
* @create 2020-07-21-16:18*/@Controller
@Slf4jpublic classIndexController {
@AutowiredprivateRedisTemplate redisTemplate;
@AutowiredprivateAdminService adminService;
@AutowiredprivateRabbitTemplate rabbitTemplate;
@ResponseBody
@GetMapping("/order/{product}")public String order(@PathVariable("product") String product){//模拟一个订单,使用map存储数据
Map map = new HashMap<>();
map.put("product",product);
String orderId=UUID.randomUUID().toString();
map.put("orderId",orderId);
map.put("status","0");//只发状态 0:未支付 1:已支付 -1:已取消//todo 将订单记录插入数据库//redisTemplate.opsForValue().set("order:"+orderId,map);
redisTemplate.opsForHash().putAll("order:"+orderId,map);//发送mq消息到超时队列
rabbitTemplate.convertAndSend("order.ttl.exchange","order.ttl.cancel",
map);
log.info("下单成功,订单号:"+orderId);return "下单成功,订单号:"+orderId;
}
@ResponseBody
@GetMapping("/order/pay/{orderId}")public String pay(@PathVariable("orderId") String orderId){
Boolean hasKey= redisTemplate.hasKey("order:" +orderId);if(hasKey){
redisTemplate.opsForHash().put("order:"+orderId,"status","1");
log.info("订单:{}支付成功",orderId);
}return "订单:"+orderId+"支付成功!";
}
}