活动优惠编写完成
This commit is contained in:
		@@ -1,8 +1,17 @@
 | 
			
		||||
package com.atguigu.ssyx.activity.controller;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
import org.springframework.web.bind.annotation.RequestMapping;
 | 
			
		||||
import org.springframework.web.bind.annotation.RestController;
 | 
			
		||||
import com.atguigu.ssyx.activity.service.CouponInfoService;
 | 
			
		||||
import com.atguigu.ssyx.common.result.Result;
 | 
			
		||||
import com.atguigu.ssyx.model.activity.CouponInfo;
 | 
			
		||||
import com.atguigu.ssyx.vo.activity.CouponRuleVo;
 | 
			
		||||
import com.baomidou.mybatisplus.core.metadata.IPage;
 | 
			
		||||
import io.swagger.annotations.ApiOperation;
 | 
			
		||||
import io.swagger.annotations.ApiParam;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.web.bind.annotation.*;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * <p>
 | 
			
		||||
@@ -13,8 +22,79 @@ import org.springframework.web.bind.annotation.RestController;
 | 
			
		||||
 * @since 2023-09-17
 | 
			
		||||
 */
 | 
			
		||||
@RestController
 | 
			
		||||
@RequestMapping("/activity/coupon-info")
 | 
			
		||||
@CrossOrigin
 | 
			
		||||
@RequestMapping("/admin/activity/couponInfo")
 | 
			
		||||
public class CouponInfoController {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private CouponInfoService couponInfoService;
 | 
			
		||||
 | 
			
		||||
    @ApiOperation(value = "获取分页列表")
 | 
			
		||||
    @GetMapping("{page}/{limit}")
 | 
			
