pharmacy_service.go 68KB

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