- move Cacher from globals to api

- vendorPromotionID for CreatePromotion
- LockPromotionSkus
This commit is contained in:
gazebo
2018-11-07 13:17:25 +08:00
parent ee9b84bd80
commit 9bb30affc4
12 changed files with 291 additions and 163 deletions

View File

@@ -6,7 +6,7 @@ import (
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
)
const (
@@ -52,7 +52,7 @@ func Login(id, loginType, secret string) (loginInfo *LoginInfo, err error) {
ExpiresIn: time.Now().Add(DefTokenDuration).Unix(),
Token: token,
}
globals.Cacher.Set(token, loginInfo, DefTokenDuration)
api.Cacher.Set(token, loginInfo, DefTokenDuration)
return loginInfo, nil
}
} else {
@@ -63,18 +63,18 @@ func Login(id, loginType, secret string) (loginInfo *LoginInfo, err error) {
func Logout(token string) (err error) {
loginInfo := new(LoginInfo)
if err = globals.Cacher.GetAs(token, loginInfo); err == nil {
if err = api.Cacher.GetAs(token, loginInfo); err == nil {
if handler := authers[loginInfo.LoginType]; handler != nil {
err = handler.Logout(loginInfo.ID)
}
globals.Cacher.Del(token)
api.Cacher.Del(token)
}
return err
}
func GetUserInfo(token string) (loginInfo *LoginInfo, err error) {
loginInfo = new(LoginInfo)
if err = globals.Cacher.GetAs(token, loginInfo); err == nil {
if err = api.Cacher.GetAs(token, loginInfo); err == nil {
return loginInfo, nil
}
return nil, model.ErrTokenIsInvalid

View File

@@ -7,6 +7,7 @@ import (
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
"github.com/KenmyZhang/aliyun-communicate"
)
@@ -23,7 +24,7 @@ func SendVerifyCode(mobileNumber string) error {
"code": code,
})))
if err == nil {
globals.Cacher.Set(mobileNumber, code, DefVerifyCodeDuration)
api.Cacher.Set(mobileNumber, code, DefVerifyCodeDuration)
} else {
globals.SugarLogger.Infof("SendVerifyCode mobileNumber:%s failed with error:%v", mobileNumber, err)
}
@@ -33,9 +34,9 @@ func SendVerifyCode(mobileNumber string) error {
func VerifyCode(mobileNumber, code string) bool {
globals.SugarLogger.Debugf("VerifyCode mobileNumber:%s, code:%s", mobileNumber, code)
if value := globals.Cacher.Get(mobileNumber); value != nil {
if value := api.Cacher.Get(mobileNumber); value != nil {
if code == value.(string) {
globals.Cacher.Del(mobileNumber)
api.Cacher.Del(mobileNumber)
return true
}
}

View File

@@ -54,7 +54,7 @@ func GetUserInfo(code string, state string) (token *UserInfoExt, err error) {
if err = err2; err == nil {
pwd := utils.GetUUID()
globals.SugarLogger.Debugf("GetUserInfo code:%s, pwd:%s", code, pwd)
globals.Cacher.Set(wxUserinfo.OpenID, pwd, DefTempPasswordDuration)
api.Cacher.Set(wxUserinfo.OpenID, pwd, DefTempPasswordDuration)
return &UserInfoExt{
UserInfo: *wxUserinfo,
TempPassword: pwd,
@@ -70,13 +70,13 @@ func GetUserInfo(code string, state string) (token *UserInfoExt, err error) {
func (a *Auther) Login(openid, password string) (err error) {
globals.SugarLogger.Debugf("Login openid:%s, password:%s", openid, password)
if value := globals.Cacher.Get(openid); value != nil {
if value := api.Cacher.Get(openid); value != nil {
if password == value.(string) {
// wxUser := &legacymodel.WeiXins{
// OpenID: openid,
// }
// if err = dao.GetEntity(nil, wxUser, "OpenID"); err == nil {
globals.Cacher.Del(openid)
api.Cacher.Del(openid)
return nil
// }
}
@@ -87,14 +87,14 @@ func (a *Auther) Login(openid, password string) (err error) {
}
func (a *Auther) Logout(openid string) error {
return globals.Cacher.Del(openid)
return api.Cacher.Del(openid)
}
func BindMobile(token, mobileNum, code, nickname string) (err error) {
globals.SugarLogger.Debugf("BindMobile mobileNum:%s, code:%s, nickname:%s", mobileNum, code, nickname)
loginInfo := new(auth.LoginInfo)
if err = globals.Cacher.GetAs(token, loginInfo); err == nil {
if err = api.Cacher.GetAs(token, loginInfo); err == nil {
if mobile.VerifyCode(mobileNum, code) {
user := &legacymodel.WeiXins{
OpenID: loginInfo.ID,

View File

@@ -197,11 +197,16 @@ func Init() {
scheduleRoutine()
}
func CreateJdPromotion(ctx *jxcontext.Context, isIDJd bool, isAsync, isContinueWhenError bool, params *PromotionParams, userName string) (hint string, err error) {
func CreateJdPromotion(ctx *jxcontext.Context, isIDJd bool, isAsync, isContinueWhenError bool, vendorPromotionID string, params *PromotionParams, userName string) (hint string, err error) {
if len(params.SkuPrices) == 0 {
return "", ErrEmptySkus
}
db := dao.GetDB()
modifyPricesList := make(map[int][]*jdapi.SkuPriceInfo)
promotionPrices := make([]map[string]interface{}, len(params.StoreIDs)*len(params.SkuPrices))
var jxStoreIDs []int
if vendorPromotionID == "" {
skuIDs := make([]int, len(params.SkuPrices))
skuPriceMap := make(map[int64]*SkuPrice)
for k, v := range params.SkuPrices {
@@ -229,10 +234,7 @@ func CreateJdPromotion(ctx *jxcontext.Context, isIDJd bool, isAsync, isContinueW
sqlParam = append(sqlParam, utils.DefaultTimeValue, skuIDs)
errMsg := ""
modifyPricesList := make(map[int][]*jdapi.SkuPriceInfo)
promotionPrices := make([]map[string]interface{}, len(params.StoreIDs)*len(params.SkuPrices))
index := 0
var jxStoreIDs []int
for _, storeID := range params.StoreIDs {
var skuBinds []*tStoreSkuBindExt
if err = dao.GetRows(db, &skuBinds, sql, append(sqlParam, storeID)...); err != nil {
@@ -282,6 +284,7 @@ func CreateJdPromotion(ctx *jxcontext.Context, isIDJd bool, isAsync, isContinueW
if len(promotionPrices) == 0 {
return "", ErrEmptySkus
}
}
promotion := &model.Promotion{
Name: params.Name,
@@ -292,6 +295,9 @@ func CreateJdPromotion(ctx *jxcontext.Context, isIDJd bool, isAsync, isContinueW
BeginAt: params.BeginAt,
EndAt: params.EndAt,
}
if vendorPromotionID != "" {
promotion.VendorPromotionID = vendorPromotionID
}
dao.WrapAddIDCULDEntity(promotion, userName)
// if promotion.Params, err = jxutils.SerializeData(params); err != nil {
// return "", err
@@ -331,6 +337,7 @@ func CreateJdPromotion(ctx *jxcontext.Context, isIDJd bool, isAsync, isContinueW
}
}
if vendorPromotionID == "" {
promotionHandler := getPromotionHander(params.Type)
if promotionHandler == nil {
return "", errors.New("非法的促销类型")
@@ -402,7 +409,11 @@ func CreateJdPromotion(ctx *jxcontext.Context, isIDJd bool, isAsync, isContinueW
if !isAsync {
_, err = rootTask.GetResult(0)
}
return rootTask.ID, err
hint = rootTask.ID
} else {
dao.Commit(db)
}
return hint, err
}
// func CreatePromotionByExcel(ctx *jxcontext.Context, isAsync bool, promotionType int, fileHeader *multipart.FileHeader, userName string) (hint string, err error) {
@@ -506,7 +517,7 @@ func GetJdPromotions(ctx *jxcontext.Context, keyword string, params map[string]i
}
if params["vendorPromotionID"] != nil {
sql += " AND t1.vendor_promotion_id = ?"
sqlParams = append(sqlParams, params["vendorPromotionID"].(int))
sqlParams = append(sqlParams, params["vendorPromotionID"].(string))
}
if params["name"] != nil {
sql += " AND t1.name LIKE ?"
@@ -651,6 +662,7 @@ func RefreshJdLockStoreSku() (err error) {
}
return err
}
storeskulock.ClearJdStoreSkuLock()
return RefreshJdPromotionItemListLockStatus(promotionItemList)
}
@@ -784,6 +796,33 @@ func RefreshJdPromotionLockStatus(promotionID int) (err error) {
return RefreshJdPromotionItemListLockStatus(promotionItemList)
}
func LockPromotionSkus(ctx *jxcontext.Context, promotionID int, isLock bool, skuIDs []int) (num int64, err error) {
sql := `
UPDATE promotion_sku t1
SET t1.is_lock = ?
WHERE t1.promotion_id = ?
`
intIsLock := 0
if isLock {
intIsLock = 1
}
sqlParams := []interface{}{
intIsLock,
promotionID,
}
if len(skuIDs) > 0 {
sql += " AND t1.sku_id IN (" + dao.GenQuestionMarks(len(skuIDs)) + ")"
sqlParams = append(sqlParams, skuIDs)
}
db := dao.GetDB()
num, err = dao.ExecuteSQL(db, sql, sqlParams...)
if err == nil {
RefreshJdPromotionLockStatus(promotionID)
RefreshJdStoreSkuStock()
}
return num, err
}
func OnStoreStockMsg(msg *jdapi.CallbackStoreStockMsg) (retVal *jdapi.CallbackResponse) {
var err error
globals.SugarLogger.Debugf("OnStoreStockMsg msg:%s", utils.Format4Output(msg, false))

View File

@@ -5,9 +5,10 @@ import (
)
type ICacher interface {
FlushAll() error
FlushKeys(prefix string) error
Set(key string, value interface{}, expiration time.Duration) error
Del(key string) error
Get(key string) interface{}
GetAs(key string, ptr interface{}) error
Keys(prefix string) ([]string, error)
}

View File

@@ -25,8 +25,19 @@ func New(host string, port int, password string) *Cacher {
}
}
func (c *Cacher) FlushAll() error {
func (c *Cacher) FlushKeys(prefix string) (err error) {
if prefix == "" {
return c.client.FlushAll().Err()
}
keys, err := c.Keys(prefix)
if err == nil {
for _, key := range keys {
if err = c.Del(key); err != nil {
break
}
}
}
return err
}
func (c *Cacher) Set(key string, value interface{}, expiration time.Duration) error {
@@ -58,3 +69,7 @@ func (c *Cacher) GetAs(key string, ptr interface{}) error {
}
return err
}
func (c *Cacher) Keys(prefix string) ([]string, error) {
return c.client.Keys(prefix + "*").Result()
}

View File

@@ -3,12 +3,16 @@ package redis
import (
"testing"
"git.rosy.net.cn/jx-callback/business/jxutils/cache"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
"git.rosy.net.cn/jx-callback/globals/beegodb"
"github.com/astaxie/beego"
)
var (
cacher cache.ICacher
)
func init() {
//E:/goprojects/src/git.rosy.net.cn/jx-callback/conf/app.conf
///Users/xujianhua/go/src/git.rosy.net.cn/jx-callback/conf/app.conf
@@ -17,7 +21,9 @@ func init() {
globals.Init()
beegodb.Init()
api.Init()
// api.Init()
cacher = New("localhost", 6379, "")
}
type FuckYou struct {
@@ -25,8 +31,7 @@ type FuckYou struct {
}
func TestIt(t *testing.T) {
cacher := New("localhost", 6379, "")
cacher.FlushAll()
cacher.FlushKeys("")
if cacher.Get("key") != nil {
t.Fatal("should get nothing")
}
@@ -46,3 +51,35 @@ func TestIt(t *testing.T) {
t.Fatal("should get value")
}
}
func TestKeys(t *testing.T) {
cacher.FlushKeys("")
keys, err := cacher.Keys("")
if err != nil {
t.Fatal(err)
}
if len(keys) != 0 {
t.Fatal("keys should be empty here")
}
cacher.Set("key1", 1, 0)
cacher.Set("key2", 2, 0)
cacher.Set("key22", 3, 0)
cacher.Set("key23", 4, 0)
cacher.Set("key24", 5, 0)
keys, err = cacher.Keys("")
if len(keys) != 5 {
t.Fatal("keys should be 5 here")
}
keys, err = cacher.Keys("key")
if len(keys) != 5 {
t.Fatal("keys should be 5 here")
}
keys, err = cacher.Keys("key2")
if len(keys) != 4 {
t.Fatal("keys should be 4 here")
}
keys, err = cacher.Keys("key22")
if len(keys) != 1 {
t.Fatal("keys should be 1 here")
}
}

View File

@@ -5,6 +5,7 @@ import (
"time"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
)
const (
@@ -15,16 +16,20 @@ func LockJdStoreSku(jdStoreID string, jdSkuID int64, expire time.Time) {
globals.SugarLogger.Debug(expire, " ", time.Now())
duration := expire.Sub(time.Now())
if duration > 0 {
globals.Cacher.Set(genCacheKey(jdStoreID, jdSkuID), 1, duration)
api.Cacher.Set(genCacheKey(jdStoreID, jdSkuID), 1, duration)
}
}
func UnlockJdStoreSku(jdStoreID string, jdSkuID int64) {
globals.Cacher.Del(genCacheKey(jdStoreID, jdSkuID))
api.Cacher.Del(genCacheKey(jdStoreID, jdSkuID))
}
func IsJdStoreSkuLocked(jdStoreID string, jdSkuID int64) bool {
return globals.Cacher.Get(genCacheKey(jdStoreID, jdSkuID)) != nil
return api.Cacher.Get(genCacheKey(jdStoreID, jdSkuID)) != nil
}
func ClearJdStoreSkuLock() {
api.Cacher.FlushKeys(cacheKeyPrefix)
}
func genCacheKey(jdStoreID string, jdSkuID int64) string {

View File

@@ -27,6 +27,7 @@ type PromotionController struct {
// @Param isAsync formData bool false "是否异步,缺省否(暂时只支持同步)"
// @Param isContinueWhenError formData bool false "单个同步失败是否继续缺省false"
// @Param advertising formData string false "广告语"
// @Param vendorPromotionID formData string false "厂商活动id设置此字段表示关联京东活动并不会真正去创建"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /CreatePromotion [post]
@@ -52,7 +53,7 @@ func (c *PromotionController) CreatePromotion() {
}
if err = utils.UnmarshalUseNumber([]byte(params.StoreIDs), &promotionParams.StoreIDs); err == nil {
if err = utils.UnmarshalUseNumber([]byte(params.SkuPrices), &promotionParams.SkuPrices); err == nil {
retVal, err = promotion.CreateJdPromotion(params.Ctx, false, params.IsAsync, params.IsContinueWhenError, promotionParams, params.Ctx.GetUserName())
retVal, err = promotion.CreateJdPromotion(params.Ctx, false, params.IsAsync, params.IsContinueWhenError, params.VendorPromotionID, promotionParams, params.Ctx.GetUserName())
}
}
return retVal, "", err
@@ -115,7 +116,7 @@ func (c *PromotionController) SendAdvertingByGoodsOrder() {
// @Param keyword query string false "关键字"
// @Param vendorID query int false "厂商ID当前只支持京东0 "
// @Param promotionID query int false "活动id"
// @Param vendorPromotionID query int false "厂商活动id"
// @Param vendorPromotionID query string false "厂商活动id"
// @Param days query int false "多少天内创建的缺省7天"
// @Param name query string false "促销名,不完全匹配"
// @Param beginAt query string false "开始日期,包括"
@@ -135,10 +136,10 @@ func (c *PromotionController) GetPromotions() {
})
}
// @Title 查询促销
// @Description 查询促销
// @Title 取消促销
// @Description 取消促销
// @Param token header string true "认证token"
// @Param promotionID query int true "活动id"
// @Param promotionID formData int true "活动id"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /CancelPromotion [put]
@@ -148,3 +149,25 @@ func (c *PromotionController) CancelPromotion() {
return retVal, "", err
})
}
// @Title 锁定解锁活动SKU
// @Description 锁定解锁活动SKU
// @Param token header string true "认证token"
// @Param promotionID formData int true "活动id"
// @Param isLock formData bool true "活动id"
// @Param skuIDs formData string false "缺省全部"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /LockPromotionSkus [put]
func (c *PromotionController) LockPromotionSkus() {
c.callLockPromotionSkus(func(params *tPromotionLockPromotionSkusParams) (retVal interface{}, errCode string, err error) {
var skuIDs []int
if params.SkuIDs != "" {
if err = utils.UnmarshalUseNumber([]byte(params.SkuIDs), &skuIDs); err != nil {
return retVal, "", err
}
}
retVal, err = promotion.LockPromotionSkus(params.Ctx, params.PromotionID, params.IsLock, skuIDs)
return retVal, "", err
})
}

View File

@@ -9,6 +9,8 @@ import (
"git.rosy.net.cn/baseapi/platformapi/mtpsapi"
"git.rosy.net.cn/baseapi/platformapi/weixinapi"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils/cache"
"git.rosy.net.cn/jx-callback/business/jxutils/cache/redis"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
@@ -24,6 +26,8 @@ var (
WeixinAPI *weixinapi.API
AutonaviAPI *autonavi.API
QiniuAPI *qbox.Mac
Cacher cache.ICacher
)
func Init() {
@@ -47,6 +51,7 @@ func Init() {
if !beego.AppConfig.DefaultBool("disableEbai", false) {
EbaiAPI = ebaiapi.New(beego.AppConfig.String("ebaiSource"), beego.AppConfig.String("ebaiSecret"))
}
Cacher = redis.New(beego.AppConfig.DefaultString("redisHost", "localhost"), beego.AppConfig.DefaultInt("redisPort", 0), beego.AppConfig.DefaultString("redisPassword", ""))
}
func initElm() {

View File

@@ -2,8 +2,6 @@ package globals
import (
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/jx-callback/business/jxutils/cache"
"git.rosy.net.cn/jx-callback/business/jxutils/cache/redis"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
_ "github.com/go-sql-driver/mysql" // import your used driver
@@ -22,8 +20,6 @@ var (
SugarLogger *zap.SugaredLogger
Cacher cache.ICacher
AliKey string
AliSecret string
@@ -51,8 +47,6 @@ func Init() {
ReallySendWeixinMsg = beego.BConfig.RunMode == "prod"
ReallyCallPlatformAPI = true
Cacher = redis.New(beego.AppConfig.DefaultString("redisHost", "localhost"), beego.AppConfig.DefaultInt("redisPort", 0), beego.AppConfig.DefaultString("redisPassword", ""))
AliKey = beego.AppConfig.DefaultString("aliKey", "")
AliSecret = beego.AppConfig.DefaultString("aliSecret", "")

View File

@@ -239,6 +239,14 @@ func init() {
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:PromotionController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:PromotionController"],
beego.ControllerComments{
Method: "LockPromotionSkus",
Router: `/LockPromotionSkus`,
AllowHTTPMethods: []string{"put"},
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:PromotionController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:PromotionController"],
beego.ControllerComments{
Method: "SendAdvertingByGoodsOrder",