admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
fanli/src/main/java/com/yeshi/fanli/service/impl/push/PushCouponServiceImpl.java
@@ -1,24 +1,27 @@
package com.yeshi.fanli.service.impl.push;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yeshi.fanli.dao.mybatis.push.PushCouponMapper;
import com.yeshi.fanli.entity.bus.user.UserSystemCoupon;
import com.yeshi.fanli.entity.push.PushCoupon;
import com.yeshi.fanli.exception.PushException;
import com.yeshi.fanli.entity.push.PushCouponRecord;
import com.yeshi.fanli.exception.push.PushCouponException;
import com.yeshi.fanli.exception.push.PushException;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.push.PushCouponRecordService;
import com.yeshi.fanli.service.inter.push.PushCouponService;
import com.yeshi.fanli.service.inter.push.PushService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
import com.yeshi.fanli.util.StringUtil;
import net.sf.json.JSONObject;
@@ -36,6 +39,9 @@
   
   @Resource
   private PushCouponRecordService pushCouponRecordService;
   @Resource
   private UserSystemCouponService userSystemCouponService;
   
   @Override
   public int deleteByPrimaryKey(Long id) {
@@ -79,24 +85,17 @@
   }
   @Override
   @Transactional
   public void save(PushCoupon record) throws PushCouponException {
   @Transactional(rollbackFor=Exception.class)
   public void save(PushCoupon record) throws PushCouponException,Exception {
      if (record == null) {
         throw new PushCouponException(1, "参数不正确");
      }
      String title = record.getTitle();
      String content = record.getContent();
      if (title == null || title.trim().length() == 0 || content == null || content.trim().length() == 0) {
         throw new PushCouponException(1, "标题或内容不能为空");
      String uids = record.getUids();
      if (StringUtil.isNullOrEmpty(uids)) {
         throw new PushCouponException(1, "发放用户不能为空");
      }
      Date startTime = record.getStartTime();
      Date endTime = record.getEndTime();
      if (startTime == null || endTime == null) {
         throw new PushCouponException(1, "有效期时间不能为空");
      }
      Integer amount = record.getAmount();
      if (amount == null) {
         throw new PushCouponException(1, "券数量不能为空");
@@ -104,48 +103,57 @@
         throw new PushCouponException(1, "券数量范围是 1~5");
      }
      
      BigDecimal percent = record.getPercent();
      if (percent != null && (percent.compareTo(new BigDecimal(60)) > 0
            || percent.compareTo(new BigDecimal(1)) < 0)) {
         throw new PushCouponException(1, "券比例范围是1~60 的正整数");
      }
      Long id = record.getId();
      if (id == null) {
         // 新增
         record.setPushed(false);
         record.setCreateTime(new Date());
         record.setUpdateTime(new Date());
         // 插入数据
         insert(record);
      } else {
         // 修改
         PushCoupon current = selectByPrimaryKey(id);
         if (current == null) {
            throw new PushCouponException(1, "该记录已不存在");
         pushCouponMapper.insert(record);
         String[] uidArray = uids.split(",");
         if (uidArray != null) {
            for (int i = 0; i < uidArray.length; i++) {
               Long uid = Long.parseLong(uidArray[i]);
               // 插入记录
               PushCouponRecord pushCouponRecord = new PushCouponRecord();
               pushCouponRecord.setPushCoupon(record);
               pushCouponRecord.setUid(uid);
               pushCouponRecord.setCreateTime(new Date());
               // 插入领取记录
               //pushCouponRecordService.insertSelective(pushCouponRecord);
               // 直接发送券
               //userSystemCouponService.randomRewardCoupon(amount, uid, UserSystemCoupon.SOURCE_SYSTEM_PUSH);
            }
         }
         
         if(current.isPushed()) {
            throw new PushCouponException(1, "已推送的信息不能修改");
         }
         current.setTitle(title);
         current.setContent(content);
         current.setAmount(amount);
         current.setStartTime(startTime);
         current.setEndTime(endTime);
         current.setUids(record.getUids());
         current.setRemark(record.getRemark());
         // 更新数据
         updateByPrimaryKey(current);
      } else {
//
//         // 修改
//         PushCoupon current = selectByPrimaryKey(id);
//         if (current == null) {
//            throw new PushCouponException(1, "该记录已不存在");
//         }
//
//         if(current.isPushed()) {
//            throw new PushCouponException(1, "已推送的信息不能修改");
//         }
//         current.setAmount(amount);
//         current.setUids(record.getUids());
//         current.setRemark(record.getRemark());
//         current.setUpdateTime(new Date());
//         // 更新数据
//         updateByPrimaryKey(current);
      }
   }
   @Override
   @Transactional
   @Transactional(rollbackFor=Exception.class)
   public void executePush(Long id) throws Exception, PushCouponException, PushException {
      PushCoupon pushCoupon = selectByPrimaryKey(id);