create_contact_field_template_logic.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package contact_field_template
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "wechat-api/ent/custom_types"
  6. "wechat-api/internal/utils/dberrorhandler"
  7. "wechat-api/internal/svc"
  8. "wechat-api/internal/types"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. )
  11. type CreateContactFieldTemplateLogic struct {
  12. logx.Logger
  13. ctx context.Context
  14. svcCtx *svc.ServiceContext
  15. }
  16. func NewCreateContactFieldTemplateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateContactFieldTemplateLogic {
  17. return &CreateContactFieldTemplateLogic{
  18. Logger: logx.WithContext(ctx),
  19. ctx: ctx,
  20. svcCtx: svcCtx}
  21. }
  22. func (l *CreateContactFieldTemplateLogic) CreateContactFieldTemplate(req *types.ContactFieldTemplateInfo) (resp *types.BaseMsgResp, err error) {
  23. organizationId := l.ctx.Value("organizationId").(uint64)
  24. template := ConvertContactFieldTemplates(req.Template)
  25. _, err = l.svcCtx.DB.ContactFieldTemplate.Create().
  26. SetOrganizationID(organizationId).
  27. SetTemplate(template).
  28. Save(l.ctx)
  29. if err != nil {
  30. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  31. }
  32. return &types.BaseMsgResp{Msg: errormsg.CreateSuccess}, nil
  33. }
  34. func ConvertContactFieldTemplates(input []types.ContactFieldTemplate) []custom_types.ContactFieldTemplate {
  35. result := make([]custom_types.ContactFieldTemplate, len(input))
  36. for i, item := range input {
  37. options := make([]custom_types.ContactFieldTemplateOptions, len(item.Options))
  38. for j, opt := range item.Options {
  39. options[j] = custom_types.ContactFieldTemplateOptions{
  40. Label: opt.Label,
  41. Value: opt.Value,
  42. }
  43. }
  44. result[i] = custom_types.ContactFieldTemplate{
  45. Type: item.Type,
  46. Id: item.Id,
  47. Label: item.Label,
  48. Options: options,
  49. }
  50. }
  51. return result
  52. }