pharmacy_service.go 73KB

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