party_invite.go 7.98 KB
Newer Older
chenweijian's avatar
chenweijian committed
1 2 3
package invite_r

import (
chenweijian's avatar
chenweijian committed
4
	"git.hilo.cn/hilo-common/domain"
chenweijian's avatar
chenweijian committed
5
	"git.hilo.cn/hilo-common/mycontext"
chenweijian's avatar
chenweijian committed
6
	"git.hilo.cn/hilo-common/utils"
chenweijian's avatar
chenweijian committed
7
	"github.com/gin-gonic/gin"
chenweijian's avatar
chenweijian committed
8
	"github.com/jinzhu/now"
chenweijian's avatar
chenweijian committed
9 10
	"hilo-user/cv/invite_cv"
	"hilo-user/domain/cache/user_c"
chenweijian's avatar
chenweijian committed
11 12 13 14
	"hilo-user/domain/model/invite_m"
	"hilo-user/domain/model/promotion_m"
	"hilo-user/domain/model/user_m"
	"hilo-user/myerr/bizerr"
chenweijian's avatar
chenweijian committed
15
	"hilo-user/req"
chenweijian's avatar
chenweijian committed
16
	"hilo-user/resp"
chenweijian's avatar
chenweijian committed
17
	"time"
chenweijian's avatar
chenweijian committed
18 19
)

chenweijian's avatar
chenweijian committed
20
// @Tags 新人邀请
chenweijian's avatar
chenweijian committed
21
// @Summary 提交申请
chenweijian's avatar
chenweijian committed
22 23
// @Param newUserCode formData string true "被邀请人id"
// @Param platform formData string true "平台"
chenweijian's avatar
chenweijian committed
24
// @Param platformId formData string true "平台Id"
chenweijian's avatar
chenweijian committed
25 26 27
// @Param recharge formData string true "充值金额"
// @Param userCode formData string true "邀请人id"
// @Param videoUrl formData string true "充值金额"
chenweijian's avatar
chenweijian committed
28
// @Success 200
chenweijian's avatar
chenweijian committed
29 30 31
// @Router /v2/user/invite/apply [post]
func InviteApply(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
chenweijian's avatar
chenweijian committed
32

chenweijian's avatar
chenweijian committed
33 34 35
	type paramStr struct {
		NewUserCode string `form:"newUserCode" binding:"required"`
		Platform    string `form:"platform" binding:"required"`
chenweijian's avatar
chenweijian committed
36
		PlatformId  string `form:"platformId" binding:"required"`
chenweijian's avatar
chenweijian committed
37 38 39 40 41
		Recharge    string `form:"recharge" binding:"required"`
		UserCode    string `form:"userCode" binding:"required"`
		VideoUrl    string `form:"videoUrl" binding:"required"`
	}

chenweijian's avatar
chenweijian committed
42 43 44 45
	myUserId, err := req.GetUserId(c)
	if err != nil {
		return myCtx, err
	}
chenweijian's avatar
chenweijian committed
46

chenweijian's avatar
chenweijian committed
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
	var param paramStr
	if err := c.ShouldBind(&param); err != nil {
		return myCtx, err
	}
	model := domain.CreateModelContext(myCtx)
	// 平台是否填写正确
	platforms := promotion_m.GetPromotionPlatforms(model)
	var existsPlatform bool
	for _, v := range platforms {
		if v == param.Platform {
			existsPlatform = true
		}
	}
	if !existsPlatform {
		model.Log.Errorf("InviteApply param:%v", param)
		return myCtx, bizerr.InvalidParameter
	}
	// code 是否存在
	newUser, err := user_m.GetUserByCode(model, param.NewUserCode)
	if err != nil {
		model.Log.Errorf("InviteApply param:%v", param)
		return myCtx, err
	}
	user, err := user_m.GetUserByCode(model, param.UserCode)
	if err != nil {
		model.Log.Errorf("InviteApply param:%v", param)
		return myCtx, err
	}
	if newUser.ID == 0 || user.ID == 0 {
		model.Log.Errorf("InviteApply param:%v", param)
		return myCtx, bizerr.InvalidParameter
	}
chenweijian's avatar
chenweijian committed
79 80
	// 邀请人是否有资格邀请
	if !promotion_m.IsPromotionAgent(model, user.ID) {
chenweijian's avatar
chenweijian committed
81 82 83
		model.Log.Errorf("InviteApply 没有邀请资格 param:%v", param)
		return myCtx, bizerr.InviteApplyNoPermission
	}
chenweijian's avatar
chenweijian committed
84
	if user.ID != myUserId && !promotion_m.IsMyPromotionManager(model, user.ID, myUserId) {
chenweijian's avatar
chenweijian committed
85 86 87
		model.Log.Errorf("InviteApply 没有邀请资格 param:%v", param)
		return myCtx, bizerr.InviteApplyNoPermission
	}
chenweijian's avatar
chenweijian committed
88 89 90 91 92 93 94 95 96 97
	// 被邀请人是否已经被人提交过申请
	isApply, err := invite_m.IsInInviteApply(model, newUser.ID)
	if err != nil {
		model.Log.Errorf("InviteApply param:%v, err:%v", param, err)
		return myCtx, err
	}
	if isApply {
		model.Log.Errorf("InviteApply 已经被别人邀请过了 param:%v", param)
		return myCtx, bizerr.InviteApplyAlreadyInvited
	}
chenweijian's avatar
chenweijian committed
98 99 100 101 102 103 104 105 106 107 108
	// 被邀请人是否符合条件
	isInvite, err := promotion_m.IsPromotionInvitee(model, user.ID)
	if err != nil {
		model.Log.Errorf("InviteApply param:%v", param)
		return myCtx, err
	}
	if isInvite {
		model.Log.Errorf("InviteApply 已经被别人邀请了 param:%v", param)
		return myCtx, bizerr.InviteApplyAlreadyInvited
	}
	// 插入邀请表
chenweijian's avatar
chenweijian committed
109
	err = invite_m.CreateInviteApply(model, user.ID, newUser.ID, param.Platform, param.PlatformId, param.Recharge, param.VideoUrl)
chenweijian's avatar
chenweijian committed
110 111 112 113 114 115 116
	if err != nil {
		model.Log.Errorf("InviteApply param:%v", param)
		return myCtx, err
	}

	resp.ResponseOk(c, nil)
	return myCtx, nil
chenweijian's avatar
chenweijian committed
117
}
chenweijian's avatar
chenweijian committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131

