child.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package biz
  2. import (
  3. "context"
  4. "go.uber.org/zap"
  5. "google.golang.org/grpc/codes"
  6. "google.golang.org/grpc/status"
  7. pb "sikey/w303a/http/api/user/v2"
  8. "sikey/w303a/http/internal/data/ent"
  9. "sikey/w303a/http/pkg/zaplog"
  10. "time"
  11. )
  12. const (
  13. MinHeight int32 = 150 // MinHeight 最小身高,当添加小孩时未设置身高体重时使用到
  14. MinWeight int32 = 150 // MinWeight 最小体重,当添加小孩时未设置身高体重时使用到
  15. )
  16. var (
  17. GenderDefault Gender = 0 // GenderDefault 默认值
  18. GenderMan Gender = 2 // GenderMan 男
  19. GenderWoman Gender = 3 // GenderWoman 女
  20. )
  21. type (
  22. Child struct {
  23. ID string // 小孩 ID
  24. Name string // 小孩名称
  25. Avatar *string // 头像
  26. Height int32 // 身高
  27. Weight int32 // 体重
  28. Birthday *string // 生日
  29. Gender Gender // 性别
  30. PhoneNumber *PhoneNumber // 手机号信息
  31. Refs []*ChildRef // 关联的小孩信息
  32. IsBlockUnknownCalls bool // 是否拦截陌生电话
  33. IsTotalFence bool // 是否开启围栏
  34. CreatedAt time.Time // 创建时间
  35. }
  36. ChildRepo interface {
  37. // FindByPhoneNumber finds a Child by the phone number.
  38. FindByPhoneNumber(ctx context.Context, phoneNumber *PhoneNumber) (*Child, error)
  39. // FindByChildID finds a Child by the child ID.
  40. FindByChildID(ctx context.Context, cid string) (*Child, error)
  41. // FindByUserID finds a Child by the user ID.
  42. FindByUserID(ctx context.Context, uid string) ([]*Child, error)
  43. // Create creates a new child.
  44. Create(ctx context.Context, child *Child) error
  45. // Update updates the child.
  46. Update(ctx context.Context, child *Child) error
  47. // Delete deletes the child.
  48. Delete(ctx context.Context, cid string) error
  49. }
  50. ChildUsecase struct {
  51. log *zaplog.Logger
  52. childRepo ChildRepo
  53. }
  54. )
  55. func NewChildUsecase(log *zaplog.Logger, childRepo ChildRepo) *ChildUsecase {
  56. return &ChildUsecase{log: log, childRepo: childRepo}
  57. }
  58. // GetChild gets the child list.
  59. func (uc *ChildUsecase) GetChild(ctx context.Context, req *pb.GetChildRequest) (*pb.GetChildResponse, error) {
  60. childs, err := uc.childRepo.FindByUserID(ctx, getIdByContext(ctx))
  61. if err != nil {
  62. if ent.IsNotFound(err) {
  63. return &pb.GetChildResponse{
  64. ChildList: make([]*pb.GetChildResponse_Child, 0),
  65. }, nil
  66. }
  67. return nil, err
  68. }
  69. var childList []*pb.GetChildResponse_Child
  70. for _, child := range childs {
  71. respChild := &pb.GetChildResponse_Child{
  72. Cid: child.ID,
  73. Name: child.Name,
  74. Avatar: child.Avatar,
  75. }
  76. // TODO 查询设备信息,和设备最后一次定位的傻乎乎
  77. childList = append(childList, respChild)
  78. }
  79. return &pb.GetChildResponse{ChildList: childList}, nil
  80. }
  81. // CreateChild creates a new child.
  82. func (uc *ChildUsecase) CreateChild(ctx context.Context, req *pb.CreateChildRequest) (resp *pb.CreateChildResponse, err error) {
  83. var (
  84. userId = getIdByContext(ctx)
  85. childId = newUUID().String()
  86. phoneNumber = NewPhoneNumber(req.PhoneNumber, req.AreaCode)
  87. )
  88. _, err = uc.childRepo.FindByPhoneNumber(ctx, phoneNumber)
  89. if err != nil {
  90. if !ent.IsNotFound(err) { // 手机号已经使用
  91. return &pb.CreateChildResponse{}, ErrUserPhoneNumberHasBeenUsed
  92. }
  93. }
  94. if err = uc.childRepo.Create(ctx, &Child{
  95. ID: childId,
  96. Name: req.Name,
  97. Avatar: req.Avatar,
  98. Height: req.Height,
  99. Weight: req.Weight,
  100. Birthday: req.Birthday,
  101. Gender: Gender(req.Gender),
  102. PhoneNumber: phoneNumber,
  103. Refs: []*ChildRef{newChildRef(userId, childId)},
  104. }); err != nil {
  105. return &pb.CreateChildResponse{}, err
  106. }
  107. uc.log.Info("创建小孩",
  108. zap.String("userId", userId),
  109. zap.String("childId", childId),
  110. zap.String("childName", req.Name),
  111. zap.String("phoneNumber", phoneNumber.String()))
  112. return &pb.CreateChildResponse{Cid: childId}, nil
  113. }
  114. // UpdateChild updates the child.
  115. func (uc *ChildUsecase) UpdateChild(context.Context, *pb.UpdateChildRequest) (*pb.UpdateChildResponse, error) {
  116. return nil, status.Errorf(codes.Unimplemented, "method UpdateChild not implemented")
  117. }
  118. // DeleteChild deletes the child.
  119. func (uc *ChildUsecase) DeleteChild(context.Context, *pb.DeleteChildRequest) (*pb.DeleteChildResponse, error) {
  120. return nil, status.Errorf(codes.Unimplemented, "method DeleteChild not implemented")
  121. }