pharmacy_service.go 77KB

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