rest_user_service.go 5.6 KB
package domain_service

import (
	"encoding/json"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/service_gateway/adaptor"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/log"
)

type UserService struct {
}

// UserFrom 获取普通用户
func (service *UserService) UserFrom(companyId int64, orgId int64, userId int64) (*domain.User, error) {
	var returnData *domain.User
	if userAdaptor, err := adaptor.NewUserAdaptor(); err != nil {
		return nil, err
	} else {
		if user, err := userAdaptor.ToParticipator(companyId, orgId, userId, "User"); err != nil {
			return nil, err
		} else {
			if user != nil {
				log.Logger.Debug("普通用户", map[string]interface{}{
					"user interface": user,
				})
				userJson, err1 := json.Marshal(user)
				if err1 != nil {
					return nil, err
				}
				err2 := json.Unmarshal(userJson, &returnData)
				if err2 != nil {
					return nil, err2
				}
			}
			return returnData, nil
		}
	}
}

// ReferrerFrom 获取推荐人
func (service *UserService) ReferrerFrom(companyId int64, orgId int64, userId int64) (*domain.Referrer, error) {
	var returnData *domain.Referrer
	if userAdaptor, err := adaptor.NewUserAdaptor(); err != nil {
		return nil, err
	} else {
		if referrer, err := userAdaptor.ToParticipator(companyId, orgId, userId, "Referrer"); err != nil {
			return nil, err
		} else {
			if referrer != nil {
				log.Logger.Debug("推荐人", map[string]interface{}{
					"referrer interface": referrer,
				})
				referrerJson, err1 := json.Marshal(referrer)
				if err1 != nil {
					return nil, err
				}
				err2 := json.Unmarshal(referrerJson, &returnData)
				if err2 != nil {
					return nil, err2
				}
			}
			return returnData, nil
		}
	}
}

// UndertakerFrom 获取承接人
func (service *UserService) UndertakerFrom(companyId int64, orgId int64, userId int64) (*domain.Undertaker, error) {
	var returnData *domain.Undertaker
	if userAdaptor, err := adaptor.NewUserAdaptor(); err != nil {
		return nil, err
	} else {
		if undertaker, err := userAdaptor.ToParticipator(companyId, orgId, userId, "Undertaker"); err != nil {
			return nil, err
		} else {
			if undertaker != nil {
				log.Logger.Debug("承接人", map[string]interface{}{
					"undertaker interface": undertaker,
				})
				undertakerJson, err1 := json.Marshal(undertaker)
				if err1 != nil {
					return nil, err
				}
				err2 := json.Unmarshal(undertakerJson, &returnData)
				if err2 != nil {
					return nil, err2
				}
			}
			return returnData, nil
		}
	}
}

// RelevantFrom 获取相关人
func (service *UserService) RelevantFrom(companyId int64, orgId int64, userId int64) (*domain.Relevant, error) {
	var returnData *domain.Relevant
	if userAdaptor, err := adaptor.NewUserAdaptor(); err != nil {
		return nil, err
	} else {
		if relevant, err := userAdaptor.ToParticipator(companyId, orgId, userId, "Relevant"); err != nil {
			return nil, err
		} else {
			if relevant != nil {
				log.Logger.Debug("相关人", map[string]interface{}{
					"relevant interface": relevant,
				})
				relevantJson, err1 := json.Marshal(relevant)
				if err1 != nil {
					return nil, err
				}
				err2 := json.Unmarshal(relevantJson, &returnData)
				if err2 != nil {
					return nil, err2
				}
			}
			return returnData, nil
		}
	}
}

// SalesmanFrom 获取业务员
func (service *UserService) SalesmanFrom(companyId int64, orgId int64, userId int64) (*domain.Salesman, error) {
	var returnData *domain.Salesman
	if userAdaptor, err := adaptor.NewUserAdaptor(); err != nil {
		return nil, err
	} else {
		if salesman, err := userAdaptor.ToParticipator(companyId, orgId, userId, "Salesman"); err != nil {
			return nil, err
		} else {
			if salesman != nil {
				log.Logger.Debug("业务员", map[string]interface{}{
					"salesman interface": salesman,
				})
				salesmanJson, err1 := json.Marshal(salesman)
				if err1 != nil {
					return nil, err
				}
				err2 := json.Unmarshal(salesmanJson, &returnData)
				if err2 != nil {
					return nil, err2
				}
			}
			return returnData, nil
		}
	}
}

// OperatorFrom 获取操作人
func (service *UserService) OperatorFrom(companyId int64, orgId int64, userId int64) (*domain.User, error) {
	var returnData *domain.User
	if userAdaptor, err := adaptor.NewUserAdaptor(); err != nil {
		return nil, err
	} else {
		if operator, err := userAdaptor.ToParticipator(companyId, orgId, userId, "Operator"); err != nil {
			return nil, err
		} else {
			if operator != nil {
				log.Logger.Debug("操作人", map[string]interface{}{
					"operator interface": operator,
				})
				operatorJson, err1 := json.Marshal(operator)
				if err1 != nil {
					return nil, err
				}
				err2 := json.Unmarshal(operatorJson, &returnData)
				if err2 != nil {
					return nil, err2
				}
			}
			return returnData, nil
		}
	}
}

// VisitorFrom 获取游客
func (service *UserService) VisitorFrom(companyId int64, orgId int64, userBaseId int64) (*domain.User, error) {
	var returnData *domain.User
	if userAdaptor, err := adaptor.NewUserAdaptor(); err != nil {
		return nil, err
	} else {
		if visitor, err := userAdaptor.ToParticipator(companyId, orgId, userBaseId, "Visitor"); err != nil {
			return nil, err
		} else {
			if visitor != nil {
				log.Logger.Debug("游客", map[string]interface{}{
					"visitor interface": visitor,
				})
				visitorJson, err1 := json.Marshal(visitor)
				if err1 != nil {
					return nil, err
				}
				err2 := json.Unmarshal(visitorJson, &returnData)
				if err2 != nil {
					return nil, err2
				}
			}
			return returnData, nil
		}
	}
}

func NewUserService() (*UserService, error) {
	return &UserService{}, nil
}