diff --git a/mall_common/src/main/java/com/mall/common/domain/request/SpikesRequest.java b/mall_common/src/main/java/com/mall/common/domain/request/SpikesRequest.java new file mode 100644 index 0000000..2f87c8e --- /dev/null +++ b/mall_common/src/main/java/com/mall/common/domain/request/SpikesRequest.java @@ -0,0 +1,74 @@ +package com.mall.common.domain.request; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.boot.autoconfigure.batch.BatchDataSource; + +import java.math.BigDecimal; +import java.util.Date; +import java.util.List; + +/** + *Spikes接参 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class SpikesRequest { + /** + * 商品主图 + */ + private String spikesImg; + /** + * 轮播图 + */ + private String spikesManyImg; + /** + * 商品标题 + */ + private String spikesTitle; + /** + * 秒杀活动简介 + */ + private String spikesThings; + /** + * 单位 + */ + private String spikesUnit; + /** + * 当天参与活动次数 + */ + private Integer spikesNum; + /** + * 创建时间 + */ + private Date createTime; + /** + * 结束时间 + */ + private Date endTime; + /** + * 状态(0已开启1为开启) + */ + private Integer spikesState; + /** + * spuId + */ + private Long spuId; + /** + * 秒杀商品信息 + */ + private List skuList; + + @Data + public static class Sku{ + private Long skuId; //商品id + private BigDecimal activityPrice; //秒杀价格 + private Integer inventoryRestrict; //限制数量 + } +} diff --git a/mall_common/src/main/java/com/mall/common/domain/vo/ActivitySkuVo.java b/mall_common/src/main/java/com/mall/common/domain/vo/ActivitySkuVo.java new file mode 100644 index 0000000..de65718 --- /dev/null +++ b/mall_common/src/main/java/com/mall/common/domain/vo/ActivitySkuVo.java @@ -0,0 +1,92 @@ +package com.mall.common.domain.vo; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.math.BigDecimal; +import java.util.Date; + +/** + *活动商品信息Vo + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class ActivitySkuVo { + + /** + * id + */ + private Long id; + /** + * spuId + */ + private Long spuId; + /** + * sku名称 + */ + private String name; + /** + * 所属分类id + */ + private Long categoryId; + /** + * 品牌id + */ + private Long brandId; + /** + * 默认图片 + */ + private String defaultImage; + /** + * 标题 + */ + private String title; + /** + * 副标题 + */ + private String subtitle; + /** + * 价格 + */ + private BigDecimal price; + /** + * 活动价格 + */ + private BigDecimal activityPrice; + /** + * 重量(克) + */ + private Integer weight; + /** + * 库存 + */ + private Integer inventory; + /** + * 库存限制量 + */ + private Integer inventoryRestrict; + /** + * 秒杀id(0未开启秒杀) + */ + private Integer spikesId; + /** + * 砍价id(0未开启砍价) + */ + private Integer bargainId; + /** + * 拼团id(0未开启拼团) + */ + private Integer groupId; + /** + * 秒杀开始时间 + */ + private Date startTime; + /** + * 秒杀结束时间 + */ + private Date endTime; +} diff --git a/mall_modules/mall_server/pom.xml b/mall_modules/mall_server/pom.xml index 34783a3..2fef7af 100644 --- a/mall_modules/mall_server/pom.xml +++ b/mall_modules/mall_server/pom.xml @@ -52,10 +52,10 @@ 1.2 - - com.alibaba.cloud - spring-cloud-starter-alibaba-seata - + + + + top.javatool diff --git a/mall_modules/mall_server/src/main/java/com/mall/server/ServerApplication.java b/mall_modules/mall_server/src/main/java/com/mall/server/ServerApplication.java new file mode 100644 index 0000000..46710c2 --- /dev/null +++ b/mall_modules/mall_server/src/main/java/com/mall/server/ServerApplication.java @@ -0,0 +1,13 @@ +package com.mall.server; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + *server启动类 + */ +@SpringBootApplication +public class ServerApplication { + public static void main(String[] args) { + SpringApplication.run(ServerApplication.class,args); + } +} diff --git a/mall_modules/mall_server/src/main/java/com/mall/server/aop/LogAnnotationAspect.java b/mall_modules/mall_server/src/main/java/com/mall/server/aop/LogAnnotationAspect.java new file mode 100644 index 0000000..aaeac6a --- /dev/null +++ b/mall_modules/mall_server/src/main/java/com/mall/server/aop/LogAnnotationAspect.java @@ -0,0 +1,39 @@ +package com.mall.server.aop; +import com.mall.common.utils.JwtUtils; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.stereotype.Component; + +import java.util.Date; +import java.util.Map; + +/** + *aop + */ +@Aspect +@Component +@Slf4j +public class LogAnnotationAspect { + + @Pointcut("execution(* com.mall.server.service.impl.*.*(..))") + private void allMethod(){} + + //应用周围通知 + @Around("allMethod()") + public void doAround(ProceedingJoinPoint call) throws Throwable{ + long start = new Date().getTime(); + String methodName = call.getSignature().getName(); // 获取方法名 + Object[] args = call.getArgs(); // 获取方法的参数 + call.proceed(); + long end = new Date().getTime(); + log.info("耗时:"+(end-start)/1000+"秒"); + log.info(methodName+"()方法执行"); + for (Object arg : args) { + log.info("参数:"+String.valueOf(arg)); + } + } + +} diff --git a/mall_modules/mall_server/src/main/java/com/mall/server/controller/SpikesController.java b/mall_modules/mall_server/src/main/java/com/mall/server/controller/SpikesController.java new file mode 100644 index 0000000..d36d1f4 --- /dev/null +++ b/mall_modules/mall_server/src/main/java/com/mall/server/controller/SpikesController.java @@ -0,0 +1,30 @@ +package com.mall.server.controller; +import com.mall.common.domain.request.SpikesRequest; +import com.mall.common.result.Result; +import com.mall.server.service.SpikesService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +/** + *秒杀controller + */ +@RestController +@RequestMapping("spikes") +public class SpikesController { + + @Autowired + private SpikesService spikesService; + + /** + * 添加秒杀场次 + * @param spikesRequest + * @return + */ + @PostMapping("add") + public Result add(@RequestBody SpikesRequest spikesRequest){ + return spikesService.add(spikesRequest); + } + +} diff --git a/mall_modules/mall_server/src/main/java/com/mall/server/domain/SkuEntity.java b/mall_modules/mall_server/src/main/java/com/mall/server/domain/SkuEntity.java new file mode 100644 index 0000000..a92c7b3 --- /dev/null +++ b/mall_modules/mall_server/src/main/java/com/mall/server/domain/SkuEntity.java @@ -0,0 +1,84 @@ +package com.mall.server.domain; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.math.BigDecimal; + +/** + *商品Sku + */ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +@TableName("pms_sku") +public class SkuEntity { + + /** + * id + */ + private Long id; + /** + * spuId + */ + private Long spuId; + /** + * sku名称 + */ + private String name; + /** + * 所属分类id + */ + private Long categoryId; + /** + * 品牌id + */ + private Long brandId; + /** + * 默认图片 + */ + private String defaultImage; + /** + * 标题 + */ + private String title; + /** + * 副标题 + */ + private String subtitle; + /** + * 价格 + */ + private BigDecimal price; + /** + * 活动价格 + */ + private BigDecimal activityPrice; + /** + * 重量(克) + */ + private Integer weight; + /** + * 库存 + */ + private Integer inventory; + /** + * 库存限制量 + */ + private Integer inventoryRestrict; + /** + * 秒杀id(0未开启秒杀) + */ + private Integer spikesId; + /** + * 砍价id(0未开启砍价) + */ + private Integer bargainId; + /** + * 拼团id(0未开启拼团) + */ + private Integer groupId; +} diff --git a/mall_modules/mall_server/src/main/java/com/mall/server/domain/SpikesEntity.java b/mall_modules/mall_server/src/main/java/com/mall/server/domain/SpikesEntity.java new file mode 100644 index 0000000..0b79ee8 --- /dev/null +++ b/mall_modules/mall_server/src/main/java/com/mall/server/domain/SpikesEntity.java @@ -0,0 +1,67 @@ +package com.mall.server.domain; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Date; + +/** + *秒杀表 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@TableName("pms_spikes") +public class SpikesEntity { + /** + * 秒杀id + */ + @TableId(type = IdType.AUTO) + private Long spikesId; + /** + * 商品主图 + */ + private String spikesImg; + /** + * 轮播图 + */ + private String spikesManyImg; + /** + * 商品标题 + */ + private String spikesTitle; + /** + * 秒杀活动简介 + */ + private String spikesThings; + /** + * 单位 + */ + private String spikesUnit; + /** + * 当天参与活动次数 + */ + private Integer spikesNum; + /** + * 创建时间 + */ + private Date createTime; + /** + * 结束时间 + */ + private Date endTime; + /** + * 状态(0已开启1为开启) + */ + private Integer spikesState; + /** + * 0未删除1已删除 + */ + private Integer isDelete; +} diff --git a/mall_modules/mall_server/src/main/java/com/mall/server/domain/SpuEntity.java b/mall_modules/mall_server/src/main/java/com/mall/server/domain/SpuEntity.java new file mode 100644 index 0000000..08fe2ef --- /dev/null +++ b/mall_modules/mall_server/src/main/java/com/mall/server/domain/SpuEntity.java @@ -0,0 +1,52 @@ +package com.mall.server.domain; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Date; + +/** + *商品Spu + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@TableName("pms_spu") +public class SpuEntity { + + /** + * id + */ + @TableId(type = IdType.AUTO) + private Long id; + /** + * 商品名称 + */ + private String name; + /** + *所属分类id + */ + private Long categoryId; + /** + *品牌id + */ + private Long brandId; + /** + *上架状态[0 - 下架,1 - 上架] + */ + private Integer publishStatus; + /** + *创建时间 + */ + private Date createTime; + /** + *更新时间 + */ + private Date updateTime; +} diff --git a/mall_modules/mall_server/src/main/java/com/mall/server/enumerate/ActivityEnum.java b/mall_modules/mall_server/src/main/java/com/mall/server/enumerate/ActivityEnum.java new file mode 100644 index 0000000..27d87b8 --- /dev/null +++ b/mall_modules/mall_server/src/main/java/com/mall/server/enumerate/ActivityEnum.java @@ -0,0 +1,38 @@ +package com.mall.server.enumerate; + +import lombok.Data; + +/** + * + */ + +public enum ActivityEnum { + SPIKES(0,"秒杀未开启"), + BARGAIN(0,"砍价未开启"), + GROUP(0,"拼团未开启"); + + private Integer code; + + private String msg; + + ActivityEnum(Integer code, String msg) { + this.code = code; + this.msg = msg; + } + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } +} diff --git a/mall_modules/mall_server/src/main/java/com/mall/server/mapper/SkuMapper.java b/mall_modules/mall_server/src/main/java/com/mall/server/mapper/SkuMapper.java new file mode 100644 index 0000000..9fe4cbc --- /dev/null +++ b/mall_modules/mall_server/src/main/java/com/mall/server/mapper/SkuMapper.java @@ -0,0 +1,12 @@ +package com.mall.server.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.mall.server.domain.SkuEntity; +import org.apache.ibatis.annotations.Mapper; + +/** + *商品SkuMapperr + */ +@Mapper +public interface SkuMapper extends BaseMapper { +} diff --git a/mall_modules/mall_server/src/main/java/com/mall/server/mapper/SpikesMapper.java b/mall_modules/mall_server/src/main/java/com/mall/server/mapper/SpikesMapper.java new file mode 100644 index 0000000..002ded7 --- /dev/null +++ b/mall_modules/mall_server/src/main/java/com/mall/server/mapper/SpikesMapper.java @@ -0,0 +1,12 @@ +package com.mall.server.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.mall.server.domain.SpikesEntity; +import org.apache.ibatis.annotations.Mapper; + +/** + * 秒杀表mapper + */ +@Mapper +public interface SpikesMapper extends BaseMapper { +} diff --git a/mall_modules/mall_server/src/main/java/com/mall/server/mapper/SpuMapper.java b/mall_modules/mall_server/src/main/java/com/mall/server/mapper/SpuMapper.java new file mode 100644 index 0000000..0966cbf --- /dev/null +++ b/mall_modules/mall_server/src/main/java/com/mall/server/mapper/SpuMapper.java @@ -0,0 +1,12 @@ +package com.mall.server.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.mall.server.domain.SpuEntity; +import org.apache.ibatis.annotations.Mapper; + +/** + *商品spuMapper + */ +@Mapper +public interface SpuMapper extends BaseMapper { +} diff --git a/mall_modules/mall_server/src/main/java/com/mall/server/service/SkuService.java b/mall_modules/mall_server/src/main/java/com/mall/server/service/SkuService.java new file mode 100644 index 0000000..a9c054e --- /dev/null +++ b/mall_modules/mall_server/src/main/java/com/mall/server/service/SkuService.java @@ -0,0 +1,10 @@ +package com.mall.server.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.mall.server.domain.SkuEntity; + +/** + *商品SkuService + */ +public interface SkuService extends IService { +} diff --git a/mall_modules/mall_server/src/main/java/com/mall/server/service/SpikesService.java b/mall_modules/mall_server/src/main/java/com/mall/server/service/SpikesService.java new file mode 100644 index 0000000..d7785ad --- /dev/null +++ b/mall_modules/mall_server/src/main/java/com/mall/server/service/SpikesService.java @@ -0,0 +1,14 @@ +package com.mall.server.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.mall.common.domain.request.SpikesRequest; +import com.mall.common.result.Result; +import com.mall.server.domain.SpikesEntity; + +/** + *秒杀service + */ +public interface SpikesService extends IService { + + Result add(SpikesRequest spikesRequest); +} diff --git a/mall_modules/mall_server/src/main/java/com/mall/server/service/SpuService.java b/mall_modules/mall_server/src/main/java/com/mall/server/service/SpuService.java new file mode 100644 index 0000000..f2dace8 --- /dev/null +++ b/mall_modules/mall_server/src/main/java/com/mall/server/service/SpuService.java @@ -0,0 +1,10 @@ +package com.mall.server.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.mall.server.domain.SpuEntity; + +/** + *商品spuService + */ +public interface SpuService extends IService { +} diff --git a/mall_modules/mall_server/src/main/java/com/mall/server/service/impl/SkuServiceImpl.java b/mall_modules/mall_server/src/main/java/com/mall/server/service/impl/SkuServiceImpl.java new file mode 100644 index 0000000..1dba36b --- /dev/null +++ b/mall_modules/mall_server/src/main/java/com/mall/server/service/impl/SkuServiceImpl.java @@ -0,0 +1,14 @@ +package com.mall.server.service.impl; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.mall.server.domain.SkuEntity; +import com.mall.server.mapper.SkuMapper; +import com.mall.server.service.SkuService; +import org.springframework.stereotype.Service; + +/** + *商品SkuServiceImpl + */ +@Service +public class SkuServiceImpl extends ServiceImpl + implements SkuService { +} diff --git a/mall_modules/mall_server/src/main/java/com/mall/server/service/impl/SpikesServiceImpl.java b/mall_modules/mall_server/src/main/java/com/mall/server/service/impl/SpikesServiceImpl.java new file mode 100644 index 0000000..075ab41 --- /dev/null +++ b/mall_modules/mall_server/src/main/java/com/mall/server/service/impl/SpikesServiceImpl.java @@ -0,0 +1,107 @@ +package com.mall.server.service.impl; + +import com.alibaba.fastjson2.JSON; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.mall.common.domain.request.SpikesRequest; +import com.mall.common.domain.vo.ActivitySkuVo; +import com.mall.common.redis.RedisCache; +import com.mall.common.result.Result; +import com.mall.server.domain.SkuEntity; +import com.mall.server.domain.SpikesEntity; +import com.mall.server.enumerate.ActivityEnum; +import com.mall.server.mapper.SpikesMapper; +import com.mall.server.service.SkuService; +import com.mall.server.service.SpikesService; +import com.mall.server.service.SpuService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.BoundZSetOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + *秒杀serviceImpl + */ +@Service +public class SpikesServiceImpl extends ServiceImpl + implements SpikesService { + + @Autowired + private SpikesMapper spikesMapper; + + @Autowired + private SpuService spuService; + + @Autowired + private SkuService skuService; + + @Autowired + private RedisCache redisCache; + + @Autowired + private RedisTemplate redisTemplate; + + @Transactional + @Override + public Result add(SpikesRequest spikesRequest) { + //秒杀表信息 + int spikesId = spikesMapper.insert( + SpikesEntity.builder() + .spikesImg(spikesRequest.getSpikesImg()) + .spikesManyImg(spikesRequest.getSpikesManyImg()) + .spikesTitle(spikesRequest.getSpikesTitle()) + .spikesThings(spikesRequest.getSpikesThings()) + .spikesUnit(spikesRequest.getSpikesUnit()) + .spikesNum(spikesRequest.getSpikesNum()) + .createTime(spikesRequest.getCreateTime()) + .endTime(spikesRequest.getEndTime()) + .spikesState(spikesRequest.getSpikesState()) + .build() + ); + //修改sku商品信息 + skuService.updateBatchById( + spikesRequest.getSkuList().stream().map(c-> + SkuEntity.builder() + .id(c.getSkuId()) + .inventoryRestrict(c.getInventoryRestrict()) + .activityPrice(c.getActivityPrice()) + .spikesId(spikesId).build() + ).collect(Collectors.toList()) + ); + //遍历修改后的sku商品信息作转换 + List activitySkuVoList = skuService.list( + new LambdaQueryWrapper() + .eq(SkuEntity::getSpikesId, spikesId) + ).stream().map(c -> + ActivitySkuVo.builder() + .id(c.getId()) + .spuId(c.getSpuId()) + .name(c.getName()) + .categoryId(c.getCategoryId()) + .brandId(c.getBrandId()) + .defaultImage(c.getDefaultImage()) + .title(c.getTitle()) + .subtitle(c.getSubtitle()) + .price(c.getPrice()) + .activityPrice(c.getActivityPrice()) + .weight(c.getWeight()) + .inventory(c.getInventory()) + .inventoryRestrict(c.getInventoryRestrict()) + .spikesId(c.getSpikesId()) + .bargainId(c.getBargainId()) + .groupId(c.getGroupId()) + .startTime(spikesRequest.getCreateTime()) + .endTime(spikesRequest.getEndTime()).build()).collect(Collectors.toList()); + activitySkuVoList.forEach(c->{ + //redis秒杀信息存在时间 + long expire = c.getEndTime().getTime() - System.currentTimeMillis()/1000/1000; + redisCache.setCacheObject("spikes_"+c.getId(),c,expire, TimeUnit.MINUTES); + }); + return Result.success(true,"添加秒杀成功"); + } +} diff --git a/mall_modules/mall_server/src/main/java/com/mall/server/service/impl/SpuServiceImpl.java b/mall_modules/mall_server/src/main/java/com/mall/server/service/impl/SpuServiceImpl.java new file mode 100644 index 0000000..b0b2071 --- /dev/null +++ b/mall_modules/mall_server/src/main/java/com/mall/server/service/impl/SpuServiceImpl.java @@ -0,0 +1,16 @@ +package com.mall.server.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.mall.server.domain.SpuEntity; +import com.mall.server.mapper.SpuMapper; +import com.mall.server.service.SpuService; +import org.springframework.stereotype.Service; + +/** + *商品spuServiceImpl + */ +@Service +public class SpuServiceImpl extends ServiceImpl + implements SpuService { + +} diff --git a/mall_modules/mall_server/src/main/resources/META-INF/spring.factories b/mall_modules/mall_server/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..73888f6 --- /dev/null +++ b/mall_modules/mall_server/src/main/resources/META-INF/spring.factories @@ -0,0 +1,7 @@ +# Auto Configure +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.mall.common.config.MybatisPlusConfig,\ + com.mall.common.config.RedisConfig,\ + com.mall.common.redis.RedisCache,\ + com.mall.common.config.ThreadPoolConfig,\ + com.mall.common.handler.GlobalExceptionHandler