anniversary.go 5.73 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6
package cp_r

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"github.com/gin-gonic/gin"
hujiebin's avatar
hujiebin committed
7
	"hilo-user/_const/enum/cp_e"
hujiebin's avatar
hujiebin committed
8
	"hilo-user/cv/cp_cv"
hujiebin's avatar
hujiebin committed
9
	"hilo-user/cv/user_cv"
hujiebin's avatar
hujiebin committed
10
	"hilo-user/domain/model/cp_m"
hujiebin's avatar
hujiebin committed
11
	"hilo-user/domain/model/user_m"
hujiebin's avatar
hujiebin committed
12 13 14 15
	"hilo-user/myerr/bizerr"
	"hilo-user/req"
	"hilo-user/resp"
	"strconv"
hujiebin's avatar
hujiebin committed
16
	"time"
hujiebin's avatar
hujiebin committed
17 18 19
)

type PostPutAnniversaryReq struct {
hujiebin's avatar
hujiebin committed
20 21 22
	Content   string `form:"content" binding:"required"`
	Timestamp int64  `form:"timestamp" binding:"required"`
	IsRemind  bool   `form:"isRemind"`
hujiebin's avatar
hujiebin committed
23 24 25 26 27 28 29
}

// @Tags CP v2
// @Summary 发布纪念日
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param content formData string true "纪念日名称"
hujiebin's avatar
hujiebin committed
30
// @Param timestamp formData int true "时间戳"
hujiebin's avatar
hujiebin committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
// @Param isRemind formData bool false "是否提醒"
// @Success 200
// @Router /v2/cp/anniversary [post]
func PostAnniversary(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	userId, err := req.GetUserId(c)
	if err != nil {
		return myCtx, err
	}
	var param PostPutAnniversaryReq
	if err := c.ShouldBind(&param); err != nil {
		return myCtx, err
	}
	model := domain.CreateModelContext(myCtx)
	relation, exits := cp_m.GetCpRelation(model, userId)
	if !exits {
		return myCtx, bizerr.CpNotRelation
	} else {
hujiebin's avatar
hujiebin committed
49
		if err := cp_m.AddCpAnniversary(model, cp_e.AnniversaryItemTypeNormal, relation, param.Content, param.Timestamp, param.IsRemind, 0); err != nil {
hujiebin's avatar
hujiebin committed
50 51 52 53 54 55 56 57 58 59 60 61
			return myCtx, err
		}
	}
	resp.ResponseOk(c, "")
	return myCtx, nil
}

// @Tags CP v2
// @Summary 修改纪念日
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param content formData string true "纪念日名称"
hujiebin's avatar
hujiebin committed
62
// @Param timestamp formData int true "时间戳"
hujiebin's avatar
hujiebin committed
63 64 65 66 67 68 69 70 71 72 73 74
// @Param isRemind formData bool false "是否提醒"
// @Param id path int true "更新的记录id"
// @Success 200
// @Router /v2/cp/anniversary/{id} [put]
func PutAnniversary(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	var param PostPutAnniversaryReq
	if err := c.ShouldBind(&param); err != nil {
		return myCtx, err
	}
	id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
	model := domain.CreateModelContext(myCtx)
hujiebin's avatar
hujiebin committed
75
	if err := cp_m.UpdateCpAnniversary(model, id, param.Content, param.Timestamp, param.IsRemind); err != nil {
hujiebin's avatar
hujiebin committed
76 77 78 79 80 81 82 83 84 85
		return myCtx, err
	}
	resp.ResponseOk(c, "")
	return myCtx, nil
}

