pharmacy_service.go 78KB

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