|
|
|
@@ -3,12 +3,18 @@ package com.iformall.service.impl; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.iformall.common.ErrorCode; |
|
|
|
import com.iformall.domain.po.WxCoupon; |
|
|
|
import com.iformall.domain.po.WxCouponChannel; |
|
|
|
import com.iformall.domain.vo.WxCouponChannelVo; |
|
|
|
import com.iformall.enums.EnumCouponChannelStatus; |
|
|
|
import com.iformall.enums.EnumCouponChannelType; |
|
|
|
import com.iformall.enums.EnumCouponStatus; |
|
|
|
import com.iformall.enums.EnumGameStatus; |
|
|
|
import com.iformall.exception.MallinkException; |
|
|
|
import com.iformall.mapper.WxCouponChannelMapper; |
|
|
|
import com.iformall.service.WxCouponChannelService; |
|
|
|
import com.iformall.service.WxCouponService; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import java.util.*; |
|
|
|
@@ -31,10 +37,12 @@ public class WxGameServiceImpl implements WxGameService { |
|
|
|
@Autowired |
|
|
|
WxCouponChannelMapper wxCouponChannelMapper; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
WxCouponService wxCouponService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
WxCouponChannelService wxCouponChannelService; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public PageInfo<WxGame> listAsPage(WxGame record, Integer pageIndex, Integer pageSize) { |
|
|
|
return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxGameMapper.findList(record)); |
|
|
|
@@ -69,32 +77,31 @@ public class WxGameServiceImpl implements WxGameService { |
|
|
|
logger.error(e.getMessage()); |
|
|
|
return null; |
|
|
|
} |
|
|
|
// 获取已上架的CouponChannel |
|
|
|
WxCouponChannel couponChannelQ = new WxCouponChannel(); |
|
|
|
couponChannelQ.setTenantId(record.getTenantId()); |
|
|
|
couponChannelQ.setTargetAd(EnumCouponChannelType.COUPON_CHANNEL_ID_GAME.getCode()); |
|
|
|
couponChannelQ.setSubTargetId(record.getId()); |
|
|
|
couponChannelQ.setStatus(EnumCouponChannelStatus.STATUS_THROW_IN.getCode()); |
|
|
|
List<WxCouponChannelVo> wxCouponChannelVos = wxCouponChannelService.listAPI(couponChannelQ); |
|
|
|
Map<Long, WxCouponChannelVo> wxCouponChannelsMap = new HashMap<Long, WxCouponChannelVo>(); |
|
|
|
for(WxCouponChannelVo wxCouponChannelVo: wxCouponChannelVos) { |
|
|
|
wxCouponChannelsMap.put(wxCouponChannelVo.getCouponId(), wxCouponChannelVo); |
|
|
|
} |
|
|
|
|
|
|
|
// get game's coupon |
|
|
|
List couponIdsList = new ArrayList(); |
|
|
|
JSONArray couponChannelIds = JSON.parseArray(wxGame.getCouponIds()); |
|
|
|
for(int i=0; i<couponChannelIds.size();i++) { |
|
|
|
JSONObject couponChanObj = (JSONObject) couponChannelIds.get(i); |
|
|
|
Long id = couponChanObj.getLong("couponChannelId"); |
|
|
|
|
|
|
|
WxCouponChannel couponChannelQ = new WxCouponChannel(); |
|
|
|
couponChannelQ.setId(id); |
|
|
|
couponChannelQ.setTenantId(record.getTenantId()); |
|
|
|
couponChannelQ.setTargetAd(EnumCouponChannelType.COUPON_CHANNEL_ID_GAME.getCode()); |
|
|
|
couponChannelQ.setSubTargetId(wxGame.getId()); |
|
|
|
couponChannelQ.setStatus(EnumCouponChannelStatus.STATUS_THROW_IN.getCode()); |
|
|
|
try { |
|
|
|
List<WxCouponChannelVo> couponChannelVoList = wxCouponChannelService.listAPI(couponChannelQ); |
|
|
|
if (couponChannelVoList.size() > 0) { |
|
|
|
WxCouponChannelVo couponChannelVo = couponChannelVoList.get(0); |
|
|
|
if (couponChannelVo != null) { |
|
|
|
couponChanObj.put("couponId", couponChannelVo.getCouponId()); |
|
|
|
couponChanObj.put("couponName", couponChannelVo.getTitle()); |
|
|
|
couponChanObj.put("coverImg", couponChannelVo.getCoverImg()); |
|
|
|
couponIdsList.add(couponChanObj); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error(e.getMessage()); |
|
|
|
Long couponId = couponChanObj.getLong("couponId"); |
|
|
|
|
|
|
|
WxCouponChannelVo couponChannelVo = wxCouponChannelsMap.get(couponId); |
|
|
|
if (couponChannelVo != null) { |
|
|
|
couponChanObj.put("couponId", couponChannelVo.getCouponId()); |
|
|
|
couponChanObj.put("couponName", couponChannelVo.getTitle()); |
|
|
|
couponChanObj.put("coverImg", couponChannelVo.getCoverImg()); |
|
|
|
couponIdsList.add(couponChanObj); |
|
|
|
} |
|
|
|
} |
|
|
|
if (couponIdsList.size() > 0) { |
|
|
|
@@ -120,18 +127,107 @@ public class WxGameServiceImpl implements WxGameService { |
|
|
|
} else { |
|
|
|
wxGameMapper.updateByPrimaryKeySelective(record); |
|
|
|
} |
|
|
|
|
|
|
|
record = wxGameMapper.selectByPrimaryKey(record); |
|
|
|
if (record.getStatus().equals(EnumGameStatus.STATUS_THROW_IN.getCode())) { |
|
|
|
addOrUpdateCouponChannelBatch(JSONArray.parseArray(record.getCouponIds()), record); |
|
|
|
} else { |
|
|
|
delCouponChannelBatch(record); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void delCouponChannelBatch(WxGame record) { |
|
|
|
// couponChannel 下架 |
|
|
|
WxCouponChannel wxCouponChannelQ = new WxCouponChannel(); |
|
|
|
wxCouponChannelQ.setTenantId(record.getTenantId()); |
|
|
|
wxCouponChannelQ.setTargetAd(EnumCouponChannelType.COUPON_CHANNEL_ID_GAME.getCode()); |
|
|
|
wxCouponChannelQ.setSubTargetId(record.getId()); |
|
|
|
wxCouponChannelQ.setStatus(EnumCouponChannelStatus.STATUS_THROW_IN.getCode()); |
|
|
|
List<WxCouponChannel> wxCouponChannels = wxCouponChannelMapper.findList(wxCouponChannelQ); |
|
|
|
if (wxCouponChannels.size() > 0) { |
|
|
|
for (WxCouponChannel ch : wxCouponChannels) { |
|
|
|
ch.setStatus(EnumCouponChannelStatus.STATUS_TAKE_OFFF.getCode()); |
|
|
|
ch.setUpdateDate(new Date()); |
|
|
|
wxCouponChannelMapper.updateByPrimaryKeySelective(ch); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void addOrUpdateCouponChannelBatch(List couponIds, WxGame record) { |
|
|
|
// 获取已上架的CouponChannel |
|
|
|
WxCouponChannel wxCouponChannelQ = new WxCouponChannel(); |
|
|
|
wxCouponChannelQ.setTenantId(record.getTenantId()); |
|
|
|
wxCouponChannelQ.setTargetAd(EnumCouponChannelType.COUPON_CHANNEL_ID_GAME.getCode()); |
|
|
|
wxCouponChannelQ.setSubTargetId(record.getId()); |
|
|
|
wxCouponChannelQ.setStatus(EnumCouponChannelStatus.STATUS_THROW_IN.getCode()); |
|
|
|
List<WxCouponChannel> wxCouponChannels = wxCouponChannelMapper.findList(wxCouponChannelQ); |
|
|
|
Map<Long, WxCouponChannel> wxCouponChannelsMap = new HashMap<Long, WxCouponChannel>(); |
|
|
|
for(WxCouponChannel wxCouponChannel: wxCouponChannels) { |
|
|
|
wxCouponChannelsMap.put(wxCouponChannel.getCouponId(), wxCouponChannel); |
|
|
|
} |
|
|
|
|
|
|
|
// 上架 couponChannel 更新 |
|
|
|
for(int i=0; i<couponIds.size();i++) { |
|
|
|
JSONObject obj = (JSONObject)couponIds.get(i); |
|
|
|
Long couponId = obj.getLong("couponId"); |
|
|
|
int weight = obj.getIntValue("weight"); |
|
|
|
saveOrUpdateCouponChannel(couponId, wxCouponChannelsMap, record); |
|
|
|
} |
|
|
|
|
|
|
|
// 下架更新 |
|
|
|
if (!wxCouponChannelsMap.isEmpty()) { |
|
|
|
for (WxCouponChannel wxCouponChannel : wxCouponChannelsMap.values()) { |
|
|
|
wxCouponChannel.setStatus(EnumCouponChannelStatus.STATUS_TAKE_OFFF.getCode()); |
|
|
|
wxCouponChannel.setUpdateDate(new Date()); |
|
|
|
wxCouponChannelMapper.updateByPrimaryKeySelective(wxCouponChannel); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void saveOrUpdateCouponChannel(Long couponId, Map<Long, WxCouponChannel> wxCouponChannelsMap, WxGame record){ |
|
|
|
|
|
|
|
WxCoupon wxCoupon = wxCouponService.getById(couponId); |
|
|
|
if (wxCoupon == null) { |
|
|
|
throw new MallinkException(ErrorCode.COUPON_IS_EMPTY); |
|
|
|
} |
|
|
|
if (wxCoupon.getStatus() != EnumCouponStatus.COUPON_STATUS_THROW_IN.getCode()) { |
|
|
|
throw new MallinkException(ErrorCode.COUPON_IS_TAKE_OFF); |
|
|
|
} |
|
|
|
if (wxCoupon.getValidEndDate() != null && wxCoupon.getValidEndDate().before(new Date())) { |
|
|
|
throw new MallinkException(ErrorCode.COUPON_IS_TAKE_OFF); |
|
|
|
} |
|
|
|
|
|
|
|
// 更新已有的couponChannel |
|
|
|
WxCouponChannel wxCouponChannel = wxCouponChannelsMap.get(couponId); |
|
|
|
if (wxCouponChannel != null) { |
|
|
|
wxCouponChannel.setStatus(EnumCouponChannelStatus.STATUS_THROW_IN.getCode()); |
|
|
|
wxCouponChannel.setBeginTime(record.getValidStartDate()); |
|
|
|
wxCouponChannel.setEndTime(record.getValidEndDate()); |
|
|
|
wxCouponChannel.setUpdateDate(new Date()); |
|
|
|
wxCouponChannelMapper.updateByPrimaryKeySelective(wxCouponChannel); |
|
|
|
wxCouponChannelsMap.remove(couponId); |
|
|
|
} else { |
|
|
|
// 不存在的新增 |
|
|
|
wxCouponChannel = new WxCouponChannel(); |
|
|
|
wxCouponChannel.setBeginTime(record.getValidStartDate()); |
|
|
|
wxCouponChannel.setEndTime(record.getValidEndDate()); |
|
|
|
wxCouponChannel.setStatus(EnumCouponChannelStatus.STATUS_THROW_IN.getCode()); |
|
|
|
wxCouponChannel.setCouponId(couponId); |
|
|
|
wxCouponChannel.setMerchantId(wxCoupon.getMerchantId()); |
|
|
|
wxCouponChannel.setType(wxCoupon.getType()); |
|
|
|
wxCouponChannel.setTargetAd(EnumCouponChannelType.COUPON_CHANNEL_ID_GAME.getCode()); |
|
|
|
wxCouponChannel.setTenantId(wxCoupon.getTenantId()); |
|
|
|
wxCouponChannel.setBusiness(wxCoupon.getBusiness()); |
|
|
|
wxCouponChannel.setTitle(wxCoupon.getTitle()); |
|
|
|
wxCouponChannel.setSubTargetId(record.getId()); |
|
|
|
final IdWorker idWorker = IdWorker.get(); |
|
|
|
wxCouponChannel.setId(idWorker.nextId()); |
|
|
|
wxCouponChannelMapper.insertSelective(wxCouponChannel); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void deleteById(Long id) { |
|
|
|
wxGameMapper.deleteByPrimaryKey(id); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |