pharmacy_service.go 86KB

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