diff --git a/mallinkAdmin/src/main/java/com/iformall/controller/market/WxCouponSendController.java b/mallinkAdmin/src/main/java/com/iformall/controller/market/WxCouponSendController.java index b661a9719..c4363da37 100644 --- a/mallinkAdmin/src/main/java/com/iformall/controller/market/WxCouponSendController.java +++ b/mallinkAdmin/src/main/java/com/iformall/controller/market/WxCouponSendController.java @@ -116,23 +116,6 @@ public class WxCouponSendController extends BaseController { return new ResultData(ErrorCode.COUPON_TYPE_IS_NOT_PASSIVE); } - //商户卡库存校验 - if(Objects.equals(wxCouponSend.getSendType(),EnumCouponSendSendType.MERCHANT.getCode())) { - JSONObject jo = JSONObject.parseObject(wxCouponSend.getConditions()) ; - if(Objects.isNull(jo)||!jo.containsKey(WxCouponSend.KEY_MERCHANT_LNVENTORY)) { - return new ResultData(ErrorCode.COUPON_SEND_LIMIT_VERIFY); - } - WxCoupon queryById = new WxCoupon() ; - queryById.setId(wxCouponSend.getId()); - WxCoupon wxCouponOfDb = wxCouponService.findById(queryById); - if(Objects.isNull(wxCouponOfDb)) { - return new ResultData(ErrorCode.COUPON_IS_EMPTY); - } - if(jo.getInteger(WxCouponSend.KEY_MERCHANT_LNVENTORY) >wxCouponOfDb.getRemainInventory()) { - return new ResultData(ErrorCode.REMAIN_IS_EMPTY); - } - } - wxCouponSend.setTenantId(wxCoupon.getTenantId()); wxCouponSend.setTitle(wxCoupon.getTitle()); diff --git a/mallinkSchedule/src/main/java/com/iformall/schedule/CouponSendSchedule.java b/mallinkSchedule/src/main/java/com/iformall/schedule/CouponSendSchedule.java index f79b738d7..b4ba40ce7 100644 --- a/mallinkSchedule/src/main/java/com/iformall/schedule/CouponSendSchedule.java +++ b/mallinkSchedule/src/main/java/com/iformall/schedule/CouponSendSchedule.java @@ -311,7 +311,6 @@ public class CouponSendSchedule { "]给:" + cu.getNickName() + " 电话-" + cu.getPhone() + " id-" + cu.getId()); - break; } }); }); diff --git a/mallinkService/src/main/java/com/iformall/domain/po/WxCouponSend.java b/mallinkService/src/main/java/com/iformall/domain/po/WxCouponSend.java index ff06d5e1a..c3f1e8b2f 100644 --- a/mallinkService/src/main/java/com/iformall/domain/po/WxCouponSend.java +++ b/mallinkService/src/main/java/com/iformall/domain/po/WxCouponSend.java @@ -45,6 +45,7 @@ public class WxCouponSend extends BaseEntity { protected Integer merchantLnventory; /**发卷所属商户名**/ + @Transient private String merchantName; @io.swagger.annotations.ApiModelProperty(value="租户ID",name="tenantId") @@ -58,7 +59,7 @@ public class WxCouponSend extends BaseEntity { private Integer sendType; @io.swagger.annotations.ApiModelProperty(value="发券条件",name="condition") private String conditions; - @io.swagger.annotations.ApiModelProperty(value="0:有效 1:无效",name="status") + @io.swagger.annotations.ApiModelProperty(value="全部时不传 0:有效 1:无效",name="status") private Integer status; @io.swagger.annotations.ApiModelProperty(value="创建时间",name="createDate") diff --git a/mallinkService/src/main/java/com/iformall/mapper/WxCouponMapper.java b/mallinkService/src/main/java/com/iformall/mapper/WxCouponMapper.java index 1ee46bedd..3a6605114 100644 --- a/mallinkService/src/main/java/com/iformall/mapper/WxCouponMapper.java +++ b/mallinkService/src/main/java/com/iformall/mapper/WxCouponMapper.java @@ -21,7 +21,11 @@ public interface WxCouponMapper extends CommonMapper { void reduceInventory(@Param("id")Long id,@Param("number")Integer number); - void backInventory(@Param("id")Long id,@Param("remainInventory")Integer remainInventory, @Param("number")Integer number); + void backInventory(@Param("id")Long id,@Param("remainInventory")Integer remainInventory, @Param("number")Integer number); + + Integer reduceRemainInventory(@Param("id") Long id, @Param("remainInventory") Integer remainInventory, @Param("number") Integer number); + + Integer backRemainInventory(@Param("id") Long id, @Param("remainInventory") Integer remainInventory, @Param("number") Integer number); void offExpiriedCouponByValidDate(); diff --git a/mallinkService/src/main/java/com/iformall/service/WxCouponService.java b/mallinkService/src/main/java/com/iformall/service/WxCouponService.java index b977c216e..28f15fc7b 100644 --- a/mallinkService/src/main/java/com/iformall/service/WxCouponService.java +++ b/mallinkService/src/main/java/com/iformall/service/WxCouponService.java @@ -6,6 +6,7 @@ import com.iformall.common.ResultData; import com.iformall.domain.po.WxCoupon; import com.iformall.domain.vo.WxCouponCVo; import com.iformall.domain.vo.WxCouponStatisVo; +import org.apache.ibatis.annotations.Param; public interface WxCouponService { @@ -202,4 +203,22 @@ public interface WxCouponService { WxCoupon findById(WxCoupon wxCoupon); + /** + * 库存减少 + * + * @param id 券ID + * @param remainInventory 当前库存 + * @param number 减少数量 + */ + void reduceRemainInventory(Long id, Integer remainInventory, Integer number); + + /** + * 库存回退 + * + * @param id 券ID + * @param remainInventory 当前库存 + * @param number 回退数量 + */ + void backRemainInventory(Long id, Integer remainInventory, Integer number); + } diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxCouponSendServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxCouponSendServiceImpl.java index d5505ac4d..dc2ee7115 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxCouponSendServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxCouponSendServiceImpl.java @@ -5,13 +5,16 @@ import com.alibaba.fastjson.JSONObject; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.google.common.collect.Lists; +import com.iformall.common.ErrorCode; import com.iformall.common.IdWorker; +import com.iformall.common.ResultData; import com.iformall.domain.po.*; import com.iformall.domain.po.msg.AppUniformMsg; import com.iformall.domain.po.msg.MpAppMsg; import com.iformall.domain.po.msg.WxMsgRecord; import com.iformall.domain.vo.WxCouponSendVo; import com.iformall.enums.*; +import com.iformall.exception.BizMessageException; import com.iformall.exception.MallinkException; import com.iformall.mapper.*; import com.iformall.mq.MqBaseProducer; @@ -23,6 +26,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @@ -84,14 +88,36 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { return wxCouponSendMapper.selectByPrimaryKey(id); } + @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void saveOrUpdate(WxCouponSend record) { + //会员生日券配置 if(Objects.equals(record.getSendType(),EnumCouponSendSendType.BIRTHDAY.getCode())) { WxCouponSend config = getConfig(record.getSendType(),record.getTenantId()) ; if(Objects.nonNull(config)) { record.setConditions(config.getConditions()); } } + + //商户卡库存校验 + if(Objects.equals(record.getSendType(),EnumCouponSendSendType.MERCHANT.getCode())) { + JSONObject jo = JSONObject.parseObject(record.getConditions()) ; + if(Objects.isNull(jo)||!jo.containsKey(WxCouponSend.KEY_MERCHANT_LNVENTORY)) { + throw new BizMessageException(ErrorCode.COUPON_SEND_LIMIT_VERIFY.getMessage()) ; + } + WxCoupon queryById = new WxCoupon() ; + queryById.setId(record.getId()); + WxCoupon wxCouponOfDb = wxCouponService.findById(queryById); + if(Objects.isNull(wxCouponOfDb)) { + throw new BizMessageException(ErrorCode.COUPON_IS_EMPTY.getMessage()) ; + } + int merchantLnventory = jo.getInteger(WxCouponSend.KEY_MERCHANT_LNVENTORY) ; + if(merchantLnventory > wxCouponOfDb.getRemainInventory()) { + throw new BizMessageException(ErrorCode.REMAIN_IS_EMPTY.getMessage()) ; + } + //更新库存 + wxCouponService.reduceRemainInventory(wxCouponOfDb.getId(),wxCouponOfDb.getRemainInventory(),merchantLnventory); + } if (record.getId() == null) { //record.setId(UUID.randomUUID().toString().replaceAll("-", "")); final IdWorker idWorker = IdWorker.get(); diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxCouponServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxCouponServiceImpl.java index 4c1a34adf..74fa7a8f5 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxCouponServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxCouponServiceImpl.java @@ -14,6 +14,7 @@ import com.iformall.domain.vo.WxCouponCVo; import com.iformall.domain.vo.WxCouponStatisVo; import com.iformall.domain.vo.WxMerchantVo; import com.iformall.enums.*; +import com.iformall.exception.BizMessageException; import com.iformall.exception.MallinkException; import com.iformall.mapper.*; import com.iformall.service.*; @@ -36,7 +37,7 @@ import java.util.stream.Collectors; @Service -public class WxCouponServiceImpl implements WxCouponService { + public class WxCouponServiceImpl implements WxCouponService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired @@ -642,4 +643,22 @@ public class WxCouponServiceImpl implements WxCouponService { } return null; } + + @Override + @Transactional(isolation= Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) + public void reduceRemainInventory(Long id, Integer remainInventory, Integer number) { + int num = wxCouponMapper.reduceRemainInventory(id, remainInventory, number); + if (num == 0) { + throw new MallinkException(ErrorCode.ORDER_IS_FAIL); + } + } + + @Override + @Transactional(isolation= Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) + public void backRemainInventory(Long id, Integer remainInventory, Integer number) { + int num = wxCouponMapper.backRemainInventory(id, remainInventory, number); + if (num == 0) { + throw new MallinkException(ErrorCode.ORDER_IS_FAIL); + } + } } diff --git a/mallinkService/src/main/resources/mapper/WxCouponMapper.xml b/mallinkService/src/main/resources/mapper/WxCouponMapper.xml index 6015c2a50..4bd768ceb 100644 --- a/mallinkService/src/main/resources/mapper/WxCouponMapper.xml +++ b/mallinkService/src/main/resources/mapper/WxCouponMapper.xml @@ -519,6 +519,14 @@ update wx_coupon SET remain_inventory = remain_inventory + #{number} where id = #{id} and remain_inventory>= #{remainInventory} + + update wx_coupon SET remain_inventory = remain_inventory - #{number} where id = #{id} and remain_inventory= #{remainInventory} + + + + update wx_coupon SET remain_inventory = remain_inventory + #{number} where id = #{id} and remain_inventory= #{remainInventory} + + update wx_coupon SET status = 1, update_date = now() where status = 0 and valid_type = 1 and valid_end_date < now() diff --git a/mallinkService/src/main/resources/mapper/WxCouponSendMapper.xml b/mallinkService/src/main/resources/mapper/WxCouponSendMapper.xml index f6498cc29..9749afc6f 100644 --- a/mallinkService/src/main/resources/mapper/WxCouponSendMapper.xml +++ b/mallinkService/src/main/resources/mapper/WxCouponSendMapper.xml @@ -104,7 +104,7 @@ from wx_coupon_send cs left join wx_coupon c on c.id = cs.coupon_id ) as tt - where tt.merchant_name like concat('%', #{merchantName},'%') + where tt.conditions like concat('%', #{merchantName},'%')