|
|
|
@@ -1,13 +1,17 @@ |
|
|
|
package com.iformall.controller; |
|
|
|
|
|
|
|
import java.lang.reflect.InvocationTargetException; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
import com.iformall.enums.EnumCouponType; |
|
|
|
import com.iformall.enums.EnumMerchantStatus; |
|
|
|
|
|
|
|
import org.apache.commons.beanutils.BeanUtils; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
@@ -20,6 +24,7 @@ import org.springframework.web.bind.annotation.GetMapping; |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.iformall.common.ErrorCode; |
|
|
|
import com.iformall.common.ResultData; |
|
|
|
import com.iformall.domain.po.WxCoupon; |
|
|
|
@@ -31,7 +36,9 @@ import com.iformall.enums.EnumCouponChannelType; |
|
|
|
import com.iformall.service.WxCouponChannelService; |
|
|
|
import com.iformall.service.WxCouponService; |
|
|
|
import com.iformall.service.WxMerchantService; |
|
|
|
import com.iformall.service.util.CouponCacheUtils; |
|
|
|
import com.iformall.utils.Constant; |
|
|
|
import com.iformall.utils.RedisCacheUtils; |
|
|
|
import com.iformall.utils.RedisLock; |
|
|
|
|
|
|
|
import io.swagger.annotations.Api; |
|
|
|
@@ -55,6 +62,166 @@ public class WxCouponController extends BaseController { |
|
|
|
@Autowired |
|
|
|
@Qualifier("couponDetailRedisTemplate") |
|
|
|
RedisTemplate<String, WxCouponCVo> cdRedisTemplate; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Qualifier("objectCommonRedisTemplate") |
|
|
|
RedisTemplate<String, Object> redisTemplate; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ApiOperation("根据id(couponChannel)查询详情接口") |
|
|
|
@GetMapping("/couponDetail") |
|
|
|
@ApiImplicitParams({ |
|
|
|
@ApiImplicitParam(name = "couponChannelId", value = "couponChannelId", dataType = "String", paramType = "query", required = true)}) |
|
|
|
public ResultData couponDetail(String couponChannelId) { |
|
|
|
if ((StringUtils.isBlank(couponChannelId) || couponChannelId.equalsIgnoreCase(Constant.UNDEFINED)) |
|
|
|
) { |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "couponChannelId为空"); |
|
|
|
} |
|
|
|
Long couponChannelIdL = 0L; |
|
|
|
|
|
|
|
try { |
|
|
|
if (StringUtils.isNotBlank(couponChannelId) && !couponChannelId.equalsIgnoreCase(Constant.UNDEFINED)) { |
|
|
|
couponChannelIdL = Long.valueOf(couponChannelId); |
|
|
|
} |
|
|
|
} catch (NumberFormatException e) { |
|
|
|
logger.error("id转换失败" + couponChannelId); |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "couponChannelId转换失败" + couponChannelId); |
|
|
|
} |
|
|
|
|
|
|
|
if(couponChannelIdL <= 0){ |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "couponChannelId为空"); |
|
|
|
} |
|
|
|
|
|
|
|
WxCouponChannel couponChannel = CouponCacheUtils.getCouponChannelCache(redisTemplate, couponChannelIdL); |
|
|
|
if (null == couponChannel) { |
|
|
|
couponChannel = wxCouponChannelService.getById(couponChannelIdL,this.getTenantInfo().getTenantId()); |
|
|
|
if (null == couponChannel) { |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "couponChannelId无效" + couponChannelId); |
|
|
|
} |
|
|
|
CouponCacheUtils.setCouponChannelCache(redisTemplate, couponChannelIdL, couponChannel); |
|
|
|
} |
|
|
|
|
|
|
|
WxCoupon coupon = CouponCacheUtils.getCouponCache(redisTemplate, couponChannel.getCouponId()); |
|
|
|
if (null == coupon) { |
|
|
|
coupon = couponService.getById(couponChannel.getCouponId(),couponChannel.getTenantId()); |
|
|
|
if (null == coupon) { |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "couponChannel找不到coupon" + couponChannelId); |
|
|
|
} |
|
|
|
if(EnumCouponType.COUPON_GIFT.getCode().equals(coupon.getType())){ |
|
|
|
WxCoupon couponQ = new WxCoupon(); |
|
|
|
couponQ.updateTenantInfo(coupon); |
|
|
|
List<Long> longs = JSON.parseArray(coupon.getGiftList(), Long.class); |
|
|
|
if(longs != null && longs.size() > 0){ |
|
|
|
couponQ.setIds(longs); |
|
|
|
coupon.setGiftCouponList(couponService.list(couponQ)); |
|
|
|
} |
|
|
|
} |
|
|
|
CouponCacheUtils.setCouponCache(redisTemplate, couponChannel.getCouponId(), coupon); |
|
|
|
} |
|
|
|
updateActivityStatus(coupon,couponChannel); |
|
|
|
coupon.setCouponChannel(couponChannel); |
|
|
|
return new ResultData(coupon); |
|
|
|
} |
|
|
|
|
|
|
|
@ApiOperation("根据id(couponChannel)查询库存价格接口") |
|
|
|
@GetMapping("/couponPriceAndStock") |
|
|
|
@ApiImplicitParams({ |
|
|
|
@ApiImplicitParam(name = "couponChannelId", value = "couponChannelId", dataType = "String", paramType = "query", required = true)}) |
|
|
|
public ResultData couponPriceAndStock(String couponChannelId) { |
|
|
|
if ((StringUtils.isBlank(couponChannelId) || couponChannelId.equalsIgnoreCase(Constant.UNDEFINED)) |
|
|
|
) { |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "couponChannelId为空"); |
|
|
|
} |
|
|
|
Long couponChannelIdL = 0L; |
|
|
|
|
|
|
|
try { |
|
|
|
if (StringUtils.isNotBlank(couponChannelId) && !couponChannelId.equalsIgnoreCase(Constant.UNDEFINED)) { |
|
|
|
couponChannelIdL = Long.valueOf(couponChannelId); |
|
|
|
} |
|
|
|
} catch (NumberFormatException e) { |
|
|
|
logger.error("id转换失败" + couponChannelId); |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "couponChannelId转换失败" + couponChannelId); |
|
|
|
} |
|
|
|
|
|
|
|
if(couponChannelIdL <= 0){ |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "couponChannelId为空"); |
|
|
|
} |
|
|
|
|
|
|
|
WxCouponChannel channelPriceAndStock = wxCouponChannelService.getPriceAndStock(this.getTenantInfo(), couponChannelIdL); |
|
|
|
if (null != channelPriceAndStock.getChannelPrice() && null != channelPriceAndStock.getChannelStock()) { |
|
|
|
return new ResultData(new StringBuffer("{").append("\"salePrice\":").append(channelPriceAndStock.getChannelPrice()) |
|
|
|
.append(",\"remainInventory\":").append(channelPriceAndStock.getChannelStock()).append("}")); |
|
|
|
}else { |
|
|
|
WxCoupon coupon = couponService.getPriceAndStock(channelPriceAndStock.getCouponId(),this.getTenantInfo().getTenantId()); |
|
|
|
StringBuffer sb = new StringBuffer("{\"salePrice\":"); |
|
|
|
if (null != channelPriceAndStock.getChannelPrice()) { |
|
|
|
sb.append(channelPriceAndStock.getChannelPrice()); |
|
|
|
}else { |
|
|
|
sb.append(coupon.getSalePrice()); |
|
|
|
} |
|
|
|
sb.append(",\"remainInventory\":"); |
|
|
|
if (null != channelPriceAndStock.getChannelStock()) { |
|
|
|
sb.append(channelPriceAndStock.getChannelStock()); |
|
|
|
}else { |
|
|
|
sb.append(coupon.getRemainInventory()); |
|
|
|
} |
|
|
|
sb.append("}"); |
|
|
|
return new ResultData(sb.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ApiOperation("根据id(couponChannel)查询商品商户接口") |
|
|
|
@GetMapping("/couponMerchant") |
|
|
|
@ApiImplicitParams({ |
|
|
|
@ApiImplicitParam(name = "couponChannelId", value = "couponChannelId", dataType = "String", paramType = "query", required = true)}) |
|
|
|
public ResultData couponMerchant(String couponChannelId) { |
|
|
|
if ((StringUtils.isBlank(couponChannelId) || couponChannelId.equalsIgnoreCase(Constant.UNDEFINED)) |
|
|
|
) { |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "couponChannelId为空"); |
|
|
|
} |
|
|
|
Long couponChannelIdL = 0L; |
|
|
|
|
|
|
|
try { |
|
|
|
if (StringUtils.isNotBlank(couponChannelId) && !couponChannelId.equalsIgnoreCase(Constant.UNDEFINED)) { |
|
|
|
couponChannelIdL = Long.valueOf(couponChannelId); |
|
|
|
} |
|
|
|
} catch (NumberFormatException e) { |
|
|
|
logger.error("id转换失败" + couponChannelId); |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "couponChannelId转换失败" + couponChannelId); |
|
|
|
} |
|
|
|
|
|
|
|
if(couponChannelIdL <= 0){ |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "couponChannelId为空"); |
|
|
|
} |
|
|
|
|
|
|
|
WxCouponChannel couponChannel = CouponCacheUtils.getCouponChannelCache(redisTemplate, couponChannelIdL); |
|
|
|
if (null == couponChannel) { |
|
|
|
couponChannel = wxCouponChannelService.getById(couponChannelIdL,this.getTenantInfo().getTenantId()); |
|
|
|
if (null == couponChannel) { |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "couponChannelId无效" + couponChannelId); |
|
|
|
} |
|
|
|
CouponCacheUtils.setCouponChannelCache(redisTemplate, couponChannelIdL, couponChannel); |
|
|
|
} |
|
|
|
|
|
|
|
List<WxMerchantVo> volist = CouponCacheUtils.getCouponMerchantCache(redisTemplate, couponChannel.getId()); |
|
|
|
if (null == volist) { |
|
|
|
List<Long> couponIdList = new ArrayList<Long>(); |
|
|
|
couponIdList.add(couponChannel.getCouponId()); |
|
|
|
Map<Long, List<WxMerchantVo>> couponMerchantVoMap = merchantService.findCouponMerchantVoList(couponIdList, couponChannel,true); |
|
|
|
List<WxMerchantVo> merchantList = couponMerchantVoMap.get(couponChannel.getCouponId()); |
|
|
|
if (merchantList != null) { |
|
|
|
merchantList = merchantList.stream().filter( |
|
|
|
m->m.getMerchantStatus().equals(EnumMerchantStatus.VALID.getCode())) |
|
|
|
.collect(Collectors.toList()); |
|
|
|
} |
|
|
|
if (null != merchantList && merchantList.size() > 0 ) { |
|
|
|
CouponCacheUtils.setCouponMerchantCache(redisTemplate, couponChannel.getCouponId(), merchantList); |
|
|
|
} |
|
|
|
} |
|
|
|
return new ResultData(volist); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@ApiOperation("根据id(couponChannel)查询接口") |
|
|
|
@GetMapping("/detail") |
|
|
|
@@ -306,5 +473,21 @@ public class WxCouponController extends BaseController { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void updateActivityStatus(WxCoupon coupon,WxCouponChannel couponChannel) { |
|
|
|
if (coupon.getType().equals(EnumCouponType.COUPON_PREORDER.getCode()) || |
|
|
|
(couponChannel.getTargetAd() != null && couponChannel.getTargetAd().equals(EnumCouponChannelType.COUPON_CHANNEL_ID_TIMED.getCode())) |
|
|
|
) { |
|
|
|
Date now = new Date(); |
|
|
|
|
|
|
|
if (couponChannel.getBeginTime().getTime() > now.getTime()) { |
|
|
|
coupon.setActStatus(EnumCouponChannelActivityStatus.PREPARED.getCode()); |
|
|
|
} else if (couponChannel.getEndTime().getTime() < now.getTime()) { |
|
|
|
coupon.setActStatus(EnumCouponChannelActivityStatus.ENDED.getCode()); |
|
|
|
} else { |
|
|
|
coupon.setActStatus(EnumCouponChannelActivityStatus.STARTED.getCode()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |