pharmacy_service.go 83KB

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