78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
package smithy
|
|
|
|
import (
|
|
"errors"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/utils"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
func (this *apiComp) CustomerCheck(session comm.IUserSession, req *pb.SmithyCustomerReq) (errdata *pb.ErrorData) {
|
|
return
|
|
}
|
|
|
|
func (this *apiComp) Customer(session comm.IUserSession, req *pb.SmithyCustomerReq) (errdata *pb.ErrorData) {
|
|
customerCount := this.module.ModuleTools.GetGlobalConf().SmithyMaxNpc
|
|
if customerCount <= 0 {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
}
|
|
return
|
|
}
|
|
cus, err := this.module.modelTrade.getDBCustomer(session.GetUserId())
|
|
if err != nil {
|
|
if err == mongo.ErrNoDocuments {
|
|
c, err := this.module.modelTrade.addCustomer(session.GetUserId(), customerCount)
|
|
if err != nil {
|
|
var customErr = new(comm.CustomError)
|
|
if errors.As(err, &customErr) {
|
|
errdata = &pb.ErrorData{
|
|
Code: customErr.Code,
|
|
Title: customErr.Code.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
} else {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
}
|
|
cus = c
|
|
} else {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
}
|
|
if utils.IsFirstTody(cus.LastRefreshTime) || (cus.Customers == nil || len(cus.Customers) == 0) {
|
|
this.module.modelTrade.DelByUId(session.GetUserId())
|
|
c, err := this.module.modelTrade.addCustomer(session.GetUserId(), customerCount)
|
|
if err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
cus = c
|
|
}
|
|
|
|
rsp := &pb.SmithyCustomerResp{
|
|
Customers: cus.Customers,
|
|
}
|
|
|
|
session.SendMsg(string(this.module.GetType()), "customer", rsp)
|
|
return
|
|
}
|