This commit is contained in:
苏尹岚
2020-10-14 18:29:36 +08:00
parent 0ff054afed
commit 54438022ff
15 changed files with 223 additions and 113 deletions

View File

@@ -4,6 +4,8 @@ import (
"fmt"
"time"
"git.rosy.net.cn/jx-callback/business/jxstore/financial"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils"
@@ -30,9 +32,20 @@ func PublishJob(ctx *jxcontext.Context, job *model.Job) (err error) {
// switch job.JobCategoryID {
// case 1:
// }
// if ctx.GetUserID() != job.UserID {
// return fmt.Errorf("用户信息已过期,请重新登录!")
// }
if ctx.GetUserID() != job.UserID {
return fmt.Errorf("用户信息已过期,请重新登录!")
}
//发布任务要扣除任务总额的保证金,不够扣就要进行充值
userBill, err := dao.GetUserBill(db, job.UserID, "")
if userBill == nil {
return fmt.Errorf("未查询到该用户的账单!")
}
job.TotalPrice = job.Count * job.AvgPrice
if userBill.DepositBalance < job.TotalPrice {
job.Status = model.JobStatusFailed
} else {
job.Status = model.JobStatusDoing
}
if job.Count <= 0 {
return fmt.Errorf("任务数量不能为0")
}
@@ -57,8 +70,6 @@ func PublishJob(ctx *jxcontext.Context, job *model.Job) (err error) {
job.Lng = jxutils.StandardCoordinate2Int(lng)
job.Lat = jxutils.StandardCoordinate2Int(lat)
}
job.TotalPrice = job.Count * job.AvgPrice
job.Status = model.JobStatusDoing
dao.WrapAddIDCULEntity(job, ctx.GetUserName())
dao.Begin(db)
defer func() {
@@ -76,6 +87,18 @@ func PublishJob(ctx *jxcontext.Context, job *model.Job) (err error) {
if err != nil {
dao.Rollback(db)
}
//发布任务要扣除任务总额的保证金,不够扣就要进行充值
if err == nil && job.Status != model.JobStatusFailed {
//1、账户支出增加一条记录
if err = financial.AddBillExpend(db, userBill.BillID, model.OrderTypeDeposit, job.TotalPrice); err != nil {
dao.Rollback(db)
}
//2、账户表保证金总额增加相应值
userBill.DepositBalance -= job.TotalPrice
if _, err = dao.UpdateEntity(db, userBill, "DepositBalance"); err != nil {
dao.Rollback(db)
}
}
dao.Commit(db)
return err
}

View File

@@ -11,10 +11,6 @@ import (
"git.rosy.net.cn/jx-callback/business/model"
)
const (
OrderTypeDeposit = 1 //保证金
)
func CreateOrder(ctx *jxcontext.Context, price, orderType int) (order *model.Order, err error) {
var (
db = dao.GetDB()
@@ -47,8 +43,24 @@ func Pay(ctx *jxcontext.Context, orderID, payType int, vendorPayType string) (er
order = &model.Order{
OrderID: int64(orderID),
}
payHandler = &financial.PayHandler{
PayType: payType,
Ctx: ctx,
VendorPayType: vendorPayType,
}
)
err = dao.GetEntity(db, order, "OrderID")
err = financial.IPayHandler.CreatePay(order, payType, vendorPayType)
payHandler.Order = order
//如果用户没有对应账单信息就给他生成一条
userBill, err := dao.GetUserBill(db, order.UserID, "")
if userBill == nil {
userBillInsert := &model.UserBill{
BillID: jxutils.GenBillID(),
UserID: order.UserID,
}
dao.WrapAddIDCULDEntity(userBillInsert, jxcontext.AdminCtx.GetUserName())
err = dao.CreateEntity(db, userBillInsert)
}
err = payHandler.CreatePay()
return err
}

View File

@@ -206,12 +206,25 @@ func RegisterUserWithMobile(ctx *jxcontext.Context, user *model.User, mobileVeri
} else {
outAuthInfo, err = auth2.BindUser(inAuthInfo, user)
}
//给用户添加一条账单记录
err = AddUserBill(user)
} else if dao.IsDuplicateError(err) {
err = auth2.ErrUserID2AlreadyExist
}
return outAuthInfo, err
}
func AddUserBill(user *model.User) (err error) {
var (
db = dao.GetDB()
)
userBill := &model.UserBill{
BillID: jxutils.GenBillID(),
UserID: user.UserID,
}
return dao.CreateEntity(db, userBill)
}
func GetUserBindAuthInfo(ctx *jxcontext.Context) (authList []*model.AuthBind, err error) {
authInfo, err := ctx.GetV2AuthInfo()
if err == nil {

View File

@@ -0,0 +1,27 @@
package financial
import (
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
)
func AddBillIncome(db *dao.DaoDB, billID int64, billType, incomePrice int) (err error) {
billIncome := &model.BillIncome{
BillID: billID,
Type: billType,
IncomePrice: incomePrice,
}
dao.WrapAddIDCULEntity(billIncome, jxcontext.AdminCtx.GetUserName())
return dao.CreateEntity(db, billIncome)
}
func AddBillExpend(db *dao.DaoDB, billID int64, billType, expendPrice int) (err error) {
billExpend := &model.BillExpend{
BillID: billID,
Type: billType,
ExpendPrice: expendPrice,
}
dao.WrapAddIDCULEntity(billExpend, jxcontext.AdminCtx.GetUserName())
return dao.CreateEntity(db, billExpend)
}

View File

@@ -17,21 +17,17 @@ import (
"git.rosy.net.cn/jx-callback/globals/api"
)
var (
IPayHandler PayHandlerInterface
)
func (p *PayHandler) CreatePay(order *model.Order, payType int, vendorPayType string) (err error) {
switch payType {
func (p *PayHandler) CreatePay() (err error) {
switch p.PayType {
case model.PayTypeTL:
param := &tonglianpayapi.CreateUnitorderOrderParam{
Trxamt: int(order.PayPrice),
Trxamt: int(p.Order.PayPrice),
NotifyUrl: globals.TLPayNotifyURL,
Reqsn: utils.Int64ToStr(order.OrderID),
PayType: vendorPayType,
Reqsn: utils.Int64ToStr(p.Order.OrderID),
PayType: p.VendorPayType,
}
if vendorPayType == tonglianpayapi.PayTypeWxXcx {
if authInfo, err := ctx.GetV2AuthInfo(); err == nil && authInfo.GetAuthType() == weixin.AuthTypeMini {
if p.VendorPayType == tonglianpayapi.PayTypeWxXcx {
if authInfo, err := p.Ctx.GetV2AuthInfo(); err == nil && authInfo.GetAuthType() == weixin.AuthTypeMini {
param.Acct = authInfo.GetAuthID()
}
}
@@ -39,66 +35,17 @@ func (p *PayHandler) CreatePay(order *model.Order, payType int, vendorPayType st
if err == nil {
var result2 tonglianpayapi.PayInfo
json.Unmarshal([]byte(result.PayInfo), &result2)
order.PrepayID = result2.Package[strings.LastIndex(result2.Package, "=")+1 : len(result2.Package)]
order.TransactionID = result.TrxID
order.OriginalData = utils.LimitUTF8StringLen(result.PayInfo, 3200)
p.Order.PrepayID = result2.Package[strings.LastIndex(result2.Package, "=")+1 : len(result2.Package)]
p.Order.TransactionID = result.TrxID
// p.Order.OriginalData = utils.LimitUTF8StringLen(result.PayInfo, 3200)
_, err = dao.UpdateEntity(dao.GetDB(), p.Order, "PrepayID", "TransactionID", "OriginalData")
}
default:
err = fmt.Errorf("支付方式:%d当前不支持", payType)
err = fmt.Errorf("支付方式:%d当前不支持", p.PayType)
}
return err
}
func Pay4OrderByTL(ctx *jxcontext.Context, order *model.GoodsOrder, payType int, vendorPayType string) (orderPay *model.OrderPay, err error) {
payCreatedAt := time.Now()
param := &tonglianpayapi.CreateUnitorderOrderParam{
Trxamt: int(order.ActualPayPrice),
NotifyUrl: globals.TLPayNotifyURL,
Reqsn: order.VendorOrderID,
PayType: vendorPayType,
}
//暂时做兼容处理
if vendorPayType == "JSAPI" {
param.PayType = tonglianpayapi.PayTypeWxXcx
}
if vendorPayType == tonglianpayapi.PayTypeWxXcx {
if authInfo, err := ctx.GetV2AuthInfo(); err == nil && authInfo.GetAuthType() == weixin.AuthTypeMini {
param.Acct = authInfo.GetAuthID()
}
}
if vendorPayType == tonglianpayapi.PayTypeH5 {
param2 := &tonglianpayapi.CreateH5UnitorderOrderParam{
Trxamt: int(order.ActualPayPrice),
NotifyUrl: globals.TLPayNotifyURL,
Body: "京西菜市",
Charset: "UTF-8",
}
err = api.TLpayAPI.CreateH5UnitorderOrder(param2)
} else {
result, err := api.TLpayAPI.CreateUnitorderOrder(param)
if err == nil {
var result2 tonglianpayapi.PayInfo
json.Unmarshal([]byte(result.PayInfo), &result2)
prePayID := result2.Package[strings.LastIndex(result2.Package, "=")+1 : len(result2.Package)]
orderPay = &model.OrderPay{
PayOrderID: param.Reqsn,
PayType: payType,
VendorPayType: vendorPayType,
TransactionID: result.TrxID,
VendorOrderID: order.VendorOrderID,
VendorID: order.VendorID,
Status: 0,
PayCreatedAt: payCreatedAt,
PrepayID: prePayID,
CodeURL: utils.LimitUTF8StringLen(result.PayInfo, 3200),
TotalFee: int(order.ActualPayPrice),
}
}
}
return orderPay, err
}
func OnTLPayCallback(call *tonglianpayapi.CallBackResult) (err error) {
globals.SugarLogger.Debugf("OnTLPayCallback msg:%s", utils.Format4Output(call, true))
switch call.TrxCode {
@@ -111,30 +58,28 @@ func OnTLPayCallback(call *tonglianpayapi.CallBackResult) (err error) {
}
func onTLpayFinished(call *tonglianpayapi.CallBackResult) (err error) {
orderPay := &model.OrderPay{
PayOrderID: call.CusorderID,
// PayType: model.PayTypeTL,
order := &model.Order{
OrderID: utils.Str2Int64(call.CusorderID),
}
orderPay.DeletedAt = utils.DefaultTimeValue
db := dao.GetDB()
if err = dao.GetEntity(db, orderPay, "PayOrderID", "DeletedAt"); err == nil {
if orderPay.Status != 0 {
if err = dao.GetEntity(db, order, "OrderID"); err == nil {
if order.Status != model.OrderStatusWait4Pay {
globals.SugarLogger.Debugf("already pay msg:%s, err:%v", utils.Format4Output(call, true), err)
return err
}
loc, _ := time.LoadLocation("Local")
t1, _ := time.ParseInLocation("20060102150405", call.PayTime, loc)
orderPay.PayFinishedAt = utils.Time2Pointer(t1)
// orderPay.TransactionID = call.ChnlTrxID
orderPay.OriginalData = utils.Format4Output(call, true)
order.PayFinishedAt = utils.Time2Pointer(t1)
// order.TransactionID = call.ChnlTrxID
order.OriginalData = utils.Format4Output(call, true)
if call.TrxStatus == tonglianpayapi.TrxStatusSuccess {
orderPay.Status = model.PayStatusYes
order.Status = model.OrderStatusFinished
} else {
orderPay.Status = model.PayStatusFailed
order.Status = model.OrderStatusCanceled
}
dao.UpdateEntity(db, orderPay)
dao.UpdateEntity(db, order)
if call.TrxStatus == tonglianpayapi.TrxStatusSuccess {
// err = OnPayFinished(orderPay)
err = OnPayFinished(order)
}
} else {
globals.SugarLogger.Debugf("onTLpayFinished msg:%s, err:%v", utils.Format4Output(call, true), err)

View File

@@ -1,13 +1,64 @@
package financial
import (
"fmt"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
)
type PayHandler struct {
PayType int `json:"-"` //支付方式
PayType int `json:"-"` //支付方式
Ctx *jxcontext.Context
Order *model.Order
VendorPayType string
}
type PayHandlerInterface interface {
CreatePay(order *model.Order, payType int, vendorPayType string) (err error)
CreatePay() (err error)
}
func OnPayFinished(order *model.Order) (err error) {
var (
db = dao.GetDB()
billID int64
)
globals.SugarLogger.Debugf("OnPayFinished begin modify account order: %v", utils.Format4Output(order, false))
dao.Begin(db)
defer func() {
if r := recover(); r != nil {
dao.Rollback(db)
panic(r)
}
}()
//如果用户没有对应账单信息就给他生成一条
userBill, err := dao.GetUserBill(db, order.UserID, "")
if userBill == nil {
globals.SugarLogger.Debugf("OnPayFinished 未找到该用户的账单 order: %v", utils.Format4Output(order, false))
return fmt.Errorf("未找到该用户的账单!%v", order.UserID)
}
//根据订单类型来操作账户
switch order.Type {
case model.OrderTypeDeposit:
//如果是发布任务的保证金
//1、账户收入表增加一条保证金记录
if err = AddBillIncome(db, billID, order.Type, order.PayPrice); err != nil {
dao.Rollback(db)
}
//2、账户表保证金总额增加相应值
userBill.DepositBalance += order.PayPrice
if _, err = dao.UpdateEntity(db, userBill, "DepositBalance"); err != nil {
dao.Rollback(db)
}
default:
globals.SugarLogger.Debugf("OnPayFinished 暂不支持此订单类型 order: %v", utils.Format4Output(order, false))
return fmt.Errorf("暂不支持此订单类型!")
}
dao.Commit(db)
globals.SugarLogger.Debugf("OnPayFinished end modify account ...")
return err
}