yujian
2020-05-23 28cf328a098334b51a3e9d2d56f983fb8c862211
fanli/src/main/java/com/yeshi/fanli/service/impl/homemodule/SpecialServiceImpl.java
@@ -5,11 +5,14 @@
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;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -19,20 +22,30 @@
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.CommonShareInfo;
import com.yeshi.fanli.entity.bus.homemodule.CommonShareInfo.CommonShareInfoEnum;
import com.yeshi.fanli.entity.bus.homemodule.Special;
import com.yeshi.fanli.entity.common.JumpDetailV2;
import com.yeshi.fanli.exception.homemodule.HomeNavbarException;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.exception.banner.SwiperPictureException;
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.CommonShareInfoService;
import com.yeshi.fanli.service.inter.homemodule.SpecialCardService;
import com.yeshi.fanli.service.inter.homemodule.SpecialPlaceService;
import com.yeshi.fanli.service.inter.homemodule.SpecialService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.FilePathEnum;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.VersionUtil;
import com.yeshi.fanli.vo.homemodule.SpecialVO;
import net.sf.json.JSONObject;
@@ -49,6 +62,9 @@
   private SpecialCardService specialCardService;
   @Resource
   private CommonShareInfoService commonShareInfoService;
   @Resource
   private JumpDetailV2Service jumpDetailV2Service;
   @Resource
