iedc-go/example/app/vip_order.go

236 lines
8.4 KiB
Go

package app
import (
. "code.hoteas.com/golang/hotime"
. "code.hoteas.com/golang/hotime/common"
"code.hoteas.com/golang/hotime/dri/wechat"
"fmt"
"time"
)
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")
userName := that.Req.FormValue("user_name")
if len(phone) != 11 || len(companyName) < 4 || len(userName) < 2 {
that.Display(3, "请求参数异常")
return
}
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
}
err := auth(that, phone, companyName, userName)
if err != nil {
that.Display(3, err.Error())
return
}
data := Map{
"sn": "SN" + time.Now().Format("20060102150405") + getSn(),
//"name":"1年VIP会员",
"amount": 36000, //720元
"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
}
//data["amount"] = 1
if user.GetCeilInt("salesman_id") != 0 {
data["salesman_id"] = user.GetCeilInt("salesman_id")
}
data["name"] = companyName + tp + "1年VIP会员"
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)
return
}
fmt.Println("成功购买", user, vipOrder, re, data)
//购买成功后赠送10张券
//user_id := that.Session("user_id").Data
data2 := Map{
"user_id": that.Session("user_id").Data,
"coupon_id": 1,
"code_no": "SN" + time.Now().Format("20060102150405") + getSn(),
"effective_start_time[#]": "NOW()",
"effective_end_time": time.Now().AddDate(0, 6, 0).Format("2006-01-02 15:04:05"),
"source_type": 1,
"status": 0,
"admin_id": user.GetCeilInt("admin_id"),
"create_time[#]": "NOW()",
}
for n := 0; n < 5; n++ {
that.Db.Insert("coupon_user", data2)
}
data2["effective_end_time"] = time.Now().AddDate(1, 0, 0).Format("2006-01-02 15:04:05")
for n := 0; n < 5; n++ {
that.Db.Insert("coupon_user", data2)
}
return
},
"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")
//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\n" +
// "LEFT 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 >= ? \nORDER BY vo.create_time",buy_date)
data := that.Db.Select("vip_order", Map{"[>]salesman": "salesman.id=vip_order.salesman_id",
"[>]provider": "provider.id = vip_order.provider_id",
"[>]user": "user.id = vip_order.user_id",
"[>]company": "company.id = vip_order.company_id",
}, "vip_order.sn 订单号, vip_order.`name` 订单名, vip_order.user_id 购买用户ID,user.`name` 购买用户名, user.phone 购买用户电话, company.id 企业ID, company.`name` 企业名,\nvip_order.expiration_time 过期时间, vip_order.amount `订单金额_单位(分)`,\nvip_order.salesman_id 业务员ID,salesman.`name` 业务员名, salesman.phone 业务员电话, provider.`name` 服务商名",
Map{"AND": Map{"vip_order.status": 1, "vip_order.create_time[>=]": buy_date}, "ORDER": "vip_order.create_time DESC"})
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购买信息分析数据")
},
}