get_batch_msg_by_id_logic.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package batch_msg
  2. import (
  3. "context"
  4. "encoding/json"
  5. "github.com/zeromicro/go-zero/core/errorx"
  6. "wechat-api/ent"
  7. "wechat-api/ent/batchmsg"
  8. "wechat-api/internal/svc"
  9. "wechat-api/internal/types"
  10. "wechat-api/internal/utils/dberrorhandler"
  11. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  12. "github.com/suyuan32/simple-admin-common/utils/pointy"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type GetBatchMsgByIdLogic struct {
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. logx.Logger
  19. }
  20. func NewGetBatchMsgByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBatchMsgByIdLogic {
  21. return &GetBatchMsgByIdLogic{
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. Logger: logx.WithContext(ctx),
  25. }
  26. }
  27. func (l *GetBatchMsgByIdLogic) GetBatchMsgById(req *types.IDReq) (*types.BatchMsgInfoResp, error) {
  28. organizationId := l.ctx.Value("organizationId").(uint64)
  29. data, err := l.svcCtx.DB.BatchMsg.Query().Where(
  30. batchmsg.ID(req.Id),
  31. batchmsg.OrganizationID(organizationId),
  32. batchmsg.Ctype(1),
  33. ).First(l.ctx)
  34. if err != nil {
  35. if ent.IsNotFound(err) {
  36. return nil, errorx.NewInvalidArgumentError("群发消息不存在")
  37. }
  38. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  39. }
  40. labels, groupLables := make([]uint64, 0), make([]uint64, 0)
  41. tagMap := make(map[string][]uint64)
  42. err = json.Unmarshal([]byte(data.Tagids), &tagMap)
  43. if err != nil {
  44. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  45. }
  46. labels = tagMap["contact_tag"]
  47. groupLables = tagMap["group_tag"]
  48. return &types.BatchMsgInfoResp{
  49. BaseDataInfo: types.BaseDataInfo{
  50. Code: 0,
  51. Msg: errormsg.Success,
  52. },
  53. Data: types.BatchMsgInfo{
  54. BaseIDInfo: types.BaseIDInfo{
  55. Id: &data.ID,
  56. CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
  57. UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
  58. },
  59. Status: &data.Status,
  60. BatchNo: &data.BatchNo,
  61. TaskName: &data.TaskName,
  62. Fromwxid: &data.Fromwxid,
  63. Msg: &data.Msg,
  64. Tag: &data.Tag,
  65. Total: &data.Total,
  66. Success: &data.Success,
  67. Fail: &data.Fail,
  68. Type: &data.Type,
  69. StartTime: pointy.GetUnixMilliPointer(data.StartTime.UnixMilli()),
  70. StopTime: pointy.GetUnixMilliPointer(data.StopTime.UnixMilli()),
  71. SendTime: pointy.GetUnixMilliPointer(data.SendTime.UnixMilli()),
  72. StartTimeStr: pointy.GetPointer(data.StartTime.Format("2006-01-02 15:04:05")),
  73. Labels: labels,
  74. GroupLabels: groupLables,
  75. },
  76. }, nil
  77. }