@@ -57,6 +73,9 @@
   @Resource
   private AppVersionService appVersionService;
   @Resource
   private AdActivityVersionControlService adActivityVersionControlService;
   @Override
   public Special selectByPrimaryKey(Long id) {
      return specialMapper.selectByPrimaryKey(id);
@@ -64,7 +83,7 @@
   @Override
   public void saveObject(MultipartFile file, MultipartFile file2, MultipartFile file3, Special record,
         String jumpType) throws SpecialException, Exception {
         String jumpType, CommonShareInfo shareInfo) throws SpecialException, Exception {
      Long cardId = record.getCardId();
      if (cardId == null) {
@@ -86,11 +105,8 @@
         record.setParams(null);
      } 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, "控制时间不能为空");
      } else {
         record.setParams(params.trim());
      }
      if (!StringUtil.isNullOrEmpty(jumpType)) {
@@ -99,9 +115,9 @@
            record.setJumpDetail(listByType.get(0));
         }
      }
      // 时间转换
      conversionTime(record);
      handleTime(record);
      Long state = record.getState();
      if (state == null) {
@@ -141,6 +157,8 @@
         record.setUpdatetime(new Date());
         specialMapper.insert(record);
         shareInfo.setPid(record.getId());
         Special s = new Special();
         s.setId(record.getId());
         s.setOrderby(Integer.parseInt(s.getId() + ""));
@@ -205,40 +223,41 @@
         record.setUpdatetime(new Date());
         specialMapper.updateByPrimaryKey(record);
         shareInfo.setPid(record.getId());
      }
      // 添加补充信息
      shareInfo.setType(CommonShareInfoEnum.special);
      commonShareInfoService.save(shareInfo);
   }
   /**
    * web段时间转换
    *
    * @param record
    */
   public void conversionTime(Special record) throws SpecialException, Exception {
      // 是否时间控制
      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)
               && (endTime_str == null || endTime_str.trim().length() == 0)) {
            throw new HomeNavbarException(1, "请输入控制时间");
         } else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            if (startTime_str != null && startTime_str.trim().length() > 0) {
               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));
            }
         }
   private void handleTime(Special record) throws SpecialException, Exception {
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
      String startTime_str = record.getStartTime_str();
      if (!StringUtil.isNullOrEmpty(startTime_str)) {
         startTime_str = startTime_str.replaceAll("T", " ");
         record.setStartTime(format.parse(startTime_str));
      }
      String endTime_str = record.getEndTime_str();
      if (!StringUtil.isNullOrEmpty(endTime_str)) {
         endTime_str = endTime_str.replaceAll("T", " ");
         record.setEndTime(format.parse(endTime_str));
      }
      if (record.getEndTime() != null && record.getStartTime() != null
            && record.getStartTime().getTime() > record.getEndTime().getTime()) {
         throw new SpecialException(1, "起始时间不能小于结束时间");
      }
   }
   /**
    * 上传图片
    * 
@@ -254,7 +273,7 @@
      String type = contentType.substring(contentType.indexOf("/") + 1);
      // 文件路径
      String filePath = "/img/special/" + UUID.randomUUID().toString().replace("-", "") + "." + type;
      String filePath = FilePathEnum.special.getPath() + UUID.randomUUID().toString().replace("-", "") + "." + type;
      // 执行上传
      String fileLink = COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
@@ -274,7 +293,7 @@
   }
   @Override
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   public void updateOrder(Long id, Integer moveType, Integer sex) throws SpecialException {
      if (id == null || moveType == null || (!moveType.equals(1) && !moveType.equals(-1)) || sex == null) {
@@ -326,7 +345,7 @@
   }
   @Override
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   public int deleteBatchByPrimaryKey(List<Long> list) throws Exception {
      List<Special> listSpecial = specialMapper.queryByListPrimaryKey(list);
      for (Special special : listSpecial) {
@@ -339,7 +358,18 @@
         if (!StringUtil.isNullOrEmpty(subPicture)) {
            COSManager.getInstance().deleteFile(subPicture);
         }
         commonShareInfoService.deleteByPidAndType(special.getId(), CommonShareInfoEnum.special.name());
      }
      if (listSpecial != null)
         for (Special special : listSpecial) {
            List<AdActivityVersionControl> versionList = adActivityVersionControlService
                  .listByTypeAndSourceId(AdActivityType.special, special.getId());
            if (versionList != null)
               for (AdActivityVersionControl control : versionList)
                  adActivityVersionControlService.deleteByPrimaryKey(control.getId());
         }
      return specialMapper.deleteBatchByPrimaryKey(list);
   }
@@ -356,67 +386,92 @@
         if (!StringUtil.isNullOrEmpty(subPicture)) {
            COSManager.getInstance().deleteFile(subPicture);
         }
         commonShareInfoService.deleteByPidAndType(special.getId(), CommonShareInfoEnum.special.name());
      }
      return specialMapper.deleteBatchByCardID(list);
   }
   @Override
   public List<Special> listQueryByCard(long start, int count, Long card, String key, Integer sex) {
   public List<SpecialVO> listQueryByCard(long start, int count, Long card, String key, Integer sex) {
      List<Special> list = specialMapper.listQueryByCard(start, count, card, key, sex);
      if (list == null || list.size() == 0) {
         return list;
      if (list == null) {
         return null;
      }
      // 跳转链接
       List<SpecialVO> listvo = new ArrayList<SpecialVO>();
      for (Special special : list) {
         SpecialVO specialVO = new SpecialVO();
         try {
            PropertyUtils.copyProperties(specialVO, special);
         } catch (Exception e) {
            e.printStackTrace();
            continue;
         }
         
         Date startTime = special.getStartTime();
         Date endTime = special.getEndTime();
         if (startTime == null && endTime == null) {
            special.setTimeTask(false);
            special.setStartTime_str("");
            special.setEndTime_str("");
         if (specialVO.getState() != null && specialVO.getState().longValue() == 0) {
            specialVO.setState(1L);
         } else {
            special.setTimeTask(true);
            specialVO.setState(0L);
         }
         Date startTime = specialVO.getStartTime();
         Date endTime = specialVO.getEndTime();
         if (startTime == null && endTime == null) {
            specialVO.setStartTime_str("");
            specialVO.setEndTime_str("");
         } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
            if (startTime == null) {
               special.setStartTime_str("");
               specialVO.setStartTime_str("");
            } else {
               special.setStartTime_str(sdf.format(startTime));
               specialVO.setStartTime_str(sdf.format(startTime));
            }
            if (endTime == null) {
               special.setEndTime_str("");
               specialVO.setEndTime_str("");
            } else {
               special.setEndTime_str(sdf.format(endTime));
               specialVO.setEndTime_str(sdf.format(endTime));
            }
         }
         String params = special.getParams();
         String params = specialVO.getParams();
         if (StringUtil.isNullOrEmpty(params)) {
            special.setParams("");
            specialVO.setParams("");
         }
         String remark = special.getRemark();
         String remark = specialVO.getRemark();
         if (StringUtil.isNullOrEmpty(remark)) {
            special.setRemark("");
            specialVO.setRemark("");
         }
         JumpDetailV2 jumpDetail = special.getJumpDetail();
         JumpDetailV2 jumpDetail = specialVO.getJumpDetail();
         if (jumpDetail == null) {
            // 默认未选择
            JumpDetailV2 jumpDetailV2 = new JumpDetailV2();
            jumpDetailV2.setName("-未选择-");
            jumpDetailV2.setType("default");
            special.setJumpDetail(jumpDetailV2);
            specialVO.setJumpDetail(jumpDetailV2);
         }
         // 分享补充信息
         CommonShareInfo info = commonShareInfoService.getByPidAndType(specialVO.getId(),
               CommonShareInfoEnum.special.name());
         if (info == null) {
            specialVO.setNeedSpin(false);
            specialVO.setComment("");
         } else {
            specialVO.setNeedSpin(info.getNeedSpin());
            if (StringUtil.isNullOrEmpty(info.getComment())) {
               specialVO.setComment("");
            } else {
               specialVO.setComment(info.getComment());
            }
         }
         listvo.add(specialVO);
      }
      return list;
      return listvo;
   }
   @Override
@@ -426,20 +481,55 @@
   @Override
   @Cacheable(value = "specialCache", key = "'listBySystemAndCard-'+#card+'-'+#systemId")
   public List<Special> listBySystemAndCard(String card, Long systemId) {
      return specialMapper.listBySystemAndCard(card, systemId);
   public List<SpecialVO> listBySystemAndCard(String card, Long systemId) {
      List<Special> specialList = specialMapper.listBySystemAndCard(card, systemId);
      return transformVO(specialList);
   }
   @Override
   @Cacheable(value = "specialCache", key = "'listPageBySystemAndCard-'+#start+'-'+#count+'-'+#card+'-'+#systemId")
   public List<Special> listPageBySystemAndCard(long start, int count, String card, Long systemId) {
      return specialMapper.listPageBySystemAndCard(start, count, card, systemId);
   public List<SpecialVO> listPageBySystemAndCard(long start, int count, String card, Long systemId) {
      List<Special> specialList = specialMapper.listPageBySystemAndCard(start, count, card, systemId);
      return transformVO(specialList);
   }
   @Override
   @Cacheable(value = "specialCache", key = "'listByVersion-'+#start+'-'+#count+'-'+#card+'-'+#platform+'-'+#versionCode")
   public List<Special> listByVersion(long start, int count, String card, Integer platform, Integer versionCode) {
      return specialMapper.listByVersion(start, count, card, platform, versionCode);
   public List<SpecialVO> listByVersion(long start, int count, String card, String platform, Integer versionCode) {
      List<Special> specialList = specialMapper.listByPlaceKey(card, null, null, null);
      filterSpecial(specialList, platform, versionCode);
      return transformVO(specialList);
   }
   /**
    * 专题版本过滤
    *
    * @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
@@ -459,10 +549,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) {
@@ -488,7 +580,7 @@
               if (hour < 6) {// 0-6点之间不进入自购立减的页面
                  s.setJumpDetail(jumpDetailV2Service.getByTypeCache("web"));
                  JSONObject params = new JSONObject();
                  params.put("url", configService.get("tlj_own_buy_nogoods"));
                  params.put("url", configService.get(ConfigKeyEnum.tljOwnBuyNogoods.getKey()));
                  s.setParams(params.toString());
               } else {
                  // 查询专题详情
@@ -497,11 +589,17 @@
                  s.setParams(special.getParams());
               }
            }
            if (s.getJumpDetail() != null) {
               s.getJumpDetail().setNeedLogin(s.isJumpLogin()
                     | (s.getJumpDetail().getNeedLogin() != null && s.getJumpDetail().getNeedLogin()));
            }
         }
      // 数据处理
      handlelist(listArc);
      arcMap.put("list", JsonUtil.getApiCommonGson().toJson(listArc));
//      handlelist(listArc);
      arcMap.put("list", JsonUtil.getApiCommonGson().toJson(transformVO(listArc)));
      // 首页-活动区域(女王节)
      JSONObject activityMap = new JSONObject();
@@ -512,6 +610,8 @@
      } else {
         listActivity = specialMapper.listByPlaceKey("index_activity", null, platformCode, version);
      }
      filterSpecial(listActivity, platform, version);
      if (listActivity == null) {
         listActivity = new ArrayList<Special>();
@@ -525,7 +625,7 @@
      handlelist(listActivity);
      activityMap.put("list", JsonUtil.getApiCommonGson().toJson(listActivity));
      activityMap.put("list", JsonUtil.getApiCommonGson().toJson(transformVO(listActivity)));
      // 首页-方形专题(品牌券、母婴...)
      String indexBlock = "index_block";
@@ -534,6 +634,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) {
@@ -557,7 +658,7 @@
      // 数据处理
      handlelist(listBlock);
      blockJsonMap.put("list", JsonUtil.getApiCommonGson().toJson(listBlock));
      blockJsonMap.put("list", JsonUtil.getApiCommonGson().toJson(transformVO(listBlock)));
      JSONObject root = new JSONObject();
      root.put("arcArea", arcMap);
@@ -570,6 +671,67 @@
   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) {
      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<SpecialVO> listByPlaceKeyHasLabel(long start, int count, List<String> list, Integer platform,
         Integer versionCode) {
      List<Special> listSpecial =  specialMapper.listByPlaceKeyHasLabel(start, count, list, null,
            platform, versionCode);
      return    transformVO(listSpecial);
   }
   /**
    * 转换VO
    * @param list
    * @return
    */
   private List<SpecialVO> transformVO(List<Special> list) {
      List<SpecialVO> listVo = new ArrayList<SpecialVO>();
      if (list != null && list.size() > 0) {
         for (Special special: list) {
            SpecialVO specialVO = new SpecialVO();
            try {
               PropertyUtils.copyProperties(specialVO, special);
            } catch (Exception e) {
               e.printStackTrace();
               continue;
            }
            // 跳转活动过渡页
            CommonShareInfo info = commonShareInfoService.getByPidAndType(specialVO.getId(),
                  CommonShareInfoEnum.special.name());
            if (info != null && !StringUtil.isNullOrEmpty(info.getComment())) {
               String link = configService.get(ConfigKeyEnum.activityDetailLink.getKey()) + "?type=%s&id=%s";
               link = String.format(link, CommonShareInfoEnum.special.name(),specialVO.getId());
               JSONObject params = new JSONObject();
               params.put("url", link);
               specialVO.setComment(info.getComment());
               specialVO.setParams(params.toString());
               specialVO.setJumpDetail(jumpDetailV2Service.getByTypeCache("web"));
            }
            listVo.add(specialVO);
         }
      }
      return listVo;
   }
   /**
    * 处理 数据
@@ -588,4 +750,79 @@
         }
      }
   }
   @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());
         }
      }
   }
   @Override
   public void switchState(Long id) throws SpecialException {
      if (id == null) {
         throw new SpecialException(1, "请传递正确参数");
      }
      Special resultObj = specialMapper.selectByPrimaryKey(id);
      if (resultObj == null) {
         throw new SpecialException(1, "此内容已不存在");
      }
      Long state = resultObj.getState();
      if (state == null || state == 0) {
         state = 1L;
      } else {
         state = 0L;
      }
      Special updateObj = new Special();
      updateObj.setId(id);
      updateObj.setState(state);
      updateObj.setJumpLogin(resultObj.isJumpLogin());
      specialMapper.updateByPrimaryKeySelective(updateObj);
   }
}