Rick.Lan 1 month ago
parent
commit
0c7939d8f7

+ 11 - 0
.vscode/settings.json View File

1
+{
2
+    "testing.coverageBarThresholds": {
3
+        "red": 0,
4
+        "yellow": 60,
5
+        "green": 90
6
+    },
7
+    "testing.showAllMessages": true,  
8
+    "terminal.integrated.defaultProfile.windows": "Command Prompt",
9
+    "go.testFlags": ["-v" ]
10
+    
11
+}

+ 4 - 0
controllers/message_api_controllor.go View File

13
 	utils.InfoLog("Revice EMQX message: %s", c.Ctx.Input.RequestBody)
13
 	utils.InfoLog("Revice EMQX message: %s", c.Ctx.Input.RequestBody)
14
 	c.ServeSuccessJSON(map[string]interface{}{})
14
 	c.ServeSuccessJSON(map[string]interface{}{})
15
 }
15
 }
16
+
17
+func HandleMsg(req []byte) {
18
+
19
+}

+ 1 - 1
controllers/sysdic_api_controller.go View File

61
 // @router /getcheckitems [get]
61
 // @router /getcheckitems [get]
62
 func (c *SysDicApiController) GetCheckItems() {
62
 func (c *SysDicApiController) GetCheckItems() {
63
 	s := service.NewCheckItemService()
63
 	s := service.NewCheckItemService()
64
-	dics, err := s.GetCheckItems("cn")
64
+	dics, err := s.GetCheckItems("cn", "")
65
 	if err != nil {
65
 	if err != nil {
66
 		c.ServeDynamicFailJsonSend(err.Error())
66
 		c.ServeDynamicFailJsonSend(err.Error())
67
 		return
67
 		return

+ 19 - 8
controllers/user_api_controller.go View File

32
 		OpenId:                  c.CurrentUser.OpenId,
32
 		OpenId:                  c.CurrentUser.OpenId,
33
 		Status:                  c.CurrentUser.Status,
33
 		Status:                  c.CurrentUser.Status,
34
 		Source:                  c.CurrentUser.Source,
34
 		Source:                  c.CurrentUser.Source,
35
-		Ctime:                   c.CurrentUser.Ctime,
36
-		Mtime:                   c.CurrentUser.Mtime,
35
+		Ctime:                   models.Time(c.CurrentUser.Ctime),
36
+		Mtime:                   models.Time(c.CurrentUser.Mtime),
37
+	}
38
+
39
+	p, err := service.NewUserHealthProfileService().GetUserHealthProfileByUserId(c.CurrentUser.Id)
40
+	if err != nil {
41
+		c.ServeDynamicFailJsonSend(err.Error())
42
+	}
43
+	if p != nil {
44
+
45
+		resp.RealName = p.RealName
46
+		resp.IdCard = p.IdCard
47
+		resp.InpatientRegPhone = p.InpatientRegPhone
37
 	}
48
 	}
38
 
49
 
39
 	c.ServeSuccessJSON(resp)
50
 	c.ServeSuccessJSON(resp)
160
 	resp := models.HealthProfileResp{}
171
 	resp := models.HealthProfileResp{}
161
 	if p != nil {
172
 	if p != nil {
162
 
173
 
163
-		resp.Birthday = p.Birthday
174
+		resp.Birthday = models.Time(p.Birthday)
164
 		resp.BloodType = p.BloodType
175
 		resp.BloodType = p.BloodType
165
-		resp.CreatineTime = p.CreatineTime
176
+		resp.CreatineTime = models.Time(p.CreatineTime)
166
 		resp.Creatinine = p.Creatinine
177
 		resp.Creatinine = p.Creatinine
167
 		resp.CreatinineUnit = p.CreatinineUnit
178
 		resp.CreatinineUnit = p.CreatinineUnit
168
-		resp.Ctime = p.Ctime
179
+		resp.Ctime = models.Time(p.Ctime)
169
 		resp.Gender = p.Gender
180
 		resp.Gender = p.Gender
170
 		resp.Height = p.Height
181
 		resp.Height = p.Height
171
 		resp.Id = p.Id
182
 		resp.Id = p.Id
172
 		resp.IllnessState = p.IllnessState
183
 		resp.IllnessState = p.IllnessState
173
-		resp.Mtime = p.Mtime
184
+		resp.Mtime = models.Time(p.Mtime)
174
 		resp.RenalFunctionStatus = p.RenalFunctionStatus
185
 		resp.RenalFunctionStatus = p.RenalFunctionStatus
175
 		resp.Status = p.Status
186
 		resp.Status = p.Status
176
 		resp.UrineProtein = p.UrineProtein
187
 		resp.UrineProtein = p.UrineProtein
177
-		resp.UrineProteinTime = p.UrineProteinTime
188
+		resp.UrineProteinTime = models.Time(p.UrineProteinTime)
178
 		resp.UrineProteinUnit = p.UrineProteinUnit
189
 		resp.UrineProteinUnit = p.UrineProteinUnit
179
 		resp.UrineProtein24h = p.UrineProtein24h
190
 		resp.UrineProtein24h = p.UrineProtein24h
180
-		resp.UrineProtein24hTime = p.UrineProtein24hTime
191
+		resp.UrineProtein24hTime = models.Time(p.UrineProtein24hTime)
181
 		resp.UrineProtein24hUnit = p.UrineProtein24hUnit
192
 		resp.UrineProtein24hUnit = p.UrineProtein24hUnit
182
 
193
 
183
 		resp.Weight = p.Weight
194
 		resp.Weight = p.Weight

+ 5 - 5
models/dbmodels.go View File

157
 	Creatinine          int       `json:"creatinine" gorm:"type:int(11); NOT NULL; DEFAULT:'0'; COMMENT:'血肌酐'" description:"血肌酐"`
157
 	Creatinine          int       `json:"creatinine" gorm:"type:int(11); NOT NULL; DEFAULT:'0'; COMMENT:'血肌酐'" description:"血肌酐"`
158
 	CreatinineUnit      string    `json:"creatinine_unit" gorm:"type:varchar(32); COMMENT:'肌酐单位(umol/L,mg/dl)'" description:"肌酐单位(umol/L,mg/dl)"`
158
 	CreatinineUnit      string    `json:"creatinine_unit" gorm:"type:varchar(32); COMMENT:'肌酐单位(umol/L,mg/dl)'" description:"肌酐单位(umol/L,mg/dl)"`
159
 	CreatineTime        time.Time `json:"creatine_time" gorm:"type:datetime; COMMENT:'肌酐检测时间'" description:"肌酐检测时间"`
159
 	CreatineTime        time.Time `json:"creatine_time" gorm:"type:datetime; COMMENT:'肌酐检测时间'" description:"肌酐检测时间"`
160
-	UrineProtein24hUnit string    `json:"urine_protein_24h_unit" gorm:"type:varchar(32); COMMENT:'24小时尿蛋白单位(g/24h,mg/24h)'" description:"24小时尿蛋白单位(g/24h,mg/24h)"`
161
-	UrineProtein24h     int       `json:"urine_protein_24h" gorm:"type:int(11); NOT NULL; DEFAULT:'0'; COMMENT:'24小时尿蛋白'" description:"24小时尿蛋白"`
162
-	UrineProtein24hTime time.Time `json:"urine_protein_24h_time" gorm:"type:datetime; COMMENT:'24小时尿蛋白检测时间'" description:"24小时尿蛋白检测时间"`
160
+	UrineProtein24hUnit string    `json:"urine_protein_24h_unit" gorm:"column:urine_protein_24h_unit; type:varchar(32); COMMENT:'24小时尿蛋白单位(g/24h,mg/24h)'" description:"24小时尿蛋白单位(g/24h,mg/24h)"`
161
+	UrineProtein24h     int       `json:"urine_protein_24h" gorm:"column:urine_protein_24h;type:int(11); NOT NULL; DEFAULT:'0'; COMMENT:'24小时尿蛋白'" description:"24小时尿蛋白"`
162
+	UrineProtein24hTime time.Time `json:"urine_protein_24h_time" gorm:"column:urine_protein_24h_time;type:datetime; COMMENT:'24小时尿蛋白检测时间'" description:"24小时尿蛋白检测时间"`
163
 	UrineProtein        int       `json:"urine_protein" gorm:"type:int(11); NOT NULL; DEFAULT:'0'; COMMENT:'尿蛋白'" description:"尿蛋白"`
163
 	UrineProtein        int       `json:"urine_protein" gorm:"type:int(11); NOT NULL; DEFAULT:'0'; COMMENT:'尿蛋白'" description:"尿蛋白"`
164
-	UrineProteinUnit    string    `json:"urine_protein_unit" gorm:"type:varchar(32); COMMENT:'尿蛋白单位(g,mg)'" description:"尿蛋白单位(g,mg)"`
165
-	UrineProteinTime    time.Time `json:"urine_protein_time" gorm:"type:datetime; COMMENT:'尿蛋白检测时间'" description:"尿蛋白检测时间"`
164
+	UrineProteinUnit    string    `json:"urine_protein_unit" gorm:"column:urine_protein_unit; type:varchar(32); COMMENT:'尿蛋白单位(g,mg)'" description:"尿蛋白单位(g,mg)"`
165
+	UrineProteinTime    time.Time `json:"urine_protein_time" gorm:"column:urine_protein_time;type:datetime; COMMENT:'尿蛋白检测时间'" description:"尿蛋白检测时间"`
166
 	Status              int       `json:"status" gorm:"type:int(11); DEFAULT:'1'; COMMENT:'状态(1:有效 0:无效 )'" description:"状态(1:有效 0:无效 )"`
166
 	Status              int       `json:"status" gorm:"type:int(11); DEFAULT:'1'; COMMENT:'状态(1:有效 0:无效 )'" description:"状态(1:有效 0:无效 )"`
167
 	Ctime               time.Time `json:"ctime" gorm:"type:datetime; DEFAULT: CURRENT_TIMESTAMP; COMMENT:'创建时间'" description:"创建时间"`
167
 	Ctime               time.Time `json:"ctime" gorm:"type:datetime; DEFAULT: CURRENT_TIMESTAMP; COMMENT:'创建时间'" description:"创建时间"`
168
 	Mtime               time.Time `json:"mtime" gorm:"type:datetime; DEFAULT: CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP; COMMENT:'更新时间 '" description:"更新时间 "`
168
 	Mtime               time.Time `json:"mtime" gorm:"type:datetime; DEFAULT: CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP; COMMENT:'更新时间 '" description:"更新时间 "`

+ 97 - 37
models/httpmodels.go View File

2
 
2
 
3
 import "time"
3
 import "time"
4
 
4
 
5
+type Time time.Time
6
+
7
+const (
8
+	timeFormart = "2006-01-02 15:04:05"
9
+)
10
+
11
+func (t *Time) UnmarshalJSON(data []byte) (err error) {
12
+	now, err := time.ParseInLocation(`"`+timeFormart+`"`, string(data), time.Local)
13
+	*t = Time(now)
14
+	return
15
+}
16
+
17
+func (t Time) MarshalJSON() ([]byte, error) {
18
+	b := make([]byte, 0, len(timeFormart)+2)
19
+	b = append(b, '"')
20
+	b = time.Time(t).AppendFormat(b, timeFormart)
21
+	b = append(b, '"')
22
+	return b, nil
23
+}
24
+func (t Time) String() string {
25
+	return time.Time(t).Format(timeFormart)
26
+}
27
+
5
 type WxXcxLoginReq struct {
28
 type WxXcxLoginReq struct {
6
 	Code string `json:"code"`
29
 	Code string `json:"code"`
7
 }
30
 }
40
 	IdCard            string `json:"id_card" description:"身份证号"`
63
 	IdCard            string `json:"id_card" description:"身份证号"`
41
 	InpatientRegPhone string `json:"inpatient_reg_phone" description:"住院登记手机号"`
64
 	InpatientRegPhone string `json:"inpatient_reg_phone" description:"住院登记手机号"`
42
 
65
 
43
-	Ctime time.Time `json:"ctime" description:"创建时间"`
44
-	Mtime time.Time `json:"mtime" description:"更新时间 "`
66
+	Ctime Time `json:"ctime" description:"创建时间"`
67
+	Mtime Time `json:"mtime" description:"更新时间 "`
45
 }
68
 }
46
 
69
 
47
 type SaveHealthProfileReq struct {
70
 type SaveHealthProfileReq struct {
48
-	Gender              int       `json:"gender" description:"性别(0:未知 1:男 2:女)"`
49
-	Height              int       `json:"height" description:"身高"`
50
-	Weight              int       `json:"weight" description:"体重"`
51
-	BloodType           string    `json:"blood_type" description:"血型"`
52
-	Birthday            time.Time `json:"birthday" description:"生日"`
53
-	IllnessState        string    `json:"illness_state" description:"病情"`
54
-	RenalFunctionStatus int       `json:"renal_function_status" description:"肾功能情况(0:未透析,1: 血液透析,2:腹膜透析,3:肾脏移植)"`
55
-	Creatinine          int       `json:"creatinine" description:"血肌酐"`
56
-	CreatinineUnit      string    `json:"creatinine_unit" description:"肌酐单位(umol/L,mg/dl)"`
57
-	CreatineTime        time.Time `json:"creatine_time" description:"肌酐检测时间"`
58
-	UrineProtein24hUnit string    `json:"urine_protein_24h_unit" description:"24小时尿蛋白单位(g/24h,mg/24h)"`
59
-	UrineProtein24h     int       `json:"urine_protein_24h" description:"24小时尿蛋白"`
60
-	UrineProtein24hTime time.Time `json:"urine_protein_24h_time" description:"24小时尿蛋白检测时间"`
61
-	UrineProtein        int       `json:"urine_protein" description:"尿蛋白"`
62
-	UrineProteinUnit    string    `json:"urine_protein_unit" description:"尿蛋白单位(g,mg)"`
63
-	UrineProteinTime    time.Time `json:"urine_protein_time" description:"尿蛋白检测时间"`
71
+	Gender              int    `json:"gender" description:"性别(0:未知 1:男 2:女)"`
72
+	Height              int    `json:"height" description:"身高"`
73
+	Weight              int    `json:"weight" description:"体重"`
74
+	BloodType           string `json:"blood_type" description:"血型"`
75
+	Birthday            Time   `json:"birthday" description:"生日"`
76
+	IllnessState        string `json:"illness_state" description:"病情"`
77
+	RenalFunctionStatus int    `json:"renal_function_status" description:"肾功能情况(0:未透析,1: 血液透析,2:腹膜透析,3:肾脏移植)"`
78
+	Creatinine          int    `json:"creatinine" description:"血肌酐"`
79
+	CreatinineUnit      string `json:"creatinine_unit" description:"肌酐单位(umol/L,mg/dl)"`
80
+	CreatineTime        Time   `json:"creatine_time" description:"肌酐检测时间"`
81
+	UrineProtein24hUnit string `json:"urine_protein_24h_unit" description:"24小时尿蛋白单位(g/24h,mg/24h)"`
82
+	UrineProtein24h     int    `json:"urine_protein_24h" description:"24小时尿蛋白"`
83
+	UrineProtein24hTime Time   `json:"urine_protein_24h_time" description:"24小时尿蛋白检测时间"`
84
+	UrineProtein        int    `json:"urine_protein" description:"尿蛋白"`
85
+	UrineProteinUnit    string `json:"urine_protein_unit" description:"尿蛋白单位(g,mg)"`
86
+	UrineProteinTime    Time   `json:"urine_protein_time" description:"尿蛋白检测时间"`
64
 }
87
 }
65
 
88
 
66
 type HealthProfileResp struct {
89
 type HealthProfileResp struct {
69
 	//RealName            string    `json:"real_name" description:"真实姓名"`
92
 	//RealName            string    `json:"real_name" description:"真实姓名"`
70
 	//IdCard              string    `json:"id_card" description:"身份证号"`
93
 	//IdCard              string    `json:"id_card" description:"身份证号"`
71
 	//InpatientRegPhone   string    `json:"inpatient_reg_phone" description:"住院登记手机号"`
94
 	//InpatientRegPhone   string    `json:"inpatient_reg_phone" description:"住院登记手机号"`
72
-	Gender              int       `json:"gender" description:"性别(0:未知 1:男 2:女)"`
73
-	Height              int       `json:"height" description:"身高"`
74
-	Weight              int       `json:"weight" description:"体重"`
75
-	BloodType           string    `json:"blood_type" description:"血型"`
76
-	Birthday            time.Time `json:"birthday" description:"生日"`
77
-	IllnessState        string    `json:"illness_state" description:"病情"`
78
-	RenalFunctionStatus int       `json:"renal_function_status" description:"肾功能情况(0:未透析,1: 血液透析,2:腹膜透析,3:肾脏移植)"`
79
-	Creatinine          int       `json:"creatinine" description:"血肌酐"`
80
-	CreatinineUnit      string    `json:"creatinine_unit" description:"肌酐单位(umol/L,mg/dl)"`
81
-	CreatineTime        time.Time `json:"creatine_time" description:"肌酐检测时间"`
82
-	UrineProtein24hUnit string    `json:"urine_protein_24h_unit" description:"24小时尿蛋白单位(g/24h,mg/24h)"`
83
-	UrineProtein24h     int       `json:"urine_protein_24h" description:"24小时尿蛋白"`
84
-	UrineProtein24hTime time.Time `json:"urine_protein_24h_time" description:"24小时尿蛋白检测时间"`
85
-	UrineProtein        int       `json:"urine_protein" description:"尿蛋白"`
86
-	UrineProteinUnit    string    `json:"urine_protein_unit" description:"尿蛋白单位(g,mg)"`
87
-	UrineProteinTime    time.Time `json:"urine_protein_time" description:"尿蛋白检测时间"`
88
-	Status              int       `json:"status" description:"状态(1:有效 0:无效 )"`
89
-	Ctime               time.Time `json:"ctime" description:"创建时间"`
90
-	Mtime               time.Time `json:"mtime" description:"更新时间 "`
95
+	Gender              int    `json:"gender" description:"性别(0:未知 1:男 2:女)"`
96
+	Height              int    `json:"height" description:"身高"`
97
+	Weight              int    `json:"weight" description:"体重"`
98
+	BloodType           string `json:"blood_type" description:"血型"`
99
+	Birthday            Time   `json:"birthday" description:"生日"`
100
+	IllnessState        string `json:"illness_state" description:"病情"`
101
+	RenalFunctionStatus int    `json:"renal_function_status" description:"肾功能情况(0:未透析,1: 血液透析,2:腹膜透析,3:肾脏移植)"`
102
+	Creatinine          int    `json:"creatinine" description:"血肌酐"`
103
+	CreatinineUnit      string `json:"creatinine_unit" description:"肌酐单位(umol/L,mg/dl)"`
104
+	CreatineTime        Time   `json:"creatine_time" description:"肌酐检测时间"`
105
+	UrineProtein24hUnit string `json:"urine_protein_24h_unit" description:"24小时尿蛋白单位(g/24h,mg/24h)"`
106
+	UrineProtein24h     int    `json:"urine_protein_24h" description:"24小时尿蛋白"`
107
+	UrineProtein24hTime Time   `json:"urine_protein_24h_time" description:"24小时尿蛋白检测时间"`
108
+	UrineProtein        int    `json:"urine_protein" description:"尿蛋白"`
109
+	UrineProteinUnit    string `json:"urine_protein_unit" description:"尿蛋白单位(g,mg)"`
110
+	UrineProteinTime    Time   `json:"urine_protein_time" description:"尿蛋白检测时间"`
111
+	Status              int    `json:"status" description:"状态(1:有效 0:无效 )"`
112
+	Ctime               Time   `json:"ctime" description:"创建时间"`
113
+	Mtime               Time   `json:"mtime" description:"更新时间 "`
91
 }
114
 }
92
 
115
 
93
 type DicResp struct {
116
 type DicResp struct {
95
 	Name  string `json:"name" description:"字典名称"`
118
 	Name  string `json:"name" description:"字典名称"`
96
 	Value int    `json:"value" description:"字典值"`
119
 	Value int    `json:"value" description:"字典值"`
97
 }
120
 }
121
+
122
+type OpenEmqMsgReqV0 struct {
123
+	UserName   string `json:"username" description:"用户名"`
124
+	Topic      string `json:"topic" description:"主题"`
125
+	PeerHost   string `json:"peerhost" description:"主机"`
126
+	Payload    string `json:"payload" description:"负载"`
127
+	Id         string `json:"id"`
128
+	ProductKey string `json:"productKey" description:"产品key"`
129
+	DeviceName string `json:"deviceName" description:"设备名称"`
130
+	Acc        int    `json:"acc"`
131
+	Type       int    `json:"type"`
132
+	Count      int    `json:"count"`
133
+	DataType   int    `json:"datatype"`
134
+	VerifyCode string `json:"verifycode"`
135
+	Data       string `json:"data"`
136
+	Origin     string `json:"origin"`
137
+	Len        int    `json:"len"`
138
+}
139
+
140
+type OpenEmqPayloadReqVO struct {
141
+	ProductKey string `json:"productKey"` // 产品密钥
142
+	DeviceName string `json:"deviceName"` // 设备名称
143
+	Acc        int    `json:"acc"`        // 准确度
144
+	Type       int    `json:"type"`       // 类型
145
+	Count      int    `json:"count"`      // 计数
146
+	Datatype   int    `json:"datatype"`   // 数据类型
147
+	Verifycode int    `json:"verifycode"` // 验证码
148
+	Data       string `json:"data"`       // 数据
149
+	Origin     string `json:"origin"`     // 来源
150
+	Len        int    `json:"len"`        // 长度
151
+}
152
+type CheckItemScopeVO struct {
153
+	Index int     `json:"index"`
154
+	Type  int     `json:"type"`
155
+	Value string  `json:"value"`
156
+	Num   float64 `json:"num"`
157
+}

+ 6 - 2
service/checkitemservice.go View File

16
 	}
16
 	}
