|
- package hdl
-
- import (
- "applet/app/customer/lib/validate"
- "applet/app/customer/md"
- "applet/app/customer/svc"
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/e"
- "applet/app/enum"
- "applet/app/utils"
- "encoding/json"
- "github.com/gin-gonic/gin"
- "time"
- )
-
- func BuyPackage(c *gin.Context) {
- var req md.BuyPackageReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- outTradeNo, tradeNo, total, err := svc.BuyPackage(c, req)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- e.OutSuc(c, map[string]interface{}{
- "out_trade_no": outTradeNo,
- "trade_no": tradeNo,
- "total": total,
- }, nil)
- return
- }
-
- func OrdState(c *gin.Context) {
- outTradeNo := c.DefaultQuery("out_trade_no", "")
- centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{}
- centralKitchenForSchoolPackageOrd.Set(outTradeNo)
- ord, err := centralKitchenForSchoolPackageOrd.GetCentralKitchenForSchoolPackageOrd()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if ord == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到对应订单记录")
- return
- }
- if ord.State == enum.CentralKitchenForSchoolPackageOrdStateForWait {
- //处于待支付状态,请求支付宝同步订单状态
- sysCfgDb := db.SysCfgDb{}
- sysCfgDb.Set()
- sysCfg := sysCfgDb.SysCfgFindWithDb(enum.JsapiPayAppAutToken, enum.OpenAlipayAppid, enum.OpenAlipayAppPrivateKey, enum.OpenAlipayPublicKey)
- err2, result := svc.CurlAlipayTradeQuery(md.CurlAlipayTradeQueryReq{
- 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[enum.OpenAlipayAppid],
- PayAliPrivateKey: sysCfg[enum.OpenAlipayAppPrivateKey],
- PayAliPublicKey: sysCfg[enum.OpenAlipayPublicKey],
- },
- OutTradeNo: outTradeNo,
- AppAuthToken: sysCfg[enum.JsapiPayAppAutToken],
- })
- if err2 != nil {
- e.OutErr(c, e.ERR, err2.Error())
- return
- }
- if result.TradeStatus == "TRADE_CLOSED" {
- ord.State = enum.CentralKitchenForSchoolPackageOrdStateForFail
- }
-
- if result.TradeStatus == "TRADE_SUCCESS" {
- ord.State = enum.CentralKitchenForSchoolPackageOrdStateForSuccess
- ord.OrdState = enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess
-
- //TODO::将预留数据插入到 `central_kitchen_for_school_user_with_day`
- var data []*model.CentralKitchenForSchoolUserWithDay
- err4 := json.Unmarshal([]byte(ord.WithDayData), &data)
- if err4 != nil {
- e.OutErr(c, e.ERR, err4.Error())
- 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)
- _, err5 := centralKitchenForSchoolUserWithDayDb.BatchAddCentralKitchenForSchoolUserWithDays(data)
- if err != nil {
- e.OutErr(c, e.ERR, err5.Error())
- 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] {
- _, err3 := 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 err3 != nil {
- e.OutErr(c, e.ERR, err3.Error())
- return
- }
- }
- }
- }
- _, err3 := centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdUpdate(ord, "state", "ord_state")
- if err3 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err3.Error())
- return
- }
- }
-
- e.OutSuc(c, map[string]interface{}{
- "sate": ord.State,
- "sate_zh": enum.CentralKitchenForSchoolPackageOrdState.String(enum.CentralKitchenForSchoolPackageOrdState(ord.State)),
- }, nil)
- return
- }
-
- func NursingHomeBuyPackage(c *gin.Context) {
- var req md.NursingHomeBuyPackageReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- outTradeNo, tradeNo, total, err := svc.NursingHomeBuyPackageReq(c, req)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- e.OutSuc(c, map[string]interface{}{
- "out_trade_no": outTradeNo,
- "trade_no": tradeNo,
- "total": total,
- }, nil)
- return
- }
-
- func NursingHomeOrdState(c *gin.Context) {
- outTradeNo := c.DefaultQuery("out_trade_no", "")
- centralKitchenForSchoolPackageOrd := db.NursingHomePackageOrd{}
- centralKitchenForSchoolPackageOrd.Set(outTradeNo)
- ord, err := centralKitchenForSchoolPackageOrd.GetNursingHomePackageOrd()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if ord == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到对应订单记录")
- return
- }
- if ord.State == enum.NursingHomePackageOrdStateForWait {
- //处于待支付状态,请求支付宝同步订单状态
- sysCfgDb := db.SysCfgDb{}
- sysCfgDb.Set()
- sysCfg := sysCfgDb.SysCfgFindWithDb(enum.JsapiPayAppAutToken, enum.OpenAlipayAppid, enum.OpenAlipayAppPrivateKey, enum.OpenAlipayPublicKey)
- err2, result := svc.CurlAlipayTradeQuery(md.CurlAlipayTradeQueryReq{
- 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[enum.OpenAlipayAppid],
- PayAliPrivateKey: sysCfg[enum.OpenAlipayAppPrivateKey],
- PayAliPublicKey: sysCfg[enum.OpenAlipayPublicKey],
- },
- OutTradeNo: outTradeNo,
- AppAuthToken: sysCfg[enum.JsapiPayAppAutToken],
- })
- if err2 != nil {
- e.OutErr(c, e.ERR, err2.Error())
- return
- }
- if result.TradeStatus == "TRADE_CLOSED" {
- ord.State = enum.NursingHomePackageOrdStateForFail
- }
-
- if result.TradeStatus == "TRADE_SUCCESS" {
- ord.State = enum.NursingHomePackageOrdStateForSuccess
- ord.OrdState = enum.NursingHomePackageOrdOrdStateForSuccess
-
- //TODO::将预留数据插入到 `central_kitchen_for_school_user_with_day`
- var data []*model.NursingHomeUserWithDay
- err4 := json.Unmarshal([]byte(ord.WithDayData), &data)
- if err4 != nil {
- e.OutErr(c, e.ERR, err4.Error())
- 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.NursingHomeUserWithDayDb{}
- centralKitchenForSchoolUserWithDayDb.Set(0)
- _, err5 := centralKitchenForSchoolUserWithDayDb.BatchAddNursingHomeUserWithDays(data)
- if err != nil {
- e.OutErr(c, e.ERR, err5.Error())
- return
- }
-
- //TODO::插入数据至 `nursing_home_package_ord_for_reserve`
- centralKitchenForSchoolPackageOrdForReserve := db.NursingHomePackageOrdForReserve{}
- centralKitchenForSchoolPackageOrdForReserve.Set(outTradeNo)
- var isHasKind []string
- for k, v := range hasMonths {
- for _, v1 := range hasKinds[k] {
- if !utils.InArr(v1, isHasKind) {
- _, err3 := centralKitchenForSchoolPackageOrdForReserve.NursingHomePackageOrdForReserveInsert(&model.NursingHomePackageOrdForReserve{
- 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 err3 != nil {
- e.OutErr(c, e.ERR, err3.Error())
- return
- }
- isHasKind = append(isHasKind, v1)
- }
- }
- }
- }
- _, err3 := centralKitchenForSchoolPackageOrd.NursingHomePackageOrdUpdate(ord, "state", "ord_state")
- if err3 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err3.Error())
- return
- }
- }
-
- e.OutSuc(c, map[string]interface{}{
- "sate": ord.State,
- "sate_zh": enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdState(ord.State)),
- }, nil)
- return
- }
|