/**
  * 处理不同的支付类型
  *
  * @param dealerBuyService
  * @param oldDealerBuyService
  * @author 周光暖
  */
 private void handChargType(
     DealerBuyService dealerBuyService, DealerBuyService oldDealerBuyService) {
   short chargType = dealerBuyService.getChargType();
   if (DealerConstant.DealerBuySerLog.CHARGE_TYPE_RENEW == chargType) // 续期
   {
     WebServiceItems webServiceItems =
         webServiceItemsService.selectByPrimaryKey(dealerBuyService.getServiceId());
     if (null == webServiceItems) {
       throw new IllegalArgumentException("网站服务不存在");
     }
     Long beginTime = dealerBuyService.getBeginTime();
     Long endTime = 0L;
     Double buyNum = 1D;
     if (webServiceItems.getRefrenceId().equals("S005")) {
       buyNum = 30D * 2D;
       endTime = new DateTime(beginTime).plusDays(buyNum.intValue()).toDate().getTime();
     } else {
       endTime = new DateTime(beginTime).plusYears(buyNum.intValue()).toDate().getTime();
     }
     if (webServiceItems.getPrice() != BigDecimal.ZERO) {
       buyNum = dealerBuyService.getBuyMoney().divide(webServiceItems.getPrice(), 2).doubleValue();
       if (buyNum.intValue() < 1) {
         throw new IllegalArgumentException("购买数量不能小于1");
       }
       endTime = new DateTime(beginTime).plusYears(buyNum.intValue()).toDate().getTime();
     }
     if (null == beginTime) {
       throw new IllegalArgumentException("服务开始时间不能为空");
     }
     if (null != oldDealerBuyService) {
       Long oldEndTime =
           null == oldDealerBuyService.getEndTime() ? 0l : oldDealerBuyService.getEndTime();
       if (com.zttx.web.utils.CalendarUtils.getCurrentLong() < oldEndTime) // 服务未过期
       {
         endTime = new DateTime(oldEndTime).plusYears(buyNum.intValue()).toDate().getTime();
         beginTime = oldDealerBuyService.getBeginTime();
       } else {
         endTime =
             new DateTime(com.zttx.web.utils.CalendarUtils.getCurrentLong())
                 .plusYears(buyNum.intValue())
                 .toDate()
                 .getTime();
         beginTime = oldDealerBuyService.getBeginTime();
       }
     }
     dealerBuyService.setEndTime(endTime);
     dealerBuyService.setBeginTime(beginTime);
   } else if (DealerConstant.DealerBuySerLog.CHARGE_TYPE_RAISE == chargType) // 增加数量
   {
     dealerBuyService.setBeginTime(null);
     dealerBuyService.setEndTime(null);
   }
 }
 /**
  * @param dealerId
  * @throws BusinessException
  */
 @Override
 public void addTrialErpDealerBuyService(String dealerId) throws BusinessException {
   Map<String, Object> params = Maps.newHashMap();
   DealerInfo dealerInfo = dealerInfoService.selectByPrimaryKey(dealerId);
   UserInfo dealerUserm = userInfoService.selectByPrimaryKey(dealerId);
   params.put("dealerId", dealerId);
   params.put("dealerName", dealerInfo.getDealerName());
   params.put("dealerMobile", dealerUserm.getUserMobile());
   params.put("dealerUser", dealerInfo.getDealerUser());
   params.put(
       "dealerMail",
       StringUtils.isEmpty(dealerUserm.getUserMail())
           ? "*****@*****.**"
           : dealerUserm.getUserMail());
   params.put(
       "dealerSex",
       dealerInfo.getDealerGender() == null ? 1 : (dealerInfo.getDealerGender() == 2 ? 1 : 0));
   params.put("dealerKey", dealerId);
   params.put(
       "outOfTime",
       com.zttx.web.utils.CalendarUtils.addDay(
           com.zttx.web.utils.CalendarUtils.getCurrentLong(), 60));
   params.put("userName", dealerUserm.getUserMobile());
   JSONObject _resJson = AppParameterUtils.getErpTrailJSONObject(params);
   if (_resJson == null
       || !_resJson.containsKey("code")
       || _resJson.getIntValue("code") != 121000) {
     throw new BusinessException("ERP 接口调用失败");
   }
   DealerBuyService _dealerBuyService =
       this.findBy(dealerId, CommonConstant.WebServiceItems.SERVICE_SYSTEM_STIALERP);
   if (_dealerBuyService != null) {
     throw new BusinessException("ERP试用服务已经开通,请刷页面!");
   }
   _dealerBuyService = new DealerBuyService();
   _dealerBuyService.setBeginTime(CalendarUtils.getCurrentLong());
   _dealerBuyService.setEndTime(
       CalendarUtils.addDay(com.zttx.web.utils.CalendarUtils.getCurrentLong(), 60));
   _dealerBuyService.setBuyTime(CalendarUtils.getCurrentLong());
   _dealerBuyService.setServiceId(CommonConstant.WebServiceItems.SERVICE_SYSTEM_STIALERP);
   _dealerBuyService.setServicerCate(Short.valueOf("1"));
   _dealerBuyService.setChargType(Short.valueOf("1"));
   _dealerBuyService.setBuyMoney(BigDecimal.ZERO);
   _dealerBuyService.setRefrenceId(SerialnoUtils.buildPrimaryKey());
   _dealerBuyService.setDealerId(dealerId);
   this.insertSelective(_dealerBuyService);
   // DealerInfo的服务起起时间
   dealerInfo.setBeginTime(CalendarUtils.getCurrentLong());
   dealerInfo.setEndTime(
       CalendarUtils.addDay(com.zttx.web.utils.CalendarUtils.getCurrentLong(), 60));
   dealerInfoService.updateByPrimaryKeySelective(dealerInfo);
 }
 @Override
 public PaginateResult<DealerShopEnv> getDealerShopEnvsBy(
     DealerShopEnv dealerShopEnv, Pagination pagination) {
   dealerShopEnv.setPage(pagination);
   List<DealerShopEnv> resultList = dealerShopEnvMapper.selectDealerShopEnvsBy(dealerShopEnv);
   for (DealerShopEnv _dealerShopEnv : resultList) {
     // 查询相应的服务
     DealerBuyService dbs =
         dealerBuyServiceMapper.findBy(
             _dealerShopEnv.getDealerId(), CommonConstant.WebServiceItems.SERVICE_SYSTEM_PLATFORM);
     if (dbs != null) {
       _dealerShopEnv.setPayedUser(
           dbs.getEndTime() != null
               && dbs.getEndTime() > com.zttx.web.utils.CalendarUtils.getCurrentLong());
     }
     _dealerShopEnv.setDealerImages(
         dealerImageMapper.selectDealerImagesByDealerId(_dealerShopEnv.getDealerId()));
     if (null != OnLineUserUtils.getPrincipal()) {
       _dealerShopEnv.setUserType(OnLineUserUtils.getPrincipal().getUserType());
       if (OnLineUserUtils.getPrincipal().getUserType().shortValue()
           == UserInfoConst.USER_TYPE_BRAND.shortValue()) {
         _dealerShopEnv.setCollectedState(
             brandFavoriteService.isCollected(
                 OnLineUserUtils.getPrincipal().getRefrenceId(), _dealerShopEnv.getDealerId()));
       }
     }
   }
   return new PaginateResult<>(pagination, resultList);
 }
  /*
   * 修改浏览次数
   */
  @Override
  public DealerInfo modifyViewContact(String brandId, String dealerId) throws BusinessException {
    DealerInfoModel dealerInfo = dealerInfoService.findById(dealerId);
    if (null == dealerInfo) {
      throw new BusinessException(CommonConst.DATA_NOT_EXISTS);
    }

    UserInfo userInfo = userInfoService.selectByPrimaryKey(dealerId);
    Integer contactIsExistInt = brandViewContactMapper.isExist(brandId, dealerId, null);
    Short viewType = BrandConstant.BrandViewContact.VIEW_TYPE_INITIATIVE;
    if (contactIsExistInt > 0) {
      Integer groomIsExistInt = dealerGroomMapper.isExist(brandId, dealerId);
      if (groomIsExistInt > 0) {
        viewType = BrandConstant.BrandViewContact.VIEW_TYPE__RECOMMEND;
      } else {
        Integer inviteIsExistInt = brandInviteMapper.isExist(brandId, dealerId);
        if (inviteIsExistInt > 0) {
          viewType = BrandConstant.BrandViewContact.VIEW_TYPE_APPLICATION;
        }
      }
    }
    BrandViewContact brandViewContact = new BrandViewContact();
    brandViewContact.setBrandId(brandId);
    brandViewContact.setDealerId(dealerId);
    brandViewContact.setViewType(viewType);
    if (contactIsExistInt == 0) {
      brandViewContact.setRefrenceId(com.zttx.sdk.utils.SerialnoUtils.buildPrimaryKey());
      brandViewContact.setViewTime(CalendarUtils.getCurrentLong());
      brandViewContactMapper.insert(brandViewContact);
    }

    BrandCount brandCount = brandCountService.selectByPrimaryKey(brandId);
    Integer viewCount = brandCountService.getViewDealerTotal(brandId);
    boolean brandCountisExist = true;
    if (null != brandCount) {
      if (viewCount.intValue() <= brandCount.getViewDealerCount().intValue()) {
        throw new BusinessException(BrandConst.VIEW_CONTACT_LACK_POINTS);
      }
    } else {
      brandCountisExist = false;
      brandCount = new BrandCount();
      brandCount.setRefrenceId(com.zttx.sdk.utils.SerialnoUtils.buildPrimaryKey());
    }
    // 查看经销商联系信息数量
    Long count = brandViewContactMapper.getBrandViewContactCount(brandViewContact);
    brandCount.setViewDealerCount(count.intValue());
    if (brandCountisExist) {
      brandCountService.updateByPrimaryKey(brandCount);
    } else {
      brandCountService.insert(brandCount);
    }
    Boolean isExits = false;
    if (contactIsExistInt > 0) {
      isExits = true;
    }
    dealerInfo.setIsViewAdd(isExits);
    dealerInfo.setUserMobile(userInfo.getUserMobile());

    return dealerInfo;
  }
 @Override
 public List<DealerShopEnv> getTopNewestDealerShopEnvs(int topn) {
   List<DealerShopEnv> list = dealerShopEnvMapper.selectTopNewestDealerShopEnvs(topn);
   for (DealerShopEnv _dealerShopEnv : list) {
     // 查询相应的服务
     DealerBuyService dbs =
         dealerBuyServiceMapper.findBy(
             _dealerShopEnv.getDealerId(), CommonConstant.WebServiceItems.SERVICE_SYSTEM_PLATFORM);
     if (dbs != null) {
       _dealerShopEnv.setPayedUser(
           dbs.getEndTime() != null
               && dbs.getEndTime() > com.zttx.web.utils.CalendarUtils.getCurrentLong());
     }
     _dealerShopEnv.setDealerImages(
         dealerImageMapper.selectDealerImagesByDealerId(_dealerShopEnv.getDealerId()));
   }
   return list;
 }
 @Override
 public PaginateResult<DealerShopEnv> getExcludeDealerShopEnvsBy(
     int currAreaNo, Pagination pagination) {
   List<DealerShopEnv> result =
       dealerShopEnvMapper.selectExcludeDealerShopEnvsBy(currAreaNo, pagination);
   for (DealerShopEnv _dealerShopEnv : result) {
     // 查询相应的服务
     DealerBuyService dbs =
         dealerBuyServiceMapper.findBy(
             _dealerShopEnv.getDealerId(), CommonConstant.WebServiceItems.SERVICE_SYSTEM_PLATFORM);
     if (dbs != null) {
       _dealerShopEnv.setPayedUser(
           dbs.getEndTime() != null
               && dbs.getEndTime() > com.zttx.web.utils.CalendarUtils.getCurrentLong());
     }
     _dealerShopEnv.setDealerImages(
         dealerImageMapper.selectDealerImagesByDealerId(_dealerShopEnv.getDealerId()));
   }
   return new PaginateResult<>(pagination, result);
 }
 public Boolean insertWithCheck(
     String brandId, String dealerId, Boolean isAdd, BrandViewContact brandViewContact)
     throws BusinessException {
   Short viewType = BrandConstant.BrandViewContact.VIEW_TYPE_INITIATIVE;
   Boolean isExist = this.isExist(brandId, dealerId, null);
   if (brandViewContact == null) {
     brandViewContact = new BrandViewContact();
   }
   if (!isExist) {
     isExist = dealerGroomMapper.isExist(brandId, dealerId) > 0;
     if (!isExist) {
       isExist = brandInviteMapper.isExist(brandId, dealerId) > 0;
       if (isExist) {
         viewType = BrandConstant.BrandViewContact.VIEW_TYPE_APPLICATION;
       }
     } else {
       viewType = BrandConstant.BrandViewContact.VIEW_TYPE__RECOMMEND;
     }
     if (isExist || isAdd) {
       brandViewContact.setRefrenceId(com.zttx.sdk.utils.SerialnoUtils.buildPrimaryKey());
       brandViewContact.setBrandId(brandId);
       brandViewContact.setDealerId(dealerId);
       brandViewContact.setViewTime(CalendarUtils.getCurrentLong());
       brandViewContact.setViewType(viewType);
       brandViewContactMapper.insert(brandViewContact);
       if (!isExist) {
         BrandCount brandCount = brandCountMapper.selectByPrimaryKey(brandId);
         Integer viewCount = getViewDealerTotal(brandCount);
         if (null != brandCount
             && viewCount.intValue() > brandCount.getViewDealerCount().intValue()) {
           List<Integer> countTypeList = Lists.newArrayList();
           // 已铺货产品数量和未铺货产品数量只需放入一个类型就可以了
           countTypeList.add(BrandConstant.BrandCountConst.BRANDCOUNT_VIEWDEALERCOUNT);
           brandCountService.modifyBrandCount(brandId, countTypeList.toArray(new Integer[] {}));
         }
       }
     }
   }
   return isExist;
 }