17
 }
17
 }
18
 
18
 
19
-func (s *CheckItemService) GetCheckItems(lang string) ([]*models.CheckItem, error) {
19
+func (s *CheckItemService) GetCheckItems(lang, t string) ([]*models.CheckItem, error) {
20
 	var items []*models.CheckItem
20
 	var items []*models.CheckItem
21
-	err := s.rdb.Where("language = ? and delete_flag = ?", lang, 0).Order("check_item_number").Find(&items).Error
21
+	db := s.rdb.Where("language = ? and delete_flag = ?", lang, 0)
22
+	if t != "" {
23
+		db = db.Where("check_type = ?", t)
24
+	}
25
+	err := db.Order("check_item_number").Find(&items).Error
22
 	return items, err
26
 	return items, err
23
 }
27
 }

+ 4 - 0
service/checkrecordservice.go View File

21
 func (s *CheckRecordService) GetCheckRecordList(pageNum, pageSize int, search string) ([]*models.CheckRecord, int, error) {
21
 func (s *CheckRecordService) GetCheckRecordList(pageNum, pageSize int, search string) ([]*models.CheckRecord, int, error) {
22
 	return nil, 0, nil
22
 	return nil, 0, nil
23
 }
23
 }
24
+
25
+func (s *CheckRecordService) CreateCheckRecord(checkRecord *models.CheckRecord) error {
26
+	return s.wdb.Model(checkRecord).Create(checkRecord).Error
27
+}

