Browse Source

批量发放卡券功能

fushengqian 1 year ago
parent
commit
82279f24a2

+ 18 - 3
fuint-application/src/main/java/com/fuint/common/service/CouponService.java

@@ -65,11 +65,26 @@ public interface CouponService extends IService<MtCoupon> {
      * 发放卡券
      *
      * @param couponId 券ID
-     * @param mobile  操作人
-     * @param num     发放套数
+     * @param userId  会员ID
+     * @param num    发放套数
+     * @param sendMessage 是否发送消息
+     * @param uuid    批次号
+     * @param operator 操作人
+     * @throws BusinessCheckException
+     */
+    void sendCoupon(Integer couponId, Integer userId, Integer num, Boolean sendMessage, String uuid, String operator) throws BusinessCheckException;
+
+    /**
+     * 发放卡券
+     *
+     * @param couponId 券ID
+     * @param userIds  会员ID
+     * @param num      发放套数
+     * @param uuid     批次号
+     * @param operator 操作人
      * @throws BusinessCheckException
      */
-    void sendCoupon(Integer couponId, String mobile, Integer num, String uuid, String operator) throws BusinessCheckException;
+    Boolean batchSendCoupon(Integer couponId, List<Integer> userIds, Integer num, String uuid, String operator) throws BusinessCheckException;
 
     /**
      * 根据分组获取卡券列表

+ 9 - 0
fuint-application/src/main/java/com/fuint/common/service/MemberService.java

@@ -201,4 +201,13 @@ public interface MemberService extends IService<MtUser> {
      * @return
      * */
     String deCodePassword(String password, String salt);
+
+    /**
+     * 获取会员ID列表
+     *
+     * @param merchantId
+     * @param storeId
+     * @return
+     * */
+    List<Integer> getUserIdList(Integer merchantId, Integer storeId);
 }

+ 4 - 2
fuint-application/src/main/java/com/fuint/common/service/impl/CouponGroupServiceImpl.java

@@ -422,7 +422,7 @@ public class CouponGroupServiceImpl extends ServiceImpl<MtCouponGroupMapper, MtC
 
              Integer totalNum = couponInfo.getTotal() == null ? 0 : couponInfo.getTotal();
              Integer sendNum = couponIdMap.get(couponId);
