|
@@ -2,9 +2,10 @@ package credit_balance
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
- "github.com/alibabacloud-go/tea/tea"
|
|
|
"github.com/suyuan32/simple-admin-core/rpc/types/core"
|
|
|
"github.com/zeromicro/go-zero/core/errorx"
|
|
|
+ "wechat-api/ent"
|
|
|
+ "wechat-api/ent/creditbalance"
|
|
|
|
|
|
"github.com/suyuan32/simple-admin-common/msg/errormsg"
|
|
|
"wechat-api/internal/svc"
|
|
@@ -30,6 +31,10 @@ func NewCreateCreditBalanceLogic(ctx context.Context, svcCtx *svc.ServiceContext
|
|
|
func (l *CreateCreditBalanceLogic) CreateCreditBalance(req *types.CreditBalanceInfo) (*types.BaseMsgResp, error) {
|
|
|
userId := l.ctx.Value("userId").(string)
|
|
|
|
|
|
+ if req.OrganizationId == nil || *req.OrganizationId <= 0 {
|
|
|
+ return nil, errorx.NewInvalidArgumentError("参数非法")
|
|
|
+ }
|
|
|
+
|
|
|
_, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: *req.OrganizationId})
|
|
|
if err != nil {
|
|
|
return nil, errorx.NewAlreadyExistsError("租户不存在")
|
|
@@ -39,17 +44,31 @@ func (l *CreateCreditBalanceLogic) CreateCreditBalance(req *types.CreditBalanceI
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- _, err = tx.CreditBalance.Create().
|
|
|
- SetNotNilUserID(req.UserId).
|
|
|
- SetNotNilBalance(req.Balance).
|
|
|
- SetNotNilStatus(tea.Int(1)).
|
|
|
- SetNotNilOrganizationID(req.OrganizationId).
|
|
|
- Save(l.ctx)
|
|
|
|
|
|
- if err != nil {
|
|
|
- l.Logger.Errorf("create credit_balance failed! err=%v\n", err)
|
|
|
- _ = tx.Rollback()
|
|
|
+ old, err := tx.CreditBalance.Query().Where(creditbalance.OrganizationID(*req.OrganizationId)).First(l.ctx)
|
|
|
+ if err != nil && ent.IsNotFound(err) {
|
|
|
+ _, err = tx.CreditBalance.Create().
|
|
|
+ SetNotNilUserID(req.UserId).
|
|
|
+ SetNotNilBalance(req.Balance).
|
|
|
+ SetNotNilOrganizationID(req.OrganizationId).
|
|
|
+ Save(l.ctx)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ l.Logger.Errorf("create credit_balance failed! err=%v\n", err)
|
|
|
+ _ = tx.Rollback()
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ _, err = tx.CreditBalance.UpdateOneID(old.ID).
|
|
|
+ SetBalance(*req.Balance + old.Balance).
|
|
|
+ Save(l.ctx)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ l.Logger.Errorf("update credit_balance failed! err=%v\n", err)
|
|
|
+ _ = tx.Rollback()
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
_, err = tx.CreditUsage.Create().
|
|
|
SetNotNilUserID(req.UserId).
|
|
|
SetNotNilNumber(req.Balance).
|
|
@@ -57,7 +76,7 @@ func (l *CreateCreditBalanceLogic) CreateCreditBalance(req *types.CreditBalanceI
|
|
|
SetTable("").
|
|
|
SetNotNilOrganizationID(req.OrganizationId).
|
|
|
SetNid(0).
|
|
|
- SetReason("后台管理员创建").
|
|
|
+ SetReason("管理员操作").
|
|
|
SetOperator(userId).
|
|
|
Save(l.ctx)
|
|
|
if err != nil {
|