xcx_mobile_api_service.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. "time"
  12. )
  13. func GetXcxMobileInformation(mobile string) (*models.XcxAdminUserRole, error) {
  14. role := models.XcxAdminUserRole{}
  15. err := UserReadDB().Model(&role).Where("mobile = ? and status = 1", mobile).Find(&role).Error
  16. if err == gorm.ErrRecordNotFound {
  17. return nil, err
  18. }
  19. if err != nil {
  20. return nil, err
  21. }
  22. return &role, nil
  23. }
  24. func CreateXcxAdminUser(role models.XcxAdminUserRole) error {
  25. err := UserReadDB().Create(&role).Error
  26. return err
  27. }
  28. func GetLoginInfor(mobile string) (models.XcxAdminUserRole, error) {
  29. role := models.XcxAdminUserRole{}
  30. err := UserReadDB().Model(&role).Where("mobile = ? and status = 1", mobile).Find(&role).Error
  31. return role, err
  32. }
  33. func GetMobilePatientInfo(mobile string) (models.XcxPatients, error) {
  34. patients := models.XcxPatients{}
  35. err := XTReadDB().Model(&patients).Where("(phone = ? or home_telephone = ? ) and status = 1", mobile, mobile).Find(&patients).Error
  36. return patients, err
  37. }
  38. func GetPatientListByPatientId(id int64) (models.XcxPatients, error) {
  39. patients := models.XcxPatients{}
  40. err := XTReadDB().Model(&patients).Where("id = ? and status = 1", id).Find(&patients).Error
  41. return patients, err
  42. }
  43. func DecryptData(app_id, session_key, iv, encrypted_data string) (map[string]interface{}, error) {
  44. aesKey, err := base64.StdEncoding.DecodeString(session_key)
  45. if err != nil {
  46. return nil, err
  47. }
  48. ivKey, err := base64.StdEncoding.DecodeString(iv)
  49. if err != nil {
  50. return nil, err
  51. }
  52. decodeData, err := base64.StdEncoding.DecodeString(encrypted_data)
  53. if err != nil {
  54. return nil, err
  55. }
  56. dataBytes, err := AesDecrypt(decodeData, aesKey, ivKey)
  57. if err != nil {
  58. return nil, err
  59. }
  60. var result map[string]interface{}
  61. err = json.Unmarshal(dataBytes, &result)
  62. watermark := result["watermark"].(map[string]interface{})
  63. if watermark["appid"] != app_id {
  64. return nil, errors.New("Invalid appid data!")
  65. }
  66. return result, err
  67. }
  68. func AesDecrypt(crypted, key, iv []byte) ([]byte, error) {
  69. block, err := aes.NewCipher(key)
  70. fmt.Println("w2222", err)
  71. if err != nil {
  72. return nil, err
  73. }
  74. blockMode := cipher.NewCBCDecrypter(block, iv)
  75. origData := make([]byte, len(crypted))
  76. blockMode.CryptBlocks(origData, crypted)
  77. // 去除填充
  78. length := len(origData)
  79. unp := int(origData[length-1])
  80. return origData[:(length - unp)], nil
  81. }
  82. func GetFirstDateOfWeek() (weekMonday string) {
  83. now := time.Now()
  84. offset := int(time.Monday - now.Weekday())
  85. if offset > 0 {
  86. offset = -6
  87. }
  88. weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  89. weekMonday = weekStartDate.Format("2006-01-02")
  90. return
  91. }
  92. func GetWeekDayOfWeek() (weekMonday string) {
  93. now := time.Now()
  94. offset := int(time.Monday - now.Weekday())
  95. if offset > 0 {
  96. offset = -6
  97. }
  98. weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  99. weekMonday = weekStartDate.Format("2006-01-02")
  100. return
  101. }
  102. func GetMobilePatient(mobile string) (*models.XcxPatients, error) {
  103. patient := models.XcxPatients{}
  104. err := XTReadDB().Model(&patient).Where("id_card_no = ? and status = 1", mobile).Find(&patient).Error
  105. if err == gorm.ErrRecordNotFound {
  106. return nil, err
  107. }
  108. if err != nil {
  109. return nil, err
  110. }
  111. return &patient, nil
  112. }
  113. func Dncrypt(rawData, key, iv string) (string, error) {
  114. data, err := base64.StdEncoding.DecodeString(rawData)
  115. key_b, err_1 := base64.StdEncoding.DecodeString(key)
  116. iv_b, _ := base64.StdEncoding.DecodeString(iv)
  117. if err != nil {
  118. return "", err
  119. }
  120. if err_1 != nil {
  121. return "", err_1
  122. }
  123. dnData, err := AesCBCDncrypt(data, key_b, iv_b)
  124. if err != nil {
  125. return "", err
  126. }
  127. return string(dnData), nil
  128. }
  129. // 解密
  130. func AesCBCDncrypt(encryptData, key, iv []byte) ([]byte, error) {
  131. block, err := aes.NewCipher(key)
  132. if err != nil {
  133. panic(err)
  134. }
  135. blockSize := block.BlockSize()
  136. if len(encryptData) < blockSize {
  137. panic("ciphertext too short")
  138. }
  139. if len(encryptData)%blockSize != 0 {
  140. panic("ciphertext is not a multiple of the block size")
  141. }
  142. mode := cipher.NewCBCDecrypter(block, iv)
  143. mode.CryptBlocks(encryptData, encryptData)
  144. // 解填充
  145. encryptData = PKCS7UnPadding(encryptData)
  146. return encryptData, nil
  147. }
  148. //去除填充
  149. func PKCS7UnPadding(origData []byte) []byte {
  150. length := len(origData)
  151. unpadding := int(origData[length-1])
  152. if length-unpadding < 0 {
  153. return origData[:0]
  154. }
  155. fmt.Println("changdu", length)
  156. fmt.Println("data", unpadding)
  157. return origData[:(length - unpadding)]
  158. }
  159. func GetScheduleInfo(startime int64, endtime int64, patient_id int64) (schedule []*models.VmBloodSchedule, err error) {
  160. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  161. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  162. fmt.Println(table)
  163. if startime > 0 {
  164. db = db.Where("x.schedule_date >=?", startime)
  165. }
  166. if endtime > 0 {
  167. db = db.Where("x.schedule_date <=?", endtime)
  168. }
  169. if patient_id > 0 {
  170. db = db.Where("x.patient_id = ?", patient_id)
  171. }
  172. 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
  173. return schedule, err
  174. }
  175. func GetTodayDialysis(recordDate int64, patient_id int64) (models.XcXDialysisOrder, error) {
  176. order := models.XcXDialysisOrder{}
  177. err := XTReadDB().Model(&order).Where("dialysis_date = ? and patient_id = ? and status = 1", recordDate, patient_id).Find(&order).Error
  178. return order, err
  179. }
  180. func GetTodayPrescription(recordDate int64, patient_id int64) (models.XcxXtDialysisPrescription, error) {
  181. prescription := models.XcxXtDialysisPrescription{}
  182. err := XTReadDB().Model(&prescription).Where("record_date = ? and patient_id = ? and status = 1", recordDate, patient_id).Find(&prescription).Error
  183. return prescription, err
  184. }
  185. func GetNextPatientSchedule(patient_id int64, dialysis_date int64) (models.BloodSchedule, error) {
  186. schedule := models.BloodSchedule{}
  187. //err := XTReadDB().Model(&schedule).Where("patient_id = ? and status = 1 and schedule_date>?", patient_id, dialysis_date).Order("schedule_date asc").First(&schedule).Error
  188. //return schedule, err
  189. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  190. table := XTReadDB().Table("xt_device_zone as z").Where("z.status =1")
  191. tables := XTReadDB().Table("xt_device_number as n").Where("n.status =1")
  192. fmt.Println(table, tables)
  193. if patient_id > 0 {
  194. db = db.Where("x.patient_id = ?", patient_id)
  195. }
  196. if dialysis_date > 0 {
  197. db = db.Where("x.schedule_date >?", dialysis_date)
  198. }
  199. 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
  200. return schedule, err
  201. }
  202. func GetXcxPatientInfo(patient_id int64) (models.XcxPatients, error) {
  203. patients := models.XcxPatients{}
  204. err := XTReadDB().Model(&patients).Where("id = ? and status = 1", patient_id).Find(&patients).Error
  205. return patients, err
  206. }
  207. func GetOrgInfo(id int64) (models.XcxSgjUserOrg, error) {
  208. org := models.XcxSgjUserOrg{}
  209. err := UserReadDB().Model(&org).Where("id = ? and status= 1", id).Find(&org).Error
  210. return org, err
  211. }
  212. func GetEducationList(startime int64, endtime int64, patient_id int64) (treatment []*models.XcxTreatmentSummary, err error) {
  213. 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
  214. return treatment, err
  215. }
  216. func GetDoctorAdviceConfig(orgid int64) (models.XcxDoctorAdviceConfig, error) {
  217. config := models.XcxDoctorAdviceConfig{}
  218. err := XTReadDB().Model(&config).Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  219. return config, err
  220. }
  221. func GetDoctorAdviceGroup(patient_id int64, startime int64, endtime int64) (advice []*models.XcxDoctorAdvice, err error) {
  222. 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
  223. return advice, err
  224. }
  225. func GetHisDoctorAdviceGroup(patient_id int64, startime int64, endtime int64) (advice []*models.XcxHisDoctorAdviceInfo, err error) {
  226. 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
  227. return advice, err
  228. }
  229. func GetDoctorAdvice(patient_id int64, startime int64, endtime int64) (advice []*models.XcxDoctorAdvice, err error) {
  230. err = XTReadDB().Where("patient_id = ? and advice_date >=? and advice_date<=? and status = 1", patient_id, startime, endtime).Find(&advice).Error
  231. return advice, err
  232. }
  233. func GetHisDoctorAdvice(patient_id int64, startime int64, endtime int64) (advice []*models.XcxHisDoctorAdviceInfo, err error) {
  234. err = XTReadDB().Where("patient_id = ? and advice_date >=? and advice_date<=? and status = 1", patient_id, startime, endtime).Find(&advice).Error
  235. return advice, err
  236. }
  237. // 用户排班信息
  238. func MobileGetSchedualDetailOne(orgID int64, patientID int64) (*MDialysisScheduleVM, error) {
  239. var vm MDialysisScheduleVM
  240. err := readDb.
  241. Table("xt_schedule").
  242. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  243. Where("status = 1 AND user_org_id = ? AND patient_id = ?", orgID, patientID).
  244. First(&vm).Error
  245. if err != nil {
  246. if err == gorm.ErrRecordNotFound {
  247. return nil, nil
  248. } else {
  249. return nil, err
  250. }
  251. }
  252. return &vm, err
  253. }
  254. // 接诊评估
  255. func MobileGetReceiverTreatmentAccessRecordOne(orgID int64, patientID int64) (*models.ReceiveTreatmentAsses, error) {
  256. var record models.ReceiveTreatmentAsses
  257. err = readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  258. if err != nil {
  259. if err == gorm.ErrRecordNotFound {
  260. return nil, nil
  261. } else {
  262. return nil, err
  263. }
  264. }
  265. return &record, nil
  266. }
  267. // 透前评估
  268. func MobileGetPredialysisEvaluationOne(orgID int64, patientID int64) (*models.PredialysisEvaluation, error) {
  269. var record models.PredialysisEvaluation
  270. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  271. if err != nil {
  272. if err == gorm.ErrRecordNotFound {
  273. return nil, nil
  274. } else {
  275. return nil, err
  276. }
  277. }
  278. return &record, nil
  279. }
  280. // 获取 maxDate 之前一次的透前评估记录
  281. func MobileGetLastTimePredialysisEvaluationOne(orgID int64, patientID int64) (*models.PredialysisEvaluation, error) {
  282. var record models.PredialysisEvaluation
  283. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Order("assessment_date desc").Last(&record).Error
  284. if err != nil {
  285. if err == gorm.ErrRecordNotFound {
  286. return nil, nil
  287. } else {
  288. return nil, err
  289. }
  290. }
  291. return &record, nil
  292. }
  293. func MobileGetDoctorAdvicesByGroupsOne(orgID int64, patientID int64) ([]*models.DoctorAdvice, error) {
  294. var records []*models.DoctorAdvice
  295. err := readDb.
  296. Model(&models.DoctorAdvice{}).
  297. Where("patient_id = ? and user_org_id = ? and status = 1 and (advice_type = 2 || advice_type = 3)", patientID, orgID).
  298. 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").
  299. Order("start_time asc, groupno desc, advice_order desc, id asc").
  300. Scan(&records).Error
  301. if err != nil {
  302. return nil, err
  303. }
  304. return records, nil
  305. }
  306. // 透析记录
  307. func MobileGetSchedualDialysisRecordOne(orgID int64, patientID int64) (*models.DialysisOrder, error) {
  308. var record models.DialysisOrder
  309. err := readDb.Model(&models.DialysisOrder{}).Preload("DeviceNumber", "org_id = ? AND status = 1", orgID).Where("user_org_id = ? AND patient_id = ?", orgID, patientID).Last(&record).Error
  310. if err != nil {
  311. if err == gorm.ErrRecordNotFound {
  312. return nil, nil
  313. } else {
  314. return nil, err
  315. }
  316. }
  317. return &record, nil
  318. }
  319. // 双人核对
  320. func MobileGetDoubleCheckOne(orgID int64, patientID int64) (*models.DoubleCheck, error) {
  321. var record models.DoubleCheck
  322. err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  323. if err != nil {
  324. if err == gorm.ErrRecordNotFound {
  325. return nil, nil
  326. } else {
  327. return nil, err
  328. }
  329. }
  330. return &record, nil
  331. }
  332. // 透析监测记录
  333. func MobileGetMonitorRecordsOne(orgID int64, patientID int64) ([]*models.MonitoringRecord, error) {
  334. var records []*models.MonitoringRecord
  335. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Order("operate_time asc").Last(&records).Error
  336. if err != nil {
  337. return nil, err
  338. }
  339. return records, nil
  340. }
  341. func MobileGetLastMonitorRecordOne(orgID int64, patientID int64) (*models.MonitoringRecord, error) {
  342. var record models.MonitoringRecord
  343. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Order("operate_time desc").Last(&record).Error
  344. if err != nil {
  345. if err == gorm.ErrRecordNotFound {
  346. return nil, nil
  347. } else {
  348. return nil, err
  349. }
  350. }
  351. return &record, nil
  352. }
  353. // 透后评估
  354. func MobileGetAssessmentAfterDislysisOne(orgID int64, patientID int64) (*models.AssessmentAfterDislysis, error) {
  355. var record models.AssessmentAfterDislysis
  356. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  357. if err != nil {
  358. if err == gorm.ErrRecordNotFound {
  359. return nil, nil
  360. } else {
  361. return nil, err
  362. }
  363. }
  364. return &record, nil
  365. }
  366. // 治疗小结
  367. func MobileGetTreatmentSummaryOne(orgID int64, patientID int64) (*models.TreatmentSummary, error) {
  368. var record models.TreatmentSummary
  369. err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  370. if err != nil {
  371. if err == gorm.ErrRecordNotFound {
  372. return nil, nil
  373. } else {
  374. return nil, err
  375. }
  376. }
  377. return &record, nil
  378. }
  379. // 透析处方
  380. func MobileGetDialysisPrescribeByModeIdSix(orgID int64, patientID int64, mode_id int64) (*models.DialysisPrescription, error) {
  381. var record models.DialysisPrescription
  382. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  383. if err != nil {
  384. if err == gorm.ErrRecordNotFound {
  385. return nil, nil
  386. } else {
  387. return nil, err
  388. }
  389. }
  390. return &record, nil
  391. }
  392. func GetHisPrescriptionProjectsOne(user_org_id int64, patient_id int64) (projects []*models.HisPrescriptionProject, err error) {
  393. 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
  394. return
  395. }
  396. func GetDialyStockOutOne(orgid int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  397. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  398. if orgid > 0 {
  399. db = db.Where("x.user_org_id = ?", orgid)
  400. }
  401. if patientId > 0 {
  402. db = db.Where("x.patient_id = ?", patientId)
  403. }
  404. 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
  405. return prepare, err
  406. }
  407. func MobileGetDialysisPrescribeByModeIdFive(orgID int64, patientID int64) (*models.DialysisPrescription, error) {
  408. var record models.DialysisPrescription
  409. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  410. if err != nil {
  411. if err == gorm.ErrRecordNotFound {
  412. return nil, nil
  413. } else {
  414. return nil, err
  415. }
  416. }
  417. return &record, nil
  418. }
  419. func GetAllHisDoctorAdviceOne(orgid int64, patientid int64) (his []*models.HisDoctorAdviceInfo, err error) {
  420. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? AND user_org_id = ? AND status=1", patientid, orgid).Last(&his).Error
  421. return
  422. }
  423. func GetDoctorName(orgid int64) (role []*models.XcxSgjUserAdminRole, err error) {
  424. err = UserReadDB().Where("org_id = ? and status = 1", orgid).Find(&role).Error
  425. return role, err
  426. }