Browse Source

抽奖增加钱包扣除限制配置

wangqi49 4 months ago
parent
commit
1dd94c241a
23 changed files with 260 additions and 37 deletions
  1. 1 0
      sql/webchat.sql
  2. 5 0
      src/main/java/com/webchat/common/constants/WebConstant.java
  3. 2 1
      src/main/java/com/webchat/common/enums/ChatMessageTypeEnum.java
  4. 2 0
      src/main/java/com/webchat/common/enums/ClickEvent.java
  5. 5 1
      src/main/java/com/webchat/common/enums/WalletTransEventEnum.java
  6. 9 1
      src/main/java/com/webchat/controller/admin/LotteryActivityController.java
  7. 4 3
      src/main/java/com/webchat/controller/admin/LotteryController.java
  8. 7 0
      src/main/java/com/webchat/domain/vo/request/lottery/LotteryActivitySaveVO.java
  9. 12 0
      src/main/java/com/webchat/domain/vo/response/lottery/LotteryActivityBaseVO.java
  10. 14 0
      src/main/java/com/webchat/domain/vo/response/lottery/LotteryDrawBaseResponseVO.java
  11. 25 0
      src/main/java/com/webchat/domain/vo/response/lottery/LotteryDrawBulkResponseVO.java
  12. 25 0
      src/main/java/com/webchat/domain/vo/response/lottery/LotteryDrawOneResponseVO.java
  13. 1 1
      src/main/java/com/webchat/domain/vo/response/lottery/LotteryItemVO.java
  14. 3 0
      src/main/java/com/webchat/domain/vo/response/mess/PublicAccountArticleMessageVO.java
  15. 7 0
      src/main/java/com/webchat/repository/entity/lottery/LotteryActivityEntity.java
  16. 5 5
      src/main/java/com/webchat/service/RedPacketService.java
  17. 2 3
      src/main/java/com/webchat/service/UserWalletService.java
  18. 1 0
      src/main/java/com/webchat/service/listener/RedisPushMessageListener.java
  19. 1 0
      src/main/java/com/webchat/service/lottery/LotteryActivityService.java
  20. 81 16
      src/main/java/com/webchat/service/lottery/LotteryService.java
  21. 10 2
      src/main/resources/templates/admin/lottery-activity.html
  22. 15 1
      src/main/resources/templates/client/header.html
  23. 23 3
      src/main/resources/templates/client/lottery.html

+ 1 - 0
sql/webchat.sql

