Commit 480adf6d by caiyt

新增接口:保存优惠券/编辑优惠券

parent bc8ffd75
......@@ -38,6 +38,19 @@ public class CouponController {
@PutMapping
@ApiOperation("新增优惠券")
public AjaxResult save(@RequestBody CouponPo couponPo) {
couponService.save(couponPo);
return AjaxResult.success();
}
/**
* 更新优惠券
*/
@PreAuthorize("@ss.hasPermi('coupon:update')")
@Log(title = "更新优惠券", businessType = BusinessType.INSERT)
@PutMapping
@ApiOperation("更新优惠券")
public AjaxResult update(@RequestBody CouponPo couponPo) {
couponService.update(couponPo);
return AjaxResult.success();
}
}
package com.soss.system.mapper;
import com.soss.system.domain.Coupon;
import com.soss.system.domain.CouponCategory;
import com.soss.system.domain.CouponRule;
import org.apache.ibatis.annotations.Param;
import java.util.List;
......@@ -15,4 +17,28 @@ import java.util.List;
*/
public interface CouponMapper {
List<CouponCategory> getCouponCategoryTypeStat(@Param("categoryIds") List<Integer> categoryIds);
/**
* 新增优惠券
*
* @param coupon 优惠券
* @return 结果
*/
int insertCoupon(Coupon coupon);
/**
* 更新优惠券
*
* @param coupon 优惠券
* @return 结果
*/
int updateCoupon(Coupon coupon);
/**
* 获取优惠券详情
*
* @param id
* @return
*/
Coupon selectCouponById(Integer id);
}
package com.soss.system.mapper;
import com.soss.system.domain.CouponCategory;
import com.soss.system.domain.CouponRule;
/**
* <p>
* 优惠券规则 Mapper 接口
......@@ -9,5 +12,27 @@ package com.soss.system.mapper;
* @since 2022-07-21
*/
public interface CouponRuleMapper {
/**
* 新增优惠券规则
*
* @param couponRule 优惠券规则
* @return 结果
*/
int insertCouponRule(CouponRule couponRule);
/**
* 更新优惠券规则
*
* @param couponRule 优惠券规则
* @return 结果
*/
int updateCouponRule(CouponRule couponRule);
/**
* 查询优惠券规则详情
*
* @param id
* @return
*/
CouponRule selectCouponRuleById(Integer id);
}
package com.soss.system.mapper;
import org.apache.ibatis.annotations.Param;
/**
* <p>
* 用户领取优惠券记录表 Mapper 接口
......@@ -9,5 +11,11 @@ package com.soss.system.mapper;
* @since 2022-07-21
*/
public interface CouponUserMapper {
/**
* 获取优惠券被领取次数
*
* @param couponId
* @return
*/
int getCouponReceiveCount(@Param("couponId") Integer couponId);
}
package com.soss.system.service;
import com.soss.system.domain.po.CouponPo;
/**
* <p>
* 优惠券 服务类
......@@ -10,4 +12,7 @@ package com.soss.system.service;
*/
public interface ICouponService {
void save(CouponPo couponPo);
void update(CouponPo couponPo);
}
package com.soss.system.service.impl;
import com.soss.common.enums.CouponCategoryType;
import com.soss.common.exception.ServiceException;
import com.soss.common.utils.GenerateCode;
import com.soss.system.domain.Coupon;
import com.soss.system.domain.CouponCategory;
......@@ -8,10 +9,13 @@ import com.soss.system.domain.CouponRule;
import com.soss.system.domain.po.CouponPo;
import com.soss.system.mapper.CouponCategoryMapper;
import com.soss.system.mapper.CouponMapper;
import com.soss.system.mapper.CouponRuleMapper;
import com.soss.system.mapper.CouponUserMapper;
import com.soss.system.service.ICouponService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
......@@ -33,36 +37,69 @@ public class CouponServiceImpl implements ICouponService {
private CouponCategoryMapper couponCategoryMapper;
@Autowired
private CouponMapper couponMapper;
@Autowired
private CouponRuleMapper couponRuleMapper;
@Autowired
private CouponUserMapper couponUserMapper;
@Override
@Transactional
public void save(CouponPo couponPo) {
Coupon coupon = new Coupon();
CouponRule couponRule = new CouponRule();
this.checkAndWrapperCoupon(couponPo, coupon, couponRule, true);
couponRuleMapper.insertCouponRule(couponRule);
coupon.setRuleId(couponRule.getId());
couponMapper.insertCoupon(coupon);
}
@Override
@Transactional
public void update(CouponPo couponPo) {
Assert.notNull(couponPo.getId(), "要修改的优惠券ID还未传递");
Coupon coupon = couponMapper.selectCouponById(couponPo.getId());
Assert.notNull(coupon, "未查询到匹配的优惠券记录[id=" + couponPo.getId() + "]");
int couponReceiveCount = couponUserMapper.getCouponReceiveCount(coupon.getId());
if (couponReceiveCount > 0) {
throw new ServiceException("优惠券已被领取,无法编辑,只可下线");
}
CouponRule couponRule = couponRuleMapper.selectCouponRuleById(coupon.getRuleId());
checkAndWrapperCoupon(couponPo, coupon, couponRule, false);
couponRuleMapper.updateCouponRule(couponRule);
couponMapper.updateCoupon(coupon);
}
private void checkAndWrapperCoupon(CouponPo couponPo, Coupon coupon, CouponRule couponRule, boolean saveFlag) {
Assert.notNull(couponPo.getName(), "优惠券名称还未填写");
Assert.notNull(couponPo.getCategoryId(), "优惠券类别还未选择");
CouponCategory couponCategory = couponCategoryMapper.selectCouponCategoryById(couponPo.getCategoryId());
Assert.notNull(couponCategory, "未查询到匹配的优惠券类别[id=" + couponPo.getCategoryId() + "]");
StringBuilder ruleName = new StringBuilder(16);
String ruleName, ruleDesc; // 规则名称,规则描述
if (CouponCategoryType.DEDUCTION.getType().equals(couponCategory.getType())) {
Assert.notNull(couponPo.getPriceDiscount(), "满减价还未设置");
Assert.notNull(couponPo.getPriceLimit(), "价格门槛还未设置");
Assert.notNull(couponPo.getDaysLimit(), "用户可领取次数还未设置");
ruleName = "立减" + couponPo.getPriceDiscount() + "元";
if (couponPo.getPriceLimit().compareTo(BigDecimal.ZERO) == 0) {
ruleName.append("无门槛");
ruleDesc = "无门槛";
} else {
ruleName.append("满").append(couponPo.getPriceLimit()).append("元");
ruleDesc = "满" + couponPo.getPriceLimit() + "元可用";
}
ruleName.append("立减").append(couponPo.getPriceDiscount()).append("元");
} else if (CouponCategoryType.DISCOUNT.getType().equals(couponCategory.getType())) {
Assert.notNull(couponPo.getPriceDiscount(), "打折数还未设置");
Assert.notNull(couponPo.getOrderLimit(), "订单限制还未设置");
ruleName.append(couponPo.getOrderLimit() ? "单杯" : "整单").append(couponPo.getOrderLimit()).append("折");
ruleName = couponPo.getOrderLimit() + "折";
ruleDesc = couponPo.getOrderLimit() ? "单杯" : "整单";
} else {
Assert.notNull(couponPo.getPriceLimit(), "价格门槛还未设置");
Assert.notNull(couponPo.getOrderLimit(), "订单限制还未设置");
ruleName = "免单";
if (couponPo.getPriceLimit().compareTo(BigDecimal.ZERO) == 0) {
ruleName.append("无门槛");
ruleDesc = "无门槛";
} else {
ruleName.append("满").append(couponPo.getPriceLimit()).append("元");
ruleDesc = "满" + couponPo.getPriceLimit() + "元可用";
}
ruleName.append("免单");
}
Assert.notNull(couponPo.getUseStartTime(), "优惠券的绝对有效时间还未设置");
Assert.notNull(couponPo.getUseEndTime(), "优惠券的绝对有效时间还未设置");
......@@ -71,28 +108,29 @@ public class CouponServiceImpl implements ICouponService {
Assert.notNull(couponPo.getSendMsg(), "是否短信通知还未设置");
Assert.isTrue(couponPo.getSendMsg() && couponPo.getMsgId() != null, "短信模板还未选择");
Coupon coupon = new Coupon();
coupon.setName(couponPo.getName());
coupon.setSerialNo(GenerateCode.getCode("CP", "%06d"));
coupon.setCategoryId(couponCategory.getId());
coupon.setCategoryName(couponCategory.getName());
// coupon.setRuleId(); todo
coupon.setType(couponCategory.getType());
LocalDateTime now = LocalDateTime.now();
coupon.setCreatedAt(now);
if (saveFlag) {
coupon.setCreatedAt(now);
}
coupon.setUpdatedAt(now);
CouponRule couponRule = new CouponRule();
BeanUtils.copyProperties(couponPo, couponRule);
couponRule.setName(ruleName.toString());
couponRule.setDesc(""); // todo
BeanUtils.copyProperties(couponPo, couponRule, "name");
couponRule.setName(ruleName);
couponRule.setDesc(ruleDesc);
couponRule.setCategoryIds(couponPo.getCategoryIds().stream().map(String::valueOf).collect(Collectors.joining(",")));
couponRule.setGoodsIds(couponPo.getGoodsIds().stream().map(String::valueOf).collect(Collectors.joining(",")));
couponRule.setProvince(String.join(",", couponPo.getProvince()));
couponRule.setCity(String.join(",", couponPo.getCity()));
couponRule.setArea(String.join(",", couponPo.getArea()));
couponRule.setShopIds(couponPo.getShopIds().stream().map(String::valueOf).collect(Collectors.joining(",")));
couponRule.setCreatedAt(now);
if (saveFlag) {
couponRule.setCreatedAt(now);
}
couponRule.setUpdatedAt(now);
}
}
......@@ -15,9 +15,55 @@
<result column="updated_at" property="updatedAt"/>
</resultMap>
<select id="selectCouponById" resultMap="BaseResultMap">
select *
from coupon
where id = #{id}
</select>
<select id="getCouponCategoryTypeStat" resultType="com.soss.system.domain.CouponCategory">
select category_id id, count(*) couponCnt from coupon where category_id in
<foreach collection="categoryIds" item="categoryId" open="(" close=")" separator=",">#{categoryId}</foreach>
group by category_id
</select>
<insert id="insertCoupon" parameterType="com.soss.system.domain.Coupon" useGeneratedKeys="true" keyProperty="id">
insert into coupon_category
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null and name != ''">name,</if>
<if test="serialNo != null and serialNo != ''">serial_no,</if>
<if test="categoryId != null">category_id,</if>
<if test="categoryName != null and categoryName != ''">category_name,</if>
<if test="ruleId != null">rule_id,</if>
<if test="type != null">type,</if>
<if test="state != null">state,</if>
<if test="createdAt != null">created_at,</if>
<if test="updatedAt != null">updated_at,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="name != null and name != ''">#{name},</if>
<if test="serialNo != null and serialNo != ''">#{serialNo},</if>
<if test="categoryId != null">#{categoryId},</if>
<if test="categoryName != null and categoryName != ''">#{categoryName},</if>
<if test="ruleId != null">#{ruleId},</if>
<if test="type != null">#{type},</if>
<if test="state != null">#{state},</if>
<if test="createdAt != null">#{createdAt},</if>
<if test="updatedAt != null">#{updatedAt},</if>
</trim>
</insert>
<update id="updateCoupon" parameterType="com.soss.system.domain.Coupon">
update coupon
<trim prefix="SET" suffixOverrides=",">
<if test="name != null and name != ''">name = #{name},</if>
<if test="categoryId != null">category_id = #{categoryId},</if>
<if test="categoryName != null and categoryName != ''">category_name = #{categoryName},</if>
<if test="ruleId != null">rule_id = #{ruleId},</if>
<if test="type != null">type = #{type},</if>
<if test="state != null">state = #{state},</if>
<if test="updatedAt != null">updated_at = #{updatedAt},</if>
</trim>
where id = #{id}
</update>
</mapper>
......@@ -3,31 +3,124 @@
<mapper namespace="com.soss.system.mapper.CouponRuleMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.soss.system.domain.CouponRule">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="desc" property="desc" />
<result column="province" property="province" />
<result column="city" property="city" />
<result column="area" property="area" />
<result column="shop_ids" property="shopIds" />
<result column="category_ids" property="categoryIds" />
<result column="goods_ids" property="goodsIds" />
<result column="start_time" property="startTime" />
<result column="end_time" property="endTime" />
<result column="use_start_time" property="useStartTime" />
<result column="use_end_time" property="useEndTime" />
<result column="relative_time" property="relativeTime" />
<result column="price_discount" property="priceDiscount" />
<result column="price_limit" property="priceLimit" />
<result column="order_limit" property="orderLimit" />
<result column="week_limit" property="weekLimit" />
<result column="user_limit" property="userLimit" />
<result column="days_limit" property="daysLimit" />
<result column="send_msg" property="sendMsg" />
<result column="msg_id" property="msgId" />
<result column="state" property="state" />
<result column="created_at" property="createdAt" />
<result column="updated_at" property="updatedAt" />
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="desc" property="desc"/>
<result column="category_ids" property="categoryIds"/>
<result column="goods_ids" property="goodsIds"/>
<result column="province" property="province"/>
<result column="city" property="city"/>
<result column="area" property="area"/>
<result column="shop_ids" property="shopIds"/>
<result column="start_time" property="startTime"/>
<result column="end_time" property="endTime"/>
<result column="use_start_time" property="useStartTime"/>
<result column="use_end_time" property="useEndTime"/>
<result column="relative_time" property="relativeTime"/>
<result column="price_discount" property="priceDiscount"/>
<result column="price_limit" property="priceLimit"/>
<result column="order_limit" property="orderLimit"/>
<result column="week_limit" property="weekLimit"/>
<result column="user_limit" property="userLimit"/>
<result column="days_limit" property="daysLimit"/>
<result column="send_msg" property="sendMsg"/>
<result column="msg_id" property="msgId"/>
<result column="state" property="state"/>
<result column="created_at" property="createdAt"/>
<result column="updated_at" property="updatedAt"/>
</resultMap>
<select id="selectCouponRuleById" resultMap="BaseResultMap">
select *
from coupon_rule
where id = #{id}
</select>
<insert id="insertCouponRule" parameterType="com.soss.system.domain.CouponRule" useGeneratedKeys="true"
keyProperty="id">
insert into coupon_category
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null and name != ''">name,</if>
<if test="desc != null and desc != ''">`desc`,</if>
<if test="categoryIds != null and categoryIds != ''">category_ids,</if>
<if test="goodsIds != null and goodsIds != ''">goods_ids,</if>
<if test="province != null and province != ''">province,</if>
<if test="city != null and city != ''">city,</if>
<if test="area != null and area != ''">area,</if>
<if test="shopIds != null and shopIds != ''">shop_ids,</if>
<if test="startTime != null">start_time,</if>
<if test="endTime != null">end_time,</if>
<if test="useStartTime != null">use_start_time,</if>
<if test="useEndTime != null">use_end_time,</if>
<if test="relativeTime != null">relative_time,</if>
<if test="priceDiscount != null">price_discount,</if>
<if test="priceLimit != null">price_limit,</if>
<if test="orderLimit != null">order_limit,</if>
<if test="weekLimit != null">week_limit,</if>
<if test="userLimit != null">user_limit,</if>
<if test="daysLimit != null">days_limit,</if>
<if test="sendMsg != null">send_msg,</if>
<if test="msgId != null">msg_id,</if>
<if test="state != null">state,</if>
<if test="createdAt != null">created_at,</if>
<if test="updatedAt != null">updated_at,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="name != null and name != ''">#{name},</if>
<if test="desc != null and desc != ''">#{desc},</if>
<if test="categoryIds != null and categoryIds != ''">#{categoryIds},</if>
<if test="goodsIds != null and goodsIds != ''">#{goodsIds},</if>
<if test="province != null and province != ''">#{province},</if>
<if test="city != null and city != ''">#{city},</if>
<if test="area != null and area != ''">#{area},</if>
<if test="shopIds != null and shopIds != ''">#{shopIds},</if>
<if test="startTime != null">#{startTime},</if>
<if test="endTime != null">#{endTime},</if>
<if test="useStartTime != null">#{useStartTime},</if>
<if test="useEndTime != null">#{useEndTime},</if>
<if test="relativeTime != null">#{relativeTime},</if>
<if test="priceDiscount != null">#{priceDiscount},</if>
<if test="priceLimit != null">#{priceLimit},</if>
<if test="orderLimit != null">#{orderLimit},</if>
<if test="weekLimit != null">#{weekLimit},</if>
<if test="userLimit != null">#{userLimit},</if>
<if test="daysLimit != null">#{daysLimit},</if>
<if test="sendMsg != null">#{sendMsg},</if>
<if test="msgId != null">#{msgId},</if>
<if test="state != null">#{state},</if>
<if test="createdAt != null">#{createdAt},</if>
<if test="updatedAt != null">#{updatedAt},</if>
</trim>
</insert>
<update id="updateCouponRule" parameterType="com.soss.system.domain.CouponRule">
update coupon_category
<trim prefix="set" suffixOverrides=",">
<if test="name != null and name != ''">name = #{name},</if>
<if test="desc != null and desc != ''">`desc` = #{desc},</if>
<if test="categoryIds != null and categoryIds != ''">category_ids = #{categoryIds},</if>
<if test="goodsIds != null and goodsIds != ''">goods_ids = #{goodsIds},</if>
<if test="province != null and province != ''">province = #{province},</if>
<if test="city != null and city != ''">city = #{city},</if>
<if test="area != null and area != ''">area = #{area},</if>
<if test="shopIds != null and shopIds != ''">shop_ids = #{shopIds},</if>
<if test="startTime != null">start_time = #{startTime},</if>
<if test="endTime != null">end_time = #{endTime},</if>
<if test="useStartTime != null">use_start_time = #{useStartTime},</if>
<if test="useEndTime != null">use_end_time = #{useEndTime},</if>
<if test="relativeTime != null">relative_time = #{relativeTime},</if>
<if test="priceDiscount != null">price_discount = #{priceDiscount},</if>
<if test="priceLimit != null">price_limit = #{priceLimit},</if>
<if test="orderLimit != null">order_limit = #{orderLimit},</if>
<if test="weekLimit != null">week_limit = #{weekLimit},</if>
<if test="userLimit != null">user_limit = #{userLimit},</if>
<if test="daysLimit != null">days_limit = #{daysLimit},</if>
<if test="sendMsg != null">send_msg = #{sendMsg},</if>
<if test="msgId != null">msg_id = #{msgId},</if>
<if test="state != null">state = #{state},</if>
<if test="createdAt != null">created_at = #{createdAt},</if>
<if test="updatedAt != null">updated_at = #{updatedAt},</if>
</trim>
where id = #{id}
</update>
</mapper>
......@@ -3,21 +3,26 @@
<mapper namespace="com.soss.system.mapper.CouponUserMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.soss.system.domain.CouponUser">
<id column="id" property="id" />
<result column="user_id" property="userId" />
<result column="user_name" property="userName" />
<result column="user_phone" property="userPhone" />
<result column="order_id" property="orderId" />
<result column="coupon_id" property="couponId" />
<result column="receive_time" property="receiveTime" />
<result column="active_time" property="activeTime" />
<result column="expired_time" property="expiredTime" />
<result column="discount" property="discount" />
<result column="source" property="source" />
<result column="type" property="type" />
<result column="state" property="state" />
<result column="created_at" property="createdAt" />
<result column="updated_at" property="updatedAt" />
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="user_name" property="userName"/>
<result column="user_phone" property="userPhone"/>
<result column="order_id" property="orderId"/>
<result column="coupon_id" property="couponId"/>
<result column="receive_time" property="receiveTime"/>
<result column="active_time" property="activeTime"/>
<result column="expired_time" property="expiredTime"/>
<result column="discount" property="discount"/>
<result column="source" property="source"/>
<result column="type" property="type"/>
<result column="state" property="state"/>
<result column="created_at" property="createdAt"/>
<result column="updated_at" property="updatedAt"/>
</resultMap>
<select id="getCouponReceiveCount">
select count(*)
from coupon_user
where coupon_id = #{couponId}
</select>
</mapper>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment