yujian
2019-12-19 626d711cb15896055c13fe344eb7fcc824589715
fanli/src/main/java/com/yeshi/fanli/service/impl/homemodule/SpecialServiceImpl.java
@@ -5,7 +5,9 @@
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.annotation.Resource;
@@ -19,14 +21,19 @@
import com.yeshi.fanli.dao.mybatis.homemodule.SpecialMapper;
import com.yeshi.fanli.dto.common.CommonContentTypeEnum;
import com.yeshi.fanli.entity.AppVersionInfo;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.homemodule.AdActivityVersionControl;
import com.yeshi.fanli.entity.bus.homemodule.AdActivityVersionControl.AdActivityType;
import com.yeshi.fanli.entity.bus.homemodule.Special;
import com.yeshi.fanli.entity.common.JumpDetailV2;
import com.yeshi.fanli.exception.banner.SwiperPictureException;
import com.yeshi.fanli.exception.homemodule.HomeNavbarException;
import com.yeshi.fanli.exception.homemodule.SpecialException;
import com.yeshi.fanli.service.inter.common.JumpDetailV2Service;
import com.yeshi.fanli.service.inter.config.AppVersionService;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.homemodule.AdActivityVersionControlService;
import com.yeshi.fanli.service.inter.homemodule.SpecialCardService;
import com.yeshi.fanli.service.inter.homemodule.SpecialPlaceService;
import com.yeshi.fanli.service.inter.homemodule.SpecialService;
@@ -56,6 +63,9 @@
   @Resource
   private AppVersionService appVersionService;
   @Resource
   private AdActivityVersionControlService adActivityVersionControlService;
   @Override
   public Special selectByPrimaryKey(Long id) {
@@ -87,7 +97,7 @@
      } else if (!StringUtil.isJson(params)) {
         throw new SpecialException(1, "跳转参数非JSON格式");
      }
      String startTime_str = record.getStartTime_str();
      if (record.isTimeTask() && (startTime_str == null || startTime_str.length() == 0)) {
         throw new SpecialException(1, "控制时间不能为空");
@@ -99,7 +109,7 @@
            record.setJumpDetail(listByType.get(0));
         }
      }
      // 时间转换
      conversionTime(record);
@@ -210,18 +220,19 @@
   /**
    * web段时间转换
    *
    * @param record
    */
   public void conversionTime(Special record) throws SpecialException, Exception {
      // 是否时间控制
      if(!record.isTimeTask()) {
      if (!record.isTimeTask()) {
         record.setStartTime(null);
         record.setEndTime(null);
      } else {
         String startTime_str = record.getStartTime_str();
         String endTime_str = record.getEndTime_str();
         if ((startTime_str == null|| startTime_str.trim().length() == 0)
         if ((startTime_str == null || startTime_str.trim().length() == 0)
               && (endTime_str == null || endTime_str.trim().length() == 0)) {
            throw new HomeNavbarException(1, "请输入控制时间");
         } else {
@@ -230,7 +241,7 @@
               startTime_str = startTime_str.replaceAll("T", " ");
               record.setStartTime(format.parse(startTime_str));
            }
            if (endTime_str != null && endTime_str.trim().length() > 0) {
               endTime_str = endTime_str.replaceAll("T", " ");
               record.setEndTime(format.parse(endTime_str));
@@ -238,7 +249,7 @@
         }
      }
   }
   /**
    * 上传图片
    * 
@@ -371,32 +382,31 @@
      // 跳转链接
      for (Special special : list) {
         Date startTime = special.getStartTime();
         Date endTime = special.getEndTime();
         if (startTime == null && endTime == null) {
            special.setTimeTask(false);
            special.setStartTime_str("");
            special.setEndTime_str("");
         } else {
            special.setTimeTask(true);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
            if (startTime == null) {
               special.setStartTime_str("");
            } else {
               special.setStartTime_str(sdf.format(startTime));
            }
            if (endTime == null) {
               special.setEndTime_str("");
            } else {
               special.setEndTime_str(sdf.format(endTime));
            }
         }
         String params = special.getParams();
         if (StringUtil.isNullOrEmpty(params)) {
            special.setParams("");
@@ -442,6 +452,37 @@
      return specialMapper.listByVersion(start, count, card, platform, versionCode);
   }
   /**
    * 专题版本过滤
    *
    * @param list
    * @param platform
    * @param versionCode
    */
   private void filterSpecial(List<Special> list, String platform, int versionCode) {
      if (list == null || list.size() == 0)
         return;
      AppVersionInfo app = appVersionService.getClientVersion(platform, versionCode);
      if (app == null) {
         list.clear();
         return;
      }
      List<Long> versionIdList = new ArrayList<>();
      versionIdList.add(app.getId());
      List<Long> sourceIdList = new ArrayList<>();
      for (Special special : list) {
         sourceIdList.add(special.getId());
      }
      Set<Long> sourceIds = adActivityVersionControlService.filterSourceIdByVersion(sourceIdList,
            AdActivityType.special, versionIdList);
      for (int i = 0; i < list.size(); i++) {
         if (!sourceIds.contains(list.get(i).getId())) {
            list.remove(i--);
         }
      }
   }
   @Override
   @Cacheable(value = "specialCache", key = "'listCacheSpecialToIndex'+#acceptData.platform+'-'+#acceptData.version+'-'+#sex")
   public JSONObject listCacheSpecialToIndex(AcceptData acceptData, Integer sex) throws Exception {
@@ -459,10 +500,12 @@
      else if (VersionUtil.greaterThan_1_5_60(acceptData.getPlatform(), acceptData.getVersion()))
         indexArc = "index_arc_1.5.6";
      int platformCode = Constant.getPlatformCode(acceptData.getPlatform());
      String platform = acceptData.getPlatform();
      int platformCode = Constant.getPlatformCode(platform);
      int version = Integer.parseInt(acceptData.getVersion());
      List<Special> listArc = specialMapper.listByPlaceKey(indexArc, sex, platformCode, version);
      filterSpecial(listArc, platform, version);
      if (listArc == null) {
         listArc = new ArrayList<Special>();
      } else if (listArc.size() > 0) {
@@ -513,6 +556,8 @@
         listActivity = specialMapper.listByPlaceKey("index_activity", null, platformCode, version);
      }
      filterSpecial(listActivity, platform, version);
      if (listActivity == null) {
         listActivity = new ArrayList<Special>();
      } else if (listActivity.size() > 0) {
@@ -534,6 +579,7 @@
      JSONObject blockJsonMap = new JSONObject();
      List<Special> listBlock = specialMapper.listByPlaceKey(indexBlock, sex, platformCode, version);
      filterSpecial(listBlock, platform, version);
      if (listBlock == null) {
         listBlock = new ArrayList<Special>();
      } else if (listBlock.size() > 0) {
@@ -570,26 +616,24 @@
   public List<Special> listByPlaceKey(String placeKey, Integer platform, Integer versionCode) {
      return specialMapper.listByPlaceKey(placeKey, null, platform, versionCode);
   }
   @Override
   public List<Special> listByPlaceKeyList(long start, int count, List<String> list, Integer platform, Integer versionCode) {
   public List<Special> listByPlaceKeyList(long start, int count, List<String> list, Integer platform,
         Integer versionCode) {
      return specialMapper.listByPlaceKeyList(start, count, list, null, platform, versionCode);
   }
   @Override
   public long countByPlaceKeyList(List<String> list, Integer platform, Integer versionCode) {
      return specialMapper.countByPlaceKeyList(list, null, platform, versionCode);
   }
   @Override
   @Cacheable(value = "specialCache", key = "'listByPlaceKeyHasLabel'+#start+'-'+#platform+'-'+#versionCode+'-'+#list")
   public List<Special> listByPlaceKeyHasLabel(long start, int count, List<String> list, Integer platform, Integer versionCode) {
   public List<Special> listByPlaceKeyHasLabel(long start, int count, List<String> list, Integer platform,
         Integer versionCode) {
      return specialMapper.listByPlaceKeyHasLabel(start, count, list, null, platform, versionCode);
   }
   /**
    * 处理 数据
@@ -608,4 +652,53 @@
         }
      }
   }
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void setVersions(Long specialId, List<Long> versions) throws Exception {
      Special special = selectByPrimaryKey(specialId);
      if (special == null) {
         throw new Exception("专题不存在");
      }
      Set<Long> oldSet = new HashSet<>();
      List<AdActivityVersionControl> versionList = adActivityVersionControlService
            .listByTypeAndSourceId(AdActivityType.special, specialId);
      if (versionList != null) {
         for (AdActivityVersionControl control : versionList)
            oldSet.add(control.getVersion().getId());
      }
      Set<Long> newSet = new HashSet<>();
      for (Long version : versions) {
         newSet.add(version);
      }
      Set<Long> delSet = new HashSet<>();
      delSet.addAll(oldSet);
      delSet.removeAll(newSet);
      for (Long versionId : delSet) {
         adActivityVersionControlService.deleteBySourceAndVersion(specialId, AdActivityType.special, versionId);
      }
      Set<Long> addSet = new HashSet<>();
      addSet.addAll(newSet);
      addSet.removeAll(oldSet);
      // 添加映射
      for (Long versionId : addSet) {
         AdActivityVersionControl control = new AdActivityVersionControl();
         control.setCreateTime(new Date());
         control.setSourceId(special.getId());
         control.setType(AdActivityType.special);
         control.setVersion(new AppVersionInfo(versionId));
         try {
            adActivityVersionControlService.addVersionControl(control);
         } catch (Exception e) {
            throw new SwiperPictureException(2, e.getMessage());
         }
      }
   }
}