Просмотр исходного кода

商户注券->更新库存操作

release_toaliyun_real
Burce 6 лет назад
Родитель
Сommit
ece83a8bec
3 измененных файлов: 59 добавлений и 68 удалений
  1. +4
    -30
      mallinkBApi/src/main/java/com/iformall/controller/WxCouponSendController.java
  2. +2
    -0
      mallinkService/src/main/java/com/iformall/service/WxCouponSendService.java
  3. +53
    -38
      mallinkService/src/main/java/com/iformall/service/impl/WxCouponSendServiceImpl.java

+ 4
- 30
mallinkBApi/src/main/java/com/iformall/controller/WxCouponSendController.java Просмотреть файл

@@ -3,26 +3,18 @@ package com.iformall.controller;
import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.domain.po.BaseEntity;
import com.iformall.domain.po.WxCUser;
import com.iformall.domain.po.WxCouponOrder;
import com.iformall.domain.po.WxCouponSend;
import com.iformall.domain.po.*;
import com.iformall.domain.vo.WxCouponSendVo;
import com.iformall.enums.EnumCouponSendSendType;
import com.iformall.enums.EnumCouponSendStatus;
import com.iformall.exception.MallinkException;
import com.iformall.mapper.WxCUserMapper;
import com.iformall.mapper.WxCouponActionLogMapper;
import com.iformall.mapper.WxCouponSendMapper;
import com.iformall.service.WxCouponActionLogService;
import com.iformall.service.WxCouponSendService;
import com.iformall.service.WxCouponService;
import com.iformall.service.WxOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -32,9 +24,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@RestController
@@ -42,16 +32,11 @@ import java.util.Objects;
@Api(description = "商户注券相关接口")
public class WxCouponSendController extends BaseController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Resource
private WxCouponSendMapper wxCouponSendMapper;
@Autowired
private WxCouponSendService wxCouponSendService;
@Autowired
private WxCouponActionLogService wxCouponActionLogService;

@Autowired
private WxOrderService wxOrderService;

@Resource
private WxCUserMapper wxCUserMapper;