// @Tags 新人邀请
// @Summary 查询历史申请
// @Param pageIndex query int true "偏移值 默认:1" default(1)
// @Param pageSize query int true "请求数量 默认:10" default(10)
// @Param beginTime query string true "开始时间2006-01-02"
// @Param endTime query string true "结束时间2006-01-02"
// @Param type query int true "1.已申请2.待审核3.已通过4.已拒绝"
// @Success 200 {object} invite_cv.InviteApplyRes
// @Router /v2/user/invite/apply [get]
func InviteApplyList(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)

	type paramStr struct {
chenweijian's avatar
chenweijian committed
132 133 134 135
		PageIndex int       `form:"pageIndex" binding:"required"`
		PageSize  int       `form:"pageSize" binding:"required"`
		BeginTime time.Time `form:"beginTime" binding:"required" time_format:"2006-01-02"`
		EndTime   time.Time `form:"endTime" binding:"required" time_format:"2006-01-02"`
chenweijian's avatar
chenweijian committed
136
		Type      int       `form:"type"`
chenweijian's avatar
chenweijian committed
137 138 139 140 141 142
	}

	var param paramStr
	if err := c.ShouldBindQuery(&param); err != nil {
		return myCtx, err
	}
chenweijian's avatar
chenweijian committed
143
	if param.Type < 0 || param.Type > 4 {
chenweijian's avatar
chenweijian committed
144 145 146
		return myCtx, bizerr.InvalidParameter
	}

chenweijian's avatar
chenweijian committed
147 148 149 150 151 152 153 154 155
	//beginTime, err := time.ParseInLocation(utils.DATE_FORMAT, param.BeginTime, time.Local)
	//if err != nil {
	//	return nil, myerr.WrapErr(err)
	//}
	//endTime, err := time.ParseInLocation(utils.DATE_FORMAT, param.EndTime, time.Local)
	//if err != nil {
	//	return nil, myerr.WrapErr(err)
	//}
	param.EndTime = utils.GetDayEndTime(param.EndTime)
chenweijian's avatar
chenweijian committed
156 157 158 159 160 161 162
	userId, err := req.GetUserId(c)
	if err != nil {
		return myCtx, err
	}

	var model = domain.CreateModelContext(myCtx)