+ 30 - 8
service/deviceservice.go View File

20
 }
20
 }
21
 
21
 
22
 func (s *DeviceService) GetDeviceInfo(id int) (*models.Device, error) {
22
 func (s *DeviceService) GetDeviceInfo(id int) (*models.Device, error) {
23
-	var device models.Device
24
-	err := s.rdb.Model(&device).First(&device, id).Error
25
-	if err != nil {
26
-		return nil, err
23
+	var device *models.Device
24
+	err := s.rdb.Model(device).First(device, id).Error
25
+	if err == nil || err == gorm.ErrRecordNotFound {
26
+		err = nil
27
 	}
27
 	}
28
-	return &device, nil
28
+	return device, err
29
+}
30
+
31
+func (s *DeviceService) GetDeviceByNo(serialno string) (*models.Device, error) {
32
+	var device *models.Device
33
+	err := s.rdb.Model(device).Where("serialno = ?", serialno).First(device).Error
34
+	if err == nil || err == gorm.ErrRecordNotFound {
35
+		err = nil
36
+	}
37
+	return device, err
29
 }
38
 }
30
 
39
 
31
 func (s *DeviceService) GetMyDevices(userId uint64) ([]*models.Device, error) {
40
 func (s *DeviceService) GetMyDevices(userId uint64) ([]*models.Device, error) {
66
 		if dr.DeleteFlag == 0 {
75
 		if dr.DeleteFlag == 0 {
67
 			return errors.New("该设备已绑定其他用户")
76
 			return errors.New("该设备已绑定其他用户")
68
 		} else { //其他用户解绑了设备
77
 		} else { //其他用户解绑了设备
69
-			return s.wdb.Model(dr).Update("delete_flag", 0, "user_id", userId).Error
78
+			return s.wdb.Model(dr).Where("id=?", dr.Id).Update("delete_flag", 0, "user_id", userId).Error
70
 		}
79
 		}
71
 	} else if dr.DeleteFlag == 1 { //直接解绑了该设备 重新绑定
80
 	} else if dr.DeleteFlag == 1 { //直接解绑了该设备 重新绑定
72
 
81
 
73
-		return s.wdb.Model(dr).Update("delete_flag", 0).Error
82
+		return s.wdb.Model(dr).Where("id=?", dr.Id).Update("delete_flag", 0).Error
74
 
83
 
75
 	} else {
84
 	} else {
76
 		return errors.New("设备已绑定")
85
 		return errors.New("设备已绑定")
93
 	if err != nil {
102
 	if err != nil {
94
 		return err
103
 		return err
95
 	}
104
 	}
96
-	return s.wdb.Model(dr).Update("delete_flag", 1).Error
105
+	return s.wdb.Model(dr).Where("id=?", dr.Id).Update("delete_flag", 1).Error
106
+}
107
+
108
+func (s *DeviceService) CreateDeviceMessageLog(log models.DeviceMessageLog) error {
109
+	return s.wdb.Model(&log).Create(&log).Error
110
+}
111
+
112
+func (s *DeviceService) GetDeviceRelateByDeviceId(deviceId uint64) (*models.DeviceRelate, error) {
113
+	dr := &models.DeviceRelate{}
114
+	err := s.rdb.Model(dr).Where("device_id = ?", deviceId).First(dr).Error
115
+	if err != nil && err != gorm.ErrRecordNotFound {
116
+		return nil, err
117
+	}
118
+	return dr, nil
97
 }
119
 }

