From c71055635df3d75e5dc838a9b66036c591913a13 Mon Sep 17 00:00:00 2001
From: jiangqs <jiangqs>
Date: 星期六, 08 七月 2023 18:37:12 +0800
Subject: [PATCH] 管理台bug和用户端

---
 ruoyi-modules/ruoyi-member/src/main/java/com/ruoyi/member/service/impl/coupon/CouponServiceImpl.java |  360 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 334 insertions(+), 26 deletions(-)

diff --git a/ruoyi-modules/ruoyi-member/src/main/java/com/ruoyi/member/service/impl/coupon/CouponServiceImpl.java b/ruoyi-modules/ruoyi-member/src/main/java/com/ruoyi/member/service/impl/coupon/CouponServiceImpl.java
index 4f8b57c..dd43e02 100644
--- a/ruoyi-modules/ruoyi-member/src/main/java/com/ruoyi/member/service/impl/coupon/CouponServiceImpl.java
+++ b/ruoyi-modules/ruoyi-member/src/main/java/com/ruoyi/member/service/impl/coupon/CouponServiceImpl.java
@@ -13,6 +13,9 @@
 import com.ruoyi.member.domain.pojo.coupon.CouponRelGoods;
 import com.ruoyi.member.domain.pojo.coupon.CouponRelUser;
 import com.ruoyi.member.domain.pojo.coupon.CouponTotal;
+import com.ruoyi.system.api.domain.dto.BirthdayGiftSendDto;
+import com.ruoyi.system.api.domain.dto.MerBaseGetDto;
+import com.ruoyi.system.api.domain.dto.MerPageDto;
 import com.ruoyi.system.api.domain.poji.member.MemberCoupon;
 import com.ruoyi.member.domain.vo.*;
 import com.ruoyi.member.mapper.coupon.CouponMapper;
@@ -25,6 +28,7 @@
 import com.ruoyi.system.api.constant.AppErrorConstant;
 import com.ruoyi.system.api.domain.dto.MgtBaseGetDto;
 import com.ruoyi.system.api.domain.poji.goods.Goods;
+import com.ruoyi.system.api.domain.vo.MerGoodsPriceListVo;
 import com.ruoyi.system.api.service.RemoteActivityService;
 import com.ruoyi.system.api.service.RemoteGoodsService;
 import org.springframework.scheduling.annotation.Async;
