|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package svc
-
- import (
- "applet/app/customer/md"
- "applet/app/db"
- "applet/app/db/model"
- enum2 "applet/app/enum"
- "applet/app/utils"
- "errors"
- "github.com/gin-gonic/gin"
- "time"
- )
-
- func BuyPackage(c *gin.Context, req md.BuyPackageReq) (outTradeNo, tradeNo, total string, err error) {
- user := GetUser(c)
- session := db.Db.NewSession()
- defer session.Close()
- session.Begin()
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- //1、判断是否为教师
- isTeacher := false
- userIdentityDb := db.UserIdentityDb{}
- userIdentityDb.Set(0)
- userIdentity, err := userIdentityDb.GetUserIdentity(req.UserIdentityId)
- if err != nil {
- return
- }
- if userIdentity == nil {
- err = errors.New("未查询到对应身份记录")
- return
- }
- if userIdentity.Kind == enum2.UserIdentityForCentralKitchenForTeacher {
- isTeacher = true
- }
-
- //2、计算数据(1:按学期购买 2:按月购买 3:按天购买)
- var totalPrice float64
- var data []*model.CentralKitchenForSchoolUserWithDay
- if req.Kind == 1 {
- totalPrice, data, err = CalcBySchoolTerm(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- }
- if req.Kind == 2 {
- totalPrice, data, err = CalcByMonth(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- }
- if req.Kind == 3 {
- totalPrice, data, err = CalcByDay(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- }
- total = utils.Float64ToStr(totalPrice)
-
- //3、生成订单号
- outTradeNo = utils.OrderUUID(user.Id)
-
- //4、请求 alipay.trade.create(统一收单交易创建接口)
- sysCfgDb := db.SysCfgDb{}
- sysCfgDb.Set()
- sysCfg := sysCfgDb.SysCfgFindWithDb(enum2.OpenAlipayAppid, enum2.OpenAlipayAppPrivateKey, enum2.OpenAlipayPublicKey, enum2.JsapiPayAppAutToken)
- err, resp := CurlAlipayTradeCreate(md.CurlAlipayTradeCreateReq{
- Config: struct {
- PayAliAppId string `json:"pay_ali_app_id" binding:"required" label:"支付宝开放平台-第三方应用-appid"`
- PayAliPrivateKey string `json:"pay_ali_private_key" binding:"required" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
- PayAliPublicKey string `json:"pay_ali_public_key" binding:"required" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"`
- }{
- PayAliAppId: sysCfg[enum2.OpenAlipayAppid],
- PayAliPrivateKey: sysCfg[enum2.OpenAlipayAppPrivateKey],
- PayAliPublicKey: sysCfg[enum2.OpenAlipayPublicKey],
- },
- BuyerId: user.UserId,
- TotalAmount: total,
- OutTradeNo: outTradeNo,
- Subject: "购买食堂套餐",
- AppAuthToken: sysCfg[enum2.JsapiPayAppAutToken],
- })
- if err != nil {
- return
- }
-
- //5、插入订单记录
- now := time.Now()
- centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{}
- centralKitchenForSchoolPackageOrd.Set(outTradeNo)
- _, err = centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdInsertBySession(session, &model.CentralKitchenForSchoolPackageOrd{
- EnterpriseId: req.EnterpriseId,
- Uid: user.Id,
- UserIdentityId: req.UserIdentityId,
- TotalPrice: total,
- Kind: req.Kind,
- OutTradeNo: outTradeNo,
- TradeNo: resp.TradeNo,
- State: enum2.CentralKitchenForSchoolPackageOrdStateForWait,
- OrdState: enum2.CentralKitchenForSchoolPackageOrdOrdStateForWait,
- ReqContent: string(utils.Serialize(req)),
- WithDayData: string(utils.Serialize(data)),
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- if err != nil {
- _ = session.Rollback()
- return
- }
- session.Commit()
- tradeNo = resp.TradeNo
- return
- }
|