|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- 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.Identity == enum2.UserIdentityForCentralKitchenForTeacher {
- isTeacher = true
- }
-
- //2、计算数据(1:按学期购买 2:按月购买 3:按天购买 4:补餐)
- 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
- }
- }
- if req.Kind == 4 {
- 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.OpenAppletAppid, enum2.OpenAlipayAppid, enum2.OpenAlipayAppPrivateKey, enum2.OpenAlipayPublicKey, enum2.JsapiPayAppAutToken)
- err, resp := CurlAlipayTradeCreate(md.CurlAlipayTradeCreateReq{
- Config: struct {
- PayAliAppId string `json:"pay_ali_app_id" label:"支付宝开放平台-第三方应用-appid"`
- PayAliPrivateKey string `json:"pay_ali_private_key" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
- PayAliPublicKey string `json:"pay_ali_public_key" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"`
- }{
- PayAliAppId: sysCfg[enum2.OpenAlipayAppid],
- PayAliPrivateKey: sysCfg[enum2.OpenAlipayAppPrivateKey],
- PayAliPublicKey: sysCfg[enum2.OpenAlipayPublicKey],
- },
- OpAppId: sysCfg[enum2.OpenAppletAppid],
- 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,
- UserIdentityName: userIdentity.Name,
- 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
- }
-
- func NursingHomeBuyPackageReq(c *gin.Context, req md.NursingHomeBuyPackageReq) (outTradeNo, tradeNo, total string, err error) {
- user := GetUser(c)
- session := db.Db.NewSession()
- defer session.Close()
- session.Begin()
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- userIdentityDb := db.UserIdentityDb{}
- userIdentityDb.Set(0)
- userIdentity, err := userIdentityDb.GetUserIdentity(req.UserIdentityId)
- if err != nil {
- return
- }
- if userIdentity == nil {
- err = errors.New("未查询到对应身份记录")
- return
- }
-
- //1、计算数据(1:按年购买 2:按月购买 3:按天购买 4:补餐)
- var totalPrice float64
- var data []*model.NursingHomeUserWithDay
- if req.Kind == 1 {
- totalPrice, data, err = CalcByYearForNursingHome(user.Id, req)
- if err != nil {
- return
- }
- }
- if req.Kind == 2 {
- totalPrice, data, err = CalcByMonthForNursingHome(user.Id, req)
- if err != nil {
- return
- }
- }
- if req.Kind == 3 {
- totalPrice, data, err = CalcByDayForNursingHome(user.Id, req)
- if err != nil {
- return
- }
- }
- if req.Kind == 4 {
- totalPrice, data, err = CalcByDayForNursingHome(user.Id, 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.OpenAppletAppid, enum2.OpenAlipayAppid, enum2.OpenAlipayAppPrivateKey, enum2.OpenAlipayPublicKey, enum2.JsapiPayAppAutToken)
- err, resp := CurlAlipayTradeCreate(md.CurlAlipayTradeCreateReq{
- Config: struct {
- PayAliAppId string `json:"pay_ali_app_id" label:"支付宝开放平台-第三方应用-appid"`
- PayAliPrivateKey string `json:"pay_ali_private_key" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
- PayAliPublicKey string `json:"pay_ali_public_key" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"`
- }{
- PayAliAppId: sysCfg[enum2.OpenAlipayAppid],
- PayAliPrivateKey: sysCfg[enum2.OpenAlipayAppPrivateKey],
- PayAliPublicKey: sysCfg[enum2.OpenAlipayPublicKey],
- },
- OpAppId: sysCfg[enum2.OpenAppletAppid],
- BuyerId: user.UserId,
- TotalAmount: total,
- OutTradeNo: outTradeNo,
- Subject: "购买食堂套餐",
- AppAuthToken: sysCfg[enum2.JsapiPayAppAutToken],
- })
- if err != nil {
- return
- }
-
- //5、插入订单记录
- now := time.Now()
- NursingHomePackageOrd := db.NursingHomePackageOrd{}
- NursingHomePackageOrd.Set(outTradeNo)
- _, err = NursingHomePackageOrd.NursingHomePackageOrdInsertBySession(session, &model.NursingHomePackageOrd{
- EnterpriseId: req.EnterpriseId,
- Uid: user.Id,
- UserIdentityId: req.UserIdentityId,
- UserIdentityName: userIdentity.Name,
- TotalPrice: total,
- Kind: req.Kind,
- OutTradeNo: outTradeNo,
- TradeNo: resp.TradeNo,
- State: enum2.NursingHomePackageOrdStateForWait,
- OrdState: enum2.NursingHomePackageOrdOrdStateForWait,
- 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
- }
|