iedc-go/example/app/vip_order.go

204 lines
6.1 KiB
Go
Raw Normal View History

2022-05-03 14:40:02 +00:00
package app
import (
. "code.hoteas.com/golang/hotime"
2022-05-04 16:29:30 +00:00
. "code.hoteas.com/golang/hotime/common"
"code.hoteas.com/golang/hotime/dri/wechat"
"fmt"
"time"
2022-05-03 14:40:02 +00:00
)
var VipOrderCtr = Ctr{
//创建V订单
"create": func(that *Context) {
if that.Session("user_id").Data == nil {
that.Display(2, "没有登录")
return
}
phone := that.Req.FormValue("phone")
companyName := that.Req.FormValue("company_name")
2022-05-06 03:35:40 +00:00
userName := that.Req.FormValue("user_name")
if len(phone) != 11 || len(companyName) < 4 || len(userName) < 2 {
2022-05-03 14:40:02 +00:00
that.Display(3, "请求参数异常")
return
}
2022-05-04 16:29:30 +00:00
that.Db.Delete("vip_order", Map{"AND": Map{"user_id": that.Session("user_id").Data, "status": 0}})
user := that.Db.Get("user", "*", Map{"id": that.Session("user_id").Data})
if user == nil {
that.Display(2, "找不到此用户")
return
}
wc := that.Db.Get("wechat", "openid", Map{"AND": Map{"appid": that.Config.GetString("wechatAppID"), "user_id": that.Session("user_id").Data}})
if wc == nil {
that.Display(2, "没有获取微信个人信息")
return
}
2022-05-06 03:35:40 +00:00
err := auth(that, phone, companyName, userName)
2022-05-03 14:40:02 +00:00
if err != nil {
2022-05-04 16:29:30 +00:00
that.Display(3, err.Error())
return
}
data := Map{
"sn": "SN" + time.Now().Format("20060102150405") + getSn(),
//"name":"1年VIP会员",
2022-05-05 08:07:01 +00:00
"amount": 36000, //720元
2022-05-04 16:29:30 +00:00
"user_id": user.GetCeilInt64("id"),
"company_id": user.GetCeilInt("company_id"),
"expiration_time": time.Now().Add(365 * 24 * time.Hour).Format("2006-01-02 15:04:05"),
"create_time[#]": "now()",
"modify_time[#]": "now()",
"del_flag": 0,
"status": 0,
}
tp := "购买"
if user.GetString("expiration_time") != "" {
data["old_expiration_time"] = user.GetString("expiration_time")
t, e := time.Parse("2006-01-02 15:04:05", user.GetString("expiration_time"))
fmt.Println(e, "时间创建失败")
if t.Unix() >= time.Now().Unix() {
tp = "续订"
data["expiration_time"] = t.Add(365 * 24 * time.Hour).Format("2006-01-02 15:04:05")
}
}
if user.GetCeilInt("provider_id") != 0 {
data["provider_id"] = user.GetCeilInt("provider_id")
data["amount"] = 36000
//tp=tp
}
2022-05-05 08:07:01 +00:00
//data["amount"] = 1
2022-05-04 16:29:30 +00:00
if user.GetCeilInt("salesman_id") != 0 {
data["salesman_id"] = user.GetCeilInt("salesman_id")
}
2022-05-11 06:55:28 +00:00
data["name"] = companyName + tp + "1年VIP会员"
2022-05-04 16:29:30 +00:00
jsParams, e := wechat.WxPay.GetJsOrder(data.GetCeilInt64("amount"), that.Config.GetString("wechatAppID"), wc.GetString("openid"), data.GetString("name"), data.GetString("sn"), that.Config.GetString("wechatAppNotifyUrl"))
if e != nil {
fmt.Println(e)
that.Display(4, e)
return
}
re := that.Db.Insert("vip_order", data)
fmt.Println(re)
that.Display(0, jsParams)
},
"callback": func(that *Context) {
data, e := wechat.WxPay.CallbackJsOrder(that.Req)
if e != nil {
fmt.Println(e)
//that.Display(4,e)
fmt.Println("返回数据错误", e)
return
}
sn := data.OutTradeNo
amount := int64(data.Amount.Total)
state := data.TradeState
//state:="SUCCESS"
//data := Map{"ces": "das"}
//sn := that.Req.FormValue("sn")
//amount := ObjToCeilInt64(that.Req.FormValue("amount"))
if state != "SUCCESS" {
fmt.Println("购买返回失败", data)
return
}
vipOrder := that.Db.Get("vip_order", "*", Map{"sn": sn})
if vipOrder == nil {
fmt.Println("找不到订单", vipOrder, data)
return
}
user := that.Db.Get("user", "*", Map{"id": vipOrder.GetCeilInt("user_id")})
if user == nil {
fmt.Println("找不到用户", vipOrder, data)
return
}
if vipOrder.GetCeilInt64("amount") != amount {
fmt.Println("金额不符", user, vipOrder, amount, data)
return
}
that.Db.Update("vip_order", Map{"status": 1}, Map{"id": vipOrder.GetCeilInt("id")})
idata := Map{"expiration_time": time.Now().Add(365 * 24 * time.Hour).Format("2006-01-02 15:04:05")}
if user.GetString("expiration_time") != "" {
t, e := time.Parse("2006-01-02 15:04:05", user.GetString("expiration_time"))
fmt.Println(e, "时间创建失败")
if t.Unix() >= time.Now().Unix() {
idata["expiration_time"] = t.Add(365 * 24 * time.Hour).Format("2006-01-02 15:04:05")
}
}
re := that.Db.Update("user", idata, Map{"id": user.GetCeilInt("id")})
if re == 0 {
fmt.Println("购买失败", user, vipOrder, re, data)
2022-05-03 14:40:02 +00:00
return
}
2022-05-04 16:29:30 +00:00
fmt.Println("成功购买", user, vipOrder, re, data)
return
2022-05-03 14:40:02 +00:00
},
2022-05-10 11:45:00 +00:00
"export": func(that *Context) {
if that.Session("user_id").Data == nil {
that.Display(2, "没有登录")
return
}
buy_date := ObjToStr(that.Req.FormValue("buy_date"))
if buy_date == "" {
that.Display(3, "请求参数异常")
return
}
data := that.Db.Query("SELECT vo.sn 订单号, vo.`name` 订单名, vo.user_id 购买用户ID,usr.`name` 购买用户名, usr.phone 购买用户电话, cp.id 企业ID, cp.`name` 企业名,\nvo.expiration_time 过期时间, vo.old_expiration_time 订购前到期时间, vo.amount `订单金额_单位(分)`,\nvo.salesman_id 业务员ID,sm.`name` 业务员名, sm.phone 业务员电话, pv.`name` 服务商名 \nFROM vip_order vo\nLEFT JOIN salesman sm ON sm.id = vo.salesman_id\nLEFT JOIN provider pv ON pv.id = vo.provider_id\nLEFT JOIN `user` usr ON usr.id = vo.user_id\nLEFT JOIN company cp ON cp.id = vo.company_id\nWHERE vo.`status` = 1 AND vo.create_time >= '" + buy_date + "' \nORDER BY vo.create_time")
if len(data) == 0 {
that.Display(0, "今日没有vip购买信息数据")
return
}
var titleList []string
if data != nil {
m := data[0]
for k, _ := range m {
titleList = append(titleList, k)
}
}
var dataList []Map
for _, v := range data {
dataList = append(dataList, v)
}
//打印最后一次sql语句
//that.Db.LastQuery
//请求的数据
//that.Db.LastData
//that.Db.LastErr
//appIns.Db.Select("company",common.Map{"[<>]user":"company.id=user.company_id"},"company.id as id",common.Map{"AND":common.Map{"id[!]":nil},"ORDER":"id DESC"})
//appIns.Db.Query("select * from user where id = ? and name = ?",common.Slice{1,"nn"})
//titleList:= []string{"aaaa", "vvvvv", "dddd", "eeeee", "gfgggg"}
DataToExcel(that.Resp, that.Req, titleList, dataList, buy_date+"vip购买信息分析数据")
},
2022-05-03 14:40:02 +00:00
}