|
|
|
@@ -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<WxCouponSend> 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<WxCouponSend> 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); |
|
|
|
} |
|
|
|
|