瀏覽代碼

t积分管理

renzheng 9 年之前
父節點
當前提交
07625b67d4

+ 10 - 0
common/src/qfw/util/common.go

@@ -331,3 +331,13 @@ func If(b bool, to, fo interface{}) interface{} {
 		return fo
 	}
 }
+
+//HashCode值
+func HashCode(uid string) int {
+	var h uint32 = 0
+	rs := []rune(uid)
+	for i := 0; i < len(rs); i++ {
+		h = 31*h + uint32(rs[i])
+	}
+	return int(h)
+}

+ 7 - 1
credit/src/main.go

@@ -1,15 +1,18 @@
 package main
 
 import (
+	"log"
 	"net"
 	"net/http"
 	"net/rpc"
+	"qfw/consts"
 	"qfw/creditlog"
 	"qfw/creditrpc"
 	"qfw/timewheel"
 	"qfw/util"
 	"qfw/util/mongodb"
 	"qfw/util/redis"
+	"strings"
 	"time"
 )
 
@@ -21,7 +24,9 @@ func init() {
 	mongodb.InitMongodbPool(util.IntAll(SysConfig["mongodbPoolSize"]), util.ObjToString(SysConfig["mongodbServers"]),
 		util.ObjToString(SysConfig["mongodbName"]))
 	//初始化redis
-	redis.InitRedisBySize(util.ObjToString(SysConfig["redisServers"]), util.IntAllDef(SysConfig["redisSize"], 100),
+	redisServer := util.ObjToString(SysConfig["redisServers"])
+	consts.RedisDB = strings.Split(redisServer, "=")[0]
+	redis.InitRedisBySize(redisServer, util.IntAllDef(SysConfig["redisSize"], 100),
 		util.IntAllDef(SysConfig["redisIdle"], 50), util.IntAllDef(SysConfig["redisTimeout"], 180))
 	creditrpc.Score = map[string]int{}
 	for k, v := range SysConfig["num"].(map[string]interface{}) {
@@ -44,6 +49,7 @@ func main() {
 	l, _ := net.Listen("tcp", ":"+port)
 	go http.Serve(l, nil)
 	quartz()
+	log.Println("启动积分系统", port)
 	flag := make(chan bool)
 	<-flag
 }

+ 4 - 0
credit/src/qfw/consts/consts.go

@@ -0,0 +1,4 @@
+package consts
+
+//redis表名
+var RedisDB string

+ 2 - 2
credit/src/qfw/creditlog/creditlog.go

@@ -34,7 +34,7 @@ func AddLog(logs map[string]interface{}) {
 
 func Save(doc map[string]interface{}) bool {
 	b := len(mongodb.Save(TB, doc)) > 0
-	if true || !b {
+	if !b {
 		redis.Put("credit", fmt.Sprintf("save_one_fail_%d", time.Now()), doc, 0)
 		v, _ := json.Marshal(doc)
 		Failed(string(v))
@@ -50,7 +50,7 @@ func SaveLog() {
 	go func() {
 		if len(tmp) > 0 {
 			b := mongodb.SaveBulk(TB, tmp...)
-			if true || !b {
+			if !b {
 				//如果保存失败
 				redis.Put("credit", fmt.Sprintf("save_bulk_fail_%d", time.Now()), tmp, 0)
 				v, _ := json.Marshal(tmp)

+ 62 - 24
credit/src/qfw/creditrpc/creditrpc.go

@@ -1,22 +1,22 @@
 package creditrpc
 
 import (
+	"log"
+	"qfw/consts"
 	"qfw/creditlog"
+	"qfw/util"
+	"qfw/util/redis"
+	"qfw/util/rpc"
 	"sync"
+	"time"
 )
 
-type CreditData struct {
-	code       string //积分代码
-	uid        string //用户
-	num        int    //积分值
-	otherParam map[string]interface{}
-}
-
 type CreditRpc struct{}
 
 var Message map[string]string
 var Score map[string]int
 
+//用户访问redis加锁
 var SN = 100
 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类为即时保存
 	//b、c、d、e为定时保存
+	log.Println("---请求", param)
 	*replay = 0
-	if len(param.code) > 1 {
-		first := param.code[:1]
+	if len(param.Code) > 1 {
+		first := param.Code[:1]
 		//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 {
 		case "a", "d", "e": //立即生效
-			if creditlog.Save(map[string]interface{}{}) {
+			creditDoc["i_score"] = Score[param.Code]
+			if creditlog.Save(creditDoc) {
 				*replay = 1
 			}
 		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: //定时任务
-			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
 }
 
 //消费积分
-func (c *CreditRpc) OutCreadit(param *CreditData, replay *int) error {
+func (c *CreditRpc) OutCreadit(param *rpc.CreditData, replay *int) error {
 	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())
+}

+ 23 - 0
credit/src/qfw/creditrpc/creditrpc_test.go

@@ -0,0 +1,23 @@
+package creditrpc
+
+import (
+	"log"
+	"net/rpc"
+	r "qfw/util/rpc"
+	"testing"
+)
+
+func Test_rpc(t *testing.T) {
+	log.Println("----------")
+	dd := r.CreditData{
+		Code: "ddddd",
+	}
+	var repl int
+	clent, _ := rpc.DialHTTP("tcp", "127.0.0.1:8765")
+	clent.Call("CreditRpc.InCreadit", &dd, &repl)
+
+	log.Println(repl)
+	clent.Close()
+	c := make(chan bool, 1)
+	<-c
+}

+ 28 - 14
credit/src/qfw/timewheel/timewheel_test.go

@@ -2,27 +2,41 @@ package timingwheel
 
 import (
 	"log"
+	"sync"
 	"testing"
 	"time"
 )
 
 func Test_t(t *testing.T) {
-	uid := "568418e062684db687cb05e3"
-	h := 0
-	l := len(uid)
-	for i := 0; i < l; i++ {
-		n1 := int([]rune(uid[i : i+1])[0])
-		if n1%2 == 0 {
-			h -= (n1 + h>>uint(n1%3)) << uint(h%2)
-		} else {
-			h += (n1 + h>>uint(n1%3)) << uint(h%2)
+	locks := new(sync.Mutex)
+	go func() {
+		for {
+			time.Sleep(3 * time.Second)
+			locks.Lock()
+			log.Println("1111111")
+			locks.Unlock()
 		}
 
-	}
-	if h < 0 {
-		h = -h
-	}
-	log.Println(h)
+	}()
+
+	go func() {
+		for {
+			time.Sleep(3 * time.Second)
+			lock := locks
+			lock.Lock()
+			log.Println("2222222")
+
+			lock.Unlock()
+		}
+	}()
+
+	now := time.Now()
+	tom := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, time.Local)
+	log.Println(tom, tom.Unix()-now.Unix())
+
+	c := make(chan bool, 1)
+	<-c
+
 }
 
 func Test_wheel(t *testing.T) {