|
@@ -1,22 +1,22 @@
|
|
package creditrpc
|
|
package creditrpc
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "log"
|
|
|
|
+ "qfw/consts"
|
|
"qfw/creditlog"
|
|
"qfw/creditlog"
|
|
|
|
+ "qfw/util"
|
|
|
|
+ "qfw/util/redis"
|
|
|
|
+ "qfw/util/rpc"
|
|
"sync"
|
|
"sync"
|
|
|
|
+ "time"
|
|
)
|
|
)
|
|
|
|
|
|
-type CreditData struct {
|
|
|
|
- code string //积分代码
|
|
|
|
- uid string //用户
|
|
|
|
- num int //积分值
|
|
|
|
- otherParam map[string]interface{}
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
type CreditRpc struct{}
|
|
type CreditRpc struct{}
|
|
|
|
|
|
var Message map[string]string
|
|
var Message map[string]string
|
|
var Score map[string]int
|
|
var Score map[string]int
|
|
|
|
|
|
|
|
+//用户访问redis加锁
|
|
var SN = 100
|
|
var SN = 100
|
|
var SLM []*sync.Mutex = make([]*sync.Mutex, SN)
|
|
var SLM []*sync.Mutex = make([]*sync.Mutex, SN)
|
|
|
|
|
|
@@ -27,41 +27,79 @@ func init() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func getMutex(uid string) *sync.Mutex {
|
|
|
|
- h := 0
|
|
|
|
- l := len(uid)
|
|
|
|
- for i := 0; i < l; i++ {
|
|
|
|
- n1 := int([]rune(uid[i : i+1])[0])
|
|
|
|
- n2 := n1 >> 1
|
|
|
|
- h += (n1+n2)<<2 + n1
|
|
|
|
- }
|
|
|
|
- return SLM[h%SN]
|
|
|
|
|
|
+func GetMutex(uid string) *sync.Mutex {
|
|
|
|
+ return SLM[util.HashCode(uid)%SN]
|
|
}
|
|
}
|
|
|
|
|
|
//增加积分
|
|
//增加积分
|
|
-func (c *CreditRpc) InCreadit(param *CreditData, replay *int) error {
|
|
|
|
|
|
+func (c *CreditRpc) InCreadit(param *rpc.CreditData, replay *int) error {
|
|
//a类为即时保存
|
|
//a类为即时保存
|
|
//b、c、d、e为定时保存
|
|
//b、c、d、e为定时保存
|
|
|
|
+ log.Println("---请求", param)
|
|
*replay = 0
|
|
*replay = 0
|
|
- if len(param.code) > 1 {
|
|
|
|
- first := param.code[:1]
|
|
|
|
|
|
+ if len(param.Code) > 1 {
|
|
|
|
+ first := param.Code[:1]
|
|
//num := Score[param.code]
|
|
//num := Score[param.code]
|
|
|
|
+ creditDoc := map[string]interface{}{
|
|
|
|
+ "s_uid": param.Uid,
|
|
|
|
+ "s_code": param.Code,
|
|
|
|
+ "i_type": 1,
|
|
|
|
+ "l_date": time.Now().Unix(),
|
|
|
|
+ }
|
|
switch first {
|
|
switch first {
|
|
case "a", "d", "e": //立即生效
|
|
case "a", "d", "e": //立即生效
|
|
- if creditlog.Save(map[string]interface{}{}) {
|
|
|
|
|
|
+ creditDoc["i_score"] = Score[param.Code]
|
|
|
|
+ if creditlog.Save(creditDoc) {
|
|
*replay = 1
|
|
*replay = 1
|
|
}
|
|
}
|
|
case "b":
|
|
case "b":
|
|
- //key := param.code + "_" + param.uid
|
|
|
|
|
|
+ key := param.Code + "_" + param.Uid
|
|
|
|
+ //[0/1,time] //是否完成,次数
|
|
|
|
+ lock := GetMutex(param.Uid)
|
|
|
|
+ lock.Lock()
|
|
|
|
+ obj := redis.Get(consts.RedisDB, key)
|
|
|
|
+ if obj != nil {
|
|
|
|
+ newobj := obj.([]int)
|
|
|
|
+ if newobj[0] == 0 {
|
|
|
|
+ newobj[1] += 1
|
|
|
|
+ if newobj[1] == Score[param.Code+"_n"] {
|
|
|
|
+ newobj[0] = 1
|
|
|
|
+ }
|
|
|
|
+ redis.Put(consts.RedisDB, key, newobj, GetSubSecond())
|
|
|
|
+ creditDoc["i_score"] = Score[param.Code]
|
|
|
|
+ creditlog.AddLog(creditDoc)
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ newobj := []int{0, 0}
|
|
|
|
+ newobj[1] += 1
|
|
|
|
+ if newobj[1] == Score[param.Code+"_n"] {
|
|
|
|
+ newobj[0] = 1
|
|
|
|
+ }
|
|
|
|
+ creditDoc["i_score"] = Score[param.Code+"_1"]
|
|
|
|
+ redis.Put(consts.RedisDB, key, newobj, GetSubSecond())
|
|
|
|
+ creditlog.AddLog(creditDoc)
|
|
|
|
+ }
|
|
|
|
+ lock.Unlock()
|
|
|
|
+ //
|
|
default: //定时任务
|
|
default: //定时任务
|
|
- creditlog.AddLog(map[string]interface{}{})
|
|
|
|
- *replay = 1
|
|
|
|
|
|
+ sc := util.If(param.Num > 0, param.Num, Score[param.Code]).(int)
|
|
|
|
+ if sc > 0 {
|
|
|
|
+ creditDoc["i_score"] = sc
|
|
|
|
+ creditlog.AddLog(creditDoc)
|
|
|
|
+ *replay = 1
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
//消费积分
|
|
//消费积分
|
|
-func (c *CreditRpc) OutCreadit(param *CreditData, replay *int) error {
|
|
|
|
|
|
+func (c *CreditRpc) OutCreadit(param *rpc.CreditData, replay *int) error {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func GetSubSecond() int {
|
|
|
|
+ now := time.Now()
|
|
|
|
+ tom := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, time.Local)
|
|
|
|
+ return int(tom.Unix() - now.Unix())
|
|
|
|
+}
|