|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705 |
- package svc
-
- import (
- "applet/app/customer/enum"
- "applet/app/customer/md"
- "applet/app/db"
- "applet/app/db/model"
- enum2 "applet/app/enum"
- svc3 "applet/app/svc"
- "applet/app/utils"
- "applet/app/utils/logx"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/wechatpay-apiv3/wechatpay-go/core"
- "github.com/wechatpay-apiv3/wechatpay-go/services/partnerpayments/jsapi"
- jsapi2 "github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
- utils2 "github.com/wechatpay-apiv3/wechatpay-go/utils"
- "io/ioutil"
- "math"
- "net/url"
- "strconv"
- "time"
- )
-
- func BuyPackageForAli(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 {
- if req.IsBuyByMealLabel {
- totalPrice, data, err = CalcBySchoolTerm(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- } else {
- totalPrice, data, err = OldCalcBySchoolTerm(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- }
- }
- if req.Kind == 2 {
- if req.IsBuyByMealLabel {
- totalPrice, data, err = CalcByMonth(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- } else {
- totalPrice, data, err = OldCalcByMonth(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- }
- }
- if req.Kind == 3 {
- if req.IsBuyByMealLabel {
- totalPrice, data, err = CalcByDay(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- } else {
- totalPrice, data, err = OldCalcByDay(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- }
- }
- if req.Kind == 4 {
- if req.IsBuyByMealLabel {
- totalPrice, data, err = CalcSupplementaryByDay(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- } else {
- totalPrice, data, err = OldCalcSupplementaryByDay(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, enum2.JsapiPayNotifyUrl)
- 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],
- NotifyUrl: sysCfg[enum2.JsapiPayNotifyUrl],
- })
- 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)),
- PayWay: enum.PayWayForAli,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- PayAt: "2006-01-02 15:04:05",
- })
- if err != nil {
- _ = session.Rollback()
- return
- }
- session.Commit()
- tradeNo = resp.TradeNo
- return
- }
-
- func BuyPackageForWx(c *gin.Context, req md.BuyPackageReq) (outTradeNo, total string, resp *jsapi2.PrepayWithRequestPaymentResponse, 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 {
- if req.IsBuyByMealLabel {
- totalPrice, data, err = CalcBySchoolTerm(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- } else {
- totalPrice, data, err = OldCalcBySchoolTerm(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- }
- }
- if req.Kind == 2 {
- if req.IsBuyByMealLabel {
- totalPrice, data, err = CalcByMonth(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- } else {
- totalPrice, data, err = OldCalcByMonth(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- }
- }
- if req.Kind == 3 {
- if req.IsBuyByMealLabel {
- totalPrice, data, err = CalcByDay(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- } else {
- totalPrice, data, err = OldCalcByDay(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- }
- }
- if req.Kind == 4 {
- if req.IsBuyByMealLabel {
- totalPrice, data, err = CalcSupplementaryByDay(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- } else {
- totalPrice, data, err = OldCalcSupplementaryByDay(user.Id, isTeacher, req)
- if err != nil {
- return
- }
- }
- }
- total = utils.Float64ToStr(totalPrice)
-
- //3、生成订单号
- outTradeNo = utils.OrderUUID(user.Id)
-
- //4、请求 /v3/pay/partner/transactions/jsapi (生成预支付交易单)
- sysCfgDb := db.SysCfgDb{}
- sysCfgDb.Set()
- client, err := svc3.NewWxPayClient(c)
- if err != nil {
- return
- }
- wxSvc := jsapi.JsapiApiService{Client: client}
-
- companyWithWxpayInfoDb := db.CompanyWithWxpayInfoDb{}
- companyWithWxpayInfoDb.Set()
- wxMchId, err := companyWithWxpayInfoDb.GetCompanyWithWxpayInfoByEnterprise(req.EnterpriseId)
- if err != nil {
- return "", "", nil, err
- }
-
- sysCfg := sysCfgDb.SysCfgFindWithDb(enum2.WxAppletAppId, enum2.WxSpAppId, enum2.WxSpMchId, enum2.WxAppletAppId, enum2.WxJsapiPayNotifyUrl)
- fmt.Println(int64(math.Round(totalPrice * 100)))
-
- openId := c.GetHeader("openid") //TODO::实时获取用户的openId
- if openId == "" {
- openId = user.OpenId
- }
- result, _, err := wxSvc.Prepay(c,
- jsapi.PrepayRequest{
- SpAppid: core.String(sysCfg[enum2.WxSpAppId]),
- SpMchid: core.String(sysCfg[enum2.WxSpMchId]),
- SubAppid: core.String(sysCfg[enum2.WxAppletAppId]),
- SubMchid: core.String(wxMchId),
- Description: core.String("购买食堂套餐"),
- OutTradeNo: core.String(outTradeNo),
- TimeExpire: core.Time(time.Now().Add(20 * time.Minute)),
- //Attach: core.String("自定义数据说明"),
- NotifyUrl: core.String(sysCfg[enum2.WxJsapiPayNotifyUrl]),
- Amount: &jsapi.Amount{
- Currency: core.String("CNY"),
- Total: core.Int64(int64(math.Round(totalPrice * 100))),
- },
- Payer: &jsapi.Payer{
- //SpOpenid: core.String("oUpF8uMuAJO_M2pxb1Q9zNjWeS6o"),
- SubOpenid: core.String(openId),
- },
- },
- )
-
- fmt.Println(">>>>>>>>>>>", core.Int64(utils.StrToInt64(total)*100))
- if err != nil {
- _ = session.Rollback()
- 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: "",
- State: enum2.CentralKitchenForSchoolPackageOrdStateForWait,
- OrdState: enum2.CentralKitchenForSchoolPackageOrdOrdStateForWait,
- ReqContent: string(utils.Serialize(req)),
- WithDayData: string(utils.Serialize(data)),
- PayWay: enum.PayWayForWx,
- PrepayId: *result.PrepayId,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- PayAt: "2006-01-02 15:04:05",
- })
- if err != nil {
- _ = session.Rollback()
- return
- }
- session.Commit()
- //prepayId = *resp.PrepayId
- resp = new(jsapi2.PrepayWithRequestPaymentResponse)
- resp.PrepayId = result.PrepayId
- resp.SignType = core.String("RSA")
- appid := sysCfg[enum2.WxAppletAppId]
- resp.Appid = &appid
- resp.TimeStamp = core.String(strconv.FormatInt(time.Now().Unix(), 10))
- nonce, err := utils2.GenerateNonce()
- if err != nil {
- _ = session.Rollback()
- return
- }
- resp.NonceStr = core.String(nonce)
- resp.Package = core.String("prepay_id=" + *result.PrepayId)
- message := fmt.Sprintf("%s\n%s\n%s\n%s\n", *resp.Appid, *resp.TimeStamp, *resp.NonceStr, *resp.Package)
- signatureResult, err := wxSvc.Client.Sign(c, message)
- if err != nil {
- _ = session.Rollback()
- return
- }
- resp.PaySign = core.String(signatureResult.Signature)
- 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
- }
-
- func DealCentralKitchenForSchoolOrderCallBack(outTradeNo, tradeStatus, successTime string) (state int, err error) {
- centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{}
- centralKitchenForSchoolPackageOrd.Set(outTradeNo)
- ord, err := centralKitchenForSchoolPackageOrd.GetCentralKitchenForSchoolPackageOrd()
- if err != nil {
- return
- }
- if ord == nil {
- err = errors.New("未查询到对应订单记录")
- return
- }
-
- if ord.State == enum2.CentralKitchenForSchoolPackageOrdStateForWait {
- if tradeStatus == "TRADE_CLOSED" {
- ord.State = enum2.CentralKitchenForSchoolPackageOrdStateForFail
- }
-
- if tradeStatus == "TRADE_SUCCESS" {
- ord.State = enum2.CentralKitchenForSchoolPackageOrdStateForSuccess
- ord.OrdState = enum2.CentralKitchenForSchoolPackageOrdOrdStateForSuccess
- ord.PayAt = successTime
-
- //TODO::将预留数据插入到 `central_kitchen_for_school_user_with_day`
- var data []*model.CentralKitchenForSchoolUserWithDay
- err = json.Unmarshal([]byte(ord.WithDayData), &data)
- if err != nil {
- return
- }
-
- var hasMonths = map[string]map[string]interface{}{}
- var hasKinds = map[string]map[string]string{}
- var hasTotalPrice = map[string]map[string]float64{}
- for _, v := range data {
- date, _ := time.ParseInLocation("2006-01-02", v.Date, time.Local)
- if hasMonths[utils.TimeParseStd(v.Date).Month().String()] == nil {
- hasMonths[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]interface{})
- hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
- hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
- } else {
- startTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"]), time.Local)
- if startTime.After(date) {
- hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
- }
-
- endTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"]), time.Local)
- if endTime.Before(date) {
- hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
- }
- }
- if hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] == nil {
- hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]float64)
- }
- if hasKinds[utils.TimeParseStd(v.Date).Month().String()] == nil {
- hasKinds[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]string)
- }
-
- hasMonths[utils.TimeParseStd(v.Date).Month().String()]["month"] = int(utils.TimeParseStd(v.Date).Month())
- hasKinds[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] = utils.IntToStr(v.Kind)
- hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] += utils.StrToFloat64(v.Amount)
- v.OrdNo = outTradeNo
- }
-
- centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
- centralKitchenForSchoolUserWithDayDb.Set(0)
- _, err = centralKitchenForSchoolUserWithDayDb.BatchAddCentralKitchenForSchoolUserWithDays(data)
- if err != nil {
- return
- }
-
- //TODO::插入数据至 `central_kitchen_for_school_package_ord_for_reserve`
- centralKitchenForSchoolPackageOrdForReserve := db.CentralKitchenForSchoolPackageOrdForReserve{}
- centralKitchenForSchoolPackageOrdForReserve.Set(outTradeNo)
- for k, v := range hasMonths {
- for _, v1 := range hasKinds[k] {
- _, err = centralKitchenForSchoolPackageOrdForReserve.CentralKitchenForSchoolPackageOrdForReserveInsert(&model.CentralKitchenForSchoolPackageOrdForReserve{
- EnterpriseId: ord.EnterpriseId,
- Uid: ord.Uid,
- UserIdentityName: ord.UserIdentityName,
- UserIdentityId: ord.UserIdentityId,
- Kind: utils.StrToInt(v1),
- OutTradeNo: ord.OutTradeNo,
- ReserveMonth: utils.AnyToString(v["month"]),
- MealTimeStart: utils.AnyToString(v["meal_time_start"]),
- MealTimeEnd: utils.AnyToString(v["meal_time_end"]),
- TotalPrice: utils.Float64ToStr(hasTotalPrice[k][v1]),
- CreateAt: time.Now().Format("2006-01-02 15:04:05"),
- UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
- })
- if err != nil {
- return
- }
- }
- }
- }
- _, err = centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdUpdate(ord, "state", "ord_state", "pay_at")
- if err != nil {
- return
- }
- }
- state = ord.State
- return
- }
-
- func DealCentralKitchenForSchoolOrderCallBackForWx(outTradeNo, transactionId, tradeStatus, successTime string) (state int, err error) {
- centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{}
- centralKitchenForSchoolPackageOrd.Set(outTradeNo)
- ord, err := centralKitchenForSchoolPackageOrd.GetCentralKitchenForSchoolPackageOrd()
- if err != nil {
- return
- }
- if ord == nil {
- err = errors.New("未查询到对应订单记录")
- return
- }
-
- if ord.State == enum2.CentralKitchenForSchoolPackageOrdStateForWait {
- if tradeStatus == "CLOSED" || tradeStatus == "PAYERROR" {
- ord.State = enum2.CentralKitchenForSchoolPackageOrdStateForFail
- }
-
- if tradeStatus == "SUCCESS" {
- ord.State = enum2.CentralKitchenForSchoolPackageOrdStateForSuccess
- ord.OrdState = enum2.CentralKitchenForSchoolPackageOrdOrdStateForSuccess
- ord.TradeNo = transactionId
- ord.PayAt = successTime
-
- //TODO::将预留数据插入到 `central_kitchen_for_school_user_with_day`
- var data []*model.CentralKitchenForSchoolUserWithDay
- err = json.Unmarshal([]byte(ord.WithDayData), &data)
- if err != nil {
- return
- }
-
- var hasMonths = map[string]map[string]interface{}{}
- var hasKinds = map[string]map[string]string{}
- var hasTotalPrice = map[string]map[string]float64{}
- for _, v := range data {
- date, _ := time.ParseInLocation("2006-01-02", v.Date, time.Local)
- if hasMonths[utils.TimeParseStd(v.Date).Month().String()] == nil {
- hasMonths[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]interface{})
- hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
- hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
- } else {
- startTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"]), time.Local)
- if startTime.After(date) {
- hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
- }
-
- endTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"]), time.Local)
- if endTime.Before(date) {
- hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
- }
- }
- if hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] == nil {
- hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]float64)
- }
- if hasKinds[utils.TimeParseStd(v.Date).Month().String()] == nil {
- hasKinds[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]string)
- }
-
- hasMonths[utils.TimeParseStd(v.Date).Month().String()]["month"] = int(utils.TimeParseStd(v.Date).Month())
- hasKinds[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] = utils.IntToStr(v.Kind)
- hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] += utils.StrToFloat64(v.Amount)
- v.OrdNo = outTradeNo
- }
-
- centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
- centralKitchenForSchoolUserWithDayDb.Set(0)
- _, err = centralKitchenForSchoolUserWithDayDb.BatchAddCentralKitchenForSchoolUserWithDays(data)
- if err != nil {
- return
- }
-
- //TODO::插入数据至 `central_kitchen_for_school_package_ord_for_reserve`
- centralKitchenForSchoolPackageOrdForReserve := db.CentralKitchenForSchoolPackageOrdForReserve{}
- centralKitchenForSchoolPackageOrdForReserve.Set(outTradeNo)
- for k, v := range hasMonths {
- for _, v1 := range hasKinds[k] {
- _, err = centralKitchenForSchoolPackageOrdForReserve.CentralKitchenForSchoolPackageOrdForReserveInsert(&model.CentralKitchenForSchoolPackageOrdForReserve{
- EnterpriseId: ord.EnterpriseId,
- Uid: ord.Uid,
- UserIdentityName: ord.UserIdentityName,
- UserIdentityId: ord.UserIdentityId,
- Kind: utils.StrToInt(v1),
- OutTradeNo: ord.OutTradeNo,
- ReserveMonth: utils.AnyToString(v["month"]),
- MealTimeStart: utils.AnyToString(v["meal_time_start"]),
- MealTimeEnd: utils.AnyToString(v["meal_time_end"]),
- TotalPrice: utils.Float64ToStr(hasTotalPrice[k][v1]),
- CreateAt: time.Now().Format("2006-01-02 15:04:05"),
- UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
- })
- if err != nil {
- return
- }
- }
- }
- }
- _, err = centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdUpdate(ord, "state", "ord_state", "trade_no", "pay_at")
- if err != nil {
- return
- }
- }
- state = ord.State
- return
- }
-
- func CheckAllCallbackParams(c *gin.Context) interface{} {
- body, _ := ioutil.ReadAll(c.Request.Body)
- utils.FilePutContents("CheckAllCallbackParams", utils.SerializeStr(string(body)))
- dataAlipay, _ := GetAlipayCallbackParams(body)
- if dataAlipay != nil {
- return dataAlipay
- }
- return nil
- }
-
- // GetAlipayCallbackParams 支付宝参数解析
- func GetAlipayCallbackParams(body []byte) (*md.AliPayCallback, error) {
- decodeArgs, err := url.QueryUnescape(string(body))
- if err != nil {
- _ = logx.Warn(err)
- return nil, logx.Warn("回调参数解码错误")
- }
- data, err := url.ParseQuery(decodeArgs)
- if err != nil {
- return nil, err
- }
- dataMap := make(map[string]interface{})
- for k := range data {
- dataMap[k] = data.Get(k)
- }
- callbackStr := utils.Serialize(dataMap)
- fmt.Println("支付宝回调数据", string(callbackStr))
- var args md.AliPayCallback
- if err := json.Unmarshal(callbackStr, &args); err != nil {
- return nil, logx.Warn(err)
- }
- return &args, nil
- }
|