his_config_service.go 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. package service
  2. import (
  3. "XT_New/enums"
  4. "XT_New/models"
  5. "XT_New/utils"
  6. "fmt"
  7. "github.com/astaxie/beego/config"
  8. "golang.org/x/sync/errgroup"
  9. "reflect"
  10. "strconv"
  11. "time"
  12. )
  13. func GetHisPrescriptionTemplatesList(patient_id int64, org_id int64, page int64, limit int64) (templates []*models.HisPrescriptionTemplate, total int64, err error) {
  14. offset := (page - 1) * limit
  15. db := readDb.Model(&models.HisPrescriptionTemplate{}).Where("user_org_id = ? AND patient_id= ? AND status = 1", org_id, patient_id)
  16. err = db.Count(&total).Offset(offset).Limit(limit).Find(&templates).Error
  17. return
  18. }
  19. func GetHisPrescriptionTemplateByID(template_id int64) (prescription models.HisPrescriptionTemplate, err error) {
  20. err = readDb.Model(&models.HisPrescriptionTemplate{}).Where("id = ? AND status = 1 ", template_id).First(&prescription).Error
  21. return
  22. }
  23. func GetHisPrescriptionTemplateByModeId(mode_id int64, patient_id int64, user_org_id int64) (prescription models.HisPrescriptionTemplate, err error) {
  24. err = readDb.Model(&models.HisPrescriptionTemplate{}).Where("mode = ? AND status = 1 AND patient_id = ? and user_org_id = ?", mode_id, patient_id, user_org_id).First(&prescription).Error
  25. return
  26. }
  27. func GetHisPrescriptionTemplateByModeIdTwo(mode_id int64, patient_id int64, user_org_id int64, id int64) (prescription models.HisPrescriptionTemplate, err error) {
  28. err = readDb.Model(&models.HisPrescriptionTemplate{}).Where("mode = ? AND status = 1 AND patient_id = ? and user_org_id = ? and id <> ?", mode_id, patient_id, user_org_id, id).First(&prescription).Error
  29. return
  30. }
  31. func SaveHisPrescriptionTemplate(template *models.HisPrescriptionTemplate) (err error) {
  32. err = writeDb.Save(&template).Error
  33. return
  34. }
  35. func DelelteHisPrescriptionInfoTemplate(id int64, user_org_id int64) (err error) {
  36. err = writeDb.Model(&models.HisPrescriptionInfoTemplate{}).Where("user_org_id = ? AND id = ?", user_org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  37. err = writeDb.Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? AND prescription_id = ?", user_org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  38. err = writeDb.Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? AND prescription_id = ?", user_org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  39. return
  40. }
  41. func DelelteHisPrescriptionTemplate(id int64, user_org_id int64) (err error) {
  42. err = writeDb.Model(&models.HisPrescriptionTemplate{}).Where("user_org_id = ? AND id = ?", user_org_id, id).Updates(map[string]interface{}{"status": 0}).Error
  43. err = writeDb.Model(&models.HisPrescriptionInfoTemplate{}).Where("user_org_id = ? AND id = ?", user_org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  44. return
  45. }
  46. func DelelteHisPrescriptionAdviceTemplate(id int64, user_org_id int64) (err error) {
  47. err = writeDb.Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? AND id = ?", user_org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  48. return
  49. }
  50. func DelelteHisPrescriptionProjectTemplate(id int64, user_org_id int64) (err error) {
  51. err = writeDb.Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? AND id = ?", user_org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  52. return
  53. }
  54. //封装保存接口
  55. func FenCreatePrescriptionTemplate(id, mode_id, types, patient_id int64, name string, adminInfo *AdminUserInfo, dataBody map[string]interface{}) (err error) {
  56. tmp_bool_id := IsDialysisMode(adminInfo.CurrentOrgId, patient_id, mode_id)
  57. var src_template models.HisPrescriptionTemplate
  58. if !tmp_bool_id {
  59. template := models.HisPrescriptionTemplate{
  60. UserOrgId: adminInfo.CurrentOrgId,
  61. PatientId: patient_id,
  62. Type: types,
  63. Status: 1,
  64. Ctime: time.Now().Unix(),
  65. Mtime: time.Now().Unix(),
  66. Name: name,
  67. Mode: mode_id,
  68. }
  69. src_template = template
  70. CreateHisPrescriptionTemplate(&src_template)
  71. } else {
  72. //查询出该模板的id
  73. err = XTReadDB().Model(&models.HisPrescriptionTemplate{}).Where("user_org_id = ? and patient_id = ? and status = 1 and mode = ?", adminInfo.CurrentOrgId, patient_id, mode_id).Find(&src_template).Error
  74. if err != nil {
  75. return
  76. }
  77. src_template.Name = name
  78. src_template.Mode = mode_id
  79. SaveHisPrescriptionTemplate(&src_template)
  80. }
  81. if dataBody["prescriptions"] != nil && reflect.TypeOf(dataBody["prescriptions"]).String() == "[]interface {}" {
  82. prescriptions, _ := dataBody["prescriptions"].([]interface{})
  83. if len(prescriptions) > 0 {
  84. for _, item := range prescriptions {
  85. items := item.(map[string]interface{})
  86. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  87. utils.ErrorLog("id")
  88. //c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  89. err = fmt.Errorf("参数错误")
  90. return
  91. }
  92. id := int64(items["id"].(float64))
  93. if items["type"] == nil || reflect.TypeOf(items["type"]).String() != "float64" {
  94. utils.ErrorLog("type")
  95. //c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  96. err = fmt.Errorf("参数错误")
  97. return
  98. }
  99. types := int64(items["type"].(float64))
  100. if items["med_type"] == nil || reflect.TypeOf(items["med_type"]).String() != "float64" {
  101. utils.ErrorLog("med_type")
  102. //c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  103. err = fmt.Errorf("参数错误")
  104. return
  105. }
  106. med_type := strconv.Itoa(int(items["med_type"].(float64)))
  107. ctime := time.Now().Unix()
  108. prescription := &models.HisPrescriptionInfoTemplate{
  109. ID: id,
  110. PatientId: patient_id,
  111. UserOrgId: adminInfo.CurrentOrgId,
  112. Ctime: ctime,
  113. Mtime: ctime,
  114. Type: types,
  115. Modifier: adminInfo.AdminUser.Id,
  116. Creator: adminInfo.AdminUser.Id,
  117. Status: 1,
  118. PTemplateId: src_template.ID,
  119. MedType: med_type,
  120. }
  121. CreateHisPrescriptionInfoTemplate(prescription)
  122. if items["advices"] != nil && reflect.TypeOf(items["advices"]).String() == "[]interface {}" {
  123. advices := items["advices"].([]interface{})
  124. //group := service.GetMaxAdviceGroupID(adminInfo.CurrentOrgId)
  125. groupNo := int64(0)
  126. ctime := time.Now().Unix()
  127. mtime := ctime
  128. if len(advices) > 0 {
  129. for _, advice := range advices {
  130. var s models.HisPrescriptionAdviceTemplate
  131. s.PrescriptionId = prescription.ID
  132. s.AdviceType = 2
  133. s.StopState = 2
  134. s.ExecutionState = 2
  135. s.Status = 1
  136. s.UserOrgId = adminInfo.CurrentOrgId
  137. s.Groupno = groupNo
  138. s.CreatedTime = ctime
  139. s.UpdatedTime = mtime
  140. s.PatientId = patient_id
  141. errcode := FensetAdviceTemplateWithJSON(&s, advice.(map[string]interface{}))
  142. if errcode > 0 {
  143. //c.ServeFailJSONWithSGJErrorCode(errcode)
  144. err = fmt.Errorf("参数错误")
  145. return
  146. }
  147. CreateHisPrescriptionAdviceTemplate(&s)
  148. }
  149. }
  150. }
  151. if items["project"] != nil && reflect.TypeOf(items["project"]).String() == "[]interface {}" {
  152. projects := items["project"].([]interface{})
  153. if len(projects) > 0 {
  154. for _, project := range projects {
  155. var p models.HisPrescriptionProjectTemplate
  156. p.PrescriptionId = prescription.ID
  157. p.Ctime = time.Now().Unix()
  158. p.Mtime = time.Now().Unix()
  159. p.PatientId = patient_id
  160. p.UserOrgId = adminInfo.CurrentOrgId
  161. p.Status = 1
  162. errcode := FensetProjectTemplateWithJSON(&p, project.(map[string]interface{}))
  163. if errcode > 0 {
  164. //c.ServeFailJSONWithSGJErrorCode(errcode)
  165. err = fmt.Errorf("参数错误")
  166. return
  167. }
  168. CreateHisPrescriptionProjectTemplate(&p)
  169. }
  170. }
  171. }
  172. }
  173. return nil
  174. }
  175. }
  176. return
  177. }
  178. //复制一个setProjectTemplateWithJSON接口
  179. func FensetProjectTemplateWithJSON(project *models.HisPrescriptionProjectTemplate, json map[string]interface{}) int {
  180. if json["id"] != nil {
  181. id := json["id"].(string)
  182. if id != "" {
  183. tmpPid := id[1:]
  184. project_id, _ := strconv.ParseInt(tmpPid, 10, 64)
  185. if id[0] == 112 {
  186. project.Type = 2
  187. }
  188. if id[0] == 105 {
  189. project.Type = 3
  190. }
  191. project.ProjectId = project_id
  192. } else {
  193. project.ProjectId = int64(0)
  194. }
  195. }
  196. if json["frequency_type"] != nil && reflect.TypeOf(json["frequency_type"]).String() == "string" {
  197. tmp_drugid := json["frequency_type"].(string)
  198. frequency_type, _ := strconv.ParseInt(tmp_drugid, 10, 64)
  199. project.FrequencyType = frequency_type
  200. }
  201. if json["frequency_type"] != nil && reflect.TypeOf(json["frequency_type"]).String() == "float64" {
  202. frequency_type := int64(json["frequency_type"].(float64))
  203. project.FrequencyType = frequency_type
  204. }
  205. if json["day_count"] != nil && reflect.TypeOf(json["day_count"]).String() == "string" {
  206. tmp_drugid := json["day_count"].(string)
  207. day_count, _ := strconv.ParseInt(tmp_drugid, 10, 64)
  208. project.DayCount = day_count
  209. }
  210. if json["day_count"] != nil && reflect.TypeOf(json["day_count"]).String() == "float64" {
  211. day_count := int64(json["day_count"].(float64))
  212. project.DayCount = day_count
  213. }
  214. if json["week_day"] != nil && reflect.TypeOf(json["week_day"]).String() == "string" {
  215. week_day, _ := json["week_day"].(string)
  216. project.WeekDay = week_day
  217. }
  218. if json["week_day"] != nil && reflect.TypeOf(json["week_day"]).String() == "float64" {
  219. week_day := config.ToString(json["week_day"].(float64))
  220. project.WeekDay = week_day
  221. }
  222. //if json["type"] != nil || reflect.TypeOf(json["type"]).String() == "float64" {
  223. // types := int64(json["type"].(float64))
  224. // project.Type = types
  225. //}
  226. //if json["project_id"] != nil || reflect.TypeOf(json["project_id"]).String() == "float64" {
  227. // project_id := int64(json["project_id"].(float64))
  228. // project.ProjectId = project_id
  229. //}
  230. if json["price"] != nil && reflect.TypeOf(json["price"]).String() == "string" {
  231. price, _ := strconv.ParseFloat(json["price"].(string), 64)
  232. project.Price = price
  233. }
  234. if json["price"] != nil && reflect.TypeOf(json["price"]).String() == "float64" {
  235. price := json["price"].(float64)
  236. project.Price = price
  237. }
  238. if json["prescribing_number"] != nil && reflect.TypeOf(json["prescribing_number"]).String() == "string" {
  239. total, _ := json["prescribing_number"].(string)
  240. //totals, _ := strconv.ParseInt(total, 10, 64)
  241. project.Count = total
  242. }
  243. if json["prescribing_number"] != nil && reflect.TypeOf(json["prescribing_number"]).String() == "float64" {
  244. total := config.ToString(json["prescribing_number"].(float64))
  245. //totals, _ := strconv.ParseInt(total, 10, 64)
  246. project.Count = total
  247. }
  248. //if json["medical_code"] != nil && reflect.TypeOf(json["medical_code"]).String() == "string" {
  249. // medical_code, _ := json["medical_code"].(string)
  250. // project.MedListCodg = medical_code
  251. //}
  252. if json["single_dose"] != nil && reflect.TypeOf(json["single_dose"]).String() == "string" {
  253. single_dose, _ := json["single_dose"].(string)
  254. project.SingleDose = single_dose
  255. }
  256. if json["single_dose"] != nil && reflect.TypeOf(json["single_dose"]).String() == "float64" {
  257. single_dose := config.ToString(json["single_dose"].(float64))
  258. project.SingleDose = single_dose
  259. }
  260. if json["delivery_way"] != nil && reflect.TypeOf(json["delivery_way"]).String() == "string" {
  261. delivery_way, _ := json["delivery_way"].(string)
  262. project.DeliveryWay = delivery_way
  263. }
  264. if json["execution_frequency"] != nil && reflect.TypeOf(json["execution_frequency"]).String() == "string" {
  265. execution_frequency, _ := json["execution_frequency"].(string)
  266. project.ExecutionFrequency = execution_frequency
  267. }
  268. if json["remark"] != nil && reflect.TypeOf(json["remark"]).String() == "string" {
  269. remark, _ := json["remark"].(string)
  270. project.Remark = remark
  271. }
  272. if json["day"] != nil && reflect.TypeOf(json["remark"]).String() == "string" {
  273. day, _ := json["number_days"].(string)
  274. project.Day = day
  275. }
  276. if json["day"] != nil && reflect.TypeOf(json["remark"]).String() == "float64" {
  277. day := config.ToString(json["number_days"].(float64))
  278. project.Day = day
  279. }
  280. if json["unit"] != nil && reflect.TypeOf(json["unit"]).String() == "string" {
  281. unit, _ := json["unit"].(string)
  282. project.Unit = unit
  283. }
  284. return 0
  285. }
  286. //复制一个setAdviceTemplateWithJSON接口
  287. func FensetAdviceTemplateWithJSON(advice *models.HisPrescriptionAdviceTemplate, json map[string]interface{}) int {
  288. //if json["advice_id"] != nil || reflect.TypeOf(json["advice_id"]).String() == "float64" {
  289. // advice_id := int64(json["advice_id"].(float64))
  290. // advice.ID = advice_id
  291. //}
  292. if json["drug_name"] == nil {
  293. utils.ErrorLog("drug_name")
  294. return enums.ErrorCodeParamWrong
  295. }
  296. var tmpdrugid int64
  297. if json["drug_name"] != nil && reflect.TypeOf(json["drug_name"]).String() == "string" {
  298. tmpdrugid, _ = strconv.ParseInt(json["drug_name"].(string), 10, 64)
  299. }
  300. if json["drug_name"] != nil && reflect.TypeOf(json["drug_name"]).String() == "float64" {
  301. tmpdrugid = int64(json["drug_name"].(float64))
  302. }
  303. advice.AdviceName = FindDrugsName(tmpdrugid)
  304. //adviceDesc, _ := json["advice_desc"].(string)
  305. //advice.AdviceDesc = adviceDesc
  306. //if json["drug_spec"] != nil && reflect.TypeOf(json["drug_spec"]).String() == "string" {
  307. // drugSpec, _ := strconv.ParseFloat(json["drug_spec"].(string), 64)
  308. // advice.DrugSpec = drugSpec
  309. //}
  310. if json["remark"] != nil && reflect.TypeOf(json["remark"]).String() == "string" {
  311. remark, _ := json["remark"].(string)
  312. advice.Remark = remark
  313. }
  314. if json["id"] == nil {
  315. advice.DrugId = 0
  316. } else {
  317. if json["id"] != nil && reflect.TypeOf(json["id"]).String() == "string" {
  318. tmp_drugid := json["id"].(string)
  319. drug_id, _ := strconv.ParseInt(tmp_drugid, 10, 64)
  320. advice.DrugId = drug_id
  321. }
  322. if json["id"] != nil && reflect.TypeOf(json["id"]).String() == "float64" {
  323. drug_id := int64(json["id"].(float64))
  324. advice.DrugId = drug_id
  325. }
  326. }
  327. //if json["min_unit"] != nil && reflect.TypeOf(json["min_unit"]).String() == "string" {
  328. // drugSpecUnit, _ := json["min_unit"].(string)
  329. // advice.DrugSpecUnit = drugSpecUnit
  330. //}
  331. if json["single_dose"] != nil && reflect.TypeOf(json["single_dose"]).String() == "string" {
  332. singleDose, _ := strconv.ParseFloat(json["single_dose"].(string), 64)
  333. advice.SingleDose = singleDose
  334. }
  335. if json["single_dose"] != nil && reflect.TypeOf(json["single_dose"]).String() == "float64" {
  336. singleDose := json["single_dose"].(float64)
  337. advice.SingleDose = singleDose
  338. }
  339. if json["single_dose_unit"] != nil && reflect.TypeOf(json["single_dose_unit"]).String() == "string" {
  340. singleDoseUnit, _ := json["single_dose_unit"].(string)
  341. advice.SingleDoseUnit = singleDoseUnit
  342. }
  343. if json["prescribing_number"] != nil && reflect.TypeOf(json["prescribing_number"]).String() == "string" {
  344. prescribingNumber, _ := strconv.ParseFloat(json["prescribing_number"].(string), 64)
  345. advice.PrescribingNumber = prescribingNumber
  346. }
  347. if json["prescribing_number"] != nil && reflect.TypeOf(json["prescribing_number"]).String() == "float64" {
  348. prescribingNumber := json["prescribing_number"].(float64)
  349. advice.PrescribingNumber = prescribingNumber
  350. }
  351. if json["prescribing_number_unit"] != nil && reflect.TypeOf(json["prescribing_number_unit"]).String() == "string" {
  352. prescribingNumberUnit, _ := json["prescribing_number_unit"].(string)
  353. advice.PrescribingNumberUnit = prescribingNumberUnit
  354. }
  355. if json["delivery_way"] != nil && reflect.TypeOf(json["delivery_way"]).String() == "string" {
  356. deliveryWay, _ := json["delivery_way"].(string)
  357. advice.DeliveryWay = deliveryWay
  358. }
  359. if json["execution_frequency"] != nil && reflect.TypeOf(json["execution_frequency"]).String() == "string" {
  360. executionFrequency, _ := json["execution_frequency"].(string)
  361. advice.ExecutionFrequency = executionFrequency
  362. }
  363. fmt.Println("44444444444")
  364. if json["price"] != nil && reflect.TypeOf(json["price"]).String() == "string" {
  365. price, _ := strconv.ParseFloat(json["price"].(string), 64)
  366. advice.Price = price
  367. }
  368. if json["price"] != nil && reflect.TypeOf(json["price"]).String() == "float64" {
  369. price := json["price"].(float64)
  370. advice.Price = price
  371. }
  372. //if json["medical_insurance_number"] != nil || reflect.TypeOf(json["medical_insurance_number"]).String() == "string" {
  373. // med_list_codg, _ := json["medical_insurance_number"].(string)
  374. // advice.MedListCodg = med_list_codg
  375. //}
  376. //fmt.Println("333333333")
  377. if json["day"] != nil && reflect.TypeOf(json["day"]).String() == "string" {
  378. day, _ := strconv.ParseInt(json["day"].(string), 10, 64)
  379. advice.Day = day
  380. }
  381. if json["day"] != nil && reflect.TypeOf(json["day"]).String() == "float64" {
  382. day := int64(json["day"].(float64))
  383. advice.Day = day
  384. }
  385. //if json["groupno"] != nil || reflect.TypeOf(json["groupno"]).String() == "float64" {
  386. // groupno := int64(json["groupno"].(float64))
  387. // advice.Groupno = groupno
  388. //}
  389. //if json["frequency_type"] != nil || reflect.TypeOf(json["frequency_type"]).String() == "float64" {//原型图没画不考虑
  390. // frequency_type := int64(json["frequency_type"].(float64))
  391. // advice.FrequencyType = frequency_type
  392. //}
  393. //
  394. //if json["day_count"] != nil || reflect.TypeOf(json["day_count"]).String() == "float64" {
  395. // day_count := int64(json["day_count"].(float64))
  396. // advice.DayCount = day_count
  397. //}
  398. //
  399. //if json["week_day"] != nil || reflect.TypeOf(json["week_day"]).String() == "string" {
  400. // week_day, _ := json["week_day"].(string)
  401. // advice.WeekDay = week_day
  402. //}
  403. //fmt.Println("aaaaaaaaaaaaaaaaaaaaaaaaa")
  404. return 0
  405. }
  406. //判断该患者是否有该透析模式
  407. func IsDialysisMode(orgid, patient_id, mode int64) bool {
  408. var total int
  409. XTReadDB().Model(&models.HisPrescriptionTemplate{}).Where("user_org_id = ? and patient_id = ? and status = 1 and mode = ?", orgid, patient_id, mode).Count(&total)
  410. if total > 0 {
  411. return true
  412. } else {
  413. return false
  414. }
  415. }
  416. //获取该透析模式的患者列表
  417. func GetDialysisModePatient(orgid, model int64) (list []*models.DialysisPatient, err error) {
  418. var tmp []*models.DialysisPatient
  419. err = XTReadDB().Raw("select id,name from xt_patients,(SELECT patient_id FROM his_prescription_template WHERE user_org_id = ? and "+
  420. "mode = ? and status = 1)a where id = a.patient_id", orgid, model).Scan(&tmp).Error
  421. if err != nil {
  422. return
  423. }
  424. return tmp, err
  425. }
  426. //如果没有名字默认透析模式
  427. func DialysisModeName(mode int64) string {
  428. tmp := make(map[int64]string)
  429. tmp[1] = "HD"
  430. tmp[2] = "HDF"
  431. tmp[3] = "HD+HP"
  432. tmp[4] = "HP"
  433. tmp[5] = "HF"
  434. tmp[6] = "SCUF"
  435. tmp[7] = "IUF"
  436. tmp[8] = "HFHD"
  437. tmp[9] = "HFHD+HP"
  438. tmp[10] = "PHF"
  439. tmp[11] = "HFR"
  440. tmp[12] = "HDF+HP"
  441. tmp[13] = "CRRT"
  442. tmp[14] = "腹水回输"
  443. //tmp[15] = "HD前置换"
  444. //tmp[16] = "HD后置换"
  445. //tmp[17] = "HDF前置换"
  446. //tmp[18] = "HDF后置换"
  447. tmp[19] = "IUF+HD"
  448. tmp[20] = "UF"
  449. tmp[21] = "HD+"
  450. tmp[22] = "血浆胆红素吸附+HDF"
  451. tmp[23] = "血浆胆红素吸附"
  452. tmp[24] = "I-HDF"
  453. tmp[25] = "HD高通"
  454. tmp[26] = "CVVH"
  455. tmp[27] = "CVVHD"
  456. tmp[28] = "CVVHDF"
  457. tmp[29] = "PE"
  458. tmp[30] = "血浆胆红素吸附+HP"
  459. tmp[31] = "HPD"
  460. tmp[32] = "HDP"
  461. if v, ok := tmp[mode]; ok {
  462. return v
  463. }
  464. return ""
  465. }
  466. //查询出该模板的id
  467. func IdOfTheTemplate(orgid, patient_id, mode_id int64) (src_template models.HisPrescriptionTemplate, err error) {
  468. err = XTReadDB().Model(&models.HisPrescriptionTemplate{}).Where("user_org_id = ? and "+
  469. "patient_id = ? and status = 1 and mode = ?", orgid, patient_id, mode_id).Find(&src_template).Error
  470. return src_template, err
  471. }
  472. //根据透析模式、患者姓名、药品项目获取处方内容
  473. func GetPrescriptionContent(mode, orgid, patient_id int64) (list []*models.HisPrescriptionInfoTemplate, err error) {
  474. var tmp models.HisPrescriptionTemplate
  475. err = XTReadDB().Model(&models.HisPrescriptionTemplate{}).Where("user_org_id = ? and patient_id = ? and mode = ? and status = 1", orgid, patient_id, mode).Find(&tmp).Error
  476. if err != nil {
  477. return
  478. }
  479. var prescriptions []*models.HisPrescriptionInfoTemplate
  480. prescriptions, err = GetHisPrescriptionTemplate(tmp.ID, orgid)
  481. return prescriptions, err
  482. }
  483. //获取该透析模式患者列表(批量删除时用)药品
  484. func GetDialysisDrugDelect(orgid, model, drug_id int64) (list []*models.DialysisPatient, err error) {
  485. var tmp []*models.DialysisPatient
  486. err = XTReadDB().Raw("select id,name from xt_patients,("+
  487. "select distinct patient_id from his_prescription_advice_template,("+
  488. "select his_prescription_info_template.id from his_prescription_info_template,("+
  489. "SELECT id FROM his_prescription_template WHERE user_org_id = ? and mode = ? and status = 1"+
  490. ")his where status = 1 and his_prescription_info_template.p_template_id = his.id"+
  491. ")hisa where status = 1 and prescription_id = hisa.id and drug_id = ?)a where id = a.patient_id", orgid, model, drug_id).Scan(&tmp).Error
  492. if err != nil {
  493. return
  494. }
  495. return tmp, err
  496. }
  497. //获取该透析模式患者列表(批量删除时用)项目
  498. func GetDialysisProjectDelect2(orgid, model, drug_id int64) (list []*models.DialysisPatient, err error) {
  499. var tmp []*models.DialysisPatient
  500. err = XTReadDB().Raw("select id,name from xt_patients,("+
  501. "select distinct patient_id from his_prescription_project_template,("+
  502. "select his_prescription_info_template.id from his_prescription_info_template,("+
  503. "SELECT id FROM his_prescription_template WHERE user_org_id = ? and mode = ? and status = 1"+
  504. ")his where status = 1 and his_prescription_info_template.p_template_id = his.id"+
  505. ")hisb where status = 1 and prescription_id = hisb.id and project_id = ? and type = 2)a where id = a.patient_id", orgid, model, drug_id).Scan(&tmp).Error
  506. if err != nil {
  507. return
  508. }
  509. return tmp, err
  510. }
  511. //获取该透析模式患者列表(批量删除时用)耗材
  512. func GetDialysisProjectDelect3(orgid, model, drug_id int64) (list []*models.DialysisPatient, err error) {
  513. var tmp []*models.DialysisPatient
  514. err = XTReadDB().Raw("select id,name from xt_patients,("+
  515. "select distinct patient_id from his_prescription_project_template,("+
  516. "select his_prescription_info_template.id from his_prescription_info_template,("+
  517. "SELECT id FROM his_prescription_template WHERE user_org_id = ? and mode = ? and status = 1"+
  518. ")his where status = 1 and his_prescription_info_template.p_template_id = his.id"+
  519. ")hisb where status = 1 and prescription_id = hisb.id and project_id = ? and type = 3)a where id = a.patient_id", orgid, model, drug_id).Scan(&tmp).Error
  520. if err != nil {
  521. return
  522. }
  523. return tmp, err
  524. }
  525. //根据透析模式删除药品
  526. func ModeDeleteDrug(orgid, model, drug_id int64, patient_id []int64) (err error) {
  527. //开事务
  528. tx := XTWriteDB().Begin()
  529. defer func() {
  530. if err != nil {
  531. utils.ErrorLog("事务失败,原因为: %v", err)
  532. tx.Rollback()
  533. } else {
  534. tx.Commit()
  535. }
  536. }()
  537. var g errgroup.Group
  538. var tmp []*models.DialysisPatient
  539. err = tx.Raw("select his_prescription_advice_template.id from his_prescription_advice_template,("+
  540. "select his_prescription_info_template.id from his_prescription_info_template,("+
  541. "SELECT id FROM his_prescription_template WHERE user_org_id = ? and mode = ? and status = 1"+
  542. ")his where status = 1 and his_prescription_info_template.p_template_id = his.id"+
  543. ")hisa where status = 1 and prescription_id = hisa.id and drug_id = ? and patient_id in (?)", orgid, model, drug_id, patient_id).Scan(&tmp).Error
  544. for _, v := range tmp {
  545. g.Go(func() error {
  546. tmp := v
  547. var err error
  548. err = tx.Model(&models.HisPrescriptionAdviceTemplate{}).Where("id = ?", tmp.ID).Updates(map[string]interface{}{
  549. "status": 0,
  550. "updated_time": time.Now().Unix(),
  551. }).Error
  552. return err
  553. })
  554. }
  555. if errs := g.Wait(); errs != nil {
  556. err = errs
  557. return
  558. }
  559. return
  560. }
  561. //根据透析模式删除项目和耗材
  562. func ModeDelectItemsAndConsumables(orgid, model int64, drug_id string, patient_id []int64) (err error) {
  563. //开事务
  564. tx := XTWriteDB().Begin()
  565. defer func() {
  566. if err != nil {
  567. utils.ErrorLog("事务失败,原因为: %v", err)
  568. tx.Rollback()
  569. } else {
  570. tx.Commit()
  571. }
  572. }()
  573. front := drug_id[:1]
  574. after := drug_id[1:]
  575. project_id, _ := strconv.ParseInt(after, 10, 64)
  576. var g errgroup.Group
  577. var tmp []*models.DialysisPatient
  578. if front == "p" {
  579. err = tx.Raw("select his_prescription_project_template.id from his_prescription_project_template,("+
  580. "select his_prescription_info_template.id from his_prescription_info_template,("+
  581. "SELECT id FROM his_prescription_template WHERE user_org_id = ? and mode = ? and status = 1"+
  582. ")his where status = 1 and his_prescription_info_template.p_template_id = his.id"+
  583. ")hisb where status = 1 and prescription_id = hisb.id and project_id = ? and type = 2 and patient_id in (?)", orgid, model, project_id, patient_id).Scan(&tmp).Error
  584. if err != nil {
  585. return
  586. }
  587. }
  588. if front == "i" {
  589. err = tx.Raw("select his_prescription_project_template.id from his_prescription_project_template,("+
  590. "select his_prescription_info_template.id from his_prescription_info_template,("+
  591. "SELECT id FROM his_prescription_template WHERE user_org_id = ? and mode = ? and status = 1"+
  592. ")his where status = 1 and his_prescription_info_template.p_template_id = his.id"+
  593. ")hisb where status = 1 and prescription_id = hisb.id and project_id = ? and type = 3 and patient_id in (?)", orgid, model, project_id, patient_id).Scan(&tmp).Error
  594. if err != nil {
  595. return
  596. }
  597. }
  598. for _, v := range tmp {
  599. g.Go(func() error {
  600. tmp := v
  601. var err error
  602. err = tx.Model(&models.HisPrescriptionProjectTemplate{}).Where("id = ?", tmp.ID).Updates(map[string]interface{}{
  603. "status": 0,
  604. "mtime": time.Now().Unix(),
  605. }).Error
  606. return err
  607. })
  608. }
  609. if errs := g.Wait(); errs != nil {
  610. err = errs
  611. return
  612. }
  613. return
  614. }
  615. //替换该透析模式药品
  616. func ReplaceDrug(orgid, model, drug_id int64, patient_id []int64, advice models.HisPrescriptionAdviceTemplate) (err error) {
  617. //开事务
  618. tx := XTWriteDB().Begin()
  619. defer func() {
  620. if err != nil {
  621. utils.ErrorLog("事务失败,原因为: %v", err)
  622. tx.Rollback()
  623. } else {
  624. tx.Commit()
  625. }
  626. }()
  627. var g errgroup.Group
  628. var tmp []*models.DialysisPatient
  629. err = tx.Raw("select his_prescription_advice_template.id from his_prescription_advice_template,("+
  630. "select his_prescription_info_template.id from his_prescription_info_template,("+
  631. "SELECT id FROM his_prescription_template WHERE user_org_id = ? and mode = ? and status = 1"+
  632. ")his where status = 1 and his_prescription_info_template.p_template_id = his.id"+
  633. ")hisa where status = 1 and prescription_id = hisa.id and drug_id = ? and patient_id in (?)", orgid, model, drug_id, patient_id).Scan(&tmp).Error
  634. for _, v := range tmp {
  635. g.Go(func() error {
  636. tmp := v
  637. var err error
  638. err = tx.Model(&models.HisPrescriptionAdviceTemplate{}).Where("id = ?", tmp.ID).Updates(map[string]interface{}{
  639. "drug_id": advice.DrugId,
  640. "single_dose": advice.SingleDose,
  641. "single_dose_unit": advice.SingleDoseUnit,
  642. "delivery_way": advice.DeliveryWay,
  643. "execution_frequency": advice.ExecutionFrequency,
  644. "day": advice.Day,
  645. "prescribing_number": advice.PrescribingNumber,
  646. "prescribing_number_unit": advice.PrescribingNumberUnit,
  647. "price": advice.Price,
  648. "remark": advice.Remark,
  649. "updated_time": time.Now().Unix(),
  650. }).Error
  651. return err
  652. })
  653. }
  654. if errs := g.Wait(); errs != nil {
  655. err = errs
  656. return
  657. }
  658. return
  659. }
  660. //替换该透析模式耗材
  661. func ReplaceItemsAndConsumables(orgid, model int64, drug_id string, patient_id []int64, project models.HisPrescriptionProjectTemplate) (err error) {
  662. //开事务
  663. tx := XTWriteDB().Begin()
  664. defer func() {
  665. if err != nil {
  666. utils.ErrorLog("事务失败,原因为: %v", err)
  667. tx.Rollback()
  668. } else {
  669. tx.Commit()
  670. }
  671. }()
  672. front := drug_id[:1]
  673. after := drug_id[1:]
  674. project_id, _ := strconv.ParseInt(after, 10, 64)
  675. var g errgroup.Group
  676. var tmp []*models.DialysisPatient
  677. if front == "p" {
  678. project.Type = 2
  679. err = tx.Raw("select his_prescription_project_template.id from his_prescription_project_template,("+
  680. "select his_prescription_info_template.id from his_prescription_info_template,("+
  681. "SELECT id FROM his_prescription_template WHERE user_org_id = ? and mode = ? and status = 1"+
  682. ")his where status = 1 and his_prescription_info_template.p_template_id = his.id"+
  683. ")hisb where status = 1 and prescription_id = hisb.id and project_id = ? and type = 2 and patient_id in (?)", orgid, model, project_id, patient_id).Scan(&tmp).Error
  684. if err != nil {
  685. return
  686. }
  687. }
  688. if front == "i" {
  689. project.Type = 3
  690. err = tx.Raw("select his_prescription_project_template.id from his_prescription_project_template,("+
  691. "select his_prescription_info_template.id from his_prescription_info_template,("+
  692. "SELECT id FROM his_prescription_template WHERE user_org_id = ? and mode = ? and status = 1"+
  693. ")his where status = 1 and his_prescription_info_template.p_template_id = his.id"+
  694. ")hisb where status = 1 and prescription_id = hisb.id and project_id = ? and type = 3 and patient_id in (?)", orgid, model, project_id, patient_id).Scan(&tmp).Error
  695. if err != nil {
  696. return
  697. }
  698. }
  699. for _, v := range tmp {
  700. g.Go(func() error {
  701. tmp := v
  702. var err error
  703. err = tx.Model(&models.HisPrescriptionProjectTemplate{}).Where("id = ?", tmp.ID).Updates(map[string]interface{}{
  704. "single_dose": project.SingleDose,
  705. "delivery_way": project.DeliveryWay,
  706. "execution_frequency": project.ExecutionFrequency,
  707. "day": project.Day,
  708. "count": project.Count,
  709. "price": project.Price,
  710. "remark": project.Remark,
  711. "type": project.Type,
  712. "mtime": time.Now().Unix(),
  713. }).Error
  714. return err
  715. })
  716. }
  717. if errs := g.Wait(); errs != nil {
  718. err = errs
  719. return
  720. }
  721. return
  722. }
  723. //跟据id查询药品、项目、耗材、套餐
  724. func QueryFourTables(id string, orgid int64) (tmp interface{}, err error) {
  725. if id[0] == 112 {
  726. after := id[1:]
  727. var project []*models.XtHisProjectL
  728. err = XTReadDB().Model(&project).Where("id = ?", after).Find(&project).Error
  729. if err != nil {
  730. return nil, err
  731. } else {
  732. for i := 0; i < len(project); i++ {
  733. project[i].Translate, err = TranslateZu(project[i].StatisticalClassification, orgid, "统计分类")
  734. if err != nil {
  735. return nil, err
  736. }
  737. }
  738. return project, err
  739. }
  740. } else if id[0] == 105 {
  741. after := id[1:]
  742. var good []*models.GoodInfoL
  743. err = XTReadDB().Model(&models.GoodInfoL{}).Where("id = ?", after).Find(&good).Error
  744. if err != nil {
  745. return nil, err
  746. } else {
  747. for i := 0; i < len(good); i++ {
  748. good[i].Translate = "耗材"
  749. }
  750. return good, err
  751. }
  752. } else if id[0] == 104 {
  753. after := id[1:]
  754. var team []*models.XtHisProjectTeam
  755. err = XTReadDB().Model(&models.XtHisProjectTeam{}).Where("id = ?", after).Find(&team).Error
  756. if err != nil {
  757. return nil, err
  758. } else {
  759. return team, err
  760. }
  761. } else {
  762. var lib []*models.BaseDrugLibL
  763. err = XTReadDB().Model(&models.BaseDrugLibL{}).Where("id = ?", id).Find(&lib).Error
  764. if err != nil {
  765. return nil, err
  766. } else {
  767. for i := 0; i < len(lib); i++ {
  768. lib[i].SingleDoseUnit = TypeConversion02(config.ToString(lib[i].DrugDoseUnit))
  769. if err != nil {
  770. return nil, err
  771. }
  772. type m struct {
  773. Name string `json:"name"`
  774. }
  775. newM := []m{
  776. {
  777. lib[i].DoseUnit, //计量单位
  778. },
  779. {
  780. lib[i].MinUnit, //拆零单位
  781. },
  782. }
  783. newN := []m{
  784. {
  785. lib[i].MinUnit,
  786. },
  787. {
  788. lib[i].MaxUnit,
  789. },
  790. }
  791. lib[i].List1 = newM
  792. lib[i].List2 = newN
  793. }
  794. return lib, err
  795. }
  796. }
  797. }
  798. //获取机构项目中所用的组
  799. func GetDataConfig(orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  800. err = XTReadDB().Raw("select * from xt_drug_data_config where parent_id in "+
  801. "(select id from xt_drug_data_config where name = \"统计分类\" and parent_id = 0) "+
  802. "and status =1 and (org_id = ? or org_id = 0)", orgid).Scan(&dataconfig).Error
  803. return
  804. }
  805. //翻译项目中的组
  806. func TranslateZu(sc, orgid int64, types string) (s string, err error) {
  807. var dataconfig []*models.DictDataconfig
  808. tmp := "select * from xt_drug_data_config where parent_id in (select id from xt_d·rug_data_config where name = \"" + types + "\" and parent_id = 0) and value = " + config.ToString(sc) + " and status = 1 and (org_id = " + config.ToString(orgid) + " or org_id = 0)"
  809. err = XTReadDB().Raw(tmp).Scan(&dataconfig).Error
  810. if err != nil {
  811. return
  812. }
  813. if len(dataconfig) > 1 {
  814. err = fmt.Errorf("sql数据异常:%v", tmp)
  815. }
  816. if len(dataconfig) == 0 {
  817. return "", err
  818. }
  819. return dataconfig[0].Name, err
  820. }