+ 21 - 6
service/userhealthprofileservice.go View File

2
 
2
 
3
 import (
3
 import (
4
 	"sws_xcx/models"
4
 	"sws_xcx/models"
5
+	"time"
5
 
6
 
6
 	"github.com/jinzhu/gorm"
7
 	"github.com/jinzhu/gorm"
7
 )
8
 )
23
 func (s *HealthProfileService) GetUserHealthProfileByUserId(userId uint64) (*models.UserHealthProfile, error) {
24
 func (s *HealthProfileService) GetUserHealthProfileByUserId(userId uint64) (*models.UserHealthProfile, error) {
24
 	p := &models.UserHealthProfile{}
25
 	p := &models.UserHealthProfile{}
25
 	err := s.rdb.Find(p, "user_id = ?", userId).Error
26
 	err := s.rdb.Find(p, "user_id = ?", userId).Error
27
+	if err == nil || err == gorm.ErrRecordNotFound {
28
+		err = nil
29
+	}
26
 	return p, err
30
 	return p, err
27
 }
31
 }
28
 
32
 
29
 func (s *HealthProfileService) Get(id uint64) (models.UserHealthProfile, error) {
33
 func (s *HealthProfileService) Get(id uint64) (models.UserHealthProfile, error) {
30
 	p := &models.UserHealthProfile{}
34
 	p := &models.UserHealthProfile{}
31
 	err := s.rdb.First(p, id).Error
35
 	err := s.rdb.First(p, id).Error
36
+	if err == nil || err == gorm.ErrRecordNotFound {
37
+		err = nil
38
+	}
32
 	return *p, err
39
 	return *p, err
33
 }
40
 }
