|
@@ -9,7 +9,9 @@ import (
|
|
"fmt"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http"
|
|
"qfw/util"
|
|
"qfw/util"
|
|
|
|
+ "qfw/util/redis"
|
|
"regexp"
|
|
"regexp"
|
|
|
|
+ "sync"
|
|
"time"
|
|
"time"
|
|
. "utils"
|
|
. "utils"
|
|
|
|
|
|
@@ -60,34 +62,92 @@ func GetAppid(tn int64) (appid string) {
|
|
func NewService(w http.ResponseWriter, r *http.Request) {
|
|
func NewService(w http.ResponseWriter, r *http.Request) {
|
|
appid := r.FormValue("appid")
|
|
appid := r.FormValue("appid")
|
|
plan := r.FormValue("plan")
|
|
plan := r.FormValue("plan")
|
|
|
|
+ endtime := r.FormValue("endtime") //时间戳1532500000
|
|
ret := JSON{"msg": "err!"}
|
|
ret := JSON{"msg": "err!"}
|
|
if appid != "" && plan != "" && Mgo.Count("user", `{"appid":"`+appid+`"}`) == 1 {
|
|
if appid != "" && plan != "" && Mgo.Count("user", `{"appid":"`+appid+`"}`) == 1 {
|
|
res, bp := Mgo.FindOne("serviceplan", &map[string]interface{}{
|
|
res, bp := Mgo.FindOne("serviceplan", &map[string]interface{}{
|
|
"_id": plan,
|
|
"_id": plan,
|
|
})
|
|
})
|
|
|
|
+ //查找服务套餐
|
|
if bp && res != nil && *res != nil {
|
|
if bp && res != nil && *res != nil {
|
|
- limit := (*res)["limit"]
|
|
|
|
|
|
+ limit := util.IntAll((*res)["limit"]) //套餐中条数
|
|
t := time.Now()
|
|
t := time.Now()
|
|
- end := t.AddDate(1, 0, 1)
|
|
|
|
|
|
+ st := t.Unix()
|
|
|
|
+ end := t.AddDate(1, 0, 1) //计算一年后的时间
|
|
end1 := time.Date(end.Year(), end.Month(), end.Day(), 0, 0, 0, 0, time.Local)
|
|
end1 := time.Date(end.Year(), end.Month(), end.Day(), 0, 0, 0, 0, time.Local)
|
|
|
|
+ et := end1.Unix()
|
|
|
|
+ //如果参数中传递了到期时间,就替换计算的时间
|
|
|
|
+ if endtime != "" && len(endtime) == 10 {
|
|
|
|
+ et1 := util.Int64All(endtime)
|
|
|
|
+ if et1 > 0 {
|
|
|
|
+ et = et1
|
|
|
|
+ } else {
|
|
|
|
+ log.Println("传入日期参数错误", endtime)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
//log.Debug(t.Unix(), "-", end.Unix(), util.FormatDate(&t, util.Date_Full_Layout), util.FormatDate(&end1, util.Date_Full_Layout))
|
|
//log.Debug(t.Unix(), "-", end.Unix(), util.FormatDate(&t, util.Date_Full_Layout), util.FormatDate(&end1, util.Date_Full_Layout))
|
|
id := Mgo.Save("buyrecord", &map[string]interface{}{"appid": appid, "plan": plan, "createtime": t.Unix(), "starttime": t.Unix(), "endtime": end1.Unix()})
|
|
id := Mgo.Save("buyrecord", &map[string]interface{}{"appid": appid, "plan": plan, "createtime": t.Unix(), "starttime": t.Unix(), "endtime": end1.Unix()})
|
|
if id != "" {
|
|
if id != "" {
|
|
- //后结考虑延期时间
|
|
|
|
- if Mgo.Update("user", `{"appid":"`+appid+`"}`, &map[string]interface{}{
|
|
|
|
- "$set": map[string]interface{}{
|
|
|
|
- "plan": map[string]interface{}{
|
|
|
|
- "name": plan,
|
|
|
|
- "starttime": t.Unix(),
|
|
|
|
- "endtime": end1.Unix(),
|
|
|
|
- "recordid": id,
|
|
|
|
- "current": limit,
|
|
|
|
|
|
+ //取出此appid的锁
|
|
|
|
+ GetDataMapLock.Lock()
|
|
|
|
+ appidLock := GetDataMap[appid]
|
|
|
|
+ if appidLock == nil {
|
|
|
|
+ appidLock = &sync.Mutex{}
|
|
|
|
+ GetDataMap[appid] = appidLock
|
|
|
|
+ }
|
|
|
|
+ GetDataMapLock.Unlock()
|
|
|
|
+ appidLock.Lock()
|
|
|
|
+ defer appidLock.Unlock()
|
|
|
|
+ //查找用户信息
|
|
|
|
+ u, b := Mgo.FindOne("user", `{"appid":"`+appid+`"}`)
|
|
|
|
+ if b && u != nil {
|
|
|
|
+ oldplan := (*u)["plan"]
|
|
|
|
+ limitnum := redis.Get(REDISDB, "limitnum_"+appid) //取redis中的剩余量
|
|
|
|
+ oplimit := 0
|
|
|
|
+ if oldplan != nil {
|
|
|
|
+ op := oldplan.(map[string]interface{})
|
|
|
|
+ opst := util.Int64All(op["starttime"])
|
|
|
|
+ opet := util.Int64All(op["endtime"])
|
|
|
|
+ oplimit = util.IntAll(op["current"])
|
|
|
|
+ if limitnum != nil {
|
|
|
|
+ oplimit = util.IntAll(limitnum)
|
|
|
|
+ }
|
|
|
|
+ if opst > 0 && opet > 0 && oplimit > -1 {
|
|
|
|
+ st = opst
|
|
|
|
+ if et < opet {
|
|
|
|
+ et = opet
|
|
|
|
+ log.Println("结束日期没原来日期大,替换")
|
|
|
|
+ }
|
|
|
|
+ limit += oplimit
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if Mgo.Update("user", `{"appid":"`+appid+`"}`, &map[string]interface{}{
|
|
|
|
+ "$set": map[string]interface{}{
|
|
|
|
+ "plan": map[string]interface{}{
|
|
|
|
+ "name": plan,
|
|
|
|
+ "starttime": st,
|
|
|
|
+ "endtime": et,
|
|
|
|
+ "recordid": id,
|
|
|
|
+ "current": limit,
|
|
|
|
+ },
|
|
},
|
|
},
|
|
- },
|
|
|
|
- }, false, false) {
|
|
|
|
- ret["msg"] = "ok"
|
|
|
|
|
|
+ }, false, false) {
|
|
|
|
+ ret["msg"] = "ok"
|
|
|
|
+ redis.Del(REDISDB, "limitnum_"+appid) //清除在redis中剩余量
|
|
|
|
+ Mgo.UpdateById("buyrecord", id, &map[string]interface{}{
|
|
|
|
+ "$set": map[string]interface{}{
|
|
|
|
+ "beforecount": oplimit,
|
|
|
|
+ "aftercount": limit,
|
|
|
|
+ },
|
|
|
|
+ })
|
|
|
|
+ log.Println("更新套餐成功:", appid)
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ log.Println("appid不存在", appid)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ } else {
|
|
|
|
+ log.Println("查找套餐不存在", appid, plan)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
WriteJSON(w, &ret)
|
|
WriteJSON(w, &ret)
|