		||||
    public Result index(
 | 
			
		||||
            @ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
 | 
			
		||||
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit) {
 | 
			
		||||
        IPage<CouponInfo> pageModel = couponInfoService.selectPage(page, limit);
 | 
			
		||||
        return Result.ok(pageModel);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @ApiOperation(value = "根据ID查询优惠券")
 | 
			
		||||
    @GetMapping("get/{id}")
 | 
			
		||||
    public Result get(@PathVariable Long id) {
 | 
			
		||||
        CouponInfo couponInfo = couponInfoService.getCouponInfo(id);
 | 
			
		||||
        return Result.ok(couponInfo);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @ApiOperation(value = "新增优惠券")
 | 
			
		||||
    @PostMapping("save")
 | 
			
		||||
    public Result save(@RequestBody CouponInfo couponInfo) {
 | 
			
		||||
        couponInfoService.save(couponInfo);
 | 
			
		||||
        return Result.ok(null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @ApiOperation(value = "修改优惠券")
 | 
			
		||||
    @PutMapping("update")
 | 
			
		||||
    public Result updateById(@RequestBody CouponInfo couponInfo) {
 | 
			
		||||
        couponInfoService.updateById(couponInfo);
 | 
			
		||||
        return Result.ok(null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @ApiOperation(value = "删除优惠券")
 | 
			
		||||
    @DeleteMapping("remove/{id}")
 | 
			
		||||
    public Result remove(@PathVariable String id) {
 | 
			
		||||
        couponInfoService.removeById(id);
 | 
			
		||||
        return Result.ok(null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @ApiOperation(value = "根据id列表删除优惠券")
 | 
			
		||||
    @DeleteMapping("batchRemove")
 | 
			
		||||
    public Result batchRemove(@RequestBody List<String> idList) {
 | 
			
		||||
        couponInfoService.removeByIds(idList);
 | 
			
		||||
        return Result.ok(null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @ApiOperation(value = "获取优惠券信息")
 | 
			
		||||
    @GetMapping("findCouponRuleList/{id}")
 | 
			
		||||
    public Result findActivityRuleList(@PathVariable Long id) {
 | 
			
		||||
        return Result.ok(couponInfoService.findCouponRuleList(id));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @ApiOperation(value = "新增优惠券")
 | 
			
		||||
    @PostMapping("saveCouponRule")
 | 
			
		||||
    public Result saveCouponRule(@RequestBody CouponRuleVo couponRuleVo) {
 | 
			
		||||
        couponInfoService.saveCouponRule(couponRuleVo);
 | 
			
		||||
        return Result.ok(null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 根据关键字获取sku列表,活动使用
 | 
			
		||||
     *
 | 
			
		||||
     * @param keyword
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    @GetMapping("findCouponByKeyword/{keyword}")
 | 
			
		||||
    public Result findCouponByKeyword(@PathVariable("keyword") String keyword) {
 | 
			
		||||
        return Result.ok(couponInfoService.findCouponByKeyword(keyword));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@ package com.atguigu.ssyx.activity.mapper;
 | 
			
		||||
 | 
			
		||||
import com.atguigu.ssyx.model.activity.CouponRange;
 | 
			
		||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 | 
			
		||||
import org.springframework.stereotype.Repository;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * <p>
 | 
			
		||||
@@ -11,6 +12,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 | 
			
		||||
 * @author atguigu
 | 
			
		||||
 * @since 2023-09-17
 | 
			
		||||
 */
 | 
			
		||||
@Repository
 | 
			
		||||
public interface CouponRangeMapper extends BaseMapper<CouponRange> {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,13 @@
 | 
			
		||||
package com.atguigu.ssyx.activity.service;
 | 
			
		||||
 | 
			
		||||
import com.atguigu.ssyx.model.activity.CouponInfo;
 | 
			
		||||
import com.atguigu.ssyx.vo.activity.CouponRuleVo;
 | 
			
		||||
import com.baomidou.mybatisplus.core.metadata.IPage;
 | 
			
		||||
import com.baomidou.mybatisplus.extension.service.IService;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * <p>
 | 
			
		||||
 * 优惠券信息 服务类
 | 
			
		||||
@@ -13,4 +18,44 @@ import com.baomidou.mybatisplus.extension.service.IService;
 | 
			
		||||
 */
 | 
			
		||||
public interface CouponInfoService extends IService<CouponInfo> {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 获取分页列表
 | 
			
		||||
     *
 | 
			
		||||
     * @param page
 | 
			
		||||
     * @param limit
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    IPage<CouponInfo> selectPage(Long page, Long limit);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 根据ID查询优惠券
 | 
			
		||||
     *
 | 
			
		||||
     * @param id
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    CouponInfo getCouponInfo(Long id);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 根据优惠卷id获取优惠券规则列表
 | 
			
		||||
     *
 | 
			
		||||
     * @param id
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    Map<String, Object> findCouponRuleList(Long id);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 新增优惠券规则
 | 
			
		||||
     *
 | 
			
		||||
     * @param couponRuleVo
 | 
			
		||||
     */
 | 
			
		||||
    void saveCouponRule(CouponRuleVo couponRuleVo);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 根据关键字获取sku列表,活动使用
 | 
			
		||||
     *
 | 
			
		||||
     * @param keyword
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    List<CouponInfo> findCouponByKeyword(String keyword);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,27 @@
 | 
			
		||||
package com.atguigu.ssyx.activity.service.impl;
 | 
			
		||||
 | 
			
		||||
import com.atguigu.ssyx.activity.mapper.CouponInfoMapper;
 | 
			
		||||
import com.atguigu.ssyx.activity.mapper.CouponRangeMapper;
 | 
			
		||||
import com.atguigu.ssyx.activity.service.CouponInfoService;
 | 
			
		||||
import com.atguigu.ssyx.client.product.ProductFeignClient;
 | 
			
		||||
import com.atguigu.ssyx.enums.CouponRangeType;
 | 
			
		||||
import com.atguigu.ssyx.model.activity.CouponInfo;
 | 
			
		||||
import com.atguigu.ssyx.model.activity.CouponRange;
 | 
			
		||||
import com.atguigu.ssyx.model.product.Category;
 | 
			
		||||
import com.atguigu.ssyx.model.product.SkuInfo;
 | 
			
		||||
import com.atguigu.ssyx.vo.activity.CouponRuleVo;
 | 
			
		||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 | 
			
		||||
import com.baomidou.mybatisplus.core.metadata.IPage;
 | 
			
		||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 | 
			
		||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.springframework.util.CollectionUtils;
 | 
			
		||||
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * <p>
 | 
			
		||||
@@ -17,4 +34,119 @@ import org.springframework.stereotype.Service;
 | 
			
		||||
@Service
 | 
			
		||||
public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponInfo> implements CouponInfoService {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    CouponRangeMapper couponRangeMapper;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    ProductFeignClient productFeignClient;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 获取分页列表
 | 
			
		||||
     *
 | 
			
		||||
     * @param page
 | 
			
		||||
     * @param limit
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public IPage<CouponInfo> selectPage(Long page, Long limit) {
 | 
			
		||||
        Page<CouponInfo> pageParam = new Page<>(page, limit);
 | 
			
		||||
        Page<CouponInfo> couponInfoPage = baseMapper.selectPage(pageParam, null);
 | 
			
		||||
        List<CouponInfo> couponInfoList = couponInfoPage.getRecords();
 | 
			
		||||
        couponInfoList.forEach(item -> {
 | 
			
		||||
            item.setCouponTypeString(item.getCouponType()
 | 
			
		||||
                                         .getComment());
 | 
			
		||||
            CouponRangeType rangeType = item.getRangeType();
 | 
			
		||||
            if (rangeType != null) {
 | 
			
		||||
                item.setRangeTypeString(rangeType.getComment());
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        return couponInfoPage;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 根据ID查询优惠券
 | 
			
		||||
     *
 | 
			
		||||
     * @param id
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public CouponInfo getCouponInfo(Long id) {
 | 
			
		||||
        CouponInfo couponInfo = baseMapper.selectById(id);
 | 
			
		||||
        couponInfo.setCouponTypeString(couponInfo.getCouponType()
 | 
			
		||||
                                                 .getComment());
 | 
			
		||||
        if (couponInfo.getRangeType() != null) {
 | 
			
		||||
            couponInfo.setRangeTypeString(couponInfo.getRangeType()
 | 
			
		||||
                                                    .getComment());
 | 
			
		||||
        }
 | 
			
		||||
        return couponInfo;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 根据优惠卷id获取优惠券规则列表
 | 
			
		||||
     *
 | 
			
		||||
     * @param id
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public Map<String, Object> findCouponRuleList(Long id) {
 | 
			
		||||
        //第一步 根据优惠卷id查询优惠卷基本信息  coupon_info表
 | 
			
		||||
        CouponInfo couponInfo = baseMapper.selectById(id);
 | 
			
		||||
 | 
			
		||||
        //第二步 根据优惠卷id查询coupon_range 查询里面对应range_id
 | 
			
		||||
        List<CouponRange> couponRangeList = couponRangeMapper.selectList(new LambdaQueryWrapper<CouponRange>().eq(CouponRange::getCouponId, id));
 | 
			
		||||
        //couponRangeList获取所有range_id
 | 
			
		||||
        //// 如果规则类型 SKU      range_id就是skuId值
 | 
			
		||||
        //// 如果规则类型 CATEGORY range_id就是分类Id值
 | 
			
		||||
        List<Long> randIdList = couponRangeList.stream()
 | 
			
		||||
                                               .map(CouponRange::getRangeId)
 | 
			
		||||
                                               .collect(Collectors.toList());
 | 
			
		||||
 | 
			
		||||
        Map<String, Object> result = new HashMap<>();
 | 
			
		||||
        //第三步 分别判断封装不同数据
 | 
			
		||||
        if (!CollectionUtils.isEmpty(randIdList)) {
 | 
			
		||||
            if (couponInfo.getRangeType() == CouponRangeType.SKU) {
 | 
			
		||||
                //// 如果规则类型是SKU ,得到skuId,
 | 
			
		||||
                // 远程调用根据多个skuId值获取对应sku信息
 | 
			
		||||
                List<SkuInfo> skuInfoList = productFeignClient.findSkuInfoList(randIdList);
 | 
			
		||||
                result.put("skuInfoList", skuInfoList);
 | 
			
		||||
 | 
			
		||||
            } else if (couponInfo.getRangeType() == CouponRangeType.CATEGORY) {
 | 
			
		||||
                //// 如果规则类型是分类,得到分类Id,远程调用根据多个分类Id值获取对应分类信息
 | 
			
		||||
                List<Category> categoryList = productFeignClient.findCategoryList(randIdList);
 | 
			
		||||
                result.put("categoryList", categoryList);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void saveCouponRule(CouponRuleVo couponRuleVo) {
 | 
			
		||||
        //优惠券couponInfo 与 couponRange 要一起操作:先删除couponRange ,更新couponInfo ,再新增couponRange !
 | 
			
		||||
 | 
			
		||||
        couponRangeMapper.delete(new LambdaQueryWrapper<CouponRange>().eq(CouponRange::getCouponId, couponRuleVo.getCouponId()));
 | 
			
		||||
 | 
			
		||||
        //更新优惠卷基本信息
 | 
			
		||||
        CouponInfo couponInfo = baseMapper.selectById(couponRuleVo.getCouponId());
 | 
			
		||||
        couponInfo.setRangeType(couponRuleVo.getRangeType());
 | 
			
		||||
        couponInfo.setConditionAmount(couponRuleVo.getConditionAmount());
 | 
			
		||||
        couponInfo.setAmount(couponRuleVo.getAmount());
 | 
			
		||||
        couponInfo.setConditionAmount(couponRuleVo.getConditionAmount());
 | 
			
		||||
        couponInfo.setRangeDesc(couponRuleVo.getRangeDesc());
 | 
			
		||||
        baseMapper.updateById(couponInfo);
 | 
			
		||||
 | 
			
		||||
        //添加优惠卷新规则数据
 | 
			
		||||
        List<CouponRange> couponRangeList = couponRuleVo.getCouponRangeList();
 | 
			
		||||
        for (CouponRange couponRange : couponRangeList) {
 | 
			
		||||
            //设置优惠卷id
 | 
			
		||||
            couponRange.setCouponId(couponRuleVo.getCouponId());
 | 
			
		||||
            //添加
 | 
			
		||||
            couponRangeMapper.insert(couponRange);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<CouponInfo> findCouponByKeyword(String keyword) {
 | 
			
		||||
        return baseMapper.selectList(new LambdaQueryWrapper<CouponInfo>().like(CouponInfo::getCouponName, keyword));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,6 @@ spring:
 | 
			
		||||
  cloud:
 | 
			
		||||
    nacos:
 | 
			
		||||
      discovery:
 | 
			
		||||
        server-addr: localhost:8848
 | 
			
		||||
      username: nacos
 | 
			
		||||
      password: nacos
 | 
			
		||||
        server-addr: 82.157.68.223:8848
 | 
			
		||||
        username: nacos
 | 
			
		||||
        password: nacos
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user