iedc-go/example/app/favorite.go
2022-05-03 22:40:02 +08:00

242 lines
7.8 KiB
Go

package app
import (
. "code.hoteas.com/golang/hotime"
. "code.hoteas.com/golang/hotime/common"
)
var FavoriteCtr = Ctr{
//关注
"follow": func(that *Context) {
if that.Session("user_id").Data == nil {
that.Display(2, "没有登录")
return
}
policyId := ObjToInt(that.Req.FormValue("policy_id"))
notifyId := ObjToInt(that.Req.FormValue("notify_id"))
declareId := ObjToInt(that.Req.FormValue("declare_id"))
providerId := ObjToInt(that.Req.FormValue("provider_id"))
favorite := Map{}
data := Map{}
if providerId != 0 {
data = Map{"provider_id": providerId, "user_id": that.Session("user_id").Data}
favorite = that.Db.Get("favorite", "*", Map{"AND": data})
data["modify_time[#]"] = "now()"
data["del_flag"] = 0
data["type"] = 1
} else if notifyId != 0 {
article := that.Db.Get("article", "id", Map{"notify_id": notifyId})
if article != nil {
data = Map{"notify_id": notifyId, "article_id": article.GetCeilInt("id"), "user_id": that.Session("user_id").Data}
favorite = that.Db.Get("favorite", "*", Map{"AND": data})
data["modify_time[#]"] = "now()"
data["del_flag"] = 0
data["type"] = 2
}
} else if policyId != 0 {
article := that.Db.Get("article", "id", Map{"policy_id": policyId})
if article != nil {
data = Map{"policy_id": policyId, "article_id": article.GetCeilInt("id"), "user_id": that.Session("user_id").Data}
favorite = that.Db.Get("favorite", "*", Map{"AND": data})
data["modify_time[#]"] = "now()"
data["del_flag"] = 0
data["type"] = 3
}
} else if declareId != 0 {
article := that.Db.Get("article", "id", Map{"declare_id": declareId})
if article != nil {
data = Map{"declare_id": declareId, "article_id": article.GetCeilInt("id"), "user_id": that.Session("user_id").Data}
favorite = that.Db.Get("favorite", "*", Map{"AND": data})
data["modify_time[#]"] = "now()"
data["del_flag"] = 0
data["type"] = 4
}
}
if len(data) != 0 {
isFavorite := int64(0)
if favorite != nil {
isFavorite = that.Db.Update("favorite", data, Map{"id": favorite.GetCeilInt("id")})
} else {
data["create_time[#]"] = "now()"
isFavorite = that.Db.Insert("favorite", data)
}
if isFavorite != 0 {
if data.GetCeilInt("article_id") != 0 {
that.Db.Update("article", Map{"favorite_num[#]": "favorite_num+1"}, Map{"id": data.GetCeilInt("article_id")})
}
if data.GetCeilInt("notify_id") != 0 {
that.Db.Update("notify", Map{"favorite_num[#]": "favorite_num+1"}, Map{"id": data.GetCeilInt("notify_id")})
}
if data.GetCeilInt("policy_id") != 0 {
that.Db.Update("policy", Map{"favorite_num[#]": "favorite_num+1"}, Map{"id": data.GetCeilInt("policy_id")})
}
if data.GetCeilInt("declare_id") != 0 {
that.Db.Update("declare", Map{"favorite_num[#]": "favorite_num+1"}, Map{"id": data.GetCeilInt("declare_id")})
}
if data.GetCeilInt("provider_id") != 0 {
that.Db.Update("provider", Map{"favorite_num[#]": "favorite_num+1"}, Map{"id": data.GetCeilInt("provider_id")})
}
}
that.Display(0, "关注成功")
return
}
that.Display(4, "找不到关注对象")
return
},
//关注
"unfollow": func(that *Context) {
if that.Session("user_id").Data == nil {
that.Display(2, "没有登录")
return
}
policyId := ObjToInt(that.Req.FormValue("policy_id"))
notifyId := ObjToInt(that.Req.FormValue("notify_id"))
declareId := ObjToInt(that.Req.FormValue("declare_id"))
providerId := ObjToInt(that.Req.FormValue("provider_id"))
favorite := Map{}
data := Map{}
if providerId != 0 {
data = Map{"provider_id": providerId, "user_id": that.Session("user_id").Data}
favorite = that.Db.Get("favorite", "*", Map{"AND": data})
data["modify_time[#]"] = "now()"
data["del_flag"] = 1
data["type"] = 1
} else if notifyId != 0 {
article := that.Db.Get("article", "id", Map{"notify_id": notifyId})
if article != nil {
data = Map{"notify_id": notifyId, "article_id": article.GetCeilInt("id"), "user_id": that.Session("user_id").Data}
favorite = that.Db.Get("favorite", "*", Map{"AND": data})
data["modify_time[#]"] = "now()"
data["del_flag"] = 1
data["type"] = 2
}
} else if policyId != 0 {
article := that.Db.Get("article", "id", Map{"policy_id": policyId})
if article != nil {
data = Map{"policy_id": policyId, "article_id": article.GetCeilInt("id"), "user_id": that.Session("user_id").Data}
favorite = that.Db.Get("favorite", "*", Map{"AND": data})
data["modify_time[#]"] = "now()"
data["del_flag"] = 1
data["type"] = 3
}
} else if declareId != 0 {
article := that.Db.Get("article", "id", Map{"declare_id": declareId})
if article != nil {
data = Map{"declare_id": declareId, "article_id": article.GetCeilInt("id"), "user_id": that.Session("user_id").Data}
favorite = that.Db.Get("favorite", "*", Map{"AND": data})
data["modify_time[#]"] = "now()"
data["del_flag"] = 1
data["type"] = 4
}
}
if len(data) != 0 && favorite != nil {
isFavorite := that.Db.Update("favorite", data, Map{"id": favorite.GetCeilInt("id")})
if isFavorite != 0 {
if data.GetCeilInt("article_id") != 0 {
that.Db.Update("article", Map{"favorite_num[#]": "favorite_num-1"}, Map{"id": data.GetCeilInt("article_id")})
}
if data.GetCeilInt("notify_id") != 0 {
that.Db.Update("notify", Map{"favorite_num[#]": "favorite_num-1"}, Map{"id": data.GetCeilInt("notify_id")})
}
if data.GetCeilInt("policy_id") != 0 {
that.Db.Update("policy", Map{"favorite_num[#]": "favorite_num-1"}, Map{"id": data.GetCeilInt("policy_id")})
}
if data.GetCeilInt("declare_id") != 0 {
that.Db.Update("declare", Map{"favorite_num[#]": "favorite_num-1"}, Map{"id": data.GetCeilInt("declare_id")})
}
if data.GetCeilInt("provider_id") != 0 {
that.Db.Update("provider", Map{"favorite_num[#]": "favorite_num-1"}, Map{"id": data.GetCeilInt("provider_id")})
}
}
that.Display(0, "取消关注成功")
return
}
that.Display(0, "没有关注")
return
},
"search": func(that *Context) {
if that.Session("user_id").Data == nil {
that.Display(2, "没有登录")
return
}
page := ObjToInt(that.Req.FormValue("page"))
pageSize := ObjToInt(that.Req.FormValue("pageSize"))
tp := ObjToInt(that.Req.FormValue("type"))
if page < 1 {
page = 1
}
if pageSize <= 0 {
pageSize = 20
}
data := Map{"del_flag": 0, "user_id": that.Session("user_id").Data}
if tp != 0 {
data["type"] = tp
}
if len(data) > 1 {
data = Map{"AND": data, "ORDER": "modify_time DESC"}
}
count := that.Db.Count("favorite", data)
res := that.Db.Page(page, pageSize).PageSelect("favorite", "*", data)
for _, v := range res {
if v.GetCeilInt64("policy_id") != 0 {
v["policy"] = that.Db.Get("policy", "id,tag", Map{"id": v.GetCeilInt64("policy_id")})
}
if v.GetCeilInt64("declare_id") != 0 {
v["declare"] = that.Db.Get("declare", "id,money_scope_min,money_scope_max,status", Map{"id": v.GetCeilInt64("declare_id")})
}
if v.GetCeilInt64("notify_id") != 0 {
v["notify"] = that.Db.Get("notify", "id,tag", Map{"id": v.GetCeilInt64("notify_id")})
}
if v.GetCeilInt64("article_id") != 0 {
v["article"] = that.Db.Get("article", "id,title,description,department_id,click_num+click_num_base AS click_num,"+
"favorite_num_base+favorite_num AS favorite_num,dispatch_num,dispatch_name,prepare_date,release_time,expire_date,area_id,status", Map{"id": v.GetCeilInt64("article_id")})
}
if v.GetCeilInt64("provider_id") != 0 {
v["provider"] = that.Db.Get("provider", "id,name,level,discount,avatar,title,description,"+
"click_num_base+click_num AS click_num,handle_num_base+handle_num AS handle_num,favorite_num_base+favorite_num AS favorite_num,modify_time", Map{"id": v.GetCeilInt64("provider_id")})
}
}
that.Display(0, Map{"total": count, "data": res})
},
}