// @Tags CP v2
// @Summary 获取纪念日
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
hujiebin's avatar
hujiebin committed
86 87
// @Param pageIndex query int true "偏移值 默认:1" default(1)
// @Param pageSize query int true "请求数量 默认:10" default(10)
hujiebin's avatar
hujiebin committed
88 89 90 91 92 93 94 95 96
// @Success 200 {object} []cp_cv.CvCpAnniversary
// @Router /v2/cp/anniversary [get]
func PageAnniversary(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	userId, err := req.GetUserId(c)
	if err != nil {
		return myCtx, err
	}
	var response = make([]cp_cv.CvCpAnniversary, 0)
hujiebin's avatar
hujiebin committed
97
	model := domain.CreateModelContext(myCtx)
hujiebin's avatar
hujiebin committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	cpRelation, exits := cp_m.GetCpRelation(model, userId)
	if exits {
		userIds := []uint64{cpRelation.UserId1, cpRelation.UserId2}
		users, err := user_m.GetUserMapByIds(model, userIds)
		if err != nil {
			return myCtx, err
		}
		cpUserId := cpRelation.UserId2
		if cpUserId == userId {
			cpUserId = cpRelation.UserId1
		}
		userInfo := user_cv.UserToTiny(users[userId])
		cpUserInfo := user_cv.UserToTiny(users[cpUserId])
		response = append(response, cp_cv.CvCpAnniversary{
			Type: cp_e.AnniversaryItemTypeAvatar,
			CpInfo: &cp_cv.CvCpBase{
				UserInfo:   *userInfo,
				CpUserInfo: *cpUserInfo,
			},
hujiebin's avatar
hujiebin committed
117 118 119
			OriginTimestamp: cpRelation.CreatedTime.Unix(),
			Timestamp:       cpRelation.CreatedTime.Unix(),
			CanDel:          false,
hujiebin's avatar
hujiebin committed
120 121
		})
	}
hujiebin's avatar
hujiebin committed
122 123
	anniversary := cp_m.GetAllCpAnniversary(model, userId)
	for _, v := range anniversary {
hujiebin's avatar
hujiebin committed
124
		timestamp := v.Timestamp
hujiebin's avatar
hujiebin committed
125
		if v.Type == cp_e.AnniversaryItemTypeAnniversary && timestamp > 0 {
hujiebin's avatar
hujiebin committed
126 127
			timestamp = calcNextAnniversary(timestamp)
		}
hujiebin's avatar
hujiebin committed
128 129
		// 客户端只认识0 1
		Type := v.Type
hujiebin's avatar
hujiebin committed
130
		if v.Type != cp_e.AnniversaryItemTypeAvatar {
hujiebin's avatar
hujiebin committed
131 132
			Type = cp_e.AnniversaryItemTypeNormal
		}
hujiebin's avatar
hujiebin committed
133
		response = append(response, cp_cv.CvCpAnniversary{
hujiebin's avatar
hujiebin committed
134 135 136 137 138 139 140 141
			Type:            Type,
			Id:              v.ID,
			Content:         v.Content,
			Timestamp:       timestamp,
			OriginTimestamp: v.Timestamp,
			IsRemind:        v.IsRemind,
			CanDel:          true,
			IsTop:           v.Sort > 0,
hujiebin's avatar
hujiebin committed
142 143 144 145 146 147
		})
	}
	resp.ResponsePageBaseOk(c, response, 0, false)
	return myCtx, nil
}

hujiebin's avatar
hujiebin committed
148 149 150
// 计算下一个纪念日
func calcNextAnniversary(timestamp int64) int64 {
	now := time.Now()
151 152 153 154
	// 还没超过一天,不用计算明年的
	if now.Unix()-timestamp < 86400 {
		return timestamp
	}
hujiebin's avatar
hujiebin committed
155 156
	birthday := time.Unix(timestamp, 0)
	// 计算今年的生日日期
hujiebin's avatar
hujiebin committed
157
	thisYearBirthday := time.Date(now.Year(), birthday.Month(), birthday.Day(), birthday.Hour(), birthday.Minute(), birthday.Second(), 0, time.Local)
hujiebin's avatar
hujiebin committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171

	// 如果今年的生日还未到,则生日日期为今年的生日日期;否则为明年的生日日期
	var next time.Time
	if thisYearBirthday.After(now) {
		next = thisYearBirthday
	} else {
		next = thisYearBirthday.AddDate(1, 0, 0)
	}

	// 计算时间戳
	nextTimestamp := next.Unix()
	return nextTimestamp
}

hujiebin's avatar
hujiebin committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
// @Tags CP v2
// @Summary 获取纪念日
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param id path int true "记录id"
// @Success 200
// @Router /v2/cp/anniversary/:id [delete]
func DelAnniversary(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
	model := domain.CreateModelContext(myCtx)
	if err := cp_m.DelCpAnniversary(model, id); err != nil {
		return myCtx, err
	}
	resp.ResponseOk(c, "")
	return myCtx, nil
}