chenweijian's avatar
chenweijian committed
163
	if param.Type == 0 { // 返回所有Type类型有多少条数
chenweijian's avatar
chenweijian committed
164
		numList := make([]*invite_cv.InviteApplyNumRes, 0, 4)
chenweijian's avatar
chenweijian committed
165 166 167 168 169
		for _, gType := range []int{1, 2, 3, 4} { // 1.已申请2.待审核3.已通过4.已拒绝
			num, err := invite_m.GetInviteApplyNumByType(model, gType, param.BeginTime, param.EndTime)
			if err != nil {
				return myCtx, err
			}
chenweijian's avatar
chenweijian committed
170
			numList = append(numList, &invite_cv.InviteApplyNumRes{Type: gType, Num: num})
chenweijian's avatar
chenweijian committed
171
		}
chenweijian's avatar
chenweijian committed
172
		resp.ResponsePageOk(c, &invite_cv.InviteApplyRes{NumList: numList}, 0, 0, 0)
chenweijian's avatar
chenweijian committed
173 174 175
		return myCtx, nil
	}

chenweijian's avatar
chenweijian committed
176 177 178 179 180 181 182 183
	agentIds := []uint64{userId}
	if promotion_m.IsPromotionManager(model, userId) {
		agentIds, err = promotion_m.GetPromotionManagerAgentList(model, userId)
		if err != nil {
			return myCtx, err
		}
	}

chenweijian's avatar
chenweijian committed
184
	list, total, err := invite_m.GetApplyList(model, agentIds, param.PageIndex, param.PageSize, param.Type, param.BeginTime, param.EndTime)
chenweijian's avatar
chenweijian committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198
	if err != nil {
		model.Log.Errorf("GetApplyList param:%v, err:%v", param, err)
		return myCtx, err
	}

	uids := make([]uint64, 0, len(list)+1)
	for _, v := range list {
		uids = append(uids, v.UserId, v.NewUserId)
	}
	users, err := user_c.GetUserTinyMap(model, uids, false)
	if err != nil {
		model.Log.Errorf("GetApplyList param:%v, err:%v", param, err)
		return myCtx, err
	}
chenweijian's avatar
chenweijian committed
199 200
	res := &invite_cv.InviteApplyRes{}
	res.List = make([]*invite_cv.InviteApply, 0, len(list))
chenweijian's avatar
chenweijian committed
201
	for _, v := range list {
chenweijian's avatar
chenweijian committed
202
		res.List = append(res.List, &invite_cv.InviteApply{
chenweijian's avatar
chenweijian committed
203 204 205 206 207 208 209
			NewUserCode: users[v.NewUserId].Code,
			Platform:    v.Platform,
			Recharge:    v.RechargeInfo,
			UserCode:    users[v.UserId].Code,
			CreateUnix:  v.CreatedTime.Unix(),
			Level:       v.Level,
			Status:      v.Status,
chenweijian's avatar
chenweijian committed
210 211
			Avatar:      users[v.NewUserId].Avatar,
			ExternalId:  users[v.NewUserId].ExternalId,
chenweijian's avatar
chenweijian committed
212 213 214 215 216 217
		})
	}

	resp.ResponsePageOk(c, res, total, param.PageSize, param.PageIndex)
	return myCtx, nil
}
chenweijian's avatar
chenweijian committed
218

chenweijian's avatar
chenweijian committed
219
// @tags 新人邀请
chenweijian's avatar
chenweijian committed
220 221 222 223 224 225 226 227
// @Summary 平台列表
// @Success 200 {object} []string
// @Router /v2/user/invite/platform [get]
func PromotionPlatform(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	resp.ResponseOk(c, []string{"Falla", "Yalla", "Whisper", "Ahlan", "Mashi", "YoYo", "Yoho", "Echo", "Hawa", "Yalla Ludo", "Hafla"})
	return myCtx, nil
}
chenweijian's avatar
chenweijian committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

// @Tags 新人邀请
// @Summary 查询周期
// @Success 200 {object} invite_cv.AgentPeriod
// @Router /v2/user/invite/period [get]
func AgentPeriod(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	var response invite_cv.AgentPeriod
	week := now.BeginningOfWeek().AddDate(0, 0, 1) // 周一开始
	for i := 0; i < 12; i++ {
		response.Week = append(response.Week, invite_cv.AgentPeriodWeek{
			StartDate: week.Format("2006-01-02"),
			EndDate:   week.AddDate(0, 0, 6).Format("2006-01-02"),
		})
		week = week.AddDate(0, 0, -7)
	}
	resp.ResponseOk(c, response)
	return myCtx, nil
}