Exemplo n.º 1
0
  @Override
  @Transactional
  public void sign(Long userId) throws Exception {
    UserInfo user = infoDao.findOne(userId);
    if (user == null) {
      GeneralExceptionHandler.handle("用户不存在");
    }
    Date lsd = user.getLastSignDate();

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date now = df.parse(df.format(new Date()));

    String[] signArr = user.getSign().split("\\|");
    Long continueCount = Long.valueOf(signArr[0]); // 连续签到次数
    Long totalCount = Long.valueOf(signArr[1]); // 总共签到次数

    if (lsd != null) {
      Long day = (now.getTime() - lsd.getTime()) / (1000 * 3600 * 24);
      if (day < 1) {
        GeneralExceptionHandler.handle("今天已经签到过了");
      }
      if (day > 1) {
        continueCount = 0l;
      }
    }
    user.setLastSignDate(new Date());
    user.setSign((continueCount + 1) + "|" + (totalCount + 1));
    infoDao.save(user);
  }