dongfeng-pay/service/models/payfor_info.go

240 lines
5.8 KiB
Go
Raw Normal View History

2019-12-19 14:47:58 +08:00
/***************************************************
** @Desc : This file for ...
** @Time : 2019/11/25 14:32
** @Author : yuebin
** @File : payfor_info
** @Last Modified by : yuebin
** @Last Modified time: 2019/11/25 14:32
** @Software: GoLand
****************************************************/
package models
import (
"fmt"
"github.com/astaxie/beego/logs"
"github.com/astaxie/beego/orm"
2019-12-19 16:43:20 +08:00
"dongfeng-pay/service/common"
"dongfeng-pay/service/utils"
2019-12-19 14:47:58 +08:00
)
type PayforInfo struct {
Id int
PayforUid string
MerchantUid string
MerchantName string
MerchantOrderId string
BankOrderId string
BankTransId string
RoadUid string
RoadName string
RollPoolCode string
RollPoolName string
PayforFee float64
PayforAmount float64
PayforTotalAmount float64
BankCode string
BankName string
BankAccountName string
BankAccountNo string
BankAccountType string
Country string
City string
Ares string
BankAccountAddress string
PhoneNo string
GiveType string
Type string
NotifyUrl string
Status string
IsSend string
RequestTime string
ResponseTime string
ResponseContext string
Remark string
CreateTime string
UpdateTime string
}
const PAYFORINFO = "payfor_info"
func InsertPayfor(payFor PayforInfo) bool {
o := orm.NewOrm()
_, err := o.Insert(&payFor)
if err != nil {
logs.Error("insert payfor fail: ", err)
return false
}
return true
}
func IsExistPayForByBankOrderId(bankOrderId string) bool {
o := orm.NewOrm()
exist := o.QueryTable(PAYFORINFO).Filter("bank_order_id", bankOrderId).Exist()
return exist
}
func IsExistPayForByMerchantOrderId(merchantOrderId string) bool {
o := orm.NewOrm()
exist := o.QueryTable(PAYFORINFO).Filter("merchant_order_id", merchantOrderId).Exist()
return exist
}
func GetPayForLenByMap(params map[string]string) int {
o := orm.NewOrm()
qs := o.QueryTable(PAYFORINFO)
for k, v := range params {
if len(v) > 0 {
qs = qs.Filter(k, v)
}
}
cnt, err := qs.Limit(-1).Count()
if err != nil {
logs.Error("get pay for len by map fail: ", err)
}
return int(cnt)
}
func GetPayForByMap(params map[string]string, displayCount, offset int) []PayforInfo {
o := orm.NewOrm()
var payForList []PayforInfo
qs := o.QueryTable(PAYFORINFO)
for k, v := range params {
if len(v) > 0 {
qs = qs.Filter(k, v)
}
}
_, err := qs.Limit(displayCount, offset).OrderBy("-create_time").All(&payForList)
if err != nil {
logs.Error("get agentInfo by map fail: ", err)
}
return payForList
}
func GetPayForListByParams(params map[string]string) []PayforInfo {
o := orm.NewOrm()
var payForList []PayforInfo
qs := o.QueryTable(PAYFORINFO)
for k, v := range params {
if len(v) > 0 {
qs = qs.Filter(k, v)
}
}
_, err := qs.Limit(-1).All(&payForList)
if err != nil {
logs.Error("GetPayForListByParams fail", err)
}
return payForList
}
func GetPayForByBankOrderId(bankOrderId string) PayforInfo {
o := orm.NewOrm()
var payFor PayforInfo
_, err := o.QueryTable(PAYFORINFO).Filter("bank_order_id", bankOrderId).Limit(1).All(&payFor)
if err != nil {
logs.Error("get pay for by bank_order_id fail: ", err)
}
return payFor
}
func GetPayForByMerchantOrderId(merchantOrderId string) PayforInfo {
o := orm.NewOrm()
var payFor PayforInfo
_, err := o.QueryTable(PAYFORINFO).Filter("merchant_order_id", merchantOrderId).Limit(1).All(&payFor)
if err != nil {
logs.Error("fail: ", err)
}
return payFor
}
func ForUpdatePayFor(payFor PayforInfo) bool {
o := orm.NewOrm()
o.Begin()
defer func(interface{}) {
if err := recover(); err != nil {
logs.Error("for update pay for painc.....")
o.Rollback()
}
}(o)
var tmp PayforInfo
if err := o.Raw("select * from payfor_info where bank_order_id = ? for update", payFor.BankOrderId).QueryRow(&tmp); err != nil || tmp.PayforUid == "" {
logs.Error("for update payfor select fail:", err)
o.Rollback()
return false
}
if tmp.Status == common.PAYFOR_FAIL || tmp.Status == common.PAYFOR_SUCCESS {
o.Rollback()
return false
}
//如果是手动打款,并且是需要处理商户金额
if payFor.Status == common.PAYFOR_SOLVING && tmp.Status == common.PAYFOR_COMFRIM &&
payFor.GiveType == common.PAYFOR_HAND && payFor.Type != common.SELF_HELP {
var account AccountInfo
if err := o.Raw("select * from account_info where account_uid = ? for update", payFor.MerchantUid).QueryRow(&account); err != nil || account.AccountUid == "" {
logs.Error("for update payfor select account infofail", err)
o.Rollback()
return false
}
//计算该用户的可用金额
ableAmount := account.SettleAmount - account.FreezeAmount - account.PayforAmount - account.LoanAmount
if ableAmount >= payFor.PayforAmount+payFor.PayforFee {
account.PayforAmount += payFor.PayforFee + payFor.PayforAmount
account.UpdateTime = utils.GetBasicDateTime()
if _, err := o.Update(&account); err != nil {
logs.Error("for update payfor update account fail", err)
o.Rollback()
return false
}
} else {
logs.Error(fmt.Sprintf("商户uid=%s可用金额不够", payFor.MerchantUid))
payFor.ResponseContext = "商户可用余额不足"
payFor.Status = common.PAYFOR_FAIL
}
}
if _, err := o.Update(&payFor); err != nil {
logs.Error("for update payfor fail: ", err)
o.Rollback()
return false
}
if err := o.Commit(); err != nil {
logs.Info("for update payfor success")
} else {
logs.Error("for update payfor fail", err)
return false
}
return true
}
func UpdatePayFor(payFor PayforInfo) bool {
o := orm.NewOrm()
_, err := o.Update(&payFor)
if err != nil {
logs.Error("update pay for fail", err)
return false
}
return true
}