package hdl import ( "applet/app/admin/lib/validate" "applet/app/admin/md" svc "applet/app/admin/svc/order" md2 "applet/app/customer/md" svc2 "applet/app/customer/svc" "applet/app/db" "applet/app/db/model" "applet/app/e" "applet/app/enum" svc3 "applet/app/svc" "applet/app/utils" "fmt" "github.com/gin-gonic/gin" "github.com/wechatpay-apiv3/wechatpay-go/core" "github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic" "math" "time" ) // Demo 测试 func Demo(c *gin.Context) { eg := db.Db var mm []model.CentralKitchenForSchoolWithSpec if err1 := eg.Asc("id").Find(&mm); err1 != nil { e.OutErr(c, e.ERR, err1.Error()) return } now := time.Now() for _, v := range mm { m := new(model.MealLabel) has, err := db.Db.Where("enterprise_id =?", v.EnterpriseId).Get(m) if err != nil { fmt.Println(err) return } if !has { //查找所有年级 gradeDb := db.GradeDb{} gradeDb.Set(v.EnterpriseId) gradeList, err := gradeDb.FindGrade() if err != nil { fmt.Println(err) return } for _, grade := range *gradeList { period := new(model.Period) has1, err := db.Db.Where("id =?", grade.PeriodId).Get(period) if err != nil { fmt.Println(err) return } if !has1 { continue } if v.IsOpenBreakfast == 1 { m = &model.MealLabel{ Name: "早餐", EnterpriseId: v.EnterpriseId, GradeId: grade.Id, Kind: 1, Price: period.BreakfastUnitPrice, CreateAt: now.Format("2006-01-02 15:04:05"), UpdateAt: now.Format("2006-01-02 15:04:05"), } _, err2 := db.Db.Insert(m) if err2 != nil { fmt.Println(err2) return } } if v.IsOpenLunch == 1 { m = &model.MealLabel{ Name: "午餐", EnterpriseId: v.EnterpriseId, GradeId: grade.Id, Kind: 2, Price: period.LunchUnitPrice, CreateAt: now.Format("2006-01-02 15:04:05"), UpdateAt: now.Format("2006-01-02 15:04:05"), } _, err2 := db.Db.Insert(m) if err2 != nil { fmt.Println(err2) return } } if v.IsOpenDinner == 1 { m = &model.MealLabel{ Name: "晚餐", EnterpriseId: v.EnterpriseId, GradeId: grade.Id, Kind: 3, Price: period.DinnerUnitPrice, CreateAt: now.Format("2006-01-02 15:04:05"), UpdateAt: now.Format("2006-01-02 15:04:05"), } _, err2 := db.Db.Insert(m) if err2 != nil { fmt.Println(err2) return } } } } } return var m []model.CentralKitchenForSchoolUserWithDay if err1 := eg.Where("state = 3").Asc("id").Find(&m); err1 != nil { e.OutErr(c, e.ERR, err1.Error()) return } var records []int for _, v := range m { mm := new(model.CentralKitchenForSchoolUserRefundDay) has, err := db.Db.Where("records_id =? and state = 1", v.Id).Get(mm) if err != nil { fmt.Println(err) return } if has == false { centralKitchenForSchoolUserRefundDayDb := db.CentralKitchenForSchoolUserRefundDayDb{} centralKitchenForSchoolUserRefundDayDb.Set(0) _, err = centralKitchenForSchoolUserRefundDayDb.CentralKitchenForSchoolUserRefundDayInsert(&model.CentralKitchenForSchoolUserRefundDay{ OutTradeNo: v.OrdNo, OutRequestNo: "", Uid: v.Uid, IdentityId: v.IdentityId, RecordsId: v.Id, State: enum.CentralKitchenForSchoolUserRefundDayStateForAuditing, Amount: v.Amount, Memo: "fill", CreateAt: now.Format("2006-01-02 15:04:05"), UpdateAt: now.Format("2006-01-02 15:04:05"), }) records = append(records, v.Id) } } e.OutSuc(c, records, nil) return var req md.CentralKitchenForSchoolOrderRefundAuditReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } err1 := svc.CentralKitchenForSchoolOrderRefundAudit(c, req) if err1 != nil { fmt.Println(err1.Error()) e.OutErr(c, e.ERR, err1.Error()) return } e.OutSuc(c, "success", nil) return sysCfgDb := db.SysCfgDb{} sysCfgDb.Set() sysCfg := sysCfgDb.SysCfgFindWithDb(enum.OpenAlipayAppid, enum.OpenAlipayAppPrivateKey, enum.OpenAlipayPublicKey) //调用支付宝进行退款 err6, _ := svc2.OldCurlAlipayTradeRefund(md2.OldCurlAlipayTradeRefundReq{ 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: "800169659125681442", RefundAmount: "180.00", RefundReason: "央厨订餐退款", OutRequestNo: "r_800169659125681442_1705546121", AppAuthToken: "202308BB8ef30019cd8d4a40a3853192c397dX35", }) if err6 != nil { fmt.Println(err6.Error()) } return qrcodeWithBatchRecordsDb := db.AdminDb{} qrcodeWithBatchRecordsDb.Set() data, _, err := qrcodeWithBatchRecordsDb.GetAdminRolePermission(1001) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } e.OutSuc(c, data, nil) go GoOnExecuting() return } // Demo 测试 func TempDemo(c *gin.Context) { eg := db.Db var list []*model.CentralKitchenForSchoolUserWithDay err := db.Db.Where("state = 3").Asc("id").Find(&list) if err != nil { return } for _, m := range list { mm := new(model.CentralKitchenForSchoolUserRefundDay) has, err := db.Db.Where("records_id =? and state = 4", m.Id).Get(mm) if err != nil { fmt.Println(err) return } if has { m.State = 4 updateAffetcd, err := db.Db.Where("id =?", m.Id).Cols("state").Update(m) if err != nil { fmt.Println(err) return } fmt.Println(">>>>>>>>>>>", updateAffetcd) } } return var m []model.CentralKitchenForSchoolUserWithDay if err1 := eg.Where("state = 3").Asc("id").Find(&m); err1 != nil { e.OutErr(c, e.ERR, err1.Error()) return } now := time.Now() var records []int for _, v := range m { mm := new(model.CentralKitchenForSchoolUserRefundDay) has, err := db.Db.Where("records_id =? and state = 1", v.Id).Get(mm) if err != nil { fmt.Println(err) return } if has == false { centralKitchenForSchoolUserRefundDayDb := db.CentralKitchenForSchoolUserRefundDayDb{} centralKitchenForSchoolUserRefundDayDb.Set(0) _, err = centralKitchenForSchoolUserRefundDayDb.CentralKitchenForSchoolUserRefundDayInsert(&model.CentralKitchenForSchoolUserRefundDay{ OutTradeNo: v.OrdNo, OutRequestNo: "", Uid: v.Uid, IdentityId: v.IdentityId, RecordsId: v.Id, State: enum.CentralKitchenForSchoolUserRefundDayStateForAuditing, Amount: v.Amount, Memo: "fill", CreateAt: now.Format("2006-01-02 15:04:05"), UpdateAt: now.Format("2006-01-02 15:04:05"), }) records = append(records, v.Id) } } e.OutSuc(c, records, nil) return var req md.CentralKitchenForSchoolOrderRefundAuditReq err = c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } err1 := svc.CentralKitchenForSchoolOrderRefundAudit(c, req) if err1 != nil { fmt.Println(err1.Error()) e.OutErr(c, e.ERR, err1.Error()) return } e.OutSuc(c, "success", nil) return sysCfgDb := db.SysCfgDb{} sysCfgDb.Set() sysCfg := sysCfgDb.SysCfgFindWithDb(enum.OpenAlipayAppid, enum.OpenAlipayAppPrivateKey, enum.OpenAlipayPublicKey) //调用支付宝进行退款 err6, _ := svc2.OldCurlAlipayTradeRefund(md2.OldCurlAlipayTradeRefundReq{ 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: "800169659125681442", RefundAmount: "180.00", RefundReason: "央厨订餐退款", OutRequestNo: "r_800169659125681442_1705546121", AppAuthToken: "202308BB8ef30019cd8d4a40a3853192c397dX35", }) if err6 != nil { fmt.Println(err6.Error()) } return qrcodeWithBatchRecordsDb := db.AdminDb{} qrcodeWithBatchRecordsDb.Set() data, _, err := qrcodeWithBatchRecordsDb.GetAdminRolePermission(1001) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } e.OutSuc(c, data, nil) go GoOnExecuting() return } func DemoV1(c *gin.Context) { var outRequestNo = c.Query("out_request_no") var outTradeNo = c.Query("out_no") centralKitchenForSchoolUserRefundDayDb := db.CentralKitchenForSchoolUserRefundDayDb{} centralKitchenForSchoolUserRefundDayDb.Set(0) var m []model.CentralKitchenForSchoolUserRefundDay if err := centralKitchenForSchoolUserRefundDayDb.Db.Where("out_request_no =?", outRequestNo).Desc("id").Find(&m); err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{} centralKitchenForSchoolPackageOrd.Set(outTradeNo) ord, _ := centralKitchenForSchoolPackageOrd.GetCentralKitchenForSchoolPackageOrd() var dealReturnOrdMapForWx = map[string]struct { OutRequestNo string `json:"out_request_no"` Amount float64 `json:"amount"` Total string `json:"total"` WxMchId string `json:"wx_mch_id"` }{} companyWithWxpayInfoDb := db.CompanyWithWxpayInfoDb{} companyWithWxpayInfoDb.Set() wxMchId, _ := companyWithWxpayInfoDb.GetCompanyWithWxpayInfoByEnterprise(ord.EnterpriseId) for _, v := range m { dealReturnOrdMapForWx[ord.OutTradeNo] = struct { OutRequestNo string `json:"out_request_no"` Amount float64 `json:"amount"` Total string `json:"total"` WxMchId string `json:"wx_mch_id"` }(struct { OutRequestNo string Amount float64 Total string WxMchId string }{OutRequestNo: outRequestNo, Amount: dealReturnOrdMapForWx[v.OutTradeNo].Amount + utils.StrToFloat64(v.Amount), Total: ord.TotalPrice, WxMchId: wxMchId}) } client, err1 := svc3.NewWxPayClient(c) //初始化微信连接 if err1 != nil { e.OutErr(c, e.ERR, err1.Error()) return } sysCfgDb := db.SysCfgDb{} sysCfgDb.Set() sysCfg := sysCfgDb.SysCfgFindWithDb(enum.JsapiPayAppAutToken, enum.OpenAlipayAppid, enum.OpenAlipayAppPrivateKey, enum.OpenAlipayPublicKey, enum.WxJsapiRefundPayNotifyUrl) wxSvc := refunddomestic.RefundsApiService{Client: client} for k, v := range dealReturnOrdMapForWx { resp, _, err6 := wxSvc.Create(c, refunddomestic.CreateRequest{ SubMchid: core.String(v.WxMchId), OutTradeNo: core.String(k), OutRefundNo: core.String(v.OutRequestNo), Reason: core.String("央厨订餐退款"), NotifyUrl: core.String(sysCfg[enum.WxJsapiRefundPayNotifyUrl]), Amount: &refunddomestic.AmountReq{ Currency: core.String("CNY"), Refund: core.Int64(int64(math.Round(v.Amount * 100))), Total: core.Int64(int64(math.Round(utils.StrToFloat64(v.Total) * 100))), }, }, ) fmt.Println(">>>>wxRefund<<<<", resp) if err6 != nil { fmt.Println(err6) e.OutErr(c, e.ERR, err6.Error()) return } } e.OutSuc(c, dealReturnOrdMapForWx, nil) return } func GoOnExecuting() { for i := 0; i < 20; i++ { time.Sleep(1 * time.Second) fmt.Println(i) } }