/*@Scheduled(cron = "0 0 23 ? * *")*/
  public void fetchTenant() {
    List<CompanyAPIModel> companyAPIModels = companyCaller.findTenantCompays();
    List<TenantDictionary> tenantDictionaries = new ArrayList<TenantDictionary>();

    for (CompanyAPIModel companyAPIModel : companyAPIModels) {

      tenantDictionaries.add(companyAPIModel.getTenantDictionary());
    }

    tenantService.delAll();
    tenantService.save(tenantDictionaries);
  }
  /*@Scheduled(cron = "0 0 8 ? * *")*/
  public void fetchNewUsers() {

    ValueDictionary lastUserDateTime = valuesRepository.findValueByKey(lastFetchUserDateTimekey);
    DateTime lastDateTime = lastUserDateTime.getCreationTime();

    // call
    List<MemberAPIModel> memberAPIModels = userCaller.getUpdatedUsers(lastDateTime);
    // notify all user
    for (MemberAPIModel memberAPIModel : memberAPIModels) {

      // if tenant has a same ic number user will skip it
      if (tenantUserService.findByIcNumber(memberAPIModel.getIcNumber()) != null) {
        continue;
      }

      UserEntity userEntity = memberAPIModel.getUserEntity();

      TenantUserEntity tenantUserEntity = memberAPIModel.getTenantUserEntity();
      TenantDictionary tenantDictionary =
          tenantService.findByID(memberAPIModel.getCompanyId().intValue());
      tenantUserEntity.setTenant(tenantDictionary);

      // invalid moblie just save, not realtionship user
      if (PatternConstant.mobilePattern.matcher(userEntity.getMobile()).matches()) {
        UserEntity existUser = null;
        try {
          existUser = userService.findUserByMobile(userEntity.getMobile());
        } catch (Exception e) {
          logger.error(
              String.format("there's more than one user by mobile [%s]", userEntity.getMobile()));
          continue;
        }

        if (existUser == null) {
          smsUtils.sendMessage(
              userEntity.getMobile(), SendMessageUtils.resigterUserMessage(userEntity));
          existUser = userService.create(userEntity);
          tenantUserEntity.setPlatformUser(userEntity);
          tenantUserRepository.save(tenantUserEntity);

        } else {
          List<TenantUserEntity> tenantUserEntities = existUser.getTenantUsers();

          if (tenantUserEntities != null) {
            for (TenantUserEntity tenant : tenantUserEntities) {

              if (!tenant.getTenant().equals(tenantUserEntity.getTenant())) {
                tenantUserEntity.setPlatformUser(existUser);
                tenantUserRepository.save(tenantUserEntity);
                break;
              }
            }
          }
        }
      }
    }

    lastUserDateTime.setCreationTime(DateTime.now());
    valuesRepository.save(lastUserDateTime);
  }
  /*@Scheduled(cron = "0 0 23 ? * *")*/
  public void fetchStadiumData() {
    SubGymnasiumAPIModel[] models =
        stadiumCaller.searchSubGym(
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            0,
            Integer.MAX_VALUE,
            null,
            null);

    // 场馆的增量升级
    for (SubGymnasiumAPIModel model : models) {
      StadiumDictionary stadium = model.getStadiumDictionary();
      stadium.setTenant(tenantService.findByID(model.getCompanyId().intValue()));
      StadiumDictionary cachedStadium =
          stadiumRepository.findStadiumDictionaryByStadiumId(stadium.getStadiumId());

      if (cachedStadium == null) {
        stadiumRepository.save(stadium);
      } else {
        cachedStadium = model.updatStadiumDiction(cachedStadium);
        stadiumRepository.save(cachedStadium);
      }
    }

    dataBaseScheduled.updateStadiumRank();
  }