@@ -85,7 +89,7 @@
         List<MgtCouponPageVo> mgtCouponPageVoList = couponMapper.pageMgtCoupon(page, mgtCouponPageDto);
         //处理使用限制和有效期
         if(mgtCouponPageVoList!=null&&!mgtCouponPageVoList.isEmpty()){
-            Integer couponType;
+            String couponType;
             Integer validTimeType;
             Date validStartTime;
             Date validEndTime;
@@ -98,20 +102,10 @@
             for(MgtCouponPageVo mgtCouponPageVo : mgtCouponPageVoList){
                 //处理使用限制
                 couponType = mgtCouponPageVo.getCouponType();
-                switch (couponType){
-                    case 1:
-                        moneyThreshold = mgtCouponPageVo.getMoneyThreshold();
-                        discountMoney = mgtCouponPageVo.getDiscountMoney();
-                        useLimit = "满" + moneyThreshold.toString() + "减" + discountMoney.toString() + "元";
-                        break;
-                    case 2:
-                        break;
-                    case 3:
-                        break;
-                    case 4:
-                        break;
-                    default:
-                        break;
+                if(couponType.equals("满减")){
+                    moneyThreshold = mgtCouponPageVo.getMoneyThreshold();
+                    discountMoney = mgtCouponPageVo.getDiscountMoney();
+                    useLimit = "满" + moneyThreshold.toString() + "减" + discountMoney.toString() + "元";
                 }
                 mgtCouponPageVo.setUseLimit(useLimit);
                 //处理有效期
@@ -157,19 +151,20 @@
                         mgtCouponGoodsListVo.setGoodsId(goods.getGoodsId());
                         mgtCouponGoodsListVo.setGoodsName(goods.getGoodsName());
                         mgtCouponGoodsListVo.setGoodsNo(goods.getGoodsNo());
+                        mgtCouponGoodsListVo.setGoodsType(goods.getGoodsType());
                         //商品类型1周期2服务3体验4单品
                         switch (goods.getGoodsType()){
                             case 1:
-                                mgtCouponGoodsListVo.setGoodsType("周期");
+                                mgtCouponGoodsListVo.setGoodsTypeName("周期");
                                 break;
                             case 2:
-                                mgtCouponGoodsListVo.setGoodsType("服务");
+                                mgtCouponGoodsListVo.setGoodsTypeName("服务");
                                 break;
                             case 3:
-                                mgtCouponGoodsListVo.setGoodsType("体验");
+                                mgtCouponGoodsListVo.setGoodsTypeName("体验");
                                 break;
                             case 4:
-                                mgtCouponGoodsListVo.setGoodsType("单品");
+                                mgtCouponGoodsListVo.setGoodsTypeName("单品");
                                 break;
                             default:
                                 break;
@@ -188,6 +183,38 @@
             mgtCouponGetVo.setRelUserList(relUserList);
         }
         return mgtCouponGetVo;
+    }
+
+    /**
+     * @description  平台获取优惠券编辑信息
+     * @author  jqs
+     * @date    2023/6/13 14:29
+     * @param couponId
+     * @return  MgtCouponGetVo
+     */
+    @Override
+    public MerCouponGetVo getMerCouponVo(String couponId){
+        MerCouponGetVo merCouponGetVo = new MerCouponGetVo();
+        Coupon coupon = this.getById(couponId);
+        BeanUtils.copyProperties(coupon, merCouponGetVo);
+        //判断优惠券是否为指定商品
+        if(coupon.getUseScope()==2){
+            String relGoodsIds = coupon.getRelGoodsIds();
+            if(StringUtils.isNotBlank(relGoodsIds)){
+                //获取指定商品列表
+                List<MerGoodsPriceListVo> goodsList = remoteGoodsService.listGoodsPriceByGoodsId(relGoodsIds).getData();
+                if(!goodsList.isEmpty()){
+                    merCouponGetVo.setRelGoodsList(goodsList);
+                }
+            }
+        }
+        //判断优惠券是否指定发放用户
+        if(coupon.getSendType()==5&&coupon.getRelationType()==1){
+            //获取优惠券发放用户
+            List<MgtCouponMemberListVo> relUserList = couponRelUserService.listCouponMemberVo(coupon.getCouponId());
+            merCouponGetVo.setRelUserList(relUserList);
+        }
+        return merCouponGetVo;
     }
 
     /**
@@ -298,8 +325,12 @@
         Date nowTime = new Date();
         Integer sendType = coupon.getSendType();
         List<Long> userIdList = new ArrayList<>();
+        Long shopId = null;
+        if(coupon.getCouponFrom()==2 && sendType==3){
+            shopId = coupon.getShopId();
+        }
         if(sendType >= 2 && sendType <= 4){
-            userIdList = memberService.listIdBySendType(sendType);
+            userIdList = memberService.listIdBySendType(sendType,shopId);
         }else if(sendType == 5){
             //关联类型1.用户管理筛选2.活动管理筛选
             if(coupon.getRelationType()==2){
@@ -316,7 +347,7 @@
                 userIdList = relUserIdList;
             }
         }
-        if(!relUserIdList.isEmpty()){
+        if(relUserIdList!=null&&!relUserIdList.isEmpty()){
             List<MemberCoupon> memberCouponList = new ArrayList<>();
             MemberCoupon memberCoupon;
             Integer sendTotal = 0;
@@ -406,7 +437,7 @@
         queryWrapper.eq(Coupon::getCouponStatus,1);
         queryWrapper.eq(Coupon::getSendTimeType,2);
         queryWrapper.eq(Coupon::getAuditStatus,2);
-        queryWrapper.eq(Coupon::getSendTime,new Date());
+        queryWrapper.eq(Coupon::getSendTime,DateUtils.getNowDate());
         List<Coupon> couponList = couponMapper.selectList(queryWrapper);
         if(couponList!=null&&!couponList.isEmpty()){
             List<CouponRelUser> couponRelUserList;
@@ -505,19 +536,20 @@
                         mgtCouponGoodsListVo.setGoodsId(goods.getGoodsId());
                         mgtCouponGoodsListVo.setGoodsName(goods.getGoodsName());
                         mgtCouponGoodsListVo.setGoodsNo(goods.getGoodsNo());
+                        mgtCouponGoodsListVo.setGoodsType(goods.getGoodsType());
                         //商品类型1周期2服务3体验4单品
                         switch (goods.getGoodsType()){
                             case 1:
-                                mgtCouponGoodsListVo.setGoodsType("周期");
+                                mgtCouponGoodsListVo.setGoodsTypeName("周期");
                                 break;
                             case 2:
-                                mgtCouponGoodsListVo.setGoodsType("服务");
+                                mgtCouponGoodsListVo.setGoodsTypeName("服务");
                                 break;
                             case 3:
-                                mgtCouponGoodsListVo.setGoodsType("体验");
+                                mgtCouponGoodsListVo.setGoodsTypeName("体验");
                                 break;
                             case 4:
-                                mgtCouponGoodsListVo.setGoodsType("单品");
+                                mgtCouponGoodsListVo.setGoodsTypeName("单品");
                                 break;
                             default:
                                 break;
@@ -570,4 +602,280 @@
             sendCoupon(coupon, userIdList);
         }
     }
+
+    /**
+     * @description  平台获取优惠券列表
+     * @author  jqs
+     * @date    2023/6/30 16:53
+     * @param page
+     * @param merPageDto
+     * @return  List<MerCouponPageVo>
+     */
+    @Override
+    public List<MerCouponPageVo> pagePlatformMerCoupon(Page page, MerPageDto merPageDto){
+        List<MerCouponPageVo> couponPageVoList = couponMapper.pagePlatformMerCoupon(page, merPageDto);
+        //处理使用限制和有效期
+        if(couponPageVoList!=null&&!couponPageVoList.isEmpty()){
+            Integer couponType;
+            Integer validTimeType;
+            Date validStartTime;
+            Date validEndTime;
+            Integer validDay;
+            String validTime = "";
+            BigDecimal moneyThreshold;
+            BigDecimal discountMoney;
+            BigDecimal discountPercent;
+            String useLimit = "";
+            for(MerCouponPageVo merCouponPageVo : couponPageVoList){
+                //处理使用限制
+                couponType = merCouponPageVo.getCouponType();
+                switch (couponType){
+                    case 1:
+                        moneyThreshold = merCouponPageVo.getMoneyThreshold();
+                        discountMoney = merCouponPageVo.getDiscountMoney();
+                        useLimit = "满" + moneyThreshold.toString() + "减" + discountMoney.toString() + "元";
+                        break;
+                    case 2:
+                        break;
+                    case 3:
+                        break;
+                    case 4:
+                        break;
+                    default:
+                        break;
+                }
+                merCouponPageVo.setUseLimit(useLimit);
+                //处理有效期
+                validTimeType = merCouponPageVo.getValidTimeType();
+                if(validTimeType==1){
+                    validStartTime = merCouponPageVo.getValidStartTime();
+                    validEndTime = merCouponPageVo.getValidEndTime();
+                    validTime = DateUtils.dateTime(validStartTime) + '-' +  DateUtils.dateTime(validEndTime);
+                }else if(validTimeType==2){
+                    validDay = merCouponPageVo.getValidDay();
+                    validTime = "领取之日起" + validDay.toString() + "天";
+                }
+                merCouponPageVo.setValidTime(validTime);
+            }
+        }
+        return couponPageVoList;
+    }
+
+    /**
+     * @description  获取商户优惠券列表
+     * @author  jqs
+     * @date    2023/6/30 16:53
+     * @param page
+     * @param merPageDto
+     * @return  List<MerCouponPageVo>
+     */
+    @Override
+    public List<MerCouponPageVo> pageShopMerCoupon(Page page, MerCouponPageDto merPageDto){
+        List<MerCouponPageVo> couponPageVoList = couponMapper.pageShopMerCoupon(page, merPageDto);
+        //处理使用限制和有效期
+        if(couponPageVoList!=null&&!couponPageVoList.isEmpty()){
+            Integer couponType;
+            Integer validTimeType;
+            Date validStartTime;
+            Date validEndTime;
+            Integer validDay;
+            String validTime = "";
+            BigDecimal moneyThreshold;
+            BigDecimal discountMoney;
+            BigDecimal discountPercent;
+            String useLimit = "";
+            for(MerCouponPageVo merCouponPageVo : couponPageVoList){
+                //处理使用限制
+                couponType = merCouponPageVo.getCouponType();
+                switch (couponType){
+                    case 1:
+                        moneyThreshold = merCouponPageVo.getMoneyThreshold();
+                        discountMoney = merCouponPageVo.getDiscountMoney();
+                        useLimit = "满" + moneyThreshold.toString() + "减" + discountMoney.toString() + "元";
+                        break;
+                    case 2:
+                        break;
+                    case 3:
+                        break;
+                    case 4:
+                        break;
+                    default:
+                        break;
+                }
+                merCouponPageVo.setUseLimit(useLimit);
+                //处理有效期
+                validTimeType = merCouponPageVo.getValidTimeType();
+                if(validTimeType==1){
+                    validStartTime = merCouponPageVo.getValidStartTime();
+                    validEndTime = merCouponPageVo.getValidEndTime();
+                    validTime = DateUtils.dateTime(validStartTime) + '-' +  DateUtils.dateTime(validEndTime);
+                }else if(validTimeType==2){
+                    validDay = merCouponPageVo.getValidDay();
+                    validTime = "领取之日起" + validDay.toString() + "天";
+                }
+                merCouponPageVo.setValidTime(validTime);
+            }
+        }
+        return couponPageVoList;
+    }
+
+
+    /**
+     * @description  商户编辑优惠券
+     * @author  jqs
+     * @date    2023/7/3 9:21
+     * @param merCouponEditDto
+     * @return  void
+     */
+    @Override
+    public void editMerCoupon(MerCouponEditDto merCouponEditDto){
+        //编辑优惠券
+        Coupon coupon;
+        CouponTotal couponTotal;
+        if(StringUtils.isNotBlank(merCouponEditDto.getCouponId())){
+            coupon = this.getById(merCouponEditDto.getCouponId());
+            if(coupon.getAuditStatus()!=3){
+                throw new ServiceException(AppErrorConstant.COUPON_AUDIT_EDIT);
+            }
+            couponRelUserService.deleteCouponRelByCouponId(coupon.getCouponId());
+            couponRelGoodsService.deleteCouponRelByCouponId(coupon.getCouponId());
+            coupon.setCouponStatus(1);
+        }else{
+            coupon = new Coupon();
+            String couponId = IdUtils.simpleUUID();
+            coupon.setCouponId(couponId);
+            coupon.setDelFlag(0);
+            coupon.setShopId(merCouponEditDto.getShopId());
+            coupon.setCouponStatus(1);
+            coupon.setCreateTime(new Date());
+            coupon.setCreateUserId(merCouponEditDto.getUserId());
+            coupon.setCouponFrom(2);
+            //创建优惠券统计
+            couponTotal = new CouponTotal();
+            couponTotal.setCouponId(couponId);
+            couponTotal.setSendCount(0);
+            couponTotal.setSendUserCount(0);
+            couponTotalService.save(couponTotal);
+        }
+        //优惠券赋值
+        coupon.setAuditStatus(1);
+        coupon.setCouponType(merCouponEditDto.getCouponType());
+        coupon.setCouponName(merCouponEditDto.getCouponName());
+        coupon.setSendType(merCouponEditDto.getSendType());
+        coupon.setSendTimeType(merCouponEditDto.getSendTimeType());
+        coupon.setSendTime(merCouponEditDto.getSendTime());
+        coupon.setMoneyThreshold(merCouponEditDto.getMoneyThreshold());
+        coupon.setDiscountMoney(merCouponEditDto.getDiscountMoney());
+        coupon.setDiscountPercent(merCouponEditDto.getDiscountPercent());
+        coupon.setUseScope(merCouponEditDto.getUseScope());
+        coupon.setValidTimeType(merCouponEditDto.getValidTimeType());
+        coupon.setValidStartTime(merCouponEditDto.getValidStartTime());
+        coupon.setValidEndTime(merCouponEditDto.getValidEndTime());
+        coupon.setValidDay(merCouponEditDto.getValidDay());
+        coupon.setUpdateTime(new Date());
+        coupon.setUpdateUserId(merCouponEditDto.getUserId());
+        coupon.setRelationType(1);
+        //判断是否指定商品
+        if(coupon.getUseScope()==2&&!merCouponEditDto.getRelGoodsIdList().isEmpty()){
+            List<String> relGoodsIdList = merCouponEditDto.getRelGoodsIdList();
+            List<CouponRelGoods> couponRelGoodsList = relGoodsIdList.stream()
+                    .map(str -> {
+                        CouponRelGoods couponRelGoods = new CouponRelGoods();
+                        couponRelGoods.setDelFlag(0);
+                        couponRelGoods.setCouponId(coupon.getCouponId());
+                        couponRelGoods.setGoodsId(str);
+                        return couponRelGoods;
+                    })
+                    .collect(Collectors.toList());
+            couponRelGoodsService.saveBatch(couponRelGoodsList);
+            String relGoodsIds = relGoodsIdList.stream().collect(Collectors.joining(","));
+            coupon.setRelGoodsIds(relGoodsIds);
+        }
+        //判断是否指定用户
+        List<Long> relUserIdList = null;
+        if(coupon.getSendType()==5&&coupon.getRelationType()==1&&!merCouponEditDto.getRelUserIdList().isEmpty()){
+            relUserIdList = merCouponEditDto.getRelUserIdList();
+            List<CouponRelUser> couponRelUserList = relUserIdList.stream()
+                    .map(str -> {
+                        CouponRelUser couponRelUser = new CouponRelUser();
+                        couponRelUser.setDelFlag(0);
+                        couponRelUser.setCouponId(coupon.getCouponId());
+                        couponRelUser.setUserId(str);
+                        return couponRelUser;
+                    })
+                    .collect(Collectors.toList());
+            couponRelUserService.saveBatch(couponRelUserList);
+        }
+        this.saveOrUpdate(coupon);
+    }
+
+    /**
+     * @description  商户删除优惠券
+     * @author  jqs
+     * @date    2023/7/3 15:04
+     * @param merBaseGetDto
+     * @return  void
+     */
+    @Override
+    public void deleteMerCoupon(MerBaseGetDto merBaseGetDto){
+        Coupon coupon = this.getById(merBaseGetDto.getId());
+        coupon.setCouponStatus(-1);
+        coupon.setDelFlag(1);
+        coupon.setUpdateUserId(merBaseGetDto.getUserId());
+        coupon.setUpdateTime(new Date());
+        this.saveOrUpdate(coupon);
+    }
+
+    /**
+     * @description  发送用户优惠券礼物
+     * @author  jqs
+     * @date    2023/7/7 19:54
+     * @param giftSendDtoList
+     * @return  void
+     */
+    @Override
+    public void sendCouponGift(List<BirthdayGiftSendDto> giftSendDtoList){
+        Long userId = giftSendDtoList.get(0).getUserId();
+        Long shopId = giftSendDtoList.get(0).getShopId();
+        String couponId;
+        Coupon coupon;
+        List<MemberCoupon> memberCouponList = new ArrayList<>();
+        MemberCoupon memberCoupon;
+        String memberCouponId;
+        for(BirthdayGiftSendDto birthdayGiftSendDto : giftSendDtoList){
+            couponId = birthdayGiftSendDto.getCouponId();
+            coupon = this.getById(couponId);
+            memberCoupon = new MemberCoupon();
+            memberCouponId = IdUtils.simpleUUID();
+            memberCoupon.setId(memberCouponId);
+            memberCoupon.setDelFlag(0);
+            memberCoupon.setCouponId(couponId);
+            memberCoupon.setUserId(userId);
+            memberCoupon.setShopId(shopId);
+            memberCoupon.setCouponType(coupon.getCouponType());
+            memberCoupon.setCouponStatus(coupon.getCouponStatus());
+            memberCoupon.setCouponName(coupon.getCouponName());
+            memberCoupon.setSendType(coupon.getSendType());
+            memberCoupon.setSendTimeType(coupon.getSendTimeType());
+            memberCoupon.setSendTime(coupon.getSendTime());
+            memberCoupon.setMoneyThreshold(coupon.getMoneyThreshold());
+            memberCoupon.setDiscountMoney(coupon.getDiscountMoney());
+            memberCoupon.setDiscountPercent(coupon.getDiscountPercent());
+            memberCoupon.setUseScope(coupon.getUseScope());
+            memberCoupon.setValidTimeType(coupon.getValidTimeType());
+            memberCoupon.setValidStartTime(coupon.getValidStartTime());
+            memberCoupon.setValidEndTime(coupon.getValidEndTime());
+            memberCoupon.setValidDay(coupon.getValidDay());
+            memberCoupon.setReceiveTime(new Date());
+            //有效期处理1.时间段2.领取之日起
+            if(coupon.getValidTimeType()==1){
+                memberCoupon.setDeadlineTime(coupon.getValidEndTime());
+            }else if(coupon.getValidTimeType()==2){
+                memberCoupon.setDeadlineTime(DateUtils.addDays(new Date(),coupon.getValidDay()));
+            }
+            memberCoupon.setRelGoodsIds(coupon.getRelGoodsIds());
+            memberCouponList.add(memberCoupon);
+        }
+        memberCouponService.saveBatch(memberCouponList);
+    }
 }

--
Gitblit v1.7.1