Forráskód Böngészése

修改积分逻辑

renzheng 9 éve
szülő
commit
764f083f2d

+ 1 - 0
common/src/qfw/util/mongodb/mongodbutil.go

@@ -97,6 +97,7 @@ func Update(c string, query interface{}, set interface{}, upsert bool, multi boo
 	if sess != nil {
 		defer func() {
 			if r := recover(); r != nil {
+				b = false
 				log.Println("[E]", r)
 				for skip := 1; ; skip++ {
 					_, file, line, ok := runtime.Caller(skip)

+ 44 - 4
credit/src/qfw/creditlog/creditlog.go

@@ -3,6 +3,7 @@ package creditlog
 import (
 	"encoding/json"
 	"fmt"
+	"gopkg.in/mgo.v2/bson"
 	"qfw/util"
 	"qfw/util/mail"
 	"qfw/util/mongodb"
@@ -35,9 +36,20 @@ func AddLog(logs map[string]interface{}) {
 func Save(doc map[string]interface{}) bool {
 	b := len(mongodb.Save(TB, doc)) > 0
 	if !b {
-		redis.Put("credit", fmt.Sprintf("save_one_fail_%d", time.Now()), doc, 0)
+		redis.Put("credit", fmt.Sprintf("save_one_fail_%d", time.Now().Unix()), doc, 0)
 		v, _ := json.Marshal(doc)
-		Failed(string(v))
+		Failed("保存到日志记录失败," + string(v))
+	} else {
+		//更新积分 i_credit到人
+		b = mongodb.Update("user", `{"_id":"`+doc["s_uid"].(string)+`"}`, &map[string]interface{}{
+			"$inc": map[string]int{
+				"i_credit": doc["i_score"].(int),
+			},
+		}, false, false)
+		if !b {
+			v, _ := json.Marshal(doc)
+			Failed("更新mongodb中积分失败," + string(v))
+		}
 	}
 	return b
 }
@@ -52,9 +64,37 @@ func SaveLog() {
 			b := mongodb.SaveBulk(TB, tmp...)
 			if !b {
 				//如果保存失败
-				redis.Put("credit", fmt.Sprintf("save_bulk_fail_%d", time.Now()), tmp, 0)
+				redis.Put("credit", fmt.Sprintf("save_bulk_fail_%d", time.Now().Unix()), tmp, 0)
 				v, _ := json.Marshal(tmp)
-				Failed(string(v))
+				Failed("批量保存到日志记录失败," + string(v))
+			} else {
+				//保存积分到人
+				sess := mongodb.GetMgoConn()
+				defer mongodb.DestoryMongoConn(sess)
+				errIds := []string{}
+				if sess != nil {
+					coll := sess.DB("qfw").C("user")
+					var err error
+					for _, v := range tmp {
+						err = coll.UpdateId(bson.ObjectIdHex(v["uid"].(string)), &map[string]interface{}{
+							"$inc": map[string]int{
+								"i_credit": v["i_score"].(int),
+							},
+						})
+						if err != nil {
+							errIds = append(errIds, v["uid"].(string)+","+err.Error())
+						}
+					}
+
+				} else {
+					b = false
+				}
+				if !b {
+					redis.Put("credit", fmt.Sprintf("save_bulk_fail_%d", time.Now().Unix()), tmp, 0)
+					v, _ := json.Marshal(tmp)
+					vids, _ := json.Marshal(errIds)
+					Failed("错误id:" + string(vids) + ",批量更新到mongodb中积分失败," + string(v))
+				}
 			}
 		}
 	}()

+ 3 - 2
credit/src/qfw/creditrpc/creditrpc.go

@@ -70,9 +70,10 @@ func (c *CreditRpc) InCreadit(param *rpc.CreditData, replay *int) error {
 					creditlog.AddLog(creditDoc)
 				}
 			} else {
-				newobj := []int{0, 0}
+				times := Score[param.Code+"_n"]
+				newobj := []int{0, 0, times}
 				newobj[1] += 1
-				if newobj[1] == Score[param.Code+"_n"] {
+				if newobj[1] == times {
 					newobj[0] = 1
 				}
 				creditDoc["i_score"] = Score[param.Code+"_1"]