79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
|
|
package logic
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"database/sql"
|
||
|
|
"fmt"
|
||
|
|
"strconv"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"muyu-apiserver/model"
|
||
|
|
"muyu-apiserver/pkg/tenantctx"
|
||
|
|
"muyu-apiserver/pkg/uid"
|
||
|
|
"muyu-apiserver/rpc/purchase/internal/svc"
|
||
|
|
"muyu-apiserver/rpc/purchase/pb"
|
||
|
|
|
||
|
|
"github.com/zeromicro/go-zero/core/logx"
|
||
|
|
)
|
||
|
|
|
||
|
|
type CreatePurchasePaymentLogic struct {
|
||
|
|
ctx context.Context
|
||
|
|
svcCtx *svc.ServiceContext
|
||
|
|
logx.Logger
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewCreatePurchasePaymentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreatePurchasePaymentLogic {
|
||
|
|
return &CreatePurchasePaymentLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (l *CreatePurchasePaymentLogic) CreatePurchasePayment(in *pb.CreatePurchasePaymentReq) (*pb.IdResp, error) {
|
||
|
|
tenantId := tenantctx.ExtractTenantId(l.ctx)
|
||
|
|
|
||
|
|
order, err := l.svcCtx.OrderModel.FindOneByOrderId(l.ctx, in.OrderId)
|
||
|
|
if err != nil {
|
||
|
|
return nil, ErrOrderNotFound
|
||
|
|
}
|
||
|
|
|
||
|
|
amount, _ := strconv.ParseFloat(in.Amount, 64)
|
||
|
|
|
||
|
|
paymentDate, err := time.Parse("2006-01-02", in.PaymentDate)
|
||
|
|
if err != nil {
|
||
|
|
paymentDate = time.Now()
|
||
|
|
}
|
||
|
|
|
||
|
|
// Generate payment number
|
||
|
|
today := time.Now().Format("20060102")
|
||
|
|
count, err := l.svcCtx.PaymentModel.CountTodayPayments(l.ctx, tenantId, time.Now().Format("2006-01-02"))
|
||
|
|
if err != nil {
|
||
|
|
count = 0
|
||
|
|
}
|
||
|
|
_ = fmt.Sprintf("PAY-%s-%04d", today, count+1)
|
||
|
|
|
||
|
|
paymentId := uid.Generate()
|
||
|
|
_, err = l.svcCtx.PaymentModel.Insert(l.ctx, &model.PurPurchasePayment{
|
||
|
|
PaymentId: paymentId,
|
||
|
|
TenantId: tenantId,
|
||
|
|
OrderId: in.OrderId,
|
||
|
|
SupplierId: order.SupplierId,
|
||
|
|
PaymentDate: paymentDate,
|
||
|
|
Amount: amount,
|
||
|
|
PaymentMethod: in.PaymentMethod,
|
||
|
|
Operator: tenantId,
|
||
|
|
Remark: sql.NullString{String: in.Remark, Valid: in.Remark != ""},
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update order paid_amount and payment_status
|
||
|
|
if err := l.svcCtx.OrderModel.UpdatePaymentProgress(l.ctx, in.OrderId, amount); err != nil {
|
||
|
|
l.Errorf("update order payment progress failed: %v", err)
|
||
|
|
}
|
||
|
|
// Update supplier paid_amount
|
||
|
|
if err := l.svcCtx.SupplierModel.UpdateStats(l.ctx, order.SupplierId, 0, 0, amount, 0); err != nil {
|
||
|
|
l.Errorf("update supplier paid_amount failed: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return &pb.IdResp{Id: paymentId}, nil
|
||
|
|
}
|