@@ -299,6 +299,7 @@ CREATE TABLE webchat.`web_chat_lottery_activity` (
        `DESCRIPTION` varchar(500) NOT NULL COMMENT '活动描述',
        `INTEGRAL` int(11) NOT NULL DEFAULT 0 COMMENT '每次抽奖需要消耗的金币个数',
        `COVER` varchar(300) NOT NULL COMMENT '活动封面图',
+       `MONEY` DECIMAL(10, 2) default '0.00' COMMENT '每次抽奖消耗钱数',
        `STATUS` int(11) NOT NULL COMMENT '活动状态',
        `CREATE_BY` char(100) DEFAULT NULL COMMENT '创建人',
        `CREATE_DATE` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',

+ 5 - 0
src/main/java/com/webchat/common/constants/WebConstant.java

@@ -15,6 +15,11 @@ public class WebConstant {
      */
     public static final String CACHE_NONE = "none";
 
+    /**
+     * 系统账户
+     */
+    public static final String SYSTEM_WALLET_ID = "U_770cce9f632543588b4e8aa6ec43e6a2";
+
 
     /**
      * 系统USERID

+ 2 - 1
src/main/java/com/webchat/common/enums/ChatMessageTypeEnum.java

@@ -13,7 +13,8 @@ public enum ChatMessageTypeEnum {
     CHAT_FILE(2, "文件"),
     RED_PACKET(3, "红包"),
     PUBLIC_ACCOUNT_ARTICLE(4, "公众号推文"),
-    APPLY(5, "申请添加好友");
+    APPLY(5, "申请添加好友"),
+    WALLET_BALANCE(6, "钱包余额");
 
     private Integer type;
     private String desc;

+ 2 - 0
src/main/java/com/webchat/common/enums/ClickEvent.java

@@ -23,6 +23,8 @@ public enum ClickEvent {
 
     SAVE_ARTICLE("保存文章"),
 
+    LUCK_LOTTERY("幸运抽奖"),
+
     SEND_RED_PACKET("发红包"),
 
     OPEN_RED_PACKET("拆红包"),

+ 5 - 1
src/main/java/com/webchat/common/enums/WalletTransEventEnum.java

@@ -17,7 +17,11 @@ public enum WalletTransEventEnum {
 
     SEND_PACKET(2, WalletTransTypeEnum.EXPENSES, "发红包"),
 
-    RECEIVE_PACKET(3, WalletTransTypeEnum.INCOME, "收红包");
+    RECEIVE_PACKET(3, WalletTransTypeEnum.INCOME, "收红包"),
+
+    JOIN_LOTTERY(4, WalletTransTypeEnum.EXPENSES, "参与抽奖"),
+
+    ;
 
     private Integer transEvent;
 

+ 9 - 1
src/main/java/com/webchat/controller/admin/LotteryActivityController.java

@@ -9,11 +9,13 @@ import com.webchat.domain.vo.request.lottery.LotteryActivitySaveVO;
 import com.webchat.domain.vo.request.lottery.LotteryActivityStatusUpdateVO;
 import com.webchat.domain.vo.response.lottery.LotteryActivityBaseVO;
 import com.webchat.domain.vo.response.lottery.LotteryActivityVO;
+import com.webchat.service.UserWalletService;
 import com.webchat.service.lottery.LotteryActivityService;
 import io.swagger.annotations.Api;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import java.math.BigDecimal;
 import java.util.List;
 
 /**
@@ -30,6 +32,9 @@ public class LotteryActivityController {
     @Autowired
     private LotteryActivityService lotteryActivityService;
 
+    @Autowired
+    private UserWalletService walletService;
+
     /**
      * 创建或更新抽奖活动
      *
@@ -64,7 +69,10 @@ public class LotteryActivityController {
      */
     @GetMapping("/detail/{lotteryActivityId}")
     public APIResponseBean<LotteryActivityVO> detail(@PathVariable String lotteryActivityId) {
-        return APIResponseBeanUtil.success(lotteryActivityService.getLotteryActivityDetailFromCache(lotteryActivityId));
+        LotteryActivityVO lotteryActivity = lotteryActivityService.getLotteryActivityDetailFromCache(lotteryActivityId);
+        BigDecimal walletBalance = walletService.getUserWalletBalanceFromCache(SessionHelper.getCurrentUserId());
+        lotteryActivity.setUserInitWalletBalance(walletBalance);
+        return APIResponseBeanUtil.success(lotteryActivity);
     }
 
     /**

+ 4 - 3
src/main/java/com/webchat/controller/admin/LotteryController.java

@@ -5,6 +5,7 @@ import com.webchat.common.enums.ClickEvent;
 import com.webchat.common.helper.SessionHelper;
 import com.webchat.config.annotation.SafeClick;
 import com.webchat.config.annotation.ValidateLogin;
+import com.webchat.domain.vo.response.lottery.LotteryDrawBulkResponseVO;
 import com.webchat.service.lottery.LotteryService;
 import io.swagger.annotations.Api;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -34,7 +35,7 @@ public class LotteryController {
      * @return
      */
     @ValidateLogin
-    @SafeClick(event = ClickEvent.SUBMIT, time = 5000L, message = "异常操作多次后账号将会永久拉黑")
+    @SafeClick(event = ClickEvent.LUCK_LOTTERY, time = 5000L, message = "异常操作多次后账号将会永久拉黑")
     @PostMapping("/luckDraw/{lotteryActivityId}")
     public APIResponseBean<Long> luckDraw(@PathVariable String lotteryActivityId) {
         String userId = SessionHelper.getCurrentUserId();
@@ -48,9 +49,9 @@ public class LotteryController {
     @ValidateLogin
     @SafeClick(event = ClickEvent.SUBMIT, time = 5000L, message = "异常操作多次后账号将会永久拉黑")
     @PostMapping("/luckDraw/{lotteryActivityId}/10")
-    public APIResponseBean<List<Long>> luckDraw10Times(@PathVariable String lotteryActivityId) {
+    public APIResponseBean<LotteryDrawBulkResponseVO> luckDraw10Times(@PathVariable String lotteryActivityId) {
         String userId = SessionHelper.getCurrentUserId();
-        List<Long> batchLuckDrawResult = lotteryService.luckDraw(lotteryActivityId, userId, 10);
+        LotteryDrawBulkResponseVO batchLuckDrawResult = lotteryService.luckDraw(lotteryActivityId, userId, 10);
         return APIResponseBeanUtil.success(batchLuckDrawResult);
     }
 

+ 7 - 0
src/main/java/com/webchat/domain/vo/request/lottery/LotteryActivitySaveVO.java

@@ -2,6 +2,8 @@ package com.webchat.domain.vo.request.lottery;
 
 import lombok.Data;
 
+import java.math.BigDecimal;
+
 /**
  * @Author 程序员七七
  * @webSite https://www.coderutil.com
@@ -31,4 +33,9 @@ public class LotteryActivitySaveVO {
      */
     private String cover;
 
+    /**
+     * 每次抽奖需要消耗钱数
+     */
+    private BigDecimal money;
+
 }

+ 12 - 0
src/main/java/com/webchat/domain/vo/response/lottery/LotteryActivityBaseVO.java

@@ -2,6 +2,8 @@ package com.webchat.domain.vo.response.lottery;
 
 import lombok.Data;
 
+import java.math.BigDecimal;
+
 /**
  * @Author 程序员七七
  * @webSite https://www.coderutil.com
@@ -39,6 +41,16 @@ public class LotteryActivityBaseVO {
     private String cover;
 
     /**
+     * 每次抽奖需要消耗的钱数
+     */
+    private BigDecimal money;
+
+    /**
+     * 用户初始钱包余额
+     */
+    private BigDecimal userInitWalletBalance;
+
+    /**
      * 活动当前状态
      */
     private Integer status;

+ 14 - 0
src/main/java/com/webchat/domain/vo/response/lottery/LotteryDrawBaseResponseVO.java

@@ -0,0 +1,14 @@
+package com.webchat.domain.vo.response.lottery;
+
+import lombok.Data;
+
+import java.math.BigDecimal;
+
+@Data
+public class LotteryDrawBaseResponseVO {
+
+    /**
+     * 用户最新余额
+     */
+    private BigDecimal walletBalance;
+}

+ 25 - 0
src/main/java/com/webchat/domain/vo/response/lottery/LotteryDrawBulkResponseVO.java

@@ -0,0 +1,25 @@
+package com.webchat.domain.vo.response.lottery;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.math.BigDecimal;
+import java.util.List;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class LotteryDrawBulkResponseVO extends LotteryDrawBaseResponseVO {
+
+    /**
+     * 批量抽奖结果
+     */
+    private List<Long> itemIdList;
+
+    public static LotteryDrawBulkResponseVO of(BigDecimal walletBalance, List<Long> itemIdList) {
+        LotteryDrawBulkResponseVO lotteryDrawBulkResponseVO = new LotteryDrawBulkResponseVO(itemIdList);
+        lotteryDrawBulkResponseVO.setWalletBalance(walletBalance);
+        return lotteryDrawBulkResponseVO;
+    }
+}

+ 25 - 0
src/main/java/com/webchat/domain/vo/response/lottery/LotteryDrawOneResponseVO.java

@@ -0,0 +1,25 @@
+package com.webchat.domain.vo.response.lottery;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.math.BigDecimal;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class LotteryDrawOneResponseVO extends LotteryDrawBaseResponseVO {
+
+    /**
+     * 用户最新余额
+     */
+    private Long itemId;
+
+
+    public static LotteryDrawOneResponseVO of(BigDecimal balance, Long itemId) {
+        LotteryDrawOneResponseVO lotteryDrawOneResponseVO = new LotteryDrawOneResponseVO(itemId);
+        lotteryDrawOneResponseVO.setWalletBalance(balance);
+        return lotteryDrawOneResponseVO;
+    }
+}

+ 1 - 1
src/main/java/com/webchat/domain/vo/response/lottery/LotteryItemVO.java

@@ -13,7 +13,7 @@ import lombok.Data;
 public class LotteryItemVO {
 
     /**
-     * 活动id
+     * 奖项id
      */
     private Long id;
 

+ 3 - 0
src/main/java/com/webchat/domain/vo/response/mess/PublicAccountArticleMessageVO.java

@@ -15,5 +15,8 @@ public class PublicAccountArticleMessageVO {
 
     private String description;
 
+    /**
+     * 外部链接,优先跳转
+     */
     private String redirectUrl;
 }

+ 7 - 0
src/main/java/com/webchat/repository/entity/lottery/LotteryActivityEntity.java

@@ -4,6 +4,7 @@ import com.webchat.repository.entity.BaseEntity;
 import lombok.Data;
 
 import javax.persistence.*;
+import java.math.BigDecimal;
 
 /**
  * @Author 程序员七七
@@ -51,6 +52,12 @@ public class LotteryActivityEntity extends BaseEntity {
     private String cover;
 
     /**
+     * 每次抽奖需要消耗钱数
+     */
+    @Column(name = "money")
+    private BigDecimal money;
+
+    /**
      * 活动状态
      * @see LotteryConstants.LotteryActivityStatus
      */

+ 5 - 5
src/main/java/com/webchat/service/RedPacketService.java

@@ -78,10 +78,10 @@ public class RedPacketService {
         BigDecimal totalMoney = sendRedPacketRequest.getTotalMoney();
 
         // 1. 钱包余额扣除,流转一条交易明细(支出类型doTrans方法会校验余额)
-        boolean transResult = userWalletService.doTrans(WalletTransEventEnum.SEND_PACKET,
-                                  sendUserId,
-                                  sendRedPacketRequest.getReceiverUserId(),
-                                  totalMoney);
+        BigDecimal lastWalletBalance = userWalletService.doTrans(WalletTransEventEnum.SEND_PACKET,
+                                                                  sendUserId,
+                                                                  sendRedPacketRequest.getReceiverUserId(),
+                                                                  totalMoney);
         RedPacketEntity redPacket = null;
         RedPacketDetailVO redPacketDetailVO = null;
         try {
@@ -103,7 +103,7 @@ public class RedPacketService {
                 this.doNotifyClientRedPacketMessage(finalRedPacketDetailVO);
             });
         } catch (Exception e) {
-            if (transResult) {
+            if (lastWalletBalance != null) {
                 // 回滚trans钱包余额,或者清空钱包余额缓存
                 // 这里场景是发红包,所以回滚一定加余额
                 userWalletService.rollbackUserWalletBalanceCache(sendUserId, totalMoney);

+ 2 - 3
src/main/java/com/webchat/service/UserWalletService.java

@@ -58,7 +58,7 @@ public class UserWalletService {
      * @return
      */
     @Transactional
-    public boolean doTrans(WalletTransEventEnum event,
+    public BigDecimal doTrans(WalletTransEventEnum event,
                            String userId,
                            String targetUserId,
                            BigDecimal money) {
@@ -70,8 +70,7 @@ public class UserWalletService {
         // 2. 钱包数据流转, 这里如果失败,会回滚操作不会产生脏数据
         this.walletMoneyTransToDatabase(event, userId, targetUserId, money);
         // 3. 刷新用户钱包最新余额缓存
-        this.refreshUserWalletBalanceCache(userId, money.multiply(BigDecimal.valueOf(event.getTransType().getTransType())));
-        return true;
+        return this.refreshUserWalletBalanceCache(userId, money.multiply(BigDecimal.valueOf(event.getTransType().getTransType())));
     }
 
     /**

+ 1 - 0
src/main/java/com/webchat/service/listener/RedisPushMessageListener.java

@@ -88,6 +88,7 @@ public class RedisPushMessageListener implements MessageListener {
             publicAccountArticleMessageDTO.setDescription(articleBaseResponse.getDescription());
             publicAccountArticleMessageDTO.setTitle(articleBaseResponse.getTitle());
             publicAccountArticleMessageDTO.setTime(System.currentTimeMillis());
+            publicAccountArticleMessageDTO.setRedirectUrl(articleBaseResponse.getRedirectUrl());
 
             ChatMessageResponseVO publicAccountArticleMessage = new ChatMessageResponseVO();
             publicAccountArticleMessage.setType(ChatMessageTypeEnum.PUBLIC_ACCOUNT_ARTICLE.getType());

+ 1 - 0
src/main/java/com/webchat/service/lottery/LotteryActivityService.java

@@ -180,6 +180,7 @@ public class LotteryActivityService {
         entity.setDescription(lotteryActivityVO.getDescription());
         entity.setIntegral(0);
         entity.setCover(lotteryActivityVO.getCover());
+        entity.setMoney(lotteryActivityVO.getMoney());
         entity.setUpdateBy(userId);
         entity.setUpdateDate(now);
         return entity;

+ 81 - 16
src/main/java/com/webchat/service/lottery/LotteryService.java

@@ -1,9 +1,15 @@
 package com.webchat.service.lottery;
 
 import com.webchat.common.constants.LotteryConstants;
+import com.webchat.common.constants.WebConstant;
+import com.webchat.common.enums.WalletTransEventEnum;
 import com.webchat.common.exception.BusinessException;
+import com.webchat.common.util.ListUtil;
 import com.webchat.domain.vo.request.lottery.LotteryActivityStatusUpdateVO;
 import com.webchat.domain.vo.response.lottery.LotteryActivityVO;
+import com.webchat.domain.vo.response.lottery.LotteryDrawBulkResponseVO;
+import com.webchat.domain.vo.response.lottery.LotteryDrawOneResponseVO;
+import com.webchat.service.UserWalletService;
 import com.webchat.service.queue.LotteryOrderQueue;
 import com.webchat.service.queue.dto.LotteryOrderQueueDTO;
 import lombok.extern.slf4j.Slf4j;
@@ -12,6 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.Assert;
 
+import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -38,6 +45,8 @@ public class LotteryService {
     private LotteryItemService lotteryItemService;
     @Autowired
     private LotteryCacheService lotteryCacheService;
+    @Autowired
+    private UserWalletService userWalletService;
 
     /**
      * 幸运抽奖
@@ -46,19 +55,60 @@ public class LotteryService {
      * @param userId            当前登录用户id
      * @return 中奖商品id
      */
-    public Long luckDraw(String lotteryActivityId, String userId) {
+    public LotteryDrawOneResponseVO luckDraw(String lotteryActivityId, String userId) {
 
+        // 查询活动配置
+        LotteryActivityVO activity = lotteryActivityService.getLotteryActivityDetailFromCache(lotteryActivityId);
+        /**
+         * 校验活动状态及用户钱包余额
+         */
+        this.validateUserJoinLotteryCondition(userId, activity, 1);
         /**
          * 1、抽奖
          */
         Long lotteryItemId = this.doLotteryDraw(lotteryActivityId);
-
+        /**
+         * 扣减钱包余额
+         */
+        boolean free = activity.getMoney() == null || activity.getMoney().compareTo(new BigDecimal("0")) == 0;
+        BigDecimal lastWalletBalance = null;
+        if (!free) {
+            // 实时钱包余额扣减
+            try {
+                lastWalletBalance = userWalletService.doTrans(WalletTransEventEnum.JOIN_LOTTERY,
+                        userId,
+                        WebConstant.SYSTEM_WALLET_ID, activity.getMoney());
+            } catch (Exception e) {
+                // 钱包扣除异常
+                // rollback
+                // TODO
+            }
+        }
         /**
          * 2、处理中奖订单
          */
         this.handleLotteryOrder(userId, lotteryActivityId, lotteryItemId);
 
-        return lotteryItemId;
+        return LotteryDrawOneResponseVO.of(lastWalletBalance, lotteryItemId);
+    }
+
+    /**
+     * 校验是否满足抽奖要求
+     *
+     * @param userId
+     * @param activity
+     */
+    private void validateUserJoinLotteryCondition(String userId, LotteryActivityVO activity, int times) {
+        Assert.notNull(activity, "活动不存在");
+        Assert.isTrue(LotteryConstants.LotteryActivityStatus.RUNNING.getStatus().equals(activity.getStatus()), "非进行中活动!");
+        BigDecimal money = activity.getMoney();
+        boolean free = money == null || money.compareTo(new BigDecimal("0")) == 0;
+        if (free) {
+            // 免费抽奖
+            return;
+        }
+        BigDecimal balance = userWalletService.getUserWalletBalanceFromCache(userId);
+        Assert.isTrue(balance.compareTo(money.multiply(new BigDecimal(times))) >=0, "钱包余额不足!");
     }
 
     /**
@@ -69,7 +119,13 @@ public class LotteryService {
      * @param times
      * @return
      */
-    public synchronized List<Long> luckDraw(String lotteryActivityId, String userId, int times) {
+    public LotteryDrawBulkResponseVO luckDraw(String lotteryActivityId, String userId, int times) {
+        // 查询活动配置
+        LotteryActivityVO activity = lotteryActivityService.getLotteryActivityDetailFromCache(lotteryActivityId);
+        /**
+         * 校验活动状态及用户钱包余额
+         */
+        this.validateUserJoinLotteryCondition(userId, activity, times);
         List<Long> result = new Vector<>();
         /**
          * 这里下简单实现,这种做法不是很好,并发量大下可能容易出问题
@@ -79,18 +135,25 @@ public class LotteryService {
             this.handleLotteryOrder(userId, lotteryActivityId, lotteryItemId);
             result.add(lotteryItemId);
         }
-        return result;
-    }
-
-    /**
-     * 最新一期活动抽奖
-     * @param userId
-     * @return
-     */
-    public Long luckDrawLastActivity(String userId) {
-        String activityId = lotteryCacheService.getLastLotteryActivityIdFromCache();
-        Assert.isTrue(StringUtils.isNotBlank(activityId), "未查询到最新一期的抽奖活动");
-        return this.luckDraw(activityId, userId);
+        /**
+         * 扣减钱包余额
+         */
+        boolean free = activity.getMoney() == null || activity.getMoney().compareTo(new BigDecimal("0")) == 0;
+        BigDecimal lastWalletBalance = null;
+        if (!free) {
+            // 实时钱包余额扣减
+            try {
+                lastWalletBalance = userWalletService.doTrans(WalletTransEventEnum.JOIN_LOTTERY,
+                        userId,
+                        WebConstant.SYSTEM_WALLET_ID,
+                        activity.getMoney().multiply(new BigDecimal(times)));
+            } catch (Exception e) {
+                // 钱包扣除异常
+                // rollback
+                // TODO
+            }
+        }
+        return LotteryDrawBulkResponseVO.of(lastWalletBalance, result);
     }
 
     /**
@@ -131,6 +194,8 @@ public class LotteryService {
             for (int i = 0; i < stock; i++) {
                 itemPool.add(itemId);
             }
+            // 打散奖池
+            ListUtil.shuffle(itemPool);
         }
 
         /**

+ 10 - 2
src/main/resources/templates/admin/lottery-activity.html

@@ -78,6 +78,9 @@
                 '<div class="popup-module-core-div-item">' +
                 '<input class="popup-input-number" id="create-activity-name" placeholder="活动名称(30字以内)">' +
                 '</div>' +
+                '<div class="popup-module-core-div-item">' +
+                '<input class="popup-input-number" id="create-activity-money" placeholder="本期抽奖需要消耗多少钱">' +
+                '</div>' +
                 '<div class="popup-module-core-div-item" style="height: 100px; overflow: hidden">' +
                 '<textarea class="allDiv" id="create-activity-description" style="border: 0px; padding: 5px; line-height: 23px; font-size: 14px; height: 100px; width: 260px; resize: none" placeholder="活动描述"></textarea>' +
                 '</div>' +
@@ -88,7 +91,7 @@
                 '</div>'+
                 '</div>' +
                 '<div class="popup-module-core-div-item" style=\"border: 0px; margin-top: 20px;\">' +
-                '<button class=\"popup-btn\" onclick=\"createLotteryActivity($(\'#create-activity-name\').val(), $(\'#create-activity-description\').val(), $(\'#activity-cover\').attr(\'src\'))\">创建抽奖活动</button>' +
+                '<button class=\"popup-btn\" onclick=\"createLotteryActivity($(\'#create-activity-name\').val(),  $(\'#create-activity-money\').val(), $(\'#create-activity-description\').val(), $(\'#activity-cover\').attr(\'src\'))\">创建抽奖活动</button>' +
                 '</div>' +
                 '</div>' +
                 '</div>'
@@ -113,7 +116,7 @@
     })
 
 
-    function createLotteryActivity(name, description, cover) {
+    function createLotteryActivity(name, money, description, cover) {
         if (name == '') {
             layer.msg("活动名不能为空");
             return;
@@ -126,6 +129,10 @@
             layer.msg("封面不能为空");
             return;
         }
+        if (money != null && money < 0) {
+            layer.msg("钱数不能为负数");
+            return;
+        }
         $.ajax({
             url: "/api/lottery/activity/save",
             type: "post",
@@ -135,6 +142,7 @@
                 name: name,
                 description: description,
                 cover: cover,
+                money: money
             }),
             success:function (data) {
                 data = eval(data);

+ 15 - 1
src/main/resources/templates/client/header.html

@@ -22,6 +22,20 @@
             top: 15px;
             height: 30px;
         }
+        .wallet-balance {
+            position: relative;
+            float: right;
+            border: none;
+            padding: 5px 20px;
+            margin-right: 100px;
+            margin-top: 15px;
+            background-color: transparent;
+            border-radius: 100px;
+            font-size: 13px;
+            color: white;
+            background-image: linear-gradient(to right, #fab160, #c58a3f);
+            opacity: 0.8;
+        }
     </style>
 
     <script>
@@ -45,7 +59,7 @@
             <span class="header-level1-link float-right layui-btn-container" id="login-user" style="float: right; margin-right: 0px; display: none; height: 100%">
                 <img src="" id="login-user-photo" class="common-login-user down-user-center" style="width: 30px; margin-top: 13px;">
             </span>
-
+            <button class="wallet-balance">钱包余额:<b><i id="walletBalance">0</i></b>  ¥</button>
         </div>
     </div>
 

+ 23 - 3
src/main/resources/templates/client/lottery.html

@@ -98,7 +98,7 @@
                     <button class="luck-border"></button>
                     <button class="luck-btn" id="luck-btn">抽奖</button>
                 </div>
-                <div class="luck-reduce-integral-rule">本期每抽奖一次需消耗 <b id="reduce-count">-</b> 猿气值</div>
+                <div class="luck-reduce-integral-rule">本期每抽奖一次需 <b id="reduce-count">-</b> ¥</div>
                 <div class="luck-item-count-container">
                     <div class="luck-item-count-container-line" style="color: white; font-weight: 700">
                         <span class="luck-item-rule-left">奖品名称</span>
@@ -186,6 +186,7 @@
 </body>
 <script>
 
+
     $("#header").load("/client/header");
 
     var index = 1;
@@ -201,6 +202,14 @@
     var itemMap = new HashMap();
     var activityId = getUserParamByName("id");
     var historyLastId = '';
+    var money = 0;
+    var walletBalance = 0;
+
+
+    function refreshUserWalletBalance(balance) {
+        this.walletBalance = balance;
+        $("#walletBalance").text(balance)
+    }
 
     if (activityId == '') {
         $.ajax({
@@ -235,8 +244,11 @@
                         return;
                     }
                     var status = data.status;
+                    money = data.money;
+                    refreshUserWalletBalance(data.userInitWalletBalance);
                     $("#activityStatus").text(data.statusName);
                     $("#activityName").text(data.name);
+                    $("#reduce-count").text(data.money);
                     setDisableLuckStateByActivityStatus();
                     if (status == 1) {
                         $("#prepare-mode").show();
@@ -323,6 +335,11 @@
         if (this.isRunning()) {
             return;
         }
+        var validateBalance = money * times;
+        if (walletBalance < validateBalance) {
+            layer.msg("钱包余额不足");
+            return;
+        }
         setDisableLuckState();
         // 抽奖api
         var luckApi = "/api/lottery/luckDraw/" + activityId;
@@ -339,10 +356,13 @@
                 data = eval(data);
                 if (data.success) {
                     // 获取到抽奖结果
+                    data = data.data;
                     if (times == 1) {
-                        luckId = data.data;
+                        luckId = data.itemId;
+                        refreshUserWalletBalance(data.walletBalance);
                     } else if (times == 10) {
-                        luckIdArr = data.data;
+                        luckIdArr = data.itemIdList;
+                        refreshUserWalletBalance(data.walletBalance);
                     } else {
                         layer.msg("不支持的抽奖个数!");
                     }