his_config_service.go 32KB

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