34
 
41
 
45
 	p.UserId = userId
52
 	p.UserId = userId
46
 	p.RealName = realName
53
 	p.RealName = realName
47
 	p.InpatientRegPhone = regPhone
54
 	p.InpatientRegPhone = regPhone
48
-	return s.wdb.Save(p).Error
55
+	p.IdCard = idCard
56
+
57
+	if p.Id == 0 {
58
+		return s.wdb.Create(p).Error
59
+	}
60
+	return s.wdb.Model(p).Update(p).Error
49
 }
61
 }
50
 
62
 
51
 func (s *HealthProfileService) SaveHealthProfile(userId uint64, req models.SaveHealthProfileReq) error {
63
 func (s *HealthProfileService) SaveHealthProfile(userId uint64, req models.SaveHealthProfileReq) error {
59
 	}
71
 	}
60
 
72
 
61
 	p.UserId = userId
73
 	p.UserId = userId
62
-	p.Birthday = req.Birthday
74
+	p.Birthday = time.Time(req.Birthday)
63
 	p.BloodType = req.BloodType
75
 	p.BloodType = req.BloodType
64
-	p.CreatineTime = req.CreatineTime
76
+	p.CreatineTime = time.Time(req.CreatineTime)
65
 	p.Creatinine = req.Creatinine
