cp.go 2.62 KB
Newer Older
hujiebin's avatar
hujiebin committed
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
package cp_corn

import (
	"git.hilo.cn/hilo-common/_const/enum/diamond_e"
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mylogrus"
	"git.hilo.cn/hilo-common/resource/config"
	"git.hilo.cn/hilo-common/utils"
	"github.com/robfig/cron"
	"hilo-user/_const/enum/cp_e"
	"hilo-user/domain/model/cp_m"
	"hilo-user/domain/model/diamond_m"
	"time"
)

// cp邀请、解除到期结算
func CpInviteCancelInit() {
	if !config.IsMaster() {
		return
	}
	mylogrus.MyLog.Infof("CpInviteCancelInit")

	// 每2min监测一次
	c := cron.New()
	spec := "0 */2 * * * *"
	if !config.AppIsRelease() {
		spec = "0 */1 * * * ?" // 测服每1分钟
	}
	_ = c.AddFunc(spec, func() {
		defer utils.CheckGoPanic()
		model := domain.CreateModelNil()
		// 获取超过24小时没被处理的cp邀请
		inviteList, err := cp_m.GetCpInviteByTime(model, time.Now().AddDate(0, 0, -1))
		if err != nil {
			model.Log.Errorf("CpInviteCancelInit err:%v", err)
			return
		}
		for _, v := range inviteList {
			model.Log.Infof("CpInviteCancelInit invite:%+v", v)
			err = model.Transaction(func(model *domain.Model) error {
				// 更新邀请记录
				err = cp_m.UpdateStatusCpInvite(model, v.Id, cp_e.CpInviteExpired)
				if err != nil {
					model.Log.Errorf("CpInviteCancelInit Id:%d, err:%v", v.Id, err)
					return err
				}
				// 退费
				err = diamond_m.ChangeDiamondAccountDetail(model, diamond_e.CpInviteRefund, v.Id, v.UserId, v.DiamondNum)
				if err != nil {
					model.Log.Errorf("CpInviteCancelInit Id:%d, err:%v", v.Id, err)
					return err
				}
				return nil
			})
			if err != nil {
				model.Log.Errorf("CpInviteCancelInit invite:%+v, err:%v", v, err)
				return
			}
			time.Sleep(time.Millisecond * 10)
		}

		// 获取超过24小时没被处理的cp解除申请
		cancelList, err := cp_m.GetCpCancelByTime(model, time.Now().AddDate(0, 0, -1))
		if err != nil {
			model.Log.Errorf("CpInviteCancelInit err:%v", err)
			return
		}
		for _, v := range cancelList {
			model.Log.Infof("CpInviteCancelInit cancel:%+v", v)
			err = model.Transaction(func(model *domain.Model) error {
				// 更新解除记录
				err = cp_m.UpdateStatusCpCancel(model, v.Id, cp_e.CpCancelAcceptAuto)
				if err != nil {
					model.Log.Errorf("CpInviteCancelInit Id:%d, err:%v", v.Id, err)
					return err
				}
				// 删除cp关系表的记录
				err = cp_m.DelCpRelation(model, v.UserId, v.RecUserId)
				if err != nil {
					model.Log.Errorf("CpInviteCancelInit Id:%d, err:%v", v.Id, err)
					return err
				}
				return nil
			})
			if err != nil {
				model.Log.Errorf("CpInviteCancelInit cancel:%+v, err:%v", v, err)
				return
			}
			time.Sleep(time.Millisecond * 10)
		}
	})
	c.Start()
}