xcx_mobile_api_service.go 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. package service
  2. import (
  3. "Xcx_New/models"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "encoding/base64"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "github.com/jinzhu/gorm"
  11. "github.com/shopspring/decimal"
  12. "net/http"
  13. "time"
  14. )
  15. // 根据患者id获取该患者当前剩余的押金
  16. func GetUserMoney(id, orgid int64) decimal.Decimal {
  17. tmp := models.Deposit{}
  18. err := XTReadDB().Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&tmp).Error
  19. if err != nil {
  20. return decimal.NewFromFloat(0)
  21. }
  22. return tmp.Deposit
  23. }
  24. func GetXcxMobileInformation(mobile string) (*models.XcxAdminUserRole, error) {
  25. role := models.XcxAdminUserRole{}
  26. err := UserReadDB().Model(&role).Where("mobile = ? and status = 1", mobile).Find(&role).Error
  27. if err == gorm.ErrRecordNotFound {
  28. return nil, err
  29. }
  30. if err != nil {
  31. return nil, err
  32. }
  33. return &role, nil
  34. }
  35. func GetXcxOpneidInformation(openid string) (*models.XcxAdminUserRole, error) {
  36. role := models.XcxAdminUserRole{}
  37. err := UserReadDB().Model(&role).Where("openid = ? and status = 1", openid).Find(&role).Error
  38. if err == gorm.ErrRecordNotFound {
  39. return nil, err
  40. }
  41. if err != nil {
  42. return nil, err
  43. }
  44. return &role, nil
  45. }
  46. func CreateXcxAdminUser(role models.XcxAdminUserRole) error {
  47. err := UserReadDB().Create(&role).Error
  48. return err
  49. }
  50. func GetLoginInfor(mobile string) (models.XcxAdminUserRole, error) {
  51. role := models.XcxAdminUserRole{}
  52. err := UserReadDB().Model(&role).Where("mobile = ? and status = 1", mobile).Find(&role).Error
  53. return role, err
  54. }
  55. func GetMobilePatientInfo(mobile string) (models.XcxPatients, error) {
  56. patients := models.XcxPatients{}
  57. err := XTReadDB().Model(&patients).Where("(phone = ? or home_telephone = ? ) and status = 1", mobile, mobile).Find(&patients).Error
  58. return patients, err
  59. }
  60. func GetPatientListByPatientId(id int64) (models.XcxPatients, error) {
  61. patients := models.XcxPatients{}
  62. err := XTReadDB().Model(&patients).Where("id = ? and status = 1", id).Find(&patients).Error
  63. return patients, err
  64. }
  65. func DecryptData(app_id, session_key, iv, encrypted_data string) (map[string]interface{}, error) {
  66. aesKey, err := base64.StdEncoding.DecodeString(session_key)
  67. if err != nil {
  68. return nil, err
  69. }
  70. ivKey, err := base64.StdEncoding.DecodeString(iv)
  71. if err != nil {
  72. return nil, err
  73. }
  74. decodeData, err := base64.StdEncoding.DecodeString(encrypted_data)
  75. if err != nil {
  76. return nil, err
  77. }
  78. dataBytes, err := AesDecrypt(decodeData, aesKey, ivKey)
  79. if err != nil {
  80. return nil, err
  81. }
  82. var result map[string]interface{}
  83. err = json.Unmarshal(dataBytes, &result)
  84. watermark := result["watermark"].(map[string]interface{})
  85. if watermark["appid"] != app_id {
  86. return nil, errors.New("Invalid appid data!")
  87. }
  88. return result, err
  89. }
  90. func AesDecrypt(crypted, key, iv []byte) ([]byte, error) {
  91. block, err := aes.NewCipher(key)
  92. fmt.Println("w2222", err)
  93. if err != nil {
  94. return nil, err
  95. }
  96. blockMode := cipher.NewCBCDecrypter(block, iv)
  97. origData := make([]byte, len(crypted))
  98. blockMode.CryptBlocks(origData, crypted)
  99. // 去除填充
  100. length := len(origData)
  101. unp := int(origData[length-1])
  102. return origData[:(length - unp)], nil
  103. }
  104. func GetFirstDateOfWeek() (weekMonday string) {
  105. now := time.Now()
  106. offset := int(time.Monday - now.Weekday())
  107. if offset > 0 {
  108. offset = -6
  109. }
  110. weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  111. weekMonday = weekStartDate.Format("2006-01-02")
  112. return
  113. }
  114. func GetWeekDayOfWeek() (weekMonday string) {
  115. now := time.Now()
  116. offset := int(time.Monday - now.Weekday())
  117. if offset > 0 {
  118. offset = -6
  119. }
  120. weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  121. weekMonday = weekStartDate.Format("2006-01-02")
  122. return
  123. }
  124. // 获取本月的起止时间戳
  125. func GetMonth() (int64, int64) {
  126. timeNow := time.Now()
  127. timeToday := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, timeNow.Location()) // 获取当天0点时间 time类型
  128. timeMonthStartUnix1 := timeToday.AddDate(0, 0, -timeToday.Day()+1).Unix() // 获取本月第一天0点 时间戳类型
  129. timeMonthEndUnix1 := timeToday.AddDate(0, 1, -timeToday.Day()+1).Unix() - 1 // 获取下个月第一天/ 本月最后一天24点 时间戳类型
  130. return timeMonthStartUnix1, timeMonthEndUnix1
  131. }
  132. // 押金流水
  133. func GetFlowList(patient_id, orgid, stime, etime int64) (deposit []models.DepositHistory, err error) {
  134. err = XTReadDB().Model(&models.DepositHistory{}).Where("status = 1 and trial_status = 1 and user_org_id = ? and his_patient_id = ? and mtime >= ? and mtime <= ?", orgid, patient_id, stime, etime).Order("mtime desc").Find(&deposit).Error
  135. return
  136. }
  137. // 获取his中的有效患者
  138. func GetHisUserName(orgid, id int64) (hisname models.GetHisName, err error) {
  139. err = XTReadDB().Model(&models.GetHisName{}).Where("status = 1 and user_org_id = ? and id = ?", orgid, id).Find(&hisname).Error
  140. return
  141. }
  142. func GetPatientByIdXcx(patient_id int64) (*models.XcxPatients, error) {
  143. patient := models.XcxPatients{}
  144. err := XTReadDB().Model(&patient).Where("id =? and status = 1", patient_id).Find(&patient).Error
  145. if err == gorm.ErrRecordNotFound {
  146. return nil, err
  147. }
  148. if err != nil {
  149. return nil, err
  150. }
  151. return &patient, nil
  152. }
  153. func GetMobilePatient(mobile string, idcard string) (*models.XcxPatients, error) {
  154. patient := models.XcxPatients{}
  155. err := XTReadDB().Model(&patient).Where("(phone = ? or relative_phone =?) and id_card_no =?", mobile, mobile, idcard).Find(&patient).Error
  156. //err := XTReadDB().Model(&patient).Where("id_card_no = ? and status = 1", mobile).Find(&patient).Error
  157. if err == gorm.ErrRecordNotFound {
  158. return nil, err
  159. }
  160. if err != nil {
  161. return nil, err
  162. }
  163. return &patient, nil
  164. }
  165. func GetPatientByIdcardno(idcard string) (*models.XcxPatients, error) {
  166. patient := models.XcxPatients{}
  167. err := XTReadDB().Model(&patient).Where("id_card_no =?", idcard).Find(&patient).Error
  168. //err := XTReadDB().Model(&patient).Where("id_card_no = ? and status = 1", mobile).Find(&patient).Error
  169. if err == gorm.ErrRecordNotFound {
  170. return nil, err
  171. }
  172. if err != nil {
  173. return nil, err
  174. }
  175. return &patient, nil
  176. }
  177. func GetNamePatient(name string, idcard string) (*models.XcxPatients, error) {
  178. patient := models.XcxPatients{}
  179. err := XTReadDB().Model(&patient).Where("name = ? and id_card_no =?", name, idcard).Find(&patient).Error
  180. //err := XTReadDB().Model(&patient).Where("id_card_no = ? and status = 1", mobile).Find(&patient).Error
  181. if err == gorm.ErrRecordNotFound {
  182. return nil, err
  183. }
  184. if err != nil {
  185. return nil, err
  186. }
  187. return &patient, nil
  188. }
  189. func GetTemplateMode(orgid int64) (models.XcxGobalTemplate, error) {
  190. template := models.XcxGobalTemplate{}
  191. err := XTReadDB().Model(&template).Where("org_id = ? and status = 1", orgid).Find(&template).Error
  192. return template, err
  193. }
  194. func GetMobilePatientOne(mobile string) (*models.XcxAdminUserRole, error) {
  195. patient := models.XcxAdminUserRole{}
  196. err := UserReadDB().Model(&patient).Where("mobile = ? and status = 1", mobile).Find(&patient).Error
  197. if err == gorm.ErrRecordNotFound {
  198. return nil, err
  199. }
  200. if err != nil {
  201. return nil, err
  202. }
  203. return &patient, nil
  204. }
  205. func Dncrypt(rawData, key, iv string) (string, error) {
  206. data, err := base64.StdEncoding.DecodeString(rawData)
  207. key_b, err_1 := base64.StdEncoding.DecodeString(key)
  208. iv_b, _ := base64.StdEncoding.DecodeString(iv)
  209. if err != nil {
  210. return "", err
  211. }
  212. if err_1 != nil {
  213. return "", err_1
  214. }
  215. dnData, err := AesCBCDncrypt(data, key_b, iv_b)
  216. if err != nil {
  217. return "", err
  218. }
  219. return string(dnData), nil
  220. }
  221. // 解密
  222. func AesCBCDncrypt(encryptData, key, iv []byte) ([]byte, error) {
  223. block, err := aes.NewCipher(key)
  224. if err != nil {
  225. panic(err)
  226. }
  227. blockSize := block.BlockSize()
  228. if len(encryptData) < blockSize {
  229. panic("ciphertext too short")
  230. }
  231. if len(encryptData)%blockSize != 0 {
  232. panic("ciphertext is not a multiple of the block size")
  233. }
  234. mode := cipher.NewCBCDecrypter(block, iv)
  235. mode.CryptBlocks(encryptData, encryptData)
  236. // 解填充
  237. encryptData = PKCS7UnPadding(encryptData)
  238. return encryptData, nil
  239. }
  240. // 去除填充
  241. func PKCS7UnPadding(origData []byte) []byte {
  242. length := len(origData)
  243. unpadding := int(origData[length-1])
  244. if length-unpadding < 0 {
  245. return origData[:0]
  246. }
  247. fmt.Println("changdu", length)
  248. fmt.Println("data", unpadding)
  249. return origData[:(length - unpadding)]
  250. }
  251. func GetScheduleByXcx(orgID, start, end, patientID int64) (schedule []*models.ScheduleXcx, err error) {
  252. err = readDb.Model(&models.ScheduleXcx{}).Where("user_org_id=? and patient_id=? and schedule_date>=? and schedule_date<=? and status=1", orgID, patientID, start, end).Find(&schedule).Error
  253. return
  254. }
  255. func GetSignsByBlood(orgID, start, end, patientID int64) (schedule []*models.PredialysisEvaluationThree, err error) {
  256. err = readDb.Model(&models.PredialysisEvaluationThree{}).Where("user_org_id=? and patient_id=? and assessment_date>=? and assessment_date<=? and status=1", orgID, patientID, start, end).Find(&schedule).Error
  257. return
  258. }
  259. func GetSignsByWeight(orgID, start, end, patientID int64) (schedule []*models.PredialysisEvaluationOne, err error) {
  260. err = readDb.Model(&models.PredialysisEvaluationOne{}).Where("user_org_id=? and patient_id=? and assessment_date>=? and assessment_date<=? and status=1", orgID, patientID, start, end).Find(&schedule).Error
  261. return
  262. }
  263. func GetSignsByDryWeight(orgID, start, end, patientID int64) (schedule []*models.PredialysisEvaluationTwo, err error) {
  264. err = readDb.Model(&models.PredialysisEvaluationTwo{}).Where("user_org_id=? and patient_id=? and assessment_date>=? and assessment_date<=? and status=1", orgID, patientID, start, end).Find(&schedule).Error
  265. return
  266. }
  267. func GetSignsByAfterWeight(orgID, start, end, patientID int64) (record []*models.AssessmentAfterDislysisOne, err error) {
  268. err = readDb.Model(&models.AssessmentAfterDislysisOne{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date >=? and assessment_date <=?", patientID, orgID, start, end).Find(&record).Error
  269. if err != nil {
  270. if err == gorm.ErrRecordNotFound {
  271. return nil, nil
  272. } else {
  273. return nil, err
  274. }
  275. }
  276. return
  277. }
  278. func GetSignsByAfterBlood(orgID, start, end, patientID int64) (record []*models.AssessmentAfterDislysisTwo, err error) {
  279. err = readDb.Model(&models.AssessmentAfterDislysisTwo{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date >=? and assessment_date <=?", patientID, orgID, start, end).Find(&record).Error
  280. if err != nil {
  281. if err == gorm.ErrRecordNotFound {
  282. return nil, nil
  283. } else {
  284. return nil, err
  285. }
  286. }
  287. return
  288. }
  289. func GetScheduleInfo(startime int64, endtime int64, patient_id int64) (schedule []*models.VmBloodSchedule, err error) {
  290. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  291. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  292. fmt.Println(table)
  293. if startime > 0 {
  294. db = db.Where("x.schedule_date >=?", startime)
  295. }
  296. if endtime > 0 {
  297. db = db.Where("x.schedule_date <=?", endtime)
  298. }
  299. if patient_id > 0 {
  300. db = db.Where("x.patient_id = ?", patient_id)
  301. }
  302. err = db.Select("x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,t.name,x.bed_id,x.user_org_id").Joins("left join xt_patients as t on t.id = x.patient_id").Scan(&schedule).Error
  303. return schedule, err
  304. }
  305. func GetAllBedNumberListTwo(user_org_id int64) (list []*models.DeviceNumber, err error) {
  306. err = XTReadDB().Where("org_id =? and status=1", user_org_id).Find(&list).Error
  307. return list, err
  308. }
  309. func GetTodayDialysis(recordDate int64, patient_id int64) (models.XcXDialysisOrder, error) {
  310. order := models.XcXDialysisOrder{}
  311. err := XTReadDB().Model(&order).Where("dialysis_date = ? and patient_id = ? and status = 1", recordDate, patient_id).Find(&order).Error
  312. return order, err
  313. }
  314. func GetTodayPrescription(recordDate int64, patient_id int64) (models.XcxXtDialysisPrescription, error) {
  315. prescription := models.XcxXtDialysisPrescription{}
  316. err := XTReadDB().Model(&prescription).Where("record_date = ? and patient_id = ? and status = 1", recordDate, patient_id).Find(&prescription).Error
  317. return prescription, err
  318. }
  319. func GetNextPatientSchedule(patient_id int64, dialysis_date int64) (models.BloodSchedule, error) {
  320. schedule := models.BloodSchedule{}
  321. //err := XTReadDB().Model(&schedule).Where("patient_id = ? and status = 1 and schedule_date>?", patient_id, dialysis_date).Order("schedule_date asc").First(&schedule).Error
  322. //return schedule, err
  323. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  324. table := XTReadDB().Table("xt_device_zone as z").Where("z.status =1")
  325. tables := XTReadDB().Table("xt_device_number as n").Where("n.status =1")
  326. fmt.Println(table, tables)
  327. if patient_id > 0 {
  328. db = db.Where("x.patient_id = ?", patient_id)
  329. }
  330. if dialysis_date > 0 {
  331. db = db.Where("x.schedule_date >?", dialysis_date)
  332. }
  333. err := db.Select("x.patient_id,x.schedule_type,x.schedule_date,x.mode_id,z.name,n.number").Joins("left join xt_device_zone as z on z.id = x.partition_id").Joins("left join xt_device_number as n on n.id = x.bed_id").Order("x.schedule_date asc").Limit(1).Scan(&schedule).Error
  334. return schedule, err
  335. }
  336. func GetXcxPatientInfo(patient_id int64) (models.XcxPatients, error) {
  337. patients := models.XcxPatients{}
  338. err := XTReadDB().Model(&patients).Where("id = ? and status = 1 ", patient_id).Find(&patients).Error
  339. return patients, err
  340. }
  341. func GetOrgInfo(id int64) (models.XcxSgjUserOrg, error) {
  342. org := models.XcxSgjUserOrg{}
  343. err := UserReadDB().Model(&org).Where("id = ? and status= 1", id).Find(&org).Error
  344. return org, err
  345. }
  346. func GetEducationList(startime int64, endtime int64, patient_id int64) (treatment []*models.XcxTreatmentSummary, err error) {
  347. err = XTReadDB().Model(&treatment).Where("assessment_date>=? and assessment_date<=? and patient_id = ? and status =1", startime, endtime, patient_id).Order("assessment_date desc").Find(&treatment).Error
  348. return treatment, err
  349. }
  350. func GetDoctorAdviceConfig(orgid int64) (models.XcxDoctorAdviceConfig, error) {
  351. config := models.XcxDoctorAdviceConfig{}
  352. err := XTReadDB().Model(&config).Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  353. return config, err
  354. }
  355. func GetDoctorAdviceGroup(patient_id int64, startime int64, endtime int64) (advice []*models.XcxDoctorAdvice, err error) {
  356. err = XTReadDB().Where("patient_id = ? and advice_date >=? and advice_date<=? and status = 6", patient_id, startime, endtime).Group("advice_date").Order("advice_date desc").Find(&advice).Error
  357. return advice, err
  358. }
  359. func GetHisDoctorAdviceGroup(patient_id int64, startime int64, endtime int64) (advice []*models.XcxHisDoctorAdviceInfo, err error) {
  360. err = XTReadDB().Where("patient_id = ? and advice_date >=? and advice_date<=? and status = 1", patient_id, startime, endtime).Group("advice_date").Order("advice_date desc").Find(&advice).Error
  361. return advice, err
  362. }
  363. func GetDoctorAdvice(patient_id int64, startime int64, endtime int64) (advice []*models.XcxDoctorAdvice, err error) {
  364. err = XTReadDB().Where("patient_id = ? and advice_date >=? and advice_date<=? and status = 6", patient_id, startime, endtime).Find(&advice).Error
  365. return advice, err
  366. }
  367. func GetHisDoctorAdvice(patient_id int64, startime int64, endtime int64) (advice []*models.XcxHisDoctorAdviceInfo, err error) {
  368. err = XTReadDB().Where("patient_id = ? and advice_date >=? and advice_date<=? and status = 1", patient_id, startime, endtime).Find(&advice).Error
  369. return advice, err
  370. }
  371. // 用户排班信息
  372. func MobileGetSchedualDetailOne(orgID int64, patientID int64) (*MDialysisScheduleVM, error) {
  373. var vm MDialysisScheduleVM
  374. err := readDb.
  375. Table("xt_schedule").
  376. Preload("DeviceNumber", "status = 6 AND org_id = ?", orgID).
  377. Where("status = 1 AND user_org_id = ? AND patient_id = ?", orgID, patientID).
  378. First(&vm).Error
  379. if err != nil {
  380. if err == gorm.ErrRecordNotFound {
  381. return nil, nil
  382. } else {
  383. return nil, err
  384. }
  385. }
  386. return &vm, err
  387. }
  388. // 接诊评估
  389. func MobileGetReceiverTreatmentAccessRecordOne(orgID int64, patientID int64, record_date int64) (*models.ReceiveTreatmentAsses, error) {
  390. var record models.ReceiveTreatmentAsses
  391. err = readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? and user_org_id = ? and status = 6 and record_date = ?", patientID, orgID, record_date).Find(&record).Error
  392. if err != nil {
  393. if err == gorm.ErrRecordNotFound {
  394. return nil, nil
  395. } else {
  396. return nil, err
  397. }
  398. }
  399. return &record, nil
  400. }
  401. // 透前评估
  402. func MobileGetPredialysisEvaluationOne(orgID int64, patientID int64, assessment_date int64) (*models.PredialysisEvaluation, error) {
  403. var record models.PredialysisEvaluation
  404. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 6 and assessment_date = ?", patientID, orgID, assessment_date).Find(&record).Error
  405. if err != nil {
  406. if err == gorm.ErrRecordNotFound {
  407. return nil, nil
  408. } else {
  409. return nil, err
  410. }
  411. }
  412. return &record, nil
  413. }
  414. // 获取 maxDate 之前一次的透前评估记录
  415. func MobileGetLastTimePredialysisEvaluationOne(orgID int64, patientID int64) (*models.PredialysisEvaluation, error) {
  416. var record models.PredialysisEvaluation
  417. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 6", patientID, orgID).Order("assessment_date desc").Last(&record).Error
  418. if err != nil {
  419. if err == gorm.ErrRecordNotFound {
  420. return nil, nil
  421. } else {
  422. return nil, err
  423. }
  424. }
  425. return &record, nil
  426. }
  427. func MobileGetDoctorAdvicesByGroupsOne(orgID int64, patientID int64, advicedate int64) ([]*models.DoctorAdvice, error) {
  428. var records []*models.DoctorAdvice
  429. err := readDb.
  430. Model(&models.DoctorAdvice{}).
  431. Where("patient_id = ? and user_org_id = ? and status = 6 and (advice_type = 2 || advice_type = 3) and advice_date = ?", patientID, orgID, advicedate).
  432. Select("id, user_org_id, patient_id, advice_type, advice_date, record_date, start_time, advice_name,advice_desc, reminder_date, drug_spec, drug_spec_unit, single_dose, single_dose_unit, prescribing_number, prescribing_number_unit, delivery_way, execution_frequency, advice_doctor, status, created_time,updated_time, advice_affirm, remark, stop_time, stop_reason, stop_doctor, stop_state, parent_id, execution_time, execution_staff, execution_state, checker, check_state, check_time, groupno,way,drug_id,drug_name_id, IF(parent_id > 0, parent_id, id) as advice_order").
  433. Order("start_time asc, groupno desc, advice_order desc, id asc").
  434. Scan(&records).Error
  435. if err != nil {
  436. return nil, err
  437. }
  438. return records, nil
  439. }
  440. // 透析记录
  441. func MobileGetSchedualDialysisRecordOne(orgID int64, patientID int64, dialysis_date int64) (*models.DialysisOrder, error) {
  442. var record models.DialysisOrder
  443. err := readDb.Model(&models.DialysisOrder{}).Preload("DeviceNumber", "org_id = ? AND status = 1", orgID).Where("user_org_id = ? AND patient_id = ? and dialysis_date = ? and status =6", orgID, patientID, dialysis_date).Find(&record).Error
  444. if err != nil {
  445. if err == gorm.ErrRecordNotFound {
  446. return nil, nil
  447. } else {
  448. return nil, err
  449. }
  450. }
  451. return &record, nil
  452. }
  453. // 双人核对
  454. func MobileGetDoubleCheckOne(orgID int64, patientID int64, check_date int64) (*models.DoubleCheck, error) {
  455. var record models.DoubleCheck
  456. err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? and user_org_id = ? and status = 6 and check_date = ?", patientID, orgID, check_date).Find(&record).Error
  457. if err != nil {
  458. if err == gorm.ErrRecordNotFound {
  459. return nil, nil
  460. } else {
  461. return nil, err
  462. }
  463. }
  464. return &record, nil
  465. }
  466. // 透析监测记录
  467. func MobileGetMonitorRecordsOne(orgID int64, patientID int64, monitoring_date int64) ([]*models.MonitoringRecord, error) {
  468. var records []*models.MonitoringRecord
  469. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 6 and monitoring_date = ?", patientID, orgID, monitoring_date).Order("operate_time asc").Find(&records).Error
  470. if err != nil {
  471. return nil, err
  472. }
  473. return records, nil
  474. }
  475. func MobileGetLastMonitorRecordOne(orgID int64, patientID int64) (*models.MonitoringRecord, error) {
  476. var record models.MonitoringRecord
  477. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 6", patientID, orgID).Order("operate_time desc").Last(&record).Error
  478. if err != nil {
  479. if err == gorm.ErrRecordNotFound {
  480. return nil, nil
  481. } else {
  482. return nil, err
  483. }
  484. }
  485. return &record, nil
  486. }
  487. // 透后评估
  488. func MobileGetAssessmentAfterDislysisOne(orgID int64, patientID int64, assessment_date int64) (*models.AssessmentAfterDislysis, error) {
  489. var record models.AssessmentAfterDislysis
  490. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 6 and assessment_date =? ", patientID, orgID, assessment_date).Find(&record).Error
  491. if err != nil {
  492. if err == gorm.ErrRecordNotFound {
  493. return nil, nil
  494. } else {
  495. return nil, err
  496. }
  497. }
  498. return &record, nil
  499. }
  500. // 治疗小结
  501. func MobileGetTreatmentSummaryOne(orgID int64, patientID int64, assessment_date int64) (*models.TreatmentSummary, error) {
  502. var record models.TreatmentSummary
  503. err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? and user_org_id = ? and status = 6 and assessment_date= ?", patientID, orgID, assessment_date).Find(&record).Error
  504. if err != nil {
  505. if err == gorm.ErrRecordNotFound {
  506. return nil, nil
  507. } else {
  508. return nil, err
  509. }
  510. }
  511. return &record, nil
  512. }
  513. // 透析处方
  514. func MobileGetDialysisPrescribeByModeIdSix(orgID int64, patientID int64, mode_id int64, record_date int64) (*models.DialysisPrescription, error) {
  515. var record models.DialysisPrescription
  516. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ? and record_date = ?", patientID, orgID, mode_id, record_date).Find(&record).Error
  517. if err != nil {
  518. if err == gorm.ErrRecordNotFound {
  519. return nil, nil
  520. } else {
  521. return nil, err
  522. }
  523. }
  524. return &record, nil
  525. }
  526. func GetHisPrescriptionProjectsOne(user_org_id int64, patient_id int64) (projects []*models.HisPrescriptionProject, err error) {
  527. err = readDb.Model(&models.HisPrescriptionProject{}).Preload("HisProject", "status = 1").Preload("GoodInfo", "status = 1").Where("user_org_id = ? AND patient_id = ? AND status = 1", user_org_id, patient_id).Find(&projects).Error
  528. return
  529. }
  530. func GetDialyStockOutOne(orgid int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  531. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  532. if orgid > 0 {
  533. db = db.Where("x.user_org_id = ?", orgid)
  534. }
  535. if patientId > 0 {
  536. db = db.Where("x.patient_id = ?", patientId)
  537. }
  538. err = db.Select("x.id,x.user_org_id,x.patient_id,x.record_date,x.good_id,x.good_type_id,x.count,x.commdity_code,t.specification_name").Joins("left join xt_good_information as t on t.id = x.good_id and t.org_id = ? and t.status = 1", orgid).Scan(&prepare).Error
  539. return prepare, err
  540. }
  541. func MobileGetDialysisPrescribeByModeIdFive(orgID int64, patientID int64, record_date int64) (*models.DialysisPrescription, error) {
  542. var record models.DialysisPrescription
  543. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 6 and record_date = ?", patientID, orgID, record_date).Find(&record).Error
  544. if err != nil {
  545. if err == gorm.ErrRecordNotFound {
  546. return nil, nil
  547. } else {
  548. return nil, err
  549. }
  550. }
  551. return &record, nil
  552. }
  553. func GetAllHisDoctorAdviceOne(orgid int64, patientid int64, advice_date int64) (his []*models.HisDoctorAdviceInfo, err error) {
  554. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? AND user_org_id = ? AND status=1 and advice_date = ?", patientid, orgid, advice_date).Find(&his).Error
  555. return
  556. }
  557. func GetDoctorName(orgid int64) (role []*models.XcxSgjUserAdminRole, err error) {
  558. err = UserReadDB().Where("org_id = ? and status = 1", orgid).Find(&role).Error
  559. return role, err
  560. }
  561. func GetPrescripionFieldConfig(orgid int64) (list []*models.XcxFiledConfig, err error) {
  562. err = XTReadDB().Model(&list).Where("org_id = ? and module = 1 ", orgid).Find(&list).Error
  563. return list, err
  564. }
  565. func GetReceiveTreatmentAssess(orgid int64) (list []*models.XcxFiledConfig, err error) {
  566. err = XTReadDB().Model(&list).Where("org_id = ? and module = 2 ", orgid).Find(&list).Error
  567. return list, err
  568. }
  569. func GetDialysisBefor(orgid int64) (list []*models.XcxFiledConfig, err error) {
  570. err = XTReadDB().Model(&list).Where("org_id = ? and module = 3", orgid).Find(&list).Error
  571. return list, err
  572. }
  573. func GetDialysisOrderConfig(orgid int64) (list []*models.XcxFiledConfig, err error) {
  574. err = XTReadDB().Model(&list).Where("org_id = ? and module = 7"+
  575. "", orgid).Find(&list).Error
  576. return list, err
  577. }
  578. func GetMonitorRecordConfig(orgid int64) (list []*models.XcxFiledConfig, err error) {
  579. err = XTReadDB().Model(&list).Where("org_id = ? and module = 4", orgid).Find(&list).Error
  580. return list, err
  581. }
  582. func GetDialysisAfeterConfig(orgid int64) (list []*models.XcxFiledConfig, err error) {
  583. err = XTReadDB().Model(&list).Where("org_id = ? and module = 5", orgid).Find(&list).Error
  584. return list, err
  585. }
  586. func GetTreatMentConfig(orgid int64) (list []*models.XcxFiledConfig, err error) {
  587. err = XTReadDB().Model(&list).Where("org_id = ? and module = 6", orgid).Find(&list).Error
  588. return list, err
  589. }
  590. func GetDataConfig(orgid int64) (list []*models.XcxDataConfig, err error) {
  591. err = XTReadDB().Model(&list).Where("(org_id = ? or org_id = 0) and status = 1", orgid).Find(&list).Error
  592. return list, err
  593. }
  594. func GetLastScheduleListOne(patient_id int64, schedule_date int64) (models.XcxSchedule, error) {
  595. schedule := models.XcxSchedule{}
  596. err := XTReadDB().Model(&schedule).Where("patient_id = ? and status = 1 and schedule_date <=?", patient_id, schedule_date).Order("schedule_date desc").Last(&schedule).Error
  597. return schedule, err
  598. }
  599. func GetInspectionGroupListByXcx(patientid int64, page int64, number int64) (inspection []*models.XcxInspectionTwo, err error) {
  600. db := XTReadDB().Table("xt_inspection as x").Where("x.status = 1")
  601. if patientid > 0 {
  602. db = db.Where("x.patient_id = ?", patientid)
  603. }
  604. offset := (page - 1) * number
  605. //if startime > 0 {
  606. // db = db.Where("x.inspect_date >= ?", startime)
  607. //}
  608. //if endtime > 0 {
  609. // db = db.Where("x.inspect_date <= ?", endtime)
  610. //}
  611. err = db.Group("x.inspect_date,x.project_id").Order("x.created_time").Offset(offset).Limit(number).Scan(&inspection).Error
  612. return inspection, err
  613. }
  614. func GetInspectionInfoByXcx(patientid int64, project_id int64, inspect_date int64) (inspection []*models.XcxInspectionTwo, err error) {
  615. err = XTReadDB().Model(&inspection).Where("(patient_id = ? and project_id = ? and inspect_date=?) and status = 1", patientid, project_id, inspect_date).Preload("InspectionReference", "status = 1").Find(&inspection).Error
  616. return inspection, err
  617. }
  618. func GetInspectionGroupList(patientid int64, startime int64, endtime int64) (inspection []*models.XcxInspection, err error) {
  619. db := XTReadDB().Table("xt_inspection as x").Where("x.status = 1")
  620. if patientid > 0 {
  621. db = db.Where("x.patient_id = ?", patientid)
  622. }
  623. if startime > 0 {
  624. db = db.Where("x.inspect_date >= ?", startime)
  625. }
  626. if endtime > 0 {
  627. db = db.Where("x.inspect_date <= ?", endtime)
  628. }
  629. err = db.Group("x.inspect_date,x.project_id").Find(&inspection).Error
  630. return inspection, err
  631. }
  632. func GetInsepctionList(patientid int64, startime int64, endtime int64) (inspection []*models.XcxInspection, err error) {
  633. db := XTReadDB().Table("xt_inspection as x").Where("x.status =1")
  634. table := XTReadDB().Table("xt_inspection_reference as r")
  635. fmt.Print("table", table)
  636. if patientid > 0 {
  637. db = db.Where("x.patient_id = ?", patientid)
  638. }
  639. if startime > 0 {
  640. db = db.Where("x.inspect_date >= ?", startime)
  641. }
  642. if endtime > 0 {
  643. db = db.Where("x.inspect_date <= ?", endtime)
  644. }
  645. err = db.Group("x.id").Select("x.id,x.patient_id,x.org_id,x.project_id,x.item_id,x.item_name,x.project_name,x.inspect_type,x.inspect_value,x.inspect_date,x.status,x.created_time,x.updated_time,r.range_type,r.range_min,r.range_max,r.range_value,r.range_options,r.unit").Joins("left join xt_inspection_reference as r on (r.item_id = x.item_id AND r.org_id > 0) OR ( x.item_id = r.id AND r.org_id = 0) ").Scan(&inspection).Error
  646. return inspection, err
  647. }
  648. func GetArticList(activityID int64, page int64, limit int64) (story []*models.SybStory, total int64, err error) {
  649. offset := (page - 1) * limit
  650. err = UserReadDB().Model(&story).Where("activity_id = ? and audit_status = 2", activityID).Count(&total).Order("ctime desc").Offset(offset).Limit(limit).Find(&story).Error
  651. return story, total, err
  652. }
  653. func GetArticListDetail(id int64) (models.SybStory, error) {
  654. story := models.SybStory{}
  655. err := UserReadDB().Where("id = ?", id).Find(&story).Error
  656. return story, err
  657. }
  658. func GetStoryScoreList(activityID int64, page int64, limit int64) (score []*models.SybStoryScore, total int64, err error) {
  659. offset := (page - 1) * limit
  660. err = UserReadDB().Model(&score).Where("activity_id = ?", activityID).Count(&total).Offset(offset).Limit(limit).Preload("SybStory").Find(&score).Error
  661. return score, total, err
  662. }
  663. func SendWxAuthAPI(appID string, appSecret string, grant_type string, code string) (map[string]string, error) {
  664. var code2sessionURL = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appID + "&secret=" + appSecret + "&js_code=" + code + "&grant_type=" + grant_type
  665. var url = fmt.Sprintf(code2sessionURL, appID, appSecret, code)
  666. fmt.Println("url233232323322323", url)
  667. resp, err := http.DefaultClient.Get(code2sessionURL)
  668. fmt.Println("resp32332232332322", resp)
  669. var wxMap map[string]string
  670. err = json.NewDecoder(resp.Body).Decode(&wxMap)
  671. if err != nil {
  672. return wxMap, err
  673. }
  674. defer resp.Body.Close()
  675. return wxMap, err
  676. }
  677. func SendWxAuthAPIOne(appID string, appSecret string, grant_type string, code string) (string, error) {
  678. var code2sessionURL = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appID + "&secret=" + appSecret + "&js_code=" + code + "&grant_type=" + grant_type
  679. var url = fmt.Sprintf(code2sessionURL, appID, appSecret, code)
  680. fmt.Println(url)
  681. resp, err := http.DefaultClient.Get(code2sessionURL)
  682. if err != nil {
  683. return "", err
  684. }
  685. var wxMap map[string]string
  686. err = json.NewDecoder(resp.Body).Decode(&wxMap)
  687. if err != nil {
  688. return "", err
  689. }
  690. defer resp.Body.Close()
  691. fmt.Println("2333232232", wxMap)
  692. return wxMap["session_key"], nil
  693. }