xcx_mobile_api_service.go 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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").Joins("left join xt_patients as t on t.id = x.patient_id").Scan(&schedule).Error
  303. return schedule, err
  304. }
  305. func GetTodayDialysis(recordDate int64, patient_id int64) (models.XcXDialysisOrder, error) {
  306. order := models.XcXDialysisOrder{}
  307. err := XTReadDB().Model(&order).Where("dialysis_date = ? and patient_id = ? and status = 1", recordDate, patient_id).Find(&order).Error
  308. return order, err
  309. }
  310. func GetTodayPrescription(recordDate int64, patient_id int64) (models.XcxXtDialysisPrescription, error) {
  311. prescription := models.XcxXtDialysisPrescription{}
  312. err := XTReadDB().Model(&prescription).Where("record_date = ? and patient_id = ? and status = 1", recordDate, patient_id).Find(&prescription).Error
  313. return prescription, err
  314. }
  315. func GetNextPatientSchedule(patient_id int64, dialysis_date int64) (models.BloodSchedule, error) {
  316. schedule := models.BloodSchedule{}
  317. //err := XTReadDB().Model(&schedule).Where("patient_id = ? and status = 1 and schedule_date>?", patient_id, dialysis_date).Order("schedule_date asc").First(&schedule).Error
  318. //return schedule, err
  319. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  320. table := XTReadDB().Table("xt_device_zone as z").Where("z.status =1")
  321. tables := XTReadDB().Table("xt_device_number as n").Where("n.status =1")
  322. fmt.Println(table, tables)
  323. if patient_id > 0 {
  324. db = db.Where("x.patient_id = ?", patient_id)
  325. }
  326. if dialysis_date > 0 {
  327. db = db.Where("x.schedule_date >?", dialysis_date)
  328. }
  329. 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
  330. return schedule, err
  331. }
  332. func GetXcxPatientInfo(patient_id int64) (models.XcxPatients, error) {
  333. patients := models.XcxPatients{}
  334. err := XTReadDB().Model(&patients).Where("id = ? and status = 1 ", patient_id).Find(&patients).Error
  335. return patients, err
  336. }
  337. func GetOrgInfo(id int64) (models.XcxSgjUserOrg, error) {
  338. org := models.XcxSgjUserOrg{}
  339. err := UserReadDB().Model(&org).Where("id = ? and status= 1", id).Find(&org).Error
  340. return org, err
  341. }
  342. func GetEducationList(startime int64, endtime int64, patient_id int64) (treatment []*models.XcxTreatmentSummary, err error) {
  343. 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
  344. return treatment, err
  345. }
  346. func GetDoctorAdviceConfig(orgid int64) (models.XcxDoctorAdviceConfig, error) {
  347. config := models.XcxDoctorAdviceConfig{}
  348. err := XTReadDB().Model(&config).Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  349. return config, err
  350. }
  351. func GetDoctorAdviceGroup(patient_id int64, startime int64, endtime int64) (advice []*models.XcxDoctorAdvice, err error) {
  352. 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
  353. return advice, err
  354. }
  355. func GetHisDoctorAdviceGroup(patient_id int64, startime int64, endtime int64) (advice []*models.XcxHisDoctorAdviceInfo, err error) {
  356. 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
  357. return advice, err
  358. }
  359. func GetDoctorAdvice(patient_id int64, startime int64, endtime int64) (advice []*models.XcxDoctorAdvice, err error) {
  360. err = XTReadDB().Where("patient_id = ? and advice_date >=? and advice_date<=? and status = 1", patient_id, startime, endtime).Find(&advice).Error
  361. return advice, err
  362. }
  363. func GetHisDoctorAdvice(patient_id int64, startime int64, endtime int64) (advice []*models.XcxHisDoctorAdviceInfo, err error) {
  364. err = XTReadDB().Where("patient_id = ? and advice_date >=? and advice_date<=? and status = 1", patient_id, startime, endtime).Find(&advice).Error
  365. return advice, err
  366. }
  367. // 用户排班信息
  368. func MobileGetSchedualDetailOne(orgID int64, patientID int64) (*MDialysisScheduleVM, error) {
  369. var vm MDialysisScheduleVM
  370. err := readDb.
  371. Table("xt_schedule").
  372. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  373. Where("status = 1 AND user_org_id = ? AND patient_id = ?", orgID, patientID).
  374. First(&vm).Error
  375. if err != nil {
  376. if err == gorm.ErrRecordNotFound {
  377. return nil, nil
  378. } else {
  379. return nil, err
  380. }
  381. }
  382. return &vm, err
  383. }
  384. // 接诊评估
  385. func MobileGetReceiverTreatmentAccessRecordOne(orgID int64, patientID int64, record_date int64) (*models.ReceiveTreatmentAsses, error) {
  386. var record models.ReceiveTreatmentAsses
  387. err = readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, record_date).Find(&record).Error
  388. if err != nil {
  389. if err == gorm.ErrRecordNotFound {
  390. return nil, nil
  391. } else {
  392. return nil, err
  393. }
  394. }
  395. return &record, nil
  396. }
  397. // 透前评估
  398. func MobileGetPredialysisEvaluationOne(orgID int64, patientID int64, assessment_date int64) (*models.PredialysisEvaluation, error) {
  399. var record models.PredialysisEvaluation
  400. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, assessment_date).Find(&record).Error
  401. if err != nil {
  402. if err == gorm.ErrRecordNotFound {
  403. return nil, nil
  404. } else {
  405. return nil, err
  406. }
  407. }
  408. return &record, nil
  409. }
  410. // 获取 maxDate 之前一次的透前评估记录
  411. func MobileGetLastTimePredialysisEvaluationOne(orgID int64, patientID int64) (*models.PredialysisEvaluation, error) {
  412. var record models.PredialysisEvaluation
  413. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Order("assessment_date desc").Last(&record).Error
  414. if err != nil {
  415. if err == gorm.ErrRecordNotFound {
  416. return nil, nil
  417. } else {
  418. return nil, err
  419. }
  420. }
  421. return &record, nil
  422. }
  423. func MobileGetDoctorAdvicesByGroupsOne(orgID int64, patientID int64, advicedate int64) ([]*models.DoctorAdvice, error) {
  424. var records []*models.DoctorAdvice
  425. err := readDb.
  426. Model(&models.DoctorAdvice{}).
  427. Where("patient_id = ? and user_org_id = ? and status = 1 and (advice_type = 2 || advice_type = 3) and advice_date = ?", patientID, orgID, advicedate).
  428. 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").
  429. Order("start_time asc, groupno desc, advice_order desc, id asc").
  430. Scan(&records).Error
  431. if err != nil {
  432. return nil, err
  433. }
  434. return records, nil
  435. }
  436. // 透析记录
  437. func MobileGetSchedualDialysisRecordOne(orgID int64, patientID int64, dialysis_date int64) (*models.DialysisOrder, error) {
  438. var record models.DialysisOrder
  439. err := readDb.Model(&models.DialysisOrder{}).Preload("DeviceNumber", "org_id = ? AND status = 1 ", orgID).Where("user_org_id = ? AND patient_id = ? and dialysis_date = ? ", orgID, patientID, dialysis_date).Find(&record).Error
  440. if err != nil {
  441. if err == gorm.ErrRecordNotFound {
  442. return nil, nil
  443. } else {
  444. return nil, err
  445. }
  446. }
  447. return &record, nil
  448. }
  449. // 双人核对
  450. func MobileGetDoubleCheckOne(orgID int64, patientID int64, check_date int64) (*models.DoubleCheck, error) {
  451. var record models.DoubleCheck
  452. err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? and user_org_id = ? and status = 1 and check_date = ?", patientID, orgID, check_date).Find(&record).Error
  453. if err != nil {
  454. if err == gorm.ErrRecordNotFound {
  455. return nil, nil
  456. } else {
  457. return nil, err
  458. }
  459. }
  460. return &record, nil
  461. }
  462. // 透析监测记录
  463. func MobileGetMonitorRecordsOne(orgID int64, patientID int64, monitoring_date int64) ([]*models.MonitoringRecord, error) {
  464. var records []*models.MonitoringRecord
  465. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1 and monitoring_date = ?", patientID, orgID, monitoring_date).Order("operate_time asc").Find(&records).Error
  466. if err != nil {
  467. return nil, err
  468. }
  469. return records, nil
  470. }
  471. func MobileGetLastMonitorRecordOne(orgID int64, patientID int64) (*models.MonitoringRecord, error) {
  472. var record models.MonitoringRecord
  473. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Order("operate_time desc").Last(&record).Error
  474. if err != nil {
  475. if err == gorm.ErrRecordNotFound {
  476. return nil, nil
  477. } else {
  478. return nil, err
  479. }
  480. }
  481. return &record, nil
  482. }
  483. // 透后评估
  484. func MobileGetAssessmentAfterDislysisOne(orgID int64, patientID int64, assessment_date int64) (*models.AssessmentAfterDislysis, error) {
  485. var record models.AssessmentAfterDislysis
  486. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date =? ", patientID, orgID, assessment_date).Find(&record).Error
  487. if err != nil {
  488. if err == gorm.ErrRecordNotFound {
  489. return nil, nil
  490. } else {
  491. return nil, err
  492. }
  493. }
  494. return &record, nil
  495. }
  496. // 治疗小结
  497. func MobileGetTreatmentSummaryOne(orgID int64, patientID int64, assessment_date int64) (*models.TreatmentSummary, error) {
  498. var record models.TreatmentSummary
  499. err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date= ?", patientID, orgID, assessment_date).Find(&record).Error
  500. if err != nil {
  501. if err == gorm.ErrRecordNotFound {
  502. return nil, nil
  503. } else {
  504. return nil, err
  505. }
  506. }
  507. return &record, nil
  508. }
  509. // 透析处方
  510. func MobileGetDialysisPrescribeByModeIdSix(orgID int64, patientID int64, mode_id int64, record_date int64) (*models.DialysisPrescription, error) {
  511. var record models.DialysisPrescription
  512. 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
  513. if err != nil {
  514. if err == gorm.ErrRecordNotFound {
  515. return nil, nil
  516. } else {
  517. return nil, err
  518. }
  519. }
  520. return &record, nil
  521. }
  522. func GetHisPrescriptionProjectsOne(user_org_id int64, patient_id int64) (projects []*models.HisPrescriptionProject, err error) {
  523. 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
  524. return
  525. }
  526. func GetDialyStockOutOne(orgid int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  527. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  528. if orgid > 0 {
  529. db = db.Where("x.user_org_id = ?", orgid)
  530. }
  531. if patientId > 0 {
  532. db = db.Where("x.patient_id = ?", patientId)
  533. }
  534. 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
  535. return prepare, err
  536. }
  537. func MobileGetDialysisPrescribeByModeIdFive(orgID int64, patientID int64, record_date int64) (*models.DialysisPrescription, error) {
  538. var record models.DialysisPrescription
  539. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, record_date).Find(&record).Error
  540. if err != nil {
  541. if err == gorm.ErrRecordNotFound {
  542. return nil, nil
  543. } else {
  544. return nil, err
  545. }
  546. }
  547. return &record, nil
  548. }
  549. func GetAllHisDoctorAdviceOne(orgid int64, patientid int64, advice_date int64) (his []*models.HisDoctorAdviceInfo, err error) {
  550. 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
  551. return
  552. }
  553. func GetDoctorName(orgid int64) (role []*models.XcxSgjUserAdminRole, err error) {
  554. err = UserReadDB().Where("org_id = ? and status = 1", orgid).Find(&role).Error
  555. return role, err
  556. }
  557. func GetPrescripionFieldConfig(orgid int64) (list []*models.XcxFiledConfig, err error) {
  558. err = XTReadDB().Model(&list).Where("org_id = ? and module = 1 ", orgid).Find(&list).Error
  559. return list, err
  560. }
  561. func GetReceiveTreatmentAssess(orgid int64) (list []*models.XcxFiledConfig, err error) {
  562. err = XTReadDB().Model(&list).Where("org_id = ? and module = 2 ", orgid).Find(&list).Error
  563. return list, err
  564. }
  565. func GetDialysisBefor(orgid int64) (list []*models.XcxFiledConfig, err error) {
  566. err = XTReadDB().Model(&list).Where("org_id = ? and module = 3", orgid).Find(&list).Error
  567. return list, err
  568. }
  569. func GetDialysisOrderConfig(orgid int64) (list []*models.XcxFiledConfig, err error) {
  570. err = XTReadDB().Model(&list).Where("org_id = ? and module = 7"+
  571. "", orgid).Find(&list).Error
  572. return list, err
  573. }
  574. func GetMonitorRecordConfig(orgid int64) (list []*models.XcxFiledConfig, err error) {
  575. err = XTReadDB().Model(&list).Where("org_id = ? and module = 4", orgid).Find(&list).Error
  576. return list, err
  577. }
  578. func GetDialysisAfeterConfig(orgid int64) (list []*models.XcxFiledConfig, err error) {
  579. err = XTReadDB().Model(&list).Where("org_id = ? and module = 5", orgid).Find(&list).Error
  580. return list, err
  581. }
  582. func GetTreatMentConfig(orgid int64) (list []*models.XcxFiledConfig, err error) {
  583. err = XTReadDB().Model(&list).Where("org_id = ? and module = 6", orgid).Find(&list).Error
  584. return list, err
  585. }
  586. func GetDataConfig(orgid int64) (list []*models.XcxDataConfig, err error) {
  587. err = XTReadDB().Model(&list).Where("(org_id = ? or org_id = 0) and status = 1", orgid).Find(&list).Error
  588. return list, err
  589. }
  590. func GetLastScheduleListOne(patient_id int64, schedule_date int64) (models.XcxSchedule, error) {
  591. schedule := models.XcxSchedule{}
  592. err := XTReadDB().Model(&schedule).Where("patient_id = ? and status = 1 and schedule_date <=?", patient_id, schedule_date).Order("schedule_date desc").Last(&schedule).Error
  593. return schedule, err
  594. }
  595. func GetInspectionGroupListByXcx(patientid int64, page int64, number int64) (inspection []*models.XcxInspectionTwo, err error) {
  596. db := XTReadDB().Table("xt_inspection as x").Where("x.status = 1")
  597. if patientid > 0 {
  598. db = db.Where("x.patient_id = ?", patientid)
  599. }
  600. offset := (page - 1) * number
  601. //if startime > 0 {
  602. // db = db.Where("x.inspect_date >= ?", startime)
  603. //}
  604. //if endtime > 0 {
  605. // db = db.Where("x.inspect_date <= ?", endtime)
  606. //}
  607. err = db.Group("x.inspect_date,x.project_id").Order("x.created_time").Offset(offset).Limit(number).Scan(&inspection).Error
  608. return inspection, err
  609. }
  610. func GetInspectionInfoByXcx(patientid int64, project_id int64, inspect_date int64) (inspection []*models.XcxInspectionTwo, err error) {
  611. 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
  612. return inspection, err
  613. }
  614. func GetInspectionGroupList(patientid int64, startime int64, endtime int64) (inspection []*models.XcxInspection, err error) {
  615. db := XTReadDB().Table("xt_inspection as x").Where("x.status = 1")
  616. if patientid > 0 {
  617. db = db.Where("x.patient_id = ?", patientid)
  618. }
  619. if startime > 0 {
  620. db = db.Where("x.inspect_date >= ?", startime)
  621. }
  622. if endtime > 0 {
  623. db = db.Where("x.inspect_date <= ?", endtime)
  624. }
  625. err = db.Group("x.inspect_date,x.project_id").Find(&inspection).Error
  626. return inspection, err
  627. }
  628. func GetInsepctionList(patientid int64, startime int64, endtime int64) (inspection []*models.XcxInspection, err error) {
  629. db := XTReadDB().Table("xt_inspection as x").Where("x.status =1")
  630. table := XTReadDB().Table("xt_inspection_reference as r")
  631. fmt.Print("table", table)
  632. if patientid > 0 {
  633. db = db.Where("x.patient_id = ?", patientid)
  634. }
  635. if startime > 0 {
  636. db = db.Where("x.inspect_date >= ?", startime)
  637. }
  638. if endtime > 0 {
  639. db = db.Where("x.inspect_date <= ?", endtime)
  640. }
  641. 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
  642. return inspection, err
  643. }
  644. func GetArticList(activityID int64, page int64, limit int64) (story []*models.SybStory, total int64, err error) {
  645. offset := (page - 1) * limit
  646. err = UserReadDB().Model(&story).Where("activity_id = ? and audit_status = 2", activityID).Count(&total).Order("ctime desc").Offset(offset).Limit(limit).Find(&story).Error
  647. return story, total, err
  648. }
  649. func GetArticListDetail(id int64) (models.SybStory, error) {
  650. story := models.SybStory{}
  651. err := UserReadDB().Where("id = ?", id).Find(&story).Error
  652. return story, err
  653. }
  654. func GetStoryScoreList(activityID int64, page int64, limit int64) (score []*models.SybStoryScore, total int64, err error) {
  655. offset := (page - 1) * limit
  656. err = UserReadDB().Model(&score).Where("activity_id = ?", activityID).Count(&total).Offset(offset).Limit(limit).Preload("SybStory").Find(&score).Error
  657. return score, total, err
  658. }
  659. func SendWxAuthAPI(appID string, appSecret string, grant_type string, code string) (map[string]string, error) {
  660. var code2sessionURL = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appID + "&secret=" + appSecret + "&js_code=" + code + "&grant_type=" + grant_type
  661. var url = fmt.Sprintf(code2sessionURL, appID, appSecret, code)
  662. fmt.Println("url233232323322323", url)
  663. resp, err := http.DefaultClient.Get(code2sessionURL)
  664. fmt.Println("resp32332232332322", resp)
  665. var wxMap map[string]string
  666. err = json.NewDecoder(resp.Body).Decode(&wxMap)
  667. if err != nil {
  668. return wxMap, err
  669. }
  670. defer resp.Body.Close()
  671. return wxMap, err
  672. }
  673. func SendWxAuthAPIOne(appID string, appSecret string, grant_type string, code string) (string, error) {
  674. var code2sessionURL = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appID + "&secret=" + appSecret + "&js_code=" + code + "&grant_type=" + grant_type
  675. var url = fmt.Sprintf(code2sessionURL, appID, appSecret, code)
  676. fmt.Println(url)
  677. resp, err := http.DefaultClient.Get(code2sessionURL)
  678. if err != nil {
  679. return "", err
  680. }
  681. var wxMap map[string]string
  682. err = json.NewDecoder(resp.Body).Decode(&wxMap)
  683. if err != nil {
  684. return "", err
  685. }
  686. defer resp.Body.Close()
  687. fmt.Println("2333232232", wxMap)
  688. return wxMap["session_key"], nil
  689. }