JAVA全系列 教程
3762个小节阅读:7095.3k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
xxxxxxxxxx
CREATE TABLE `storage_tbl` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`commodity_code` varchar(255) DEFAULT NULL,
`count` int(11) DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `commodity_code` (`commodity_code`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
xxxxxxxxxx
@Data
@TableName("storage_tbl")
public class TStock {
@TableId( type = IdType.AUTO)
private Integer id;
@TableField("commodity_code")
private String commodityCode;
private Integer count;
}
TStockMapper
接口xxxxxxxxxx
public interface TStockMapper extends BaseMapper<TStock> {
}
xxxxxxxxxx
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BaseResponse implements Serializable {
private static final long serialVersionUID = 1L;
private Integer code ;
private String message;
private Object data;
public static BaseResponse success()
{
return BaseResponse.success("操作成功");
}
public static BaseResponse success(Object data)
{
return BaseResponse.success("操作成功", data);
}
public static BaseResponse error(String msg)
{
return BaseResponse.error(msg, null);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 成功消息
*/
public static BaseResponse success(String msg, Object data) {
return new BaseResponse(200, msg, data);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @return 成功消息
*/
public static BaseResponse success(String msg) {
return new BaseResponse(200, msg, null);
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 错误消息
*/
public static BaseResponse error(String msg, Object data)
{
return new BaseResponse(500, msg, data);
}
}
xxxxxxxxxx
/**
* 库存服务接口
*/
public interface IStorageService {
/**
* 扣除存储数量
*/
BaseResponse deduct(String commodityCode, int count);
}
xxxxxxxxxx
@DubboService
public class StorageServiceImpl implements IStorageService{
@Autowired
private TStockMapper tStockMapper;
/**
* 扣除存储数量
* @param commodityCode
* @param count 数量
*/
@Override
@Transactional
@SneakyThrows
public BaseResponse deduct(String commodityCode, int count) {
// 查询库存
LambdaQueryWrapper<TStock> tStockLambdaQueryWrapper = new LambdaQueryWrapper();
tStockLambdaQueryWrapper.eq(TStock::getCommodityCode,commodityCode);
TStock tStock = tStockMapper.selectOne(tStockLambdaQueryWrapper);
if (tStock == null){
return BaseResponse.error("商品不存在");
}
if (count > tStock.getCount()){
return BaseResponse.error("商品库存不足");
}
// 更新库存
LambdaUpdateWrapper<TStock> lu = new LambdaUpdateWrapper<TStock>();
lu.eq(TStock::getCommodityCode,commodityCode);
lu.set(TStock::getCount,tStock.getCount() - count);
int result = tStockMapper.update(null, lu);
if (result > 0){
return BaseResponse.success();
}else {
return BaseResponse.error("库存服务异常");
}
}
}