pharmacy_service.go 69KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059
  1. package service
  2. import (
  3. "XT_New/models"
  4. "XT_New/utils"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/astaxie/beego/config"
  8. "github.com/astaxie/beego/context"
  9. "github.com/jinzhu/gorm"
  10. "golang.org/x/sync/errgroup"
  11. "math"
  12. "strconv"
  13. "strings"
  14. "sync"
  15. "time"
  16. )
  17. func SaveErrs(org_id int64, input *context.BeegoInput, err error) {
  18. dataBody := make(map[string]interface{}, 0)
  19. json.Unmarshal(input.RequestBody, &dataBody)
  20. route := input.Context.Request.RequestURI
  21. parameter := MapToJson(dataBody)
  22. tmp := models.XtErrs{
  23. org_id,
  24. route,
  25. parameter,
  26. err.Error(),
  27. }
  28. XTWriteDB().Create(&tmp)
  29. }
  30. func SavePharmacy() (err error) {
  31. var advice_info []*models.HisDoctorAdviceInfo
  32. err = XTReadDB().Raw("select * from xt_doctor_advice where id > 21780 and id < 21790").Scan(&advice_info).Error
  33. if err != nil {
  34. return
  35. }
  36. return nil
  37. }
  38. // 查询药房中某一天的患者人数,is_medicine:0未发,1已发(改)
  39. func GetTodayPharmacy(stime, etime, orgid, is_medicine int64) (num int, err error) {
  40. //orgid = 9675
  41. InitDrugidIsNil(orgid, stime, etime)
  42. var tmp []*models.TmpTTT
  43. if is_medicine == 0 {
  44. err = XTReadDB().Raw("select distinct patient_id from his_doctor_advice_info where "+
  45. "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = 0 and "+
  46. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) "+
  47. "union "+
  48. "select distinct patient_id from xt_doctor_advice where "+
  49. "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = 0 and "+
  50. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) ",
  51. stime, etime, orgid, orgid, stime, etime, orgid, orgid).Scan(&tmp).Error
  52. if err != nil {
  53. return
  54. }
  55. } else {
  56. err = XTReadDB().Raw("select distinct patient_id from his_doctor_advice_info where "+
  57. "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = 1 and "+
  58. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) "+
  59. "union "+
  60. "select distinct patient_id from xt_doctor_advice where "+
  61. "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = 1 and "+
  62. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) ",
  63. stime, etime, orgid, orgid, stime, etime, orgid, orgid).Scan(&tmp).Error
  64. if err != nil {
  65. return
  66. }
  67. }
  68. return len(tmp), err
  69. }
  70. func GetTodayAdviceCount(stime, etime, orgid, is_medicine int64) (num int, err error) {
  71. //orgid = 9675
  72. InitDrugidIsNil(orgid, stime, etime)
  73. var tmp []*models.TmpTTT
  74. if is_medicine == 0 {
  75. err = XTReadDB().Raw("select * from his_doctor_advice_info where status= 1 and advice_date>=? and advice_date<=? and user_org_id=? and is_medicine=0 group by patient_id",
  76. stime, etime, orgid).Scan(&tmp).Error
  77. if err != nil {
  78. return
  79. }
  80. } else {
  81. err = XTReadDB().Raw("select * from his_doctor_advice_info where status= 1 and advice_date>=? and advice_date<=? and user_org_id=? and is_medicine=1 group by patient_id",
  82. stime, etime, orgid).Scan(&tmp).Error
  83. if err != nil {
  84. return
  85. }
  86. }
  87. return len(tmp), err
  88. }
  89. func GetSchedulePatientId(start_time int64, end_time int64, org_id int64, class_type int64, partion_type int64) (schedule []*models.XtSchedules, err error) {
  90. db := XTReadDB().Model(&schedule).Where("status = 1")
  91. if start_time > 0 {
  92. db = db.Where("schedule_date >=?", start_time)
  93. }
  94. if end_time > 0 {
  95. db = db.Where("schedule_date <=?", end_time)
  96. }
  97. if org_id > 0 {
  98. db = db.Where("user_org_id = ?", org_id)
  99. }
  100. if class_type > 0 {
  101. db = db.Where("schedule_type =?", class_type)
  102. }
  103. if partion_type > 0 {
  104. db = db.Where("partition_id = ?", partion_type)
  105. }
  106. err = db.Find(&schedule).Error
  107. return schedule, err
  108. }
  109. func GetTodayAdviceCountOne(stime int64, etime int64, orgid int64, is_medicine int64, ids []int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  110. db := XTReadDB().Model(&advice).Where("status=1 and is_medicine = ?", is_medicine)
  111. if stime > 0 {
  112. db = db.Where("advice_date>=?", stime)
  113. }
  114. if etime > 0 {
  115. db = db.Where("advice_date<=?", stime)
  116. }
  117. if orgid > 0 {
  118. db = db.Where("user_org_id = ?", orgid)
  119. }
  120. if len(ids) == 0 {
  121. db = db.Where("patient_id not in(?)", ids)
  122. }
  123. if len(ids) > 0 {
  124. db = db.Where("patient_id in(?)", ids)
  125. }
  126. err = db.Group("patient_id").Find(&advice).Error
  127. return advice, err
  128. }
  129. func GetPatientByAdviceId(patient_id int64) (models.TmpPatientOne, error) {
  130. patient := models.TmpPatientOne{}
  131. err = XTReadDB().Where("id = ?", patient_id).Find(&patient).Error
  132. return patient, err
  133. }
  134. // (改)
  135. func GetTodayDrug(stime, etime, orgid, is_medicine int64, keyword string) (list []*models.TmpPatient, err error) {
  136. InitDrugidIsNil(orgid, stime, etime)
  137. //病人
  138. var tmp []*models.TmpTTT
  139. //tmp := make([]string,0)
  140. if is_medicine == 0 {
  141. if keyword != "" {
  142. keyword = "%" + keyword + "%"
  143. err = XTReadDB().Raw("select a.* from(select distinct patient_id,dispensing_time from his_doctor_advice_info where "+
  144. "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
  145. "patient_id in (select id from xt_patients where user_org_id = ? and name like ? or dialysis_no like ?) and "+
  146. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) "+
  147. "union "+
  148. "select distinct patient_id,dispensing_time from xt_doctor_advice where "+
  149. "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
  150. "patient_id in (select id from xt_patients where user_org_id = ? and name like ? or dialysis_no like ?) and "+
  151. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1))a order by dispensing_time desc ",
  152. stime, etime, orgid, is_medicine, orgid, keyword, keyword, orgid, stime, etime, orgid, is_medicine, orgid, keyword, keyword, orgid).Scan(&tmp).Error
  153. } else {
  154. err = XTReadDB().Raw("select a.* from(select distinct patient_id,dispensing_time from his_doctor_advice_info where "+
  155. "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
  156. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) "+
  157. "union "+
  158. "select distinct patient_id,dispensing_time from xt_doctor_advice where "+
  159. "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
  160. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1))a order by dispensing_time desc",
  161. stime, etime, orgid, is_medicine, orgid, stime, etime, orgid, is_medicine, orgid).Scan(&tmp).Error
  162. }
  163. if err != nil {
  164. return
  165. }
  166. } else {
  167. if keyword != "" {
  168. keyword = "%" + keyword + "%"
  169. err = XTReadDB().Raw("select a.* from(select distinct patient_id,dispensing_time from his_doctor_advice_info where "+
  170. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  171. "patient_id in (select id from xt_patients where user_org_id = ? and name like ? or dialysis_no like ?) and "+
  172. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) "+
  173. "union "+
  174. "select distinct patient_id,dispensing_time from xt_doctor_advice where "+
  175. "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  176. "patient_id in (select id from xt_patients where user_org_id = ? and name like ? or dialysis_no like ?) and "+
  177. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1))a order by dispensing_time desc ",
  178. stime, etime, orgid, is_medicine, orgid, keyword, keyword, orgid, stime, etime, orgid, is_medicine, orgid, keyword, keyword, orgid).Scan(&tmp).Error
  179. } else {
  180. err = XTReadDB().Raw("select a.* from(select distinct patient_id,dispensing_time from his_doctor_advice_info where "+
  181. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  182. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) "+
  183. "union "+
  184. "select distinct patient_id,dispensing_time from xt_doctor_advice where "+
  185. "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  186. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1))a order by dispensing_time desc",
  187. stime, etime, orgid, is_medicine, orgid, stime, etime, orgid, is_medicine, orgid).Scan(&tmp).Error
  188. }
  189. if err != nil {
  190. return
  191. }
  192. }
  193. tmp_s := make([]int64, 0)
  194. s_map := make(map[int64]int64)
  195. for _, v := range tmp {
  196. if _, ok := s_map[v.PatientID]; !ok {
  197. s_map[v.PatientID] = v.PatientID
  198. tmp_s = append(tmp_s, v.PatientID)
  199. }
  200. }
  201. list, err = GetManyUsers(tmp_s)
  202. return
  203. }
  204. // 查询患者(改)
  205. func GetManyUsers(tmp []int64) (list []*models.TmpPatient, err error) {
  206. for i := 0; i < len(tmp); i++ {
  207. var tt models.Patients
  208. err = XTReadDB().Model(&models.Patients{}).Where("id = ?", tmp[i]).Find(&tt).Error
  209. tmp_list := &models.TmpPatient{
  210. PatientID: tt.ID,
  211. Name: tt.Name,
  212. DialysisNo: tt.DialysisNo,
  213. }
  214. list = append(list, tmp_list)
  215. }
  216. return
  217. }
  218. // 查询患者当天时间段中的药,is_medicine:0未发,1已发(改)
  219. func GetPatientMedication(orgid, patient_id, stime, etime, is_medicine int64) (pp []*models.PharmacyContent, err error) {
  220. InitDrugidIsNil(orgid, stime, etime)
  221. var tmp []*models.HisDoctorAdviceInfoL
  222. if is_medicine == 0 {
  223. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
  224. "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and patient_id = ? and is_medicine = ? and drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1)", stime, etime, orgid, patient_id, is_medicine, orgid).Find(&tmp).Error
  225. if err != nil {
  226. return pp, err
  227. }
  228. } else {
  229. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
  230. "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and patient_id = ? and is_medicine = ? and drug_id in (select id from xt_base_drug where org_id = ? and status = 1)", stime, etime, orgid, patient_id, is_medicine, orgid).Find(&tmp).Error
  231. if err != nil {
  232. return pp, err
  233. }
  234. }
  235. for _, v := range tmp {
  236. pp = append(pp, &models.PharmacyContent{
  237. Name: v.AdviceName,
  238. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  239. Usage: v.DeliveryWay,
  240. Frequency: v.ExecutionFrequency,
  241. Days: config.ToString(v.Day) + "天",
  242. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  243. Doctor: GetAdminUserName(v.AdviceDoctor, orgid), //开立医生
  244. DataSources: "his处方",
  245. Remarks: v.Remark, //备注
  246. DrugId: v.DrugId,
  247. })
  248. }
  249. var tmp_advice []*models.XtDoctorAdviceL
  250. if is_medicine == 0 {
  251. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  252. "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and patient_id = ? and is_medicine = ? and drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1)", stime, etime, orgid, patient_id, is_medicine, orgid).Find(&tmp_advice).Error
  253. if err != nil {
  254. return pp, err
  255. }
  256. } else {
  257. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  258. "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and patient_id = ? and is_medicine = ? and drug_id in (select id from xt_base_drug where org_id = ? and status = 1)", stime, etime, orgid, patient_id, is_medicine, orgid).Find(&tmp_advice).Error
  259. if err != nil {
  260. return pp, err
  261. }
  262. }
  263. for _, v := range tmp_advice {
  264. pp = append(pp, &models.PharmacyContent{
  265. Name: v.AdviceName,
  266. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  267. Usage: v.DeliveryWay,
  268. Frequency: v.ExecutionFrequency,
  269. Days: "-",
  270. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  271. Doctor: GetAdminUserName(v.AdviceDoctor, orgid),
  272. DataSources: "临时医嘱",
  273. Remarks: v.Remark,
  274. DrugId: v.DrugId,
  275. })
  276. }
  277. return
  278. }
  279. // 获取创建者姓名(改)
  280. func GetAdminUserName(doctor, orgid int64) string {
  281. var tmp models.VmUserAdminRole
  282. XTReadDB().Model(&models.VmUserAdminRole{}).Where("admin_user_id = ? and org_id = ? and status = 1", doctor, orgid).Find(&tmp)
  283. return tmp.UserName
  284. }
  285. // 查询该机构的药房配置,true: 通过药房发药,false:不通过药房发药。如果没有该机构的信息则生成一条数据
  286. func GetOrgIdPharmacyConfig(orgid int64) bool {
  287. var total int
  288. var tmp_pharmacy models.PharmacyConfig
  289. XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and status = 1", orgid).Find(&tmp_pharmacy).Count(&total)
  290. if total > 0 {
  291. //有记录
  292. if tmp_pharmacy.IsOpen == 1 {
  293. return true
  294. } else {
  295. return false
  296. }
  297. } else {
  298. //没有记录生成一条
  299. tmp := models.PharmacyConfig{
  300. UserOrgId: orgid,
  301. IsOpen: 2,
  302. Status: 1,
  303. Ctime: time.Now().Unix(),
  304. Mtime: time.Now().Unix(),
  305. }
  306. XTWriteDB().Create(&tmp)
  307. return false
  308. }
  309. }
  310. // 修改该机构的药房配置
  311. func ModifyPharmacyConfig(orgid, isopen int64) (err error) {
  312. if isopen == 1 || isopen == 2 {
  313. err = XTWriteDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{
  314. "mtime": time.Now().Unix(),
  315. "is_open": isopen,
  316. }).Error
  317. } else {
  318. err = fmt.Errorf("类型解析错误")
  319. }
  320. return
  321. }
  322. // 查询该药品是否通过药房发药.0否;1是
  323. func DrugAllocation(drug_id int64) (is_pharmacy int64, err error) {
  324. tmp := models.BaseDrugLib{}
  325. err = XTReadDB().Model(&models.BaseDrugLib{}).Where("id = ?", drug_id).Find(&tmp).Error
  326. return tmp.IsPharmacy, err
  327. }
  328. // 根据id查询该药是否发药
  329. func MedicineState(id int64) (is_medicine models.HisDoctorAdviceInfoL, err error) {
  330. tmp := models.HisDoctorAdviceInfoL{}
  331. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ?", id).Find(&tmp).Error
  332. return tmp, err
  333. }
  334. // 发药明细列表(
  335. func DispensingDetailsList(stime, etime, orgid, page, limit int64, keyword string) (dislist []*models.DispensingList, total int64, err error) {
  336. var fenye []*models.Pharmary //分页用的
  337. offset := (page - 1) * limit
  338. dbone := XTReadDB().Model(&models.Pharmary{}).Where("status = 1 and user_org_id = ? and record_date >= ? and record_date <= ?", orgid, stime, etime)
  339. if keyword != "" {
  340. var tmp_id []*models.TmpID
  341. keyword = "%" + keyword + "%"
  342. XTReadDB().Raw("select id from xt_patients where name like ? and user_org_id = ?", keyword, orgid).Scan(&tmp_id)
  343. tmp_ids := make([]int64, 0)
  344. for _, v := range tmp_id {
  345. tmp_ids = append(tmp_ids, v.Id)
  346. }
  347. dbone = dbone.Where("patient_id in (?)", tmp_ids)
  348. }
  349. //查询出分页用的数据
  350. err = dbone.Count(&total).Offset(offset).Order("mtime desc").Limit(limit).Find(&fenye).Error
  351. if err != nil {
  352. return
  353. }
  354. for _, v := range fenye {
  355. tmp_doctorid, tmp_doctorname, errs := GetDoctorIds(v.PatientId, v.RecordDate, orgid)
  356. if errs != nil {
  357. err = errs
  358. return
  359. }
  360. pat, _ := GetPatientName(v.PatientId)
  361. dislist = append(dislist, &models.DispensingList{
  362. PatientID: v.PatientId,
  363. Name: pat.Name,
  364. DoctorId: tmp_doctorid,
  365. DoctorName: tmp_doctorname,
  366. RecordTime: v.RecordDate,
  367. RecordDate: fmt.Sprintf(time.Unix(v.RecordDate, 0).Format("2006-01-02 15:04:05")),
  368. })
  369. }
  370. return
  371. }
  372. // 处方详情//////////////////////
  373. func PrescriptionDetails(patient_id, record_date, orgid int64) (pre []*models.PrescripDetails, err error) {
  374. var tmp []*models.HisDoctorAdviceInfoL
  375. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
  376. "status = 1 and patient_id = ? and user_org_id = ? and dispensing_time = ? and is_medicine = 1 and drug_id in (select id from xt_base_drug where org_id = ? and status = 1)", patient_id, orgid, record_date, orgid).Find(&tmp).Error
  377. if err != nil {
  378. return
  379. }
  380. for _, v := range tmp {
  381. pre = append(pre, &models.PrescripDetails{
  382. Drugname: v.AdviceName,
  383. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  384. Usage: v.DeliveryWay,
  385. Frequency: v.ExecutionFrequency,
  386. Days: config.ToString(v.Day) + "天",
  387. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  388. UnitPrice: config.ToString(v.Price) + "元",
  389. Remarks: v.Remark,
  390. })
  391. }
  392. var tmp_advice []*models.XtDoctorAdviceL
  393. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  394. "status = 1 and (advice_type = 2 or advice_type = 3) and patient_id = ? and user_org_id = ? and dispensing_time = ? and is_medicine = 1 and drug_id in (select id from xt_base_drug where org_id = ? and status = 1)", patient_id, orgid, record_date, orgid).Find(&tmp_advice).Error
  395. if err != nil {
  396. return
  397. }
  398. for _, v := range tmp_advice {
  399. pre = append(pre, &models.PrescripDetails{
  400. Drugname: v.AdviceName,
  401. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  402. Usage: v.DeliveryWay,
  403. Frequency: v.ExecutionFrequency,
  404. Days: "-",
  405. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  406. UnitPrice: "-",
  407. Remarks: v.Remark,
  408. })
  409. }
  410. return
  411. }
  412. // 查询病人的his id
  413. func GetHisID(prescription_id, orgid int64) (id int64) {
  414. var tmp models.HisPrintPrescription
  415. XTReadDB().Model(&models.HisPrintPrescription{}).Where("id = ?", prescription_id).Find(&tmp)
  416. var fin models.HisPrescriptionInfoTwo
  417. XTReadDB().Model(&models.HisPrescriptionInfoTwo{}).Where("prescription_number = ? and user_org_id = ?", tmp.PrescriptionNumber, orgid).Find(&fin)
  418. return fin.ID
  419. }
  420. // 根据药品id获取药品信息
  421. func GetDrugNameTX(id int64, tx *gorm.DB) (tmp models.SpBaseDrug, err error) {
  422. err = tx.Model(&models.SpBaseDrug{}).Where("id = ? and status = 1", id).Find(&tmp).Error
  423. return
  424. }
  425. func GetXtManufacturer(id int64, tx *gorm.DB) (name string, err error) {
  426. var tmp models.Manufacturer
  427. err = tx.Model(&models.Manufacturer{}).Where("id = ? and status = 1", id).Find(&tmp).Error
  428. name = tmp.ManufacturerName
  429. return
  430. }
  431. // 查询药品(使用keyword)
  432. func GetManyDrugs(orgid int64, keyword string) (map[int64]models.PharmacyBaseDrug, error) {
  433. var tmp []*models.PharmacyBaseDrug
  434. tt := make(map[int64]models.PharmacyBaseDrug)
  435. err := XTReadDB().Model(&models.PharmacyBaseDrug{}).Where(" org_id = ? ", orgid).Where(" drug_name like ? ", "%"+keyword+"%").Find(&tmp).Error
  436. for _, v := range tmp {
  437. tt[v.ID] = *v
  438. }
  439. return tt, err
  440. }
  441. // 查询(
  442. func GetTodayMedicine(stime, etime, orgid, is_medicine int64, keyword string) (finlly []*models.ListOfDrugs, err error) {
  443. InitDrugidIsNil(orgid, stime, etime)
  444. var tmp []*models.TmpLLL
  445. if is_medicine == 0 {
  446. if keyword != "" {
  447. keyword = "%" + keyword + "%"
  448. err = XTReadDB().Raw("select distinct drug_id from his_doctor_advice_info where "+
  449. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  450. "drug_id in (select id from xt_base_drug where org_id = ? and drug_name like ? and is_pharmacy = 1) "+
  451. "union "+
  452. "select distinct drug_id from xt_doctor_advice where "+
  453. "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  454. "drug_id in (select id from xt_base_drug where org_id = ? and drug_name like ? and is_pharmacy = 1)",
  455. stime, etime, orgid, is_medicine, orgid, keyword, stime, etime, orgid, is_medicine, orgid, keyword).Scan(&tmp).Error
  456. } else {
  457. err = XTReadDB().Raw("select distinct drug_id from his_doctor_advice_info where "+
  458. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  459. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) "+
  460. "union "+
  461. "select distinct drug_id from xt_doctor_advice where "+
  462. "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  463. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) ",
  464. stime, etime, orgid, is_medicine, orgid, stime, etime, orgid, is_medicine, orgid).Scan(&tmp).Error
  465. }
  466. if err != nil {
  467. return
  468. }
  469. } else {
  470. if keyword != "" {
  471. keyword = "%" + keyword + "%"
  472. err = XTReadDB().Raw("select distinct drug_id from his_doctor_advice_info where "+
  473. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  474. "drug_id in (select id from xt_base_drug where org_id = ? and drug_name like ? and is_pharmacy = 1) "+
  475. "union "+
  476. "select distinct drug_id from xt_doctor_advice where "+
  477. "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  478. "drug_id in (select id from xt_base_drug where org_id = ? and drug_name like ? and is_pharmacy = 1)",
  479. stime, etime, orgid, is_medicine, orgid, keyword, stime, etime, orgid, is_medicine, orgid, keyword).Scan(&tmp).Error
  480. } else {
  481. err = XTReadDB().Raw("select distinct drug_id from his_doctor_advice_info where "+
  482. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  483. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) "+
  484. "union "+
  485. "select distinct drug_id from xt_doctor_advice where "+
  486. "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  487. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) ",
  488. stime, etime, orgid, is_medicine, orgid, stime, etime, orgid, is_medicine, orgid).Scan(&tmp).Error
  489. }
  490. if err != nil {
  491. return
  492. }
  493. }
  494. for _, v := range tmp {
  495. name, specifications := FindDrugSpecifications(v.DrugId)
  496. number, errs := GetInventoryQuantity(orgid, v.DrugId)
  497. if errs != nil {
  498. err = errs
  499. return
  500. }
  501. finlly = append(finlly, &models.ListOfDrugs{
  502. ID: v.DrugId,
  503. Name: name,
  504. Specifications: specifications,
  505. Stock: number,
  506. })
  507. }
  508. return
  509. }
  510. // 根据药品id获取药品规格(
  511. func FindDrugSpecifications(id int64) (name, specifications string) {
  512. var tmp models.PharmacyBaseDrug
  513. XTReadDB().Model(&models.PharmacyBaseDrug{}).Where("id = ?", id).Find(&tmp)
  514. name = tmp.DrugName
  515. specifications = config.ToString(tmp.Dose) + tmp.DoseUnit + "*" + config.ToString(tmp.MinNumber) + tmp.MinUnit + "/" + config.ToString(tmp.MaxUnit)
  516. return
  517. }
  518. // 根据药品id获取当前药品出库仓库的库存数量
  519. func GetInventoryQuantity(orgid, drugid int64) (number string, err error) {
  520. //获取药品拆零数量
  521. var phar models.PharmacyBaseDrug
  522. XTReadDB().Model(&models.PharmacyBaseDrug{}).Where("id = ?", drugid).Find(&phar)
  523. min_number := phar.MinNumber
  524. if min_number == 0 {
  525. err = fmt.Errorf("药品拆零数量不能为零!")
  526. return
  527. }
  528. storeconfig, _ := FindStorehouseConfig(orgid)
  529. var tmp []models.SpDrugWarehouseInfo
  530. XTReadDB().Model(&models.SpDrugWarehouseInfo{}).Where("org_id = ? and drug_id = ? and storehouse_id = ? and status = 1", orgid, drugid, storeconfig.DrugStorehouseOut).Find(&tmp)
  531. max, min := int64(0), int64(0)
  532. for _, v := range tmp {
  533. max += v.StockMaxNumber
  534. min += v.StockMinNumber
  535. }
  536. max += min / min_number
  537. min = min % min_number
  538. if max != 0 {
  539. number = config.ToString(max) + phar.MaxUnit
  540. }
  541. if min != 0 {
  542. number = number + config.ToString(min) + phar.MinUnit
  543. }
  544. if number == "" {
  545. number = "0"
  546. }
  547. //number = config.ToString(max)+phar.MaxUnit+config.ToString(min)+phar.MinUnit
  548. return
  549. }
  550. // 获取该药品的病人信息(
  551. func FindMedicationList(orgid, drug_id, stime, etime, is_medicine int64) (pp []*models.PatientInformation, err error) { ///////////////
  552. InitDrugidIsNil(orgid, stime, etime)
  553. var tmp []*models.HisDoctorAdviceInfoL
  554. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
  555. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and drug_id = ? and is_medicine = ?", stime, etime, orgid, drug_id, is_medicine).Find(&tmp).Error
  556. if err != nil {
  557. return pp, err
  558. }
  559. for _, v := range tmp {
  560. pp = append(pp, &models.PatientInformation{
  561. Id: "h" + config.ToString(v.ID),
  562. Name: FindUserName(v.PatientId),
  563. PatientId: v.PatientId,
  564. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  565. Usage: v.DeliveryWay,
  566. Frequency: v.ExecutionFrequency,
  567. Days: config.ToString(v.Day) + "天",
  568. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  569. DataSources: "his处方",
  570. People: GetUserAdminName(v.People, orgid), //领药人!!!!!!!!!!!!!!!!!!!!!!
  571. Quantity: int64(v.PrescribingNumber),
  572. Unit: v.PrescribingNumberUnit,
  573. })
  574. }
  575. var tmp_advice []*models.XtDoctorAdviceL
  576. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  577. "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and drug_id = ? and is_medicine = ?", stime, etime, orgid, drug_id, is_medicine).Find(&tmp_advice).Error
  578. if err != nil {
  579. return pp, err
  580. }
  581. for _, v := range tmp_advice {
  582. pp = append(pp, &models.PatientInformation{
  583. Id: "x" + config.ToString(v.ID),
  584. Name: FindUserName(v.PatientId),
  585. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  586. Usage: v.DeliveryWay,
  587. Frequency: v.ExecutionFrequency,
  588. Days: "",
  589. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  590. DataSources: "临时医嘱",
  591. People: GetUserAdminName(v.People, orgid), //领药人
  592. Quantity: int64(v.PrescribingNumber),
  593. Unit: v.PrescribingNumberUnit,
  594. })
  595. }
  596. return
  597. }
  598. // 患者发药按钮点击(
  599. func DispensingMedicine(orgid, patient_id, stime, etime, creater int64) (err error) {
  600. //开事务
  601. tx := XTWriteDB().Begin()
  602. defer func() {
  603. if err != nil {
  604. utils.ErrorLog("事务失败,原因为: %v", err)
  605. tx.Rollback()
  606. } else {
  607. tx.Commit()
  608. }
  609. }()
  610. time_now := time.Now().Unix()
  611. var hids []*models.TmpID
  612. var xids []*models.TmpID
  613. err = tx.Raw("select id from his_doctor_advice_info where status = 1 and advice_date >= ? and advice_date <= ? "+
  614. "and user_org_id = ? and patient_id = ? and is_medicine = 0 and drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) ", stime, etime, orgid, patient_id, orgid).Scan(&hids).Error
  615. if err != nil {
  616. return
  617. }
  618. hid := make([]int64, 0)
  619. for _, v := range hids {
  620. hid = append(hid, v.Id)
  621. }
  622. var advice_info []*models.HisDoctorAdviceInfo
  623. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", hid).Find(&advice_info).Error
  624. if err != nil {
  625. return
  626. }
  627. for _, v := range advice_info {
  628. tmp_bool := IsPharmacyConfig(orgid)
  629. if tmp_bool {
  630. if PettyCash(v.DrugId) {
  631. continue
  632. }
  633. if orgid != 10217 && orgid != 10188 && orgid != 9671 && orgid != 10164 && orgid != 10387 && orgid != 10375 {
  634. ////判断药品库存是否充足
  635. kou := FenDrugInventory(v, orgid)
  636. if !kou {
  637. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  638. return
  639. }
  640. }
  641. //扣减库存
  642. err = FenStock(orgid, creater, v)
  643. if err != nil {
  644. err = fmt.Errorf("!:%v", err)
  645. return
  646. }
  647. }
  648. }
  649. //修改状态
  650. errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id in (?)", hid).Updates(map[string]interface{}{
  651. "is_medicine": 1,
  652. "people": 0,
  653. "dispensing_time": time_now,
  654. "updated_time": time.Now().Unix(),
  655. }).Error
  656. if errs != nil {
  657. return errs
  658. }
  659. err1 := ChangeHisPrescription(hid)
  660. if err1 != nil {
  661. return err1
  662. }
  663. //针对大丰响水
  664. if orgid != 10217 && orgid != 10188 && orgid != 9671 && orgid != 10164 && orgid != 10387 && orgid != 10375 {
  665. err = tx.Raw("select id from xt_doctor_advice where status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and "+
  666. "user_org_id = ? and patient_id = ? and is_medicine = 0 and drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1)", stime, etime, orgid, patient_id, orgid).Scan(&xids).Error
  667. if err != nil {
  668. return
  669. }
  670. xid := make([]int64, 0)
  671. for _, v := range xids {
  672. xid = append(xid, v.Id)
  673. }
  674. var advice []*models.HisDoctorAdviceInfo
  675. err = tx.Raw("select * from xt_doctor_advice where id in (?) and status = 1", xid).Scan(&advice).Error
  676. if err != nil {
  677. return
  678. }
  679. for _, v := range advice {
  680. tmp_bool := IsPharmacyConfig(orgid)
  681. if tmp_bool {
  682. if PettyCash(v.DrugId) {
  683. continue
  684. }
  685. kou := FenDrugInventory(v, orgid)
  686. if !kou {
  687. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  688. return
  689. }
  690. //扣减库存
  691. err = FenStock(orgid, creater, v)
  692. if err != nil {
  693. err = fmt.Errorf("!:%v", err)
  694. return
  695. }
  696. }
  697. }
  698. //修改状态
  699. errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id in (?)", xid).Updates(map[string]interface{}{
  700. "is_medicine": 1,
  701. "people": 0,
  702. "dispensing_time": time_now,
  703. "updated_time": time.Now().Unix(),
  704. }).Error
  705. if errs1 != nil {
  706. return errs1
  707. }
  708. }
  709. //生成明细记录
  710. tmp_ph := models.Pharmary{
  711. UserOrgId: orgid,
  712. PatientId: patient_id,
  713. Ctime: time.Now().Unix(),
  714. Mtime: time.Now().Unix(),
  715. Status: 1,
  716. RecordDate: time_now,
  717. }
  718. err = tx.Create(&tmp_ph).Error
  719. return
  720. }
  721. // 患者退药按钮点击
  722. func DrugWithdrawal(orgid, patient_id, stime, etime, creater int64) (err error) {
  723. //开事务
  724. tx := XTWriteDB().Begin()
  725. defer func() {
  726. if err != nil {
  727. utils.ErrorLog("事务失败,原因为: %v", err)
  728. tx.Rollback()
  729. } else {
  730. tx.Commit()
  731. }
  732. }()
  733. map_time := make(map[int64]int64)
  734. var hids []*models.TmpID
  735. var xids []*models.TmpID
  736. //var hid,xid []int64//查询已发药的药品
  737. err = tx.Raw("select id,dispensing_time from his_doctor_advice_info where status = 1 and advice_date >= ? and advice_date <= ? "+
  738. "and user_org_id = ? and patient_id = ? and is_medicine = 1 and drug_id in (select id from xt_base_drug where org_id = ? and status = 1)", stime, etime, orgid, patient_id, orgid).Scan(&hids).Error
  739. if err != nil {
  740. return
  741. }
  742. hid := make([]int64, 0)
  743. for _, v := range hids {
  744. hid = append(hid, v.Id)
  745. map_time[v.DispensingTime] = v.DispensingTime
  746. }
  747. //修改状态
  748. errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id in (?)", hid).Updates(map[string]interface{}{
  749. "is_medicine": 0,
  750. "people": creater,
  751. "dispensing_time": 0,
  752. "updated_time": time.Now().Unix(),
  753. }).Error
  754. if errs != nil {
  755. return errs
  756. }
  757. err1 := ChangeHisPrescriptionT(hid)
  758. if err1 != nil {
  759. return err1
  760. }
  761. var advice_info []*models.HisDoctorAdviceInfo
  762. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", hid).Find(&advice_info).Error
  763. if err != nil {
  764. return
  765. }
  766. for _, v := range advice_info {
  767. //扣减库存
  768. if !IsPharmacyDelivery(v.DrugId, orgid) {
  769. continue
  770. }
  771. err = DrugAutoAddCancelInfo(v, creater)
  772. if err != nil {
  773. err = fmt.Errorf("!:%v", err)
  774. return
  775. }
  776. //drug, _ := FindBaseDrugLibRecordSeven(orgid, v.DrugId)
  777. //查询默认仓库
  778. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  779. //查询默认仓库剩余多少库存
  780. var sum_count int64
  781. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  782. for _, its := range stockInfo {
  783. baseDrug, _ := GetBaseDrugMedical(its.DrugId)
  784. if its.MaxUnit == baseDrug.MaxUnit {
  785. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  786. }
  787. sum_count += its.StockMaxNumber + its.StockMinNumber
  788. }
  789. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  790. //扣减库存
  791. UpdateDrugStockCount(v.DrugId, v.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
  792. }
  793. err = tx.Raw("select id,dispensing_time from xt_doctor_advice where status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and "+
  794. "user_org_id = ? and patient_id = ? and is_medicine = 1 and drug_id in (select id from xt_base_drug where org_id = ? and status = 1)", stime, etime, orgid, patient_id, orgid).Scan(&xids).Error
  795. if err != nil {
  796. return
  797. }
  798. xid := make([]int64, 0)
  799. for _, v := range xids {
  800. xid = append(xid, v.Id)
  801. map_time[v.DispensingTime] = v.DispensingTime
  802. }
  803. //修改状态
  804. errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id in (?)", xid).Updates(map[string]interface{}{
  805. "is_medicine": 0,
  806. "people": creater,
  807. "dispensing_time": 0,
  808. "updated_time": time.Now().Unix(),
  809. }).Error
  810. if errs1 != nil {
  811. return errs1
  812. }
  813. var advice []*models.HisDoctorAdviceInfo
  814. err = tx.Raw("select * from xt_doctor_advice where id in (?) and status = 1", xid).Scan(&advice).Error
  815. if err != nil {
  816. return
  817. }
  818. for _, v := range advice {
  819. //扣减库存
  820. if !IsPharmacyDelivery(v.DrugId, orgid) {
  821. continue
  822. }
  823. err = DrugAutoAddCancelInfo(v, creater)
  824. if err != nil {
  825. err = fmt.Errorf("!:%v", err)
  826. return
  827. }
  828. //drug, _ := FindBaseDrugLibRecordSeven(orgid, v.DrugId)
  829. //查询默认仓库
  830. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  831. //查询默认仓库剩余多少库存
  832. var sum_count int64
  833. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  834. for _, its := range stockInfo {
  835. baseDrug, _ := GetBaseDrugMedical(its.DrugId)
  836. if its.MaxUnit == baseDrug.MaxUnit {
  837. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  838. }
  839. sum_count += its.StockMaxNumber + its.StockMinNumber
  840. }
  841. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  842. //扣减库存
  843. UpdateDrugStockCount(v.DrugId, v.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
  844. }
  845. //删除掉发药明细
  846. for k, _ := range map_time {
  847. err = tx.Model(&models.Pharmary{}).Where("user_org_id = ? and record_date = ? and patient_id = ? ", orgid, k, patient_id).Updates(map[string]interface{}{
  848. "status": 0,
  849. "mtime": time.Now().Unix(),
  850. }).Error
  851. if err != nil {
  852. return
  853. }
  854. }
  855. return
  856. }
  857. // 获取患者名称
  858. func FindUserName(patient_id int64) (name string) {
  859. var tmp models.GetHisName
  860. XTReadDB().Model(&models.GetHisName{}).Where("id = ?", patient_id).Find(&tmp)
  861. name = tmp.Name
  862. return
  863. }
  864. // 获取药品名称
  865. func FindDrugsName(drug_id int64) (name string) {
  866. var tmp models.Drug
  867. XTReadDB().Model(&models.Drug{}).Where("id = ?", drug_id).Find(&tmp)
  868. name = tmp.DrugName
  869. return
  870. }
  871. // 药品发药按钮点击(
  872. func MedicineDeparture(ids string, creater, orgid int64) (err error) {
  873. //开事务
  874. tx := XTWriteDB().Begin()
  875. defer func() {
  876. if err != nil {
  877. utils.ErrorLog("事务失败,原因为: %v", err)
  878. tx.Rollback()
  879. } else {
  880. tx.Commit()
  881. }
  882. }()
  883. //处理下字符串
  884. t_ids := ""
  885. if ids[len(ids)-1] == 44 {
  886. t_ids = ids[:len(ids)-1]
  887. } else {
  888. t_ids = ids
  889. }
  890. time_now := time.Now().Unix()
  891. tmp_id := strings.Split(t_ids, ",")
  892. var parameter string //测试
  893. patient_id := make(map[int64]int64)
  894. for _, v := range tmp_id {
  895. front := v[:1]
  896. after := v[1:]
  897. if front == "h" {
  898. var advice_info []*models.HisDoctorAdviceInfo
  899. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and status = 1", after).Find(&advice_info).Error
  900. if err != nil {
  901. return
  902. }
  903. for _, v := range advice_info {
  904. if _, ok := patient_id[v.PatientId]; !ok {
  905. patient_id[v.PatientId] = v.PatientId
  906. }
  907. tmp_bool := IsPharmacyConfig(orgid)
  908. if tmp_bool {
  909. if PettyCash(v.DrugId) {
  910. continue
  911. }
  912. kou := FenDrugInventory(v, orgid)
  913. if !kou {
  914. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  915. tmp := models.XtErrs{
  916. OrgId: orgid,
  917. Route: "creater:" + config.ToString(creater),
  918. Parameter: parameter,
  919. Text_err: err.Error(),
  920. }
  921. XTWriteDB().Create(&tmp)
  922. return
  923. } else {
  924. tmps, _ := json.Marshal(advice_info) //
  925. parameter = parameter + string(tmps) //
  926. }
  927. //扣减库存
  928. err = FenStock(orgid, creater, v)
  929. if err != nil {
  930. err = fmt.Errorf("!:%v", err)
  931. return
  932. }
  933. }
  934. }
  935. //修改状态
  936. errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ?", after).Updates(map[string]interface{}{
  937. "is_medicine": 1,
  938. "people": creater,
  939. "dispensing_time": time_now,
  940. "updated_time": time.Now().Unix(),
  941. }).Error
  942. if errs != nil {
  943. return errs
  944. }
  945. err1 := ChangeHisPrescriptionid(after)
  946. if err1 != nil {
  947. return err1
  948. }
  949. } else {
  950. var advice []*models.HisDoctorAdviceInfo
  951. err = tx.Raw("select * from xt_doctor_advice where id = ? and status = 1 and (advice_type = 2 or advice_type = 3)", after).Scan(&advice).Error
  952. if err != nil {
  953. return
  954. }
  955. for _, v := range advice {
  956. if _, ok := patient_id[v.PatientId]; !ok {
  957. patient_id[v.PatientId] = v.PatientId
  958. }
  959. tmp_bool := IsPharmacyConfig(orgid)
  960. if tmp_bool {
  961. if PettyCash(v.DrugId) {
  962. continue
  963. }
  964. kou := FenDrugInventory(v, orgid)
  965. if !kou {
  966. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  967. tmp := models.XtErrs{
  968. OrgId: orgid,
  969. Route: "creater:" + config.ToString(creater),
  970. Parameter: parameter,
  971. Text_err: err.Error(),
  972. }
  973. XTWriteDB().Create(&tmp)
  974. return
  975. } else {
  976. tmps, _ := json.Marshal(advice) //
  977. parameter = parameter + string(tmps) //
  978. }
  979. //扣减库存
  980. err = FenStock(orgid, creater, v)
  981. if err != nil {
  982. err = fmt.Errorf("!:%v", err)
  983. return
  984. }
  985. }
  986. }
  987. //修改状态
  988. errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id = ?", after).Updates(map[string]interface{}{
  989. "is_medicine": 1,
  990. "people": creater,
  991. "dispensing_time": time_now,
  992. "updated_time": time.Now().Unix(),
  993. }).Error
  994. if errs1 != nil {
  995. return errs1
  996. }
  997. }
  998. }
  999. for _, v := range patient_id {
  1000. //生成明细记录
  1001. tmp_ph := models.Pharmary{
  1002. UserOrgId: orgid,
  1003. PatientId: v,
  1004. Ctime: time.Now().Unix(),
  1005. Mtime: time.Now().Unix(),
  1006. Status: 1,
  1007. RecordDate: time_now,
  1008. }
  1009. err = tx.Create(&tmp_ph).Error
  1010. if err != nil {
  1011. return err
  1012. }
  1013. }
  1014. return
  1015. }
  1016. // 获取领药人姓名(
  1017. func GetUserAdminName(id, orgID int64) string {
  1018. var tmp models.XTSgjUserAdminRole
  1019. XTReadDB().Model(&models.XTSgjUserAdminRole{}).Where("admin_user_id = ? and org_id = ?", id, orgID).Find(&tmp)
  1020. return tmp.UserName
  1021. }
  1022. // 根据id查询是否已发药,true已发药,false未发药gai
  1023. func GiveTheMedicine(id int64) bool {
  1024. var xue models.PharmacyDoctorAdvice
  1025. XTReadDB().Model(&models.PharmacyDoctorAdvice{}).Where("id = ?", id).Find(&xue)
  1026. if xue.IsMedicine == 1 {
  1027. return true
  1028. }
  1029. return false
  1030. }
  1031. // 查询改组中的药品是否包含已发药的,true已发药,false未发药
  1032. func GiveGroupMedicine(orgid, groupNo int64) bool {
  1033. var total int
  1034. XTReadDB().Model(&models.DoctorAdvice{}).Where("user_org_id = ? and groupno = ? and status = 1 and is_medicine = 1", orgid, groupNo).Count(&total)
  1035. if total > 0 {
  1036. return true
  1037. }
  1038. return false
  1039. }
  1040. // 查询处方中是否包含已发药的
  1041. func GiveChuMedicine(id int64) bool {
  1042. var total int
  1043. XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", id).Count(&total)
  1044. if total > 0 {
  1045. return true
  1046. }
  1047. return false
  1048. }
  1049. // 根据处方id查找该处方中是否存在已发药的药品
  1050. func IsChuIssuedDrugs(prescription_id int64) bool {
  1051. var total int
  1052. XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", prescription_id).Count(&total)
  1053. if total > 0 {
  1054. return true
  1055. }
  1056. return false
  1057. }
  1058. // 判断该药品是否通过药房管理出库,true是,false否
  1059. // id 药品id,org_id 机构id
  1060. func IsPharmacyDelivery(id, org_id int64) bool {
  1061. //判断药品是否通过药房管理
  1062. var total01 int
  1063. XTReadDB().Model(&models.PharmacyBaseDrug{}).Where("id = ? and is_pharmacy = 1", id).Count(&total01)
  1064. //判断该机构是否通过药房管理出库
  1065. var total02 int
  1066. XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and is_open = 1", org_id).Count(&total02)
  1067. if total01 > 0 && total02 > 0 {
  1068. return true
  1069. }
  1070. return false
  1071. }
  1072. // 判断机构是否通过药房管理出库
  1073. func IsPharmacyConfig(orgid int64) (bo bool) {
  1074. var total int
  1075. XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and is_open = 1 and status = 1", orgid).Count(&total)
  1076. if total > 0 {
  1077. return true
  1078. }
  1079. return false
  1080. }
  1081. // 根据患者id和发药时间,获取医生的id和姓名
  1082. func GetDoctorIds(id, recordtime, orgid int64) (doctor_id int64, doctor_name string, err error) {
  1083. var tmp []*models.TmpAdviceDoctor
  1084. //var tmp []int64
  1085. err = XTReadDB().Raw("select distinct advice_doctor from his_doctor_advice_info where "+
  1086. "status = 1 and dispensing_time = ? and user_org_id = ? and is_medicine = 1 and patient_id = ? and drug_id in (select id from xt_base_drug where org_id = ? and status = 1) "+
  1087. "union "+
  1088. "select distinct advice_doctor from xt_doctor_advice where "+
  1089. "status = 1 and (advice_type = 2 or advice_type = 3) and dispensing_time = ? and user_org_id = ? and is_medicine = 1 and patient_id = ? and drug_id in (select id from xt_base_drug where org_id = ? and status = 1) ",
  1090. recordtime, orgid, id, orgid, recordtime, orgid, id, orgid).Scan(&tmp).Error
  1091. if err != nil || len(tmp) == 0 {
  1092. return
  1093. }
  1094. doctor_id = tmp[0].AdviceDoctor
  1095. doctor_name = GetUserAdminName(doctor_id, orgid)
  1096. return
  1097. }
  1098. func MapToJson(param map[string]interface{}) string {
  1099. dataType, _ := json.Marshal(param)
  1100. dataString := string(dataType)
  1101. return dataString
  1102. }
  1103. // 封装扣减库存
  1104. func FenStock(orgid, creater int64, v *models.HisDoctorAdviceInfo) (err error) {
  1105. err = HisDrugsDelivery(orgid, creater, v)
  1106. if err != nil {
  1107. err = fmt.Errorf("!:%v", err)
  1108. return
  1109. }
  1110. //查询默认仓库
  1111. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  1112. //查询默认仓库剩余多少库存
  1113. var sum_count int64
  1114. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  1115. for _, its := range stockInfo {
  1116. baseDrug, _ := GetBaseDrugMedical(its.DrugId)
  1117. if its.MaxUnit == baseDrug.MaxUnit {
  1118. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  1119. }
  1120. sum_count += its.StockMaxNumber + its.StockMinNumber
  1121. }
  1122. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  1123. //扣减库存
  1124. UpdateDrugStockCount(v.DrugId, v.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
  1125. over, _ := FindOverCount(v.DrugId, v.UserOrgId, storeHouseConfig.DrugStorehouseOut)
  1126. UpdateActOut(over.ID, over.SumInCount, over.FlushCount, over.SumCancelCount)
  1127. return
  1128. }
  1129. // 封装查询药品库存是否足够
  1130. func FenDrugInventory(item *models.HisDoctorAdviceInfo, orgid int64) bool {
  1131. var total int64
  1132. var prescribing_number_total int64
  1133. houseConfig, _ := GetAllStoreHouseConfig(orgid)
  1134. ////查询该药品是否有库存
  1135. list, _ := GetDrugTotalCountTwenty(item.DrugId, item.UserOrgId, houseConfig.DrugStorehouseOut)
  1136. ////查询改药品信息
  1137. medical, _ := GetBaseDrugMedical(item.DrugId)
  1138. ////判断单位是否相等
  1139. if medical.MaxUnit == item.PrescribingNumberUnit {
  1140. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1141. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1142. //转化为最小单位
  1143. total = list.Count*medical.MinNumber + list.StockMinNumber
  1144. prescribing_number_total = count * medical.MinNumber
  1145. }
  1146. if medical.MinUnit == item.PrescribingNumberUnit {
  1147. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1148. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1149. total = list.Count*medical.MinNumber + list.StockMinNumber
  1150. prescribing_number_total = count
  1151. }
  1152. if prescribing_number_total <= total {
  1153. //可以扣减
  1154. return true
  1155. } else {
  1156. //不可扣减
  1157. return false
  1158. }
  1159. }
  1160. func GetAdviceIds1(ids []string, orgid, execution_staff int64, thetime time.Time) (tmp []string, err error) {
  1161. var advice []*models.DoctorAdvice
  1162. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND status = 1", ids).Find(&advice).Error
  1163. if err != nil {
  1164. return
  1165. }
  1166. for _, v := range advice {
  1167. if !IsPharmacyDelivery(v.DrugId, orgid) {
  1168. tmp = append(tmp, config.ToString(v.ID))
  1169. } else {
  1170. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? ", v.ID).Updates(map[string]interface{}{
  1171. "execution_staff": execution_staff,
  1172. "execution_time": thetime.Unix(),
  1173. "updated_time": time.Now().Unix(),
  1174. }).Error
  1175. if err != nil {
  1176. return
  1177. }
  1178. }
  1179. }
  1180. return
  1181. }
  1182. func GetAdviceIds2(ids []string, orgid, execution_staff int64, thetime time.Time) (tmp []string, err error) {
  1183. var advice []*models.HisDoctorAdviceInfoL
  1184. err = readDb.Model(&models.HisDoctorAdviceInfoL{}).Where("id IN (?) AND status = 1", ids).Find(&advice).Error
  1185. if err != nil {
  1186. return
  1187. }
  1188. for _, v := range advice {
  1189. if !IsPharmacyDelivery(v.DrugId, orgid) {
  1190. tmp = append(tmp, config.ToString(v.ID))
  1191. } else {
  1192. err = XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? ", v.ID).Updates(map[string]interface{}{
  1193. "execution_staff": execution_staff,
  1194. "execution_time": thetime.Unix(),
  1195. "updated_time": time.Now().Unix(),
  1196. }).Error
  1197. if err != nil {
  1198. return
  1199. }
  1200. }
  1201. }
  1202. return
  1203. }
  1204. func GetAdviceId1(id, orgid, execution_staff int64, thetime time.Time) (bo bool, err error) {
  1205. var advice models.DoctorAdvice
  1206. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND status = 1", id).Find(&advice).Error
  1207. if err != nil {
  1208. return false, err
  1209. }
  1210. if IsPharmacyDelivery(advice.DrugId, orgid) {
  1211. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? ", id).Updates(map[string]interface{}{
  1212. "execution_staff": execution_staff,
  1213. "execution_time": thetime.Unix(),
  1214. "updated_time": time.Now().Unix(),
  1215. }).Error
  1216. if err != nil {
  1217. return false, err
  1218. }
  1219. return true, err
  1220. }
  1221. return false, err
  1222. }
  1223. func GetAdviceId2(id, orgid, execution_staff int64, thetime time.Time) (bo bool, err error) {
  1224. var advice models.HisDoctorAdviceInfoL
  1225. err = readDb.Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? AND status = 1", id).Find(&advice).Error
  1226. if err != nil {
  1227. return false, err
  1228. }
  1229. if IsPharmacyDelivery(advice.DrugId, orgid) {
  1230. err = XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? ", id).Updates(map[string]interface{}{
  1231. "execution_staff": execution_staff,
  1232. "execution_time": thetime.Unix(),
  1233. "updated_time": time.Now().Unix(),
  1234. }).Error
  1235. if err != nil {
  1236. return false, err
  1237. }
  1238. return true, err
  1239. }
  1240. return false, err
  1241. }
  1242. // 判断药品是否零用
  1243. func PettyCash(id int64) bool {
  1244. drug := models.XtBaseDrug{}
  1245. XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug)
  1246. if drug.IsUse == 1 {
  1247. return true
  1248. }
  1249. return false
  1250. }
  1251. // 初始化
  1252. func InitDrugidIsNil(orgid, stime, etime int64) {
  1253. var advice []*models.DoctorAdvice
  1254. XTReadDB().Model(&models.DoctorAdvice{}).Where("drug_id = 0 and user_org_id = ? and status = 1 and created_time >= ? and created_time <= ?", orgid, stime, etime).Find(&advice)
  1255. for _, v := range advice {
  1256. XTWriteDB().Exec("update xt_doctor_advice set drug_id = (select id from xt_base_drug where drug_name = ? and org_id = ? and status = 1 limit 1) where id = ?", v.AdviceName, v.UserOrgId, v.ID)
  1257. }
  1258. }
  1259. // 改变处方状态(发药
  1260. func ChangeHisPrescription(tmp []int64) (err error) {
  1261. var advice_info []*models.HisDoctorAdviceInfo
  1262. err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", tmp).Find(&advice_info).Error
  1263. if err != nil {
  1264. if err == gorm.ErrRecordNotFound {
  1265. return nil
  1266. }
  1267. return
  1268. }
  1269. tmp_id := make(map[int64]int64)
  1270. for _, v := range advice_info {
  1271. tmp_id[v.PrescriptionId] = v.PrescriptionId
  1272. }
  1273. for k, _ := range tmp_id {
  1274. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", k).Updates(map[string]interface{}{
  1275. "is_medicine": 1,
  1276. "mtime": time.Now().Unix(),
  1277. }).Error
  1278. if err != nil {
  1279. return
  1280. }
  1281. }
  1282. return
  1283. }
  1284. // 改变处方状态(退药
  1285. func ChangeHisPrescriptionT(tmp []int64) (err error) {
  1286. var advice_info []*models.HisDoctorAdviceInfo
  1287. err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", tmp).Find(&advice_info).Error
  1288. if err != nil {
  1289. if err == gorm.ErrRecordNotFound {
  1290. return nil
  1291. }
  1292. return
  1293. }
  1294. tmp_id := make(map[int64]int64)
  1295. for _, v := range advice_info {
  1296. tmp_id[v.PrescriptionId] = v.PrescriptionId
  1297. }
  1298. for k, _ := range tmp_id {
  1299. var total int
  1300. //查询退药的
  1301. XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", k).Count(&total)
  1302. if total == 0 {
  1303. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", k).Updates(map[string]interface{}{
  1304. "is_medicine": 0,
  1305. "mtime": time.Now().Unix(),
  1306. }).Error
  1307. if err != nil {
  1308. return
  1309. }
  1310. }
  1311. }
  1312. return
  1313. }
  1314. // 改变处方状态
  1315. func ChangeHisPrescriptionid(id string) (err error) {
  1316. var advice models.HisDoctorAdviceInfo
  1317. err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and status = 1", id).Find(&advice).Error
  1318. if err != nil {
  1319. if err == gorm.ErrRecordNotFound {
  1320. return nil
  1321. }
  1322. return
  1323. }
  1324. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", advice.PrescriptionId).Updates(map[string]interface{}{
  1325. "is_medicine": 1,
  1326. "mtime": time.Now().Unix(),
  1327. }).Error
  1328. if err != nil {
  1329. return
  1330. }
  1331. return
  1332. }
  1333. // 获取药品规格
  1334. func ReplacementDrugs(orgid int64, special bool) (list []*models.ReplacementDrugs, err error) {
  1335. var g errgroup.Group
  1336. var lib []*models.BaseDrugLib
  1337. var lock sync.Mutex
  1338. if special {
  1339. err = readDb.Model(&models.BaseDrugLib{}).Where("org_id = ? and drug_status not like '%停用%' AND status = 1 and is_special_diseases = 1", orgid).Find(&lib).Error
  1340. } else {
  1341. err = readDb.Model(&models.BaseDrugLib{}).Where("org_id = ? and drug_status not like '%停用%' AND status = 1 ", orgid).Find(&lib).Error
  1342. }
  1343. if err != nil {
  1344. return
  1345. }
  1346. for _, v := range lib {
  1347. v := v
  1348. g.Go(func() error {
  1349. //var tmp models.Manufacturer
  1350. //err := readDb.Model(&models.Manufacturer{}).Where("id = ?", v.Manufacturer).Find(&tmp).Error
  1351. //if err != nil {
  1352. // return err
  1353. //}
  1354. lock.Lock()
  1355. if orgid == 10206 {
  1356. list = append(list, &models.ReplacementDrugs{
  1357. Id: v.ID,
  1358. Name: v.DrugName + " " + v.DrugSpec + " " + v.Number,
  1359. })
  1360. lock.Unlock()
  1361. } else {
  1362. list = append(list, &models.ReplacementDrugs{
  1363. Id: v.ID,
  1364. Name: v.DrugName + " " + v.Dose + v.DoseUnit + "*" + config.ToString(v.MinNumber) + v.MinUnit + "/" + v.MaxUnit + " " + v.Number,
  1365. })
  1366. lock.Unlock()
  1367. }
  1368. return err
  1369. })
  1370. }
  1371. if errs := g.Wait(); errs != nil {
  1372. err = errs
  1373. return
  1374. }
  1375. return
  1376. }
  1377. // 获取药品规格,不考虑特殊病
  1378. func ReplacementDrugsT(orgid int64) (list []*models.ReplacementDrugs, err error) {
  1379. var lib []*models.BaseDrugLib
  1380. err = readDb.Model(&models.BaseDrugLib{}).Where("org_id = ? AND status = 1 and drug_status not like '%停用%' ", orgid).Find(&lib).Error
  1381. if err != nil {
  1382. return
  1383. }
  1384. for _, v := range lib {
  1385. //var tmp models.Manufacturer
  1386. //err = readDb.Model(&models.Manufacturer{}).Where("id = ?", v.Manufacturer).Find(&tmp).Error
  1387. //if err != nil {
  1388. // return
  1389. //}
  1390. if orgid == 10206 {
  1391. list = append(list, &models.ReplacementDrugs{
  1392. Id: v.ID,
  1393. Name: v.DrugName + " " + v.DrugSpec + " " + v.Number,
  1394. })
  1395. } else {
  1396. list = append(list, &models.ReplacementDrugs{
  1397. Id: v.ID,
  1398. Name: v.DrugName + " " + v.Dose + v.DoseUnit + "*" + config.ToString(v.MinNumber) + v.MinUnit + "/" + v.PrescribingNumberUnit + " " + v.Number,
  1399. })
  1400. }
  1401. }
  1402. return
  1403. }
  1404. // 根据药品id获取药品名字
  1405. func IdToDrugName(id int64) (name string) {
  1406. var lib models.BaseDrugLib
  1407. readDb.Model(&models.BaseDrugLib{}).Where("id = ?", id).Find(&lib)
  1408. return lib.DrugName
  1409. }
  1410. // 获取项目和耗材和套餐
  1411. func ProjectConsumables3(orgid int64) (list []*models.DropDownList, err error) {
  1412. var project []*models.XtHisProject
  1413. project, err = GetHisProject(orgid)
  1414. if err != nil {
  1415. return
  1416. }
  1417. for _, v := range project {
  1418. list = append(list, &models.DropDownList{
  1419. Id: "p" + config.ToString(v.ID),
  1420. Name: v.ProjectName,
  1421. })
  1422. }
  1423. var good []*models.GoodInfo
  1424. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? and status = 1", orgid).Find(&good).Error
  1425. if err != nil {
  1426. return
  1427. }
  1428. for _, v := range good {
  1429. list = append(list, &models.DropDownList{
  1430. Id: "i" + config.ToString(v.ID),
  1431. Name: v.GoodName,
  1432. })
  1433. }
  1434. var team []*models.XtHisProjectTeam
  1435. team, err = GetAllProjectTeam(orgid)
  1436. if err != nil {
  1437. return
  1438. }
  1439. for _, v := range team {
  1440. list = append(list, &models.DropDownList{
  1441. Id: "h" + config.ToString(v.ID),
  1442. Name: v.ProjectTeam,
  1443. })
  1444. }
  1445. return
  1446. }
  1447. // 获取项目和耗材
  1448. func ProjectConsumables2(orgid int64, special bool) (list []*models.DropDownList, err error) {
  1449. var project []*models.XtHisProject
  1450. if special {
  1451. err = XTReadDB().Model(&project).Where("user_org_id = ? and medical_status != 1 and status = 1 and disease_directory = 1", orgid).Find(&project).Error
  1452. } else {
  1453. err = XTReadDB().Model(&project).Where("user_org_id = ? and medical_status != 1 and status = 1", orgid).Find(&project).Error
  1454. }
  1455. if err != nil {
  1456. return
  1457. }
  1458. for _, v := range project {
  1459. list = append(list, &models.DropDownList{
  1460. Id: "p" + config.ToString(v.ID),
  1461. Name: v.ProjectName,
  1462. })
  1463. }
  1464. var good []*models.GoodInfo
  1465. if special {
  1466. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? and good_status not like '%停用%' and status = 1 and is_special_diseases = 1", orgid).Find(&good).Error
  1467. } else {
  1468. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? and good_status not like '%停用%' and status = 1", orgid).Find(&good).Error
  1469. }
  1470. if err != nil {
  1471. return
  1472. }
  1473. for _, v := range good {
  1474. list = append(list, &models.DropDownList{
  1475. Id: "i" + config.ToString(v.ID),
  1476. Name: v.GoodName + " " + v.SpecificationName,
  1477. })
  1478. }
  1479. return
  1480. }
  1481. // 获取项目和耗材不考虑特殊病
  1482. func ProjectConsumables2T(orgid int64) (list []*models.DropDownList, err error) {
  1483. var project []*models.XtHisProject
  1484. err = XTReadDB().Model(&project).Where("user_org_id = ? and status = 1 and medical_status != 1", orgid).Find(&project).Error
  1485. if err != nil {
  1486. return
  1487. }
  1488. for _, v := range project {
  1489. list = append(list, &models.DropDownList{
  1490. Id: "p" + config.ToString(v.ID),
  1491. Name: v.ProjectName,
  1492. })
  1493. }
  1494. var good []*models.GoodInfo
  1495. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? and status = 1 and good_status != 1", orgid).Find(&good).Error
  1496. if err != nil {
  1497. return
  1498. }
  1499. for _, v := range good {
  1500. list = append(list, &models.DropDownList{
  1501. Id: "i" + config.ToString(v.ID),
  1502. Name: v.GoodName + " " + v.SpecificationName,
  1503. })
  1504. }
  1505. return
  1506. }
  1507. // 查询药品
  1508. func FindPatientDrug(orgid, patient_id, drugid, mode int64) bool {
  1509. var total int
  1510. XTReadDB().Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and status = 1 and "+
  1511. "prescription_id in (select id from his_prescription_info_template where status = 1 and user_org_id = ? and type = 1 and p_template_id in ("+
  1512. "select id from his_prescription_template where status= 1 and user_org_id = ? and mode = ?))", orgid, patient_id, drugid, orgid, orgid, mode).Count(&total)
  1513. if total > 0 {
  1514. return true
  1515. }
  1516. return false
  1517. }
  1518. // 查询项目
  1519. func FindPatientXiang(orgid, patient_id, drugid, mode int64) bool {
  1520. var total int
  1521. XTReadDB().Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and status = 1 and type = 2 and "+
  1522. "prescription_id in (select id from his_prescription_info_template where status = 1 and user_org_id = ? and type = 2 and p_template_id in ("+
  1523. "select id from his_prescription_template where status= 1 and user_org_id = ? and mode = ?))", orgid, patient_id, drugid, orgid, orgid, mode).Count(&total)
  1524. if total > 0 {
  1525. return true
  1526. }
  1527. return false
  1528. }
  1529. // 查询耗材
  1530. func FindPatientXiang2(orgid, patient_id, drugid, mode int64) bool {
  1531. var total int
  1532. XTReadDB().Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and status = 1 and type = 3 and "+
  1533. "prescription_id in (select id from his_prescription_info_template where status = 1 and user_org_id = ? and type = 2 and p_template_id in ("+
  1534. "select id from his_prescription_template where status= 1 and user_org_id = ? and mode = ?))", orgid, patient_id, drugid, orgid, orgid, mode).Count(&total)
  1535. if total > 0 {
  1536. return true
  1537. }
  1538. return false
  1539. }
  1540. // types处方类型,1.药品 2.项目,model透析模式
  1541. func GetHisInfoTempalteId(model, orgid, types int64) (tp []int64) {
  1542. type tmpid struct {
  1543. Id int64
  1544. }
  1545. var tmp []*tmpid
  1546. XTReadDB().Raw("select id from his_prescription_info_template where status = 1 and user_org_id = ? and type = ? and p_template_id in ("+
  1547. "select id from his_prescription_template where status = 1 and user_org_id = ? and mode = ?)", orgid, types, orgid, model).Scan(&tmp)
  1548. for _, v := range tmp {
  1549. tp = append(tp, v.Id)
  1550. }
  1551. return
  1552. }
  1553. // 替换药品处方模板
  1554. func ReplaceDrugPrescriptionTemplate(orgid, patient_id, drugid int64, ids []int64, s models.HisPrescriptionAdviceTemplate, tx *gorm.DB) (err error) {
  1555. err = tx.Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and "+
  1556. "status = 1 and prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
  1557. "drug_id": s.DrugId,
  1558. "advice_name": s.AdviceName,
  1559. "single_dose": s.SingleDose,
  1560. "single_dose_unit": s.SingleDoseUnit,
  1561. "delivery_way": s.DeliveryWay,
  1562. "execution_frequency": s.ExecutionFrequency,
  1563. "day": s.Day,
  1564. "prescribing_number": s.PrescribingNumber,
  1565. "prescribing_number_unit": s.PrescribingNumberUnit,
  1566. "price": s.Price,
  1567. "remark": s.Remark,
  1568. "updated_time": time.Now().Unix(),
  1569. }).Error
  1570. return err
  1571. }
  1572. // 替换项目模板
  1573. func ReplaceProjectPrescriptionTemplate(orgid, patient_id, drugid int64, ids []int64, s models.HisPrescriptionProjectTemplate, tx *gorm.DB) (err error) {
  1574. err = tx.Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and "+
  1575. "status = 1 and prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
  1576. "project_id": s.ProjectId,
  1577. "type": s.Type,
  1578. "frequency_type": s.FrequencyType,
  1579. "day_count": s.DayCount,
  1580. "week_day": s.WeekDay,
  1581. "price": s.Price,
  1582. "count": s.Count,
  1583. "single_dose": s.SingleDose,
  1584. "delivery_way": s.DeliveryWay,
  1585. "execution_frequency": s.ExecutionFrequency,
  1586. "remark": s.Remark,
  1587. "day": s.Day,
  1588. "unit": s.Unit,
  1589. "mtime": time.Now().Unix(),
  1590. }).Error
  1591. return err
  1592. }
  1593. // 删除项目模板
  1594. func DeleteProjectTemplate(orgid, patient_id, drugid int64, ids []int64, tx *gorm.DB) (err error) {
  1595. err = tx.Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and "+
  1596. "prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
  1597. "status": 0,
  1598. "mtime": time.Now().Unix(),
  1599. }).Error
  1600. return err
  1601. }
  1602. // 删除处方模板
  1603. func DeletePrescriptionTemplate(orgid, patient_id, drugid int64, ids []int64, tx *gorm.DB) (err error) {
  1604. err = tx.Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and "+
  1605. "prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
  1606. "status": 0,
  1607. "updated_time": time.Now().Unix(),
  1608. }).Error
  1609. return err
  1610. }
  1611. // 根据透析模式和患者id获取该患者的处方内容
  1612. func PTemplateInformation(orgid, mode_id, patient_id int64) (tmp interface{}, err error) {
  1613. //获取处方id
  1614. var tt []*models.HisPrescriptionTemplate
  1615. err = XTReadDB().Raw("select id from his_prescription_template where status = 1 and user_org_id = ? and mode = ? and patient_id = ?", orgid, mode_id, patient_id).Scan(&tt).Error
  1616. if len(tt) > 1 {
  1617. err = fmt.Errorf("数据有误")
  1618. return
  1619. }
  1620. x := make(map[int64]string, 0) //项目
  1621. h := make(map[int64]string, 0) //耗材
  1622. for _, v := range tt {
  1623. var prescriptions []*models.HisPrescriptionInfoTemplateL
  1624. prescriptions, err = GetHisPrescriptionTemplateL(v.ID, orgid)
  1625. if err != nil {
  1626. return
  1627. }
  1628. for i := 0; i < len(prescriptions); i++ {
  1629. for j := 0; j < len(prescriptions[i].HisPrescriptionProjectTemplate); j++ {
  1630. if prescriptions[i].HisPrescriptionProjectTemplate[j].Type == 2 {
  1631. //项目
  1632. if v, ok := x[prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.StatisticalClassification]; ok {
  1633. prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.Translate = v
  1634. } else {
  1635. prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.Translate, err = TranslateZu(prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.StatisticalClassification, orgid, "统计分类")
  1636. if err != nil {
  1637. return
  1638. }
  1639. x[prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.StatisticalClassification] = prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.Translate
  1640. }
  1641. }
  1642. if prescriptions[i].HisPrescriptionProjectTemplate[j].Type == 3 {
  1643. //耗材
  1644. if v, ok := h[prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.GoodKind]; ok {
  1645. prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.Translate = v
  1646. } else {
  1647. prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.Translate = "耗材"
  1648. h[prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.GoodKind] = prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.Translate
  1649. }
  1650. }
  1651. }
  1652. }
  1653. tmp = prescriptions
  1654. }
  1655. return
  1656. }
  1657. func DeleteOne(types, id int64) (err error) {
  1658. if types == 1 {
  1659. //药品
  1660. err = XTWriteDB().Model(&models.HisPrescriptionAdviceTemplate{}).Where("id = ?", id).Updates(map[string]interface{}{
  1661. "status": 0,
  1662. "updated_time": time.Now().Unix(),
  1663. }).Error
  1664. if err != nil {
  1665. return
  1666. }
  1667. } else if types == 2 {
  1668. //项目
  1669. err = XTWriteDB().Model(&models.HisPrescriptionProjectTemplateL{}).Where("id = ?", id).Updates(map[string]interface{}{
  1670. "status": 0,
  1671. "mtime": time.Now().Unix(),
  1672. }).Error
  1673. if err != nil {
  1674. return
  1675. }
  1676. } else {
  1677. err = fmt.Errorf("参数错误")
  1678. return
  1679. }
  1680. return
  1681. }
  1682. // 收尾工作
  1683. func Scavenger(orgid int64) (err error) {
  1684. //开事务
  1685. tx := XTWriteDB().Begin()
  1686. defer func() {
  1687. if err != nil {
  1688. utils.ErrorLog("事务失败,原因为: %v", err)
  1689. tx.Rollback()
  1690. } else {
  1691. tx.Commit()
  1692. }
  1693. }()
  1694. //查询该机构的所有模板
  1695. var tmp1 []*models.HisPrescriptionTemplate
  1696. var tmp2 []*models.HisPrescriptionInfoTemplate
  1697. deltmp1 := make([]int64, 0) //清除的数据
  1698. deltmp2 := make([]int64, 0) //清除的数据
  1699. err = tx.Model(&models.HisPrescriptionTemplate{}).Where("user_org_id = ? and status = 1", orgid).Find(&tmp1).Error
  1700. if err != nil {
  1701. return
  1702. }
  1703. for _, v := range tmp1 {
  1704. var total1 int
  1705. tx.Model(&models.HisPrescriptionInfoTemplate{}).Where("user_org_id = ? and p_template_id = ? and status = 1", orgid, v.ID).Count(&total1)
  1706. if total1 == 0 {
  1707. deltmp1 = append(deltmp1, v.ID)
  1708. }
  1709. }
  1710. err = tx.Model(&models.HisPrescriptionInfoTemplate{}).Where("user_org_id = ? and status = 1", orgid).Find(&tmp2).Error
  1711. if err != nil {
  1712. return
  1713. }
  1714. for _, v := range tmp2 {
  1715. if v.Type == 1 {
  1716. var total2 int
  1717. tx.Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and prescription_id = ? and status = 1", orgid, v.ID).Count(&total2)
  1718. if total2 == 0 {
  1719. deltmp2 = append(deltmp2, v.ID)
  1720. }
  1721. }
  1722. if v.Type == 2 {
  1723. var total3 int
  1724. tx.Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and prescription_id = ? and status = 1", orgid, v.ID).Count(&total3)
  1725. if total3 == 0 {
  1726. deltmp2 = append(deltmp2, v.ID)
  1727. }
  1728. }
  1729. }
  1730. //清除1
  1731. err = tx.Model(&models.HisPrescriptionTemplate{}).Where("id in (?)", deltmp1).Updates(map[string]interface{}{
  1732. "status": 0,
  1733. "mtime": time.Now().Unix(),
  1734. }).Error
  1735. if err != nil {
  1736. return
  1737. }
  1738. //清除1
  1739. err = tx.Model(&models.HisPrescriptionInfoTemplate{}).Where("id in (?)", deltmp2).Updates(map[string]interface{}{
  1740. "status": 0,
  1741. "mtime": time.Now().Unix(),
  1742. }).Error
  1743. if err != nil {
  1744. return
  1745. }
  1746. return
  1747. }
  1748. // 分区和收费(患者发药
  1749. func PartitionAndLayout(stime, etime, orgid, shift, partition int64, flist []*models.TmpPatient) (tmp []*models.TmpPatient, err error) {
  1750. //获取排班
  1751. var sch []*models.XtScheduleTwo
  1752. ma := make(map[int64]int64)
  1753. s := "user_org_id = ? and status = 1 and schedule_date >= ? and schedule_date <= ?"
  1754. if shift != 0 {
  1755. s = s + " and schedule_type = " + config.ToString(shift)
  1756. }
  1757. if partition != 0 {
  1758. s = s + " and partition_id = " + config.ToString(partition)
  1759. }
  1760. if shift == 0 && partition == 0 {
  1761. return flist, nil
  1762. }
  1763. err = XTReadDB().Model(&models.XtScheduleTwo{}).Where(s, orgid, stime, etime).Find(&sch).Error
  1764. if err != nil {
  1765. return
  1766. }
  1767. for _, v := range sch {
  1768. ma[v.PatientId] = v.PatientId
  1769. }
  1770. for _, v := range flist {
  1771. if _, ok := ma[v.PatientID]; ok {
  1772. tmp = append(tmp, v)
  1773. }
  1774. }
  1775. return
  1776. }
  1777. // 分区和收费(药品发药
  1778. func PartitionAndLayoutDrug(deliveryway string, stime, etime, orgid, shift, partition int64, flist []*models.PatientInformation) (tmp []*models.PatientInformation, err error) {
  1779. //获取排班
  1780. var sch []*models.XtScheduleTwo
  1781. ma := make(map[int64]int64)
  1782. s := "user_org_id = ? and status = 1 and schedule_date >= ? and schedule_date <= ?"
  1783. if shift != 0 {
  1784. s = s + " and schedule_type = " + config.ToString(shift)
  1785. }
  1786. if partition != 0 {
  1787. s = s + " and partition_id = " + config.ToString(partition)
  1788. }
  1789. if shift == 0 && partition == 0 {
  1790. for _, v := range flist {
  1791. ma[v.PatientId] = v.PatientId
  1792. }
  1793. for _, v := range flist {
  1794. if _, ok := ma[v.PatientId]; ok && (v.Usage == deliveryway || deliveryway == "全部") {
  1795. tmp = append(tmp, v)
  1796. }
  1797. }
  1798. return tmp, nil
  1799. }
  1800. err = XTWriteDB().Model(&models.XtScheduleTwo{}).Where(s, orgid, stime, etime).Find(&sch).Error
  1801. if err != nil {
  1802. return
  1803. }
  1804. for _, v := range sch {
  1805. ma[v.PatientId] = v.PatientId
  1806. }
  1807. for _, v := range flist {
  1808. if _, ok := ma[v.PatientId]; ok && (v.Usage == deliveryway || deliveryway == "全部") {
  1809. tmp = append(tmp, v)
  1810. }
  1811. }
  1812. return
  1813. }
  1814. // 给药途径
  1815. func Administration(deliveryway string, orgid int64, flist []*models.ListOfDrugs) (tmp []*models.ListOfDrugs, err error) {
  1816. //查询药品
  1817. var pp []*models.PharmacyBaseDrug
  1818. s := "org_id = ? and status = 1"
  1819. if deliveryway == "全部" {
  1820. tmp = flist
  1821. return
  1822. } else {
  1823. s = s + " and delivery_way = '" + deliveryway + "'"
  1824. }
  1825. err = XTReadDB().Model(&models.PharmacyBaseDrug{}).Where(s, orgid).Find(&pp).Error
  1826. if err != nil {
  1827. return
  1828. }
  1829. ma := make(map[int64]int64)
  1830. for _, v := range pp {
  1831. ma[v.ID] = v.ID
  1832. }
  1833. for _, v := range flist {
  1834. if _, ok := ma[v.ID]; ok {
  1835. tmp = append(tmp, v)
  1836. }
  1837. }
  1838. return
  1839. }
  1840. // 计算总量
  1841. func CalculateTheTotalAmount(tmp []*models.PatientInformation, drug_id int64) (total string, err error) {
  1842. //获取药品信息
  1843. var t models.SpBaseDrug
  1844. err = XTReadDB().Model(&models.SpBaseDrug{}).Where("id = ?", drug_id).Find(&t).Error
  1845. if err != nil {
  1846. return
  1847. }
  1848. terr, maxunit, minunit := int64(0), int64(0), int64(0) //terr异常的单位数量,maxunit包装单位数量,minunit拆零单位数量
  1849. minmunber := t.MinNumber //拆零数量
  1850. terrUnit := ""
  1851. for _, v := range tmp {
  1852. fmt.Println("v2332233232322323", v.Quantity)
  1853. switch v.Unit {
  1854. case t.MaxUnit:
  1855. maxunit = maxunit + v.Quantity
  1856. case t.MinUnit:
  1857. minunit = minunit + v.Quantity
  1858. default:
  1859. terr = terr + v.Quantity
  1860. terrUnit = v.Unit
  1861. }
  1862. }
  1863. if minunit >= minmunber {
  1864. if minmunber == 0 {
  1865. err = fmt.Errorf("拆零数量不能为零")
  1866. return
  1867. }
  1868. maxunit = maxunit + minunit/minmunber
  1869. minunit = minunit % minmunber
  1870. }
  1871. if terr > 0 {
  1872. total = total + config.ToString(terr) + terrUnit
  1873. }
  1874. if maxunit > 0 {
  1875. total = total + config.ToString(maxunit) + t.MaxUnit
  1876. }
  1877. if minunit > 0 {
  1878. total = total + config.ToString(minunit) + t.MinUnit
  1879. }
  1880. return
  1881. }
  1882. func GetAllValidDeviceZones02(orgID int64) ([]*models.DeviceZone, error) {
  1883. var zones []*models.DeviceZone
  1884. err := readDb.Model(&models.DeviceZone{}).Where("org_id = ? and status = 1", orgID).Find(&zones).Error
  1885. if err != nil {
  1886. return nil, err
  1887. }
  1888. return zones, nil
  1889. }
  1890. func FindeHisAdviceDocAdvice(orgid int64, patient_id int64, stime int64, etime int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  1891. db := XTReadDB().Model(&advice).Where("status= 1 and is_medicine = 0")
  1892. if orgid > 0 {
  1893. db = db.Where("user_org_id = ?", orgid)
  1894. }
  1895. if patient_id > 0 {
  1896. db = db.Where("patient_id = ?", patient_id)
  1897. }
  1898. if stime > 0 {
  1899. db = db.Where("advice_date >=?", stime)
  1900. }
  1901. if etime > 0 {
  1902. db = db.Where("advice_date<=?", etime)
  1903. }
  1904. err = db.Find(&advice).Error
  1905. return advice, err
  1906. }
  1907. func GetFlow(drug_id int64, system_time int64, org_id int64, patient_id int64) (flow []*models.DrugFlow, err error) {
  1908. err = XTWriteDB().Where("drug_id = ? and system_time =? and user_org_id = ? and status = 1 and patient_id =?", drug_id, system_time, org_id, patient_id).Find(&flow).Error
  1909. return flow, err
  1910. }
  1911. func UpdateAdviceFlow(id int64) error {
  1912. err := XTWriteDB().Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and status = 1 and is_medicine =0", id).Update(map[string]interface{}{"is_medicine": 1}).Error
  1913. return err
  1914. }