pharmacy_service.go 64KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  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. kou := FenDrugInventory(v, orgid)
  569. if !kou {
  570. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  571. return
  572. }
  573. //扣减库存
  574. err = FenStock(orgid, creater, v)
  575. if err != nil {
  576. err = fmt.Errorf("!:%v", err)
  577. return
  578. }
  579. }
  580. }
  581. //修改状态
  582. errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id in (?)", hid).Updates(map[string]interface{}{
  583. "is_medicine": 1,
  584. "people": 0,
  585. "dispensing_time": time_now,
  586. "updated_time": time.Now().Unix(),
  587. }).Error
  588. if errs != nil {
  589. return errs
  590. }
  591. err1 := ChangeHisPrescription(hid)
  592. if err1 != nil {
  593. return err1
  594. }
  595. 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 "+
  596. "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
  597. if err != nil {
  598. return
  599. }
  600. xid := make([]int64, 0)
  601. for _, v := range xids {
  602. xid = append(xid, v.Id)
  603. }
  604. var advice []*models.HisDoctorAdviceInfo
  605. err = tx.Raw("select * from xt_doctor_advice where id in (?) and status = 1", xid).Scan(&advice).Error
  606. if err != nil {
  607. return
  608. }
  609. for _, v := range advice {
  610. tmp_bool := IsPharmacyConfig(orgid)
  611. if tmp_bool {
  612. if PettyCash(v.DrugId) {
  613. continue
  614. }
  615. kou := FenDrugInventory(v, orgid)
  616. if !kou {
  617. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  618. return
  619. }
  620. //扣减库存
  621. err = FenStock(orgid, creater, v)
  622. if err != nil {
  623. err = fmt.Errorf("!:%v", err)
  624. return
  625. }
  626. }
  627. }
  628. //修改状态
  629. errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id in (?)", xid).Updates(map[string]interface{}{
  630. "is_medicine": 1,
  631. "people": 0,
  632. "dispensing_time": time_now,
  633. "updated_time": time.Now().Unix(),
  634. }).Error
  635. if errs1 != nil {
  636. return errs1
  637. }
  638. //生成明细记录
  639. tmp_ph := models.Pharmary{
  640. UserOrgId: orgid,
  641. PatientId: patient_id,
  642. Ctime: time.Now().Unix(),
  643. Mtime: time.Now().Unix(),
  644. Status: 1,
  645. RecordDate: time_now,
  646. }
  647. err = tx.Create(&tmp_ph).Error
  648. return
  649. }
  650. //患者退药按钮点击
  651. func DrugWithdrawal(orgid, patient_id, stime, etime, creater int64) (err error) {
  652. //开事务
  653. tx := XTWriteDB().Begin()
  654. defer func() {
  655. if err != nil {
  656. utils.ErrorLog("事务失败,原因为: %v", err)
  657. tx.Rollback()
  658. } else {
  659. tx.Commit()
  660. }
  661. }()
  662. map_time := make(map[int64]int64)
  663. var hids []*models.TmpID
  664. var xids []*models.TmpID
  665. //var hid,xid []int64//查询已发药的药品
  666. err = tx.Raw("select id,dispensing_time from his_doctor_advice_info where status = 1 and created_time >= ? and created_time <= ? "+
  667. "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
  668. if err != nil {
  669. return
  670. }
  671. hid := make([]int64, 0)
  672. for _, v := range hids {
  673. hid = append(hid, v.Id)
  674. map_time[v.DispensingTime] = v.DispensingTime
  675. }
  676. //修改状态
  677. errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id in (?)", hid).Updates(map[string]interface{}{
  678. "is_medicine": 0,
  679. "people": creater,
  680. "dispensing_time": 0,
  681. "updated_time": time.Now().Unix(),
  682. }).Error
  683. if errs != nil {
  684. return errs
  685. }
  686. err1 := ChangeHisPrescriptionT(hid)
  687. if err1 != nil {
  688. return err1
  689. }
  690. var advice_info []*models.HisDoctorAdviceInfo
  691. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", hid).Find(&advice_info).Error
  692. if err != nil {
  693. return
  694. }
  695. for _, v := range advice_info {
  696. //扣减库存
  697. if !IsPharmacyDelivery(v.DrugId, orgid) {
  698. continue
  699. }
  700. err = DrugAutoAddCancelInfo(v, creater)
  701. if err != nil {
  702. err = fmt.Errorf("!:%v", err)
  703. return
  704. }
  705. drug, _ := FindBaseDrugLibRecordSeven(orgid, v.DrugId)
  706. //查询默认仓库
  707. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  708. //查询默认仓库剩余多少库存
  709. var sum_count int64
  710. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  711. for _, its := range stockInfo {
  712. sum_count += its.StockMaxNumber*drug.MinNumber + its.StockMinNumber
  713. }
  714. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  715. }
  716. 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 "+
  717. "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
  718. if err != nil {
  719. return
  720. }
  721. xid := make([]int64, 0)
  722. for _, v := range xids {
  723. xid = append(xid, v.Id)
  724. map_time[v.DispensingTime] = v.DispensingTime
  725. }
  726. //修改状态
  727. errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id in (?)", xid).Updates(map[string]interface{}{
  728. "is_medicine": 0,
  729. "people": creater,
  730. "dispensing_time": 0,
  731. "updated_time": time.Now().Unix(),
  732. }).Error
  733. if errs1 != nil {
  734. return errs1
  735. }
  736. var advice []*models.HisDoctorAdviceInfo
  737. err = tx.Raw("select * from xt_doctor_advice where id in (?) and status = 1", xid).Scan(&advice).Error
  738. if err != nil {
  739. return
  740. }
  741. for _, v := range advice {
  742. //扣减库存
  743. if !IsPharmacyDelivery(v.DrugId, orgid) {
  744. continue
  745. }
  746. err = DrugAutoAddCancelInfo(v, creater)
  747. if err != nil {
  748. err = fmt.Errorf("!:%v", err)
  749. return
  750. }
  751. drug, _ := FindBaseDrugLibRecordSeven(orgid, v.DrugId)
  752. //查询默认仓库
  753. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  754. //查询默认仓库剩余多少库存
  755. var sum_count int64
  756. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  757. for _, its := range stockInfo {
  758. sum_count += its.StockMaxNumber*drug.MinNumber + its.StockMinNumber
  759. }
  760. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  761. }
  762. //删除掉发药明细
  763. for k, _ := range map_time {
  764. err = tx.Model(&models.Pharmary{}).Where("user_org_id = ? and record_date = ? and patient_id = ? ", orgid, k, patient_id).Updates(map[string]interface{}{
  765. "status": 0,
  766. "mtime": time.Now().Unix(),
  767. }).Error
  768. if err != nil {
  769. return
  770. }
  771. }
  772. return
  773. }
  774. //获取患者名称
  775. func FindUserName(patient_id int64) (name string) {
  776. var tmp models.GetHisName
  777. XTReadDB().Model(&models.GetHisName{}).Where("id = ?", patient_id).Find(&tmp)
  778. name = tmp.Name
  779. return
  780. }
  781. //获取药品名称
  782. func FindDrugsName(drug_id int64) (name string) {
  783. var tmp models.Drug
  784. XTReadDB().Model(&models.Drug{}).Where("id = ?", drug_id).Find(&tmp)
  785. name = tmp.DrugName
  786. return
  787. }
  788. //药品发药按钮点击(
  789. func MedicineDeparture(ids string, creater, orgid int64) (err error) {
  790. //开事务
  791. tx := XTWriteDB().Begin()
  792. defer func() {
  793. if err != nil {
  794. utils.ErrorLog("事务失败,原因为: %v", err)
  795. tx.Rollback()
  796. } else {
  797. tx.Commit()
  798. }
  799. }()
  800. //处理下字符串
  801. t_ids := ""
  802. if ids[len(ids)-1] == 44 {
  803. t_ids = ids[:len(ids)-1]
  804. } else {
  805. t_ids = ids
  806. }
  807. time_now := time.Now().Unix()
  808. tmp_id := strings.Split(t_ids, ",")
  809. var parameter string //测试
  810. patient_id := make(map[int64]int64)
  811. for _, v := range tmp_id {
  812. front := v[:1]
  813. after := v[1:]
  814. if front == "h" {
  815. var advice_info []*models.HisDoctorAdviceInfo
  816. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and status = 1", after).Find(&advice_info).Error
  817. if err != nil {
  818. return
  819. }
  820. for _, v := range advice_info {
  821. if _, ok := patient_id[v.PatientId]; !ok {
  822. patient_id[v.PatientId] = v.PatientId
  823. }
  824. tmp_bool := IsPharmacyConfig(orgid)
  825. if tmp_bool {
  826. if PettyCash(v.DrugId) {
  827. continue
  828. }
  829. kou := FenDrugInventory(v, orgid)
  830. if !kou {
  831. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  832. tmp := models.XtErrs{
  833. OrgId: orgid,
  834. Route: "creater:" + config.ToString(creater),
  835. Parameter: parameter,
  836. Text_err: err.Error(),
  837. }
  838. XTWriteDB().Create(&tmp)
  839. return
  840. } else {
  841. tmps, _ := json.Marshal(advice_info) //
  842. parameter = parameter + string(tmps) //
  843. }
  844. //扣减库存
  845. err = FenStock(orgid, creater, v)
  846. if err != nil {
  847. err = fmt.Errorf("!:%v", err)
  848. return
  849. }
  850. }
  851. }
  852. //修改状态
  853. errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ?", after).Updates(map[string]interface{}{
  854. "is_medicine": 1,
  855. "people": creater,
  856. "dispensing_time": time_now,
  857. "updated_time": time.Now().Unix(),
  858. }).Error
  859. if errs != nil {
  860. return errs
  861. }
  862. err1 := ChangeHisPrescriptionid(after)
  863. if err1 != nil {
  864. return err1
  865. }
  866. } else {
  867. var advice []*models.HisDoctorAdviceInfo
  868. 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
  869. if err != nil {
  870. return
  871. }
  872. for _, v := range advice {
  873. if _, ok := patient_id[v.PatientId]; !ok {
  874. patient_id[v.PatientId] = v.PatientId
  875. }
  876. tmp_bool := IsPharmacyConfig(orgid)
  877. if tmp_bool {
  878. if PettyCash(v.DrugId) {
  879. continue
  880. }
  881. kou := FenDrugInventory(v, orgid)
  882. if !kou {
  883. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  884. tmp := models.XtErrs{
  885. OrgId: orgid,
  886. Route: "creater:" + config.ToString(creater),
  887. Parameter: parameter,
  888. Text_err: err.Error(),
  889. }
  890. XTWriteDB().Create(&tmp)
  891. return
  892. } else {
  893. tmps, _ := json.Marshal(advice) //
  894. parameter = parameter + string(tmps) //
  895. }
  896. //扣减库存
  897. err = FenStock(orgid, creater, v)
  898. if err != nil {
  899. err = fmt.Errorf("!:%v", err)
  900. return
  901. }
  902. }
  903. }
  904. //修改状态
  905. errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id = ?", after).Updates(map[string]interface{}{
  906. "is_medicine": 1,
  907. "people": creater,
  908. "dispensing_time": time_now,
  909. "updated_time": time.Now().Unix(),
  910. }).Error
  911. if errs1 != nil {
  912. return errs1
  913. }
  914. }
  915. }
  916. for _, v := range patient_id {
  917. //生成明细记录
  918. tmp_ph := models.Pharmary{
  919. UserOrgId: orgid,
  920. PatientId: v,
  921. Ctime: time.Now().Unix(),
  922. Mtime: time.Now().Unix(),
  923. Status: 1,
  924. RecordDate: time_now,
  925. }
  926. err = tx.Create(&tmp_ph).Error
  927. if err != nil {
  928. return err
  929. }
  930. }
  931. return
  932. }
  933. //获取领药人姓名(
  934. func GetUserAdminName(id, orgID int64) string {
  935. var tmp models.XTSgjUserAdminRole
  936. XTReadDB().Model(&models.XTSgjUserAdminRole{}).Where("admin_user_id = ? and org_id = ?", id, orgID).Find(&tmp)
  937. return tmp.UserName
  938. }
  939. //根据id查询是否已发药,true已发药,false未发药gai
  940. func GiveTheMedicine(id int64) bool {
  941. var xue models.PharmacyDoctorAdvice
  942. XTReadDB().Model(&models.PharmacyDoctorAdvice{}).Where("id = ?", id).Find(&xue)
  943. if xue.IsMedicine == 1 {
  944. return true
  945. }
  946. return false
  947. }
  948. //查询改组中的药品是否包含已发药的,true已发药,false未发药
  949. func GiveGroupMedicine(orgid, groupNo int64) bool {
  950. var total int
  951. XTReadDB().Model(&models.DoctorAdvice{}).Where("user_org_id = ? and groupno = ? and status = 1 and is_medicine = 1", orgid, groupNo).Count(&total)
  952. if total > 0 {
  953. return true
  954. }
  955. return false
  956. }
  957. //查询处方中是否包含已发药的
  958. func GiveChuMedicine(id int64) bool {
  959. var total int
  960. XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", id).Count(&total)
  961. if total > 0 {
  962. return true
  963. }
  964. return false
  965. }
  966. //根据处方id查找该处方中是否存在已发药的药品
  967. func IsChuIssuedDrugs(prescription_id int64) bool {
  968. var total int
  969. XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", prescription_id).Count(&total)
  970. if total > 0 {
  971. return true
  972. }
  973. return false
  974. }
  975. //判断该药品是否通过药房管理出库,true是,false否
  976. //id 药品id,org_id 机构id
  977. func IsPharmacyDelivery(id, org_id int64) bool {
  978. //判断药品是否通过药房管理
  979. var total01 int
  980. XTReadDB().Model(&models.PharmacyBaseDrug{}).Where("id = ? and is_pharmacy = 1", id).Count(&total01)
  981. //判断该机构是否通过药房管理出库
  982. var total02 int
  983. XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and is_open = 1", org_id).Count(&total02)
  984. if total01 > 0 && total02 > 0 {
  985. return true
  986. }
  987. return false
  988. }
  989. //判断机构是否通过药房管理出库
  990. func IsPharmacyConfig(orgid int64) (bo bool) {
  991. var total int
  992. XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and is_open = 1 and status = 1", orgid).Count(&total)
  993. if total > 0 {
  994. return true
  995. }
  996. return false
  997. }
  998. //根据患者id和发药时间,获取医生的id和姓名
  999. func GetDoctorIds(id, recordtime, orgid int64) (doctor_id int64, doctor_name string, err error) {
  1000. var tmp []*models.TmpAdviceDoctor
  1001. //var tmp []int64
  1002. err = XTReadDB().Raw("select distinct advice_doctor from his_doctor_advice_info where "+
  1003. "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) "+
  1004. "union "+
  1005. "select distinct advice_doctor from xt_doctor_advice where "+
  1006. "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) ",
  1007. recordtime, orgid, id, orgid, recordtime, orgid, id, orgid).Scan(&tmp).Error
  1008. if err != nil || len(tmp) == 0 {
  1009. return
  1010. }
  1011. doctor_id = tmp[0].AdviceDoctor
  1012. doctor_name = GetUserAdminName(doctor_id, orgid)
  1013. return
  1014. }
  1015. func MapToJson(param map[string]interface{}) string {
  1016. dataType, _ := json.Marshal(param)
  1017. dataString := string(dataType)
  1018. return dataString
  1019. }
  1020. //封装扣减库存
  1021. func FenStock(orgid, creater int64, v *models.HisDoctorAdviceInfo) (err error) {
  1022. err = HisDrugsDelivery(orgid, creater, v)
  1023. if err != nil {
  1024. err = fmt.Errorf("!:%v", err)
  1025. return
  1026. }
  1027. drug, _ := FindBaseDrugLibRecordSeven(orgid, v.DrugId)
  1028. //查询默认仓库
  1029. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  1030. //查询默认仓库剩余多少库存
  1031. var sum_count int64
  1032. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  1033. for _, its := range stockInfo {
  1034. sum_count += its.StockMaxNumber*drug.MinNumber + its.StockMinNumber
  1035. }
  1036. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  1037. return
  1038. }
  1039. //封装查询药品库存是否足够
  1040. func FenDrugInventory(item *models.HisDoctorAdviceInfo, orgid int64) bool {
  1041. var total int64
  1042. var prescribing_number_total int64
  1043. houseConfig, _ := GetAllStoreHouseConfig(orgid)
  1044. ////查询该药品是否有库存
  1045. list, _ := GetDrugTotalCountTwenty(item.DrugId, item.UserOrgId, houseConfig.DrugStorehouseOut)
  1046. ////查询改药品信息
  1047. medical, _ := GetBaseDrugMedical(item.DrugId)
  1048. ////判断单位是否相等
  1049. if medical.MaxUnit == item.PrescribingNumberUnit {
  1050. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1051. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1052. //转化为最小单位
  1053. total = list.Count*medical.MinNumber + list.StockMinNumber
  1054. prescribing_number_total = count * medical.MinNumber
  1055. }
  1056. if medical.MinUnit == item.PrescribingNumberUnit {
  1057. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1058. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1059. total = list.Count*medical.MinNumber + list.StockMinNumber
  1060. prescribing_number_total = count
  1061. }
  1062. if prescribing_number_total <= total {
  1063. //可以扣减
  1064. return true
  1065. } else {
  1066. //不可扣减
  1067. return false
  1068. }
  1069. }
  1070. func GetAdviceIds1(ids []string, orgid, execution_staff int64, thetime time.Time) (tmp []string, err error) {
  1071. var advice []*models.DoctorAdvice
  1072. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND status = 1", ids).Find(&advice).Error
  1073. if err != nil {
  1074. return
  1075. }
  1076. for _, v := range advice {
  1077. if !IsPharmacyDelivery(v.DrugId, orgid) {
  1078. tmp = append(tmp, config.ToString(v.ID))
  1079. } else {
  1080. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? ", v.ID).Updates(map[string]interface{}{
  1081. "execution_staff": execution_staff,
  1082. "execution_time": thetime.Unix(),
  1083. "updated_time": time.Now().Unix(),
  1084. }).Error
  1085. if err != nil {
  1086. return
  1087. }
  1088. }
  1089. }
  1090. return
  1091. }
  1092. func GetAdviceIds2(ids []string, orgid, execution_staff int64, thetime time.Time) (tmp []string, err error) {
  1093. var advice []*models.HisDoctorAdviceInfoL
  1094. err = readDb.Model(&models.HisDoctorAdviceInfoL{}).Where("id IN (?) AND status = 1", ids).Find(&advice).Error
  1095. if err != nil {
  1096. return
  1097. }
  1098. for _, v := range advice {
  1099. if !IsPharmacyDelivery(v.DrugId, orgid) {
  1100. tmp = append(tmp, config.ToString(v.ID))
  1101. } else {
  1102. err = XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? ", v.ID).Updates(map[string]interface{}{
  1103. "execution_staff": execution_staff,
  1104. "execution_time": thetime.Unix(),
  1105. "updated_time": time.Now().Unix(),
  1106. }).Error
  1107. if err != nil {
  1108. return
  1109. }
  1110. }
  1111. }
  1112. return
  1113. }
  1114. func GetAdviceId1(id, orgid, execution_staff int64, thetime time.Time) (bo bool, err error) {
  1115. var advice models.DoctorAdvice
  1116. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND status = 1", id).Find(&advice).Error
  1117. if err != nil {
  1118. return false, err
  1119. }
  1120. if IsPharmacyDelivery(advice.DrugId, orgid) {
  1121. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? ", 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 false, err
  1128. }
  1129. return true, err
  1130. }
  1131. return false, err
  1132. }
  1133. func GetAdviceId2(id, orgid, execution_staff int64, thetime time.Time) (bo bool, err error) {
  1134. var advice models.HisDoctorAdviceInfoL
  1135. err = readDb.Model(&models.HisDoctorAdviceInfoL{}).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.HisDoctorAdviceInfoL{}).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. //判断药品是否零用
  1153. func PettyCash(id int64) bool {
  1154. drug := models.XtBaseDrug{}
  1155. XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug)
  1156. if drug.IsUse == 1 {
  1157. return true
  1158. }
  1159. return false
  1160. }
  1161. //初始化
  1162. func InitDrugidIsNil(orgid, stime, etime int64) {
  1163. var advice []*models.DoctorAdvice
  1164. 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)
  1165. for _, v := range advice {
  1166. 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)
  1167. }
  1168. }
  1169. //改变处方状态(发药
  1170. func ChangeHisPrescription(tmp []int64) (err error) {
  1171. var advice_info []*models.HisDoctorAdviceInfo
  1172. err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", tmp).Find(&advice_info).Error
  1173. if err != nil {
  1174. if err == gorm.ErrRecordNotFound {
  1175. return nil
  1176. }
  1177. return
  1178. }
  1179. tmp_id := make(map[int64]int64)
  1180. for _, v := range advice_info {
  1181. tmp_id[v.PrescriptionId] = v.PrescriptionId
  1182. }
  1183. for k, _ := range tmp_id {
  1184. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", k).Updates(map[string]interface{}{
  1185. "is_medicine": 1,
  1186. "mtime": time.Now().Unix(),
  1187. }).Error
  1188. if err != nil {
  1189. return
  1190. }
  1191. }
  1192. return
  1193. }
  1194. //改变处方状态(退药
  1195. func ChangeHisPrescriptionT(tmp []int64) (err error) {
  1196. var advice_info []*models.HisDoctorAdviceInfo
  1197. err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", tmp).Find(&advice_info).Error
  1198. if err != nil {
  1199. if err == gorm.ErrRecordNotFound {
  1200. return nil
  1201. }
  1202. return
  1203. }
  1204. tmp_id := make(map[int64]int64)
  1205. for _, v := range advice_info {
  1206. tmp_id[v.PrescriptionId] = v.PrescriptionId
  1207. }
  1208. for k, _ := range tmp_id {
  1209. var total int
  1210. //查询退药的
  1211. XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", k).Count(&total)
  1212. if total == 0 {
  1213. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", k).Updates(map[string]interface{}{
  1214. "is_medicine": 0,
  1215. "mtime": time.Now().Unix(),
  1216. }).Error
  1217. if err != nil {
  1218. return
  1219. }
  1220. }
  1221. }
  1222. return
  1223. }
  1224. //改变处方状态
  1225. func ChangeHisPrescriptionid(id string) (err error) {
  1226. var advice models.HisDoctorAdviceInfo
  1227. err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and status = 1", id).Find(&advice).Error
  1228. if err != nil {
  1229. if err == gorm.ErrRecordNotFound {
  1230. return nil
  1231. }
  1232. return
  1233. }
  1234. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", advice.PrescriptionId).Updates(map[string]interface{}{
  1235. "is_medicine": 1,
  1236. "mtime": time.Now().Unix(),
  1237. }).Error
  1238. if err != nil {
  1239. return
  1240. }
  1241. return
  1242. }
  1243. //获取药品规格
  1244. func ReplacementDrugs(orgid int64, special bool) (list []*models.ReplacementDrugs, err error) {
  1245. var g errgroup.Group
  1246. var lib []*models.BaseDrugLib
  1247. var lock sync.Mutex
  1248. if special {
  1249. 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
  1250. } else {
  1251. err = readDb.Model(&models.BaseDrugLib{}).Where("org_id = ? and drug_status not like '%停用%' AND status = 1 ", orgid).Find(&lib).Error
  1252. }
  1253. if err != nil {
  1254. return
  1255. }
  1256. for _, v := range lib {
  1257. v := v
  1258. g.Go(func() error {
  1259. var tmp models.Manufacturer
  1260. err := readDb.Model(&models.Manufacturer{}).Where("id = ?", v.Manufacturer).Find(&tmp).Error
  1261. if err != nil {
  1262. return err
  1263. }
  1264. lock.Lock()
  1265. list = append(list, &models.ReplacementDrugs{
  1266. Id: v.ID,
  1267. Name: v.DrugName + " " + v.Dose + v.DoseUnit + "*" + config.ToString(v.MinNumber) + v.MinUnit + "/" + v.MaxUnit + " " + tmp.ManufacturerName,
  1268. })
  1269. lock.Unlock()
  1270. return err
  1271. })
  1272. }
  1273. if errs := g.Wait(); errs != nil {
  1274. err = errs
  1275. return
  1276. }
  1277. return
  1278. }
  1279. //获取药品规格,不考虑特殊病
  1280. func ReplacementDrugsT(orgid int64) (list []*models.ReplacementDrugs, err error) {
  1281. var lib []*models.BaseDrugLib
  1282. err = readDb.Model(&models.BaseDrugLib{}).Where("org_id = ? AND status = 1 and drug_status not like '%停用%' ", orgid).Find(&lib).Error
  1283. if err != nil {
  1284. return
  1285. }
  1286. for _, v := range lib {
  1287. var tmp models.Manufacturer
  1288. err = readDb.Model(&models.Manufacturer{}).Where("id = ?", v.Manufacturer).Find(&tmp).Error
  1289. if err != nil {
  1290. return
  1291. }
  1292. list = append(list, &models.ReplacementDrugs{
  1293. Id: v.ID,
  1294. Name: v.DrugName + " " + v.Dose + v.DoseUnit + "*" + config.ToString(v.MinNumber) + v.MinUnit + "/" + v.PrescribingNumberUnit + " " + tmp.ManufacturerName,
  1295. })
  1296. }
  1297. return
  1298. }
  1299. //根据药品id获取药品名字
  1300. func IdToDrugName(id int64) (name string) {
  1301. var lib models.BaseDrugLib
  1302. readDb.Model(&models.BaseDrugLib{}).Where("id = ?", id).Find(&lib)
  1303. return lib.DrugName
  1304. }
  1305. //获取项目和耗材和套餐
  1306. func ProjectConsumables3(orgid int64) (list []*models.DropDownList, err error) {
  1307. var project []*models.XtHisProject
  1308. project, err = GetHisProject(orgid)
  1309. if err != nil {
  1310. return
  1311. }
  1312. for _, v := range project {
  1313. list = append(list, &models.DropDownList{
  1314. Id: "p" + config.ToString(v.ID),
  1315. Name: v.ProjectName,
  1316. })
  1317. }
  1318. var good []*models.GoodInfo
  1319. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? and status = 1", orgid).Find(&good).Error
  1320. if err != nil {
  1321. return
  1322. }
  1323. for _, v := range good {
  1324. list = append(list, &models.DropDownList{
  1325. Id: "i" + config.ToString(v.ID),
  1326. Name: v.GoodName,
  1327. })
  1328. }
  1329. var team []*models.XtHisProjectTeam
  1330. team, err = GetAllProjectTeam(orgid)
  1331. if err != nil {
  1332. return
  1333. }
  1334. for _, v := range team {
  1335. list = append(list, &models.DropDownList{
  1336. Id: "h" + config.ToString(v.ID),
  1337. Name: v.ProjectTeam,
  1338. })
  1339. }
  1340. return
  1341. }
  1342. //获取项目和耗材
  1343. func ProjectConsumables2(orgid int64, special bool) (list []*models.DropDownList, err error) {
  1344. var project []*models.XtHisProject
  1345. if special {
  1346. err = XTReadDB().Model(&project).Where("user_org_id = ? and medical_status != 1 and status = 1 and disease_directory = 1", orgid).Find(&project).Error
  1347. } else {
  1348. err = XTReadDB().Model(&project).Where("user_org_id = ? and medical_status != 1 and status = 1", orgid).Find(&project).Error
  1349. }
  1350. if err != nil {
  1351. return
  1352. }
  1353. for _, v := range project {
  1354. list = append(list, &models.DropDownList{
  1355. Id: "p" + config.ToString(v.ID),
  1356. Name: v.ProjectName,
  1357. })
  1358. }
  1359. var good []*models.GoodInfo
  1360. if special {
  1361. 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
  1362. } else {
  1363. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? and good_status not like '%停用%' and status = 1", orgid).Find(&good).Error
  1364. }
  1365. if err != nil {
  1366. return
  1367. }
  1368. for _, v := range good {
  1369. list = append(list, &models.DropDownList{
  1370. Id: "i" + config.ToString(v.ID),
  1371. Name: v.GoodName + " " + v.SpecificationName,
  1372. })
  1373. }
  1374. return
  1375. }
  1376. //获取项目和耗材不考虑特殊病
  1377. func ProjectConsumables2T(orgid int64) (list []*models.DropDownList, err error) {
  1378. var project []*models.XtHisProject
  1379. err = XTReadDB().Model(&project).Where("user_org_id = ? and status = 1 and medical_status != 1", orgid).Find(&project).Error
  1380. if err != nil {
  1381. return
  1382. }
  1383. for _, v := range project {
  1384. list = append(list, &models.DropDownList{
  1385. Id: "p" + config.ToString(v.ID),
  1386. Name: v.ProjectName,
  1387. })
  1388. }
  1389. var good []*models.GoodInfo
  1390. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? and status = 1 and good_status != 1", orgid).Find(&good).Error
  1391. if err != nil {
  1392. return
  1393. }
  1394. for _, v := range good {
  1395. list = append(list, &models.DropDownList{
  1396. Id: "i" + config.ToString(v.ID),
  1397. Name: v.GoodName + " " + v.SpecificationName,
  1398. })
  1399. }
  1400. return
  1401. }
  1402. //查询药品
  1403. func FindPatientDrug(orgid, patient_id, drugid, mode int64) bool {
  1404. var total int
  1405. XTReadDB().Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and status = 1 and "+
  1406. "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 ("+
  1407. "select id from his_prescription_template where status= 1 and user_org_id = ? and mode = ?))", orgid, patient_id, drugid, orgid, orgid, mode).Count(&total)
  1408. if total > 0 {
  1409. return true
  1410. }
  1411. return false
  1412. }
  1413. //查询项目
  1414. func FindPatientXiang(orgid, patient_id, drugid, mode int64) bool {
  1415. var total int
  1416. XTReadDB().Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and status = 1 and type = 2 and "+
  1417. "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 ("+
  1418. "select id from his_prescription_template where status= 1 and user_org_id = ? and mode = ?))", orgid, patient_id, drugid, orgid, orgid, mode).Count(&total)
  1419. if total > 0 {
  1420. return true
  1421. }
  1422. return false
  1423. }
  1424. //查询耗材
  1425. func FindPatientXiang2(orgid, patient_id, drugid, mode int64) bool {
  1426. var total int
  1427. XTReadDB().Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and status = 1 and type = 3 and "+
  1428. "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 ("+
  1429. "select id from his_prescription_template where status= 1 and user_org_id = ? and mode = ?))", orgid, patient_id, drugid, orgid, orgid, mode).Count(&total)
  1430. if total > 0 {
  1431. return true
  1432. }
  1433. return false
  1434. }
  1435. //types处方类型,1.药品 2.项目,model透析模式
  1436. func GetHisInfoTempalteId(model, orgid, types int64) (tp []int64) {
  1437. type tmpid struct {
  1438. Id int64
  1439. }
  1440. var tmp []*tmpid
  1441. XTReadDB().Raw("select id from his_prescription_info_template where status = 1 and user_org_id = ? and type = ? and p_template_id in ("+
  1442. "select id from his_prescription_template where status = 1 and user_org_id = ? and mode = ?)", orgid, types, orgid, model).Scan(&tmp)
  1443. for _, v := range tmp {
  1444. tp = append(tp, v.Id)
  1445. }
  1446. return
  1447. }
  1448. //替换药品处方模板
  1449. func ReplaceDrugPrescriptionTemplate(orgid, patient_id, drugid int64, ids []int64, s models.HisPrescriptionAdviceTemplate, tx *gorm.DB) (err error) {
  1450. err = tx.Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and "+
  1451. "status = 1 and prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
  1452. "drug_id": s.DrugId,
  1453. "advice_name": s.AdviceName,
  1454. "single_dose": s.SingleDose,
  1455. "single_dose_unit": s.SingleDoseUnit,
  1456. "delivery_way": s.DeliveryWay,
  1457. "execution_frequency": s.ExecutionFrequency,
  1458. "day": s.Day,
  1459. "prescribing_number": s.PrescribingNumber,
  1460. "prescribing_number_unit": s.PrescribingNumberUnit,
  1461. "price": s.Price,
  1462. "remark": s.Remark,
  1463. "updated_time": time.Now().Unix(),
  1464. }).Error
  1465. return err
  1466. }
  1467. //替换项目模板
  1468. func ReplaceProjectPrescriptionTemplate(orgid, patient_id, drugid int64, ids []int64, s models.HisPrescriptionProjectTemplate, tx *gorm.DB) (err error) {
  1469. err = tx.Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and "+
  1470. "status = 1 and prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
  1471. "project_id": s.ProjectId,
  1472. "type": s.Type,
  1473. "frequency_type": s.FrequencyType,
  1474. "day_count": s.DayCount,
  1475. "week_day": s.WeekDay,
  1476. "price": s.Price,
  1477. "count": s.Count,
  1478. "single_dose": s.SingleDose,
  1479. "delivery_way": s.DeliveryWay,
  1480. "execution_frequency": s.ExecutionFrequency,
  1481. "remark": s.Remark,
  1482. "day": s.Day,
  1483. "unit": s.Unit,
  1484. "mtime": time.Now().Unix(),
  1485. }).Error
  1486. return err
  1487. }
  1488. //删除项目模板
  1489. func DeleteProjectTemplate(orgid, patient_id, drugid int64, ids []int64, tx *gorm.DB) (err error) {
  1490. err = tx.Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and "+
  1491. "prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
  1492. "status": 0,
  1493. "mtime": time.Now().Unix(),
  1494. }).Error
  1495. return err
  1496. }
  1497. //删除处方模板
  1498. func DeletePrescriptionTemplate(orgid, patient_id, drugid int64, ids []int64, tx *gorm.DB) (err error) {
  1499. err = tx.Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and "+
  1500. "prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
  1501. "status": 0,
  1502. "updated_time": time.Now().Unix(),
  1503. }).Error
  1504. return err
  1505. }
  1506. //根据透析模式和患者id获取该患者的处方内容
  1507. func PTemplateInformation(orgid, mode_id, patient_id int64) (tmp interface{}, err error) {
  1508. //获取处方id
  1509. var tt []*models.HisPrescriptionTemplate
  1510. 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
  1511. if len(tt) > 1 {
  1512. err = fmt.Errorf("数据有误")
  1513. return
  1514. }
  1515. x := make(map[int64]string, 0) //项目
  1516. h := make(map[int64]string, 0) //耗材
  1517. for _, v := range tt {
  1518. var prescriptions []*models.HisPrescriptionInfoTemplateL
  1519. prescriptions, err = GetHisPrescriptionTemplateL(v.ID, orgid)
  1520. if err != nil {
  1521. return
  1522. }
  1523. for i := 0; i < len(prescriptions); i++ {
  1524. for j := 0; j < len(prescriptions[i].HisPrescriptionProjectTemplate); j++ {
  1525. if prescriptions[i].HisPrescriptionProjectTemplate[j].Type == 2 {
  1526. //项目
  1527. if v, ok := x[prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.StatisticalClassification]; ok {
  1528. prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.Translate = v
  1529. } else {
  1530. prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.Translate, err = TranslateZu(prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.StatisticalClassification, orgid, "统计分类")
  1531. if err != nil {
  1532. return
  1533. }
  1534. x[prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.StatisticalClassification] = prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.Translate
  1535. }
  1536. }
  1537. if prescriptions[i].HisPrescriptionProjectTemplate[j].Type == 3 {
  1538. //耗材
  1539. if v, ok := h[prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.GoodKind]; ok {
  1540. prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.Translate = v
  1541. } else {
  1542. prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.Translate = "耗材"
  1543. h[prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.GoodKind] = prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.Translate
  1544. }
  1545. }
  1546. }
  1547. }
  1548. tmp = prescriptions
  1549. }
  1550. return
  1551. }
  1552. func DeleteOne(types, id int64) (err error) {
  1553. if types == 1 {
  1554. //药品
  1555. err = XTWriteDB().Model(&models.HisPrescriptionAdviceTemplate{}).Where("id = ?", id).Updates(map[string]interface{}{
  1556. "status": 0,
  1557. "updated_time": time.Now().Unix(),
  1558. }).Error
  1559. if err != nil {
  1560. return
  1561. }
  1562. } else if types == 2 {
  1563. //项目
  1564. err = XTWriteDB().Model(&models.HisPrescriptionProjectTemplateL{}).Where("id = ?", id).Updates(map[string]interface{}{
  1565. "status": 0,
  1566. "mtime": time.Now().Unix(),
  1567. }).Error
  1568. if err != nil {
  1569. return
  1570. }
  1571. } else {
  1572. err = fmt.Errorf("参数错误")
  1573. return
  1574. }
  1575. return
  1576. }
  1577. //收尾工作
  1578. func Scavenger(orgid int64) (err error) {
  1579. //开事务
  1580. tx := XTWriteDB().Begin()
  1581. defer func() {
  1582. if err != nil {
  1583. utils.ErrorLog("事务失败,原因为: %v", err)
  1584. tx.Rollback()
  1585. } else {
  1586. tx.Commit()
  1587. }
  1588. }()
  1589. //查询该机构的所有模板
  1590. var tmp1 []*models.HisPrescriptionTemplate
  1591. var tmp2 []*models.HisPrescriptionInfoTemplate
  1592. deltmp1 := make([]int64, 0) //清除的数据
  1593. deltmp2 := make([]int64, 0) //清除的数据
  1594. err = tx.Model(&models.HisPrescriptionTemplate{}).Where("user_org_id = ? and status = 1", orgid).Find(&tmp1).Error
  1595. if err != nil {
  1596. return
  1597. }
  1598. for _, v := range tmp1 {
  1599. var total1 int
  1600. tx.Model(&models.HisPrescriptionInfoTemplate{}).Where("user_org_id = ? and p_template_id = ? and status = 1", orgid, v.ID).Count(&total1)
  1601. if total1 == 0 {
  1602. deltmp1 = append(deltmp1, v.ID)
  1603. }
  1604. }
  1605. err = tx.Model(&models.HisPrescriptionInfoTemplate{}).Where("user_org_id = ? and status = 1", orgid).Find(&tmp2).Error
  1606. if err != nil {
  1607. return
  1608. }
  1609. for _, v := range tmp2 {
  1610. if v.Type == 1 {
  1611. var total2 int
  1612. tx.Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and prescription_id = ? and status = 1", orgid, v.ID).Count(&total2)
  1613. if total2 == 0 {
  1614. deltmp2 = append(deltmp2, v.ID)
  1615. }
  1616. }
  1617. if v.Type == 2 {
  1618. var total3 int
  1619. tx.Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and prescription_id = ? and status = 1", orgid, v.ID).Count(&total3)
  1620. if total3 == 0 {
  1621. deltmp2 = append(deltmp2, v.ID)
  1622. }
  1623. }
  1624. }
  1625. //清除1
  1626. err = tx.Model(&models.HisPrescriptionTemplate{}).Where("id in (?)", deltmp1).Updates(map[string]interface{}{
  1627. "status": 0,
  1628. "mtime": time.Now().Unix(),
  1629. }).Error
  1630. if err != nil {
  1631. return
  1632. }
  1633. //清除1
  1634. err = tx.Model(&models.HisPrescriptionInfoTemplate{}).Where("id in (?)", deltmp2).Updates(map[string]interface{}{
  1635. "status": 0,
  1636. "mtime": time.Now().Unix(),
  1637. }).Error
  1638. if err != nil {
  1639. return
  1640. }
  1641. return
  1642. }
  1643. //分区和收费(患者发药
  1644. func PartitionAndLayout(stime, etime, orgid, shift, partition int64, flist []*models.TmpPatient) (tmp []*models.TmpPatient, err error) {
  1645. //获取排班
  1646. var sch []*models.XtScheduleTwo
  1647. ma := make(map[int64]int64)
  1648. s := "user_org_id = ? and status = 1 and schedule_date >= ? and schedule_date <= ?"
  1649. if shift != 0 {
  1650. s = s + " and schedule_type = " + config.ToString(shift)
  1651. }
  1652. if partition != 0 {
  1653. s = s + " and partition_id = " + config.ToString(partition)
  1654. }
  1655. if shift == 0 && partition == 0 {
  1656. return flist, nil
  1657. }
  1658. err = XTWriteDB().Model(&models.XtScheduleTwo{}).Where(s, orgid, stime, etime).Find(&sch).Error
  1659. if err != nil {
  1660. return
  1661. }
  1662. for _, v := range sch {
  1663. ma[v.PatientId] = v.PatientId
  1664. }
  1665. for _, v := range flist {
  1666. if _, ok := ma[v.PatientID]; ok {
  1667. tmp = append(tmp, v)
  1668. }
  1669. }
  1670. return
  1671. }
  1672. //分区和收费(药品发药
  1673. func PartitionAndLayoutDrug(deliveryway string, stime, etime, orgid, shift, partition int64, flist []*models.PatientInformation) (tmp []*models.PatientInformation, err error) {
  1674. //获取排班
  1675. var sch []*models.XtScheduleTwo
  1676. ma := make(map[int64]int64)
  1677. s := "user_org_id = ? and status = 1 and schedule_date >= ? and schedule_date <= ?"
  1678. if shift != 0 {
  1679. s = s + " and schedule_type = " + config.ToString(shift)
  1680. }
  1681. if partition != 0 {
  1682. s = s + " and partition_id = " + config.ToString(partition)
  1683. }
  1684. if shift == 0 && partition == 0 {
  1685. for _, v := range flist {
  1686. ma[v.PatientId] = v.PatientId
  1687. }
  1688. for _, v := range flist {
  1689. if _, ok := ma[v.PatientId]; ok && (v.Usage == deliveryway || deliveryway == "全部") {
  1690. tmp = append(tmp, v)
  1691. }
  1692. }
  1693. return tmp, nil
  1694. }
  1695. err = XTWriteDB().Model(&models.XtScheduleTwo{}).Where(s, orgid, stime, etime).Find(&sch).Error
  1696. if err != nil {
  1697. return
  1698. }
  1699. for _, v := range sch {
  1700. ma[v.PatientId] = v.PatientId
  1701. }
  1702. for _, v := range flist {
  1703. if _, ok := ma[v.PatientId]; ok && (v.Usage == deliveryway || deliveryway == "全部") {
  1704. tmp = append(tmp, v)
  1705. }
  1706. }
  1707. return
  1708. }
  1709. //给药途径
  1710. func Administration(deliveryway string, orgid int64, flist []*models.ListOfDrugs) (tmp []*models.ListOfDrugs, err error) {
  1711. //查询药品
  1712. var pp []*models.PharmacyBaseDrug
  1713. s := "org_id = ? and status = 1"
  1714. if deliveryway == "全部" {
  1715. tmp = flist
  1716. return
  1717. } else {
  1718. s = s + " and delivery_way = '" + deliveryway + "'"
  1719. }
  1720. err = XTReadDB().Model(&models.PharmacyBaseDrug{}).Where(s, orgid).Find(&pp).Error
  1721. if err != nil {
  1722. return
  1723. }
  1724. ma := make(map[int64]int64)
  1725. for _, v := range pp {
  1726. ma[v.ID] = v.ID
  1727. }
  1728. for _, v := range flist {
  1729. if _, ok := ma[v.ID]; ok {
  1730. tmp = append(tmp, v)
  1731. }
  1732. }
  1733. return
  1734. }
  1735. //计算总量
  1736. func CalculateTheTotalAmount(tmp []*models.PatientInformation, drug_id int64) (total string, err error) {
  1737. //获取药品信息
  1738. var t models.SpBaseDrug
  1739. err = XTReadDB().Model(&models.SpBaseDrug{}).Where("id = ?", drug_id).Find(&t).Error
  1740. if err != nil {
  1741. return
  1742. }
  1743. terr, maxunit, minunit := int64(0), int64(0), int64(0) //terr异常的单位数量,maxunit包装单位数量,minunit拆零单位数量
  1744. minmunber := t.MinNumber //拆零数量
  1745. terrUnit := ""
  1746. for _, v := range tmp {
  1747. switch v.Unit {
  1748. case t.MaxUnit:
  1749. maxunit = maxunit + v.Quantity
  1750. case t.MinUnit:
  1751. minunit = minunit + v.Quantity
  1752. default:
  1753. terr = terr + v.Quantity
  1754. terrUnit = v.Unit
  1755. }
  1756. }
  1757. if minunit >= minmunber {
  1758. if minmunber == 0 {
  1759. err = fmt.Errorf("拆零数量不能为零")
  1760. return
  1761. }
  1762. maxunit = maxunit + minunit/minmunber
  1763. minunit = minunit % minmunber
  1764. }
  1765. if terr > 0 {
  1766. total = total + config.ToString(terr) + terrUnit
  1767. }
  1768. if maxunit > 0 {
  1769. total = total + config.ToString(maxunit) + t.MaxUnit
  1770. }
  1771. if minunit > 0 {
  1772. total = total + config.ToString(minunit) + t.MinUnit
  1773. }
  1774. return
  1775. }
  1776. func GetAllValidDeviceZones02(orgID int64) ([]*models.DeviceZone, error) {
  1777. var zones []*models.DeviceZone
  1778. err := readDb.Model(&models.DeviceZone{}).Where("org_id = ? and status = 1", orgID).Find(&zones).Error
  1779. if err != nil {
  1780. return nil, err
  1781. }
  1782. return zones, nil
  1783. }