pharmacy_service.go 80KB

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