-             Integer hasSendNum = this.getSendNum(Integer.parseInt(couponId));
+             Integer hasSendNum = getSendNum(Integer.parseInt(couponId));
              if (totalNum > 0 && ((totalNum - hasSendNum) < sendNum)) {
                  Integer needNum = sendNum - (totalNum - hasSendNum);
                  if (StringUtil.isNotBlank(errorMsgNoNum.toString())) {
@@ -456,7 +456,9 @@ public class CouponGroupServiceImpl extends ServiceImpl<MtCouponGroupMapper, MtC
                 // 发送总价值
                 BigDecimal totalMoney = new BigDecimal("0.0");
                 for (int gid = 0; gid < cellDto.getGroupId().size(); gid++) {
-                    couponService.sendCoupon(cellDto.getGroupId().get(gid).intValue(), cellDto.getMobile(), cellDto.getNum().get(gid), uuid, operator);
+                    MtCouponGroup mtCouponGroup = getById(cellDto.getGroupId().get(gid).intValue());
+                    MtUser mtUser = memberService.queryMemberByMobile(mtCouponGroup.getMerchantId(), cellDto.getMobile());
+                    couponService.sendCoupon(cellDto.getGroupId().get(gid).intValue(), mtUser.getId(), cellDto.getNum().get(gid), false, uuid, operator);
                     List<MtCoupon> couponList = couponService.queryCouponListByGroupId(cellDto.getGroupId().get(gid).intValue());
                     // 累加总张数、总价值
                     for (MtCoupon coupon : couponList) {

+ 94 - 15
fuint-application/src/main/java/com/fuint/common/service/impl/CouponServiceImpl.java

@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.fuint.common.Constants;
 import com.fuint.common.dto.CouponDto;
 import com.fuint.common.dto.ReqCouponDto;
+import com.fuint.common.dto.ReqSendLogDto;
 import com.fuint.common.enums.*;
 import com.fuint.common.param.CouponListParam;
 import com.fuint.common.service.*;
@@ -78,6 +79,16 @@ public class CouponServiceImpl extends ServiceImpl<MtCouponMapper, MtCoupon> imp
      * */
     private ConfirmLogService confirmLogService;
 
+    /**
+     * 卡券发放记录服务接口
+     * */
+    private SendLogService sendLogService;
+
+    /**
+     * 卡券分组服务接口
+     * */
+    private CouponGroupService couponGroupService;
+
     /**
      * 系统配置服务接口
      * */
@@ -542,22 +553,27 @@ public class CouponServiceImpl extends ServiceImpl<MtCouponMapper, MtCoupon> imp
     /**
      * 发放卡券
      *
-     * @param couponId 卡券ID
-     * @param mobile   手机号
-     * @param num      发放套数
+     * @param  couponId 卡券ID
+     * @param  userId 会员ID
+     * @param  num 发放套数
+     * @param  sendMessage 是否发送消息
+     * @param  uuid 批次号
+     * @param  operator 操作人
      * @throws BusinessCheckException
      */
     @Override
     @Transactional(rollbackFor = Exception.class)
     @OperationServiceLog(description = "发放卡券")
-    public void sendCoupon(Integer couponId, String mobile, Integer num, String uuid, String operator) throws BusinessCheckException {
+    public void sendCoupon(Integer couponId, Integer userId, Integer num, Boolean sendMessage, String uuid, String operator) throws BusinessCheckException {
         MtCoupon couponInfo = queryCouponById(couponId);
-        MtUser userInfo = memberService.queryMemberByMobile(couponInfo.getMerchantId(), mobile);
+        MtUser userInfo = memberService.queryMemberById(userId);
 
         if (null == userInfo || !userInfo.getStatus().equals(StatusEnum.ENABLED.getKey())) {
             throw new BusinessCheckException("该会员不存在或已禁用,请先注册会员");
         }
 
+        String mobile = StringUtil.isNotEmpty(userInfo.getMobile()) ? userInfo.getMobile() : "";
+
         // 判断券是否有效
         if (!couponInfo.getStatus().equals(StatusEnum.ENABLED.getKey())) {
             throw new BusinessCheckException("卡券“"+couponInfo.getName()+"”已停用,不能发放");
@@ -580,7 +596,6 @@ public class CouponServiceImpl extends ServiceImpl<MtCouponMapper, MtCoupon> imp
                 param.put("userId", userInfo.getId());
                 param.put("param", storeParams);
                 param.put("orderId", 0);
-
                 userCouponService.preStore(param);
             }
             return;
@@ -627,18 +642,82 @@ public class CouponServiceImpl extends ServiceImpl<MtCouponMapper, MtCoupon> imp
             }
         }
 
-        // 发送小程序订阅消息
-        if (userInfo != null && couponInfo != null && couponInfo.getAmount().compareTo(new BigDecimal("0")) > 0) {
-            Date nowTime = new Date();
-            Date sendTime = new Date(nowTime.getTime());
-            Map<String, Object> params = new HashMap<>();
-            params.put("name", couponInfo.getName());
-            params.put("amount", couponInfo.getAmount());
-            params.put("tips", "您的卡券已到账,请查收~");
-            weixinService.sendSubscribeMessage(userInfo.getMerchantId(), userInfo.getId(), userInfo.getOpenId(), WxMessageEnum.COUPON_ARRIVAL.getKey(), "pages/user/index", params, sendTime);
+        // 发放记录
+        MtCouponGroup mtCouponGroup = couponGroupService.queryCouponGroupById(couponInfo.getGroupId());
+        ReqSendLogDto sendLogDto = new ReqSendLogDto();
+        sendLogDto.setMerchantId(couponInfo.getMerchantId());
+        sendLogDto.setType(1);
+        sendLogDto.setMobile(mobile);
+        sendLogDto.setUserId(userInfo.getId());
+        sendLogDto.setFileName("");
+        sendLogDto.setGroupId(couponInfo.getGroupId());
+        sendLogDto.setGroupName(mtCouponGroup.getName());
+        sendLogDto.setCouponId(couponInfo.getId());
+        sendLogDto.setSendNum(num);
+        sendLogDto.setOperator(operator);
+        sendLogDto.setUuid(uuid);
+        sendLogDto.setMerchantId(couponInfo.getMerchantId());
+        sendLogDto.setStoreId(couponInfo.getStoreId());
+        sendLogService.addSendLog(sendLogDto);
+
+        if (sendMessage) {
+            // 发送手机短信
+            if (StringUtil.isNotEmpty(mobile)) {
+                List<String> mobileList = new ArrayList<>();
+                mobileList.add(mobile);
+                Integer totalNum = 0;
+                BigDecimal totalMoney = new BigDecimal("0.0");
+                List<MtCoupon> couponList = queryCouponListByGroupId(couponInfo.getGroupId());
+                for (MtCoupon coupon : couponList) {
+                    totalNum = totalNum + (coupon.getSendNum() * num);
+                    totalMoney = totalMoney.add((coupon.getAmount().multiply(new BigDecimal(num).multiply(new BigDecimal(coupon.getSendNum())))));
+                }
+                Map<String, String> params = new HashMap<>();
+                params.put("totalNum", totalNum + "");
+                params.put("totalMoney", totalMoney + "");
+                sendSmsService.sendSms(couponInfo.getMerchantId(), "received-coupon", mobileList, params);
+            }
+
+            // 发送小程序订阅消息
+            if (userInfo != null && couponInfo != null && couponInfo.getAmount().compareTo(new BigDecimal("0")) > 0) {
+                Date nowTime = new Date();
+                Date sendTime = new Date(nowTime.getTime());
+                Map<String, Object> params = new HashMap<>();
+                params.put("name", couponInfo.getName());
+                params.put("amount", couponInfo.getAmount());
+                params.put("tips", "您的卡券已到账,请查收~");
+                weixinService.sendSubscribeMessage(userInfo.getMerchantId(), userInfo.getId(), userInfo.getOpenId(), WxMessageEnum.COUPON_ARRIVAL.getKey(), "pages/user/index", params, sendTime);
+            }
         }
     }
 
+    /**
+     * 发放卡券
+     *
+     * @param couponId 券ID
+     * @param userIds  会员ID
+     * @param num      发放套数
+     * @param uuid     批次号
+     * @param operator 操作人
+     * @throws BusinessCheckException
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    @OperationServiceLog(description = "发放卡券")
+    public Boolean batchSendCoupon(Integer couponId, List<Integer> userIds, Integer num, String uuid, String operator) throws BusinessCheckException {
+       if (userIds == null || userIds.size() < 1) {
+           throw new BusinessCheckException("发放对象异常,卡券发放失败");
+       }
+       // 发放人数大于10就不发送消息了
+       Boolean sendMsg = userIds.size() >= 10 ? false : true;
+       if (userIds != null && userIds.size() > 0) {
+           for (Integer userId : userIds) {
+                sendCoupon(couponId, userId, num, sendMsg, uuid, operator);
+           }
+       }
+       return true;
+    }
+
     /**
      * 核销卡券
      *

+ 12 - 0
fuint-application/src/main/java/com/fuint/common/service/impl/MemberServiceImpl.java

@@ -906,4 +906,16 @@ public class MemberServiceImpl extends ServiceImpl<MtUserMapper, MtUser> impleme
     public String deCodePassword(String password, String salt) {
         return MD5Util.getMD5(password + salt);
     }
+
+    /**
+     * 获取会员ID列表
+     *
+     * @param merchantId
+     * @param storeId
+     * @return
+     * */
+    @Override
+    public List<Integer> getUserIdList(Integer merchantId, Integer storeId) {
+        return mtUserMapper.getUserIdList(merchantId, storeId);
+    }
 }

+ 4 - 0
fuint-application/src/main/java/com/fuint/common/service/impl/UserCouponServiceImpl.java

@@ -489,6 +489,10 @@ public class UserCouponServiceImpl extends ServiceImpl<MtUserCouponMapper, MtUse
         PaginationResponse<MyCouponDto> paginationResponse = new PaginationResponse(pageImpl, MyCouponDto.class);
         paginationResponse.setTotalPages(pageHelper.getPages());
         paginationResponse.setTotalElements(pageHelper.getTotal());
+        if (dataList.size() == 0) {
+            paginationResponse.setTotalPages(0);
+            paginationResponse.setTotalElements(0);
+        }
         paginationResponse.setContent(dataList);
 
         return new ResponseObject(200, "查询成功", paginationResponse);

+ 21 - 59
fuint-application/src/main/java/com/fuint/module/backendApi/controller/BackendCouponController.java

@@ -23,7 +23,6 @@ import lombok.AllArgsConstructor;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
 import javax.servlet.http.HttpServletRequest;
-import java.math.BigDecimal;
 import java.text.ParseException;
 import java.util.*;
 import java.util.regex.Pattern;
@@ -64,21 +63,11 @@ public class BackendCouponController extends BaseController {
      * */
     private GoodsService goodsService;
 
-    /**
-     * 卡券发放记录服务接口
-     * */
-    private SendLogService sendLogService;
-
     /**
      * 会员服务接口
      * */
     private MemberService memberService;
 
-    /**
-     * 短信服务接口
-     * */
-    private SendSmsService sendSmsService;
-
     /**
      * 后台账户服务接口
      * */
@@ -431,7 +420,8 @@ public class BackendCouponController extends BaseController {
         String mobile = request.getParameter("mobile");
         String num = request.getParameter("num");
         String couponId = request.getParameter("couponId");
-
+        String userIds = request.getParameter("userIds");
+        String object = request.getParameter("object");
         AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
         if (accountInfo == null) {
             return getFailureResult(1001, "请先登录");
@@ -440,68 +430,40 @@ public class BackendCouponController extends BaseController {
         if (couponId == null) {
             return getFailureResult(201, "系统参数有误");
         }
-
         if (!PhoneFormatCheckUtils.isChinaPhoneLegal(mobile) && StringUtil.isNotEmpty(mobile)) {
             return getFailureResult(201, "手机号格式有误");
         }
-
         Pattern pattern = Pattern.compile("[0-9]*");
         if (num == null || (!pattern.matcher(num).matches())) {
             return getFailureResult(201, "发放数量必须为正整数");
         }
-
         if (Integer.parseInt(num) > 100) {
             return getFailureResult(201, "发放数量最多为100");
         }
-
         // 导入批次
         String uuid = UUID.randomUUID().toString().replaceAll("-", "");
-        couponService.sendCoupon(Integer.parseInt(couponId), mobile, Integer.parseInt(num), uuid, accountInfo.getAccountName());
-
-        MtCoupon couponInfo = couponService.queryCouponById(Integer.parseInt(couponId));
-        MtUser mtUser = memberService.queryMemberByMobile(accountInfo.getMerchantId(), mobile);
-        MtCouponGroup mtCouponGroup = couponGroupService.queryCouponGroupById(couponInfo.getGroupId());
-
-        // 发放记录
-        ReqSendLogDto dto = new ReqSendLogDto();
-        dto.setMerchantId(couponInfo.getMerchantId());
-        dto.setType(1);
-        dto.setMobile(mobile);
-        dto.setUserId(mtUser.getId());
-        dto.setFileName("");
-        dto.setGroupId(couponInfo.getGroupId());
-        dto.setGroupName(mtCouponGroup.getName());
-        dto.setCouponId(couponInfo.getId());
-        dto.setSendNum(Integer.parseInt(num));
-        String operator = accountInfo.getAccountName();
-        dto.setOperator(operator);
-        dto.setUuid(uuid);
-        dto.setMerchantId(accountInfo.getMerchantId());
-        dto.setStoreId(accountInfo.getStoreId());
-        sendLogService.addSendLog(dto);
-
-        // 发送短信
-        try {
-            List<String> mobileList = new ArrayList<>();
-            mobileList.add(mobile);
-
-            Integer totalNum = 0;
-            BigDecimal totalMoney = new BigDecimal("0.0");
-
-            List<MtCoupon> couponList = couponService.queryCouponListByGroupId(couponInfo.getGroupId());
-            for (MtCoupon coupon : couponList) {
-                totalNum = totalNum + (coupon.getSendNum()*Integer.parseInt(num));
-                totalMoney = totalMoney.add((coupon.getAmount().multiply(new BigDecimal(num).multiply(new BigDecimal(coupon.getSendNum())))));
+        List<Integer> userIdList = new ArrayList<>();
+        if (StringUtil.isNotEmpty(mobile)) {
+            MtUser mtUser = memberService.queryMemberByMobile(accountInfo.getMerchantId(), mobile);
+            if (mtUser != null) {
+                userIdList.add(mtUser.getId());
             }
-
-            Map<String, String> params = new HashMap<>();
-            params.put("totalNum", totalNum+"");
-            params.put("totalMoney", totalMoney+"");
-            sendSmsService.sendSms(couponInfo.getMerchantId(), "received-coupon", mobileList, params);
-        } catch (Exception e) {
-            //empty
+        } else if (object.equals("part") && StringUtil.isNotEmpty(userIds)) {
+            List<String> ids = Arrays.asList(userIds.split(","));
+            if (ids != null && ids.size() > 0) {
+                for (String userId : ids) {
+                     if (StringUtil.isNotEmpty(userId)) {
+                         userIdList.add(Integer.parseInt(userId));
+                     }
+                }
+            }
+        } else if (object.equals("all")) {
+            userIdList = memberService.getUserIdList(accountInfo.getMerchantId(), accountInfo.getStoreId());
+        } else {
+            return getFailureResult(201, "系统参数有误");
         }
 
+        couponService.batchSendCoupon(Integer.parseInt(couponId), userIdList, Integer.parseInt(num), uuid, accountInfo.getAccountName());
         return getSuccessResult(true);
     }
 }

+ 25 - 0
fuint-application/src/main/java/com/fuint/module/backendApi/controller/BackendStatisticController.java

@@ -141,4 +141,29 @@ public class BackendStatisticController extends BaseController {
 
         return getSuccessResult(result);
     }
+
+    /**
+     * 获取会员数量
+     *
+     * @return
+     */
+    @ApiOperation(value = "获取会员数量")
+    @RequestMapping(value = "/totalMember", method = RequestMethod.GET)
+    @CrossOrigin
+    public ResponseObject totalMember(HttpServletRequest request) throws BusinessCheckException {
+        String token = request.getHeader("Access-Token");
+        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
+        if (accountInfo == null) {
+            return getFailureResult(1001, "请先登录");
+        }
+
+        Integer merchantId = accountInfo.getMerchantId();
+        Integer storeId = accountInfo.getStoreId();
+
+        Long totalMember = memberService.getUserCount(merchantId, storeId);
+        Map<String, Object> result = new HashMap<>();
+        result.put("totalMember", totalMember);
+
+        return getSuccessResult(result);
+    }
 }

+ 3 - 0
fuint-repository/src/main/java/com/fuint/repository/mapper/MtUserMapper.java

@@ -40,4 +40,7 @@ public interface MtUserMapper extends BaseMapper<MtUser> {
     Long getStoreUserCountByTime(@Param("storeId") Integer storeId, @Param("beginTime") Date beginTime, @Param("endTime") Date endTime);
 
     List<MemberTopBean> getMemberConsumeTopList(@Param("merchantId") Integer merchantId, @Param("storeId") Integer storeId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);
+
+    List<Integer> getUserIdList(@Param("merchantId") Integer merchantId, @Param("storeId") Integer storeId);
+
 }

+ 11 - 0
fuint-repository/src/main/resources/mapper/MtUserMapper.xml

@@ -91,4 +91,15 @@
         GROUP BY u.`ID` ORDER BY SUM(o.`AMOUNT`) DESC LIMIT 10
     </select>
 
+    <select id="getUserIdList" resultType="java.lang.Integer">
+        select t.ID from mt_user t where t.STATUS = 'A'
+        <if test="merchantId != null and merchantId > 0">
+            AND t.MERCHANT_ID = #{merchantId}
+        </if>
+        <if test="storeId != null and storeId > 0">
+            AND t.STORE_ID = #{storeId}
+        </if>
+        limit 50000;
+    </select>
+
 </mapper>