his_config_service.go 34KB

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