userhealthprofileservice.go 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package service
  2. import (
  3. "sws_xcx/models"
  4. "github.com/jinzhu/gorm"
  5. )
  6. type HealthProfileService struct {
  7. rdb *gorm.DB
  8. wdb *gorm.DB
  9. }
  10. func NewUserHealthProfileService() *HealthProfileService {
  11. p := &models.UserHealthProfile{}
  12. return &HealthProfileService{
  13. rdb: readDb.Model(p),
  14. wdb: writeDb.Model(p),
  15. }
  16. }
  17. func (s *HealthProfileService) GetUserHealthProfileByUserId(userId uint64) (*models.UserHealthProfile, error) {
  18. p := &models.UserHealthProfile{}
  19. err := s.rdb.Find(p, "user_id = ?", userId).Error
  20. return p, err
  21. }
  22. func (s *HealthProfileService) Get(id uint64) (models.UserHealthProfile, error) {
  23. p := &models.UserHealthProfile{}
  24. err := s.rdb.First(p, id).Error
  25. return *p, err
  26. }
  27. func (s *HealthProfileService) SavePatientInfo(userId uint64, realName string, idCard string, regPhone string) error {
  28. p, err := s.GetUserHealthProfileByUserId(userId)
  29. if err != nil {
  30. return err
  31. }
  32. if p == nil {
  33. p = &models.UserHealthProfile{}
  34. }
  35. p.UserId = userId
  36. p.RealName = realName
  37. p.InpatientRegPhone = regPhone
  38. return s.wdb.Save(p).Error
  39. }
  40. func (s *HealthProfileService) SaveHealthProfile(userId uint64, req models.SaveHealthProfileReq) error {
  41. p, err := s.GetUserHealthProfileByUserId(userId)
  42. if err != nil {
  43. return err
  44. }
  45. if p == nil {
  46. p = &models.UserHealthProfile{}
  47. }
  48. p.UserId = userId
  49. p.Birthday = req.Birthday
  50. p.BloodType = req.BloodType
  51. p.CreatineTime = req.CreatineTime
  52. p.Creatinine = req.Creatinine
  53. p.CreatinineUnit = req.CreatinineUnit
  54. p.Gender = req.Gender
  55. p.Height = req.Height
  56. p.IllnessState = req.IllnessState
  57. p.RenalFunctionStatus = req.RenalFunctionStatus
  58. p.UrineProtein = req.UrineProtein
  59. p.UrineProtein24h = req.UrineProtein24h
  60. p.UrineProtein24hTime = req.UrineProtein24hTime
  61. p.UrineProtein24hUnit = req.UrineProtein24hUnit
  62. p.UrineProteinTime = req.UrineProteinTime
  63. p.UrineProteinUnit = req.UrineProteinUnit
  64. p.Weight = req.Weight
  65. return s.wdb.Save(p).Error
  66. }
  67. func (s *HealthProfileService) Save(p *models.UserHealthProfile) error {
  68. return s.wdb.Save(p).Error
  69. }