init
This commit is contained in:
+37
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springblade.seata.storage;
|
||||
|
||||
import org.springblade.common.constant.LauncherConstant;
|
||||
import org.springblade.core.cloud.feign.EnableBladeFeign;
|
||||
import org.springblade.core.launch.BladeApplication;
|
||||
import org.springblade.core.transaction.annotation.SeataCloudApplication;
|
||||
|
||||
/**
|
||||
* Storage启动器
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
@EnableBladeFeign
|
||||
@SeataCloudApplication
|
||||
public class SeataStorageApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
BladeApplication.run(LauncherConstant.APPLICATION_SEATA_STORAGE_NAME, SeataStorageApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package org.springblade.seata.storage.controller;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springblade.seata.storage.service.IStorageService;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* StorageController
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("storage")
|
||||
public class StorageController {
|
||||
|
||||
private final IStorageService storageService;
|
||||
|
||||
/**
|
||||
* 减库存
|
||||
*
|
||||
* @param commodityCode 商品代码
|
||||
* @param count 数量
|
||||
*/
|
||||
@RequestMapping(path = "/deduct")
|
||||
public int deduct(String commodityCode, Integer count) {
|
||||
return storageService.deduct(commodityCode, count);
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package org.springblade.seata.storage.entity;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* storage
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
@Data
|
||||
@TableName("tb_storage")
|
||||
public class Storage implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Long id;
|
||||
private String commodityCode;
|
||||
private Long count;
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package org.springblade.seata.storage.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.springblade.seata.storage.entity.Storage;
|
||||
|
||||
/**
|
||||
* StorageMapper
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
public interface StorageMapper extends BaseMapper<Storage> {
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="org.springblade.seata.storage.mapper.StorageMapper">
|
||||
|
||||
</mapper>
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package org.springblade.seata.storage.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.springblade.seata.storage.entity.Storage;
|
||||
|
||||
/**
|
||||
* IStorageService
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
public interface IStorageService extends IService<Storage> {
|
||||
|
||||
/**
|
||||
* 减库存
|
||||
*
|
||||
* @param commodityCode 商品代码
|
||||
* @param count 数量
|
||||
* @return boolean
|
||||
*/
|
||||
int deduct(String commodityCode, int count);
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package org.springblade.seata.storage.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springblade.seata.storage.entity.Storage;
|
||||
import org.springblade.seata.storage.mapper.StorageMapper;
|
||||
import org.springblade.seata.storage.service.IStorageService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* StorageServiceImpl
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
@Service
|
||||
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements IStorageService {
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int deduct(String commodityCode, int count) {
|
||||
Storage storage = baseMapper.selectOne(Wrappers.<Storage>query().lambda().eq(Storage::getCommodityCode, commodityCode));
|
||||
if (storage.getCount() < count) {
|
||||
throw new RuntimeException("超过库存数,扣除失败!");
|
||||
}
|
||||
storage.setCount(storage.getCount() - count);
|
||||
return baseMapper.updateById(storage);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#服务器端口
|
||||
server:
|
||||
port: 8502
|
||||
|
||||
#数据源配置
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mysql://localhost:3306/seata_storage?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
|
||||
username: root
|
||||
password: root
|
||||
|
||||
#常规配置
|
||||
blade:
|
||||
mybatis-plus:
|
||||
tenant-mode: false
|
||||
Reference in New Issue
Block a user