|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- package svc
-
- import (
- "applet/app/customer/md"
- "applet/app/db"
- "applet/app/db/model"
- enum2 "applet/app/enum"
- "applet/app/utils"
- "applet/app/utils/logx"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/gin-gonic/gin"
- "io/ioutil"
- "net/url"
- "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 = CalcSupplementaryByDay(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)),
- 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
- }
-
- func DealCentralKitchenForSchoolOrderCallBack(outTradeNo, tradeStatus 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
-
- //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")
- 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
- }
|