You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
203 lines
7.4 KiB
203 lines
7.4 KiB
6 months ago
|
package data
|
||
|
|
||
|
import (
|
||
|
"bkb-payment/api"
|
||
|
"bkb-payment/internal/biz"
|
||
|
"bkb-payment/internal/data/model"
|
||
|
"bkb-payment/pkg/util"
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
"github.com/go-kratos/kratos/v2/log"
|
||
|
)
|
||
|
|
||
|
type greeterRepo struct {
|
||
|
data *Data
|
||
|
log *log.Helper
|
||
|
}
|
||
|
|
||
|
// NewGreeterRepo .
|
||
|
func NewGreeterRepo(data *Data, logger log.Logger) biz.GreeterRepo {
|
||
|
return &greeterRepo{
|
||
|
data: data,
|
||
|
log: log.NewHelper(logger),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (r *greeterRepo) Save(ctx context.Context, g *biz.Greeter) (*biz.Greeter, error) {
|
||
|
return g, nil
|
||
|
}
|
||
|
|
||
|
func (r *greeterRepo) FindByID(context.Context, int64) (*biz.Greeter, error) {
|
||
|
return nil, nil
|
||
|
}
|
||
|
|
||
|
func (r *greeterRepo) ListByName(context.Context, string) ([]*biz.Greeter, error) {
|
||
|
return nil, nil
|
||
|
}
|
||
|
|
||
|
func (r *greeterRepo) ListAll(context.Context) ([]*biz.Greeter, error) {
|
||
|
return nil, nil
|
||
|
}
|
||
|
|
||
|
func (r *greeterRepo) CountryList(ctx context.Context) (result []*api.Country, err error) {
|
||
|
var list []model.Country
|
||
|
err = r.data.db.Model(&model.Country{}).Where("status = 1").Select("id,name").Find(&list).Error
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
result = make([]*api.Country, 0)
|
||
|
for _, country := range list {
|
||
|
result = append(result, &api.Country{
|
||
|
Id: int32(country.ID),
|
||
|
Name: country.Name,
|
||
|
})
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (r *greeterRepo) DistrictCascade(ctx context.Context, parentId uint) (result []*biz.DistrictCascade, err error) {
|
||
|
var list []model.District
|
||
|
err = r.data.db.Model(&model.District{}).Select("id,adcode,name,abbr,lat,lng,parent_id").Where("status = 1 AND country_id = ?", parentId).Order("adcode ASC").Find(&list).Error
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
result = make([]*biz.DistrictCascade, 0)
|
||
|
for _, district := range list {
|
||
|
tmp := biz.DistrictCascade{
|
||
|
ID: district.ID,
|
||
|
ParentID: district.ParentID,
|
||
|
Name: district.Name,
|
||
|
Adcode: district.Adcode,
|
||
|
Children: []*biz.DistrictCascade{},
|
||
|
}
|
||
|
result = append(result, &tmp)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (r *greeterRepo) GetAccount(ctx context.Context, appid string) (result *model.Account, err error) {
|
||
|
result = &model.Account{}
|
||
|
err = r.data.db.Model(&model.Account{}).Where("appid = ?", appid).First(result).Error
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (r *greeterRepo) GetBillByOutTradeNo(ctx context.Context, mchid, outTradeNo string) (result model.Bill, err error) {
|
||
|
err = r.data.db.Model(&model.Bill{}).Where("mchid = ? AND out_trade_no = ?", mchid, outTradeNo).First(&result).Error
|
||
|
return
|
||
|
}
|
||
|
func (r *greeterRepo) GetBillByTransactionId(ctx context.Context, mchid, transactionId string) (result model.Bill, err error) {
|
||
|
err = r.data.db.Model(&model.Bill{}).Where("mchid = ? AND transaction_id = ?", mchid, transactionId).First(&result).Error
|
||
|
return
|
||
|
}
|
||
|
func (r *greeterRepo) GetBillByPayId(ctx context.Context, mchid, payChannel, payId string) (result model.Bill, err error) {
|
||
|
result = model.Bill{}
|
||
|
err = r.data.db.Model(&model.Bill{}).Where("mchid = ? AND pay_channel = ? AND pay_id = ?", mchid, payChannel, payId).First(&result).Error
|
||
|
return
|
||
|
}
|
||
|
func (r *greeterRepo) GetBillByOutTradeNoPayId(ctx context.Context, mchid, outTradeNo, payId string) (result model.Bill, err error) {
|
||
|
err = r.data.db.Model(&model.Bill{}).Where("mchid = ? AND out_trade_no = ? AND pay_id = ?", mchid, outTradeNo, payId).First(&result).Error
|
||
|
return
|
||
|
}
|
||
|
func (r *greeterRepo) UpdateBillPayChannel(ctx context.Context, mchid, transactionId, payChannel string) error {
|
||
|
return r.data.db.Model(&model.Bill{}).Where("mchid = ? AND transaction_id = ? AND trade_state = 'ING'", mchid, transactionId).UpdateColumn("pay_channel", payChannel).Error
|
||
|
}
|
||
|
|
||
|
func (r *greeterRepo) UpdateBillPayId(ctx context.Context, mchid, transactionId, payId string) error {
|
||
|
return r.data.db.Model(&model.Bill{}).Where("mchid = ? AND transaction_id = ? AND trade_state = 'ING'", mchid, transactionId).UpdateColumn("pay_id", payId).Error
|
||
|
}
|
||
|
|
||
|
func (r *greeterRepo) CloseBill(ctx context.Context, transactionId string) error {
|
||
|
return r.data.db.Model(&model.Bill{}).Where("transaction_id = ? AND trade_state = 'ING'", transactionId).UpdateColumn("trade_state", "CLOSED").Error
|
||
|
}
|
||
|
|
||
|
func (r *greeterRepo) checkBillExistByOutTradeNo(ctx context.Context, mchid, outTradeNo string) (result bool) {
|
||
|
err := r.data.db.Model(&model.Bill{}).Where("mchid = ? AND out_trade_no = ?", mchid, outTradeNo).Unscoped().First(&model.Bill{}).Error
|
||
|
if err == nil {
|
||
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func (r *greeterRepo) CreateBill(ctx context.Context, request *biz.CreateBillReq) (result *biz.Bill, err error) {
|
||
|
//if r.checkBillExistByOutTradeNo(ctx, request.Mchid, request.OutTradeNo) {
|
||
|
// err = errors.New(400, "", "out_trade_no is exist")
|
||
|
// return
|
||
|
//}
|
||
|
var (
|
||
|
bill model.Bill
|
||
|
billAttach model.BillAttach
|
||
|
)
|
||
|
result = &biz.Bill{}
|
||
|
bill.Appid = request.Appid
|
||
|
bill.Mchid = request.Mchid
|
||
|
bill.TerminalType = request.TerminalType
|
||
|
bill.OsType = request.OsType
|
||
|
bill.OutTradeNo = request.OutTradeNo
|
||
|
bill.TransactionId = createTransactionId()
|
||
|
bill.TradeType = request.TradeType
|
||
|
bill.TradeState = "ING"
|
||
|
bill.PayChannel = request.PayChannel
|
||
|
bill.Currency = request.Currency
|
||
|
bill.Amount = request.Amount
|
||
|
bill.PayAccount = request.PayAccount
|
||
|
bill.PayUserId = ""
|
||
|
bill.PaymentMethod = request.PaymentMethod
|
||
|
tx := r.data.db.Begin()
|
||
|
err = tx.Model(&model.Bill{}).Create(&bill).Error
|
||
|
if err != nil {
|
||
|
tx.Rollback()
|
||
|
return nil, err
|
||
|
}
|
||
|
billAttach.TransactionId = bill.TransactionId
|
||
|
billAttach.Attach = request.Attach
|
||
|
billAttach.Description = request.Description
|
||
|
billAttach.NotifyUrl = request.NotifyUrl
|
||
|
billAttach.NotifyStatus = 0
|
||
|
err = tx.Model(&model.BillAttach{}).Create(&billAttach).Error
|
||
|
if err != nil {
|
||
|
tx.Rollback()
|
||
|
return nil, err
|
||
|
}
|
||
|
err = tx.Commit().Error
|
||
|
result.OutTradeNo = bill.OutTradeNo
|
||
|
result.TransactionId = bill.TransactionId
|
||
|
result.TradeState = bill.TradeState
|
||
|
result.PayChannel = bill.PayChannel
|
||
|
result.Currency = bill.Currency
|
||
|
result.Amount = bill.Amount
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (r *greeterRepo) UpdateBillTradeStatus(ctx context.Context, mchid, transactionId, status string) error {
|
||
|
return r.data.db.Model(&model.Bill{}).Where("mchid = ? AND transaction_id = ? AND trade_state = 'ING'", mchid, transactionId).UpdateColumn("trade_state", status).Error
|
||
|
}
|
||
|
|
||
|
func (r *greeterRepo) UpdateBillTradeStatusByPayId(ctx context.Context, payChannel, payId, status string) error {
|
||
|
return r.data.db.Model(&model.Bill{}).Where("pay_channel = ? AND pay_id = ? AND trade_state = 'ING'", payChannel, payId).
|
||
|
UpdateColumns(map[string]interface{}{"trade_state": status, "pay_time": time.Now()}).Error
|
||
|
}
|
||
|
|
||
|
func createTransactionId() string {
|
||
|
now := time.Now()
|
||
|
result := fmt.Sprintf("B%v%02d%02d%03d%v", now.Year(), now.YearDay()/7+1, int(now.Weekday()), now.YearDay(), strings.ReplaceAll(now.Format("150405.99"), ".", ""))
|
||
|
return result + util.Filler[:(20-len(result))]
|
||
|
}
|
||
|
|
||
|
func (r *greeterRepo) GetBillAttachByPayId(ctx context.Context, payChannel, payId string) (*biz.BillAttach, error) {
|
||
|
var (
|
||
|
err error
|
||
|
result biz.BillAttach
|
||
|
)
|
||
|
err = r.data.db.Model(&model.Bill{}).Joins("INNER JOIN bill_attach ON bill_attach.transaction_id = bill.transaction_id").
|
||
|
Where("bill.pay_channel = ? AND bill.pay_id = ?", payChannel, payId).
|
||
|
Select("bill.out_trade_no,bill.transaction_id,bill.trade_state,bill.pay_channel,bill.pay_id,bill_attach.attach,bill_attach.notify_url").First(&result).Error
|
||
|
return &result, err
|
||
|
}
|
||
|
|
||
|
func (r *greeterRepo) UpdateBillAttachNotifyStatus(ctx context.Context, transactionId string, notifyStatus int) error {
|
||
|
return r.data.db.WithContext(ctx).Model(&model.BillAttach{}).Where("transaction_id = ?", transactionId).UpdateColumn("notify_status", notifyStatus).Error
|
||
|
}
|