create_contact_field_template_logic.go 2.1 KB

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