Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/com/daydreamdev/secondskill/common/MockConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.daydreamdev.secondskill.common;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
* Mock配置类
* 当配置文件中mock.enabled=true时生效
* @auther G.Fukang
* @date 6/10 14:36
*/
@Configuration
@ConditionalOnProperty(name = "mock.enabled", havingValue = "true")
public class MockConfig {
// 可以在这里配置mock相关的bean
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.daydreamdev.secondskill.common;

import com.daydreamdev.secondskill.common.stockWithRedis.RedisKeysConstant;
import com.daydreamdev.secondskill.common.StockWithRedis.RedisKeysConstant;
import com.daydreamdev.secondskill.common.utils.RedisPoolUtil;
import com.daydreamdev.secondskill.pojo.Stock;
import com.daydreamdev.secondskill.service.api.StockService;
Expand All @@ -26,9 +26,9 @@ public void run(ApplicationArguments args) throws Exception {
Stock stock = stockService.getStockById(1);

// 删除旧缓存
RedisPoolUtil.del(RedisKeysConstant.STOCK_COUNT + stock.getCount());
RedisPoolUtil.del(RedisKeysConstant.STOCK_SALE + stock.getSale());
RedisPoolUtil.del(RedisKeysConstant.STOCK_VERSION + stock.getVersion());
RedisPoolUtil.del(RedisKeysConstant.STOCK_COUNT + stock.getId());
RedisPoolUtil.del(RedisKeysConstant.STOCK_SALE + stock.getId());
RedisPoolUtil.del(RedisKeysConstant.STOCK_VERSION + stock.getId());
//缓存预热
int sid = stock.getId();
RedisPoolUtil.set(RedisKeysConstant.STOCK_COUNT + sid, String.valueOf(stock.getCount()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.daydreamdev.secondskill.common.stockWithRedis;
package com.daydreamdev.secondskill.common.StockWithRedis;

import com.daydreamdev.secondskill.common.utils.RedisPool;
import com.daydreamdev.secondskill.common.utils.RedisPoolUtil;
Expand Down Expand Up @@ -26,10 +26,10 @@ public static void updateStockWithRedis(Stock stock) {
jedis = RedisPool.getJedis();
// 开始事务
Transaction transaction = jedis.multi();
// 事务操作
RedisPoolUtil.decr(RedisKeysConstant.STOCK_COUNT + stock.getId());
RedisPoolUtil.incr(RedisKeysConstant.STOCK_SALE + stock.getId());
RedisPoolUtil.incr(RedisKeysConstant.STOCK_VERSION + stock.getId());
// 事务操作:直接使用同一个Jedis实例执行命令
transaction.decr(RedisKeysConstant.STOCK_COUNT + stock.getId());
transaction.incr(RedisKeysConstant.STOCK_SALE + stock.getId());
transaction.incr(RedisKeysConstant.STOCK_VERSION + stock.getId());
// 结束事务
List<Object> list = transaction.exec();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.daydreamdev.secondskill.common.stockWithRedis;
package com.daydreamdev.secondskill.common.StockWithRedis;

/**
* @auther G.Fukang
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.daydreamdev.secondskill.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Redis工具类的Mock实现
* 用于测试环境,模拟Redis的基本操作
* @auther G.Fukang
* @date 6/10 15:36
*/
@Slf4j
public class MockRedisPoolUtil {

private static Map<String, String> mockRedis = new HashMap<>();

/**
* 设置 key - value 值
*
* @param key
* @param value
*/
public static String set(String key, String value) {
log.info("Mock Redis set key:{} value:{}", key, value);
mockRedis.put(key, value);
return "OK";
}

/**
* 获取 key - value 值
*
* @param key
*/
public static String get(String key) {
log.info("Mock Redis get key:{}", key);
return mockRedis.get(key);
}

/**
* 删除 key - value 值
*
* @param key
*/
public static Long del(String key) {
log.info("Mock Redis del key:{}", key);
if (mockRedis.remove(key) != null) {
return 1L;
}
return 0L;
}

/**
* key - value 自增
*/
public static Long incr(String key) {
log.info("Mock Redis incr key:{}", key);
String value = mockRedis.get(key);
long num = 0;
if (value != null) {
num = Long.parseLong(value);
}
num++;
mockRedis.put(key, String.valueOf(num));
return num;
}

/**
* key - value 自减
*/
public static Long decr(String key) {
log.info("Mock Redis decr key:{}", key);
String value = mockRedis.get(key);
long num = 0;
if (value != null) {
num = Long.parseLong(value);
}
num--;
mockRedis.put(key, String.valueOf(num));
return num;
}

/**
* List - get 操作
*/
public static List<String> listGet(String key) {
log.info("Mock Redis listGet key:{}", key);
// 简单模拟list操作
String value = mockRedis.get(key);
List<String> result = new ArrayList<>();
if (value != null) {
result.add(value);
}
return result;
}

/**
* List - put 操作
*/
public static Long listPut(String key, String count, String sale, String version) {
log.info("Mock Redis listPut key:{}, count:{}, sale:{}, version:{}", key, count, sale, version);
// 简单模拟list操作
mockRedis.put(key + "_count", count);
mockRedis.put(key + "_sale", sale);
mockRedis.put(key + "_version", version);
return 3L;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package com.daydreamdev.secondskill.controller;

import com.daydreamdev.secondskill.pojo.Stock;
import com.daydreamdev.secondskill.service.api.StockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
* 后台管理系统控制器
* @auther G.Fukang
* @date 6/7 15:36
*/
@Slf4j
@Controller
@RequestMapping(value = "/admin")
public class AdminController {

private static final String success = "SUCCESS";
private static final String error = "ERROR";

@Autowired
private StockService stockService;

/**
* 获取所有商品列表
* @return List<Stock>
*/
@RequestMapping(value = "/product", method = RequestMethod.GET)
@ResponseBody
public List<Stock> getAllProducts() {
try {
return stockService.getAllStocks();
} catch (Exception e) {
log.error("Exception: ", e);
return null;
}
}

/**
* 添加商品
* @param stock
* @return String
*/
@RequestMapping(value = "/product", method = RequestMethod.POST)
@ResponseBody
public String addProduct(@RequestBody Stock stock) {
try {
// 设置默认状态为上架
if (stock.getStatus() == null) {
stock.setStatus(1);
}
// 设置初始销量为0
if (stock.getSale() == null) {
stock.setSale(0);
}
// 设置初始版本号为0
if (stock.getVersion() == null) {
stock.setVersion(0);
}
int res = stockService.addStock(stock);
return res == 1 ? success : error;
} catch (Exception e) {
log.error("Exception: ", e);
return error;
}
}

/**
* 更新商品状态(上架/下架)
* @param id
* @param status
* @return String
*/
@RequestMapping(value = "/product/status", method = RequestMethod.PUT)
@ResponseBody
public String updateProductStatus(int id, int status) {
try {
Stock stock = stockService.getStockById(id);
if (stock == null) {
return error;
}
stock.setStatus(status);
int res = stockService.updateStockById(stock);
return res == 1 ? success : error;
} catch (Exception e) {
log.error("Exception: ", e);
return error;
}
}

/**
* 更新商品库存
* @param id
* @param count
* @return String
*/
@RequestMapping(value = "/stock", method = RequestMethod.PUT)
@ResponseBody
public String updateStock(int id, int count) {
try {
Stock stock = stockService.getStockById(id);
if (stock == null) {
return error;
}
stock.setCount(count);
int res = stockService.updateStockById(stock);
return res == 1 ? success : error;
} catch (Exception e) {
log.error("Exception: ", e);
return error;
}
}

/**
* 删除商品
* @param id
* @return String
*/
@RequestMapping(value = "/product", method = RequestMethod.DELETE)
@ResponseBody
public String deleteProduct(int id) {
try {
int res = stockService.deleteStockById(id);
return res == 1 ? success : error;
} catch (Exception e) {
log.error("Exception: ", e);
return error;
}
}
}
19 changes: 14 additions & 5 deletions src/main/java/com/daydreamdev/secondskill/dao/StockMapper.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.daydreamdev.secondskill.dao;

import com.daydreamdev.secondskill.pojo.Stock;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* @auther G.Fukang
Expand All @@ -22,11 +21,21 @@ public interface StockMapper {
@Select("SELECT * FROM stock WHERE id = #{id, jdbcType = INTEGER}")
Stock selectByPrimaryKey(@Param("id") int id);

@Select("SELECT * FROM stock")
List<Stock> selectAll();

@Insert("INSERT INTO stock (name, count, sale, version, status) VALUES " +
"(#{name, jdbcType = VARCHAR}, #{count, jdbcType = INTEGER}, #{sale, jdbcType = INTEGER}, #{version, jdbcType = INTEGER}, #{status, jdbcType = INTEGER})")
int insertSelective(Stock stock);

@Update("UPDATE stock SET count = #{count, jdbcType = INTEGER}, name = #{name, jdbcType = VARCHAR}, " +
"sale = #{sale,jdbcType = INTEGER},version = #{version,jdbcType = INTEGER} " +
"sale = #{sale,jdbcType = INTEGER},version = #{version,jdbcType = INTEGER}, status = #{status, jdbcType = INTEGER} " +
"WHERE id = #{id, jdbcType = INTEGER}")
int updateByPrimaryKeySelective(Stock stock);

@Delete("DELETE FROM stock WHERE id = #{id, jdbcType = INTEGER}")
int deleteByPrimaryKey(@Param("id") int id);

/**
* 乐观锁 version
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.daydreamdev.secondskill.pojo;

import lombok.Getter;
import lombok.Setter;

import java.util.Date;

/**
* 设备指纹实体类
* 用于记录用户设备信息
* @auther G.Fukang
* @date 6/10 16:36
*/
@Getter
@Setter
public class DeviceFingerprint {

private Long id;
private String userId;
private String deviceId;
private String deviceType;
private String os;
private String browser;
private String ipAddress;
private String userAgent;
private String networkType;
private Date createTime;
private Date updateTime;
}
2 changes: 2 additions & 0 deletions src/main/java/com/daydreamdev/secondskill/pojo/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ public class Stock {
private Integer sale;

private Integer version;

private Integer status; // 0: 下架, 1: 上架
}
Loading