Browse Source

添加积分、成长值、红包、优惠券等函数实现

master
lijianbin 3 years ago
parent
commit
371829ca29
2 changed files with 716 additions and 0 deletions
  1. +707
    -0
      account.client.go
  2. +9
    -0
      account.client_test.go

+ 707
- 0
account.client.go View File

@ -3,8 +3,63 @@ package userrpc
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"log"
) )
type GetAddReq struct {
Req
UserId string
Value string
}
type GetUpdateReq struct {
Req
UserId string
Value string
UsedValue string
}
type GetLogReq struct {
Req
UserId string
Value string
Before string
After string
Source string
SourceId string
OrderSn string
Target string
TargetId string
ProductThumb string
Memo string
}
type GetUpgradeLogReq struct {
Req
UserId string
Upgrade string
Before string
After string
Source string
Memo string
}
type GetCouponReq struct {
Req
UserId string
CouponId string
ExpireTime string
}
type GetUpdateCouponReq struct {
Req
UserId string
CouponId string
OrderSn string
Remark string
IsUse string
}
/** /**
* 获取用户积分 * 获取用户积分
*/ */
@ -105,4 +160,656 @@ func GetUserRedEnvelope(dbname, userId string, url ...string) (string, error) {
return redEnvelope, nil return redEnvelope, nil
} }
/**
* 添加积分
* @2021/09/30
* @bin
*/
func AddUserScore(dbname, userId, score string, url ...string) (int64, error) {
if dbname == "" || userId == "" || score == "" {
return 0, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return 0, err
}
defer conn.Close()
data := GetAddReq{}
data.Dbname = dbname
data.UserId = userId
data.Value = score
req, err := SetReqData(data)
if err != nil {
log.Println("AddUserScore crypter error:", err)
return 0, err
}
res := &Response{}
err = conn.AddUserScore(req, res)
if err != nil {
log.Println("userrpc AddUserScore error:", err)
return 0, err
}
res_data_de, err := GetResData(res)
if err != nil {
return 0, err
}
if res_data_de == "" {
return 0, nil
}
var insertId int64
err = json.Unmarshal([]byte(res_data_de), &insertId)
if err != nil {
return 0, err
}
return insertId, nil
}
/**
* 更新积分记录
* @2021/09/30
* @bin
*/
func UpdateUserScore(dbname, userId, score, used_score string, url ...string) (bool, error) {
if dbname == "" || userId == "" || score == "" || used_score == "" {
return false, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return false, err
}
defer conn.Close()
data := GetUpdateReq{}
data.Dbname = dbname
data.UserId = userId
data.Value = score
data.UsedValue = used_score
req, err := SetReqData(data)
if err != nil {
log.Println("UpdateUserScore crypter error:", err)
return false, err
}
res := &Response{}
err = conn.UpdateUserScore(req, res)
if err != nil {
log.Println("userrpc UpdateUserScore error:", err)
return false, err
}
res_data_de, err := GetResData(res)
if err != nil {
return false, err
}
if res_data_de == "" {
return false, nil
}
var updateres bool
err = json.Unmarshal([]byte(res_data_de), &updateres)
if err != nil {
return false, err
}
return updateres, nil
}
/**
* 增加积分日志
* @2021/09/30
* @bin
*/
func AddUserScoreLog(dbname, UserId, Score, Before, After, Source, SourceId, OrderSn, Target, TargetId, ProductThumb, Memo string, url ...string) (int64, error) {
if dbname == "" || UserId == "" || Score == "" || After == "" {
return 0, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return 0, err
}
defer conn.Close()
data := GetLogReq{}
data.Dbname = dbname
data.UserId = UserId
data.Value = Score
data.Before = Before
data.After = After
data.Source = Source
data.SourceId = SourceId
data.OrderSn = OrderSn
data.Target = Target
data.TargetId = TargetId
data.ProductThumb = ProductThumb
data.Memo = Memo
req, err := SetReqData(data)
if err != nil {
log.Println("AddUserScoreLog crypter error:", err)
return 0, err
}
res := &Response{}
err = conn.AddUserScoreLog(req, res)
if err != nil {
log.Println("userrpc AddUserScoreLog error:", err)
return 0, err
}
res_data_de, err := GetResData(res)
if err != nil {
return 0, err
}
if res_data_de == "" {
return 0, nil
}
var insertId int64
err = json.Unmarshal([]byte(res_data_de), &insertId)
if err != nil {
return 0, err
}
return insertId, nil
}
/**
* 添加成长值
* @2021/09/30
* @bin
*/
func AddUserUpgrade(dbname, userId, upgrade string, url ...string) (int64, error) {
if dbname == "" || userId == "" || upgrade == "" {
return 0, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return 0, err
}
defer conn.Close()
data := GetAddReq{}
data.Dbname = dbname
data.UserId = userId
data.Value = upgrade
req, err := SetReqData(data)
if err != nil {
log.Println("AddUserUpgrade crypter error:", err)
return 0, err
}
res := &Response{}
err = conn.AddUserUpgrade(req, res)
if err != nil {
log.Println("userrpc AddUserUpgrade error:", err)
return 0, err
}
res_data_de, err := GetResData(res)
if err != nil {
return 0, err
}
if res_data_de == "" {
return 0, nil
}
var insertId int64
err = json.Unmarshal([]byte(res_data_de), &insertId)
if err != nil {
return 0, err
}
return insertId, nil
}
/**
* 更新成长值记录
* @2021/09/30
* @bin
*/
func UpdateUserUpgrade(dbname, userId, upgrade string, url ...string) (bool, error) {
if dbname == "" || userId == "" || upgrade == "" {
return false, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return false, err
}
defer conn.Close()
data := GetAddReq{}
data.Dbname = dbname
data.UserId = userId
data.Value = upgrade
req, err := SetReqData(data)
if err != nil {
log.Println("UpdateUserUpgrade crypter error:", err)
return false, err
}
res := &Response{}
err = conn.UpdateUserUpgrade(req, res)
if err != nil {
log.Println("userrpc UpdateUserUpgrade error:", err)
return false, err
}
res_data_de, err := GetResData(res)
if err != nil {
return false, err
}
if res_data_de == "" {
return false, nil
}
var updateres bool
err = json.Unmarshal([]byte(res_data_de), &updateres)
if err != nil {
return false, err
}
return updateres, nil
}
/**
* 增加成长值日志
* @2021/09/30
* @bin
*/
func AddUserUpgradeLog(dbname, UserId, Upgrade, Before, After, Source, Memo string, url ...string) (int64, error) {
if dbname == "" || UserId == "" || Upgrade == "" || After == "" {
return 0, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return 0, err
}
defer conn.Close()
data := GetUpgradeLogReq{}
data.Dbname = dbname
data.UserId = UserId
data.Upgrade = Upgrade
data.Before = Before
data.After = After
data.Source = Source
data.Memo = Memo
req, err := SetReqData(data)
if err != nil {
log.Println("AddUserUpgradeLog crypter error:", err)
return 0, err
}
res := &Response{}
err = conn.AddUserUpgradeLog(req, res)
if err != nil {
log.Println("userrpc AddUserUpgradeLog error:", err)
return 0, err
}
res_data_de, err := GetResData(res)
if err != nil {
return 0, err
}
if res_data_de == "" {
return 0, nil
}
var insertId int64
err = json.Unmarshal([]byte(res_data_de), &insertId)
if err != nil {
return 0, err
}
return insertId, nil
}
/**
* 添加红包
* @2021/09/30
* @bin
*/
func AddUserRedEnvelope(dbname, userId, money string, url ...string) (int64, error) {
if dbname == "" || userId == "" || money == "" {
return 0, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return 0, err
}
defer conn.Close()
data := GetAddReq{}
data.Dbname = dbname
data.UserId = userId
data.Value = money
req, err := SetReqData(data)
if err != nil {
log.Println("AddUserRedEnvelope crypter error:", err)
return 0, err
}
res := &Response{}
err = conn.AddUserRedEnvelope(req, res)
if err != nil {
log.Println("userrpc AddUserRedEnvelope error:", err)
return 0, err
}
res_data_de, err := GetResData(res)
if err != nil {
return 0, err
}
if res_data_de == "" {
return 0, nil
}
var insertId int64
err = json.Unmarshal([]byte(res_data_de), &insertId)
if err != nil {
return 0, err
}
return insertId, nil
}
/**
* 更新红包记录
* @2021/09/30
* @bin
*/
func UpdateUserRedEnvelope(dbname, userId, money string, url ...string) (bool, error) {
if dbname == "" || userId == "" || money == "" {
return false, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return false, err
}
defer conn.Close()
data := GetAddReq{}
data.Dbname = dbname
data.UserId = userId
data.Value = money
req, err := SetReqData(data)
if err != nil {
log.Println("UpdateUserRedEnvelope crypter error:", err)
return false, err
}
res := &Response{}
err = conn.UpdateUserUpgrade(req, res)
if err != nil {
log.Println("userrpc UpdateUserRedEnvelope error:", err)
return false, err
}
res_data_de, err := GetResData(res)
if err != nil {
return false, err
}
if res_data_de == "" {
return false, nil
}
var updateres bool
err = json.Unmarshal([]byte(res_data_de), &updateres)
if err != nil {
return false, err
}
return updateres, nil
}
/**
* 增加红包日志
* @2021/09/30
* @bin
*/
func AddUserRedEnvelopeLog(dbname, UserId, Money, Before, After, Source, SourceId, OrderSn, Target, TargetId, ProductThumb, Memo string, url ...string) (int64, error) {
if dbname == "" || UserId == "" || Money == "" || After == "" {
return 0, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return 0, err
}
defer conn.Close()
data := GetLogReq{}
data.Dbname = dbname
data.UserId = UserId
data.Value = Money
data.Before = Before
data.After = After
data.Source = Source
data.SourceId = SourceId
data.OrderSn = OrderSn
data.Target = Target
data.TargetId = TargetId
data.ProductThumb = ProductThumb
data.Memo = Memo
req, err := SetReqData(data)
if err != nil {
log.Println("AddUserRedEnvelopeLog crypter error:", err)
return 0, err
}
res := &Response{}
err = conn.AddUserRedEnvelopeLog(req, res)
if err != nil {
log.Println("userrpc AddUserRedEnvelopeLog error:", err)
return 0, err
}
res_data_de, err := GetResData(res)
if err != nil {
return 0, err
}
if res_data_de == "" {
return 0, nil
}
var insertId int64
err = json.Unmarshal([]byte(res_data_de), &insertId)
if err != nil {
return 0, err
}
return insertId, nil
}
/**
* 添加优惠券
* @2021/09/30
* @bin
*/
func AddUserCoupon(dbname, userId, couponId, expireTime string, url ...string) (int64, error) {
if dbname == "" || userId == "" || couponId == "" {
return 0, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return 0, err
}
defer conn.Close()
data := GetCouponReq{}
data.Dbname = dbname
data.UserId = userId
data.CouponId = couponId
data.ExpireTime = expireTime
req, err := SetReqData(data)
if err != nil {
log.Println("AddUserCoupon crypter error:", err)
return 0, err
}
res := &Response{}
err = conn.AddUserCoupon(req, res)
if err != nil {
log.Println("userrpc AddUserCoupon error:", err)
return 0, err
}
res_data_de, err := GetResData(res)
if err != nil {
return 0, err
}
if res_data_de == "" {
return 0, nil
}
var insertId int64
err = json.Unmarshal([]byte(res_data_de), &insertId)
if err != nil {
return 0, err
}
return insertId, nil
}
/**
* 更新优惠券记录
* @2021/09/30
* @bin
*/
func UpdateUserCoupon(dbname, userId, couponId, remark, orderSn, isUse string, url ...string) (bool, error) {
if dbname == "" || userId == "" || couponId == "" {
return false, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return false, err
}
defer conn.Close()
data := GetUpdateCouponReq{}
data.Dbname = dbname
data.UserId = userId
data.CouponId = couponId
data.Remark = remark
data.OrderSn = orderSn
data.IsUse = isUse
req, err := SetReqData(data)
if err != nil {
log.Println("UpdateUserCoupon crypter error:", err)
return false, err
}
res := &Response{}
err = conn.UpdateUserUpgrade(req, res)
if err != nil {
log.Println("userrpc UpdateUserCoupon error:", err)
return false, err
}
res_data_de, err := GetResData(res)
if err != nil {
return false, err
}
if res_data_de == "" {
return false, nil
}
var updateres bool
err = json.Unmarshal([]byte(res_data_de), &updateres)
if err != nil {
return false, err
}
return updateres, nil
}

+ 9
- 0
account.client_test.go View File

@ -23,3 +23,12 @@ func Test_GetUserRedEnvelope(t *testing.T) {
t.Log(ret) t.Log(ret)
t.Log(err) t.Log(err)
} }
func Test_AddUserScore(t *testing.T) {
dbname := "shop_v2"
userid := "2"
ret, err := AddUserScore(dbname,userid,"20")
t.Log(ret)
t.Log(err)
}

Loading…
Cancel
Save