doctor_advice_service.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package service
  2. import (
  3. "XT_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. }