@@ -60,7 +45,6 @@ public class WxCouponSendController extends BaseController {
@ApiImplicitParams({
@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
@ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
@ApiImplicitParam(name = "merchantId", value = "商户ID", dataType = "long", paramType = "query", required = true),
})
public ResultData list(WxCouponSend wxCouponSend, Integer pageNum, Integer pageSize) {
logger.debug("[" + getIpAddr() + "] WxCouponSendController::list");
@@ -68,6 +52,7 @@ public class WxCouponSendController extends BaseController {
throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL);
}
wxCouponSend.setTenantId(getTenantId());
wxCouponSend.setMerchantId(getUser().getMerchantId());
wxCouponSend.setStatus(Objects.isNull(wxCouponSend.getStatus()) ? null : wxCouponSend.getStatus());
//库存大于0
wxCouponSend.setMerchantLnventory(0);
@@ -94,24 +79,13 @@ public class WxCouponSendController extends BaseController {
if (ArrayUtils.isEmpty(wxCouponSendIds) || Objects.isNull(cUserId)) {
throw new MallinkException(ErrorCode.COUPON_IS_EMPTY);
}

WxCouponSend wxCouponSend = new WxCouponSend();
wxCouponSend.setIds(Arrays.asList(wxCouponSendIds));
wxCouponSend.setTenantId(getTenantId());
wxCouponSend.setSendType(EnumCouponSendSendType.MERCHANT.getCode());
wxCouponSend.setStatus(EnumCouponSendStatus.VALID.getCode());
List<WxCouponSendVo> couponSendList = wxCouponSendMapper.findListVo(wxCouponSend);
if (CollectionUtils.isEmpty(couponSendList)) {
throw new MallinkException(ErrorCode.COUPON_IS_EMPTY);
}
couponSendList.forEach(cs -> {
// 发放免费券
try {
WxCouponOrder couponOrder = wxOrderService.sendFreeCouponToUser(cu.getId(), cs.getCouponId(), cs);
wxCouponActionLogService.addOne(cu.getTenantId(), cs.getCouponId(), couponOrder.getId(), cs.getSendType(), cs.getId());
} catch (Exception e) {
logger.error("定时发券:发券失败 levelId=" + cu.getId() + " couponId=" + cs.getCouponId() + " userId=" + cu.getId() + e.getMessage());
}
});
wxCouponSendService.handSel(wxCouponSend,cu);

return new ResultData();
}


+ 2
- 0
mallinkService/src/main/java/com/iformall/service/WxCouponSendService.java Просмотреть файл

@@ -1,6 +1,7 @@
package com.iformall.service;

import com.github.pagehelper.PageInfo;
import com.iformall.domain.po.WxCUser;
import com.iformall.domain.po.WxCouponSend;
import com.iformall.domain.vo.WxCouponSendVo;
import com.iformall.enums.EnumCouponSendSendType;
@@ -90,5 +91,6 @@ public interface WxCouponSendService {
*/
void backMerchantRemainInventory(Long id, Integer remainInventory, Integer number);

void handSel(WxCouponSend wxCouponSend, WxCUser user) ;

}

+ 53
- 38
mallinkService/src/main/java/com/iformall/service/impl/WxCouponSendServiceImpl.java Просмотреть файл

@@ -33,7 +33,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
@@ -65,7 +65,7 @@ import java.util.*;
@Autowired
WxMsgLimitService wxMsgLimitService;
@Autowired
WxCouponMerchantMapper wxCouponMerchantMapper ;
WxCouponMerchantMapper wxCouponMerchantMapper;

@Override
public PageInfo<WxCouponSendVo> listAsPage(WxCouponSend record, Integer pageIndex, Integer pageSize) {
@@ -93,46 +93,46 @@ import java.util.*;
@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)) {
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 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());
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();
couponMerchantQ.setTenantId(record.getTenantId());
couponMerchantQ.setProductId(record.getCouponId());
couponMerchantQ.setMerchantId(Long.parseLong(merchantIdStr));
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) {
//record.setId(UUID.randomUUID().toString().replaceAll("-", ""));
@@ -149,22 +149,22 @@ import java.util.*;
public void updateInvalid(WxCouponSend record) {
record.setStatus(EnumCouponSendStatus.INVALID.getCode());
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());
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库存
wxCouponService.backRemainInventory(queryById.getId(),wxCouponOfDb.getRemainInventory(),merchantLnventory);
wxCouponService.backRemainInventory(queryById.getId(), wxCouponOfDb.getRemainInventory(), merchantLnventory);
}
wxCouponSendMapper.updateByPrimaryKeySelective(record);
}
@@ -315,7 +315,7 @@ import java.util.*;
// 发放免费券
WxCouponOrder couponOrder = null;
try {
couponOrder = wxOrderService.sendFreeCouponToUser(cUserId, send.getCouponId(),null);
couponOrder = wxOrderService.sendFreeCouponToUser(cUserId, send.getCouponId(), null);
if (couponOrder != null) {
bRet = true;

@@ -489,13 +489,13 @@ import java.util.*;

@Override
public void saveOrUpdateConfig(Integer beforeDays, Integer sendType, String tenantId) {
WxCouponSend config = getConfig(sendType, tenantId) ;
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()+"配置信息 ");
couponSendQuery.setTitle(EnumCouponSendSendType.BIRTHDAY.getMessage() + "配置信息 ");
final IdWorker idWorker = IdWorker.get();
couponSendQuery.setId(idWorker.nextId());
couponSendQuery.setStatus(EnumCouponSendStatus.INVALID.getCode());
@@ -506,7 +506,7 @@ import java.util.*;
wxCouponSendMapper.insertSelective(couponSendQuery);
} else {
JSONObject couponSendJo = JSONObject.parseObject(config.getConditions());
if(Objects.isNull(couponSendJo)) {
if (Objects.isNull(couponSendJo)) {
couponSendJo = new JSONObject();
}
couponSendJo.put(WxCouponSend.KEY_BEFOREDAYS, beforeDays);
@@ -517,7 +517,7 @@ import java.util.*;
}

@Override
public WxCouponSend getConfig(Integer sendType,String tenantId) {
public WxCouponSend getConfig(Integer sendType, String tenantId) {
WxCouponSend couponSendQuery = new WxCouponSend();
couponSendQuery.setSendType(sendType);
couponSendQuery.setTenantId(tenantId);
@@ -550,4 +550,19 @@ import java.util.*;
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());
});
}
}

Загрузка…
Отмена
Сохранить