From 4888dfaf2a780a97238f0e3588fef7b33211273a Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 12 May 2019 18:47:45 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=90=8E=E7=AB=AF=EF=BC=9A=E5=BC=95?= =?UTF-8?q?=E5=85=A5=E5=88=86=E5=B8=83=E5=BC=8F=E4=BA=8B=E5=8A=A1=E6=A1=86?= =?UTF-8?q?=E6=9E=B6=20seata=20=E6=A1=86=E6=9E=B6=E3=80=82=20-=20=E5=90=8E?= =?UTF-8?q?=E7=AB=AF=EF=BC=9A=E7=9B=AE=E5=89=8D=E8=AE=A2=E5=8D=95=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E6=97=B6=EF=BC=8C=E5=A6=82=E6=9E=9C=E5=8F=91=E7=94=9F?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=EF=BC=8C=E4=BC=9A=E5=9B=9E=E6=BB=9A=E4=BC=98?= =?UTF-8?q?=E6=83=A0=E5=8A=B5=E7=9A=84=E4=BD=BF=E7=94=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mobile-web/src/page/shipping/order.vue | 1 + order/order-application/pom.xml | 12 +-- .../controller/users/OrderController.java | 14 +++- order/order-service-impl/pom.xml | 36 ++++---- .../biz/config/DatabaseConfiguration.java | 63 +++++++++++++- .../order/biz/config/SeataConfiguration.java | 9 ++ .../mall/order/biz/dao/OrderMapper.java | 10 +-- .../mall/order/biz/dataobject/OrderDO.java | 2 + .../order/biz/service/OrderServiceImpl.java | 10 ++- .../main/resources/config/application.yaml | 1 + .../src/main/resources/mapper/OrderMapper.xml | 45 ++-------- .../resources/mapper/OrderRecipientMapper.xml | 2 +- pom.xml | 2 +- promotion/promotion-application/pom.xml | 1 - .../config/SwaggerConfiguration.java | 36 -------- .../mall/promotion/api/CouponService.java | 2 +- promotion/promotion-service-impl/pom.xml | 84 +++++++++++++------ .../biz/config/DatabaseConfiguration.java | 66 ++++++++++++++- .../biz/service/CouponServiceImpl.java | 10 +-- .../main/resources/config/application.yaml | 15 +++- 20 files changed, 275 insertions(+), 146 deletions(-) create mode 100644 order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/config/SeataConfiguration.java delete mode 100644 promotion/promotion-application/src/main/java/cn/iocoder/mall/promotion/application/config/SwaggerConfiguration.java diff --git a/mobile-web/src/page/shipping/order.vue b/mobile-web/src/page/shipping/order.vue index 993e4699e..b87e27851 100644 --- a/mobile-web/src/page/shipping/order.vue +++ b/mobile-web/src/page/shipping/order.vue @@ -173,6 +173,7 @@ createOrder({ orderItems, userAddressId, + couponCardId, remark, }).then(result => { if (result) { diff --git a/order/order-application/pom.xml b/order/order-application/pom.xml index 5ada24e2a..fca546efc 100644 --- a/order/order-application/pom.xml +++ b/order/order-application/pom.xml @@ -74,11 +74,13 @@ org.springframework.boot spring-boot-starter-actuator - - org.springframework.boot - spring-boot-devtools - true - + + + + + + + de.codecentric spring-boot-admin-starter-client diff --git a/order/order-application/src/main/java/cn/iocoder/mall/order/application/controller/users/OrderController.java b/order/order-application/src/main/java/cn/iocoder/mall/order/application/controller/users/OrderController.java index 730594622..8b718e357 100644 --- a/order/order-application/src/main/java/cn/iocoder/mall/order/application/controller/users/OrderController.java +++ b/order/order-application/src/main/java/cn/iocoder/mall/order/application/controller/users/OrderController.java @@ -17,6 +17,8 @@ import cn.iocoder.mall.order.application.convert.CartConvert; import cn.iocoder.mall.order.application.convert.OrderConvertAPP; import cn.iocoder.mall.order.application.po.user.OrderCreatePO; import cn.iocoder.mall.order.application.vo.UsersOrderConfirmCreateVO; +import cn.iocoder.mall.promotion.api.CouponService; +import cn.iocoder.mall.promotion.api.bo.CouponCardAvailableBO; import cn.iocoder.mall.user.sdk.context.UserSecurityContextHolder; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -29,6 +31,8 @@ import java.util.Collections; import java.util.List; import java.util.stream.Collectors; +import static cn.iocoder.common.framework.vo.CommonResult.success; + /** * 订单API(users) * @@ -46,6 +50,8 @@ public class OrderController { private CartService cartService; @Reference(validation = "true", version = "${dubbo.consumer.DataDictService.version}") private DataDictService dataDictService; + @Reference(validation = "true", version = "${dubbo.consumer.CouponService.version}") + private CouponService couponService; @GetMapping("order_page") @ApiOperation("订单分页") @@ -97,14 +103,18 @@ public class OrderController { public CommonResult getConfirmCreateOrder(@RequestParam("skuId") Integer skuId, @RequestParam("quantity") Integer quantity, @RequestParam(value = "couponCardId", required = false) Integer couponCardId) { + Integer userId = UserSecurityContextHolder.getContext().getUserId(); // 创建 CalcOrderPriceDTO 对象,并执行价格计算 CalcOrderPriceDTO calcOrderPriceDTO = new CalcOrderPriceDTO() - .setUserId(UserSecurityContextHolder.getContext().getUserId()) + .setUserId(userId) .setItems(Collections.singletonList(new CalcOrderPriceDTO.Item(skuId, quantity, true))) .setCouponCardId(couponCardId); CalcOrderPriceBO calcOrderPrice = cartService.calcOrderPrice(calcOrderPriceDTO); + // 获得优惠劵 + List couponCards = couponService.getCouponCardList(userId, + CartConvert.INSTANCE.convertList(calcOrderPrice.getItemGroups())); // 执行数据拼装 - return CommonResult.success(CartConvert.INSTANCE.convert(calcOrderPrice)); + return success(CartConvert.INSTANCE.convert(calcOrderPrice).setCouponCards(couponCards)); } @PostMapping("confirm_receiving") diff --git a/order/order-service-impl/pom.xml b/order/order-service-impl/pom.xml index 881b6e0b1..7d9dfd68e 100644 --- a/order/order-service-impl/pom.xml +++ b/order/order-service-impl/pom.xml @@ -30,7 +30,6 @@ cn.iocoder.mall pay-service-api 1.0-SNAPSHOT - compile cn.iocoder.mall @@ -41,13 +40,11 @@ cn.iocoder.mall system-service-api 1.0-SNAPSHOT - compile cn.iocoder.mall user-service-api 1.0-SNAPSHOT - compile cn.iocoder.mall @@ -72,10 +69,17 @@ mysql mysql-connector-java - - org.springframework.boot - spring-boot-starter-jdbc - + + + + + + + + + + + @@ -94,21 +98,25 @@ org.apache.dubbo dubbo-spring-boot-starter + com.baomidou mybatis-plus-boot-starter - ${mybatis-plus.version} - - org.mybatis.spring.boot - mybatis-spring-boot-starter - 2.0.0 - org.apache.rocketmq rocketmq-spring-boot-starter - 2.0.1 + + + + io.seata + seata-spring + + + + io.seata + seata-dubbo diff --git a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/config/DatabaseConfiguration.java b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/config/DatabaseConfiguration.java index 1475b5a4f..e0da6e15a 100644 --- a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/config/DatabaseConfiguration.java +++ b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/config/DatabaseConfiguration.java @@ -1,9 +1,20 @@ package cn.iocoder.mall.order.biz.config; +import com.alibaba.druid.pool.DruidDataSource; +import io.seata.rm.datasource.DataSourceProxy; +import io.seata.spring.annotation.GlobalTransactionScanner; import org.mybatis.spring.annotation.MapperScan; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; import org.springframework.transaction.annotation.EnableTransactionManagement; +import javax.sql.DataSource; + @Configuration @MapperScan("cn.iocoder.mall.order.biz.dao") // 扫描对应的 Mapper 接口 @EnableTransactionManagement(proxyTargetClass = true) // 启动事务管理。为什么使用 proxyTargetClass 参数,参见 https://blog.csdn.net/huang_550/article/details/76492600 @@ -11,4 +22,54 @@ public class DatabaseConfiguration { // 数据源,使用 HikariCP -} \ No newline at end of file + @Value("${spring.application.name}") + private String applicationId; + + @Autowired + private DataSourceProperties dataSourceProperties; + + @Bean +// @Primary + public DruidDataSource druidDataSource(){ + DruidDataSource druidDataSource = new DruidDataSource(); + druidDataSource.setUrl(dataSourceProperties.getUrl()); + druidDataSource.setUsername(dataSourceProperties.getUsername()); + druidDataSource.setPassword(dataSourceProperties.getPassword()); + druidDataSource.setDriverClassName(dataSourceProperties.getDriverClassName()); + druidDataSource.setInitialSize(0); + druidDataSource.setMaxActive(180); + druidDataSource.setMaxWait(60000); + druidDataSource.setMinIdle(0); + druidDataSource.setValidationQuery("Select 1 from DUAL"); + druidDataSource.setTestOnBorrow(false); + druidDataSource.setTestOnReturn(false); + druidDataSource.setTestWhileIdle(true); + druidDataSource.setTimeBetweenEvictionRunsMillis(60000); + druidDataSource.setMinEvictableIdleTimeMillis(25200000); + druidDataSource.setRemoveAbandoned(true); + druidDataSource.setRemoveAbandonedTimeout(1800); + druidDataSource.setLogAbandoned(true); + return druidDataSource; + } + + @ConfigurationProperties(prefix = "spring.datasource") + @Primary + @Bean("dataSource") +// @Bean + public DataSource dataSource(DruidDataSource dataSource) { + return new DataSourceProxy(dataSource); + } + + /** + * 注册一个StatViewServlet + * + * @return global transaction scanner + */ + @Bean + public GlobalTransactionScanner globalTransactionScanner() { + return new GlobalTransactionScanner(applicationId, "my_test_tx_group"); + // TODO 芋艿,txServiceGroup 后续要编辑下 + } + + +} diff --git a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/config/SeataConfiguration.java b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/config/SeataConfiguration.java new file mode 100644 index 000000000..ace05c0cf --- /dev/null +++ b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/config/SeataConfiguration.java @@ -0,0 +1,9 @@ +package cn.iocoder.mall.order.biz.config; + +import org.springframework.context.annotation.Configuration; + +@Configuration +public class SeataConfiguration { + + +} diff --git a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/dao/OrderMapper.java b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/dao/OrderMapper.java index e45488336..642e96f48 100644 --- a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/dao/OrderMapper.java +++ b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/dao/OrderMapper.java @@ -2,6 +2,7 @@ package cn.iocoder.mall.order.biz.dao; import cn.iocoder.mall.order.api.dto.OrderQueryDTO; import cn.iocoder.mall.order.biz.dataobject.OrderDO; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @@ -14,14 +15,7 @@ import java.util.List; * @time 2019-03-16 15:09 */ @Repository -public interface OrderMapper { - - /** - * 插入数据 - * - * @param orderDO - */ - void insert(OrderDO orderDO); +public interface OrderMapper extends BaseMapper { /** * 更新 - 根据 id 更新 diff --git a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/dataobject/OrderDO.java b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/dataobject/OrderDO.java index f16e9b7a1..554176fcc 100644 --- a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/dataobject/OrderDO.java +++ b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/dataobject/OrderDO.java @@ -1,6 +1,7 @@ package cn.iocoder.mall.order.biz.dataobject; import cn.iocoder.common.framework.dataobject.DeletableDO; +import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; import lombok.experimental.Accessors; @@ -14,6 +15,7 @@ import java.util.Date; */ @Data @Accessors(chain = true) +@TableName(value = "orders") public class OrderDO extends DeletableDO { /** diff --git a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/service/OrderServiceImpl.java b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/service/OrderServiceImpl.java index 151cfa4ea..436cb04d7 100644 --- a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/service/OrderServiceImpl.java +++ b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/service/OrderServiceImpl.java @@ -21,6 +21,7 @@ import cn.iocoder.mall.product.api.bo.ProductSkuDetailBO; import cn.iocoder.mall.promotion.api.CouponService; import cn.iocoder.mall.user.api.UserAddressService; import cn.iocoder.mall.user.api.bo.UserAddressBO; +import io.seata.spring.annotation.GlobalTransactional; import org.apache.dubbo.config.annotation.Reference; import com.google.common.collect.Lists; import org.springframework.beans.factory.annotation.Autowired; @@ -210,6 +211,7 @@ public class OrderServiceImpl implements OrderService { } @Override + @GlobalTransactional @Transactional // TODO 芋艿,先不考虑分布式事务的问题 public CommonResult createOrder(OrderCreateDTO orderCreateDTO) { Integer userId = orderCreateDTO.getUserId(); @@ -284,8 +286,8 @@ public class OrderServiceImpl implements OrderService { .setHasReturnExchange(OrderHasReturnExchangeEnum.NO.getValue()) .setRemark(Optional.ofNullable(orderCreateDTO.getRemark()).orElse("")); orderDO.setDeleted(DeletedStatusEnum.DELETED_NO.getValue()); - orderDO.setCreateTime(new Date()); - orderDO.setUpdateTime(null); +// orderDO.setCreateTime(new Date()); +// orderDO.setUpdateTime(null); orderMapper.insert(orderDO); // 收件人信息 @@ -321,6 +323,10 @@ public class OrderServiceImpl implements OrderService { // 一次性插入 orderItemMapper.insert(orderItemDOList); + if (true) { + throw new RuntimeException("测试 seata 事务回滚"); + } + // 创建预订单 createPayTransaction(orderDO, orderItemDOList, orderCreateDTO.getIp()); diff --git a/order/order-service-impl/src/main/resources/config/application.yaml b/order/order-service-impl/src/main/resources/config/application.yaml index b583380eb..6cb9ee36e 100644 --- a/order/order-service-impl/src/main/resources/config/application.yaml +++ b/order/order-service-impl/src/main/resources/config/application.yaml @@ -38,6 +38,7 @@ dubbo: CartService: version: 1.0.0 consumer: + timeout: 120000 # 设置长一点,方便调试代码 ProductSpuService: version: 1.0.0 PromotionActivityService: diff --git a/order/order-service-impl/src/main/resources/mapper/OrderMapper.xml b/order/order-service-impl/src/main/resources/mapper/OrderMapper.xml index 87829dad8..3ad48e677 100644 --- a/order/order-service-impl/src/main/resources/mapper/OrderMapper.xml +++ b/order/order-service-impl/src/main/resources/mapper/OrderMapper.xml @@ -9,26 +9,6 @@ status, remark, create_time, update_time, `deleted` - - - INSERT INTO `order` ( - user_id, order_no, buy_price, discount_price, logistics_price, present_price, pay_amount, - payment_time, delivery_time, receiver_time, closing_time, - has_return_exchange, - status, remark, create_time, update_time, `deleted` - ) VALUES ( - #{userId}, #{orderNo}, #{buyPrice}, #{discountPrice}, #{logisticsPrice}, #{presentPrice}, #{payAmount}, - #{paymentTime}, #{deliveryTime}, #{receiverTime}, #{closingTime}, - #{hasReturnExchange}, - #{status}, #{remark}, #{createTime}, #{updateTime}, #{deleted} - ) - - - @@ -86,9 +66,6 @@ - UPDATE `order` @@ -112,9 +89,6 @@ AND status = #{status} - - AND `status` = #{status} @@ -147,26 +118,22 @@ - - diff --git a/order/order-service-impl/src/main/resources/mapper/OrderRecipientMapper.xml b/order/order-service-impl/src/main/resources/mapper/OrderRecipientMapper.xml index c3a12f08a..77c893b0b 100644 --- a/order/order-service-impl/src/main/resources/mapper/OrderRecipientMapper.xml +++ b/order/order-service-impl/src/main/resources/mapper/OrderRecipientMapper.xml @@ -16,7 +16,7 @@ `type`, create_time, update_time ) VALUES ( #{orderId}, #{areaNo}, #{name}, #{mobile}, #{address}, - #{type}, #{createTime}, #{updateTime} + #{type}, #{createTime,jdbcType=TIMESTAMP} , #{updateTime} ) diff --git a/pom.xml b/pom.xml index 363510e39..0b9528a1c 100644 --- a/pom.xml +++ b/pom.xml @@ -30,7 +30,7 @@ 2.1.3.RELEASE 2.7.1 - 5.1.47 + 5.1.46 1.3.0.Final 2.13.0 diff --git a/promotion/promotion-application/pom.xml b/promotion/promotion-application/pom.xml index b6b11aac4..a83040df4 100644 --- a/promotion/promotion-application/pom.xml +++ b/promotion/promotion-application/pom.xml @@ -22,7 +22,6 @@ mall-spring-boot 1.0-SNAPSHOT - cn.iocoder.mall product-service-api diff --git a/promotion/promotion-application/src/main/java/cn/iocoder/mall/promotion/application/config/SwaggerConfiguration.java b/promotion/promotion-application/src/main/java/cn/iocoder/mall/promotion/application/config/SwaggerConfiguration.java deleted file mode 100644 index 7923969c5..000000000 --- a/promotion/promotion-application/src/main/java/cn/iocoder/mall/promotion/application/config/SwaggerConfiguration.java +++ /dev/null @@ -1,36 +0,0 @@ -package cn.iocoder.mall.promotion.application.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import springfox.documentation.builders.ApiInfoBuilder; -import springfox.documentation.builders.PathSelectors; -import springfox.documentation.builders.RequestHandlerSelectors; -import springfox.documentation.service.ApiInfo; -import springfox.documentation.spi.DocumentationType; -import springfox.documentation.spring.web.plugins.Docket; -import springfox.documentation.swagger2.annotations.EnableSwagger2; - -@Configuration -@EnableSwagger2 // TODO 生产环境时,禁用掉。 -public class SwaggerConfiguration { - - @Bean - public Docket createRestApi() { - return new Docket(DocumentationType.SWAGGER_2) - .apiInfo(apiInfo()) - .select() - .apis(RequestHandlerSelectors.basePackage("cn.iocoder.mall.promotion.application.controller")) - .paths(PathSelectors.any()) - .build(); - } - - private ApiInfo apiInfo() { - return new ApiInfoBuilder() - .title("营销子系统") - .description("营销子系统") - .termsOfServiceUrl("http://www.iocoder.cn") - .version("1.0.0") - .build(); - } - -} \ No newline at end of file diff --git a/promotion/promotion-service-api/src/main/java/cn/iocoder/mall/promotion/api/CouponService.java b/promotion/promotion-service-api/src/main/java/cn/iocoder/mall/promotion/api/CouponService.java index 18ff6b6b4..406d2772d 100644 --- a/promotion/promotion-service-api/src/main/java/cn/iocoder/mall/promotion/api/CouponService.java +++ b/promotion/promotion-service-api/src/main/java/cn/iocoder/mall/promotion/api/CouponService.java @@ -80,7 +80,7 @@ public interface CouponService { * @return 是否成功 */ Boolean useCouponCard(Integer userId, - @NotNull(message = "优惠劵编号不能为空") Integer couponCardId); + @NotNull(message = "优惠劵编号不能为空") Integer couponCardId); /** * 取消优惠劵的使用 diff --git a/promotion/promotion-service-impl/pom.xml b/promotion/promotion-service-impl/pom.xml index b995b7ec9..ed8ae8e00 100644 --- a/promotion/promotion-service-impl/pom.xml +++ b/promotion/promotion-service-impl/pom.xml @@ -28,6 +28,35 @@ 1.0-SNAPSHOT + + com.google.guava + guava + + + com.xuxueli + xxl-job-core + + + + mysql + mysql-connector-java + + + + + + + + + + + + + + + + + org.apache.dubbo dubbo @@ -46,50 +75,53 @@ - mysql - mysql-connector-java - - - org.springframework.boot - spring-boot-starter-jdbc + com.baomidou + mybatis-plus-boot-starter - logback-classic - ch.qos.logback - - - spring-boot-starter-logging org.springframework.boot + spring-boot-starter-jdbc - - org.mybatis.spring.boot - mybatis-spring-boot-starter - - - - com.google.guava - guava - - - - com.xuxueli - xxl-job-core - - org.apache.rocketmq rocketmq-spring-boot-starter + + io.seata + seata-spring + + + + io.seata + seata-dubbo + + org.springframework.boot spring-boot-starter-test test + + org.springframework + spring-tx + 5.1.3.RELEASE + + + org.springframework + spring-jdbc + 5.1.3.RELEASE + + + + + + + diff --git a/promotion/promotion-service-impl/src/main/java/cn/iocoder/mall/promotion/biz/config/DatabaseConfiguration.java b/promotion/promotion-service-impl/src/main/java/cn/iocoder/mall/promotion/biz/config/DatabaseConfiguration.java index 82f158edd..a98fefa88 100644 --- a/promotion/promotion-service-impl/src/main/java/cn/iocoder/mall/promotion/biz/config/DatabaseConfiguration.java +++ b/promotion/promotion-service-impl/src/main/java/cn/iocoder/mall/promotion/biz/config/DatabaseConfiguration.java @@ -1,14 +1,78 @@ package cn.iocoder.mall.promotion.biz.config; +import com.alibaba.druid.pool.DruidDataSource; +import io.seata.rm.datasource.DataSourceProxy; +import io.seata.spring.annotation.GlobalTransactionScanner; import org.mybatis.spring.annotation.MapperScan; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; import org.springframework.transaction.annotation.EnableTransactionManagement; +import javax.sql.DataSource; + @Configuration @MapperScan("cn.iocoder.mall.promotion.biz.dao") // 扫描对应的 Mapper 接口 @EnableTransactionManagement(proxyTargetClass = true) // 启动事务管理。为什么使用 proxyTargetClass 参数,参见 https://blog.csdn.net/huang_550/article/details/76492600 +@EnableConfigurationProperties(DataSourceProperties.class) public class DatabaseConfiguration { // 数据源,使用 HikariCP -} \ No newline at end of file + @Value("${spring.application.name}") + private String applicationId; + + @Autowired + private DataSourceProperties dataSourceProperties; + +// @Bean // TODO 芋艿,加了就一直报错,后面在找原因。 +// @Primary + public DruidDataSource druidDataSource(){ + DruidDataSource druidDataSource = new DruidDataSource(); + druidDataSource.setUrl(dataSourceProperties.getUrl()); + druidDataSource.setUsername(dataSourceProperties.getUsername()); + druidDataSource.setPassword(dataSourceProperties.getPassword()); + druidDataSource.setDriverClassName(dataSourceProperties.getDriverClassName()); + druidDataSource.setInitialSize(0); + druidDataSource.setMaxActive(180); + druidDataSource.setMaxWait(60000); + druidDataSource.setMinIdle(0); + druidDataSource.setValidationQuery("Select 1 from DUAL"); + druidDataSource.setTestOnBorrow(false); + druidDataSource.setTestOnReturn(false); + druidDataSource.setTestWhileIdle(true); + druidDataSource.setTimeBetweenEvictionRunsMillis(60000); + druidDataSource.setMinEvictableIdleTimeMillis(25200000); + druidDataSource.setRemoveAbandoned(true); + druidDataSource.setRemoveAbandonedTimeout(1800); + druidDataSource.setLogAbandoned(true); + return druidDataSource; + } + + @ConfigurationProperties(prefix = "spring.datasource") + @Primary + @Bean("dataSource") +// @Bean + public DataSource dataSource() { + DruidDataSource dataSource = druidDataSource(); + + return new DataSourceProxy(dataSource); + } + + /** + * 注册一个StatViewServlet + * + * @return global transaction scanner + */ + @Bean + public GlobalTransactionScanner globalTransactionScanner() { + return new GlobalTransactionScanner(applicationId, "my_test_tx_group"); + // TODO 芋艿,txServiceGroup 后续要编辑下 + } + +} diff --git a/promotion/promotion-service-impl/src/main/java/cn/iocoder/mall/promotion/biz/service/CouponServiceImpl.java b/promotion/promotion-service-impl/src/main/java/cn/iocoder/mall/promotion/biz/service/CouponServiceImpl.java index 234d1149f..9c4285782 100644 --- a/promotion/promotion-service-impl/src/main/java/cn/iocoder/mall/promotion/biz/service/CouponServiceImpl.java +++ b/promotion/promotion-service-impl/src/main/java/cn/iocoder/mall/promotion/biz/service/CouponServiceImpl.java @@ -242,14 +242,14 @@ public class CouponServiceImpl implements CouponService { if (!userId.equals(card.getUserId())) { throw ServiceExceptionUtil.exception(PromotionErrorCodeEnum.COUPON_CARD_ERROR_USER.getCode()); } - if (CouponCardStatusEnum.UNUSED.getValue().equals(card.getStatus())) { + if (!CouponCardStatusEnum.UNUSED.getValue().equals(card.getStatus())) { throw ServiceExceptionUtil.exception(PromotionErrorCodeEnum.COUPON_CARD_STATUS_NOT_UNUSED.getCode()); } if (DateUtil.isBetween(card.getValidStartTime(), card.getValidEndTime())) { // 为避免定时器没跑,实际优惠劵已经过期 throw ServiceExceptionUtil.exception(PromotionErrorCodeEnum.COUPON_CARD_STATUS_NOT_UNUSED.getCode()); } // 更新优惠劵已使用 - int updateCount = couponCardMapper.updateByIdAndStatus(card.getId(), CouponCardStatusEnum.USED.getValue(), + int updateCount = couponCardMapper.updateByIdAndStatus(card.getId(), CouponCardStatusEnum.UNUSED.getValue(), new CouponCardDO().setStatus(CouponCardStatusEnum.USED.getValue()).setUsedTime(new Date())); if (updateCount == 0) { throw ServiceExceptionUtil.exception(PromotionErrorCodeEnum.COUPON_CARD_STATUS_NOT_UNUSED.getCode()); @@ -267,12 +267,12 @@ public class CouponServiceImpl implements CouponService { if (!userId.equals(card.getUserId())) { throw ServiceExceptionUtil.exception(PromotionErrorCodeEnum.COUPON_CARD_ERROR_USER.getCode()); } - if (CouponCardStatusEnum.USED.getValue().equals(card.getStatus())) { + if (!CouponCardStatusEnum.USED.getValue().equals(card.getStatus())) { throw ServiceExceptionUtil.exception(PromotionErrorCodeEnum.COUPON_CARD_STATUS_NOT_USED.getCode()); } // 更新优惠劵已使用 - int updateCount = couponCardMapper.updateByIdAndStatus(card.getId(), CouponCardStatusEnum.UNUSED.getValue(), - new CouponCardDO().setStatus(CouponCardStatusEnum.USED.getValue())); // TODO 芋艿,usedTime 未设置空,后面处理。 + int updateCount = couponCardMapper.updateByIdAndStatus(card.getId(), CouponCardStatusEnum.USED.getValue(), + new CouponCardDO().setStatus(CouponCardStatusEnum.UNUSED.getValue())); // TODO 芋艿,usedTime 未设置空,后面处理。 if (updateCount == 0) { throw ServiceExceptionUtil.exception(PromotionErrorCodeEnum.COUPON_CARD_STATUS_NOT_USED.getCode()); } diff --git a/promotion/promotion-service-impl/src/main/resources/config/application.yaml b/promotion/promotion-service-impl/src/main/resources/config/application.yaml index 41426d793..73ba7adc2 100644 --- a/promotion/promotion-service-impl/src/main/resources/config/application.yaml +++ b/promotion/promotion-service-impl/src/main/resources/config/application.yaml @@ -7,9 +7,18 @@ spring: password: ${MALL_MYSQL_PASSWORD} # mybatis -mybatis: - config-location: classpath:mybatis-config.xml - mapper-locations: classpath:mapper/*.xml +#mybatis: +# config-location: classpath:mybatis-config.xml +# mapper-locations: classpath:mapper/*.xml +# type-aliases-package: cn.iocoder.mall.promotion.biz.dataobject +# mybatis-plus +mybatis-plus: + configuration: + map-underscore-to-camel-case: true # 虽然默认为 true ,但是还是显示去指定下。 + global-config: + db-config: + id-type: auto + mapper-locations: classpath*:mapper/*.xml type-aliases-package: cn.iocoder.mall.promotion.biz.dataobject # dubbo