feat: 添加争议微服务,支持订单争议流程
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"juwan-backend/app/dispute/rpc/internal/svc"
|
||||
"juwan-backend/app/dispute/rpc/pb"
|
||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AddDisputeTimelineLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewAddDisputeTimelineLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddDisputeTimelineLogic {
|
||||
return &AddDisputeTimelineLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------disputeTimeline-----------------------
|
||||
func (l *AddDisputeTimelineLogic) AddDisputeTimeline(in *pb.AddDisputeTimelineReq) (*pb.AddDisputeTimelineResp, error) {
|
||||
if in.GetDisputeId() <= 0 {
|
||||
return nil, errors.New("disputeId is required")
|
||||
}
|
||||
if in.GetEventType() == "" {
|
||||
return nil, errors.New("eventType is required")
|
||||
}
|
||||
|
||||
details, err := parseJSONMap(in.GetDetails())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
idResp, err := l.svcCtx.Snowflake.NextId(l.ctx, &snowflake.NextIdReq{})
|
||||
if err != nil {
|
||||
return nil, errors.New("create dispute timeline id failed")
|
||||
}
|
||||
|
||||
_, err = l.svcCtx.DisputeModelRW.DisputeTimeline.Create().
|
||||
SetID(idResp.Id).
|
||||
SetDisputeID(in.GetDisputeId()).
|
||||
SetEventType(in.GetEventType()).
|
||||
SetActorID(in.GetActorId()).
|
||||
SetActorName(in.GetActorName()).
|
||||
SetDetails(details).
|
||||
Save(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("addDisputeTimeline err: %v", err)
|
||||
return nil, errors.New("add dispute timeline failed")
|
||||
}
|
||||
|
||||
return &pb.AddDisputeTimelineResp{}, nil
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"juwan-backend/app/dispute/rpc/internal/svc"
|
||||
"juwan-backend/app/dispute/rpc/pb"
|
||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AddDisputesLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewAddDisputesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddDisputesLogic {
|
||||
return &AddDisputesLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------disputes-----------------------
|
||||
func (l *AddDisputesLogic) AddDisputes(in *pb.AddDisputesReq) (*pb.AddDisputesResp, error) {
|
||||
if in.GetOrderId() <= 0 {
|
||||
return nil, errors.New("orderId is required")
|
||||
}
|
||||
if in.GetInitiatorId() <= 0 {
|
||||
return nil, errors.New("initiatorId is required")
|
||||
}
|
||||
if in.GetRespondentId() <= 0 {
|
||||
return nil, errors.New("respondentId is required")
|
||||
}
|
||||
if in.GetReason() == "" {
|
||||
return nil, errors.New("reason is required")
|
||||
}
|
||||
|
||||
status := in.GetStatus()
|
||||
if status == "" {
|
||||
status = "open"
|
||||
}
|
||||
|
||||
idResp, err := l.svcCtx.Snowflake.NextId(l.ctx, &snowflake.NextIdReq{})
|
||||
if err != nil {
|
||||
return nil, errors.New("create dispute id failed")
|
||||
}
|
||||
|
||||
created, err := l.svcCtx.DisputeModelRW.Disputes.Create().
|
||||
SetID(idResp.Id).
|
||||
SetOrderID(in.GetOrderId()).
|
||||
SetInitiatorID(in.GetInitiatorId()).
|
||||
SetInitiatorName(in.GetInitiatorName()).
|
||||
SetRespondentID(in.GetRespondentId()).
|
||||
SetReason(in.GetReason()).
|
||||
SetEvidence(toTextArray(in.GetEvidence())).
|
||||
SetStatus(status).
|
||||
Save(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("addDisputes err: %v", err)
|
||||
return nil, errors.New("add dispute failed")
|
||||
}
|
||||
|
||||
return &pb.AddDisputesResp{Id: created.ID}, nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/dispute/rpc/internal/svc"
|
||||
"juwan-backend/app/dispute/rpc/pb"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DelDisputesLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewDelDisputesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelDisputesLogic {
|
||||
return &DelDisputesLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DelDisputesLogic) DelDisputes(in *pb.DelDisputesReq) (*pb.DelDisputesResp, error) {
|
||||
err := l.svcCtx.DisputeModelRW.Disputes.DeleteOneID(in.GetId()).Exec(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("delDisputes err: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.DelDisputesResp{}, nil
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/dispute/rpc/internal/svc"
|
||||
"juwan-backend/app/dispute/rpc/pb"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetDisputesByIdLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetDisputesByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetDisputesByIdLogic {
|
||||
return &GetDisputesByIdLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetDisputesByIdLogic) GetDisputesById(in *pb.GetDisputesByIdReq) (*pb.GetDisputesByIdResp, error) {
|
||||
d, err := l.svcCtx.DisputeModelRO.Disputes.Get(l.ctx, in.GetId())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.GetDisputesByIdResp{Disputes: entDisputeToPb(d)}, nil
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"juwan-backend/app/dispute/rpc/internal/models"
|
||||
"juwan-backend/app/dispute/rpc/pb"
|
||||
"juwan-backend/pkg/types"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
func toTextArray(s []string) types.TextArray {
|
||||
if len(s) == 0 {
|
||||
return types.TextArray{Valid: true}
|
||||
}
|
||||
return types.TextArray{
|
||||
Elements: s,
|
||||
Dims: []pgtype.ArrayDimension{{Length: int32(len(s)), LowerBound: 1}},
|
||||
Valid: true,
|
||||
}
|
||||
}
|
||||
|
||||
func parseJSONMap(v string) (map[string]any, error) {
|
||||
if v == "" {
|
||||
return map[string]any{}, nil
|
||||
}
|
||||
var result map[string]any
|
||||
if err := json.Unmarshal([]byte(v), &result); err != nil {
|
||||
return nil, errors.New("invalid json value")
|
||||
}
|
||||
if result == nil {
|
||||
return map[string]any{}, nil
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func entDisputeToPb(d *models.Disputes) *pb.Disputes {
|
||||
out := &pb.Disputes{
|
||||
Id: d.ID,
|
||||
OrderId: d.OrderID,
|
||||
InitiatorId: d.InitiatorID,
|
||||
InitiatorName: d.InitiatorName,
|
||||
RespondentId: d.RespondentID,
|
||||
Reason: d.Reason,
|
||||
Evidence: d.Evidence.Elements,
|
||||
Status: d.Status,
|
||||
RespondentEvidence: d.RespondentEvidence.Elements,
|
||||
CreatedAt: d.CreatedAt.Unix(),
|
||||
UpdatedAt: d.UpdatedAt.Unix(),
|
||||
}
|
||||
if d.Result != nil {
|
||||
out.Result = *d.Result
|
||||
}
|
||||
if d.RespondentReason != nil {
|
||||
out.RespondentReason = *d.RespondentReason
|
||||
}
|
||||
if d.AppealReason != nil {
|
||||
out.AppealReason = *d.AppealReason
|
||||
}
|
||||
if d.AppealedAt != nil {
|
||||
out.AppealedAt = d.AppealedAt.Unix()
|
||||
}
|
||||
if d.ResolvedBy != nil {
|
||||
out.ResolvedBy = *d.ResolvedBy
|
||||
}
|
||||
if d.ResolvedAt != nil {
|
||||
out.ResolvedAt = d.ResolvedAt.Unix()
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func entDisputeTimelineToPb(t *models.DisputeTimeline) *pb.DisputeTimeline {
|
||||
details := "{}"
|
||||
if t.Details != nil {
|
||||
if b, err := json.Marshal(t.Details); err == nil {
|
||||
details = string(b)
|
||||
}
|
||||
}
|
||||
return &pb.DisputeTimeline{
|
||||
Id: t.ID,
|
||||
DisputeId: t.DisputeID,
|
||||
EventType: t.EventType,
|
||||
ActorId: t.ActorID,
|
||||
ActorName: t.ActorName,
|
||||
Details: details,
|
||||
CreatedAt: t.CreatedAt.Unix(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"juwan-backend/app/dispute/rpc/internal/models/disputetimeline"
|
||||
"juwan-backend/app/dispute/rpc/internal/svc"
|
||||
"juwan-backend/app/dispute/rpc/pb"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SearchDisputeTimelineLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewSearchDisputeTimelineLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchDisputeTimelineLogic {
|
||||
return &SearchDisputeTimelineLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SearchDisputeTimelineLogic) SearchDisputeTimeline(in *pb.SearchDisputeTimelineReq) (*pb.SearchDisputeTimelineResp, error) {
|
||||
limit := in.GetLimit()
|
||||
if limit <= 0 {
|
||||
limit = 20
|
||||
}
|
||||
if limit > 100 {
|
||||
return nil, errors.New("limit too large")
|
||||
}
|
||||
offset := in.GetOffset()
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
|
||||
query := l.svcCtx.DisputeModelRO.DisputeTimeline.Query()
|
||||
if in.DisputeId != nil {
|
||||
query = query.Where(disputetimeline.DisputeIDEQ(in.GetDisputeId()))
|
||||
}
|
||||
|
||||
list, err := query.
|
||||
Order(disputetimeline.ByCreatedAt(sql.OrderAsc()), disputetimeline.ByID(sql.OrderAsc())).
|
||||
Offset(int(offset)).
|
||||
Limit(int(limit)).
|
||||
All(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("searchDisputeTimeline err: %v", err)
|
||||
return nil, errors.New("search dispute timeline failed")
|
||||
}
|
||||
|
||||
out := make([]*pb.DisputeTimeline, len(list))
|
||||
for i, item := range list {
|
||||
out[i] = entDisputeTimelineToPb(item)
|
||||
}
|
||||
|
||||
return &pb.SearchDisputeTimelineResp{Timeline: out}, nil
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"juwan-backend/app/dispute/rpc/internal/models/disputes"
|
||||
"juwan-backend/app/dispute/rpc/internal/svc"
|
||||
"juwan-backend/app/dispute/rpc/pb"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SearchDisputesLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewSearchDisputesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchDisputesLogic {
|
||||
return &SearchDisputesLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SearchDisputesLogic) SearchDisputes(in *pb.SearchDisputesReq) (*pb.SearchDisputesResp, error) {
|
||||
limit := in.GetLimit()
|
||||
if limit <= 0 {
|
||||
limit = 20
|
||||
}
|
||||
if limit > 100 {
|
||||
return nil, errors.New("limit too large")
|
||||
}
|
||||
offset := in.GetOffset()
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
|
||||
query := l.svcCtx.DisputeModelRO.Disputes.Query()
|
||||
if in.Id != nil {
|
||||
query = query.Where(disputes.IDEQ(in.GetId()))
|
||||
}
|
||||
if in.OrderId != nil {
|
||||
query = query.Where(disputes.OrderIDEQ(in.GetOrderId()))
|
||||
}
|
||||
if in.InitiatorId != nil {
|
||||
query = query.Where(disputes.InitiatorIDEQ(in.GetInitiatorId()))
|
||||
}
|
||||
if in.RespondentId != nil {
|
||||
query = query.Where(disputes.RespondentIDEQ(in.GetRespondentId()))
|
||||
}
|
||||
if in.Status != nil {
|
||||
query = query.Where(disputes.StatusEQ(in.GetStatus()))
|
||||
}
|
||||
|
||||
list, err := query.
|
||||
Order(disputes.ByCreatedAt(sql.OrderDesc()), disputes.ByID(sql.OrderDesc())).
|
||||
Offset(int(offset)).
|
||||
Limit(int(limit)).
|
||||
All(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("searchDisputes err: %v", err)
|
||||
return nil, errors.New("search disputes failed")
|
||||
}
|
||||
|
||||
out := make([]*pb.Disputes, len(list))
|
||||
for i, d := range list {
|
||||
out[i] = entDisputeToPb(d)
|
||||
}
|
||||
|
||||
return &pb.SearchDisputesResp{Disputes: out}, nil
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/dispute/rpc/internal/svc"
|
||||
"juwan-backend/app/dispute/rpc/pb"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdateDisputesLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewUpdateDisputesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateDisputesLogic {
|
||||
return &UpdateDisputesLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateDisputesLogic) UpdateDisputes(in *pb.UpdateDisputesReq) (*pb.UpdateDisputesResp, error) {
|
||||
updater := l.svcCtx.DisputeModelRW.Disputes.UpdateOneID(in.GetId())
|
||||
|
||||
if in.Status != nil {
|
||||
updater = updater.SetStatus(in.GetStatus())
|
||||
}
|
||||
if in.Result != nil {
|
||||
updater = updater.SetResult(in.GetResult())
|
||||
}
|
||||
if in.RespondentReason != nil {
|
||||
updater = updater.SetRespondentReason(in.GetRespondentReason())
|
||||
}
|
||||
if len(in.GetRespondentEvidence()) > 0 {
|
||||
updater = updater.SetRespondentEvidence(toTextArray(in.GetRespondentEvidence()))
|
||||
}
|
||||
if in.AppealReason != nil {
|
||||
updater = updater.SetAppealReason(in.GetAppealReason())
|
||||
}
|
||||
if in.AppealedAt != nil {
|
||||
updater = updater.SetAppealedAt(time.Unix(in.GetAppealedAt(), 0))
|
||||
}
|
||||
if in.ResolvedBy != nil {
|
||||
updater = updater.SetResolvedBy(in.GetResolvedBy())
|
||||
}
|
||||
if in.ResolvedAt != nil {
|
||||
updater = updater.SetResolvedAt(time.Unix(in.GetResolvedAt(), 0))
|
||||
}
|
||||
|
||||
_, err := updater.Save(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("updateDisputes err: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.UpdateDisputesResp{}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user