doctor_advice_service.go 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. package service
  2. import (
  3. "strings"
  4. "time"
  5. "XT_New/models"
  6. "github.com/jinzhu/gorm"
  7. )
  8. func GetPublicDrugDics() (drugs []*models.DrugDic, total int64, err error) {
  9. err = readDb.Model(&models.DrugDic{}).Where("org_id = 0 and status=1").Count(&total).Order("id desc").Find(&drugs).Error
  10. return
  11. }
  12. func GetPublicDrugWayDics() (drugways []*models.DrugwayDic, total int64, err error) {
  13. err = readDb.Model(&models.DrugwayDic{}).Where("org_id = 0 and status=1").Count(&total).Order("id desc").Find(&drugways).Error
  14. return
  15. }
  16. func GetPublicExecutionFrequencyDics() (dics []*models.ExecutionFrequencyDic, total int64, err error) {
  17. err = readDb.Model(&models.ExecutionFrequencyDic{}).Where("org_id = 0 and status=1").Count(&total).Order("id desc").Find(&dics).Error
  18. return
  19. }
  20. func GetDrugDics(orgid int64) (drugs []*models.DrugDic, total int64, err error) {
  21. err = readDb.Model(&models.DrugDic{}).Where("org_id =? and status=1", orgid).Count(&total).Order("id desc").Find(&drugs).Error
  22. return
  23. }
  24. func GetAdviceTemplates(orgid int64) (templates []*models.DoctorAdviceTemplate, total int64, err error) {
  25. err = readDb.Model(&models.DoctorAdviceTemplate{}).Where("org_id =? and status=1", orgid).Count(&total).Order("id desc").Find(&templates).Error
  26. return
  27. }
  28. func GetDrugWayDics(orgid int64) (drugways []*models.DrugwayDic, total int64, err error) {
  29. err = readDb.Model(&models.DrugwayDic{}).Where("(org_id =? or org_id = 0) and status=1", orgid).Count(&total).Order("id desc").Find(&drugways).Error
  30. return
  31. }
  32. func GetExecutionFrequencyDics(orgid int64) (dics []*models.ExecutionFrequencyDic, total int64, err error) {
  33. err = readDb.Model(&models.ExecutionFrequencyDic{}).Where("(org_id =? or org_id = 0) and status=1", orgid).Count(&total).Order("id desc").Find(&dics).Error
  34. return
  35. }
  36. func CreateDrugDic(d *models.DrugDic) (err error) {
  37. err = readDb.Create(&d).Error
  38. return
  39. }
  40. func UpdateDrugDic(d *models.DrugDic) (err error) {
  41. err = readDb.Save(&d).Error
  42. return
  43. }
  44. func FindDrugDic(orgid, id int64) (*models.DrugDic, error) {
  45. var drugdic models.DrugDic
  46. err := readDb.Model(&models.DrugDic{}).Where("id=? and org_id =? and status=1", id, orgid).First(&drugdic).Error
  47. if err == gorm.ErrRecordNotFound {
  48. return nil, nil
  49. }
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &drugdic, nil
  54. }
  55. func CreateDrugWay(d *models.DrugwayDic) (err error) {
  56. err = readDb.Create(&d).Error
  57. return
  58. }
  59. func FindDrugWay(orgid, id int64) (*models.DrugwayDic, error) {
  60. var drugway models.DrugwayDic
  61. err := readDb.Model(&models.DrugwayDic{}).Where("id=? and org_id =? and status=1", id, orgid).First(&drugway).Error
  62. if err == gorm.ErrRecordNotFound {
  63. return nil, nil
  64. }
  65. if err != nil {
  66. return nil, err
  67. }
  68. return &drugway, nil
  69. }
  70. func UpdateDrugWay(d *models.DrugwayDic) (err error) {
  71. err = readDb.Save(&d).Error
  72. return
  73. }
  74. func CreateExecutionFrequency(d *models.ExecutionFrequencyDic) (err error) {
  75. err = readDb.Create(&d).Error
  76. return
  77. }
  78. func FindExecutionFrequency(orgid, id int64) (*models.ExecutionFrequencyDic, error) {
  79. var ef models.ExecutionFrequencyDic
  80. err := readDb.Model(&models.ExecutionFrequencyDic{}).Where("id=? and org_id =? and status=1", id, orgid).First(&ef).Error
  81. if err == gorm.ErrRecordNotFound {
  82. return nil, nil
  83. }
  84. if err != nil {
  85. return nil, err
  86. }
  87. return &ef, nil
  88. }
  89. func UpdateExecutionFrequency(d *models.ExecutionFrequencyDic) (err error) {
  90. err = readDb.Save(&d).Error
  91. return
  92. }
  93. func CreateAdviceTemplate(d *models.DoctorAdviceTemplate) (err error) {
  94. err = readDb.Create(&d).Error
  95. return
  96. }
  97. func CreateHisAdviceTemplate(d *models.HisDoctorAdviceTemplate) (err error) {
  98. err = readDb.Create(&d).Error
  99. return
  100. }
  101. func FindAdviceTemplate(orgid, id int64) (*models.DoctorAdviceTemplate, error) {
  102. var template models.DoctorAdviceTemplate
  103. err := readDb.Model(&models.DoctorAdviceTemplate{}).Where("id=? and org_id =? and status=1", id, orgid).First(&template).Error
  104. if err == gorm.ErrRecordNotFound {
  105. return nil, nil
  106. }
  107. if err != nil {
  108. return nil, err
  109. }
  110. return &template, nil
  111. }
  112. func FindHisAdviceTemplate(orgid, id int64) (*models.HisDoctorAdviceTemplate, error) {
  113. var template models.HisDoctorAdviceTemplate
  114. err := readDb.Model(&models.HisDoctorAdviceTemplate{}).Where("id=? and org_id =? and status=1", id, orgid).First(&template).Error
  115. if err == gorm.ErrRecordNotFound {
  116. return nil, nil
  117. }
  118. if err != nil {
  119. return nil, err
  120. }
  121. return &template, nil
  122. }
  123. func UpdateAdviceTemplate(d *models.DoctorAdviceTemplate) (err error) {
  124. err = writeDb.Save(&d).Error
  125. return
  126. }
  127. func UpdateHisAdviceTemplate(d *models.HisDoctorAdviceTemplate) (err error) {
  128. err = writeDb.Save(&d).Error
  129. return
  130. }
  131. func UpdateAdviceAndSubAdviceTemplate(d *models.DoctorAdviceTemplate) (err error) {
  132. err = writeDb.Save(&d).Error
  133. err = writeDb.Model(&models.DoctorAdviceTemplate{}).Where("status=1 AND parent_id = ?", d.ID).Updates(map[string]interface{}{"status": 2, "updated_time": time.Now().Unix()}).Error
  134. return
  135. }
  136. func UpdateHisAdviceAndSubAdviceTemplate(d *models.HisDoctorAdviceTemplate) (err error) {
  137. err = writeDb.Save(&d).Error
  138. err = writeDb.Model(&models.HisDoctorAdviceTemplate{}).Where("status=1 AND parent_id = ?", d.ID).Updates(map[string]interface{}{"status": 2, "updated_time": time.Now().Unix()}).Error
  139. return
  140. }
  141. func CreateTemplate(template *models.DoctorAdviceParentTemplate) (err error) {
  142. err = readDb.Create(&template).Error
  143. return
  144. }
  145. func CreateHisDoctorAdviceTemplate(template *models.HisDoctorAdviceParentTemplate) (err error) {
  146. err = readDb.Create(&template).Error
  147. return
  148. }
  149. func CreateHisSubDoctorAdviceTemplate(template []*models.HisDoctorAdviceTemplate) (err error) {
  150. if len(template) > 0 {
  151. utx := writeDb.Begin()
  152. if len(template) > 0 {
  153. thisSQL := "INSERT INTO his_doctor_advice_template (org_id, advice_name, advice_desc, single_dose, single_dose_unit,prescribing_number," +
  154. "prescribing_number_unit,delivery_way,execution_frequency,advice_doctor,status,created_time,updated_time,template_id,drug_spec,drug_spec_unit,advice_type,day_count,week_days,frequency_type,drug_id,way) VALUES "
  155. insertParams := make([]string, 0)
  156. insertData := make([]interface{}, 0)
  157. for _, info := range template {
  158. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  159. insertData = append(insertData, info.OrgId)
  160. insertData = append(insertData, info.AdviceName)
  161. insertData = append(insertData, info.AdviceDesc)
  162. insertData = append(insertData, info.SingleDose)
  163. insertData = append(insertData, info.SingleDoseUnit)
  164. insertData = append(insertData, info.PrescribingNumber)
  165. insertData = append(insertData, info.PrescribingNumberUnit)
  166. insertData = append(insertData, info.DeliveryWay)
  167. insertData = append(insertData, info.ExecutionFrequency)
  168. insertData = append(insertData, info.AdviceDoctor)
  169. insertData = append(insertData, info.Status)
  170. insertData = append(insertData, info.CreatedTime)
  171. insertData = append(insertData, info.UpdatedTime)
  172. insertData = append(insertData, info.TemplateId)
  173. insertData = append(insertData, info.DrugSpec)
  174. insertData = append(insertData, info.DrugSpecUnit)
  175. insertData = append(insertData, info.AdviceType)
  176. insertData = append(insertData, info.DayCount)
  177. insertData = append(insertData, info.WeekDays)
  178. insertData = append(insertData, info.FrequencyType)
  179. insertData = append(insertData, info.DrugId)
  180. insertData = append(insertData, info.Way)
  181. }
  182. thisSQL += strings.Join(insertParams, ", ")
  183. err = utx.Exec(thisSQL, insertData...).Error
  184. if err != nil {
  185. utx.Rollback()
  186. return
  187. }
  188. }
  189. utx.Commit()
  190. }
  191. return
  192. }
  193. //func CreateBatchRecord(template []*models.DoctorAdviceTemplate) (err error) {
  194. // err = writeDb.Create(template).Error
  195. // return
  196. //}
  197. func CreateSubTemplate(template []*models.DoctorAdviceTemplate) (err error) {
  198. if len(template) > 0 {
  199. utx := writeDb.Begin()
  200. if len(template) > 0 {
  201. thisSQL := "INSERT INTO xt_doctor_advice_template (org_id, advice_name, advice_desc, single_dose, single_dose_unit,prescribing_number," +
  202. "prescribing_number_unit,delivery_way,execution_frequency,advice_doctor,status,created_time,updated_time,template_id,drug_spec,drug_spec_unit,advice_type,day_count,week_days,frequency_type,drug_id,way,remark) VALUES "
  203. insertParams := make([]string, 0)
  204. insertData := make([]interface{}, 0)
  205. for _, info := range template {
  206. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  207. insertData = append(insertData, info.OrgId)
  208. insertData = append(insertData, info.AdviceName)
  209. insertData = append(insertData, info.AdviceDesc)
  210. insertData = append(insertData, info.SingleDose)
  211. insertData = append(insertData, info.SingleDoseUnit)
  212. insertData = append(insertData, info.PrescribingNumber)
  213. insertData = append(insertData, info.PrescribingNumberUnit)
  214. insertData = append(insertData, info.DeliveryWay)
  215. insertData = append(insertData, info.ExecutionFrequency)
  216. insertData = append(insertData, info.AdviceDoctor)
  217. insertData = append(insertData, info.Status)
  218. insertData = append(insertData, info.CreatedTime)
  219. insertData = append(insertData, info.UpdatedTime)
  220. insertData = append(insertData, info.TemplateId)
  221. insertData = append(insertData, info.DrugSpec)
  222. insertData = append(insertData, info.DrugSpecUnit)
  223. insertData = append(insertData, info.AdviceType)
  224. insertData = append(insertData, info.DayCount)
  225. insertData = append(insertData, info.WeekDays)
  226. insertData = append(insertData, info.FrequencyType)
  227. insertData = append(insertData, info.DrugId)
  228. insertData = append(insertData, info.Way)
  229. insertData = append(insertData, info.Remark)
  230. }
  231. thisSQL += strings.Join(insertParams, ", ")
  232. err = utx.Exec(thisSQL, insertData...).Error
  233. if err != nil {
  234. utx.Rollback()
  235. return
  236. }
  237. }
  238. utx.Commit()
  239. }
  240. return
  241. }
  242. func FindDoctorAdviceTemplateById(id int64, org_id int64) (temps models.DoctorAdviceParentTemplate, err error) {
  243. err = readDb.Model(&models.DoctorAdviceParentTemplate{}).Preload("DoctorAdviceTemplate", "status = 1").Where("org_id = ? AND status=1 AND id = ?", org_id, id).Order("id desc").First(&temps).Error
  244. return
  245. }
  246. func FindHisDoctorAdviceTemplateById(id int64, org_id int64) (temps models.HisDoctorAdviceParentTemplate, err error) {
  247. err = readDb.Model(&models.HisDoctorAdviceParentTemplate{}).Preload("HisDoctorAdviceTemplate", "status = 1").Where("org_id = ? AND status=1 AND id = ?", org_id, id).Order("id desc").First(&temps).Error
  248. return
  249. }
  250. func FindAllAdviceTemplate(org_id int64, advice_type int64) (temps []*models.DoctorAdviceParentTemplate, err error) {
  251. err = readDb.Model(&models.DoctorAdviceParentTemplate{}).Preload("DoctorAdviceTemplate", func(db *gorm.DB) *gorm.DB {
  252. return db.Select("id,advice_name,advice_desc,single_dose,single_dose_unit,prescribing_number,prescribing_number_unit,delivery_way,execution_frequency,status,created_time,updated_time,parent_id,template_id,drug_spec,drug_spec_unit,advice_type,day_count,week_days,frequency_type,way,drug_id,drug_name_id, IF(parent_id>0, parent_id, id) as advice_order").Where("status = 1").Order("advice_order desc, id")
  253. }).Where("org_id = ? AND status=1 AND advice_type = ?", org_id, advice_type).Find(&temps).Error
  254. return
  255. }
  256. func GetAllAdviceSubTemplates(orgid int64, template_id int64) (total int64, err error) {
  257. err = readDb.Model(&models.DoctorAdviceTemplate{}).Where("org_id =? AND status=1 AND template_id = ?", orgid, template_id).Count(&total).Error
  258. return
  259. }
  260. func UpdateParentAdviceTemplates(orgid int64, tempelate_id int64) (err error) {
  261. err = readDb.Model(&models.DoctorAdviceParentTemplate{}).Where("org_id =? AND status=1 AND id = ?", orgid, tempelate_id).Updates(map[string]interface{}{"status": "0", "updated_time": time.Now().Unix()}).Error
  262. return
  263. }
  264. func FindTemplateRecordByName(orgid int64, name string) (total int64) {
  265. readDb.Model(&models.DoctorAdviceParentTemplate{}).Where("name = ? AND org_id = ? AND status= 1 ", name, orgid).Count(&total)
  266. return
  267. }
  268. func FindParentTemplateRecordById(orgid int64, template_id int64) (parentTemplate models.DoctorAdviceParentTemplate, err error) {
  269. err = readDb.Model(&models.DoctorAdviceParentTemplate{}).Where("id = ? AND org_id = ? AND status= 1 ", template_id, orgid).First(&parentTemplate).Error
  270. return
  271. }
  272. func FindParentHisTemplateRecordById(orgid int64, template_id int64) (parentTemplate models.HisDoctorAdviceParentTemplate, err error) {
  273. err = readDb.Model(&models.HisDoctorAdviceParentTemplate{}).Where("id = ? AND org_id = ? AND status= 1 ", template_id, orgid).First(&parentTemplate).Error
  274. return
  275. }
  276. func DeleteParentDoctorAdviceByTemplateId(template_id int64, org_id int64) (err error) {
  277. ut := writeDb.Begin()
  278. err = writeDb.Model(&models.DoctorAdviceParentTemplate{}).Where("status=1 AND id = ? AND org_id = ?", template_id, org_id).Updates(map[string]interface{}{"status": "0", "updated_time": time.Now().Unix()}).Error
  279. if err != nil {
  280. ut.Rollback()
  281. return
  282. }
  283. err = writeDb.Model(&models.DoctorAdviceTemplate{}).Where("status=1 AND template_id = ? AND org_id = ?", template_id, org_id).Updates(map[string]interface{}{"status": "2", "updated_time": time.Now().Unix()}).Error
  284. if err != nil {
  285. ut.Rollback()
  286. return
  287. }
  288. ut.Commit()
  289. return err
  290. }
  291. func DeleteHisParentDoctorAdviceByTemplateId(template_id int64, org_id int64) (err error) {
  292. ut := writeDb.Begin()
  293. err = writeDb.Model(&models.HisDoctorAdviceParentTemplate{}).Where("status=1 AND id = ? AND org_id = ?", template_id, org_id).Updates(map[string]interface{}{"status": "0", "updated_time": time.Now().Unix()}).Error
  294. if err != nil {
  295. ut.Rollback()
  296. return
  297. }
  298. err = writeDb.Model(&models.HisDoctorAdviceTemplate{}).Where("status=1 AND template_id = ? AND org_id = ?", template_id, org_id).Updates(map[string]interface{}{"status": "2", "updated_time": time.Now().Unix()}).Error
  299. if err != nil {
  300. ut.Rollback()
  301. return
  302. }
  303. ut.Commit()
  304. return err
  305. }
  306. func ModifyTemplateName(id int64, template_name string) (err error) {
  307. err = writeDb.Model(&models.DoctorAdviceParentTemplate{}).Where("status=1 AND id = ?", id).Updates(map[string]interface{}{"name": template_name, "updated_time": time.Now().Unix()}).Error
  308. return err
  309. }
  310. func ModifyTemplateNameOne(id int64, template_name string, sort int64) (err error) {
  311. err = writeDb.Model(&models.DoctorAdviceParentTemplate{}).Where("status=1 AND id = ?", id).Updates(map[string]interface{}{"name": template_name, "updated_time": time.Now().Unix(), "sort": sort}).Error
  312. return err
  313. }
  314. func ModifyHisTemplateName(id int64, template_name string) (err error) {
  315. err = writeDb.Model(&models.HisDoctorAdviceParentTemplate{}).Where("status=1 AND id = ?", id).Updates(map[string]interface{}{"name": template_name, "updated_time": time.Now().Unix()}).Error
  316. return err
  317. }
  318. func FindOtherAllAdviceTemplate(org_id int64) (temps []*models.DoctorAdviceParentTemplate, err error) {
  319. err = readDb.Model(&models.DoctorAdviceParentTemplate{}).Preload("DoctorAdviceTemplate", func(db *gorm.DB) *gorm.DB {
  320. return db.Select("id,advice_name,advice_desc,single_dose,single_dose_unit,prescribing_number,prescribing_number_unit,delivery_way,execution_frequency,status,created_time,updated_time,parent_id,template_id,drug_spec,drug_spec_unit,advice_type,day_count,week_days,frequency_type,drug_id,way,remark, IF(parent_id>0, parent_id, id) as advice_order").Where("status = 1").Order("advice_order desc, id")
  321. }).Where("org_id = ? AND status=1 ", org_id).Find(&temps).Error
  322. return
  323. //.Scan(&advices).Error
  324. }
  325. func FindHisAllAdviceTemplate(org_id int64) (temps []*models.HisDoctorAdviceParentTemplate, err error) {
  326. err = readDb.Model(&models.HisDoctorAdviceParentTemplate{}).Preload("HisDoctorAdviceTemplate", func(db *gorm.DB) *gorm.DB {
  327. return db.Select("id,advice_name,advice_desc,single_dose,single_dose_unit,prescribing_number,prescribing_number_unit,delivery_way,execution_frequency,status,created_time,updated_time,parent_id,template_id,drug_spec,drug_spec_unit,advice_type,day_count,week_days,frequency_type,drug_id,way, IF(parent_id>0, parent_id, id) as advice_order").Where("status = 1").Order("advice_order desc, id")
  328. }).Where("org_id = ? AND status=1 ", org_id).Find(&temps).Error
  329. return
  330. //.Scan(&advices).Error
  331. }
  332. func GetHandleData(id int64) (models.ConfigViewModel, error) {
  333. config := models.ConfigViewModel{}
  334. err := readDb.Model(&config).Where("id=?", id).Find(&config).Error
  335. return config, err
  336. }
  337. func UpdateDataTwo(id int64, model models.ConfigViewModel) error {
  338. err := writeDb.Model(&model).Where("id=?", id).Update(map[string]interface{}{"name": model.Name, "orders": model.Order, "remark": model.Remark, "update_time": time.Now().Unix()}).Error
  339. return err
  340. }
  341. type AdviceDate struct {
  342. RecordDate int64
  343. }
  344. type DoctorAdvice struct {
  345. ID int64 `gorm:"column:id" json:"id" form:"id"`
  346. GroupNo int64 `gorm:"column:groupno" json:"groupno" form:"groupno"`
  347. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  348. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  349. AdviceType int64 `gorm:"column:advice_type" json:"advice_type" form:"advice_type"`
  350. AdviceDate int64 `gorm:"column:advice_date" json:"advice_date" form:"advice_date"`
  351. StartTime int64 `gorm:"column:start_time" json:"start_time" form:"start_time"`
  352. AdviceName string `gorm:"column:advice_name" json:"advice_name" form:"advice_name"`
  353. AdviceDesc string `gorm:"column:advice_desc" json:"advice_desc" form:"advice_desc"`
  354. ReminderDate int64 `gorm:"column:reminder_date" json:"reminder_date" form:"reminder_date"`
  355. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  356. SingleDoseUnit string `gorm:"column:single_dose_unit" json:"single_dose_unit" form:"single_dose_unit"`
  357. DrugSpec float64 `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  358. DrugSpecUnit string `gorm:"column:drug_spec_unit" json:"drug_spec_unit" form:"drug_spec_unit"`
  359. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  360. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  361. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  362. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  363. AdviceDoctor int64 `gorm:"column:advice_doctor" json:"advice_doctor" form:"advice_doctor"`
  364. Status int64 `gorm:"column:status" json:"status" form:"status"`
  365. CreatedTime int64 `gorm:"column:created_time" json:"created_time" form:"created_time"`
  366. UpdatedTime int64 `gorm:"column:updated_time" json:"updated_time" form:"updated_time"`
  367. AdviceAffirm string `gorm:"column:advice_affirm" json:"advice_affirm" form:"advice_affirm"`
  368. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  369. StopTime int64 `gorm:"column:stop_time" json:"stop_time" form:"stop_time"`
  370. StopReason string `gorm:"column:stop_reason" json:"stop_reason" form:"stop_reason"`
  371. StopDoctor int64 `gorm:"column:stop_doctor" json:"stop_doctor" form:"stop_doctor"`
  372. StopState int64 `gorm:"column:stop_state" json:"stop_state" form:"stop_state"`
  373. ParentId int64 `gorm:"column:parent_id" json:"parent_id" form:"parent_id"`
  374. ExecutionTime int64 `gorm:"column:execution_time" json:"execution_time" form:"execution_time"`
  375. ExecutionStaff int64 `gorm:"column:execution_staff" json:"execution_staff" form:"execution_staff"`
  376. ExecutionState int64 `gorm:"column:execution_state" json:"execution_state" form:"execution_state"`
  377. Checker int64 `gorm:"column:checker" json:"checker" form:"checker"`
  378. RecordDate int64 `gorm:"column:record_date" json:"record_date"`
  379. DialysisOrderId int64 `gorm:"column:dialysis_order_id" json:"dialysis_order_id"`
  380. CheckTime int64 `gorm:"column:check_time" json:"check_time" form:"check_time"`
  381. CheckState int64 `gorm:"column:check_state" json:"check_state" form:"check_state"`
  382. AdviceId int64 `gorm:"-"`
  383. RemindType int64 `gorm:"column:remind_type" json:"remind_type"`
  384. FrequencyType int64 `gorm:"column:frequency_type" json:"frequency_type"`
  385. DayCount int64 `gorm:"column:day_count" json:"day_count"`
  386. WeekDay string `gorm:"column:week_day" json:"week_day"`
  387. DoctorAdvice []*DoctorAdvice `gorm:"ForeignKey:ParentId;AssociationForeignKey:ID" json:"children"`
  388. TemplateId string `gorm:"column:template_id" json:"template_id"`
  389. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  390. IsCheck int64 `gorm:"-" json:"is_check" form:"is_check"`
  391. Way int64 `gorm:"column:way" json:"way" form:"way"`
  392. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  393. DrugNameId int64 `gorm:"column:drug_name_id" json:"drug_name_id" form:"drug_name_id"`
  394. }
  395. func (DoctorAdvice) TableName() string {
  396. return "xt_doctor_advice"
  397. }
  398. func GetDoctorAdviceByType(change_type int64, record_time int64, org_id int64, patient_id int64) (advice []*DoctorAdvice, sch models.Schedule, err error) {
  399. if change_type == 1 { //根据日期取出上一方数据
  400. var Id AdviceDate
  401. err = readDb.Model(&DoctorAdvice{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND record_date < ?", patient_id, org_id, record_time).Select("record_date").Group("record_date").Order("record_date asc").Scan(&Id).Error
  402. err = readDb.Model(&models.Schedule{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND schedule_date = ?", patient_id, org_id, Id.RecordDate).Find(&sch).Error
  403. err = readDb.Model(&DoctorAdvice{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND record_date = ? AND parent_id= 0", patient_id, org_id, Id.RecordDate).Preload("DoctorAdvice", "status = 1 AND user_org_id = ? AND patient_id = ?", org_id, patient_id).Find(&advice).Error
  404. } else if change_type == 2 {
  405. var Id AdviceDate
  406. err = readDb.Model(&DoctorAdvice{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND record_date > ?", patient_id, org_id, record_time).Select("record_date").Group("record_date").Order("record_date desc").Scan(&Id).Error
  407. err = readDb.Model(&models.Schedule{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND schedule_date = ?", patient_id, org_id, Id.RecordDate).Find(&sch).Error
  408. err = readDb.Model(&DoctorAdvice{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND record_date = ? AND parent_id= 0", patient_id, org_id, Id.RecordDate).Preload("DoctorAdvice", "status = 1 AND user_org_id = ? AND patient_id = ?", org_id, patient_id).Find(&advice).Error
  409. }
  410. return
  411. }
  412. func GetDict(orgid int64) (drugways []*models.DrugwayDic, total int64, err error) {
  413. err = readDb.Model(&models.DrugwayDic{}).Where("(org_id =? or org_id = 0) and status=1", orgid).Count(&total).Order("id desc").Find(&drugways).Error
  414. return
  415. }
  416. func GetBloodDoctorAdvice(record_time int64, org_id int64, patient_id int64) (advice []*DoctorAdvice, err error) {
  417. err = XTReadDB().Where("advice_date = ? and user_org_id = ? and patient_id = ? and status = 1", record_time, org_id, patient_id).Order("created_time asc").Find(&advice).Error
  418. return advice, err
  419. }
  420. func GetHisDoctorAdvice(record_time int64, org_id int64, patient_id int64) (advice []*models.HisDoctorAdvice, err error) {
  421. err = XTReadDB().Where("advice_date = ? and user_org_id = ? and patient_id = ? and status = 1", record_time, org_id, patient_id).Order("created_time asc").Find(&advice).Error
  422. return advice, err
  423. }
  424. func GetPatientByName(keyword string, user_org_id int64) (patients []*models.XtPatients, err error) {
  425. db := XTReadDB().Model(&patients).Where("status=1")
  426. if len(keyword) > 0 {
  427. likeKey := "%" + keyword + "%"
  428. db = db.Where("name LIKE ? OR dialysis_no LIKE ?", likeKey, likeKey)
  429. }
  430. if user_org_id > 0 {
  431. db = db.Where("user_org_id =?", user_org_id)
  432. }
  433. err = db.Find(&patients).Error
  434. return patients, err
  435. }
  436. func GetDoctorAdviceByPatientId(patient_id int64, user_org_id int64, start_time int64, end_time int64, limit int64, page int64) (advice []*models.DoctorAdvice, total int64, err error) {
  437. db := XTReadDB().Model(&advice).Where("status=1")
  438. //offset := (page - 1) * limit
  439. if patient_id > 0 {
  440. db = db.Where("patient_id = ?", patient_id)
  441. }
  442. if user_org_id > 0 {
  443. db = db.Where("user_org_id =?", user_org_id)
  444. }
  445. if start_time > 0 {
  446. db = db.Where("advice_date >= ?", start_time)
  447. }
  448. if end_time > 0 {
  449. db = db.Where("advice_date <= ?", end_time)
  450. }
  451. err = db.Find(&advice).Error
  452. return advice, total, err
  453. }
  454. func GetDoctorAdivceMonthList(user_org_id int64, start_time int64, end_time int64) (list []*models.XtDoctorAdvice, err error) {
  455. db := XTReadDB().Model(models.XtDoctorAdvice{}).Where("status=1 and (advice_type =2 or advice_type =3)")
  456. if user_org_id > 0 {
  457. db = db.Where("user_org_id = ?", user_org_id)
  458. }
  459. if start_time > 0 {
  460. db = db.Where("advice_date >=?", start_time)
  461. }
  462. if end_time > 0 {
  463. db = db.Where("advice_date<=?", end_time)
  464. }
  465. err = db.Find(&list).Error
  466. return list, err
  467. }
  468. func GetHisDoctorAdivceMonthList(user_org_id int64, start_time int64, end_time int64) (list []*models.HisDoctorAdvice, err error) {
  469. db := XTReadDB().Model(models.HisDoctorAdvice{}).Where("status=1")
  470. if user_org_id > 0 {
  471. db = db.Where("user_org_id = ?", user_org_id)
  472. }
  473. if start_time > 0 {
  474. db = db.Where("advice_date >=?", start_time)
  475. }
  476. if end_time > 0 {
  477. db = db.Where("advice_date<=?", end_time)
  478. }
  479. err = db.Preload("Drug", "status=1 and org_id = ?", user_org_id).Find(&list).Error
  480. return list, err
  481. }
  482. func GetHisPrescriptionProjectMonthList(user_org_id int64, start_time int64, end_time int64) (project []*models.HisPrescriptionProject, err error) {
  483. db := XTReadDB().Model(&models.HisPrescriptionProject{}).Where("status=1 and type =3")
  484. if user_org_id > 0 {
  485. db = db.Where("user_org_id = ?", user_org_id)
  486. }
  487. if start_time > 0 {
  488. db = db.Where("record_date >=?", start_time)
  489. }
  490. if end_time > 0 {
  491. db = db.Where("record_date<=?", end_time)
  492. }
  493. err = db.Preload("GoodInfo", "org_id= ? and status=1", user_org_id).Find(&project).Error
  494. return project, err
  495. }