get_xunji_logic.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package xunji
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "github.com/suyuan32/simple-admin-common/utils/pointy"
  6. "wechat-api/ent"
  7. "wechat-api/ent/xunji"
  8. "wechat-api/internal/utils/dberrorhandler"
  9. "wechat-api/internal/svc"
  10. "wechat-api/internal/types"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. )
  13. type GetXunjiLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. }
  18. func NewGetXunjiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetXunjiLogic {
  19. return &GetXunjiLogic{
  20. Logger: logx.WithContext(ctx),
  21. ctx: ctx,
  22. svcCtx: svcCtx}
  23. }
  24. func (l *GetXunjiLogic) GetXunji() (resp *types.XunjiInfoResp, err error) {
  25. organizationId := l.ctx.Value("organizationId").(uint64)
  26. data, err := l.svcCtx.DB.Xunji.Query().Where(
  27. xunji.OrganizationID(organizationId),
  28. ).Order(ent.Desc(xunji.FieldCreatedAt)).First(l.ctx)
  29. if err != nil {
  30. // 如果记录为空则插入一条默认值的记录
  31. if ent.IsNotFound(err) {
  32. _, err = l.svcCtx.DB.Xunji.Create().
  33. SetStatus(2).
  34. SetOrganizationID(organizationId).
  35. Save(l.ctx)
  36. if err != nil {
  37. return nil, dberrorhandler.DefaultEntError(l.Logger, err, nil)
  38. }
  39. data, _ = l.svcCtx.DB.Xunji.Query().Where(
  40. xunji.OrganizationID(organizationId),
  41. ).Order(ent.Desc(xunji.FieldCreatedAt)).First(l.ctx)
  42. } else {
  43. return nil, dberrorhandler.DefaultEntError(l.Logger, err, nil)
  44. }
  45. }
  46. return &types.XunjiInfoResp{
  47. BaseDataInfo: types.BaseDataInfo{
  48. Code: 0,
  49. Msg: errormsg.Success,
  50. },
  51. Data: types.XunjiInfo{
  52. BaseIDInfo: types.BaseIDInfo{
  53. Id: &data.ID,
  54. CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
  55. UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
  56. },
  57. Status: &data.Status,
  58. AppKey: &data.AppKey,
  59. AppSecret: &data.AppSecret,
  60. Token: &data.Token,
  61. EncodingKey: &data.EncodingKey,
  62. OrganizationId: &data.OrganizationID,
  63. },
  64. }, nil
  65. }