pharmacy_service.go 104KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047
  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 GetPatientKeyWord(keywword string, org_id int64) (patient []*models.XtPatients, err error) {
  90. db := XTReadDB().Model(&patient).Where("status=1")
  91. likeKey := "%" + keywword + "%"
  92. if len(keywword) > 0 {
  93. db = db.Where("name like ? or first_letter like ?", likeKey, likeKey)
  94. }
  95. if org_id > 0 {
  96. db = db.Where("user_org_id = ?", org_id)
  97. }
  98. err = db.Find(&patient).Error
  99. return patient, err
  100. }
  101. func GetSchedulePatientId(start_time int64, end_time int64, org_id int64, class_type int64, partion_type int64, patientsId []int64) (schedule []*models.XtSchedules, err error) {
  102. db := XTReadDB().Model(&schedule).Where("status = 1")
  103. if start_time > 0 {
  104. db = db.Where("schedule_date >=?", start_time)
  105. }
  106. if end_time > 0 {
  107. db = db.Where("schedule_date <=?", end_time)
  108. }
  109. if org_id > 0 {
  110. db = db.Where("user_org_id = ?", org_id)
  111. }
  112. if class_type > 0 {
  113. db = db.Where("schedule_type =?", class_type)
  114. }
  115. if partion_type > 0 {
  116. db = db.Where("partition_id = ?", partion_type)
  117. }
  118. if len(patientsId) == 0 {
  119. db = db.Where("patient_id in(?)", patientsId)
  120. }
  121. if len(patientsId) > 0 {
  122. db = db.Where("patient_id in(?)", patientsId)
  123. }
  124. err = db.Find(&schedule).Error
  125. return schedule, err
  126. }
  127. func GetTodayAdviceCountOne(stime int64, etime int64, orgid int64, is_medicine int64, ids []int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  128. db := XTReadDB().Model(&advice).Where("status=1 and is_medicine = ?", is_medicine)
  129. if stime > 0 {
  130. db = db.Where("advice_date>=?", stime)
  131. }
  132. if etime > 0 {
  133. db = db.Where("advice_date<=?", stime)
  134. }
  135. if orgid > 0 {
  136. db = db.Where("user_org_id = ?", orgid)
  137. }
  138. if len(ids) == 0 {
  139. db = db.Where("patient_id not in(?)", ids)
  140. }
  141. if len(ids) > 0 {
  142. db = db.Where("patient_id in(?)", ids)
  143. }
  144. err = db.Group("patient_id").Find(&advice).Error
  145. return advice, err
  146. }
  147. func GetTodayAdviceCountTwo(stime int64, etime int64, orgid int64, is_medicine int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  148. db := XTReadDB().Model(&advice).Where("status=1 and is_medicine = ?", is_medicine)
  149. if stime > 0 {
  150. db = db.Where("advice_date>=?", stime)
  151. }
  152. if etime > 0 {
  153. db = db.Where("advice_date<=?", stime)
  154. }
  155. if orgid > 0 {
  156. db = db.Where("user_org_id = ?", orgid)
  157. }
  158. err = db.Preload("Patient", "user_org_id = ? and status=1", orgid).Group("patient_id").Find(&advice).Error
  159. return advice, err
  160. }
  161. func GetPatientAdviceScheduleByPatient(patient_id int64, user_org_id int64, advice_date int64) (list []*models.XtSchedule, err error) {
  162. err = XTReadDB().Where("patient_id= ? and user_org_id = ? and schedule_date = ? and status=1", patient_id, user_org_id, advice_date).Find(&list).Error
  163. return list, err
  164. }
  165. func GetPatientByAdviceId(patient_id int64) (models.TmpPatientOne, error) {
  166. patient := models.TmpPatientOne{}
  167. err = XTReadDB().Where("id = ?", patient_id).Find(&patient).Error
  168. return patient, err
  169. }
  170. // (改)
  171. func GetTodayDrug(stime, etime, orgid, is_medicine int64, keyword string) (list []*models.TmpPatient, err error) {
  172. InitDrugidIsNil(orgid, stime, etime)
  173. //病人
  174. var tmp []*models.TmpTTT
  175. //tmp := make([]string,0)
  176. if is_medicine == 0 {
  177. if keyword != "" {
  178. keyword = "%" + keyword + "%"
  179. err = XTReadDB().Raw("select a.* from(select distinct patient_id,dispensing_time from his_doctor_advice_info where "+
  180. "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
  181. "patient_id in (select id from xt_patients where user_org_id = ? and name like ? or dialysis_no like ?) and "+
  182. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 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 advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
  186. "patient_id in (select id from xt_patients where user_org_id = ? and name like ? or dialysis_no like ?) and "+
  187. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1))a order by dispensing_time desc ",
  188. stime, etime, orgid, is_medicine, orgid, keyword, keyword, orgid, stime, etime, orgid, is_medicine, orgid, keyword, keyword, orgid).Scan(&tmp).Error
  189. } else {
  190. err = XTReadDB().Raw("select a.* from(select distinct patient_id,dispensing_time from his_doctor_advice_info where "+
  191. "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
  192. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) "+
  193. "union "+
  194. "select distinct patient_id,dispensing_time from xt_doctor_advice where "+
  195. "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
  196. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1))a order by dispensing_time desc",
  197. stime, etime, orgid, is_medicine, orgid, stime, etime, orgid, is_medicine, orgid).Scan(&tmp).Error
  198. }
  199. if err != nil {
  200. return
  201. }
  202. } else {
  203. if keyword != "" {
  204. keyword = "%" + keyword + "%"
  205. err = XTReadDB().Raw("select a.* from(select distinct patient_id,dispensing_time from his_doctor_advice_info where "+
  206. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  207. "patient_id in (select id from xt_patients where user_org_id = ? and name like ? or dialysis_no like ?) and "+
  208. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) "+
  209. "union "+
  210. "select distinct patient_id,dispensing_time from xt_doctor_advice where "+
  211. "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  212. "patient_id in (select id from xt_patients where user_org_id = ? and name like ? or dialysis_no like ?) and "+
  213. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1))a order by dispensing_time desc ",
  214. stime, etime, orgid, is_medicine, orgid, keyword, keyword, orgid, stime, etime, orgid, is_medicine, orgid, keyword, keyword, orgid).Scan(&tmp).Error
  215. } else {
  216. err = XTReadDB().Raw("select a.* from(select distinct patient_id,dispensing_time from his_doctor_advice_info where "+
  217. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  218. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) "+
  219. "union "+
  220. "select distinct patient_id,dispensing_time from xt_doctor_advice where "+
  221. "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  222. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1))a order by dispensing_time desc",
  223. stime, etime, orgid, is_medicine, orgid, stime, etime, orgid, is_medicine, orgid).Scan(&tmp).Error
  224. }
  225. if err != nil {
  226. return
  227. }
  228. }
  229. tmp_s := make([]int64, 0)
  230. s_map := make(map[int64]int64)
  231. for _, v := range tmp {
  232. if _, ok := s_map[v.PatientID]; !ok {
  233. s_map[v.PatientID] = v.PatientID
  234. tmp_s = append(tmp_s, v.PatientID)
  235. }
  236. }
  237. list, err = GetManyUsers(tmp_s)
  238. return
  239. }
  240. // 查询患者(改)
  241. func GetManyUsers(tmp []int64) (list []*models.TmpPatient, err error) {
  242. for i := 0; i < len(tmp); i++ {
  243. var tt models.Patients
  244. err = XTReadDB().Model(&models.Patients{}).Where("id = ?", tmp[i]).Find(&tt).Error
  245. tmp_list := &models.TmpPatient{
  246. PatientID: tt.ID,
  247. Name: tt.Name,
  248. DialysisNo: tt.DialysisNo,
  249. }
  250. list = append(list, tmp_list)
  251. }
  252. return
  253. }
  254. // 查询患者当天时间段中的药,is_medicine:0未发,1已发(改)
  255. func GetPatientMedication(orgid, patient_id, stime, etime, is_medicine int64) (pp []*models.PharmacyContent, err error) {
  256. InitDrugidIsNil(orgid, stime, etime)
  257. var tmp []*models.HisDoctorAdviceInfoL
  258. if orgid == 10375 {
  259. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Preload("Drug", func(db *gorm.DB) *gorm.DB {
  260. return db.Preload("Manufacturers", "status = 1").Where("status = 1")
  261. }).Where(
  262. "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
  263. if err != nil {
  264. return pp, err
  265. }
  266. for _, v := range tmp {
  267. pp = append(pp, &models.PharmacyContent{
  268. ID: v.ID,
  269. Name: v.AdviceName,
  270. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  271. Usage: v.DeliveryWay,
  272. Frequency: v.ExecutionFrequency,
  273. Days: config.ToString(v.Day) + "天",
  274. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  275. Doctor: GetAdminUserName(v.AdviceDoctor, orgid), //开立医生
  276. DataSources: "his处方",
  277. Remarks: v.Remark, //备注
  278. DrugId: v.DrugId,
  279. Price: v.Price,
  280. DrugCode: v.DrugCode,
  281. ZeroFlag: config.ToString(v.ZeroFlag),
  282. MName: v.Drug.Manufacturers.ManufacturerName,
  283. Spec: v.Drug.Dose + v.Drug.DoseUnit + strconv.FormatInt(v.Drug.MinNumber, 10) + v.Drug.MinUnit + "/" + v.Drug.MaxUnit,
  284. Type: 1,
  285. Bm: v.Drug.MedicalInsuranceNumber,
  286. })
  287. }
  288. var tmp_advice []*models.XtDoctorAdviceL
  289. if is_medicine == 0 {
  290. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  291. "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
  292. if err != nil {
  293. return pp, err
  294. }
  295. } else {
  296. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  297. "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
  298. if err != nil {
  299. return pp, err
  300. }
  301. }
  302. for _, v := range tmp_advice {
  303. pp = append(pp, &models.PharmacyContent{
  304. ID: v.ID,
  305. Name: v.AdviceName,
  306. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  307. Usage: v.DeliveryWay,
  308. Frequency: v.ExecutionFrequency,
  309. Days: "-",
  310. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  311. Doctor: GetAdminUserName(v.AdviceDoctor, orgid),
  312. DataSources: "临时医嘱",
  313. Remarks: v.Remark,
  314. DrugId: v.DrugId,
  315. DrugCode: v.DrugCode,
  316. Type: 1,
  317. })
  318. }
  319. return
  320. }
  321. if orgid != 10375 {
  322. if is_medicine == 0 {
  323. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Preload("Drug", func(db *gorm.DB) *gorm.DB {
  324. return db.Preload("Manufacturers", "status = 1").Where("status = 1")
  325. }).Where(
  326. "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
  327. if err != nil {
  328. return pp, err
  329. }
  330. } else {
  331. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Preload("Drug", func(db *gorm.DB) *gorm.DB {
  332. return db.Preload("Manufacturers", "status = 1").Where("status = 1")
  333. }).Where(
  334. "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
  335. if err != nil {
  336. return pp, err
  337. }
  338. }
  339. for _, v := range tmp {
  340. pp = append(pp, &models.PharmacyContent{
  341. ID: v.ID,
  342. Name: v.AdviceName,
  343. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  344. Usage: v.DeliveryWay,
  345. Frequency: v.ExecutionFrequency,
  346. Days: config.ToString(v.Day) + "天",
  347. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  348. Doctor: GetAdminUserName(v.AdviceDoctor, orgid), //开立医生
  349. DataSources: "his处方",
  350. Remarks: v.Remark, //备注
  351. DrugId: v.DrugId,
  352. ExecutionState: v.ExecutionState,
  353. ExecutionStaff: v.ExecutionStaff,
  354. ExecutionTime: v.ExecutionTime,
  355. Price: v.Price,
  356. DrugCode: v.DrugCode,
  357. ZeroFlag: config.ToString(v.ZeroFlag),
  358. MName: v.Drug.Manufacturers.ManufacturerName,
  359. Spec: v.Drug.Dose + v.Drug.DoseUnit + strconv.FormatInt(v.Drug.MinNumber, 10) + v.Drug.MinUnit + "/" + v.Drug.MaxUnit,
  360. Type: 1,
  361. Bm: v.Drug.MedicalInsuranceNumber,
  362. })
  363. }
  364. var tmp_advice []*models.XtDoctorAdviceL
  365. if is_medicine == 0 {
  366. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  367. "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
  368. if err != nil {
  369. return pp, err
  370. }
  371. } else {
  372. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  373. "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
  374. if err != nil {
  375. return pp, err
  376. }
  377. }
  378. for _, v := range tmp_advice {
  379. pp = append(pp, &models.PharmacyContent{
  380. ID: v.ID,
  381. Name: v.AdviceName,
  382. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  383. Usage: v.DeliveryWay,
  384. Frequency: v.ExecutionFrequency,
  385. Days: "-",
  386. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  387. Doctor: GetAdminUserName(v.AdviceDoctor, orgid),
  388. DataSources: "临时医嘱",
  389. Remarks: v.Remark,
  390. DrugId: v.DrugId,
  391. ExecutionState: v.ExecutionState,
  392. ExecutionStaff: v.ExecutionStaff,
  393. ExecutionTime: v.ExecutionTime,
  394. DrugCode: v.DrugCode,
  395. Type: 1,
  396. //Price: v.Price,
  397. })
  398. }
  399. return
  400. }
  401. return
  402. }
  403. func GetPatientProject(orgid, patient_id, stime, etime, is_medicine int64) (pp []*models.PharmacyContent, err error) {
  404. var tmp []*models.HisPrintPrescriptionProjectL
  405. if is_medicine == 0 {
  406. err = XTReadDB().Model(&models.HisPrintPrescriptionProjectL{}).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  407. return db.Preload("Manufacturers", "status = 1").Where("status = 1")
  408. }).Where(
  409. "status = 1 and record_date >= ? and record_date <= ? and user_org_id = ? and patient_id = ? and is_medicine = ? and type = 3", stime, etime, orgid, patient_id, is_medicine).Find(&tmp).Error
  410. if err != nil {
  411. return pp, err
  412. }
  413. } else {
  414. err = XTReadDB().Model(&models.HisPrintPrescriptionProjectL{}).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  415. return db.Preload("Manufacturers", "status = 1").Where("status = 1")
  416. }).Where(
  417. "status = 1 and record_date >= ? and record_date <= ? and user_org_id = ? and patient_id = ? and is_medicine = ? and type = 3", stime, etime, orgid, patient_id, is_medicine).Find(&tmp).Error
  418. if err != nil {
  419. return pp, err
  420. }
  421. }
  422. for _, v := range tmp {
  423. pp = append(pp, &models.PharmacyContent{
  424. ID: v.ID,
  425. Name: v.GoodInfo.GoodName,
  426. SingleDosage: config.ToString(v.SingleDose) + v.Unit,
  427. Usage: v.DeliveryWay,
  428. Frequency: v.ExecutionFrequency,
  429. Days: config.ToString(v.Day) + "天",
  430. Total: config.ToString(v.Count) + v.Unit,
  431. Doctor: GetAdminUserName(v.Doctor, orgid), //开立医生
  432. DataSources: "his处方",
  433. Remarks: v.Remark, //备注
  434. DrugId: v.GoodInfo.ID,
  435. ExecutionState: v.ExecutionState,
  436. ExecutionStaff: v.ExecutionStaff,
  437. ExecutionTime: v.ExecutionTime,
  438. Price: v.Price,
  439. DrugCode: v.DrugCode,
  440. MName: v.GoodInfo.Manufacturers.ManufacturerName,
  441. Spec: v.GoodInfo.SpecificationName,
  442. Type: 2,
  443. Bm: v.GoodInfo.MedicalInsuranceNumber,
  444. })
  445. }
  446. return
  447. }
  448. // 获取创建者姓名(改)
  449. func GetAdminUserName(doctor, orgid int64) string {
  450. var tmp models.VmUserAdminRole
  451. XTReadDB().Model(&models.VmUserAdminRole{}).Where("admin_user_id = ? and org_id = ? and status = 1", doctor, orgid).Find(&tmp)
  452. return tmp.UserName
  453. }
  454. // 查询该机构的药房配置,true: 通过药房发药,false:不通过药房发药。如果没有该机构的信息则生成一条数据
  455. func GetOrgIdPharmacyConfig(orgid int64) bool {
  456. var total int
  457. var tmp_pharmacy models.PharmacyConfig
  458. XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and status = 1", orgid).Find(&tmp_pharmacy).Count(&total)
  459. if total > 0 {
  460. //有记录
  461. if tmp_pharmacy.IsOpen == 1 {
  462. return true
  463. } else {
  464. return false
  465. }
  466. } else {
  467. //没有记录生成一条
  468. tmp := models.PharmacyConfig{
  469. UserOrgId: orgid,
  470. IsOpen: 2,
  471. Status: 1,
  472. Ctime: time.Now().Unix(),
  473. Mtime: time.Now().Unix(),
  474. }
  475. XTWriteDB().Create(&tmp)
  476. return false
  477. }
  478. }
  479. // 修改该机构的药房配置
  480. func ModifyPharmacyConfig(orgid, isopen int64) (err error) {
  481. if isopen == 1 || isopen == 2 {
  482. err = XTWriteDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{
  483. "mtime": time.Now().Unix(),
  484. "is_open": isopen,
  485. }).Error
  486. } else {
  487. err = fmt.Errorf("类型解析错误")
  488. }
  489. return
  490. }
  491. // 查询该药品是否通过药房发药.0否;1是
  492. func DrugAllocation(drug_id int64) (is_pharmacy int64, err error) {
  493. tmp := models.BaseDrugLib{}
  494. err = XTReadDB().Model(&models.BaseDrugLib{}).Where("id = ?", drug_id).Find(&tmp).Error
  495. return tmp.IsPharmacy, err
  496. }
  497. // 根据id查询该药是否发药
  498. func MedicineState(id int64) (is_medicine models.HisDoctorAdviceInfoL, err error) {
  499. tmp := models.HisDoctorAdviceInfoL{}
  500. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ?", id).Find(&tmp).Error
  501. return tmp, err
  502. }
  503. // 发药明细列表(
  504. func DispensingDetailsList(stime, etime, orgid, page, limit int64, keyword string) (dislist []*models.DispensingList, total int64, err error) {
  505. var fenye []*models.Pharmary //分页用的
  506. offset := (page - 1) * limit
  507. dbone := XTReadDB().Model(&models.Pharmary{}).Where("status = 1 and user_org_id = ? and record_date >= ? and record_date <= ?", orgid, stime, etime)
  508. if keyword != "" {
  509. var tmp_id []*models.TmpID
  510. keyword = "%" + keyword + "%"
  511. XTReadDB().Raw("select id from xt_patients where name like ? and user_org_id = ?", keyword, orgid).Scan(&tmp_id)
  512. tmp_ids := make([]int64, 0)
  513. for _, v := range tmp_id {
  514. tmp_ids = append(tmp_ids, v.Id)
  515. }
  516. dbone = dbone.Where("patient_id in (?)", tmp_ids)
  517. }
  518. //查询出分页用的数据
  519. err = dbone.Count(&total).Offset(offset).Order("mtime desc").Limit(limit).Find(&fenye).Error
  520. if err != nil {
  521. return
  522. }
  523. for _, v := range fenye {
  524. tmp_doctorid, tmp_doctorname, errs := GetDoctorIds(v.PatientId, v.RecordDate, orgid)
  525. if errs != nil {
  526. err = errs
  527. return
  528. }
  529. pat, _ := GetPatientName(v.PatientId)
  530. dislist = append(dislist, &models.DispensingList{
  531. PatientID: v.PatientId,
  532. Name: pat.Name,
  533. DoctorId: tmp_doctorid,
  534. DoctorName: tmp_doctorname,
  535. RecordTime: v.RecordDate,
  536. RecordDate: fmt.Sprintf(time.Unix(v.RecordDate, 0).Format("2006-01-02 15:04:05")),
  537. })
  538. }
  539. return
  540. }
  541. // 处方详情//////////////////////
  542. func PrescriptionDetails(patient_id, record_date, orgid int64) (pre []*models.PrescripDetails, err error) {
  543. var tmp []*models.HisDoctorAdviceInfoL
  544. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
  545. "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
  546. if err != nil {
  547. return
  548. }
  549. for _, v := range tmp {
  550. pre = append(pre, &models.PrescripDetails{
  551. Drugname: v.AdviceName,
  552. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  553. Usage: v.DeliveryWay,
  554. Frequency: v.ExecutionFrequency,
  555. Days: config.ToString(v.Day) + "天",
  556. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  557. UnitPrice: config.ToString(v.Price) + "元",
  558. Remarks: v.Remark,
  559. })
  560. }
  561. var tmp_advice []*models.XtDoctorAdviceL
  562. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  563. "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
  564. if err != nil {
  565. return
  566. }
  567. for _, v := range tmp_advice {
  568. pre = append(pre, &models.PrescripDetails{
  569. Drugname: v.AdviceName,
  570. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  571. Usage: v.DeliveryWay,
  572. Frequency: v.ExecutionFrequency,
  573. Days: "-",
  574. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  575. UnitPrice: "-",
  576. Remarks: v.Remark,
  577. })
  578. }
  579. return
  580. }
  581. // 查询病人的his id
  582. func GetHisID(prescription_id, orgid int64) (id int64) {
  583. var tmp models.HisPrintPrescription
  584. XTReadDB().Model(&models.HisPrintPrescription{}).Where("id = ?", prescription_id).Find(&tmp)
  585. var fin models.HisPrescriptionInfoTwo
  586. XTReadDB().Model(&models.HisPrescriptionInfoTwo{}).Where("prescription_number = ? and user_org_id = ?", tmp.PrescriptionNumber, orgid).Find(&fin)
  587. return fin.ID
  588. }
  589. // 根据药品id获取药品信息
  590. func GetDrugNameTX(id int64, tx *gorm.DB) (tmp models.SpBaseDrug, err error) {
  591. err = tx.Model(&models.SpBaseDrug{}).Where("id = ? and status = 1", id).Find(&tmp).Error
  592. return
  593. }
  594. func GetXtManufacturer(id int64, tx *gorm.DB) (name string, err error) {
  595. var tmp models.Manufacturer
  596. err = tx.Model(&models.Manufacturer{}).Where("id = ? and status = 1", id).Find(&tmp).Error
  597. name = tmp.ManufacturerName
  598. return
  599. }
  600. // 查询药品(使用keyword)
  601. func GetManyDrugs(orgid int64, keyword string) (map[int64]models.PharmacyBaseDrug, error) {
  602. var tmp []*models.PharmacyBaseDrug
  603. tt := make(map[int64]models.PharmacyBaseDrug)
  604. err := XTReadDB().Model(&models.PharmacyBaseDrug{}).Where(" org_id = ? ", orgid).Where(" drug_name like ? ", "%"+keyword+"%").Find(&tmp).Error
  605. for _, v := range tmp {
  606. tt[v.ID] = *v
  607. }
  608. return tt, err
  609. }
  610. // 查询(
  611. func GetTodayMedicine(stime, etime, orgid, is_medicine int64, keyword string) (finlly []*models.ListOfDrugs, err error) {
  612. InitDrugidIsNil(orgid, stime, etime)
  613. var tmp []*models.TmpLLL
  614. if is_medicine == 0 {
  615. if keyword != "" {
  616. keyword = "%" + keyword + "%"
  617. err = XTReadDB().Raw("select distinct drug_id from his_doctor_advice_info where "+
  618. "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
  619. "drug_id in (select id from xt_base_drug where org_id = ? and drug_name like ? and is_pharmacy = 1) "+
  620. "union "+
  621. "select distinct drug_id from xt_doctor_advice where "+
  622. "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
  623. "drug_id in (select id from xt_base_drug where org_id = ? and drug_name like ? and is_pharmacy = 1)",
  624. stime, etime, orgid, is_medicine, orgid, keyword, stime, etime, orgid, is_medicine, orgid, keyword).Scan(&tmp).Error
  625. } else {
  626. err = XTReadDB().Raw("select distinct drug_id from his_doctor_advice_info where "+
  627. "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
  628. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) "+
  629. "union "+
  630. "select distinct drug_id from xt_doctor_advice where "+
  631. "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
  632. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) ",
  633. stime, etime, orgid, is_medicine, orgid, stime, etime, orgid, is_medicine, orgid).Scan(&tmp).Error
  634. }
  635. if err != nil {
  636. return
  637. }
  638. } else {
  639. if keyword != "" {
  640. keyword = "%" + keyword + "%"
  641. err = XTReadDB().Raw("select distinct drug_id from his_doctor_advice_info where "+
  642. "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
  643. "drug_id in (select id from xt_base_drug where org_id = ? and drug_name like ? and is_pharmacy = 1) "+
  644. "union "+
  645. "select distinct drug_id from xt_doctor_advice where "+
  646. "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
  647. "drug_id in (select id from xt_base_drug where org_id = ? and drug_name like ? and is_pharmacy = 1)",
  648. stime, etime, orgid, is_medicine, orgid, keyword, stime, etime, orgid, is_medicine, orgid, keyword).Scan(&tmp).Error
  649. } else {
  650. err = XTReadDB().Raw("select distinct drug_id from his_doctor_advice_info where "+
  651. "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
  652. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) "+
  653. "union "+
  654. "select distinct drug_id from xt_doctor_advice where "+
  655. "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
  656. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) ",
  657. stime, etime, orgid, is_medicine, orgid, stime, etime, orgid, is_medicine, orgid).Scan(&tmp).Error
  658. }
  659. if err != nil {
  660. return
  661. }
  662. }
  663. for _, v := range tmp {
  664. name, specifications := FindDrugSpecifications(v.DrugId)
  665. number, errs := GetInventoryQuantity(orgid, v.DrugId)
  666. if errs != nil {
  667. err = errs
  668. return
  669. }
  670. finlly = append(finlly, &models.ListOfDrugs{
  671. ID: v.DrugId,
  672. Name: name,
  673. Specifications: specifications,
  674. Stock: number,
  675. })
  676. }
  677. return
  678. }
  679. // 根据药品id获取药品规格(
  680. func FindDrugSpecifications(id int64) (name, specifications string) {
  681. var tmp models.PharmacyBaseDrug
  682. XTReadDB().Model(&models.PharmacyBaseDrug{}).Where("id = ?", id).Find(&tmp)
  683. name = tmp.DrugName
  684. specifications = config.ToString(tmp.Dose) + tmp.DoseUnit + "*" + config.ToString(tmp.MinNumber) + tmp.MinUnit + "/" + config.ToString(tmp.MaxUnit)
  685. return
  686. }
  687. // 根据药品id获取当前药品出库仓库的库存数量
  688. func GetInventoryQuantity(orgid, drugid int64) (number string, err error) {
  689. //获取药品拆零数量
  690. var phar models.PharmacyBaseDrug
  691. XTReadDB().Model(&models.PharmacyBaseDrug{}).Where("id = ?", drugid).Find(&phar)
  692. min_number := phar.MinNumber
  693. if min_number == 0 {
  694. err = fmt.Errorf("药品拆零数量不能为零!")
  695. return
  696. }
  697. storeconfig, _ := FindStorehouseConfig(orgid)
  698. var tmp []models.SpDrugWarehouseInfo
  699. XTReadDB().Model(&models.SpDrugWarehouseInfo{}).Where("org_id = ? and drug_id = ? and storehouse_id = ? and status = 1", orgid, drugid, storeconfig.DrugStorehouseOut).Find(&tmp)
  700. max, min := int64(0), int64(0)
  701. for _, v := range tmp {
  702. max += v.StockMaxNumber
  703. min += v.StockMinNumber
  704. }
  705. max += min / min_number
  706. min = min % min_number
  707. if max != 0 {
  708. number = config.ToString(max) + phar.MaxUnit
  709. }
  710. if min != 0 {
  711. number = number + config.ToString(min) + phar.MinUnit
  712. }
  713. if number == "" {
  714. number = "0"
  715. }
  716. //number = config.ToString(max)+phar.MaxUnit+config.ToString(min)+phar.MinUnit
  717. return
  718. }
  719. // 获取该药品的病人信息(
  720. func FindMedicationList(orgid, drug_id, stime, etime, is_medicine int64) (pp []*models.PatientInformation, err error) { ///////////////
  721. InitDrugidIsNil(orgid, stime, etime)
  722. var tmp []*models.HisDoctorAdviceInfoL
  723. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
  724. "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and drug_id = ? and is_medicine = ?", stime, etime, orgid, drug_id, is_medicine).Find(&tmp).Error
  725. if err != nil {
  726. return pp, err
  727. }
  728. for _, v := range tmp {
  729. pp = append(pp, &models.PatientInformation{
  730. Id: "h" + config.ToString(v.ID),
  731. Name: FindUserName(v.PatientId),
  732. PatientId: v.PatientId,
  733. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  734. Usage: v.DeliveryWay,
  735. Frequency: v.ExecutionFrequency,
  736. Days: config.ToString(v.Day) + "天",
  737. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  738. DataSources: "his处方",
  739. People: GetUserAdminName(v.People, orgid), //领药人!!!!!!!!!!!!!!!!!!!!!!
  740. Quantity: int64(v.PrescribingNumber),
  741. Unit: v.PrescribingNumberUnit,
  742. DrugCode: v.DrugCode,
  743. ID: v.ID,
  744. })
  745. }
  746. var tmp_advice []*models.XtDoctorAdviceL
  747. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  748. "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and drug_id = ? and is_medicine = ?", stime, etime, orgid, drug_id, is_medicine).Find(&tmp_advice).Error
  749. if err != nil {
  750. return pp, err
  751. }
  752. for _, v := range tmp_advice {
  753. pp = append(pp, &models.PatientInformation{
  754. Id: "x" + config.ToString(v.ID),
  755. Name: FindUserName(v.PatientId),
  756. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  757. Usage: v.DeliveryWay,
  758. Frequency: v.ExecutionFrequency,
  759. Days: "",
  760. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  761. DataSources: "临时医嘱",
  762. People: GetUserAdminName(v.People, orgid), //领药人
  763. Quantity: int64(v.PrescribingNumber),
  764. Unit: v.PrescribingNumberUnit,
  765. DrugCode: v.DrugCode,
  766. ID: v.ID,
  767. })
  768. }
  769. return
  770. }
  771. // 患者发药按钮点击(
  772. func DispensingMedicine(orgid, patient_id, stime, etime, creater int64) (err error) {
  773. //开事务
  774. tx := XTWriteDB().Begin()
  775. defer func() {
  776. if err != nil {
  777. utils.ErrorLog("事务失败,原因为: %v", err)
  778. tx.Rollback()
  779. } else {
  780. tx.Commit()
  781. }
  782. }()
  783. time_now := time.Now().Unix()
  784. var hids []*models.TmpID
  785. var xids []*models.TmpID
  786. var projectids []*models.TmpID
  787. err = tx.Raw("select id from his_doctor_advice_info where status = 1 and advice_date >= ? and advice_date <= ? "+
  788. "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
  789. if err != nil {
  790. return
  791. }
  792. err = tx.Raw("select id from his_prescription_project where status = 1 and record_date >= ? and record_date <= ? "+
  793. "and user_org_id = ? and patient_id = ? and is_medicine = 0 ", stime, etime, orgid, patient_id).Scan(&projectids).Error
  794. if err != nil {
  795. return
  796. }
  797. hid := make([]int64, 0)
  798. pid := make([]int64, 0)
  799. for _, v := range hids {
  800. hid = append(hid, v.Id)
  801. }
  802. for _, v := range projectids {
  803. pid = append(pid, v.Id)
  804. }
  805. var advice_info []*models.HisDoctorAdviceInfo
  806. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", hid).Find(&advice_info).Error
  807. if err != nil {
  808. return
  809. }
  810. var project_info []*models.HisPrescriptionProject
  811. err = tx.Model(&models.HisPrescriptionProject{}).Where("id in (?) and status = 1", pid).Find(&project_info).Error
  812. if err != nil {
  813. return
  814. }
  815. if orgid != 10480 {
  816. for _, v := range advice_info {
  817. tmp_bool := IsPharmacyConfig(orgid)
  818. if tmp_bool {
  819. if PettyCash(v.DrugId) {
  820. continue
  821. }
  822. if orgid != 10217 && orgid != 10188 && orgid != 9671 && orgid != 10164 && orgid != 10387 && orgid != 10375 && orgid != 10480 {
  823. ////判断药品库存是否充足
  824. kou := FenDrugInventory(v, orgid)
  825. if !kou {
  826. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  827. return
  828. }
  829. }
  830. //扣减库存
  831. err = FenStock(orgid, creater, v)
  832. if err != nil {
  833. err = fmt.Errorf("!:%v", err)
  834. return
  835. }
  836. }
  837. }
  838. }
  839. //修改状态
  840. errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id in (?)", hid).Updates(map[string]interface{}{
  841. "is_medicine": 1,
  842. "people": 0,
  843. "dispensing_time": time_now,
  844. }).Error
  845. if errs != nil {
  846. return errs
  847. }
  848. err1 := ChangeHisPrescription(hid)
  849. if err1 != nil {
  850. return err1
  851. }
  852. //针对大丰响水
  853. if orgid != 10217 && orgid != 10188 && orgid != 9671 && orgid != 10164 && orgid != 10387 && orgid != 10375 && orgid != 10480 {
  854. 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 "+
  855. "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
  856. if err != nil {
  857. return
  858. }
  859. xid := make([]int64, 0)
  860. for _, v := range xids {
  861. xid = append(xid, v.Id)
  862. }
  863. var advice []*models.HisDoctorAdviceInfo
  864. err = tx.Raw("select * from xt_doctor_advice where id in (?) and status = 1", xid).Scan(&advice).Error
  865. if err != nil {
  866. return
  867. }
  868. for _, v := range advice {
  869. tmp_bool := IsPharmacyConfig(orgid)
  870. if tmp_bool {
  871. if PettyCash(v.DrugId) {
  872. continue
  873. }
  874. kou := FenDrugInventory(v, orgid)
  875. if !kou {
  876. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  877. return
  878. }
  879. //扣减库存
  880. err = FenStock(orgid, creater, v)
  881. if err != nil {
  882. err = fmt.Errorf("!:%v", err)
  883. return
  884. }
  885. }
  886. }
  887. //修改状态
  888. errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id in (?)", xid).Updates(map[string]interface{}{
  889. "is_medicine": 1,
  890. "people": 0,
  891. "dispensing_time": time_now,
  892. "updated_time": time.Now().Unix(),
  893. }).Error
  894. if errs1 != nil {
  895. return errs1
  896. }
  897. }
  898. //生成明细记录
  899. tmp_ph := models.Pharmary{
  900. UserOrgId: orgid,
  901. PatientId: patient_id,
  902. Ctime: time.Now().Unix(),
  903. Mtime: time.Now().Unix(),
  904. Status: 1,
  905. RecordDate: time_now,
  906. }
  907. err = tx.Create(&tmp_ph).Error
  908. return
  909. }
  910. func DispensingGoods(orgid, patient_id, stime, etime, creater int64) (err error) {
  911. //开事务
  912. tx := XTWriteDB().Begin()
  913. defer func() {
  914. if err != nil {
  915. utils.ErrorLog("事务失败,原因为: %v", err)
  916. tx.Rollback()
  917. } else {
  918. tx.Commit()
  919. }
  920. }()
  921. time_now := time.Now().Unix()
  922. var projectids []*models.TmpID
  923. err = tx.Raw("select id from his_prescription_project where status = 1 and record_date >= ? and record_date <= ? "+
  924. "and user_org_id = ? and patient_id = ? and is_medicine = 0 and type = 3 ", stime, etime, orgid, patient_id).Scan(&projectids).Error
  925. if err != nil {
  926. return
  927. }
  928. pid := make([]int64, 0)
  929. for _, v := range projectids {
  930. pid = append(pid, v.Id)
  931. }
  932. var project_info []*models.HisPrescriptionProject
  933. err = tx.Model(&models.HisPrescriptionProject{}).Where("id in (?) and status = 1", pid).Find(&project_info).Error
  934. if err != nil {
  935. return
  936. }
  937. //修改状态
  938. errs := XTWriteDB().Model(&models.HisPrescriptionProject{}).Where("id in (?)", pid).Updates(map[string]interface{}{
  939. "is_medicine": 1,
  940. "dispensing_time": time_now,
  941. }).Error
  942. if errs != nil {
  943. return errs
  944. }
  945. err1 := ChangeHisPrescriptionByGood(pid)
  946. if err1 != nil {
  947. return err1
  948. }
  949. return
  950. }
  951. func DispensingMedicineOne(orgid, patient_id, stime, etime, creater int64) (err error) {
  952. //开事务
  953. tx := XTWriteDB().Begin()
  954. defer func() {
  955. if err != nil {
  956. utils.ErrorLog("事务失败,原因为: %v", err)
  957. tx.Rollback()
  958. } else {
  959. tx.Commit()
  960. }
  961. }()
  962. //查询当前患者未发药的列表
  963. adviceList, err := GetHisPatientNoMedical(orgid, patient_id, stime, etime)
  964. //发药
  965. if len(adviceList) > 0 {
  966. for _, item := range adviceList {
  967. if orgid != 10480 {
  968. pharmacyConfig := IsPharmacyConfig(orgid)
  969. if pharmacyConfig == true {
  970. //扣减库存
  971. err = HisFaDrugsDelivery(orgid, creater, item)
  972. if err != nil {
  973. err = fmt.Errorf("!:%v", err)
  974. return
  975. }
  976. }
  977. }
  978. //修改发药状态
  979. err = UpdateIsMedicalHisAdvice(item.ID)
  980. if err != nil {
  981. err = fmt.Errorf("!:%v", err)
  982. return
  983. }
  984. err = UpdateHisPrescription(item.PrescriptionId)
  985. if err != nil {
  986. err = fmt.Errorf("!:%v", err)
  987. return
  988. }
  989. //查询默认仓库
  990. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  991. //查询默认仓库剩余多少库存
  992. var sum_count int64
  993. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, item.DrugId)
  994. for _, its := range stockInfo {
  995. baseDrug, _ := GetBaseDrugMedical(its.DrugId)
  996. if its.MaxUnit == baseDrug.MaxUnit {
  997. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  998. }
  999. sum_count += its.StockMaxNumber + its.StockMinNumber
  1000. }
  1001. UpdateBaseDrugSumTwo(item.DrugId, sum_count, orgid)
  1002. //扣减库存
  1003. UpdateDrugStockCount(item.DrugId, item.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
  1004. over, _ := FindOverCount(item.DrugId, item.UserOrgId, storeHouseConfig.DrugStorehouseOut)
  1005. UpdateActOut(over.ID, over.SumInCount, over.FlushCount, over.SumCancelCount)
  1006. }
  1007. }
  1008. advice, err := GetDocAdviceList(orgid, patient_id, stime, etime)
  1009. //响水不走这里
  1010. if orgid != 10188 && orgid != 10480 && orgid != 10217 {
  1011. if len(advice) > 0 {
  1012. for _, item := range advice {
  1013. err = DrugsDelivery(item.UserOrgId, item.ExecutionStaff, item)
  1014. //查询默认仓库
  1015. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  1016. //查询默认仓库剩余多少库存
  1017. var sum_count int64
  1018. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, item.DrugId)
  1019. for _, its := range stockInfo {
  1020. baseDrug, _ := GetBaseDrugMedical(its.DrugId)
  1021. if its.MaxUnit == baseDrug.MaxUnit {
  1022. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  1023. }
  1024. sum_count += its.StockMaxNumber + its.StockMinNumber
  1025. }
  1026. UpdateBaseDrugSumTwo(item.DrugId, sum_count, orgid)
  1027. //扣减库存
  1028. UpdateDrugStockCount(item.DrugId, item.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
  1029. over, _ := FindOverCount(item.DrugId, item.UserOrgId, storeHouseConfig.DrugStorehouseOut)
  1030. UpdateActOut(over.ID, over.SumInCount, over.FlushCount, over.SumCancelCount)
  1031. if err != nil {
  1032. err = fmt.Errorf("!:%v", err)
  1033. return
  1034. }
  1035. err = UpdateIsAdvice(item.ID)
  1036. if err != nil {
  1037. err = fmt.Errorf("!:%v", err)
  1038. return
  1039. }
  1040. }
  1041. }
  1042. }
  1043. //生成明细记录
  1044. tmp_ph := models.Pharmary{
  1045. UserOrgId: orgid,
  1046. PatientId: patient_id,
  1047. Ctime: time.Now().Unix(),
  1048. Mtime: time.Now().Unix(),
  1049. Status: 1,
  1050. RecordDate: time.Now().Unix(),
  1051. }
  1052. err = tx.Create(&tmp_ph).Error
  1053. return
  1054. }
  1055. func GetHisPatientNoMedical(user_org_id int64, patient_id int64, startime int64, end_time int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  1056. 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
  1057. return advice, err
  1058. }
  1059. func GetHisPatientNoMedicalOne(user_org_id int64, patient_id int64, drug_id int64, advice_date int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  1060. err = XTReadDB().Where("user_org_id =? and patient_id = ? and drug_id =? and advice_date=? and is_medicine =0 and status=1", user_org_id, patient_id, drug_id, advice_date).Find(&advice).Error
  1061. return advice, err
  1062. }
  1063. func UpdateIsMedicalHisAdvice(id int64) (err error) {
  1064. adviceInfo := models.HisDoctorAdviceInfo{}
  1065. err = XTWriteDB().Model(&adviceInfo).Where("id =? and status =1", id).Update(map[string]interface{}{"is_medicine": 1, "dispensing_time": time.Now().Unix()}).Error
  1066. return err
  1067. }
  1068. func UpdateHisPrescription(id int64) (err error) {
  1069. prescriptionInfo := models.HisPrescription{}
  1070. err = XTWriteDB().Model(&prescriptionInfo).Where("id = ? and status=1", id).Update(map[string]interface{}{"is_medicine": 1, "mtime": time.Now().Unix()}).Error
  1071. return err
  1072. }
  1073. func GetDocAdviceList(user_org_id int64, patient_id int64, startime int64, end_time int64) (advice []*models.DoctorAdvice, err error) {
  1074. 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
  1075. return advice, err
  1076. }
  1077. func UpdateIsAdvice(id int64) (err error) {
  1078. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_medicine": 1, "dispensing_time": time.Now().Unix()}).Error
  1079. return err
  1080. }
  1081. func GetHisPrescriptionByStartTime(org_id int64, patient_id int64, start_time int64, end_time int64) (prescription []*models.HisPrescription, err error) {
  1082. err = XTReadDB().Where("user_org_id = ? and patient_id = ? and record_date>=? and record_date<=? and status=1 and is_medicine =1", org_id, patient_id, start_time, end_time).Find(&prescription).Error
  1083. return prescription, err
  1084. }
  1085. func ModiftyPrescpiton(id int64) (models.HisPrescription, error) {
  1086. prescription := models.HisPrescription{}
  1087. err := XTWriteDB().Model(&prescription).Where("id = ? and status=1", id).Updates(map[string]interface{}{"is_medicine": 0}).Error
  1088. return prescription, err
  1089. }
  1090. // 患者退药按钮点击
  1091. func DrugWithdrawal(orgid, patient_id, stime, etime, creater int64) (err error) {
  1092. //开事务
  1093. tx := XTWriteDB().Begin()
  1094. defer func() {
  1095. if err != nil {
  1096. utils.ErrorLog("事务失败,原因为: %v", err)
  1097. tx.Rollback()
  1098. } else {
  1099. tx.Commit()
  1100. }
  1101. }()
  1102. map_time := make(map[int64]int64)
  1103. var hids []*models.TmpID
  1104. var xids []*models.TmpID
  1105. //var hid,xid []int64//查询已发药的药品
  1106. err = tx.Raw("select id,dispensing_time from his_doctor_advice_info where status = 1 and advice_date >= ? and advice_date <= ? "+
  1107. "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
  1108. if err != nil {
  1109. return
  1110. }
  1111. hid := make([]int64, 0)
  1112. for _, v := range hids {
  1113. hid = append(hid, v.Id)
  1114. map_time[v.DispensingTime] = v.DispensingTime
  1115. }
  1116. //修改状态
  1117. errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id in (?)", hid).Updates(map[string]interface{}{
  1118. "is_medicine": 0,
  1119. "people": creater,
  1120. "dispensing_time": 0,
  1121. }).Error
  1122. if errs != nil {
  1123. return errs
  1124. }
  1125. err1 := ChangeHisPrescriptionT(hid)
  1126. if err1 != nil {
  1127. return err1
  1128. }
  1129. var advice_info []*models.HisDoctorAdviceInfo
  1130. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", hid).Find(&advice_info).Error
  1131. if err != nil {
  1132. return
  1133. }
  1134. //针对阜阳经沃
  1135. if orgid != 10480 {
  1136. for _, v := range advice_info {
  1137. //扣减库存
  1138. if !IsPharmacyDelivery(v.DrugId, orgid) {
  1139. continue
  1140. }
  1141. err = DrugAutoAddCancelInfo(v, creater)
  1142. if err != nil {
  1143. err = fmt.Errorf("!:%v", err)
  1144. return
  1145. }
  1146. //drug, _ := FindBaseDrugLibRecordSeven(orgid, v.DrugId)
  1147. //查询默认仓库
  1148. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  1149. //查询默认仓库剩余多少库存
  1150. var sum_count int64
  1151. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  1152. for _, its := range stockInfo {
  1153. baseDrug, _ := GetBaseDrugMedical(its.DrugId)
  1154. if its.MaxUnit == baseDrug.MaxUnit {
  1155. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  1156. }
  1157. sum_count += its.StockMaxNumber + its.StockMinNumber
  1158. }
  1159. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  1160. //扣减库存
  1161. UpdateDrugStockCount(v.DrugId, v.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
  1162. //退药
  1163. UpdateHisPrescriptionInfo(v.PatientId, v.AdviceDate, v.UserOrgId)
  1164. //退药
  1165. UpdateHisPrescriptionInfoOne(v.PatientId, v.AdviceDate, v.UserOrgId)
  1166. }
  1167. }
  1168. 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 "+
  1169. "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
  1170. if err != nil {
  1171. return
  1172. }
  1173. xid := make([]int64, 0)
  1174. for _, v := range xids {
  1175. xid = append(xid, v.Id)
  1176. map_time[v.DispensingTime] = v.DispensingTime
  1177. }
  1178. //修改状态
  1179. errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id in (?)", xid).Updates(map[string]interface{}{
  1180. "is_medicine": 0,
  1181. "people": creater,
  1182. "dispensing_time": 0,
  1183. "updated_time": time.Now().Unix(),
  1184. }).Error
  1185. if errs1 != nil {
  1186. return errs1
  1187. }
  1188. var advice []*models.HisDoctorAdviceInfo
  1189. err = tx.Raw("select * from xt_doctor_advice where id in (?) and status = 1", xid).Scan(&advice).Error
  1190. if err != nil {
  1191. return
  1192. }
  1193. if orgid != 10480 {
  1194. for _, v := range advice {
  1195. //扣减库存
  1196. if !IsPharmacyDelivery(v.DrugId, orgid) {
  1197. continue
  1198. }
  1199. err = DrugAutoAddCancelInfo(v, creater)
  1200. if err != nil {
  1201. err = fmt.Errorf("!:%v", err)
  1202. return
  1203. }
  1204. //查询默认仓库
  1205. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  1206. //查询默认仓库剩余多少库存
  1207. var sum_count int64
  1208. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  1209. for _, its := range stockInfo {
  1210. baseDrug, _ := GetBaseDrugMedical(its.DrugId)
  1211. if its.MaxUnit == baseDrug.MaxUnit {
  1212. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  1213. }
  1214. sum_count += its.StockMaxNumber + its.StockMinNumber
  1215. }
  1216. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  1217. //扣减库存
  1218. UpdateDrugStockCount(v.DrugId, v.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
  1219. }
  1220. }
  1221. //删除掉发药明细
  1222. for k, _ := range map_time {
  1223. err = tx.Model(&models.Pharmary{}).Where("user_org_id = ? and record_date = ? and patient_id = ? ", orgid, k, patient_id).Updates(map[string]interface{}{
  1224. "status": 0,
  1225. "mtime": time.Now().Unix(),
  1226. }).Error
  1227. if err != nil {
  1228. return
  1229. }
  1230. }
  1231. return
  1232. }
  1233. func DrugWithdrawalForGood(orgid, patient_id, stime, etime, creater int64) (err error) {
  1234. //开事务
  1235. tx := XTWriteDB().Begin()
  1236. defer func() {
  1237. if err != nil {
  1238. utils.ErrorLog("事务失败,原因为: %v", err)
  1239. tx.Rollback()
  1240. } else {
  1241. tx.Commit()
  1242. }
  1243. }()
  1244. map_time := make(map[int64]int64)
  1245. var hids []*models.TmpID
  1246. //var hid,xid []int64//查询已发药的药品
  1247. err = tx.Raw("select id,dispensing_time from his_prescription_project where type = 3 and status = 1 and record_date >= ? and record_date <= ?"+
  1248. " and user_org_id = ? and patient_id = ? and is_medicine = 1", stime, etime, orgid, patient_id).Scan(&hids).Error
  1249. if err != nil {
  1250. return
  1251. }
  1252. hid := make([]int64, 0)
  1253. for _, v := range hids {
  1254. hid = append(hid, v.Id)
  1255. map_time[v.DispensingTime] = v.DispensingTime
  1256. }
  1257. //修改状态
  1258. errs := XTWriteDB().Model(&models.HisPrintPrescriptionProjectL{}).Where("id in (?)", hid).Updates(map[string]interface{}{
  1259. "is_medicine": 0,
  1260. "dispensing_time": 0,
  1261. }).Error
  1262. if errs != nil {
  1263. return errs
  1264. }
  1265. ChangeHisPrescriptionTwo(hid)
  1266. //删除掉发药明细
  1267. for k, _ := range map_time {
  1268. err = tx.Model(&models.Pharmary{}).Where("user_org_id = ? and record_date = ? and patient_id = ? ", orgid, k, patient_id).Updates(map[string]interface{}{
  1269. "status": 0,
  1270. "mtime": time.Now().Unix(),
  1271. }).Error
  1272. if err != nil {
  1273. return
  1274. }
  1275. }
  1276. return
  1277. }
  1278. // 获取患者名称
  1279. func FindUserName(patient_id int64) (name string) {
  1280. var tmp models.GetHisName
  1281. XTReadDB().Model(&models.GetHisName{}).Where("id = ?", patient_id).Find(&tmp)
  1282. name = tmp.Name
  1283. return
  1284. }
  1285. // 获取药品名称
  1286. func FindDrugsName(drug_id int64) (name string) {
  1287. var tmp models.Drug
  1288. XTReadDB().Model(&models.Drug{}).Where("id = ?", drug_id).Find(&tmp)
  1289. name = tmp.DrugName
  1290. return
  1291. }
  1292. // 获取药品名称
  1293. func FindGoodsName(project_id int64) (name string) {
  1294. var tmp models.GoodInfo
  1295. XTReadDB().Model(&models.GoodInfo{}).Where("id = ?", project_id).Find(&tmp)
  1296. name = tmp.GoodName
  1297. return
  1298. }
  1299. // 药品发药按钮点击(
  1300. func MedicineDeparture(ids string, creater, orgid int64) (err error) {
  1301. //开事务
  1302. tx := XTWriteDB().Begin()
  1303. defer func() {
  1304. if err != nil {
  1305. utils.ErrorLog("事务失败,原因为: %v", err)
  1306. tx.Rollback()
  1307. } else {
  1308. tx.Commit()
  1309. }
  1310. }()
  1311. //处理下字符串
  1312. t_ids := ""
  1313. if ids[len(ids)-1] == 44 {
  1314. t_ids = ids[:len(ids)-1]
  1315. } else {
  1316. t_ids = ids
  1317. }
  1318. time_now := time.Now().Unix()
  1319. tmp_id := strings.Split(t_ids, ",")
  1320. var parameter string //测试
  1321. patient_id := make(map[int64]int64)
  1322. for _, v := range tmp_id {
  1323. front := v[:1]
  1324. after := v[1:]
  1325. if front == "h" {
  1326. var advice_info []*models.HisDoctorAdviceInfo
  1327. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and status = 1", after).Find(&advice_info).Error
  1328. if err != nil {
  1329. return
  1330. }
  1331. for _, v := range advice_info {
  1332. if _, ok := patient_id[v.PatientId]; !ok {
  1333. patient_id[v.PatientId] = v.PatientId
  1334. }
  1335. tmp_bool := IsPharmacyConfig(orgid)
  1336. if tmp_bool {
  1337. if PettyCash(v.DrugId) {
  1338. continue
  1339. }
  1340. kou := FenDrugInventory(v, orgid)
  1341. if !kou {
  1342. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  1343. tmp := models.XtErrs{
  1344. OrgId: orgid,
  1345. Route: "creater:" + config.ToString(creater),
  1346. Parameter: parameter,
  1347. Text_err: err.Error(),
  1348. }
  1349. XTWriteDB().Create(&tmp)
  1350. return
  1351. } else {
  1352. tmps, _ := json.Marshal(advice_info) //
  1353. parameter = parameter + string(tmps) //
  1354. }
  1355. //扣减库存
  1356. err = FenStock(orgid, creater, v)
  1357. if err != nil {
  1358. err = fmt.Errorf("!:%v", err)
  1359. return
  1360. }
  1361. }
  1362. }
  1363. //修改状态
  1364. errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ?", after).Updates(map[string]interface{}{
  1365. "is_medicine": 1,
  1366. "people": creater,
  1367. "dispensing_time": time_now,
  1368. }).Error
  1369. if errs != nil {
  1370. return errs
  1371. }
  1372. err1 := ChangeHisPrescriptionid(after)
  1373. if err1 != nil {
  1374. return err1
  1375. }
  1376. } else {
  1377. var advice []*models.HisDoctorAdviceInfo
  1378. 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
  1379. if err != nil {
  1380. return
  1381. }
  1382. for _, v := range advice {
  1383. if _, ok := patient_id[v.PatientId]; !ok {
  1384. patient_id[v.PatientId] = v.PatientId
  1385. }
  1386. tmp_bool := IsPharmacyConfig(orgid)
  1387. if tmp_bool {
  1388. codeConfig, _ := GetDrugCodeConfig(orgid)
  1389. if codeConfig.IsOpen == 1 {
  1390. if v.DrugCode == "" || len(v.DrugCode) == 0 {
  1391. err = fmt.Errorf(FindDrugsName(v.DrugId) + "药品追溯码不能为空!")
  1392. return
  1393. }
  1394. }
  1395. if PettyCash(v.DrugId) {
  1396. continue
  1397. }
  1398. kou := FenDrugInventory(v, orgid)
  1399. if !kou {
  1400. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  1401. tmp := models.XtErrs{
  1402. OrgId: orgid,
  1403. Route: "creater:" + config.ToString(creater),
  1404. Parameter: parameter,
  1405. Text_err: err.Error(),
  1406. }
  1407. XTWriteDB().Create(&tmp)
  1408. return
  1409. } else {
  1410. tmps, _ := json.Marshal(advice) //
  1411. parameter = parameter + string(tmps) //
  1412. }
  1413. //扣减库存
  1414. err = FenStock(orgid, creater, v)
  1415. if err != nil {
  1416. err = fmt.Errorf("!:%v", err)
  1417. return
  1418. }
  1419. }
  1420. }
  1421. //修改状态
  1422. errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id = ?", after).Updates(map[string]interface{}{
  1423. "is_medicine": 1,
  1424. "people": creater,
  1425. "dispensing_time": time_now,
  1426. "updated_time": time.Now().Unix(),
  1427. }).Error
  1428. if errs1 != nil {
  1429. return errs1
  1430. }
  1431. }
  1432. }
  1433. for _, v := range patient_id {
  1434. //生成明细记录
  1435. tmp_ph := models.Pharmary{
  1436. UserOrgId: orgid,
  1437. PatientId: v,
  1438. Ctime: time.Now().Unix(),
  1439. Mtime: time.Now().Unix(),
  1440. Status: 1,
  1441. RecordDate: time_now,
  1442. }
  1443. err = tx.Create(&tmp_ph).Error
  1444. if err != nil {
  1445. return err
  1446. }
  1447. }
  1448. return
  1449. }
  1450. // 获取领药人姓名(
  1451. func GetUserAdminName(id, orgID int64) string {
  1452. var tmp models.XTSgjUserAdminRole
  1453. XTReadDB().Model(&models.XTSgjUserAdminRole{}).Where("admin_user_id = ? and org_id = ?", id, orgID).Find(&tmp)
  1454. return tmp.UserName
  1455. }
  1456. // 根据id查询是否已发药,true已发药,false未发药gai
  1457. func GiveTheMedicine(id int64) bool {
  1458. var xue models.PharmacyDoctorAdvice
  1459. XTReadDB().Model(&models.PharmacyDoctorAdvice{}).Where("id = ?", id).Find(&xue)
  1460. if xue.IsMedicine == 1 {
  1461. return true
  1462. }
  1463. return false
  1464. }
  1465. // 查询改组中的药品是否包含已发药的,true已发药,false未发药
  1466. func GiveGroupMedicine(orgid, groupNo int64) bool {
  1467. var total int
  1468. XTReadDB().Model(&models.DoctorAdvice{}).Where("user_org_id = ? and groupno = ? and status = 1 and is_medicine = 1", orgid, groupNo).Count(&total)
  1469. if total > 0 {
  1470. return true
  1471. }
  1472. return false
  1473. }
  1474. // 查询处方中是否包含已发药的
  1475. func GiveChuMedicine(id int64) bool {
  1476. var total int
  1477. XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", id).Count(&total)
  1478. if total > 0 {
  1479. return true
  1480. }
  1481. return false
  1482. }
  1483. // 根据处方id查找该处方中是否存在已发药的药品
  1484. func IsChuIssuedDrugs(prescription_id int64) bool {
  1485. var total int
  1486. XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", prescription_id).Count(&total)
  1487. if total > 0 {
  1488. return true
  1489. }
  1490. return false
  1491. }
  1492. // 判断该药品是否通过药房管理出库,true是,false否
  1493. // id 药品id,org_id 机构id
  1494. func IsPharmacyDelivery(id, org_id int64) bool {
  1495. //判断药品是否通过药房管理
  1496. var total01 int
  1497. XTReadDB().Model(&models.PharmacyBaseDrug{}).Where("id = ? and is_pharmacy = 1", id).Count(&total01)
  1498. //判断该机构是否通过药房管理出库
  1499. var total02 int
  1500. XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and is_open = 1", org_id).Count(&total02)
  1501. if total01 > 0 && total02 > 0 {
  1502. return true
  1503. }
  1504. return false
  1505. }
  1506. // 判断机构是否通过药房管理出库
  1507. func IsPharmacyConfig(orgid int64) (bo bool) {
  1508. var total int
  1509. XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and is_open = 1 and status = 1", orgid).Count(&total)
  1510. if total > 0 {
  1511. return true
  1512. }
  1513. return false
  1514. }
  1515. // 根据患者id和发药时间,获取医生的id和姓名
  1516. func GetDoctorIds(id, recordtime, orgid int64) (doctor_id int64, doctor_name string, err error) {
  1517. var tmp []*models.TmpAdviceDoctor
  1518. //var tmp []int64
  1519. err = XTReadDB().Raw("select distinct advice_doctor from his_doctor_advice_info where "+
  1520. "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) "+
  1521. "union "+
  1522. "select distinct advice_doctor from xt_doctor_advice where "+
  1523. "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) ",
  1524. recordtime, orgid, id, orgid, recordtime, orgid, id, orgid).Scan(&tmp).Error
  1525. if err != nil || len(tmp) == 0 {
  1526. return
  1527. }
  1528. doctor_id = tmp[0].AdviceDoctor
  1529. doctor_name = GetUserAdminName(doctor_id, orgid)
  1530. return
  1531. }
  1532. func MapToJson(param map[string]interface{}) string {
  1533. dataType, _ := json.Marshal(param)
  1534. dataString := string(dataType)
  1535. return dataString
  1536. }
  1537. // 封装扣减库存
  1538. func FenStock(orgid, creater int64, v *models.HisDoctorAdviceInfo) (err error) {
  1539. err = HisDrugsDelivery(orgid, creater, v)
  1540. if err != nil {
  1541. err = fmt.Errorf("!:%v", err)
  1542. return
  1543. }
  1544. //查询默认仓库
  1545. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  1546. //查询默认仓库剩余多少库存
  1547. var sum_count int64
  1548. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  1549. for _, its := range stockInfo {
  1550. baseDrug, _ := GetBaseDrugMedical(its.DrugId)
  1551. if its.MaxUnit == baseDrug.MaxUnit {
  1552. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  1553. }
  1554. sum_count += its.StockMaxNumber + its.StockMinNumber
  1555. }
  1556. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  1557. //扣减库存
  1558. UpdateDrugStockCount(v.DrugId, v.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
  1559. over, _ := FindOverCount(v.DrugId, v.UserOrgId, storeHouseConfig.DrugStorehouseOut)
  1560. UpdateActOut(over.ID, over.SumInCount, over.FlushCount, over.SumCancelCount)
  1561. return
  1562. }
  1563. // 封装查询药品库存是否足够
  1564. func FenDrugInventory(item *models.HisDoctorAdviceInfo, orgid int64) bool {
  1565. var total int64
  1566. var prescribing_number_total int64
  1567. houseConfig, _ := GetAllStoreHouseConfig(orgid)
  1568. ////查询该药品是否有库存
  1569. list, _ := GetDrugTotalCountTwenty(item.DrugId, item.UserOrgId, houseConfig.DrugStorehouseOut)
  1570. ////查询改药品信息
  1571. medical, _ := GetBaseDrugMedical(item.DrugId)
  1572. ////判断单位是否相等
  1573. if medical.MaxUnit == item.PrescribingNumberUnit {
  1574. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1575. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1576. //转化为最小单位
  1577. total = list.Count*medical.MinNumber + list.StockMinNumber
  1578. prescribing_number_total = count * medical.MinNumber
  1579. }
  1580. if medical.MinUnit == item.PrescribingNumberUnit {
  1581. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1582. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1583. total = list.Count*medical.MinNumber + list.StockMinNumber
  1584. prescribing_number_total = count
  1585. }
  1586. if prescribing_number_total <= total {
  1587. //可以扣减
  1588. return true
  1589. } else {
  1590. //不可扣减
  1591. return false
  1592. }
  1593. }
  1594. func GetAdviceIds1(ids []string, orgid, execution_staff int64, thetime time.Time) (tmp []string, err error) {
  1595. var advice []*models.DoctorAdvice
  1596. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND status = 1", ids).Find(&advice).Error
  1597. if err != nil {
  1598. return
  1599. }
  1600. for _, v := range advice {
  1601. if !IsPharmacyDelivery(v.DrugId, orgid) {
  1602. tmp = append(tmp, config.ToString(v.ID))
  1603. } else {
  1604. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? ", v.ID).Updates(map[string]interface{}{
  1605. "execution_staff": execution_staff,
  1606. "execution_time": thetime.Unix(),
  1607. "updated_time": time.Now().Unix(),
  1608. }).Error
  1609. if err != nil {
  1610. return
  1611. }
  1612. }
  1613. }
  1614. return
  1615. }
  1616. func GetAdviceIds2(ids []string, orgid, execution_staff int64, thetime time.Time) (tmp []string, err error) {
  1617. var advice []*models.HisDoctorAdviceInfoL
  1618. err = readDb.Model(&models.HisDoctorAdviceInfoL{}).Where("id IN (?) AND status = 1", ids).Find(&advice).Error
  1619. if err != nil {
  1620. return
  1621. }
  1622. for _, v := range advice {
  1623. if !IsPharmacyDelivery(v.DrugId, orgid) {
  1624. tmp = append(tmp, config.ToString(v.ID))
  1625. } else {
  1626. err = XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? ", v.ID).Updates(map[string]interface{}{
  1627. "execution_staff": execution_staff,
  1628. "execution_time": thetime.Unix(),
  1629. "updated_time": time.Now().Unix(),
  1630. }).Error
  1631. if err != nil {
  1632. return
  1633. }
  1634. }
  1635. }
  1636. return
  1637. }
  1638. func GetAdviceId1(id, orgid, execution_staff int64, thetime time.Time) (bo bool, err error) {
  1639. var advice models.DoctorAdvice
  1640. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND status = 1", id).Find(&advice).Error
  1641. if err != nil {
  1642. return false, err
  1643. }
  1644. if IsPharmacyDelivery(advice.DrugId, orgid) {
  1645. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? ", id).Updates(map[string]interface{}{
  1646. "execution_staff": execution_staff,
  1647. "execution_time": thetime.Unix(),
  1648. "updated_time": time.Now().Unix(),
  1649. }).Error
  1650. if err != nil {
  1651. return false, err
  1652. }
  1653. return true, err
  1654. }
  1655. return false, err
  1656. }
  1657. func GetAdviceId2(id, orgid, execution_staff int64, thetime time.Time) (bo bool, err error) {
  1658. var advice models.HisDoctorAdviceInfoL
  1659. err = readDb.Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? AND status = 1", id).Find(&advice).Error
  1660. if err != nil {
  1661. return false, err
  1662. }
  1663. if IsPharmacyDelivery(advice.DrugId, orgid) {
  1664. err = XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? ", id).Updates(map[string]interface{}{
  1665. "execution_staff": execution_staff,
  1666. "execution_time": thetime.Unix(),
  1667. "updated_time": time.Now().Unix(),
  1668. }).Error
  1669. if err != nil {
  1670. return false, err
  1671. }
  1672. return true, err
  1673. }
  1674. return false, err
  1675. }
  1676. // 判断药品是否零用
  1677. func PettyCash(id int64) bool {
  1678. drug := models.XtBaseDrug{}
  1679. XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug)
  1680. if drug.IsUse == 1 {
  1681. return true
  1682. }
  1683. return false
  1684. }
  1685. // 初始化
  1686. func InitDrugidIsNil(orgid, stime, etime int64) {
  1687. var advice []*models.DoctorAdvice
  1688. 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)
  1689. for _, v := range advice {
  1690. 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)
  1691. }
  1692. }
  1693. // 改变处方状态(发药
  1694. func ChangeHisPrescription(tmp []int64) (err error) {
  1695. var advice_info []*models.HisDoctorAdviceInfo
  1696. err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", tmp).Find(&advice_info).Error
  1697. if err != nil {
  1698. if err == gorm.ErrRecordNotFound {
  1699. return nil
  1700. }
  1701. return
  1702. }
  1703. tmp_id := make(map[int64]int64)
  1704. for _, v := range advice_info {
  1705. tmp_id[v.PrescriptionId] = v.PrescriptionId
  1706. }
  1707. for k, _ := range tmp_id {
  1708. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", k).Updates(map[string]interface{}{
  1709. "is_medicine": 1,
  1710. "mtime": time.Now().Unix(),
  1711. }).Error
  1712. if err != nil {
  1713. return
  1714. }
  1715. }
  1716. return
  1717. }
  1718. func ChangeHisPrescriptionByGood(tmp []int64) (err error) {
  1719. var advice_info []*models.HisPrescriptionProject
  1720. err = XTReadDB().Model(&models.HisPrescriptionProject{}).Where("id in (?) and status = 1", tmp).Find(&advice_info).Error
  1721. if err != nil {
  1722. if err == gorm.ErrRecordNotFound {
  1723. return nil
  1724. }
  1725. return
  1726. }
  1727. tmp_id := make(map[int64]int64)
  1728. for _, v := range advice_info {
  1729. tmp_id[v.PrescriptionId] = v.PrescriptionId
  1730. }
  1731. for k, _ := range tmp_id {
  1732. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", k).Updates(map[string]interface{}{
  1733. "is_medicine": 1,
  1734. }).Error
  1735. if err != nil {
  1736. return
  1737. }
  1738. }
  1739. return
  1740. }
  1741. // 改变处方状态(退药
  1742. func ChangeHisPrescriptionT(tmp []int64) (err error) {
  1743. var advice_info []*models.HisDoctorAdviceInfo
  1744. err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", tmp).Find(&advice_info).Error
  1745. if err != nil {
  1746. if err == gorm.ErrRecordNotFound {
  1747. return nil
  1748. }
  1749. return
  1750. }
  1751. tmp_id := make(map[int64]int64)
  1752. for _, v := range advice_info {
  1753. tmp_id[v.PrescriptionId] = v.PrescriptionId
  1754. }
  1755. for k, _ := range tmp_id {
  1756. var total int
  1757. //查询退药的
  1758. XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", k).Count(&total)
  1759. if total == 0 {
  1760. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", k).Updates(map[string]interface{}{
  1761. "is_medicine": 0,
  1762. "mtime": time.Now().Unix(),
  1763. }).Error
  1764. if err != nil {
  1765. return
  1766. }
  1767. }
  1768. }
  1769. return
  1770. }
  1771. func ChangeHisPrescriptionTwo(tmp []int64) (err error) {
  1772. var advice_info []*models.HisPrescriptionProject
  1773. err = XTReadDB().Model(&models.HisPrescriptionProject{}).Where("id in (?) and status = 1", tmp).Find(&advice_info).Error
  1774. if err != nil {
  1775. if err == gorm.ErrRecordNotFound {
  1776. return nil
  1777. }
  1778. return
  1779. }
  1780. tmp_id := make(map[int64]int64)
  1781. for _, v := range advice_info {
  1782. tmp_id[v.PrescriptionId] = v.PrescriptionId
  1783. }
  1784. for k, _ := range tmp_id {
  1785. var total int
  1786. //查询退药的
  1787. XTReadDB().Model(&models.HisPrescriptionProject{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", k).Count(&total)
  1788. if total == 0 {
  1789. err = XTWriteDB().Model(&models.HisPrescriptionProject{}).Where("id = ?", k).Updates(map[string]interface{}{
  1790. "is_medicine": 0,
  1791. }).Error
  1792. if err != nil {
  1793. return
  1794. }
  1795. }
  1796. }
  1797. return
  1798. }
  1799. // 改变处方状态
  1800. func ChangeHisPrescriptionid(id string) (err error) {
  1801. var advice models.HisDoctorAdviceInfo
  1802. err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and status = 1", id).Find(&advice).Error
  1803. if err != nil {
  1804. if err == gorm.ErrRecordNotFound {
  1805. return nil
  1806. }
  1807. return
  1808. }
  1809. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", advice.PrescriptionId).Updates(map[string]interface{}{
  1810. "is_medicine": 1,
  1811. "mtime": time.Now().Unix(),
  1812. }).Error
  1813. if err != nil {
  1814. return
  1815. }
  1816. return
  1817. }
  1818. // 获取药品规格
  1819. func ReplacementDrugs(orgid int64, special bool) (list []*models.ReplacementDrugs, err error) {
  1820. var g errgroup.Group
  1821. var lib []*models.BaseDrugLib
  1822. var lock sync.Mutex
  1823. if special {
  1824. 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
  1825. } else {
  1826. err = readDb.Model(&models.BaseDrugLib{}).Where("org_id = ? and drug_status not like '%停用%' AND status = 1 ", orgid).Find(&lib).Error
  1827. }
  1828. if err != nil {
  1829. return
  1830. }
  1831. for _, v := range lib {
  1832. v := v
  1833. g.Go(func() error {
  1834. var tmp models.Manufacturer
  1835. err := readDb.Model(&models.Manufacturer{}).Where("id = ?", v.Manufacturer).Find(&tmp).Error
  1836. if err != nil {
  1837. return err
  1838. }
  1839. lock.Lock()
  1840. if orgid == 10206 {
  1841. list = append(list, &models.ReplacementDrugs{
  1842. Id: v.ID,
  1843. Name: v.DrugName + " " + v.DrugSpec + " " + v.Number,
  1844. })
  1845. lock.Unlock()
  1846. } else {
  1847. list = append(list, &models.ReplacementDrugs{
  1848. Id: v.ID,
  1849. Name: v.DrugName + " " + v.Dose + v.DoseUnit + "*" + config.ToString(v.MinNumber) + v.MinUnit + "/" + v.MaxUnit + " " + tmp.ManufacturerName,
  1850. })
  1851. lock.Unlock()
  1852. }
  1853. return err
  1854. })
  1855. }
  1856. if errs := g.Wait(); errs != nil {
  1857. err = errs
  1858. return
  1859. }
  1860. return
  1861. }
  1862. // 获取药品规格,不考虑特殊病
  1863. func ReplacementDrugsT(orgid int64) (list []*models.ReplacementDrugs, err error) {
  1864. var lib []*models.BaseDrugLib
  1865. err = readDb.Model(&models.BaseDrugLib{}).Where("org_id = ? AND status = 1 and drug_status not like '%停用%' ", orgid).Find(&lib).Error
  1866. if err != nil {
  1867. return
  1868. }
  1869. for _, v := range lib {
  1870. if orgid == 10206 {
  1871. list = append(list, &models.ReplacementDrugs{
  1872. Id: v.ID,
  1873. Name: v.DrugName + " " + v.DrugSpec + " " + v.Number,
  1874. })
  1875. } else {
  1876. var tmp models.Manufacturer
  1877. err = readDb.Model(&models.Manufacturer{}).Where("id = ?", v.Manufacturer).Find(&tmp).Error
  1878. if err != nil {
  1879. return
  1880. }
  1881. list = append(list, &models.ReplacementDrugs{
  1882. Id: v.ID,
  1883. Name: v.DrugName + " " + v.Dose + v.DoseUnit + "*" + config.ToString(v.MinNumber) + v.MinUnit + "/" + v.PrescribingNumberUnit + " " + tmp.ManufacturerName,
  1884. })
  1885. }
  1886. }
  1887. return
  1888. }
  1889. // 根据药品id获取药品名字
  1890. func IdToDrugName(id int64) (name string) {
  1891. var lib models.BaseDrugLib
  1892. readDb.Model(&models.BaseDrugLib{}).Where("id = ?", id).Find(&lib)
  1893. return lib.DrugName
  1894. }
  1895. // 获取项目和耗材和套餐
  1896. func ProjectConsumables3(orgid int64) (list []*models.DropDownList, err error) {
  1897. var project []*models.XtHisProject
  1898. project, err = GetHisProject(orgid)
  1899. if err != nil {
  1900. return
  1901. }
  1902. for _, v := range project {
  1903. list = append(list, &models.DropDownList{
  1904. Id: "p" + config.ToString(v.ID),
  1905. Name: v.ProjectName,
  1906. })
  1907. }
  1908. var good []*models.GoodInfo
  1909. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? and status = 1", orgid).Find(&good).Error
  1910. if err != nil {
  1911. return
  1912. }
  1913. for _, v := range good {
  1914. list = append(list, &models.DropDownList{
  1915. Id: "i" + config.ToString(v.ID),
  1916. Name: v.GoodName,
  1917. })
  1918. }
  1919. var team []*models.XtHisProjectTeam
  1920. team, err = GetAllProjectTeam(orgid)
  1921. if err != nil {
  1922. return
  1923. }
  1924. for _, v := range team {
  1925. list = append(list, &models.DropDownList{
  1926. Id: "h" + config.ToString(v.ID),
  1927. Name: v.ProjectTeam,
  1928. })
  1929. }
  1930. return
  1931. }
  1932. // 获取项目和耗材
  1933. func ProjectConsumables2(orgid int64, special bool) (list []*models.DropDownList, err error) {
  1934. var project []*models.XtHisProject
  1935. if special {
  1936. err = XTReadDB().Model(&project).Where("user_org_id = ? and medical_status != 1 and status = 1 and disease_directory = 1", orgid).Find(&project).Error
  1937. } else {
  1938. err = XTReadDB().Model(&project).Where("user_org_id = ? and medical_status != 1 and status = 1", orgid).Find(&project).Error
  1939. }
  1940. if err != nil {
  1941. return
  1942. }
  1943. for _, v := range project {
  1944. list = append(list, &models.DropDownList{
  1945. Id: "p" + config.ToString(v.ID),
  1946. Name: v.ProjectName,
  1947. })
  1948. }
  1949. var good []*models.GoodInfo
  1950. if special {
  1951. 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
  1952. } else {
  1953. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? and good_status not like '%停用%' and status = 1", orgid).Find(&good).Error
  1954. }
  1955. if err != nil {
  1956. return
  1957. }
  1958. for _, v := range good {
  1959. list = append(list, &models.DropDownList{
  1960. Id: "i" + config.ToString(v.ID),
  1961. Name: v.GoodName + " " + v.SpecificationName + " " + v.GoodNumber,
  1962. })
  1963. }
  1964. return
  1965. }
  1966. // 获取项目和耗材不考虑特殊病
  1967. func ProjectConsumables2T(orgid int64) (list []*models.DropDownList, err error) {
  1968. var project []*models.XtHisProject
  1969. err = XTReadDB().Model(&project).Where("user_org_id = ? and status = 1 and medical_status != 1", orgid).Find(&project).Error
  1970. if err != nil {
  1971. return
  1972. }
  1973. for _, v := range project {
  1974. list = append(list, &models.DropDownList{
  1975. Id: "p" + config.ToString(v.ID),
  1976. Name: v.ProjectName,
  1977. })
  1978. }
  1979. var good []*models.GoodInfo
  1980. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? and status = 1 and good_status != 1", orgid).Find(&good).Error
  1981. if err != nil {
  1982. return
  1983. }
  1984. for _, v := range good {
  1985. list = append(list, &models.DropDownList{
  1986. Id: "i" + config.ToString(v.ID),
  1987. Name: v.GoodName + " " + v.SpecificationName + " " + v.GoodNumber,
  1988. })
  1989. }
  1990. return
  1991. }
  1992. // 查询药品
  1993. func FindPatientDrug(orgid, patient_id, drugid, mode int64) bool {
  1994. var total int
  1995. XTReadDB().Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and status = 1 and "+
  1996. "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 ("+
  1997. "select id from his_prescription_template where status= 1 and user_org_id = ? and mode = ?))", orgid, patient_id, drugid, orgid, orgid, mode).Count(&total)
  1998. if total > 0 {
  1999. return true
  2000. }
  2001. return false
  2002. }
  2003. // 查询项目
  2004. func FindPatientXiang(orgid, patient_id, drugid, mode int64) bool {
  2005. var total int
  2006. XTReadDB().Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and status = 1 and type = 2 and "+
  2007. "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 ("+
  2008. "select id from his_prescription_template where status= 1 and user_org_id = ? and mode = ?))", orgid, patient_id, drugid, orgid, orgid, mode).Count(&total)
  2009. if total > 0 {
  2010. return true
  2011. }
  2012. return false
  2013. }
  2014. // 查询耗材
  2015. func FindPatientXiang2(orgid, patient_id, drugid, mode int64) bool {
  2016. var total int
  2017. XTReadDB().Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and status = 1 and type = 3 and "+
  2018. "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 ("+
  2019. "select id from his_prescription_template where status= 1 and user_org_id = ? and mode = ?))", orgid, patient_id, drugid, orgid, orgid, mode).Count(&total)
  2020. if total > 0 {
  2021. return true
  2022. }
  2023. return false
  2024. }
  2025. // types处方类型,1.药品 2.项目,model透析模式
  2026. func GetHisInfoTempalteId(model, orgid, types int64) (tp []int64) {
  2027. type tmpid struct {
  2028. Id int64
  2029. }
  2030. var tmp []*tmpid
  2031. XTReadDB().Raw("select id from his_prescription_info_template where status = 1 and user_org_id = ? and type = ? and p_template_id in ("+
  2032. "select id from his_prescription_template where status = 1 and user_org_id = ? and mode = ?)", orgid, types, orgid, model).Scan(&tmp)
  2033. for _, v := range tmp {
  2034. tp = append(tp, v.Id)
  2035. }
  2036. return
  2037. }
  2038. // 替换药品处方模板
  2039. func ReplaceDrugPrescriptionTemplate(orgid, patient_id, drugid int64, ids []int64, s models.HisPrescriptionAdviceTemplate, tx *gorm.DB) (err error) {
  2040. err = tx.Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and "+
  2041. "status = 1 and prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
  2042. "drug_id": s.DrugId,
  2043. "advice_name": s.AdviceName,
  2044. "single_dose": s.SingleDose,
  2045. "single_dose_unit": s.SingleDoseUnit,
  2046. "delivery_way": s.DeliveryWay,
  2047. "execution_frequency": s.ExecutionFrequency,
  2048. "day": s.Day,
  2049. "prescribing_number": s.PrescribingNumber,
  2050. "prescribing_number_unit": s.PrescribingNumberUnit,
  2051. "price": s.Price,
  2052. "remark": s.Remark,
  2053. "updated_time": time.Now().Unix(),
  2054. }).Error
  2055. return err
  2056. }
  2057. // 替换项目模板
  2058. func ReplaceProjectPrescriptionTemplate(orgid, patient_id, drugid int64, ids []int64, s models.HisPrescriptionProjectTemplate, tx *gorm.DB) (err error) {
  2059. err = tx.Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and "+
  2060. "status = 1 and prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
  2061. "project_id": s.ProjectId,
  2062. "type": s.Type,
  2063. "frequency_type": s.FrequencyType,
  2064. "day_count": s.DayCount,
  2065. "week_day": s.WeekDay,
  2066. "price": s.Price,
  2067. "count": s.Count,
  2068. "single_dose": s.SingleDose,
  2069. "delivery_way": s.DeliveryWay,
  2070. "execution_frequency": s.ExecutionFrequency,
  2071. "remark": s.Remark,
  2072. "day": s.Day,
  2073. "unit": s.Unit,
  2074. "mtime": time.Now().Unix(),
  2075. }).Error
  2076. return err
  2077. }
  2078. // 删除项目模板
  2079. func DeleteProjectTemplate(orgid, patient_id, drugid int64, ids []int64, tx *gorm.DB) (err error) {
  2080. err = tx.Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and "+
  2081. "prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
  2082. "status": 0,
  2083. "mtime": time.Now().Unix(),
  2084. }).Error
  2085. return err
  2086. }
  2087. // 删除处方模板
  2088. func DeletePrescriptionTemplate(orgid, patient_id, drugid int64, ids []int64, tx *gorm.DB) (err error) {
  2089. err = tx.Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and "+
  2090. "prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
  2091. "status": 0,
  2092. "updated_time": time.Now().Unix(),
  2093. }).Error
  2094. return err
  2095. }
  2096. // 根据透析模式和患者id获取该患者的处方内容
  2097. func PTemplateInformation(orgid, mode_id, patient_id int64) (tmp interface{}, err error) {
  2098. //获取处方id
  2099. var tt []*models.HisPrescriptionTemplate
  2100. 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
  2101. if len(tt) > 1 {
  2102. err = fmt.Errorf("数据有误")
  2103. return
  2104. }
  2105. x := make(map[int64]string, 0) //项目
  2106. h := make(map[int64]string, 0) //耗材
  2107. for _, v := range tt {
  2108. var prescriptions []*models.HisPrescriptionInfoTemplateL
  2109. prescriptions, err = GetHisPrescriptionTemplateL(v.ID, orgid)
  2110. if err != nil {
  2111. return
  2112. }
  2113. for i := 0; i < len(prescriptions); i++ {
  2114. for j := 0; j < len(prescriptions[i].HisPrescriptionProjectTemplate); j++ {
  2115. if prescriptions[i].HisPrescriptionProjectTemplate[j].Type == 2 {
  2116. //项目
  2117. if v, ok := x[prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.StatisticalClassification]; ok {
  2118. prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.Translate = v
  2119. } else {
  2120. prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.Translate, err = TranslateZu(prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.StatisticalClassification, orgid, "统计分类")
  2121. if err != nil {
  2122. return
  2123. }
  2124. x[prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.StatisticalClassification] = prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.Translate
  2125. }
  2126. }
  2127. if prescriptions[i].HisPrescriptionProjectTemplate[j].Type == 3 {
  2128. //耗材
  2129. if v, ok := h[prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.GoodKind]; ok {
  2130. prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.Translate = v
  2131. } else {
  2132. prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.Translate = "耗材"
  2133. h[prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.GoodKind] = prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.Translate
  2134. }
  2135. }
  2136. }
  2137. }
  2138. tmp = prescriptions
  2139. }
  2140. return
  2141. }
  2142. func DeleteOne(types, id int64) (err error) {
  2143. if types == 1 {
  2144. //药品
  2145. err = XTWriteDB().Model(&models.HisPrescriptionAdviceTemplate{}).Where("id = ?", id).Updates(map[string]interface{}{
  2146. "status": 0,
  2147. "updated_time": time.Now().Unix(),
  2148. }).Error
  2149. if err != nil {
  2150. return
  2151. }
  2152. } else if types == 2 {
  2153. //项目
  2154. err = XTWriteDB().Model(&models.HisPrescriptionProjectTemplateL{}).Where("id = ?", id).Updates(map[string]interface{}{
  2155. "status": 0,
  2156. "mtime": time.Now().Unix(),
  2157. }).Error
  2158. if err != nil {
  2159. return
  2160. }
  2161. } else {
  2162. err = fmt.Errorf("参数错误")
  2163. return
  2164. }
  2165. return
  2166. }
  2167. // 收尾工作
  2168. func Scavenger(orgid int64) (err error) {
  2169. //开事务
  2170. tx := XTWriteDB().Begin()
  2171. defer func() {
  2172. if err != nil {
  2173. utils.ErrorLog("事务失败,原因为: %v", err)
  2174. tx.Rollback()
  2175. } else {
  2176. tx.Commit()
  2177. }
  2178. }()
  2179. //查询该机构的所有模板
  2180. var tmp1 []*models.HisPrescriptionTemplate
  2181. var tmp2 []*models.HisPrescriptionInfoTemplate
  2182. deltmp1 := make([]int64, 0) //清除的数据
  2183. deltmp2 := make([]int64, 0) //清除的数据
  2184. err = tx.Model(&models.HisPrescriptionTemplate{}).Where("user_org_id = ? and status = 1", orgid).Find(&tmp1).Error
  2185. if err != nil {
  2186. return
  2187. }
  2188. for _, v := range tmp1 {
  2189. var total1 int
  2190. tx.Model(&models.HisPrescriptionInfoTemplate{}).Where("user_org_id = ? and p_template_id = ? and status = 1", orgid, v.ID).Count(&total1)
  2191. if total1 == 0 {
  2192. deltmp1 = append(deltmp1, v.ID)
  2193. }
  2194. }
  2195. err = tx.Model(&models.HisPrescriptionInfoTemplate{}).Where("user_org_id = ? and status = 1", orgid).Find(&tmp2).Error
  2196. if err != nil {
  2197. return
  2198. }
  2199. for _, v := range tmp2 {
  2200. if v.Type == 1 {
  2201. var total2 int
  2202. tx.Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and prescription_id = ? and status = 1", orgid, v.ID).Count(&total2)
  2203. if total2 == 0 {
  2204. deltmp2 = append(deltmp2, v.ID)
  2205. }
  2206. }
  2207. if v.Type == 2 {
  2208. var total3 int
  2209. tx.Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and prescription_id = ? and status = 1", orgid, v.ID).Count(&total3)
  2210. if total3 == 0 {
  2211. deltmp2 = append(deltmp2, v.ID)
  2212. }
  2213. }
  2214. }
  2215. //清除1
  2216. err = tx.Model(&models.HisPrescriptionTemplate{}).Where("id in (?)", deltmp1).Updates(map[string]interface{}{
  2217. "status": 0,
  2218. "mtime": time.Now().Unix(),
  2219. }).Error
  2220. if err != nil {
  2221. return
  2222. }
  2223. //清除1
  2224. err = tx.Model(&models.HisPrescriptionInfoTemplate{}).Where("id in (?)", deltmp2).Updates(map[string]interface{}{
  2225. "status": 0,
  2226. "mtime": time.Now().Unix(),
  2227. }).Error
  2228. if err != nil {
  2229. return
  2230. }
  2231. return
  2232. }
  2233. // 分区和收费(患者发药
  2234. func PartitionAndLayout(stime, etime, orgid, shift, partition int64, flist []*models.TmpPatient) (tmp []*models.TmpPatient, err error) {
  2235. //获取排班
  2236. var sch []*models.XtScheduleTwo
  2237. ma := make(map[int64]int64)
  2238. s := "user_org_id = ? and status = 1 and schedule_date >= ? and schedule_date <= ?"
  2239. if shift != 0 {
  2240. s = s + " and schedule_type = " + config.ToString(shift)
  2241. }
  2242. if partition != 0 {
  2243. s = s + " and partition_id = " + config.ToString(partition)
  2244. }
  2245. if shift == 0 && partition == 0 {
  2246. return flist, nil
  2247. }
  2248. err = XTReadDB().Model(&models.XtScheduleTwo{}).Where(s, orgid, stime, etime).Find(&sch).Error
  2249. if err != nil {
  2250. return
  2251. }
  2252. for _, v := range sch {
  2253. ma[v.PatientId] = v.PatientId
  2254. }
  2255. for _, v := range flist {
  2256. if _, ok := ma[v.PatientID]; ok {
  2257. tmp = append(tmp, v)
  2258. }
  2259. }
  2260. return
  2261. }
  2262. // 分区和收费(药品发药
  2263. func PartitionAndLayoutDrug(deliveryway string, stime, etime, orgid, shift, partition int64, flist []*models.PatientInformation) (tmp []*models.PatientInformation, err error) {
  2264. //获取排班
  2265. var sch []*models.XtScheduleTwo
  2266. ma := make(map[int64]int64)
  2267. s := "user_org_id = ? and status = 1 and schedule_date >= ? and schedule_date <= ?"
  2268. if shift != 0 {
  2269. s = s + " and schedule_type = " + config.ToString(shift)
  2270. }
  2271. if partition != 0 {
  2272. s = s + " and partition_id = " + config.ToString(partition)
  2273. }
  2274. if shift == 0 && partition == 0 {
  2275. for _, v := range flist {
  2276. ma[v.PatientId] = v.PatientId
  2277. }
  2278. for _, v := range flist {
  2279. if _, ok := ma[v.PatientId]; ok && (v.Usage == deliveryway || deliveryway == "全部") {
  2280. tmp = append(tmp, v)
  2281. }
  2282. }
  2283. return tmp, nil
  2284. }
  2285. err = XTWriteDB().Model(&models.XtScheduleTwo{}).Where(s, orgid, stime, etime).Find(&sch).Error
  2286. if err != nil {
  2287. return
  2288. }
  2289. for _, v := range sch {
  2290. ma[v.PatientId] = v.PatientId
  2291. }
  2292. for _, v := range flist {
  2293. if _, ok := ma[v.PatientId]; ok && (v.Usage == deliveryway || deliveryway == "全部") {
  2294. tmp = append(tmp, v)
  2295. }
  2296. }
  2297. return
  2298. }
  2299. // 给药途径
  2300. func Administration(deliveryway string, orgid int64, flist []*models.ListOfDrugs) (tmp []*models.ListOfDrugs, err error) {
  2301. //查询药品
  2302. var pp []*models.PharmacyBaseDrug
  2303. s := "org_id = ? and status = 1"
  2304. if deliveryway == "全部" {
  2305. tmp = flist
  2306. return
  2307. } else {
  2308. s = s + " and delivery_way = '" + deliveryway + "'"
  2309. }
  2310. err = XTReadDB().Model(&models.PharmacyBaseDrug{}).Where(s, orgid).Find(&pp).Error
  2311. if err != nil {
  2312. return
  2313. }
  2314. ma := make(map[int64]int64)
  2315. for _, v := range pp {
  2316. ma[v.ID] = v.ID
  2317. }
  2318. for _, v := range flist {
  2319. if _, ok := ma[v.ID]; ok {
  2320. tmp = append(tmp, v)
  2321. }
  2322. }
  2323. return
  2324. }
  2325. // 计算总量
  2326. func CalculateTheTotalAmount(tmp []*models.PatientInformation, drug_id int64) (total string, err error) {
  2327. //获取药品信息
  2328. var t models.SpBaseDrug
  2329. err = XTReadDB().Model(&models.SpBaseDrug{}).Where("id = ?", drug_id).Find(&t).Error
  2330. if err != nil {
  2331. return
  2332. }
  2333. terr, maxunit, minunit := int64(0), int64(0), int64(0) //terr异常的单位数量,maxunit包装单位数量,minunit拆零单位数量
  2334. minmunber := t.MinNumber //拆零数量
  2335. terrUnit := ""
  2336. for _, v := range tmp {
  2337. switch v.Unit {
  2338. case t.MaxUnit:
  2339. maxunit = maxunit + v.Quantity
  2340. case t.MinUnit:
  2341. minunit = minunit + v.Quantity
  2342. default:
  2343. terr = terr + v.Quantity
  2344. terrUnit = v.Unit
  2345. }
  2346. }
  2347. if minunit >= minmunber {
  2348. if minmunber == 0 {
  2349. err = fmt.Errorf("拆零数量不能为零")
  2350. return
  2351. }
  2352. maxunit = maxunit + minunit/minmunber
  2353. minunit = minunit % minmunber
  2354. }
  2355. if terr > 0 {
  2356. total = total + config.ToString(terr) + terrUnit
  2357. }
  2358. if maxunit > 0 {
  2359. total = total + config.ToString(maxunit) + t.MaxUnit
  2360. }
  2361. if minunit > 0 {
  2362. total = total + config.ToString(minunit) + t.MinUnit
  2363. }
  2364. return
  2365. }
  2366. func GetAllValidDeviceZones02(orgID int64) ([]*models.DeviceZone, error) {
  2367. var zones []*models.DeviceZone
  2368. err := readDb.Model(&models.DeviceZone{}).Where("org_id = ? and status = 1", orgID).Find(&zones).Error
  2369. if err != nil {
  2370. return nil, err
  2371. }
  2372. return zones, nil
  2373. }
  2374. func FindeHisAdviceDocAdvice(orgid int64, patient_id int64, stime int64, etime int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  2375. db := XTReadDB().Model(&advice).Where("status= 1 and is_medicine = 0")
  2376. if orgid > 0 {
  2377. db = db.Where("user_org_id = ?", orgid)
  2378. }
  2379. if patient_id > 0 {
  2380. db = db.Where("patient_id = ?", patient_id)
  2381. }
  2382. if stime > 0 {
  2383. db = db.Where("advice_date >=?", stime)
  2384. }
  2385. if etime > 0 {
  2386. db = db.Where("advice_date<=?", etime)
  2387. }
  2388. err = db.Find(&advice).Error
  2389. return advice, err
  2390. }
  2391. func FindeHisProjectDocAdvice(orgid int64, patient_id int64, stime int64, etime int64) (advice []*models.HisPrescriptionProject, err error) {
  2392. db := XTReadDB().Model(&advice).Where("status= 1 and is_medicine = 0")
  2393. if orgid > 0 {
  2394. db = db.Where("user_org_id = ?", orgid)
  2395. }
  2396. if patient_id > 0 {
  2397. db = db.Where("patient_id = ?", patient_id)
  2398. }
  2399. if stime > 0 {
  2400. db = db.Where("recrod_date >=?", stime)
  2401. }
  2402. if etime > 0 {
  2403. db = db.Where("recrod_date<=?", etime)
  2404. }
  2405. err = db.Find(&advice).Error
  2406. return advice, err
  2407. }
  2408. func FindeHisAdviceDocAdviceOne(orgid int64, patient_id int64, drug_id int64, advice_date int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  2409. db := XTReadDB().Model(&advice).Where("status= 1 and is_medicine = 0")
  2410. if orgid > 0 {
  2411. db = db.Where("user_org_id = ?", orgid)
  2412. }
  2413. if patient_id > 0 {
  2414. db = db.Where("patient_id = ?", patient_id)
  2415. }
  2416. if drug_id > 0 {
  2417. db = db.Where("drug_id =?", drug_id)
  2418. }
  2419. if advice_date > 0 {
  2420. db = db.Where("advice_date = ?", advice_date)
  2421. }
  2422. err = db.Find(&advice).Error
  2423. return advice, err
  2424. }
  2425. func GetFlow(drug_id int64, system_time int64, org_id int64, patient_id int64) (flow []*models.DrugFlow, err error) {
  2426. 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
  2427. return flow, err
  2428. }
  2429. func UpdateAdviceFlow(id int64) error {
  2430. err := XTWriteDB().Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and status = 1 and is_medicine =0", id).Update(map[string]interface{}{"is_medicine": 1}).Error
  2431. return err
  2432. }
  2433. func UpdateHisPrescriptionInfo(patient_id int64, advice_date int64, user_org_id int64) error {
  2434. 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
  2435. 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
  2436. return err
  2437. }
  2438. func UpdateHisPrescriptionInfoOne(patient_id int64, advice_date int64, user_org_id int64) error {
  2439. 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
  2440. return err
  2441. }
  2442. func GetHisPatientAdviceList(user_org_id int64, patient_id int64, start_time int64, end_time int64) (list []*models.HisDoctorAdvice, err error) {
  2443. 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
  2444. return list, err
  2445. }
  2446. func GetGoodInventoryList(keyword string, org_id int64) (good []*models.GoodInfoEight, err error) {
  2447. keyword = "%" + keyword + "%"
  2448. db := XTReadDB().Where("status=1")
  2449. if len(keyword) > 0 {
  2450. db = db.Where("good_name like ? or specification_name like ?", keyword, keyword)
  2451. }
  2452. if org_id > 0 {
  2453. db = db.Where("org_id = ?", org_id)
  2454. }
  2455. err = db.Order("id desc").Find(&good).Error
  2456. return good, err
  2457. }
  2458. func GetGoodInventoryByGoodId(id int64) (info []*models.XtWarehouseInfoEight, err error) {
  2459. err = XTReadDB().Where("good_id = ? and status=1 and is_check=1", id).Order("id desc").Find(&info).Error
  2460. return info, err
  2461. }
  2462. func GetInventoryWarehouseInfo(id int64) (models.XtWarehouseInfoEight, error) {
  2463. infoEight := models.XtWarehouseInfoEight{}
  2464. err := XTReadDB().Where("id = ? and status=1", id).Find(&id).Error
  2465. return infoEight, err
  2466. }
  2467. func UpdateWarehouseInfoById(last_stock_count int64, id int64) (models.WarehousingInfo, error) {
  2468. info := models.WarehousingInfo{}
  2469. err := XTWriteDB().Model(&info).Where("id = ? and status =1", id).Updates(map[string]interface{}{"stock_count": last_stock_count}).Error
  2470. return info, err
  2471. }
  2472. func ChangeHisDrugCode(id int64, drug_code string, user_org_id int64) (models.HisDoctorAdviceInfo, error) {
  2473. adviceInfo := models.HisDoctorAdviceInfo{}
  2474. err := XTWriteDB().Model(&adviceInfo).Where("id = ? and user_org_id = ? and status =1", id, user_org_id).Updates(map[string]interface{}{"drug_code": drug_code}).Error
  2475. fmt.Println("", err)
  2476. return adviceInfo, err
  2477. }
  2478. func ChangeHisProjectCode(id int64, drug_code string, user_org_id int64) (models.HisPrescriptionProject, error) {
  2479. adviceInfo := models.HisPrescriptionProject{}
  2480. err := XTWriteDB().Model(&adviceInfo).Where("id = ? and user_org_id = ? and status =1", id, user_org_id).Updates(map[string]interface{}{"drug_code": drug_code}).Error
  2481. return adviceInfo, err
  2482. }
  2483. func ChangeAdivceDrugCode(id int64, drug_code string, user_org_id int64) (models.DoctorAdvice, error) {
  2484. adviceInfo := models.DoctorAdvice{}
  2485. err := XTWriteDB().Model(&adviceInfo).Where("id = ? and user_org_id = ? and status =1", id, user_org_id).Updates(map[string]interface{}{"drug_code": drug_code}).Error
  2486. return adviceInfo, err
  2487. }
  2488. func ChangeAdivceZeroFlag(id int64, zero_flag int64, user_org_id int64) (models.HisDoctorAdviceInfo, error) {
  2489. adviceInfo := models.HisDoctorAdviceInfo{}
  2490. err := XTWriteDB().Model(&adviceInfo).Where("id = ? and user_org_id = ? and status =1", id, user_org_id).Updates(map[string]interface{}{"zero_flag": zero_flag}).Error
  2491. return adviceInfo, err
  2492. }
  2493. func DispensingMedicineTwo(orgid, patient_id, advice_date, drug_id, creater int64) (err error) {
  2494. //开事务
  2495. tx := XTWriteDB().Begin()
  2496. defer func() {
  2497. if err != nil {
  2498. utils.ErrorLog("事务失败,原因为: %v", err)
  2499. tx.Rollback()
  2500. } else {
  2501. tx.Commit()
  2502. }
  2503. }()
  2504. //查询当前患者未发药的列表
  2505. adviceList, err := GetHisPatientNoMedicalOne(orgid, patient_id, drug_id, advice_date)
  2506. //发药
  2507. if len(adviceList) > 0 {
  2508. for _, item := range adviceList {
  2509. pharmacyConfig := IsPharmacyConfig(orgid)
  2510. if pharmacyConfig == true {
  2511. //扣减库存
  2512. err = HisFaDrugsDelivery(orgid, creater, item)
  2513. if err != nil {
  2514. err = fmt.Errorf("!:%v", err)
  2515. return
  2516. }
  2517. }
  2518. //修改发药状态
  2519. err = UpdateIsMedicalHisAdvice(item.ID)
  2520. if err != nil {
  2521. err = fmt.Errorf("!:%v", err)
  2522. return
  2523. }
  2524. err = UpdateHisPrescription(item.PrescriptionId)
  2525. if err != nil {
  2526. err = fmt.Errorf("!:%v", err)
  2527. return
  2528. }
  2529. //查询默认仓库
  2530. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  2531. //查询默认仓库剩余多少库存
  2532. var sum_count int64
  2533. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, item.DrugId)
  2534. for _, its := range stockInfo {
  2535. baseDrug, _ := GetBaseDrugMedical(its.DrugId)
  2536. if its.MaxUnit == baseDrug.MaxUnit {
  2537. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  2538. }
  2539. sum_count += its.StockMaxNumber + its.StockMinNumber
  2540. }
  2541. UpdateBaseDrugSumTwo(item.DrugId, sum_count, orgid)
  2542. //扣减库存
  2543. UpdateDrugStockCount(item.DrugId, item.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
  2544. over, _ := FindOverCount(item.DrugId, item.UserOrgId, storeHouseConfig.DrugStorehouseOut)
  2545. UpdateActOut(over.ID, over.SumInCount, over.FlushCount, over.SumCancelCount)
  2546. }
  2547. }
  2548. //生成明细记录
  2549. tmp_ph := models.Pharmary{
  2550. UserOrgId: orgid,
  2551. PatientId: patient_id,
  2552. Ctime: time.Now().Unix(),
  2553. Mtime: time.Now().Unix(),
  2554. Status: 1,
  2555. RecordDate: time.Now().Unix(),
  2556. }
  2557. err = tx.Create(&tmp_ph).Error
  2558. return
  2559. }
  2560. func GetDrugTocalCountByDrugId(drug_id int64, advice_date int64, user_org_id int64) (advice []*models.NBHisDoctorAdviceInfo, err error) {
  2561. err = XTReadDB().Where("drug_id = ? and advice_date>=? and user_org_id = ? and status=1 and drug_code!=''", drug_id, advice_date, user_org_id).Find(&advice).Error
  2562. return advice, err
  2563. }
  2564. func GetNewBaseDrugById(id int64) (models.NBXtBaseDrug, error) {
  2565. drug := models.NBXtBaseDrug{}
  2566. err := XTReadDB().Where("id = ?", id).Find(&drug).Error
  2567. return drug, err
  2568. }
  2569. func GetNewPatientMedication(orgid, patient_id, stime, etime, is_medicine int64) (pp []*models.PharmacyContent, err error) {
  2570. InitDrugidIsNil(orgid, stime, etime)
  2571. var tmp []*models.HisDoctorAdviceInfoL
  2572. if orgid == 10375 {
  2573. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
  2574. "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
  2575. if err != nil {
  2576. return pp, err
  2577. }
  2578. for _, v := range tmp {
  2579. pp = append(pp, &models.PharmacyContent{
  2580. ID: v.ID,
  2581. Name: v.AdviceName,
  2582. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  2583. Usage: v.DeliveryWay,
  2584. Frequency: v.ExecutionFrequency,
  2585. Days: config.ToString(v.Day) + "天",
  2586. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  2587. Doctor: GetAdminUserName(v.AdviceDoctor, orgid), //开立医生
  2588. DataSources: "his处方",
  2589. Remarks: v.Remark, //备注
  2590. DrugId: v.DrugId,
  2591. Price: v.Price,
  2592. DrugCode: v.DrugCode,
  2593. ZeroFlag: config.ToString(v.ZeroFlag),
  2594. })
  2595. }
  2596. var tmp_advice []*models.XtDoctorAdviceL
  2597. if is_medicine == 0 {
  2598. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  2599. "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
  2600. if err != nil {
  2601. return pp, err
  2602. }
  2603. } else {
  2604. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  2605. "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
  2606. if err != nil {
  2607. return pp, err
  2608. }
  2609. }
  2610. for _, v := range tmp_advice {
  2611. pp = append(pp, &models.PharmacyContent{
  2612. ID: v.ID,
  2613. Name: v.AdviceName,
  2614. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  2615. Usage: v.DeliveryWay,
  2616. Frequency: v.ExecutionFrequency,
  2617. Days: "-",
  2618. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  2619. Doctor: GetAdminUserName(v.AdviceDoctor, orgid),
  2620. DataSources: "临时医嘱",
  2621. Remarks: v.Remark,
  2622. DrugId: v.DrugId,
  2623. DrugCode: v.DrugCode,
  2624. })
  2625. }
  2626. return
  2627. }
  2628. if orgid != 10375 {
  2629. if is_medicine == 0 {
  2630. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
  2631. "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
  2632. if err != nil {
  2633. return pp, err
  2634. }
  2635. } else {
  2636. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
  2637. "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
  2638. if err != nil {
  2639. return pp, err
  2640. }
  2641. }
  2642. for _, v := range tmp {
  2643. pp = append(pp, &models.PharmacyContent{
  2644. ID: v.ID,
  2645. Name: v.AdviceName,
  2646. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  2647. Usage: v.DeliveryWay,
  2648. Frequency: v.ExecutionFrequency,
  2649. Days: config.ToString(v.Day) + "天",
  2650. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  2651. Doctor: GetAdminUserName(v.AdviceDoctor, orgid), //开立医生
  2652. DataSources: "his处方",
  2653. Remarks: v.Remark, //备注
  2654. DrugId: v.DrugId,
  2655. ExecutionState: v.ExecutionState,
  2656. ExecutionStaff: v.ExecutionStaff,
  2657. ExecutionTime: v.ExecutionTime,
  2658. Price: v.Price,
  2659. DrugCode: v.DrugCode,
  2660. ZeroFlag: config.ToString(v.ZeroFlag),
  2661. })
  2662. }
  2663. var tmp_advice []*models.XtDoctorAdviceL
  2664. if is_medicine == 0 {
  2665. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  2666. "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
  2667. if err != nil {
  2668. return pp, err
  2669. }
  2670. } else {
  2671. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  2672. "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
  2673. if err != nil {
  2674. return pp, err
  2675. }
  2676. }
  2677. for _, v := range tmp_advice {
  2678. pp = append(pp, &models.PharmacyContent{
  2679. ID: v.ID,
  2680. Name: v.AdviceName,
  2681. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  2682. Usage: v.DeliveryWay,
  2683. Frequency: v.ExecutionFrequency,
  2684. Days: "-",
  2685. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  2686. Doctor: GetAdminUserName(v.AdviceDoctor, orgid),
  2687. DataSources: "临时医嘱",
  2688. Remarks: v.Remark,
  2689. DrugId: v.DrugId,
  2690. ExecutionState: v.ExecutionState,
  2691. ExecutionStaff: v.ExecutionStaff,
  2692. ExecutionTime: v.ExecutionTime,
  2693. DrugCode: v.DrugCode,
  2694. //Price: v.Price,
  2695. })
  2696. }
  2697. return
  2698. }
  2699. return
  2700. }
  2701. func GetWaitPharmacyHISAdvicePatient(zone_id int64, sch_type int64, record_date int64, org_id int64, keyword string, is_medicine int) (patient []*models.PharmacyPatients, err error) {
  2702. fmt.Println(zone_id)
  2703. db := readDb.Model(&models.PharmacyPatients{})
  2704. if zone_id > 0 {
  2705. db = db.Joins("join xt_schedule on xt_schedule.patient_id = xt_patients.id and xt_schedule.schedule_date = ? and xt_schedule.user_org_id = ? and xt_schedule.status = 1 and xt_schedule.partition_id = ?", record_date, org_id, zone_id)
  2706. }
  2707. if sch_type > 0 {
  2708. db = db.Joins("join xt_schedule on xt_schedule.patient_id = xt_patients.id and xt_schedule.schedule_date = ? and xt_schedule.user_org_id = ? and xt_schedule.status = 1 and xt_schedule.schedule_type = ?", record_date, org_id, sch_type)
  2709. }
  2710. db = db.Joins("join his_doctor_advice_info on xt_patients.id = his_doctor_advice_info.patient_id and his_doctor_advice_info.is_medicine = ? and his_doctor_advice_info.advice_date = ? and his_doctor_advice_info.user_org_id = ? and his_doctor_advice_info.status = 1", is_medicine, record_date, org_id)
  2711. if len(keyword) > 0 {
  2712. keyword = "%" + keyword + "%"
  2713. db = db.Where("xt_patients.name like ?", keyword)
  2714. }
  2715. err = db.Where("xt_patients.user_org_id = ?", org_id).Group("xt_patients.id").Find(&patient).Error
  2716. return
  2717. }
  2718. func GetWaitPharmacyXTPatient(zone_id int64, sch_type int64, record_date int64, org_id int64, keyword string, is_medicine int) (patient []*models.PharmacyPatients, err error) {
  2719. db := readDb.Model(&models.PharmacyPatients{})
  2720. if zone_id > 0 {
  2721. db = db.Joins("join xt_schedule on xt_schedule.patient_id = xt_patients.id and xt_schedule.schedule_date = ? and xt_schedule.user_org_id = ? and xt_schedule.status = 1 and xt_schedule.schedule_type = ?", record_date, org_id, sch_type)
  2722. }
  2723. if sch_type > 0 {
  2724. db = db.Joins("join xt_schedule on xt_schedule.patient_id = xt_patients.id and xt_schedule.schedule_date = ? and xt_schedule.user_org_id = ? and xt_schedule.status = 1 and xt_schedule.partition_id = ?", record_date, org_id, zone_id)
  2725. }
  2726. db = db.Joins("join xt_doctor_advice on xt_patients.id = xt_doctor_advice.patient_id and xt_doctor_advice.is_medicine = ? and xt_doctor_advice.advice_date = ? and xt_doctor_advice.user_org_id = ? and xt_doctor_advice.status = 1", is_medicine, record_date, org_id)
  2727. if len(keyword) > 0 {
  2728. keyword = "%" + keyword + "%"
  2729. db = db.Where("xt_patients.name like ?", keyword)
  2730. }
  2731. err = db.Where("xt_patients.user_org_id = ?", org_id).Group("xt_patients.id").Find(&patient).Error
  2732. return
  2733. }
  2734. func GetWaitPharmacyHISGoodsPatient(zone_id int64, sch_type int64, record_date int64, org_id int64, keyword string, is_medicine int) (patient []*models.PharmacyPatients, err error) {
  2735. db := readDb.Model(&models.PharmacyPatients{})
  2736. if zone_id > 0 {
  2737. db = db.Joins("join xt_schedule on xt_schedule.patient_id = xt_patients.id and xt_schedule.schedule_date = ? and xt_schedule.user_org_id = ? and xt_schedule.status = 1 and xt_schedule.schedule_type = ?", record_date, org_id, sch_type)
  2738. }
  2739. if sch_type > 0 {
  2740. db = db.Joins("join xt_schedule on xt_schedule.patient_id = xt_patients.id and xt_schedule.schedule_date = ? and xt_schedule.user_org_id = ? and xt_schedule.status = 1 and xt_schedule.partition_id = ?", record_date, org_id, zone_id)
  2741. }
  2742. db = db.Joins("join his_prescription_project on xt_patients.id = his_prescription_project.patient_id and his_prescription_project.record_date = ? and his_prescription_project.user_org_id = ? and his_prescription_project.type = 3 and his_prescription_project.status = 1 and his_prescription_project.is_medicine = ?", record_date, org_id, is_medicine)
  2743. if len(keyword) > 0 {
  2744. keyword = "%" + keyword + "%"
  2745. db = db.Where("xt_patients.name like ?", keyword)
  2746. }
  2747. err = db.Where("xt_patients.user_org_id = ?", org_id).Group("xt_patients.id").Find(&patient).Error
  2748. return
  2749. }