| @@ -8,6 +8,7 @@ import com.iformall.common.IdWorker; | |||
| import com.iformall.domain.po.WxCampaign; | |||
| import com.iformall.domain.po.WxCoupon; | |||
| import com.iformall.domain.po.WxCouponChannel; | |||
| import com.iformall.enums.EnumCampaignStatus; | |||
| import com.iformall.enums.EnumCouponChannelStatus; | |||
| import com.iformall.enums.EnumCouponChannelType; | |||
| import com.iformall.enums.EnumCouponStatus; | |||
| @@ -25,7 +26,9 @@ import org.springframework.transaction.annotation.Propagation; | |||
| import org.springframework.transaction.annotation.Transactional; | |||
| import java.util.Date; | |||
| import java.util.HashMap; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| @Service | |||
| public class WxCampaignServiceImpl implements WxCampaignService { | |||
| @@ -64,7 +67,7 @@ public class WxCampaignServiceImpl implements WxCampaignService { | |||
| wxCampaignMapper.updateByPrimaryKeySelective(record); | |||
| } | |||
| record = wxCampaignMapper.selectByPrimaryKey(record); | |||
| if (record.getStatus() == 0) { | |||
| if (record.getStatus().equals(EnumCampaignStatus.STATUS_THROW_IN.getCode())) { | |||
| addOrUpdateBatch(JSONArray.parseArray(record.getCouponIds(),String.class), record); | |||
| } else { | |||
| delBatch(record); | |||
| @@ -72,12 +75,13 @@ public class WxCampaignServiceImpl implements WxCampaignService { | |||
| } | |||
| public void delBatch(WxCampaign record) { | |||
| WxCouponChannel wxCouponChannelQuery = new WxCouponChannel(); | |||
| wxCouponChannelQuery.setTenantId(record.getTenantId()); | |||
| wxCouponChannelQuery.setTargetAd(EnumCouponChannelType.COUPON_CHANNEL_ID_CAMPAIN.getCode()); | |||
| wxCouponChannelQuery.setSubTargetId(record.getId()); | |||
| List<WxCouponChannel> wxCouponChannels = wxCouponChannelMapper.findList(wxCouponChannelQuery); | |||
| // couponChannel 下架 | |||
| WxCouponChannel wxCouponChannelQ = new WxCouponChannel(); | |||
| wxCouponChannelQ.setTenantId(record.getTenantId()); | |||
| wxCouponChannelQ.setTargetAd(EnumCouponChannelType.COUPON_CHANNEL_ID_CAMPAIN.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()); | |||
| @@ -88,30 +92,35 @@ public class WxCampaignServiceImpl implements WxCampaignService { | |||
| } | |||
| public void addOrUpdateBatch(List<String> ids, WxCampaign record) { | |||
| // 获取已上架的CouponChannel | |||
| WxCouponChannel wxCouponChannelQ = new WxCouponChannel(); | |||
| wxCouponChannelQ.setTenantId(record.getTenantId()); | |||
| wxCouponChannelQ.setTargetAd(EnumCouponChannelType.COUPON_CHANNEL_ID_CAMPAIN.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); | |||
| } | |||
| WxCouponChannel wxCouponChannelQuery = new WxCouponChannel(); | |||
| wxCouponChannelQuery.setTenantId(record.getTenantId()); | |||
| wxCouponChannelQuery.setTargetAd(EnumCouponChannelType.COUPON_CHANNEL_ID_CAMPAIN.getCode()); | |||
| wxCouponChannelQuery.setSubTargetId(record.getId()); | |||
| List<WxCouponChannel> wxCouponChannels = wxCouponChannelMapper.findList(wxCouponChannelQuery); | |||
| // 上架 couponChannel 更新 | |||
| for (String couponIdStr:ids) { | |||
| Long couponId = Long.parseLong(couponIdStr); | |||
| saveOrUpdateCouponChannel(couponId, wxCouponChannels, record); | |||
| saveOrUpdateCouponChannel(couponId, wxCouponChannelsMap, record); | |||
| } | |||
| if (wxCouponChannels.size() > 0) | |||
| { | |||
| for (int i = 0; i < wxCouponChannels.size(); i++) { | |||
| wxCouponChannels.get(i).setStatus(EnumCouponChannelStatus.STATUS_TAKE_OFFF.getCode()); | |||
| wxCouponChannels.get(i).setUpdateDate(new Date()); | |||
| wxCouponChannelMapper.updateByPrimaryKeySelective(wxCouponChannels.get(i)); | |||
| // 下架更新 | |||
| 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, List<WxCouponChannel> wxCouponChannels, WxCampaign record){ | |||
| public void saveOrUpdateCouponChannel(Long couponId, Map<Long, WxCouponChannel> wxCouponChannelsMap, WxCampaign record){ | |||
| WxCoupon wxCoupon = wxCouponService.getById(couponId); | |||
| if (wxCoupon == null) { | |||
| @@ -124,36 +133,33 @@ public class WxCampaignServiceImpl implements WxCampaignService { | |||
| throw new MallinkException(ErrorCode.COUPON_IS_TAKE_OFF); | |||
| } | |||
| for (int i = 0; i < wxCouponChannels.size(); i++) { | |||
| if (wxCouponChannels.get(i).getCouponId().longValue() == couponId.longValue()) { | |||
| wxCouponChannels.get(i).setStatus(EnumCouponChannelStatus.STATUS_THROW_IN.getCode()); | |||
| wxCouponChannels.get(i).setBeginTime(record.getValidStartDate()); | |||
| wxCouponChannels.get(i).setEndTime(record.getValidEndDate()); | |||
| wxCouponChannels.get(i).setUpdateDate(new Date()); | |||
| wxCouponChannelMapper.updateByPrimaryKeySelective(wxCouponChannels.get(i)); | |||
| wxCouponChannels.remove(i); | |||
| return; | |||
| } | |||
| // 已有的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); | |||
| } | |||
| WxCouponChannel 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_CAMPAIN.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 | |||