Browse Source

Merge branch 'dev_v1.3.4_20220902'

Eric 3 years ago
parent
commit
7d33113b33

+ 6 - 2
cooleshow-user/user-admin/src/main/java/com/yonge/cooleshow/admin/io/request/coupon/CouponInfoVO.java

@@ -368,12 +368,16 @@ public class CouponInfoVO {
         }
 
         public CouponInfo inventory(Integer inventory) {
-            this.inventory = inventory;
+            if (Objects.isNull(this.inventory)) {
+                this.inventory = inventory;
+            }
             return this;
         }
 
         public CouponInfo quantityLimit(Integer quantityLimit) {
-            this.quantityLimit = quantityLimit;
+            if (Objects.isNull(this.quantityLimit)) {
+                this.quantityLimit = quantityLimit;
+            }
             return this;
         }
 

+ 1 - 1
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/entity/CouponIssue.java

@@ -55,7 +55,7 @@ public class CouponIssue implements Serializable {
 
     @ApiModelProperty("订单编号")
     @TableField("order_no_")
-    private Long orderNo;
+    private String orderNo;
 
     @ApiModelProperty("生效时间")
     @TableField("start_time_")

+ 4 - 4
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/CouponIssueService.java

@@ -1,15 +1,15 @@
 package com.yonge.cooleshow.biz.dal.service;
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.service.IService;
 import com.yonge.cooleshow.biz.dal.dto.UserParam;
 import com.yonge.cooleshow.biz.dal.entity.CouponIssue;
-import com.baomidou.mybatisplus.extension.service.IService;
 import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
 import com.yonge.cooleshow.biz.dal.queryInfo.CouponInfoQuery;
 import com.yonge.cooleshow.biz.dal.queryInfo.CouponIssueQueryInfo;
 import com.yonge.cooleshow.biz.dal.vo.CouponIssueUserVo;
+import com.yonge.cooleshow.biz.dal.vo.coupon.CouponInfoWrapper;
 import com.yonge.cooleshow.biz.dal.vo.coupon.CouponIssueWrapper;
-import com.yonge.cooleshow.biz.dal.wrapper.StatGroupWrapper;
 
 import java.util.List;
 
@@ -62,7 +62,7 @@ public interface CouponIssueService extends IService<CouponIssue> {
      * 统计用户优惠券状态
      * @param id 用户ID
      * @param query CouponInfoQuery.CouponStateStatQuery
-     * @return List<StatGroupWrapper>
+     * @return List<CouponInfoWrapper.CouponStat>
      */
-    List<StatGroupWrapper> queryCouponStateStatInfo(Long id, CouponInfoQuery.CouponStateStatQuery query);
+    List<CouponInfoWrapper.CouponStat> queryCouponStateStatInfo(Long id, CouponInfoQuery.CouponStateStatQuery query);
 }

+ 17 - 0
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/impl/CouponInfoServiceImp.java

@@ -19,6 +19,7 @@ import com.yonge.cooleshow.biz.dal.service.CouponInfoService;
 import com.yonge.cooleshow.biz.dal.vo.coupon.CouponInfoWrapper;
 import com.yonge.cooleshow.biz.dal.wrapper.StatGroupWrapper;
 import com.yonge.cooleshow.biz.dal.wrapper.coupon.CouponInventoryWrapper;
+import com.yonge.cooleshow.common.enums.EStatus;
 import com.yonge.toolset.base.exception.BizException;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -125,6 +126,7 @@ public class CouponInfoServiceImp extends ServiceImpl<CouponInfoMapper, CouponIn
      * @param couponInfo CouponInfo
      * @return CouponInfo
      */
+    @Transactional(rollbackFor = Exception.class)
     @Override
     public CouponInfo saveOrUpdateCouponInfo(CouponInfo couponInfo) {
 
@@ -132,6 +134,21 @@ public class CouponInfoServiceImp extends ServiceImpl<CouponInfoMapper, CouponIn
 
             // 新增优惠券
             save(couponInfo);
+
+            // 添加库存变更记录
+            if (couponInfo.getInventory() > 0) {
+
+                CouponInventoryWrapper wrapper = CouponInventoryWrapper.builder()
+                        .userId(couponInfo.getCreatedBy())
+                        .couponId(couponInfo.getId())
+                        .dataType(CouponInventoryEnum.ADDITION)
+                        .number(couponInfo.getInventory())
+                        .remark("")
+                        .status(EStatus.ENABLE.getValue())
+                        .build();
+
+                couponInventoryMapper.insert(JSON.parseObject(wrapper.jsonString(), CouponInventory.class));
+            }
         } else {
 
             CouponInfo info = getById(couponInfo.getId());

+ 30 - 5
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/impl/CouponIssueServiceImp.java

@@ -21,6 +21,7 @@ import com.yonge.cooleshow.biz.dal.service.CouponInfoService;
 import com.yonge.cooleshow.biz.dal.service.CouponIssueService;
 import com.yonge.cooleshow.biz.dal.service.SysMessageService;
 import com.yonge.cooleshow.biz.dal.vo.CouponIssueUserVo;
+import com.yonge.cooleshow.biz.dal.vo.coupon.CouponInfoWrapper;
 import com.yonge.cooleshow.biz.dal.vo.coupon.CouponIssueWrapper;
 import com.yonge.cooleshow.biz.dal.wrapper.StatGroupWrapper;
 import com.yonge.cooleshow.common.enums.EStatus;
@@ -35,7 +36,12 @@ import org.springframework.transaction.annotation.Transactional;
 
 import java.time.LocalDateTime;
 import java.time.ZoneId;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
 import java.util.stream.Collectors;
 
 /**
@@ -103,9 +109,15 @@ public class CouponIssueServiceImp extends ServiceImpl<CouponIssueMapper, Coupon
     @Transactional
     public void issueCoupon(Long couponId, List<UserParam> userParam, Long issuer, String reason) {
         CouponInfo couponInfo = couponInfoService.queryCouponInfoById(couponId);
+        if (couponInfo == null) {
+            throw new BizException("未找到优惠券信息");
+        }
         if (couponInfo.getInventory() < userParam.size()) {
             throw new BizException("库存不足");
         }
+        if (couponInfo.getStatus() == EStatus.DISABLE.getValue()) {
+            throw new BizException("优惠券已被禁用");
+        }
         // 判断优惠券类型 设置优惠券时间
         Long startTime = null;
         Long endTime = null;
@@ -197,14 +209,27 @@ public class CouponIssueServiceImp extends ServiceImpl<CouponIssueMapper, Coupon
      *
      * @param id         用户ID
      * @param query     CouponInfoQuery.CouponStateStatQuery
-     * @return List<StatGroupWrapper>
+     * @return List<CouponInfoWrapper.CouponStat>
      */
     @Override
-    public List<StatGroupWrapper> queryCouponStateStatInfo(Long id, CouponInfoQuery.CouponStateStatQuery query) {
+    public List<CouponInfoWrapper.CouponStat> queryCouponStateStatInfo(Long id, CouponInfoQuery.CouponStateStatQuery query) {
 
         // 优惠券统计状态
-        List<StatGroupWrapper> wrappers = getBaseMapper().selectCouponStateStatInfo(id, query);
+        Map<String, Integer> collect = getBaseMapper().selectCouponStateStatInfo(id, query).stream()
+                .collect(Collectors.toMap(StatGroupWrapper::getGid, StatGroupWrapper::getTotal, (o, n) -> n));
+
+        List<CouponInfoWrapper.CouponStat> couponStats = Lists.newArrayList();
+
+        // 统计数据封装
+        CouponUseStateEnum[] values = CouponUseStateEnum.values();
+        for (CouponUseStateEnum item : values) {
+
+            couponStats.add(CouponInfoWrapper.CouponStat.builder()
+                            .useState(item)
+                            .total(collect.getOrDefault(item.getCode(), 0))
+                    .build());
+        }
 
-        return Optional.ofNullable(wrappers).orElse(Lists.newArrayList());
+        return couponStats;
     }
 }

+ 17 - 0
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/vo/coupon/CouponInfoWrapper.java

@@ -1,6 +1,7 @@
 package com.yonge.cooleshow.biz.dal.vo.coupon;
 
 import com.alibaba.fastjson.JSON;
+import com.yonge.cooleshow.biz.dal.enums.coupon.CouponUseStateEnum;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Data;
@@ -51,4 +52,20 @@ public class CouponInfoWrapper implements Serializable {
         this.updatedUser = updateUser;
         return this;
     }
+
+    /**
+     * 优惠券状态统计
+     */
+    @Data
+    @Builder
+    @NoArgsConstructor
+    @AllArgsConstructor
+    public static class CouponStat implements Serializable {
+
+        // 优惠券统计状态
+        private CouponUseStateEnum useState;
+        // 统计数据
+        private Integer total;
+
+    }
 }

+ 5 - 0
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/wrapper/coupon/CouponInventoryWrapper.java

@@ -1,5 +1,6 @@
 package com.yonge.cooleshow.biz.dal.wrapper.coupon;
 
+import com.alibaba.fastjson.JSON;
 import com.yonge.cooleshow.biz.dal.enums.coupon.CouponInventoryEnum;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
@@ -32,6 +33,10 @@ public class CouponInventoryWrapper implements Serializable {
     private String username;
     private String realName;
 
+    public String jsonString() {
+        return JSON.toJSONString(this);
+    }
+
     public String getUsername() {
         if (StringUtils.isEmpty(this.username)
                 && StringUtils.isNotEmpty(getRealName())) {

+ 2 - 1
cooleshow-user/user-biz/src/main/resources/config/mybatis/CouponInfoMapper.xml

@@ -47,6 +47,7 @@
                 AND t1.category_ = #{record.category}
             </if>
         </where>
+        ORDER BY t1.id_ DESC
     </select>
 
     <!--优惠券发放统计-->
@@ -97,7 +98,7 @@
                 AND (#{record.startTime} &lt;= UNIX_TIMESTAMP(t1.created_time_) AND UNIX_TIMESTAMP(t1.created_time_) &lt;= #{record.endTime})
             </if>
         </where>
-        <if test="record.groupByUser != null">GROUP BY t1.user_id_</if>
+        <if test="record.groupByUser != null">GROUP BY t1.user_id_</if> ORDER BY t1.id_ DESC
     </select>
     <!--优惠券库存调整信息-->
 

+ 4 - 15
cooleshow-user/user-student/src/main/java/com/yonge/cooleshow/student/controller/coupon/CouponInfoController.java

@@ -2,16 +2,14 @@ package com.yonge.cooleshow.student.controller.coupon;
 
 import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.metadata.IPage;
-import com.google.common.collect.Lists;
 import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
 import com.yonge.cooleshow.auth.api.entity.SysUser;
 import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
-import com.yonge.cooleshow.biz.dal.enums.coupon.CouponUseStateEnum;
 import com.yonge.cooleshow.biz.dal.queryInfo.CouponInfoQuery;
 import com.yonge.cooleshow.biz.dal.queryInfo.CouponIssueQueryInfo;
 import com.yonge.cooleshow.biz.dal.service.CouponIssueService;
+import com.yonge.cooleshow.biz.dal.vo.coupon.CouponInfoWrapper;
 import com.yonge.cooleshow.biz.dal.vo.coupon.CouponIssueWrapper;
-import com.yonge.cooleshow.biz.dal.wrapper.StatGroupWrapper;
 import com.yonge.cooleshow.common.controller.BaseController;
 import com.yonge.cooleshow.common.entity.HttpResponseResult;
 import com.yonge.cooleshow.student.io.request.CouponInfoVO;
@@ -87,21 +85,12 @@ public class CouponInfoController extends BaseController {
             return failed(HttpStatus.FORBIDDEN, "请登录");
         }
 
-        List<StatGroupWrapper> wrappers = couponIssueService.queryCouponStateStatInfo(user.getId(),
+        // 优惠券统计
+        List<CouponInfoWrapper.CouponStat> wrappers = couponIssueService.queryCouponStateStatInfo(user.getId(),
                 CouponInfoQuery.CouponStateStatQuery.builder()
                         .clientType(ClientEnum.STUDENT)
                         .build().couponType(couponType));
 
-        List<CouponInfoVO.CouponStateStat> retlist = Lists.newArrayList();
-
-        for (StatGroupWrapper item : wrappers) {
-
-            retlist.add(CouponInfoVO.CouponStateStat.builder()
-                            .useState(CouponUseStateEnum.valueOf(item.getGid()))
-                            .total(item.getTotal())
-                    .build());
-        }
-
-        return succeed(retlist);
+        return succeed(JSON.parseArray(JSON.toJSONString(wrappers), CouponInfoVO.CouponStateStat.class));
     }
 }

+ 3 - 15
cooleshow-user/user-teacher/src/main/java/com/yonge/cooleshow/teacher/controller/coupon/CouponInfoController.java

@@ -2,16 +2,14 @@ package com.yonge.cooleshow.teacher.controller.coupon;
 
 import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.metadata.IPage;
-import com.google.common.collect.Lists;
 import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
 import com.yonge.cooleshow.auth.api.entity.SysUser;
 import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
-import com.yonge.cooleshow.biz.dal.enums.coupon.CouponUseStateEnum;
 import com.yonge.cooleshow.biz.dal.queryInfo.CouponInfoQuery;
 import com.yonge.cooleshow.biz.dal.queryInfo.CouponIssueQueryInfo;
 import com.yonge.cooleshow.biz.dal.service.CouponIssueService;
+import com.yonge.cooleshow.biz.dal.vo.coupon.CouponInfoWrapper;
 import com.yonge.cooleshow.biz.dal.vo.coupon.CouponIssueWrapper;
-import com.yonge.cooleshow.biz.dal.wrapper.StatGroupWrapper;
 import com.yonge.cooleshow.common.controller.BaseController;
 import com.yonge.cooleshow.common.entity.HttpResponseResult;
 import com.yonge.cooleshow.teacher.io.request.CouponInfoVO;
@@ -87,21 +85,11 @@ public class CouponInfoController extends BaseController {
             return failed(HttpStatus.FORBIDDEN, "请登录");
         }
 
-        List<StatGroupWrapper> wrappers = couponIssueService.queryCouponStateStatInfo(user.getId(),
+        List<CouponInfoWrapper.CouponStat> wrappers = couponIssueService.queryCouponStateStatInfo(user.getId(),
                 CouponInfoQuery.CouponStateStatQuery.builder()
                 .clientType(ClientEnum.TEACHER)
                 .build().couponType(couponType));
 
-        List<CouponInfoVO.CouponStateStat> retlist = Lists.newArrayList();
-
-        for (StatGroupWrapper item : wrappers) {
-
-            retlist.add(CouponInfoVO.CouponStateStat.builder()
-                    .useState(CouponUseStateEnum.valueOf(item.getGid()))
-                    .total(item.getTotal())
-                    .build());
-        }
-
-        return succeed(retlist);
+        return succeed(JSON.parseArray(JSON.toJSONString(wrappers), CouponInfoVO.CouponStateStat.class));
     }
 }