noble.go 4.46 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
package noble_tx

import (
	"fmt"
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/internal/enum/headwear_e"
	"git.hilo.cn/hilo-common/internal/enum/msg_e"
	"git.hilo.cn/hilo-common/internal/enum/property_e"
	"git.hilo.cn/hilo-common/internal/model/msg_m"
	"git.hilo.cn/hilo-common/internal/model/noble_m"
	"git.hilo.cn/hilo-common/internal/model/user_m"
	"git.hilo.cn/hilo-common/resource/mysql"
	"git.hilo.cn/hilo-common/txop/headwear_tx"
	"strconv"
	"time"
)

// 下发贵族
func SendNoble(model *domain.Model, receiverUserId mysql.ID, level uint16, days int) error {
	if level <= 0 {
		return fmt.Errorf("level is 0")
	}

hujiebin's avatar
hujiebin committed
24
	cfg, err := noble_m.GetConfigByLevel(model.DB(), level)
hujiebin's avatar
hujiebin committed
25 26 27 28 29 30 31
	if err != nil {
		return err
	}
	if cfg.PurchasePrice <= 0 || cfg.RenewalPrice <= 0 {
		return fmt.Errorf("level is 0")
	}
	n := noble_m.UserNoble{UserId: receiverUserId, Level: level}
hujiebin's avatar
hujiebin committed
32
	records, err := n.FindAll(model.DB())
hujiebin's avatar
hujiebin committed
33 34 35 36 37 38 39 40 41 42 43 44
	if err != nil {
		return err
	}
	if len(records) > 1 {
		// DB表结构决定了不可能发生
		return fmt.Errorf("bizerr.IncorrectState")
	}

	if len(records) <= 0 {
		// 新增贵族
		endTime := time.Now().AddDate(0, 0, int(days))
		n = noble_m.UserNoble{UserId: receiverUserId, Level: level, EndTime: endTime}
hujiebin's avatar
hujiebin committed
45
		err = n.Create(model.DB())
hujiebin's avatar
hujiebin committed
46
		if err != nil {
hujiebin's avatar
hujiebin committed
47 48 49 50 51
			if af2, err := n.ExtenedEndTime(model.DB(), int(days)); err != nil {
				return err
			} else if af2 <= 0 {
				return fmt.Errorf("bizerr.TransactionFailed create rows 0")
			}
hujiebin's avatar
hujiebin committed
52 53 54 55 56 57 58 59 60 61
		}

		nbl := noble_m.UserNobleLog{
			SenderId:   0,
			ReceiverId: receiverUserId,
			Level:      level,
			Money:      0,
			SrcType:    noble_m.SRC_APP,
			NewEndTime: endTime,
		}
hujiebin's avatar
hujiebin committed
62
		err = nbl.Create(model.DB())
hujiebin's avatar
hujiebin committed
63
		if err != nil {
hujiebin's avatar
hujiebin committed
64
			return err
hujiebin's avatar
hujiebin committed
65 66 67 68 69 70 71 72 73 74 75 76 77
		}
	} else {
		// 延长贵族
		n = records[0]
		now := time.Now()
		endTime := n.EndTime.AddDate(0, 0, int(days))
		if now.After(n.EndTime) {
			endTime = now.AddDate(0, 0, int(days))
		}
		nn := noble_m.UserNoble{
			Entity: mysql.Entity{ID: n.ID, UpdatedTime: n.UpdatedTime},
		}

hujiebin's avatar
hujiebin committed
78
		af, err := nn.UpdateEndTime(model.DB(), endTime)
hujiebin's avatar
hujiebin committed
79 80 81 82
		if err != nil {
			return err
		}
		if af <= 0 {
hujiebin's avatar
hujiebin committed
83 84 85 86 87
			if af2, err := nn.ExtenedEndTime(model.DB(), days); err != nil {
				return err
			} else if af2 <= 0 {
				return fmt.Errorf("bizerr.TransactionFailed rows 0")
			}
hujiebin's avatar
hujiebin committed
88 89 90 91 92 93 94 95 96 97 98
		}

		nbl := noble_m.UserNobleLog{
			SenderId:   0,
			ReceiverId: receiverUserId,
			Level:      level,
			Money:      0,
			SrcType:    noble_m.SRC_APP,
			OldEndTime: n.EndTime,
			NewEndTime: endTime,
		}
hujiebin's avatar
hujiebin committed
99
		err = nbl.Create(model.DB())
hujiebin's avatar
hujiebin committed
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
		if err != nil {
			return err
		}
	}
	// 头饰增加
	receiveHeadwearDuration := uint32(days) * 3600 * 24
	if cfg.HeaddressId != 0 {
		userHeadwear, err := user_m.GetUserHeadwearOrInit(model, receiverUserId, cfg.HeaddressId)
		if err != nil {
			return err
		}
		nowTime := time.Now()
		if userHeadwear.EndTime.After(nowTime) {
			nowTime = userHeadwear.EndTime
		}
		userHeadwear.EndTime = nowTime.Add(time.Duration(receiveHeadwearDuration) * time.Second)
		if err := userHeadwear.Persistent(); err != nil {
			return err
		}
		//日志错误,并不事务回调
		if _, err := headwear_tx.AddUserHeadwearLog(model, receiverUserId, cfg.HeaddressId, headwear_e.ActivityTrigger, headwear_e.AddSecond, &receiveHeadwearDuration, nil, 0); err != nil {
			model.Log.Error(err)
		}
	}
	// 增加座驾
	receivePropertyDuration := uint32(days) * 3600 * 24
	if cfg.RideId != 0 {
		userProperty, err := user_m.GetUserPropertyOrInit(model, receiverUserId, cfg.RideId)
		if err != nil {
			return err
		}
		nowTime := time.Now()
		if userProperty.EndTime.After(nowTime) {
			nowTime = userProperty.EndTime
		}
		userProperty.EndTime = nowTime.Add(time.Duration(receivePropertyDuration) * time.Second)
		if err := userProperty.Persistent(); err != nil {
			return err
		}
		//日志错误,并不事务回调
		if err := (&user_m.UserPropertyLog{
			Model:         model,
			UserId:        receiverUserId,
			PropertyId:    cfg.RideId,
			OriginType:    property_e.ActivityBillboardTrigger,
			Type:          property_e.AddSecond,
			AddSecond:     &receivePropertyDuration,
			UpdateEndTime: nil,
		}).Persistent(); err != nil {
			model.Log.Error(err)
		}
	}
	// 推送
	user, err := user_m.GetUser(model, receiverUserId)
	if err != nil {
		return err
	}
	nobleDuration := days * 3600 * 24
	if err := msg_m.NewUserRecord(model, user.ID, msg_e.AddNoble, user.Nick, user.ID, "", strconv.Itoa(int(nobleDuration)/(24*3600)), "", "", "").Persistent(); err != nil {
		return err
	}
	msg_m.SendEmasMsgAssistant(model, user.ExternalId, user.DeviceType)
	return nil
}