pharmacy_service.go 78KB

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