From 075bcbe513661ab07fdfb32715adc677d1bb0c6f Mon Sep 17 00:00:00 2001 From: Burce Date: Thu, 22 Aug 2019 14:48:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E6=88=B7=E6=B3=A8=E5=88=B8->=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=E5=95=86=E6=88=B7=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/iformall/service/WxCouponService.java | 2 +- .../service/impl/WxCouponSendServiceImpl.java | 120 +++++++----------- 2 files changed, 50 insertions(+), 72 deletions(-) diff --git a/mallinkService/src/main/java/com/iformall/service/WxCouponService.java b/mallinkService/src/main/java/com/iformall/service/WxCouponService.java index 28f15fc7b..dffc887e8 100644 --- a/mallinkService/src/main/java/com/iformall/service/WxCouponService.java +++ b/mallinkService/src/main/java/com/iformall/service/WxCouponService.java @@ -213,7 +213,7 @@ public interface WxCouponService { void reduceRemainInventory(Long id, Integer remainInventory, Integer number); /** - * 库存回退 + * 库存退回 * * @param id 券ID * @param remainInventory 当前库存 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 b7ebfe6d0..39b39c4ef 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxCouponSendServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxCouponSendServiceImpl.java @@ -32,7 +32,7 @@ import java.text.SimpleDateFormat; import java.util.*; @Service -public class WxCouponSendServiceImpl implements WxCouponSendService { + public class WxCouponSendServiceImpl implements WxCouponSendService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired @@ -96,22 +96,23 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { 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()) ; + throw new MallinkException(ErrorCode.COUPON_SEND_LIMIT_VERIFY) ; } + //查询总库存 WxCoupon queryById = new WxCoupon() ; - queryById.setId(record.getId()); + queryById.setId(record.getCouponId()); WxCoupon wxCouponOfDb = wxCouponService.findById(queryById); if(Objects.isNull(wxCouponOfDb)) { - throw new BizMessageException(ErrorCode.COUPON_IS_EMPTY.getMessage()) ; + throw new MallinkException(ErrorCode.COUPON_IS_EMPTY) ; } int merchantLnventory = jo.getInteger(WxCouponSend.KEY_MERCHANT_LNVENTORY) ; + //商家购券数不能大于总库存 if(merchantLnventory > wxCouponOfDb.getRemainInventory()) { - throw new BizMessageException(ErrorCode.REMAIN_IS_EMPTY.getMessage()) ; + throw new MallinkException(ErrorCode.REMAIN_IS_EMPTY) ; } //更新库存 wxCouponService.reduceRemainInventory(wxCouponOfDb.getId(),wxCouponOfDb.getRemainInventory(),merchantLnventory); @@ -126,52 +127,29 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { } } - /** - * 会员生日券处理 - * - * @param record - * @return - */ - private WxCouponSend buildConditions(WxCouponSend record) { - if (!Objects.equals(record.getSendType(), EnumCouponSendSendType.BIRTHDAY.getCode())) { - return record; - } - - //添加会员生日券 - JSONObject conditions = JSONObject.parseObject(record.getConditions()); - Long couponId = record.getCouponId(); - if (Objects.nonNull(couponId)) { - JSONArray couponIds; - if (Objects.isNull(record.getId())) { - //添加默认值 - if (Objects.isNull(record.getTitle())) { - record.setTitle(""); - } - if (Objects.isNull(record.getCouponId())) { - record.setCouponId(0L); - } - //添加新卡券 - couponIds = new JSONArray(); - couponIds.add(couponId); - } else { - //更新卡券列表 - WxCouponSend couponSend = wxCouponSendMapper.selectByPrimaryKey(record.getId()); - conditions = JSONObject.parseObject(couponSend.getConditions()); - couponIds = conditions.getJSONArray(WxCouponSend.KEY_COUPONIDS); - if (Objects.isNull(couponIds)) { - couponIds = new JSONArray(); - couponIds.add(couponId); - } else { - //添加新卡券 - if (!couponIds.contains(couponId)) { - couponIds.add(couponId); - } - } + @Override + @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) + public void updateInvalid(WxCouponSend record) { + record.setStatus(EnumCouponSendStatus.INVALID.getCode()); + record.setUpdateDate(new Date()); + if(Objects.equals(record.getSendType(),EnumCouponSendSendType.MERCHANT.getCode())) { + //查询总库存 + WxCoupon queryById = new WxCoupon() ; + queryById.setId(record.getCouponId()); + WxCoupon wxCouponOfDb = wxCouponService.findById(queryById); + if(Objects.isNull(wxCouponOfDb)) { + throw new MallinkException(ErrorCode.COUPON_IS_EMPTY) ; + } + //查询商家 + JSONObject jo = JSONObject.parseObject(record.getConditions()) ; + if(Objects.isNull(jo)||!jo.containsKey(WxCouponSend.KEY_MERCHANT_LNVENTORY)) { + throw new MallinkException(ErrorCode.COUPON_SEND_LIMIT_VERIFY) ; } - conditions.put(WxCouponSend.KEY_COUPONIDS, couponIds); + int merchantLnventory = jo.getInteger(WxCouponSend.KEY_MERCHANT_LNVENTORY) ; + //退回库存 + wxCouponService.backRemainInventory(queryById.getId(),wxCouponOfDb.getRemainInventory(),merchantLnventory); } - record.setConditions(conditions.toJSONString()); - return record; + wxCouponSendMapper.updateByPrimaryKeySelective(record); } @Override @@ -494,34 +472,30 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { @Override public void saveOrUpdateConfig(Integer beforeDays, Integer sendType, String tenantId) { - WxCouponSend couponSendQuery = new WxCouponSend(); - couponSendQuery.setSendType(sendType); - couponSendQuery.setTenantId(tenantId); - List wxCouponSendList = wxCouponSendMapper.select(couponSendQuery); - if (CollectionUtils.isEmpty(wxCouponSendList)) { - WxCouponSend couponSend = new WxCouponSend(); - couponSend.setTitle(EnumCouponSendSendType.BIRTHDAY.getMessage()+"配置信息 "); - couponSend.setCouponId(0L); - couponSend.setSendType(sendType); + WxCouponSend config = getConfig(sendType, tenantId) ; + if (Objects.isNull(config)) { + WxCouponSend couponSendQuery = new WxCouponSend(); + couponSendQuery.setSendType(sendType); + couponSendQuery.setTenantId(tenantId); + couponSendQuery.setCouponId(0L); + couponSendQuery.setTitle(EnumCouponSendSendType.BIRTHDAY.getMessage()+"配置信息 "); final IdWorker idWorker = IdWorker.get(); - couponSend.setId(idWorker.nextId()); - couponSend.setTenantId(tenantId); - couponSend.setStatus(EnumCouponSendStatus.INVALID.getCode()); - couponSend.setCreateDate(new Date()); + couponSendQuery.setId(idWorker.nextId()); + couponSendQuery.setStatus(EnumCouponSendStatus.INVALID.getCode()); + couponSendQuery.setCreateDate(new Date()); JSONObject couponSendJo = new JSONObject(); couponSendJo.put(WxCouponSend.KEY_BEFOREDAYS, beforeDays); - couponSend.setConditions(couponSendJo.toJSONString()); - wxCouponSendMapper.insertSelective(couponSend); + couponSendQuery.setConditions(couponSendJo.toJSONString()); + wxCouponSendMapper.insertSelective(couponSendQuery); } else { - WxCouponSend couponSend = wxCouponSendList.get(0); - JSONObject couponSendJo = JSONObject.parseObject(couponSend.getConditions()); + JSONObject couponSendJo = JSONObject.parseObject(config.getConditions()); if(Objects.isNull(couponSendJo)) { couponSendJo = new JSONObject(); } couponSendJo.put(WxCouponSend.KEY_BEFOREDAYS, beforeDays); - couponSend.setConditions(couponSendJo.toJSONString()); - couponSend.setUpdateDate(new Date()); - wxCouponSendMapper.updateBirthdayConfig(couponSend); + config.setConditions(couponSendJo.toJSONString()); + config.setUpdateDate(new Date()); + wxCouponSendMapper.updateBirthdayConfig(config); } } @@ -535,7 +509,11 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { couponSendQuery.setStatus(EnumCouponSendStatus.INVALID.getCode()); List wxCouponSendList = wxCouponSendMapper.select(couponSendQuery); if (CollectionUtils.isEmpty(wxCouponSendList)) { - return null; + //默认配置 + JSONObject couponSendJo = new JSONObject(); + couponSendJo.put(WxCouponSend.KEY_BEFOREDAYS, 0); + couponSendQuery.setConditions(couponSendJo.toJSONString()); + return couponSendQuery; } return wxCouponSendList.get(0); }