77
 	p.Creatinine = req.Creatinine
66
 	p.CreatinineUnit = req.CreatinineUnit
78
 	p.CreatinineUnit = req.CreatinineUnit
67
 	p.Gender = req.Gender
79
 	p.Gender = req.Gender
70
 	p.RenalFunctionStatus = req.RenalFunctionStatus
82
 	p.RenalFunctionStatus = req.RenalFunctionStatus
71
 	p.UrineProtein = req.UrineProtein
83
 	p.UrineProtein = req.UrineProtein
72
 	p.UrineProtein24h = req.UrineProtein24h
84
 	p.UrineProtein24h = req.UrineProtein24h
73
-	p.UrineProtein24hTime = req.UrineProtein24hTime
85
+	p.UrineProtein24hTime = time.Time(req.UrineProtein24hTime)
74
 	p.UrineProtein24hUnit = req.UrineProtein24hUnit
86
 	p.UrineProtein24hUnit = req.UrineProtein24hUnit
75
-	p.UrineProteinTime = req.UrineProteinTime
87
+	p.UrineProteinTime = time.Time(req.UrineProteinTime)
76
 	p.UrineProteinUnit = req.UrineProteinUnit
88
 	p.UrineProteinUnit = req.UrineProteinUnit
77
 	p.Weight = req.Weight
89
 	p.Weight = req.Weight
78
 
90
 
79
-	return s.wdb.Save(p).Error
91
+	if p.Id == 0 {
92
+		return s.wdb.Create(p).Error
93
+	}
94
+	return s.wdb.Model(p).Update(p).Error
80
 }
95
 }
81
 
96
 
