channel.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package serializer
  2. import (
  3. "dsbqj-admin/model/mongo/channel"
  4. )
  5. // ChannelResponse 渠道响应,匹配前端 ChannelData 格式
  6. type ChannelResponse struct {
  7. ID string `json:"id"`
  8. Name string `json:"name"`
  9. Code string `json:"code"`
  10. Description string `json:"description"`
  11. Status bool `json:"status"`
  12. CreateTime string `json:"createTime"`
  13. }
  14. // BuildChannel 构建单条渠道响应
  15. func BuildChannel(c *channel.Channel) *ChannelResponse {
  16. if c == nil {
  17. return nil
  18. }
  19. return &ChannelResponse{
  20. ID: c.ID.Hex(),
  21. Name: c.Name,
  22. Code: c.Code,
  23. Description: c.Description,
  24. Status: c.Status,
  25. CreateTime: c.GetCreateTime(),
  26. }
  27. }
  28. // BuildChannelList 构建渠道列表响应
  29. func BuildChannelList(list []channel.Channel, total int) map[string]interface{} {
  30. items := make([]*ChannelResponse, 0, len(list))
  31. for i := range list {
  32. items = append(items, BuildChannel(&list[i]))
  33. }
  34. return map[string]interface{}{
  35. "list": items,
  36. "total": total,
  37. }
  38. }
  39. // BuildChannelAll 构建渠道列表响应
  40. func BuildChannelAll(list []channel.Channel) []*ChannelResponse {
  41. items := make([]*ChannelResponse, 0, len(list))
  42. for i := range list {
  43. items = append(items, BuildChannel(&list[i]))
  44. }
  45. return items
  46. }