1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| type CheckinAddLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext }
func NewCheckinAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckinAddLogic { return &CheckinAddLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } }
func (l *CheckinAddLogic) CheckinAdd(req *types.CheckinAddReq) (resp *types.CheckinAddResp, err error) { resp = &types.CheckinAddResp{} sUserId := l.ctx.Value("userid").(string) iUserId, _ := strconv.Atoi(sUserId) checkinDate, err := time.ParseInLocation(time.DateOnly, req.Date, time.Local) if err != nil { return nil, errors.New("日期格式不正确") } logx.Infof("checkinDate:%s", checkinDate.String())
db := l.svcCtx.DB var userCheckin checkin.UserCheckin db = db.Model(&checkin.UserCheckin{}). Where("user_id = ? AND checkin_date = ?", iUserId, checkinDate.Format(time.DateOnly)).First(&userCheckin) if db.Error != nil && !errors.Is(db.Error, gorm.ErrRecordNotFound) { return nil, db.Error } if userCheckin.ID > 0 { return resp, errors.New("已签到") }
t := time.Now() startTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()).In(time.Local) endTime := time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 0, t.Location()).In(time.Local) if checkinDate.Before(startTime) || checkinDate.After(endTime) { return resp, errors.New("只能当天签到") }
tx := l.svcCtx.DB.Begin() var lastCheckin checkin.UserCheckin result := tx.Model(&checkin.UserCheckin{}).Where("user_id = ?", iUserId).Order("checkin_date DESC"). First(&lastCheckin) if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) { tx.Rollback() return nil, result.Error }
continuousDays := 1 if result.Error == nil { yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02") if lastCheckin.CheckinDate.Format("2006-01-02") == yesterday { continuousDays = lastCheckin.ContinuousDays + 1 } }
userCheckin := checkin.UserCheckin{ UserID: int64(iUserId), CheckinDate: checkinDate, CheckinTime: time.Now(), ContinuousDays: continuousDays, RewardPoints: constant.CheckinRewardPoints, RewardType: 1, Remark: fmt.Sprintf("签到:奖励积分%d", constant.CheckinRewardPoints), CreatedAt: time.Now(), UpdatedAt: time.Now(), } result = tx.Model(&checkin.UserCheckin{}).Create(&userCheckin) if result.Error != nil || result.RowsAffected == 0 { tx.Rollback() return nil, result.Error } var userCheckinStat checkin.UserCheckinStats result = tx.Model(&checkin.UserCheckinStats{}).Where("user_id = ?", iUserId).First(&userCheckinStat) if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) { tx.Rollback() return nil, result.Error }
if userCheckinStat.ID == 0 { result = tx.Model(&checkin.UserCheckinStats{}).Create(&checkin.UserCheckinStats{ UserID: int64(iUserId), TotalCheckinDays: 1, MaxContinuousDays: 1, CurrentContinuousDays: continuousDays, LastCheckinDate: time.Now(), CreatedAt: time.Now(), UpdatedAt: time.Now(), }) if result.Error != nil || result.RowsAffected == 0 { tx.Rollback() return nil, result.Error } } else { result = tx.Model(&checkin.UserCheckinStats{}).Where("user_id = ?", iUserId).Updates(map[string]interface{}{ "total_checkin_days": gorm.Expr("total_checkin_days + ?", 1), "max_continuous_days": gorm.Expr("GREATEST(max_continuous_days, ?)", continuousDays), "current_continuous_days": continuousDays, "last_checkin_date": time.Now(), "updated_at": time.Now(), }) if result.Error != nil || result.RowsAffected == 0 { tx.Rollback() return nil, result.Error } } var userInfo model.UserInfo result = tx.Model(&model.UserInfo{}).Where("user_id = ?", iUserId).First(&userInfo) if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) { tx.Rollback() return nil, result.Error }
pointBalance := userInfo.Point + constant.CheckinRewardPoints result = tx.Model(&checkin.UserPointChangeLog{}).Create(&checkin.UserPointChangeLog{ UserID: int64(iUserId), Point: constant.CheckinRewardPoints, PointBalance: int(pointBalance), Type: 1, BusinessID: userCheckin.ID, ChangeDate: time.Now(), ChangeTime: time.Now(), Remark: fmt.Sprintf("签到:奖励积分%d", constant.CheckinRewardPoints), CreatedAt: time.Now(), UpdatedAt: time.Now(), }) if result.Error != nil || result.RowsAffected == 0 { tx.Rollback() return nil, result.Error } result = db.Model(&model.UserInfo{}).Where("user_id = ?", iUserId).Updates(map[string]interface{}{ "point": gorm.Expr("point + ?", constant.CheckinRewardPoints), "updated_at": time.Now(), }) if result.Error != nil || result.RowsAffected == 0 { tx.Rollback() return nil, result.Error }
tx.Commit() resp.UserID = int64(userInfo.UserID) resp.Point = int64(pointBalance)
return resp, nil }
|