Commit cb214744 by caiyt

修复优化优惠券联调过程中遇到的问题

parent 480adf6d
......@@ -44,11 +44,23 @@ public class CouponCategoryController extends BaseController {
}
/**
* 更新优惠券类别
*/
@PreAuthorize("@ss.hasPermi('coupon:category:update')")
@Log(title = "更新优惠券类别", businessType = BusinessType.INSERT)
@PostMapping("update")
@ApiOperation("更新优惠券类别")
public AjaxResult update(@RequestBody CouponCategory couponCategory) {
couponCategoryService.updateCouponCategory(couponCategory);
return AjaxResult.success();
}
/**
* 查询优惠券类别列表
*/
@PreAuthorize("@ss.hasPermi('coupon:category:list')")
@Log(title = "查询优惠券类别列表", businessType = BusinessType.INSERT)
@GetMapping
@PostMapping
@ApiOperation("查询优惠券类别列表")
public TableDataInfo listCouponCategory(@RequestBody CouponCategory couponCategory) {
startPage();
......
......@@ -2,18 +2,33 @@ package com.soss.web.controller.coupon;
import com.soss.common.annotation.Log;
import com.soss.common.core.controller.BaseController;
import com.soss.common.core.domain.AjaxResult;
import com.soss.common.core.page.TableDataInfo;
import com.soss.common.enums.BusinessType;
import com.soss.common.enums.CouponCategoryType;
import com.soss.common.enums.CouponState;
import com.soss.common.utils.StringUtils;
import com.soss.common.utils.bean.BeanUtils;
import com.soss.system.domain.Coupon;
import com.soss.system.domain.CouponRule;
import com.soss.system.domain.po.CouponListPo;
import com.soss.system.domain.po.CouponPo;
import com.soss.system.domain.vo.CouponVo;
import com.soss.system.service.ICouponRuleService;
import com.soss.system.service.ICouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* <p>
......@@ -26,9 +41,80 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@Api(tags = "优惠券服务类")
@RequestMapping("/coupon")
public class CouponController {
public class CouponController extends BaseController {
@Autowired
private ICouponService couponService;
@Autowired
private ICouponRuleService couponRuleService;
/**
* 查询优惠券列表
*/
@PreAuthorize("@ss.hasPermi('coupon:list')")
@Log(title = "查询优惠券列表", businessType = BusinessType.INSERT)
@PostMapping("list")
@ApiOperation("查询优惠券列表")
public TableDataInfo listCoupon(@RequestBody CouponListPo couponListPo) {
startPage();
Coupon couponParam = new Coupon();
BeanUtils.copyProperties(couponListPo, couponParam);
List<Coupon> coupons = couponService.list(couponParam);
Map<Integer, CouponRule> ruleMap;
if (CollectionUtils.isEmpty(coupons)) {
ruleMap = new HashMap<>();
} else {
List<Integer> ruleIds = coupons.stream().map(Coupon::getRuleId).collect(Collectors.toList());
ruleMap = couponRuleService.getCouponRuleMap(ruleIds);
}
List<CouponVo> voList = coupons.stream().map(coupon -> {
CouponVo couponVo = new CouponVo();
BeanUtils.copyProperties(coupon, couponVo);
couponVo.setStateDesc(CouponState.getDesc(coupon.getState()));
couponVo.setTypeDesc(CouponCategoryType.getDesc(coupon.getType()));
CouponRule couponRule = ruleMap.get(coupon.getRuleId());
if (couponRule != null) {
BeanUtils.copyProperties(couponRule, couponVo, "name", "state", "categoryIds", "goodsIds", "province", "city", "area", "shopIds");
couponVo.setRuleId(coupon.getRuleId());
couponVo.setRuleName(couponRule.getName());
couponVo.setRuleDesc(couponRule.getDesc());
}
return couponVo;
}).collect(Collectors.toList());
return getDataTable(voList);
}
/**
* 获取优惠券详情
*/
@PreAuthorize("@ss.hasPermi('coupon:detail')")
@Log(title = "获取优惠券详情", businessType = BusinessType.INSERT)
@GetMapping("/detail/{id}")
@ApiOperation("获取优惠券详情")
public AjaxResult detail(@PathVariable Integer id) {
Coupon coupon = couponService.detail(id);
CouponVo couponVo = new CouponVo();
BeanUtils.copyProperties(coupon, couponVo);
CouponRule couponRule = couponRuleService.detail(coupon.getRuleId());
BeanUtils.copyProperties(couponRule, couponVo, "name", "state", "categoryIds", "goodsIds", "province", "city", "area", "shopIds");
couponVo.setCategoryIds(transStrToIntList(couponRule.getCategoryIds()));
couponVo.setGoodsIds(transStrToIntList(couponRule.getGoodsIds()));
couponVo.setProvince(transStrToCodeList(couponRule.getProvince()));
couponVo.setCity(transStrToCodeList(couponRule.getCity()));
couponVo.setArea(transStrToCodeList(couponRule.getArea()));
couponVo.setShopIds(transStrToIntList(couponRule.getShopIds()));
return AjaxResult.success(couponVo);
}
private String[] transStrToCodeList(String codeStr) {
return StringUtils.isEmpty(codeStr) ? null : codeStr.split(",");
}
private Integer[] transStrToIntList(String idsStr) {
if (StringUtils.isEmpty(idsStr)) {
return null;
}
return Arrays.stream(idsStr.split(",")).map(Integer::parseInt).toArray(Integer[]::new);
}
/**
* 新增优惠券
......@@ -47,10 +133,59 @@ public class CouponController {
*/
@PreAuthorize("@ss.hasPermi('coupon:update')")
@Log(title = "更新优惠券", businessType = BusinessType.INSERT)
@PutMapping
@PostMapping
@ApiOperation("更新优惠券")
public AjaxResult update(@RequestBody CouponPo couponPo) {
couponService.update(couponPo);
return AjaxResult.success();
}
/**
* 上架优惠券
*/
@PreAuthorize("@ss.hasPermi('coupon:online')")
@Log(title = "上架优惠券", businessType = BusinessType.INSERT)
@GetMapping("/online/{id}")
@ApiOperation("上架优惠券")
public AjaxResult onlineCoupon(@PathVariable Integer id) {
int updateCnt = couponService.onlineCoupon(id);
return toAjax(updateCnt);
}
/**
* 下架优惠券
*/
@PreAuthorize("@ss.hasPermi('coupon:offline')")
@Log(title = "下架优惠券", businessType = BusinessType.INSERT)
@GetMapping("/offline/{id}")
@ApiOperation("下架优惠券")
public AjaxResult offlineCoupon(@PathVariable Integer id) {
int updateCnt = couponService.offlineCoupon(id);
return toAjax(updateCnt);
}
/**
* 删除优惠券
*/
@PreAuthorize("@ss.hasPermi('coupon:delete')")
@Log(title = "删除优惠券", businessType = BusinessType.INSERT)
@DeleteMapping("/{id}")
@ApiOperation("删除优惠券")
public AjaxResult deleteCoupon(@PathVariable Integer id) {
int updateCnt = couponService.deleteCoupon(id);
return toAjax(updateCnt);
}
/**
* 获取当前有效的规则集合
*/
@PreAuthorize("@ss.hasPermi('coupon:list:effective')")
@Log(title = "获取当前有效的规则集合", businessType = BusinessType.INSERT)
@DeleteMapping("/list/effective")
@ApiOperation("获取当前有效的规则集合")
public TableDataInfo listEffective() {
startPage();
List<Coupon> effectiveCoupons = couponService.getEffectiveCoupon();
return getDataTable(effectiveCoupons);
}
}
......@@ -2,9 +2,9 @@ package com.soss.common.enums;
public enum CouponState {
DEFAULT(0, "默认"),
ONLINE(1, "上架"),
OFFLINE(2, "下架"),
DELETE(3, "删除");
ONLINE(1, "生效中"),
OFFLINE(2, "未生效"),
DELETE(3, "删除");
private Integer state;
private String desc;
......
......@@ -3,6 +3,7 @@ package com.soss.system.domain;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.soss.common.core.domain.BaseEntity;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
......@@ -15,6 +16,7 @@ import java.time.LocalDateTime;
* @since 2022-07-21
*/
@Data
@NoArgsConstructor
public class Coupon extends BaseEntity {
/**
* 主键
......@@ -67,4 +69,9 @@ public class Coupon extends BaseEntity {
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updatedAt;
public Coupon(Integer id, Integer state) {
this.id = id;
this.state = state;
}
}
......@@ -32,6 +32,7 @@ public class CouponCategory extends BaseEntity {
* 状态 0 默认 1 生效 2 下线 3删除
*/
private Integer state;
private String stateDesc;
/**
* 优惠券类别描述
......
......@@ -104,7 +104,7 @@ public class CouponRule extends BaseEntity {
/**
* 订单限制 0 整单 1 单杯
*/
private Integer orderLimit;
private Boolean orderLimit;
/**
* 周几限制 1,2,3
......@@ -124,7 +124,7 @@ public class CouponRule extends BaseEntity {
/**
* 是否发送短信0不1发
*/
private Integer sendMsg;
private Boolean sendMsg;
/**
* 短信模板ID
......
package com.soss.system.domain.po;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel("优惠券列表请求类")
public class CouponListPo {
@ApiModelProperty("优惠劵名")
private String name;
@ApiModelProperty("优惠券类别ID")
private Integer categoryId;
@ApiModelProperty("优惠券状态")
private Integer state;
}
......@@ -5,7 +5,6 @@ import com.soss.common.exception.ServiceException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.time.LocalDateTime;
......@@ -20,41 +19,26 @@ public class CouponPo {
@ApiModelProperty("优惠劵名")
private String name;
@ApiModelProperty("优惠券编码")
private String serialNo;
@ApiModelProperty("优惠券类别ID")
private Integer categoryId;
@ApiModelProperty("优惠券类别名")
private String categoryName;
@ApiModelProperty("优惠券规则id")
private Integer ruleId;
@ApiModelProperty("优惠券类型 1 抵扣 2 折扣 3 免单")
private Integer type;
@ApiModelProperty("优惠劵规则描述")
private String desc;
@ApiModelProperty("可用商品类别列表")
private List<Integer> categoryIds;
@ApiModelProperty("可用商品类别列表,全品类传 [0]")
private Integer[] categoryIds;
@ApiModelProperty("可用商品id列表")
private List<Integer> goodsIds;
private Integer[] goodsIds;
@ApiModelProperty("可用省份列表 可存code列表")
private List<String> province;
@ApiModelProperty("可用省份列表,全国通用传 [0]")
private String[] province;
@ApiModelProperty("可用城市列表")
private List<String> city;
private String[] city;
@ApiModelProperty("可用区域列表")
private List<String> area;
private String[] area;
@ApiModelProperty("可用店铺ID列表")
private List<Integer> shopIds;
private Integer[] shopIds;
@ApiModelProperty("领取开始时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
......@@ -101,16 +85,16 @@ public class CouponPo {
public void couponUseLimitCheck() {
int limitDim = 0;
if (!CollectionUtils.isEmpty(province)) {
if (province != null && province.length > 0) {
limitDim += 1;
}
if (!CollectionUtils.isEmpty(city)) {
if (city != null && city.length > 0) {
limitDim += 1;
}
if (!CollectionUtils.isEmpty(area)) {
if (area != null && area.length > 0) {
limitDim += 1;
}
if (!CollectionUtils.isEmpty(shopIds)) {
if (shopIds != null && shopIds.length > 0) {
limitDim += 1;
}
if (limitDim > 1) {
......
......@@ -8,6 +8,49 @@ import lombok.Data;
@ApiModel
@Data
public class CouponVo extends CouponPo {
@ApiModelProperty("优惠券规则id")
private Integer ruleId;
@ApiModelProperty("优惠劵规则名称")
private String ruleName;
@ApiModelProperty("优惠劵规则描述")
private String ruleDesc;
@ApiModelProperty("优惠券编码")
private String serialNo;
@ApiModelProperty("优惠券类型 1 抵扣 2 折扣 3 免单")
private Integer type;
@ApiModelProperty("优惠券类型描述")
private String typeDesc;
@ApiModelProperty("优惠券状态")
private Integer state;
@ApiModelProperty("优惠券状态描述")
private String stateDesc;
@ApiModelProperty("优惠券类别名")
private String categoryName;
@ApiModelProperty("可用商品类别描述")
private String categoryDesc;
@ApiModelProperty("可用商品描述")
private String goodsDesc;
@ApiModelProperty("可用省份描述")
private String provinceDesc;
@ApiModelProperty("可用城市描述")
private String cityDesc;
@ApiModelProperty("可用区域描述")
private String areaDesc;
@ApiModelProperty("可用店铺描述")
private String shopDesc;
}
......@@ -2,7 +2,6 @@ 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;
......@@ -16,6 +15,14 @@ import java.util.List;
* @since 2022-07-21
*/
public interface CouponMapper {
/**
* 查询优惠券列表
*
* @param coupon
* @return
*/
List<Coupon> listCoupon(Coupon coupon);
List<CouponCategory> getCouponCategoryTypeStat(@Param("categoryIds") List<Integer> categoryIds);
/**
......@@ -41,4 +48,11 @@ public interface CouponMapper {
* @return
*/
Coupon selectCouponById(Integer id);
/**
* 获取当前有效的规则集合
*
* @return
*/
List<Coupon> getEffectiveCoupon();
}
package com.soss.system.mapper;
import com.soss.system.domain.CouponCategory;
import com.soss.system.domain.CouponRule;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* <p>
......@@ -35,4 +37,12 @@ public interface CouponRuleMapper {
* @return
*/
CouponRule selectCouponRuleById(Integer id);
/**
* 查询优惠券规则列表
*
* @param ids
* @return
*/
List<CouponRule> selectCouponRuleByIds(@Param("ids") List<Integer> ids);
}
......@@ -16,6 +16,8 @@ public interface ICouponCategoryService {
void insertCouponCategory(CouponCategory couponCategory);
void updateCouponCategory(CouponCategory couponCategory);
List<CouponCategory> listCouponCategory(CouponCategory couponCategory);
int onlineCouponCategory(Integer id);
......
package com.soss.system.service;
import com.soss.system.domain.CouponRule;
import java.util.List;
import java.util.Map;
/**
* <p>
* 优惠券规则 服务类
......@@ -10,4 +15,7 @@ package com.soss.system.service;
*/
public interface ICouponRuleService {
Map<Integer, CouponRule> getCouponRuleMap(List<Integer> ids);
CouponRule detail(Integer id);
}
package com.soss.system.service;
import com.soss.system.domain.Coupon;
import com.soss.system.domain.po.CouponPo;
import java.util.List;
/**
* <p>
* 优惠券 服务类
......@@ -12,7 +15,19 @@ import com.soss.system.domain.po.CouponPo;
*/
public interface ICouponService {
List<Coupon> list(Coupon coupon);
Coupon detail(Integer id);
void save(CouponPo couponPo);
void update(CouponPo couponPo);
int onlineCoupon(Integer id);
int offlineCoupon(Integer id);
int deleteCoupon(Integer id);
List<Coupon> getEffectiveCoupon();
}
......@@ -2,16 +2,21 @@ package com.soss.system.service.impl;
import com.soss.common.enums.CouponCategoryType;
import com.soss.common.enums.CouponState;
import com.soss.common.exception.ServiceException;
import com.soss.system.domain.Coupon;
import com.soss.system.domain.CouponCategory;
import com.soss.system.mapper.CouponCategoryMapper;
import com.soss.system.mapper.CouponMapper;
import com.soss.system.service.ICouponCategoryService;
import io.jsonwebtoken.lang.Assert;
import org.apache.http.util.Asserts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
......@@ -43,12 +48,34 @@ public class CouponCategoryServiceImpl implements ICouponCategoryService {
Assert.notNull(couponCategory.getName(), "优惠券类别名称还未填写");
Assert.notNull(couponCategory.getType(), "优惠券类别的折扣形式还未选择");
LocalDateTime now = LocalDateTime.now();
couponCategory.setState(CouponState.ONLINE.getState());
couponCategory.setCreatedAt(now);
couponCategory.setUpdatedAt(now);
couponCategoryMapper.insertCouponCategory(couponCategory);
}
/**
* 修改优惠券类别
*
* @param couponCategory 优惠券类别
* @return 结果
*/
@Override
@Transactional
public void updateCouponCategory(CouponCategory couponCategory) {
Asserts.notNull(couponCategory.getId(), "优惠券类别ID还未传递");
CouponCategory couponCategoryOri = this.detail(couponCategory.getId());
if (!couponCategoryOri.getState().equals(CouponState.OFFLINE.getState())) {
throw new ServiceException("修改类别需先下架类别");
}
Assert.notNull(couponCategory.getName(), "优惠券类别名称还未填写");
Assert.notNull(couponCategory.getType(), "优惠券类别的折扣形式还未选择");
LocalDateTime now = LocalDateTime.now();
couponCategory.setUpdatedAt(now);
couponCategoryMapper.updateCouponCategory(couponCategory);
}
/**
* 查询优惠券类别列表
*
* @param couponCategory 优惠券类别
......@@ -57,14 +84,19 @@ public class CouponCategoryServiceImpl implements ICouponCategoryService {
@Override
public List<CouponCategory> listCouponCategory(CouponCategory couponCategory) {
List<CouponCategory> couponCategories = couponCategoryMapper.listCouponCategory(couponCategory);
Map<Integer, Long> countMap;
if (CollectionUtils.isEmpty(couponCategories)) {
List<Integer> categoryIds = couponCategories.stream().map(CouponCategory::getId).collect(Collectors.toList());
List<CouponCategory> couponCategoryCountList = couponMapper.getCouponCategoryTypeStat(categoryIds);
Map<Integer, Long> countMap = couponCategoryCountList.stream()
.collect(Collectors.toMap(CouponCategory::getId, CouponCategory::getCouponCnt));
countMap = couponCategoryCountList.stream().collect(Collectors.toMap(CouponCategory::getId, CouponCategory::getCouponCnt));
} else {
countMap = new HashMap<>();
}
for (CouponCategory category : couponCategories) {
category.setTypeDesc(CouponCategoryType.getDesc(category.getType()));
Long count = countMap.get(category.getId());
category.setCouponCnt(count == null ? 0L : count);
category.setStateDesc(CouponState.getDesc(category.getState()));
}
return couponCategories;
}
......@@ -94,7 +126,13 @@ public class CouponCategoryServiceImpl implements ICouponCategoryService {
public int offlineCouponCategory(Integer id) {
CouponCategory online = new CouponCategory(id, CouponState.OFFLINE.getState());
int updateCnt = couponCategoryMapper.updateCouponCategory(online);
// offline logic todo
Coupon coupon = new Coupon();
List<Coupon> coupons = couponMapper.listCoupon(coupon);
long unOfflineCnt = coupons.stream().filter(c -> c.getState().equals(CouponState.DEFAULT.getState())
|| c.getState().equals(CouponState.ONLINE.getState())).count();
if (unOfflineCnt > 0) {
throw new ServiceException("需类下优惠券都处于下线状态才可下线");
}
return updateCnt;
}
......@@ -107,9 +145,24 @@ public class CouponCategoryServiceImpl implements ICouponCategoryService {
@Override
@Transactional
public int deleteCouponCategory(Integer id) {
CouponCategory couponCategory = this.detail(id);
if (!couponCategory.getState().equals(CouponState.OFFLINE.getState())) {
throw new ServiceException("需未生效状态才可删除");
}
CouponCategory online = new CouponCategory(id, CouponState.DELETE.getState());
int updateCnt = couponCategoryMapper.updateCouponCategory(online);
// delete logic todo
return updateCnt;
}
/**
* 获取优惠券类别详情
*
* @param id
* @return
*/
private CouponCategory detail(Integer id) {
CouponCategory couponCategory = couponCategoryMapper.selectCouponCategoryById(id);
Asserts.notNull(couponCategory, "未查询到匹配记录[id=" + couponCategory + "]");
return couponCategory;
}
}
package com.soss.system.service.impl;
import com.soss.system.domain.CouponRule;
import com.soss.system.mapper.CouponRuleMapper;
import com.soss.system.service.ICouponRuleService;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* <p>
* 优惠券规则 服务实现类
......@@ -13,5 +22,25 @@ import org.springframework.stereotype.Service;
*/
@Service
public class CouponRuleServiceImpl implements ICouponRuleService {
@Autowired
private CouponRuleMapper couponRuleMapper;
@Override
public Map<Integer, CouponRule> getCouponRuleMap(List<Integer> ids) {
List<CouponRule> couponRules = couponRuleMapper.selectCouponRuleByIds(ids);
return couponRules.stream().collect(Collectors.toMap(CouponRule::getId, Function.identity()));
}
/**
* 查询优惠券规则详情
*
* @param id
* @return
*/
@Override
public CouponRule detail(Integer id) {
CouponRule couponRule = couponRuleMapper.selectCouponRuleById(id);
Assert.notNull(couponRule, "未查询到匹配的优惠券规则记录[id=" + id + "]");
return couponRule;
}
}
package com.soss.system.service.impl;
import com.soss.common.enums.CouponCategoryType;
import com.soss.common.enums.CouponState;
import com.soss.common.exception.ServiceException;
import com.soss.common.utils.GenerateCode;
import com.soss.system.domain.Coupon;
......@@ -17,10 +18,11 @@ 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;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
......@@ -42,6 +44,24 @@ public class CouponServiceImpl implements ICouponService {
@Autowired
private CouponUserMapper couponUserMapper;
/**
* 查询优惠券列表
*
* @param coupon
* @return
*/
@Override
public List<Coupon> list(Coupon coupon) {
return couponMapper.listCoupon(coupon);
}
@Override
public Coupon detail(Integer id) {
Coupon coupon = couponMapper.selectCouponById(id);
Assert.notNull(coupon, "未查询到匹配的优惠券记录[id=" + id + "]");
return coupon;
}
@Override
@Transactional
public void save(CouponPo couponPo) {
......@@ -49,8 +69,10 @@ public class CouponServiceImpl implements ICouponService {
CouponRule couponRule = new CouponRule();
this.checkAndWrapperCoupon(couponPo, coupon, couponRule, true);
couponRule.setState(CouponState.ONLINE.getState());
couponRuleMapper.insertCouponRule(couponRule);
coupon.setRuleId(couponRule.getId());
coupon.setState(CouponState.ONLINE.getState());
couponMapper.insertCoupon(coupon);
}
......@@ -58,11 +80,12 @@ public class CouponServiceImpl implements ICouponService {
@Transactional
public void update(CouponPo couponPo) {
Assert.notNull(couponPo.getId(), "要修改的优惠券ID还未传递");
Coupon coupon = couponMapper.selectCouponById(couponPo.getId());
Assert.notNull(coupon, "未查询到匹配的优惠券记录[id=" + couponPo.getId() + "]");
Coupon coupon = this.detail(couponPo.getId());
int couponReceiveCount = couponUserMapper.getCouponReceiveCount(coupon.getId());
if (couponReceiveCount > 0) {
throw new ServiceException("优惠券已被领取,无法编辑,只可下线");
} else if (!coupon.getState().equals(CouponState.OFFLINE.getState())) {
throw new ServiceException("修改优惠券需先下架优惠券");
}
CouponRule couponRule = couponRuleMapper.selectCouponRuleById(coupon.getRuleId());
checkAndWrapperCoupon(couponPo, coupon, couponRule, false);
......@@ -84,7 +107,7 @@ public class CouponServiceImpl implements ICouponService {
if (couponPo.getPriceLimit().compareTo(BigDecimal.ZERO) == 0) {
ruleDesc = "无门槛";
} else {
ruleDesc = "满" + couponPo.getPriceLimit() + "元可用";
ruleDesc = "满" + couponPo.getPriceLimit() + "元";
}
} else if (CouponCategoryType.DISCOUNT.getType().equals(couponCategory.getType())) {
Assert.notNull(couponPo.getPriceDiscount(), "打折数还未设置");
......@@ -98,15 +121,16 @@ public class CouponServiceImpl implements ICouponService {
if (couponPo.getPriceLimit().compareTo(BigDecimal.ZERO) == 0) {
ruleDesc = "无门槛";
} else {
ruleDesc = "满" + couponPo.getPriceLimit() + "元可用";
ruleDesc = "满" + couponPo.getPriceLimit() + "元";
}
}
Assert.notNull(couponPo.getUseStartTime(), "优惠券的绝对有效时间还未设置");
Assert.notNull(couponPo.getUseEndTime(), "优惠券的绝对有效时间还未设置");
Assert.isTrue(!CollectionUtils.isEmpty(couponPo.getCategoryIds())
&& !CollectionUtils.isEmpty(couponPo.getGoodsIds()), "商品范围还未设置");
Assert.isTrue((couponPo.getCategoryIds() != null && couponPo.getCategoryIds().length > 0)
|| (couponPo.getGoodsIds() != null && couponPo.getGoodsIds().length > 0), "商品范围还未设置");
couponPo.couponUseLimitCheck();
Assert.notNull(couponPo.getSendMsg(), "是否短信通知还未设置");
Assert.isTrue(couponPo.getSendMsg() && couponPo.getMsgId() != null, "短信模板还未选择");
Assert.isTrue(!couponPo.getSendMsg() || couponPo.getMsgId() != null, "短信模板还未选择");
coupon.setName(couponPo.getName());
coupon.setSerialNo(GenerateCode.getCode("CP", "%06d"));
......@@ -122,15 +146,73 @@ public class CouponServiceImpl implements ICouponService {
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.setCategoryIds(Arrays.stream(couponPo.getCategoryIds()).map(String::valueOf).collect(Collectors.joining(",")));
couponRule.setGoodsIds(Arrays.stream(couponPo.getGoodsIds()).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.setShopIds(Arrays.stream(couponPo.getShopIds()).map(String::valueOf).collect(Collectors.joining(",")));
if (saveFlag) {
couponRule.setCreatedAt(now);
}
couponRule.setUpdatedAt(now);
}
/**
* 上架优惠券
*
* @param id
* @return
*/
@Override
@Transactional
public int onlineCoupon(Integer id) {
Coupon online = new Coupon(id, CouponState.ONLINE.getState());
int updateCnt = couponMapper.updateCoupon(online);
return updateCnt;
}
/**
* 下架优惠券
*
* @param id
* @return
*/
@Override
@Transactional
public int offlineCoupon(Integer id) {
Coupon offline = new Coupon(id, CouponState.OFFLINE.getState());
int updateCnt = couponMapper.updateCoupon(offline);
return updateCnt;
}
/**
* 删除优惠券
*
* @param id
* @return
*/
@Override
@Transactional
public int deleteCoupon(Integer id) {
Coupon coupon = this.detail(id);
if (!coupon.getState().equals(CouponState.OFFLINE.getState())) {
throw new ServiceException("需下线优惠券才能删除");
}
Coupon delete = new Coupon(id, CouponState.DELETE.getState());
int updateCnt = couponMapper.updateCoupon(delete);
return updateCnt;
}
/**
* 获取当前有效的规则集合
*
* @return
*/
@Override
public List<Coupon> getEffectiveCoupon() {
return couponMapper.getEffectiveCoupon();
}
}
......@@ -18,16 +18,24 @@
<select id="selectCouponCategoryById" resultMap="BaseResultMap">
<include refid="selectCouponCategory"/>
where id = #{id} and state = ${@com.soss.common.enums.CouponState @ONLINE.getState}
where id = #{id}
</select>
<select id="listCouponCategory" parameterType="com.soss.system.domain.CouponCategory" resultMap="BaseResultMap">
<include refid="selectCouponCategory"/>
<where>
<if test="name != null and name != ''">and name like concat('%', #{name}, '%')</if>
<if test="type != null"> and type = #{type}</if>
<if test="state != null and state != ''"> and state = #{state}</if>
<choose>
<when test="state != null">
and state = #{state}
</when>
<otherwise>
and state &lt; ${@com.soss.common.enums.CouponState @DELETE.getState}
</otherwise>
</choose>
</where>
order by updated_at desc, id desc
order by id desc
</select>
<insert id="insertCouponCategory" parameterType="com.soss.system.domain.CouponCategory" useGeneratedKeys="true" keyProperty="id">
......
......@@ -21,6 +21,16 @@
where id = #{id}
</select>
<select id="listCoupon" parameterType="com.soss.system.domain.Coupon" resultMap="BaseResultMap">
select * from coupon
<where>
<if test="name != null and name != ''">and name like concat('%', #{name}, '%')</if>
<if test="categoryId != null">and category_id = #{categoryId}</if>
<if test="state != null">and state = #{state}</if>
</where>
order by updated_at desc, id desc
</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>
......@@ -28,7 +38,7 @@
</select>
<insert id="insertCoupon" parameterType="com.soss.system.domain.Coupon" useGeneratedKeys="true" keyProperty="id">
insert into coupon_category
insert into coupon
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null and name != ''">name,</if>
<if test="serialNo != null and serialNo != ''">serial_no,</if>
......@@ -66,4 +76,12 @@
</trim>
where id = #{id}
</update>
<!-- 获取当前有效的规则集合 -->
<select id="getEffectiveCoupon" resultMap="BaseResultMap">
select c.id, c.name
from coupon_rule cr
left join coupon c on c.rule_id = cr.id
where cr.use_start_time > now() and cr.use_end_time &lt; now()
</select>
</mapper>
......@@ -36,9 +36,16 @@
where id = #{id}
</select>
<select id="selectCouponRuleByIds" resultMap="BaseResultMap">
select *
from coupon_rule
where id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">#{id}</foreach>
</select>
<insert id="insertCouponRule" parameterType="com.soss.system.domain.CouponRule" useGeneratedKeys="true"
keyProperty="id">
insert into coupon_category
insert into coupon_rule
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null and name != ''">name,</if>
<if test="desc != null and desc != ''">`desc`,</if>
......@@ -94,7 +101,7 @@
</insert>
<update id="updateCouponRule" parameterType="com.soss.system.domain.CouponRule">
update coupon_category
update coupon_rule
<trim prefix="set" suffixOverrides=",">
<if test="name != null and name != ''">name = #{name},</if>
<if test="desc != null and desc != ''">`desc` = #{desc},</if>
......
......@@ -25,4 +25,60 @@
from coupon_user
where coupon_id = #{couponId}
</select>
<insert id="insertCouponUser" parameterType="com.soss.system.domain.CouponUser" useGeneratedKeys="true" keyProperty="id">
insert into coupon_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="userId != null">user_id,</if>
<if test="userName != null and userName != ''">user_name,</if>
<if test="userPhone != null and userPhone != ''">user_phone,</if>
<if test="orderId != null">order_id,</if>
<if test="couponId != null">coupon_id,</if>
<if test="receiveTime != null">receive_time,</if>
<if test="activeTime != null">active_time,</if>
<if test="expiredTime != null">expired_time,</if>
<if test="discount != null">discount,</if>
<if test="source != null">source,</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="userId != null">#{userId},</if>
<if test="userName != null and userName != ''">#{userName},</if>
<if test="userPhone != null and userPhone != ''">#{userPhone},</if>
<if test="orderId != null">#{orderId},</if>
<if test="couponId != null">#{couponId},</if>
<if test="receiveTime != null">#{receiveTime},</if>
<if test="activeTime != null">#{activeTime},</if>
<if test="expiredTime != null">#{expiredTime},</if>
<if test="discount != null">#{discount},</if>
<if test="source != null">#{source},</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="updateCouponUser" parameterType="com.soss.system.domain.CouponUser">
update coupon_user
<trim prefix="SET" suffixOverrides=",">
<if test="userId != null">user_id = #{userId},</if>
<if test="userName != null and userName != ''">user_name = #{userName},</if>
<if test="userPhone != null and userPhone != ''">user_phone = #{userPhone},</if>
<if test="orderId != null">order_id = #{orderId},</if>
<if test="couponId != null">coupon_id = #{couponId},</if>
<if test="receiveTime != null">receive_time = #{receiveTime},</if>
<if test="activeTime != null">active_time = #{activeTime},</if>
<if test="expiredTime != null">expired_time = #{expiredTime},</if>
<if test="discount != null">discount = #{discount},</if>
<if test="source != null">source = #{source},</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>
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