doctor_advice_service.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package service
  2. import (
  3. "Xcx_New/models"
  4. "strings"
  5. "time"
  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 FindAdviceTemplate(orgid, id int64) (*models.DoctorAdviceTemplate, error) {
  98. var template models.DoctorAdviceTemplate
  99. err := readDb.Model(&models.DoctorAdviceTemplate{}).Where("id=? and org_id =? and status=1", id, orgid).First(&template).Error
  100. if err == gorm.ErrRecordNotFound {
  101. return nil, nil
  102. }
  103. if err != nil {
  104. return nil, err
  105. }
  106. return &template, nil
  107. }
  108. func UpdateAdviceTemplate(d *models.DoctorAdviceTemplate) (err error) {
  109. err = writeDb.Save(&d).Error
  110. return
  111. }
  112. func UpdateAdviceAndSubAdviceTemplate(d *models.DoctorAdviceTemplate) (err error) {
  113. err = writeDb.Save(&d).Error
  114. 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
  115. return
  116. }
  117. func CreateTemplate(template *models.DoctorAdviceParentTemplate) (err error) {
  118. err = readDb.Create(&template).Error
  119. return
  120. }
  121. //func CreateBatchRecord(template []*models.DoctorAdviceTemplate) (err error) {
  122. // err = writeDb.Create(template).Error
  123. // return
  124. //}
  125. func CreateSubTemplate(template []*models.DoctorAdviceTemplate) (err error) {
  126. if len(template) > 0 {
  127. utx := writeDb.Begin()
  128. if len(template) > 0 {
  129. thisSQL := "INSERT INTO xt_doctor_advice_template (org_id, advice_name, advice_desc, single_dose, single_dose_unit,prescribing_number," +
  130. "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) VALUES "
  131. insertParams := make([]string, 0)
  132. insertData := make([]interface{}, 0)
  133. for _, info := range template {
  134. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  135. insertData = append(insertData, info.OrgId)
  136. insertData = append(insertData, info.AdviceName)
  137. insertData = append(insertData, info.AdviceDesc)
  138. insertData = append(insertData, info.SingleDose)
  139. insertData = append(insertData, info.SingleDoseUnit)
  140. insertData = append(insertData, info.PrescribingNumber)
  141. insertData = append(insertData, info.PrescribingNumberUnit)
  142. insertData = append(insertData, info.DeliveryWay)
  143. insertData = append(insertData, info.ExecutionFrequency)
  144. insertData = append(insertData, info.AdviceDoctor)
  145. insertData = append(insertData, info.Status)
  146. insertData = append(insertData, info.CreatedTime)
  147. insertData = append(insertData, info.UpdatedTime)
  148. insertData = append(insertData, info.TemplateId)
  149. insertData = append(insertData, info.DrugSpec)
  150. insertData = append(insertData, info.DrugSpecUnit)
  151. insertData = append(insertData, info.AdviceType)
  152. insertData = append(insertData, info.DayCount)
  153. insertData = append(insertData, info.WeekDays)
  154. insertData = append(insertData, info.FrequencyType)
  155. }
  156. thisSQL += strings.Join(insertParams, ", ")
  157. err = utx.Exec(thisSQL, insertData...).Error
  158. if err != nil {
  159. utx.Rollback()
  160. return
  161. }
  162. }
  163. utx.Commit()
  164. }
  165. return
  166. }
  167. func FindDoctorAdviceTemplateById(id int64, org_id int64) (temps models.DoctorAdviceParentTemplate, err error) {
  168. 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
  169. return
  170. }
  171. func FindAllAdviceTemplate(org_id int64, advice_type int64) (temps []*models.DoctorAdviceParentTemplate, err error) {
  172. err = readDb.Model(&models.DoctorAdviceParentTemplate{}).Preload("DoctorAdviceTemplate", func(db *gorm.DB) *gorm.DB {
  173. 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, IF(parent_id>0, parent_id, id) as advice_order").Where("status = 1").Order("advice_order desc, id")
  174. }).Where("org_id = ? AND status=1 AND advice_type = ?", org_id, advice_type).Find(&temps).Error
  175. return
  176. }
  177. func GetAllAdviceSubTemplates(orgid int64, template_id int64) (total int64, err error) {
  178. err = readDb.Model(&models.DoctorAdviceTemplate{}).Where("org_id =? AND status=1 AND template_id = ?", orgid, template_id).Count(&total).Error
  179. return
  180. }
  181. func UpdateParentAdviceTemplates(orgid int64, tempelate_id int64) (err error) {
  182. 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
  183. return
  184. }
  185. func FindTemplateRecordByName(orgid int64, name string) (total int64) {
  186. readDb.Model(&models.DoctorAdviceParentTemplate{}).Where("name = ? AND org_id = ? AND status= 1 ", name, orgid).Count(&total)
  187. return
  188. }
  189. func FindParentTemplateRecordById(orgid int64, template_id int64) (parentTemplate models.DoctorAdviceParentTemplate, err error) {
  190. err = readDb.Model(&models.DoctorAdviceParentTemplate{}).Where("id = ? AND org_id = ? AND status= 1 ", template_id, orgid).First(&parentTemplate).Error
  191. return
  192. }
  193. func DeleteParentDoctorAdviceByTemplateId(template_id int64, org_id int64) (err error) {
  194. ut := writeDb.Begin()
  195. 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
  196. if err != nil {
  197. ut.Rollback()
  198. return
  199. }
  200. 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
  201. if err != nil {
  202. ut.Rollback()
  203. return
  204. }
  205. ut.Commit()
  206. return err
  207. }
  208. func ModifyTemplateName(id int64, template_name string) (err error) {
  209. err = writeDb.Model(&models.DoctorAdviceParentTemplate{}).Where("status=1 AND id = ?", id).Updates(map[string]interface{}{"name": template_name, "updated_time": time.Now().Unix()}).Error
  210. return err
  211. }
  212. func FindOtherAllAdviceTemplate(org_id int64) (temps []*models.DoctorAdviceParentTemplate, err error) {
  213. err = readDb.Model(&models.DoctorAdviceParentTemplate{}).Preload("DoctorAdviceTemplate", func(db *gorm.DB) *gorm.DB {
  214. 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, IF(parent_id>0, parent_id, id) as advice_order").Where("status = 1").Order("advice_order desc, id")
  215. }).Where("org_id = ? AND status=1 ", org_id).Find(&temps).Error
  216. return
  217. //.Scan(&advices).Error
  218. }
  219. func GetHandleData(id int64) (models.ConfigViewModel, error) {
  220. config := models.ConfigViewModel{}
  221. err := readDb.Model(&config).Where("id=?", id).Find(&config).Error
  222. return config, err
  223. }
  224. func UpdateDataTwo(id int64, model models.ConfigViewModel) error {
  225. 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
  226. return err
  227. }
  228. type AdviceDate struct {
  229. RecordDate int64
  230. }
  231. type DoctorAdvice struct {
  232. ID int64 `gorm:"column:id" json:"id" form:"id"`
  233. GroupNo int64 `gorm:"column:groupno" json:"groupno" form:"groupno"`
  234. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  235. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  236. AdviceType int64 `gorm:"column:advice_type" json:"advice_type" form:"advice_type"`
  237. AdviceDate int64 `gorm:"column:advice_date" json:"advice_date" form:"advice_date"`
  238. StartTime int64 `gorm:"column:start_time" json:"start_time" form:"start_time"`
  239. AdviceName string `gorm:"column:advice_name" json:"advice_name" form:"advice_name"`
  240. AdviceDesc string `gorm:"column:advice_desc" json:"advice_desc" form:"advice_desc"`
  241. ReminderDate int64 `gorm:"column:reminder_date" json:"reminder_date" form:"reminder_date"`
  242. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  243. SingleDoseUnit string `gorm:"column:single_dose_unit" json:"single_dose_unit" form:"single_dose_unit"`
  244. DrugSpec float64 `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  245. DrugSpecUnit string `gorm:"column:drug_spec_unit" json:"drug_spec_unit" form:"drug_spec_unit"`
  246. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  247. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  248. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  249. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  250. AdviceDoctor int64 `gorm:"column:advice_doctor" json:"advice_doctor" form:"advice_doctor"`
  251. Status int64 `gorm:"column:status" json:"status" form:"status"`
  252. CreatedTime int64 `gorm:"column:created_time" json:"created_time" form:"created_time"`
  253. UpdatedTime int64 `gorm:"column:updated_time" json:"updated_time" form:"updated_time"`
  254. AdviceAffirm string `gorm:"column:advice_affirm" json:"advice_affirm" form:"advice_affirm"`
  255. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  256. StopTime int64 `gorm:"column:stop_time" json:"stop_time" form:"stop_time"`
  257. StopReason string `gorm:"column:stop_reason" json:"stop_reason" form:"stop_reason"`
  258. StopDoctor int64 `gorm:"column:stop_doctor" json:"stop_doctor" form:"stop_doctor"`
  259. StopState int64 `gorm:"column:stop_state" json:"stop_state" form:"stop_state"`
  260. ParentId int64 `gorm:"column:parent_id" json:"parent_id" form:"parent_id"`
  261. ExecutionTime int64 `gorm:"column:execution_time" json:"execution_time" form:"execution_time"`
  262. ExecutionStaff int64 `gorm:"column:execution_staff" json:"execution_staff" form:"execution_staff"`
  263. ExecutionState int64 `gorm:"column:execution_state" json:"execution_state" form:"execution_state"`
  264. Checker int64 `gorm:"column:checker" json:"checker" form:"checker"`
  265. RecordDate int64 `gorm:"column:record_date" json:"record_date"`
  266. DialysisOrderId int64 `gorm:"column:dialysis_order_id" json:"dialysis_order_id"`
  267. CheckTime int64 `gorm:"column:check_time" json:"check_time" form:"check_time"`
  268. CheckState int64 `gorm:"column:check_state" json:"check_state" form:"check_state"`
  269. AdviceId int64 `gorm:"-"`
  270. RemindType int64 `gorm:"column:remind_type" json:"remind_type"`
  271. FrequencyType int64 `gorm:"column:frequency_type" json:"frequency_type"`
  272. DayCount int64 `gorm:"column:day_count" json:"day_count"`
  273. WeekDay string `gorm:"column:week_day" json:"week_day"`
  274. DoctorAdvice []*DoctorAdvice `gorm:"ForeignKey:ParentId;AssociationForeignKey:ID" json:"children"`
  275. TemplateId string `gorm:"column:template_id" json:"template_id"`
  276. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  277. IsCheck int64 `gorm:"-" json:"is_check" form:"is_check"`
  278. }
  279. func (DoctorAdvice) TableName() string {
  280. return "xt_doctor_advice"
  281. }
  282. func GetDoctorAdviceByType(change_type int64, record_time int64, org_id int64, patient_id int64) (advice []*DoctorAdvice, sch models.Schedule, err error) {
  283. if change_type == 1 { //根据日期取出上一方数据
  284. var Id AdviceDate
  285. 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
  286. 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
  287. 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
  288. } else if change_type == 2 {
  289. var Id AdviceDate
  290. 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
  291. 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
  292. 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
  293. }
  294. return
  295. }