pharmacy_service.go 80KB

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