|
|
@@ -33,7 +33,7 @@ import java.text.SimpleDateFormat; |
|
|
import java.util.*; |
|
|
import java.util.*; |
|
|
|
|
|
|
|
|
@Service |
|
|
@Service |
|
|
public class WxCouponSendServiceImpl implements WxCouponSendService { |
|
|
|
|
|
|
|
|
public class WxCouponSendServiceImpl implements WxCouponSendService { |
|
|
private final Logger logger = LoggerFactory.getLogger(this.getClass()); |
|
|
private final Logger logger = LoggerFactory.getLogger(this.getClass()); |
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
@@ -65,7 +65,7 @@ import java.util.*; |
|
|
@Autowired |
|
|
@Autowired |
|
|
WxMsgLimitService wxMsgLimitService; |
|
|
WxMsgLimitService wxMsgLimitService; |
|
|
@Autowired |
|
|
@Autowired |
|
|
WxCouponMerchantMapper wxCouponMerchantMapper ; |
|
|
|
|
|
|
|
|
WxCouponMerchantMapper wxCouponMerchantMapper; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public PageInfo<WxCouponSendVo> listAsPage(WxCouponSend record, Integer pageIndex, Integer pageSize) { |
|
|
public PageInfo<WxCouponSendVo> listAsPage(WxCouponSend record, Integer pageIndex, Integer pageSize) { |
|
|
@@ -93,46 +93,46 @@ import java.util.*; |
|
|
@Override |
|
|
@Override |
|
|
public void saveOrUpdate(WxCouponSend record) { |
|
|
public void saveOrUpdate(WxCouponSend record) { |
|
|
//会员生日券配置 |
|
|
//会员生日券配置 |
|
|
if(Objects.equals(record.getSendType(),EnumCouponSendSendType.BIRTHDAY.getCode())) { |
|
|
|
|
|
WxCouponSend config = getConfig(record.getSendType(),record.getTenantId()) ; |
|
|
|
|
|
if(Objects.nonNull(config)) { |
|
|
|
|
|
|
|
|
if (Objects.equals(record.getSendType(), EnumCouponSendSendType.BIRTHDAY.getCode())) { |
|
|
|
|
|
WxCouponSend config = getConfig(record.getSendType(), record.getTenantId()); |
|
|
|
|
|
if (Objects.nonNull(config)) { |
|
|
record.setConditions(config.getConditions()); |
|
|
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 MallinkException(ErrorCode.COUPON_SEND_LIMIT_VERIFY) ; |
|
|
|
|
|
|
|
|
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 MallinkException(ErrorCode.COUPON_SEND_LIMIT_VERIFY); |
|
|
} |
|
|
} |
|
|
//查询总库存 |
|
|
//查询总库存 |
|
|
WxCoupon queryById = new WxCoupon() ; |
|
|
|
|
|
|
|
|
WxCoupon queryById = new WxCoupon(); |
|
|
queryById.setId(record.getCouponId()); |
|
|
queryById.setId(record.getCouponId()); |
|
|
WxCoupon wxCouponOfDb = wxCouponService.findById(queryById); |
|
|
|
|
|
if(Objects.isNull(wxCouponOfDb)) { |
|
|
|
|
|
throw new MallinkException(ErrorCode.COUPON_IS_EMPTY) ; |
|
|
|
|
|
|
|
|
WxCoupon wxCouponOfDb = wxCouponService.findById(queryById); |
|
|
|
|
|
if (Objects.isNull(wxCouponOfDb)) { |
|
|
|
|
|
throw new MallinkException(ErrorCode.COUPON_IS_EMPTY); |
|
|
} |
|
|
} |
|
|
String merchantIdStr = jo.getString("id") ; |
|
|
|
|
|
if(Objects.isNull(merchantIdStr)) { |
|
|
|
|
|
throw new MallinkException(ErrorCode.MERCHANT_INFO_NOT_FOUND) ; |
|
|
|
|
|
|
|
|
String merchantIdStr = jo.getString("id"); |
|
|
|
|
|
if (Objects.isNull(merchantIdStr)) { |
|
|
|
|
|
throw new MallinkException(ErrorCode.MERCHANT_INFO_NOT_FOUND); |
|
|
} |
|
|
} |
|
|
WxCouponMerchant couponMerchantQ = new WxCouponMerchant(); |
|
|
WxCouponMerchant couponMerchantQ = new WxCouponMerchant(); |
|
|
couponMerchantQ.setTenantId(record.getTenantId()); |
|
|
couponMerchantQ.setTenantId(record.getTenantId()); |
|
|
couponMerchantQ.setProductId(record.getCouponId()); |
|
|
couponMerchantQ.setProductId(record.getCouponId()); |
|
|
couponMerchantQ.setMerchantId(Long.parseLong(merchantIdStr)); |
|
|
couponMerchantQ.setMerchantId(Long.parseLong(merchantIdStr)); |
|
|
couponMerchantQ.setStatus(EnumCouponMerchantStatus.COUPON_MERCHANT_STATUS_VALID.getCode()); |
|
|
couponMerchantQ.setStatus(EnumCouponMerchantStatus.COUPON_MERCHANT_STATUS_VALID.getCode()); |
|
|
WxCouponMerchant couponMerchant = wxCouponMerchantMapper.selectOne(couponMerchantQ) ; |
|
|
|
|
|
if(Objects.isNull(couponMerchant)) { |
|
|
|
|
|
throw new MallinkException(ErrorCode.COUPON_MERCHANT_NOT_FOUND) ; |
|
|
|
|
|
|
|
|
WxCouponMerchant couponMerchant = wxCouponMerchantMapper.selectOne(couponMerchantQ); |
|
|
|
|
|
if (Objects.isNull(couponMerchant)) { |
|
|
|
|
|
throw new MallinkException(ErrorCode.COUPON_MERCHANT_NOT_FOUND); |
|
|
} |
|
|
} |
|
|
int merchantLnventory = jo.getInteger(WxCouponSend.KEY_MERCHANT_LNVENTORY) ; |
|
|
|
|
|
|
|
|
int merchantLnventory = jo.getInteger(WxCouponSend.KEY_MERCHANT_LNVENTORY); |
|
|
//商家购券数不能大于总库存 |
|
|
//商家购券数不能大于总库存 |
|
|
if(merchantLnventory > wxCouponOfDb.getRemainInventory()) { |
|
|
|
|
|
throw new MallinkException(ErrorCode.REMAIN_IS_EMPTY) ; |
|
|
|
|
|
|
|
|
if (merchantLnventory > wxCouponOfDb.getRemainInventory()) { |
|
|
|
|
|
throw new MallinkException(ErrorCode.REMAIN_IS_EMPTY); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//更新库存 |
|
|
//更新库存 |
|
|
wxCouponService.reduceRemainInventory(wxCouponOfDb.getId(),wxCouponOfDb.getRemainInventory(),merchantLnventory); |
|
|
|
|
|
|
|
|
wxCouponService.reduceRemainInventory(wxCouponOfDb.getId(), wxCouponOfDb.getRemainInventory(), merchantLnventory); |
|
|
} |
|
|
} |
|
|
if (record.getId() == null) { |
|
|
if (record.getId() == null) { |
|
|
//record.setId(UUID.randomUUID().toString().replaceAll("-", "")); |
|
|
//record.setId(UUID.randomUUID().toString().replaceAll("-", "")); |
|
|
@@ -149,22 +149,22 @@ import java.util.*; |
|
|
public void updateInvalid(WxCouponSend record) { |
|
|
public void updateInvalid(WxCouponSend record) { |
|
|
record.setStatus(EnumCouponSendStatus.INVALID.getCode()); |
|
|
record.setStatus(EnumCouponSendStatus.INVALID.getCode()); |
|
|
record.setUpdateDate(new Date()); |
|
|
record.setUpdateDate(new Date()); |
|
|
if(Objects.equals(record.getSendType(),EnumCouponSendSendType.MERCHANT.getCode())) { |
|
|
|
|
|
|
|
|
if (Objects.equals(record.getSendType(), EnumCouponSendSendType.MERCHANT.getCode())) { |
|
|
//查询总库存 |
|
|
//查询总库存 |
|
|
WxCoupon queryById = new WxCoupon() ; |
|
|
|
|
|
|
|
|
WxCoupon queryById = new WxCoupon(); |
|
|
queryById.setId(record.getCouponId()); |
|
|
queryById.setId(record.getCouponId()); |
|
|
WxCoupon wxCouponOfDb = wxCouponService.findById(queryById); |
|
|
|
|
|
if(Objects.isNull(wxCouponOfDb)) { |
|
|
|
|
|
throw new MallinkException(ErrorCode.COUPON_IS_EMPTY) ; |
|
|
|
|
|
|
|
|
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) ; |
|
|
|
|
|
|
|
|
JSONObject jo = JSONObject.parseObject(record.getConditions()); |
|
|
|
|
|
if (Objects.isNull(jo) || !jo.containsKey(WxCouponSend.KEY_MERCHANT_LNVENTORY)) { |
|
|
|
|
|
throw new MallinkException(ErrorCode.COUPON_SEND_LIMIT_VERIFY); |
|
|
} |
|
|
} |
|
|
int merchantLnventory = jo.getInteger(WxCouponSend.KEY_MERCHANT_LNVENTORY) ; |
|
|
|
|
|
|
|
|
int merchantLnventory = jo.getInteger(WxCouponSend.KEY_MERCHANT_LNVENTORY); |
|
|
//退回coupon库存 |
|
|
//退回coupon库存 |
|
|
wxCouponService.backRemainInventory(queryById.getId(),wxCouponOfDb.getRemainInventory(),merchantLnventory); |
|
|
|
|
|
|
|
|
wxCouponService.backRemainInventory(queryById.getId(), wxCouponOfDb.getRemainInventory(), merchantLnventory); |
|
|
} |
|
|
} |
|
|
wxCouponSendMapper.updateByPrimaryKeySelective(record); |
|
|
wxCouponSendMapper.updateByPrimaryKeySelective(record); |
|
|
} |
|
|
} |
|
|
@@ -315,7 +315,7 @@ import java.util.*; |
|
|
// 发放免费券 |
|
|
// 发放免费券 |
|
|
WxCouponOrder couponOrder = null; |
|
|
WxCouponOrder couponOrder = null; |
|
|
try { |
|
|
try { |
|
|
couponOrder = wxOrderService.sendFreeCouponToUser(cUserId, send.getCouponId(),null); |
|
|
|
|
|
|
|
|
couponOrder = wxOrderService.sendFreeCouponToUser(cUserId, send.getCouponId(), null); |
|
|
if (couponOrder != null) { |
|
|
if (couponOrder != null) { |
|
|
bRet = true; |
|
|
bRet = true; |
|
|
|
|
|
|
|
|
@@ -489,13 +489,13 @@ import java.util.*; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void saveOrUpdateConfig(Integer beforeDays, Integer sendType, String tenantId) { |
|
|
public void saveOrUpdateConfig(Integer beforeDays, Integer sendType, String tenantId) { |
|
|
WxCouponSend config = getConfig(sendType, tenantId) ; |
|
|
|
|
|
|
|
|
WxCouponSend config = getConfig(sendType, tenantId); |
|
|
if (Objects.isNull(config)) { |
|
|
if (Objects.isNull(config)) { |
|
|
WxCouponSend couponSendQuery = new WxCouponSend(); |
|
|
WxCouponSend couponSendQuery = new WxCouponSend(); |
|
|
couponSendQuery.setSendType(sendType); |
|
|
couponSendQuery.setSendType(sendType); |
|
|
couponSendQuery.setTenantId(tenantId); |
|
|
couponSendQuery.setTenantId(tenantId); |
|
|
couponSendQuery.setCouponId(0L); |
|
|
couponSendQuery.setCouponId(0L); |
|
|
couponSendQuery.setTitle(EnumCouponSendSendType.BIRTHDAY.getMessage()+"配置信息 "); |
|
|
|
|
|
|
|
|
couponSendQuery.setTitle(EnumCouponSendSendType.BIRTHDAY.getMessage() + "配置信息 "); |
|
|
final IdWorker idWorker = IdWorker.get(); |
|
|
final IdWorker idWorker = IdWorker.get(); |
|
|
couponSendQuery.setId(idWorker.nextId()); |
|
|
couponSendQuery.setId(idWorker.nextId()); |
|
|
couponSendQuery.setStatus(EnumCouponSendStatus.INVALID.getCode()); |
|
|
couponSendQuery.setStatus(EnumCouponSendStatus.INVALID.getCode()); |
|
|
@@ -506,7 +506,7 @@ import java.util.*; |
|
|
wxCouponSendMapper.insertSelective(couponSendQuery); |
|
|
wxCouponSendMapper.insertSelective(couponSendQuery); |
|
|
} else { |
|
|
} else { |
|
|
JSONObject couponSendJo = JSONObject.parseObject(config.getConditions()); |
|
|
JSONObject couponSendJo = JSONObject.parseObject(config.getConditions()); |
|
|
if(Objects.isNull(couponSendJo)) { |
|
|
|
|
|
|
|
|
if (Objects.isNull(couponSendJo)) { |
|
|
couponSendJo = new JSONObject(); |
|
|
couponSendJo = new JSONObject(); |
|
|
} |
|
|
} |
|
|
couponSendJo.put(WxCouponSend.KEY_BEFOREDAYS, beforeDays); |
|
|
couponSendJo.put(WxCouponSend.KEY_BEFOREDAYS, beforeDays); |
|
|
@@ -517,7 +517,7 @@ import java.util.*; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public WxCouponSend getConfig(Integer sendType,String tenantId) { |
|
|
|
|
|
|
|
|
public WxCouponSend getConfig(Integer sendType, String tenantId) { |
|
|
WxCouponSend couponSendQuery = new WxCouponSend(); |
|
|
WxCouponSend couponSendQuery = new WxCouponSend(); |
|
|
couponSendQuery.setSendType(sendType); |
|
|
couponSendQuery.setSendType(sendType); |
|
|
couponSendQuery.setTenantId(tenantId); |
|
|
couponSendQuery.setTenantId(tenantId); |
|
|
@@ -550,4 +550,19 @@ import java.util.*; |
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) |
|
|
|
|
|
public void handSel(WxCouponSend wxCouponSend, WxCUser user) { |
|
|
|
|
|
List<WxCouponSendVo> couponSendList = wxCouponSendMapper.findListVo(wxCouponSend); |
|
|
|
|
|
if (CollectionUtils.isEmpty(couponSendList)) { |
|
|
|
|
|
throw new MallinkException(ErrorCode.COUPON_IS_EMPTY); |
|
|
|
|
|
} |
|
|
|
|
|
couponSendList.forEach(cs -> { |
|
|
|
|
|
// 发放免费券 |
|
|
|
|
|
WxCouponOrder couponOrder = wxOrderService.sendFreeCouponToUser(user.getId(), cs.getCouponId(), cs); |
|
|
|
|
|
wxCouponActionLogService.addOne(user.getTenantId(), cs.getCouponId(), couponOrder.getId(), cs.getSendType(), cs.getId()); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
} |
|
|
} |