82
 func (s *HealthProfileService) Save(p *models.UserHealthProfile) error {
97
 func (s *HealthProfileService) Save(p *models.UserHealthProfile) error {

+ 3 - 1
service/userservice.go View File

2
 
2
 
3
 import (
3
 import (
4
 	"sws_xcx/models"
4
 	"sws_xcx/models"
5
+	"time"
5
 
6
 
6
 	"github.com/jinzhu/gorm"
7
 	"github.com/jinzhu/gorm"
7
 )
8
 )
40
 
41
 
41
 func (s *XcxUserService) UpdateUser(user *models.XcxUser) error {
42
 func (s *XcxUserService) UpdateUser(user *models.XcxUser) error {
42
 
43
 
43
-	db := writeDb.Where("id = ?", user.Id).Update(user)
44
+	user.Mtime = time.Now()
45
+	db := writeDb.Model(user).Update(user)
44
 
46
 
45
 	return db.Error
47
 	return db.Error
46
 
48
 

+ 181 - 0
tests/default_test.go View File

1
 package test
1
 package test
2
 
2
 
3
 import (
3
 import (
4
+	"encoding/json"
5
+	"errors"
6
+	"fmt"
4
 	"path/filepath"
7
 	"path/filepath"
5
 	"runtime"
8
 	"runtime"
9
+	"strconv"
10
+	"strings"
11
+	"sws_xcx/models"
6
 	_ "sws_xcx/routers"
12
 	_ "sws_xcx/routers"
7
 	"sws_xcx/service"
13
 	"sws_xcx/service"
14
+	"sws_xcx/utils"
8
 	"testing"
15
 	"testing"
9
 
16
 
10
 	//beego "github.com/beego/beego/v2/server/web"
17
 	//beego "github.com/beego/beego/v2/server/web"
11
 	//"github.com/beego/beego/v2/core/logs"
18
 	//"github.com/beego/beego/v2/core/logs"
12
 	"github.com/astaxie/beego"
19
 	"github.com/astaxie/beego"
20
+	"github.com/jinzhu/gorm"
13
 	"github.com/medivhzhan/weapp/v3/auth"
21
 	"github.com/medivhzhan/weapp/v3/auth"
14
 	// "github.com/smartystreets/goconvey/convey"
22
 	// "github.com/smartystreets/goconvey/convey"
15
 )
23
 )
36
 	//                 So(w.Body.Len(), ShouldBeGreaterThan, 0)
44
 	//                 So(w.Body.Len(), ShouldBeGreaterThan, 0)
37
 	//         })
45
 	//         })
38
 	// })
46
 	// })
47
+	t.Log("TestGet")
39
 }
48
 }
40
 
49
 
41
 func TestWxappSessionCode(t *testing.T) {
50
 func TestWxappSessionCode(t *testing.T) {
57
 	t.Logf("resp: %+v", *resp)
66
 	t.Logf("resp: %+v", *resp)
58
 
67
 
59
 }
68
 }
69
+func TestJson(t *testing.T) {
70
+	req := `{"id":1,"nick_name":"zhangsan","ctime":"2019-11-20 16:49:08"}`
71
+	u := models.UserInfoResp{}
72
+	err := json.Unmarshal([]byte(req), &u)
73
+	if err != nil {
74
+		t.Error(err)
75
+	}
76
+	t.Logf("u:%+v", u)
77
+
78
+}
79
+
80
+func TestHandleMsg(t *testing.T) {
81
+
82
+	req := []byte(`{"publish_received_at":1722839054218,"pub_props":{"User-Property":{}},"peerhost":"124.133.43.122","qos":0,"topic":"/CH-U100/09012201100010104/user/checkresult","clientid":"42915152F3F1","payload":"{\"productKey\":\"CH-U100\",\"deviceName\":\"09012201100010104\",\"acc\":12378,\"type\":0,\"count\":14,\"datatype\":0,\"verifycode\":212,\"data\":\"0,0,0,2,2,0,0,0,0,0,4,3,0,1\",\"origin\":\"4243305A0000010E0000000202000000000004030001D4\",\"len\":23}","username":"09012201100010104","event":"message.publish","metadata":{"rule_id":"checkresult"},"timestamp":1722839054218,"node":"emqx@127.0.0.1","id":"00061EE9B9D4F6AFF445000924860003","flags":{"retain":false,"dup":false}}`)
83
+
84
+	const EventType string = "emq"
85
+	msg := &models.OpenEmqMsgReqV0{}
86
+	err := json.Unmarshal(req, msg)
87
+	if err != nil {
88
+		t.Error(err)
89
+	}
90
+
91
+	ds := service.NewDeviceService()
92
+
93
+	device, err := ds.GetDeviceByNo(msg.DeviceName)
94
+	if err != nil && err != gorm.ErrRecordNotFound {
95
+		utils.ErrorLog("GetDeviceByNo %v:", err)
96
+		t.Error(err)
97
+	}
98
+	if device == nil {
99
+		utils.WarningLog("非本系统设备,数据废弃")
100
+		return
101
+	}
102
+	err = ds.CreateDeviceMessageLog(models.DeviceMessageLog{
103
+		DeviceName: msg.DeviceName,
104
+		Content:    string(req),
105
+		EventType:  EventType,
106
+		MessageId:  msg.Id,
107
+		Topic:      msg.Topic,
108
+	})
109
+
110
+	if err != nil {
111
+		utils.ErrorLog("CreateDeviceMessageLog %v:", err)
112
+		t.Error(err)
113
+	}
114
+	payload := &models.OpenEmqPayloadReqVO{}
115
+	err = json.Unmarshal([]byte(msg.Payload), payload)
116
+	if err != nil {
117
+		utils.ErrorLog("json.Unmarshal %v:", err)
118
+		t.Error(err)
119
+	}
120
+	if payload.Len != 23 {
121
+		utils.WarningLog("非标准数据,数据废弃:%v", msg.Payload)
122
+	}
123
+
124
+	dr, err := ds.GetDeviceRelateByDeviceId(device.Id)
125
+	if err != nil {
126
+		utils.ErrorLog("GetDeviceRelateByDeviceId %v:", err)
127
+		t.Error(err)
128
+	}
129
+
130
+	checkRecord := &models.CheckRecord{
131
+
132
+		Acc:          payload.Acc,
133
+		PutSources:   EventType,
134
+		MessageId:    msg.Id,
135
+		DeviceId:     device.Id,
136
+		UserId:       dr.UserId,
137
+		DeviceStatus: device.Status,
138
+	}
139
+
140
+	if dr.UserId > 0 {
141
+
142
+		hp, err := service.NewUserHealthProfileService().GetUserHealthProfileByUserId(dr.UserId)
143
+		if err != nil {
144
+			utils.ErrorLog("GetUserHealthProfileByUserId %v:", err)
145
+			t.Error(err)
146
+		}
147
+		checkRecord.UserHealthProfileId = int64(hp.Id)
148
+	}
149
+	crs := service.NewCheckRecordService()
150
+	err = crs.CreateCheckRecord(checkRecord)
151
+	if err != nil {
152
+		utils.ErrorLog("CreateCheckRecord %v:", err)
153
+		t.Error(err)
154
+	}
155
+	t.Log(checkRecord.Id)
156
+	t.Log(payload.Data)
157
+
158
+	da := strings.Split(payload.Data, ",")
159
+	count := payload.Count
160
+	if len(da) != count {
161
+		t.Logf("数据长度不匹配,截取存储 %+v", payload)
162
+		if len(da) < count {
163
+			count = len(da)
164
+		}
165
+	}
166
+
167
+	checkItems, err := service.NewCheckItemService().GetCheckItems("cn", device.DeviceType)
168
+
169
+	if err != nil {
170
+		utils.ErrorLog("GetCheckItems %v:", err)
171
+		t.Error(err)
172
+	}
173
+
174
+	if len(checkItems) < count {
175
+		t.Error("接收的数据于检查项目配置不一致")
176
+	}
177
+
178
+	alerts := make([]string, 0)
179
+	for i := 0; i < count; i++ {
180
+		vi, _ := strconv.Atoi(da[i])
181
+		err = createCheckRecordItem(checkItems, checkRecord.Id, alerts, vi, i+1)
182
+		if err != nil {
183
+			utils.ErrorLog("createCheckRecordItem valule:%v, err:%v:", vi, err)
184
+		}
185
+	}
186
+
187
+	if len(alerts) > 0 {
188
+		//save alerts
189
+	}
190
+
191
+}
192
+
193
+func createCheckRecordItem(items []*models.CheckItem, crId int64, alerts []string, vi, n int) error {
194
+	item := findItem(items, n)
195
+	if item == nil {
196
+		return fmt.Errorf("检测项目不匹配,需要检查基础数据!序号:%v", n)
197
+	}
198
+
199
+	cri := models.CheckRecordItem{
200
+		CheckId:         crId,
201
+		CheckItemId:     n,
202
+		CheckValueIndex: vi,
203
+	}
204
+
205
+	scopes := []*models.CheckItemScopeVO{}
206
+	err := json.Unmarshal([]byte(item.ScopeList), &scopes)
207
+	if err != nil {
208
+		return fmt.Errorf("检测项目数值范围定义不正确 err:%v", err)
209
+	}
210
+	scope := findScope(scopes, vi)
211
+	if scope == nil {
212
+		return errors.New("检测结果不匹配")
213
+	}
214
+
215
+	cri.CheckValue = scope.Value
216
+	if scope.Type == 1 {
217
+		alerts = append(alerts, strconv.Itoa(n))
218
+	}
219
+	//create cri
220
+	return nil
221
+}
222
+
223
+func findScope(scopes []*models.CheckItemScopeVO, vi int) *models.CheckItemScopeVO {
224
+	for _, scope := range scopes {
225
+		if scope.Index == vi {
226
+			return scope
227
+		}
228
+	}
229
+	return nil
230
+}
231
+
232
+func findItem(items []*models.CheckItem, itemNumber int) *models.CheckItem {
233
+	//循环items
234
+	for _, item := range items {
235
+		if item.CheckItemNumber == itemNumber {
236
+			return item
237
+		}
238
+	}
239
+	return nil
240
+}

+ 44 - 0
tests/gendb_test.go View File

1
 package test
1
 package test
2
 
2
 
3
 import (
3
 import (
4
+	"sws_xcx/models"
4
 	"sws_xcx/service"
5
 	"sws_xcx/service"
5
 	"testing"
6
 	"testing"
7
+
8
+	"github.com/jinzhu/gorm"
6
 )
9
 )
7
 
10
 
8
 func init() {
11
 func init() {
16
 		t.Error(err)
19
 		t.Error(err)
17
 	}
20
 	}
18
 }
21
 }
