pharmacy_service.go 65KB

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