hotime/example/app/order.go
2022-05-06 11:35:40 +08:00

209 lines
5.9 KiB
Go

package app
import (
. "code.hoteas.com/golang/hotime"
. "code.hoteas.com/golang/hotime/common"
"time"
)
var OrderCtr = Ctr{
//创建订单
"create": func(that *Context) {
if that.Session("user_id").Data == nil {
that.Display(2, "没有登录")
return
}
providerId := ObjToInt(that.Req.FormValue("provider_id"))
phone := that.Req.FormValue("phone")
companyName := that.Req.FormValue("company_name")
userName := that.Req.FormValue("user_name")
if providerId == 0 || len(phone) != 11 || len(companyName) < 4 || len(userName) < 2 {
that.Display(3, "请求参数异常")
return
}
err := auth(that, phone, companyName, userName)
if err != nil {
that.Display(3, err.Error())
return
}
declareId := ObjToInt(that.Req.FormValue("declare_id"))
tp := that.Req.FormValue("type")
//新建流程
user := that.Db.Get("user", "*", Map{"id": that.Session("user_id").Data})
if user == nil {
that.Display(4, "找不到用户")
return
}
company := that.Db.Get("company", "name", Map{"id": user.GetCeilInt("company_id")})
if company == nil {
that.Display(4, "找不到企业")
return
}
provider := that.Db.Get("provider", "*", Map{"id": providerId})
if provider == nil {
that.Display(4, "找不到该服务商")
return
}
//是否以前已经创建了该服务商的订单,如果创建了则直接跳转到订单详情中去
oldOrder := that.Db.Get("order", "id", Map{"AND": Map{"provider_id": providerId, "user_id": that.Session("user_id").Data, "del_flag": 0, "status": 0}})
if oldOrder != nil {
orderRecord := Map{
"order_id": oldOrder.GetCeilInt64("id"),
"user_id": user.GetCeilInt64("id"),
"remarks": user.GetString("nickname") + "向“" + provider.GetString("name") + "”服务商再次发起了订单请求",
"create_time[#]": "now()",
"modify_time[#]": "now()",
"del_flag": 0,
}
orderRecord["id"] = that.Db.Insert("order_record", orderRecord)
that.Db.Update("order", Map{"order_record_id": orderRecord.GetCeilInt64("id"), "modify_time[#]": "now()"}, Map{"id": oldOrder.GetCeilInt64("id")})
that.Display(0, oldOrder.GetCeilInt64("id"))
return
}
data := Map{
"name": "购买“" + provider.GetString("title") + "”服务",
"sn": "SN" + time.Now().Format("20060102150405") + getSn(),
"user_id": user.GetCeilInt64("id"),
"salesman_id": provider.GetCeilInt64("salesman_id"),
"provider_id": provider.GetCeilInt64("id"),
"company_id": company.GetCeilInt64("id"),
"company_name": company.GetString("name"),
"phone": user.GetString("phone"),
"create_time[#]": "now()",
"modify_time[#]": "now()",
"del_flag": 0,
}
if declareId != 0 {
data["policy_declare_flag"] = 1
data["declare_id"] = declareId
}
if tp == "policy_declare_flag" {
data["policy_declare_flag"] = 1
}
data["id"] = that.Db.Insert("order", data)
if data.GetCeilInt64("id") == 0 {
that.Display(4, "无法生成订单!")
return
}
orderRecord := Map{
"order_id": data["id"],
"user_id": user.GetCeilInt64("id"),
"remarks": user.GetString("nickname") + "向“" + provider.GetString("name") + "”服务商发起了订单请求",
"create_time[#]": "now()",
"modify_time[#]": "now()",
"del_flag": 0,
}
orderRecord["id"] = that.Db.Insert("order_record", orderRecord)
if orderRecord.GetCeilInt64("id") == 0 {
that.Display(4, "无法生成订单记录!")
return
}
that.Db.Update("order", Map{"order_record_id": orderRecord.GetCeilInt64("id")}, Map{"id": data.GetCeilInt64("id")})
that.Display(0, data.GetCeilInt64("id"))
},
"info": func(that *Context) {
if that.Session("user_id").Data == nil {
that.Display(2, "没有登录")
return
}
id := ObjToInt(that.Req.FormValue("id"))
if id == 0 {
that.Display(3, "请求参数异常")
return
}
res := that.Db.Get("order", "*", Map{"AND": Map{"id": id, "user_id": that.Session("user_id").Data}})
if res == nil {
that.Display(4, "找不到对应订单")
return
}
if res.GetCeilInt("provider_id") > 0 {
res["provider"] = that.Db.Get("provider", "name,title,phone", Map{"id": res.GetCeilInt("provider_id")})
}
res["order_record"] = that.Db.Select("order_record", "remarks,modify_time", Map{"order_id": res.GetCeilInt("id"), "ORDER": "modify_time DESC"})
that.Display(0, res)
},
//用户微信公众号或者小程序登录
"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"))
if page < 1 {
page = 1
}
if pageSize <= 0 {
pageSize = 20
}
data := Map{"del_flag": 0, "user_id": that.Session("user_id").Data}
keywords := that.Req.FormValue("keywords")
if keywords != "" {
data["OR"] = Map{"sn[~]": keywords, "company_name[~]": keywords, "name[~]": keywords}
}
startTime := that.Req.FormValue("starttime")
finishTime := that.Req.FormValue("finishtime")
if startTime != "" {
data["modify_time[>=]"] = startTime
}
if finishTime != "" {
data["modify_time[<=]"] = finishTime
}
if len(data) > 1 {
data = Map{"AND": data, "ORDER": "modify_time DESC"}
}
count := that.Db.Count("order", data)
res := that.Db.Page(page, pageSize).PageSelect("order", "*", data)
for _, v := range res {
//if v.GetCeilInt("policy_id")>0{
// v["policy"]=that.Db.Get("policy","id,tag",Map{"id":v.GetCeilInt("policy_id")})
//}
if v.GetCeilInt("provider_id") > 0 {
v["provider"] = that.Db.Get("provider", "name,title", Map{"id": v.GetCeilInt("provider_id")})
}
if v.GetCeilInt("order_record_id") > 0 {
v["order_record"] = that.Db.Get("order_record", "remarks,modify_time", Map{"id": v.GetCeilInt("order_record_id")})
}
}
that.Display(0, Map{"total": count, "data": res})
},
}