22
+
23
+func TestUpdate(t *testing.T) {
24
+
25
+	s := service.NewXcxUserService()
26
+
27
+	u := &models.XcxUser{Id: 1, UnionId: "123"}
28
+
29
+	err := s.UpdateUser(u)
30
+	if err != nil {
31
+		t.Error(err)
32
+	}
33
+
34
+}
35
+
36
+func TestDBGet(t *testing.T) {
37
+	wdb := service.WriteDB()
38
+	p := &models.UserHealthProfile{}
39
+	err := wdb.Model(p).First(p, "user_id = ?", 2).Error
40
+	if err != nil && err != gorm.ErrRecordNotFound {
41
+		t.Error(err)
42
+	}
43
+
44
+	t.Log(p)
45
+}
46
+
47
+func TestHealthProfileService_SavePatientInfo(t *testing.T) {
48
+
49
+	userId := uint64(1)
50
+	realName := "John Doe"
51
+	idCard := "1234567890"
52
+	regPhone := "123-456-7890"
53
+
54
+	s := service.NewUserHealthProfileService()
55
+
56
+	// 调用待测试的函数
57
+	err := s.SavePatientInfo(userId, realName, idCard, regPhone)
58
+
59
+	if err != nil {
60
+		t.Error(err)
61
+	}
62
+}