pharmacy_service.go 78KB

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