pharmacy_service.go 69KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  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. // 患者退药按钮点击
  724. func DrugWithdrawal(orgid, patient_id, stime, etime, creater int64) (err error) {
  725. //开事务
  726. tx := XTWriteDB().Begin()
  727. defer func() {
  728. if err != nil {
  729. utils.ErrorLog("事务失败,原因为: %v", err)
  730. tx.Rollback()
  731. } else {
  732. tx.Commit()
  733. }
  734. }()
  735. map_time := make(map[int64]int64)
  736. var hids []*models.TmpID
  737. var xids []*models.TmpID
  738. //var hid,xid []int64//查询已发药的药品
  739. err = tx.Raw("select id,dispensing_time from his_doctor_advice_info where status = 1 and advice_date >= ? and advice_date <= ? "+
  740. "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
  741. if err != nil {
  742. return
  743. }
  744. hid := make([]int64, 0)
  745. for _, v := range hids {
  746. hid = append(hid, v.Id)
  747. map_time[v.DispensingTime] = v.DispensingTime
  748. }
  749. //修改状态
  750. errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id in (?)", hid).Updates(map[string]interface{}{
  751. "is_medicine": 0,
  752. "people": creater,
  753. "dispensing_time": 0,
  754. "updated_time": time.Now().Unix(),
  755. }).Error
  756. if errs != nil {
  757. return errs
  758. }
  759. err1 := ChangeHisPrescriptionT(hid)
  760. if err1 != nil {
  761. return err1
  762. }
  763. var advice_info []*models.HisDoctorAdviceInfo
  764. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", hid).Find(&advice_info).Error
  765. if err != nil {
  766. return
  767. }
  768. //针对阜阳经沃
  769. if orgid != 10480 {
  770. for _, v := range advice_info {
  771. //扣减库存
  772. if !IsPharmacyDelivery(v.DrugId, orgid) {
  773. continue
  774. }
  775. err = DrugAutoAddCancelInfo(v, creater)
  776. if err != nil {
  777. err = fmt.Errorf("!:%v", err)
  778. return
  779. }
  780. //drug, _ := FindBaseDrugLibRecordSeven(orgid, v.DrugId)
  781. //查询默认仓库
  782. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  783. //查询默认仓库剩余多少库存
  784. var sum_count int64
  785. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  786. for _, its := range stockInfo {
  787. baseDrug, _ := GetBaseDrugMedical(its.DrugId)
  788. if its.MaxUnit == baseDrug.MaxUnit {
  789. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  790. }
  791. sum_count += its.StockMaxNumber + its.StockMinNumber
  792. }
  793. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  794. //扣减库存
  795. UpdateDrugStockCount(v.DrugId, v.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
  796. }
  797. }
  798. 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 "+
  799. "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
  800. if err != nil {
  801. return
  802. }
  803. xid := make([]int64, 0)
  804. for _, v := range xids {
  805. xid = append(xid, v.Id)
  806. map_time[v.DispensingTime] = v.DispensingTime
  807. }
  808. //修改状态
  809. errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id in (?)", xid).Updates(map[string]interface{}{
  810. "is_medicine": 0,
  811. "people": creater,
  812. "dispensing_time": 0,
  813. "updated_time": time.Now().Unix(),
  814. }).Error
  815. if errs1 != nil {
  816. return errs1
  817. }
  818. var advice []*models.HisDoctorAdviceInfo
  819. err = tx.Raw("select * from xt_doctor_advice where id in (?) and status = 1", xid).Scan(&advice).Error
  820. if err != nil {
  821. return
  822. }
  823. if orgid != 10480 {
  824. for _, v := range advice {
  825. //扣减库存
  826. if !IsPharmacyDelivery(v.DrugId, orgid) {
  827. continue
  828. }
  829. err = DrugAutoAddCancelInfo(v, creater)
  830. if err != nil {
  831. err = fmt.Errorf("!:%v", err)
  832. return
  833. }
  834. //drug, _ := FindBaseDrugLibRecordSeven(orgid, v.DrugId)
  835. //查询默认仓库
  836. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  837. //查询默认仓库剩余多少库存
  838. var sum_count int64
  839. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  840. for _, its := range stockInfo {
  841. baseDrug, _ := GetBaseDrugMedical(its.DrugId)
  842. if its.MaxUnit == baseDrug.MaxUnit {
  843. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  844. }
  845. sum_count += its.StockMaxNumber + its.StockMinNumber
  846. }
  847. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  848. //扣减库存
  849. UpdateDrugStockCount(v.DrugId, v.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
  850. }
  851. }
  852. //删除掉发药明细
  853. for k, _ := range map_time {
  854. err = tx.Model(&models.Pharmary{}).Where("user_org_id = ? and record_date = ? and patient_id = ? ", orgid, k, patient_id).Updates(map[string]interface{}{
  855. "status": 0,
  856. "mtime": time.Now().Unix(),
  857. }).Error
  858. if err != nil {
  859. return
  860. }
  861. }
  862. return
  863. }
  864. // 获取患者名称
  865. func FindUserName(patient_id int64) (name string) {
  866. var tmp models.GetHisName
  867. XTReadDB().Model(&models.GetHisName{}).Where("id = ?", patient_id).Find(&tmp)
  868. name = tmp.Name
  869. return
  870. }
  871. // 获取药品名称
  872. func FindDrugsName(drug_id int64) (name string) {
  873. var tmp models.Drug
  874. XTReadDB().Model(&models.Drug{}).Where("id = ?", drug_id).Find(&tmp)
  875. name = tmp.DrugName
  876. return
  877. }
  878. // 药品发药按钮点击(
  879. func MedicineDeparture(ids string, creater, orgid int64) (err error) {
  880. //开事务
  881. tx := XTWriteDB().Begin()
  882. defer func() {
  883. if err != nil {
  884. utils.ErrorLog("事务失败,原因为: %v", err)
  885. tx.Rollback()
  886. } else {
  887. tx.Commit()
  888. }
  889. }()
  890. //处理下字符串
  891. t_ids := ""
  892. if ids[len(ids)-1] == 44 {
  893. t_ids = ids[:len(ids)-1]
  894. } else {
  895. t_ids = ids
  896. }
  897. time_now := time.Now().Unix()
  898. tmp_id := strings.Split(t_ids, ",")
  899. var parameter string //测试
  900. patient_id := make(map[int64]int64)
  901. for _, v := range tmp_id {
  902. front := v[:1]
  903. after := v[1:]
  904. if front == "h" {
  905. var advice_info []*models.HisDoctorAdviceInfo
  906. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and status = 1", after).Find(&advice_info).Error
  907. if err != nil {
  908. return
  909. }
  910. for _, v := range advice_info {
  911. if _, ok := patient_id[v.PatientId]; !ok {
  912. patient_id[v.PatientId] = v.PatientId
  913. }
  914. tmp_bool := IsPharmacyConfig(orgid)
  915. if tmp_bool {
  916. if PettyCash(v.DrugId) {
  917. continue
  918. }
  919. kou := FenDrugInventory(v, orgid)
  920. if !kou {
  921. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  922. tmp := models.XtErrs{
  923. OrgId: orgid,
  924. Route: "creater:" + config.ToString(creater),
  925. Parameter: parameter,
  926. Text_err: err.Error(),
  927. }
  928. XTWriteDB().Create(&tmp)
  929. return
  930. } else {
  931. tmps, _ := json.Marshal(advice_info) //
  932. parameter = parameter + string(tmps) //
  933. }
  934. //扣减库存
  935. err = FenStock(orgid, creater, v)
  936. if err != nil {
  937. err = fmt.Errorf("!:%v", err)
  938. return
  939. }
  940. }
  941. }
  942. //修改状态
  943. errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ?", after).Updates(map[string]interface{}{
  944. "is_medicine": 1,
  945. "people": creater,
  946. "dispensing_time": time_now,
  947. "updated_time": time.Now().Unix(),
  948. }).Error
  949. if errs != nil {
  950. return errs
  951. }
  952. err1 := ChangeHisPrescriptionid(after)
  953. if err1 != nil {
  954. return err1
  955. }
  956. } else {
  957. var advice []*models.HisDoctorAdviceInfo
  958. 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
  959. if err != nil {
  960. return
  961. }
  962. for _, v := range advice {
  963. if _, ok := patient_id[v.PatientId]; !ok {
  964. patient_id[v.PatientId] = v.PatientId
  965. }
  966. tmp_bool := IsPharmacyConfig(orgid)
  967. if tmp_bool {
  968. if PettyCash(v.DrugId) {
  969. continue
  970. }
  971. kou := FenDrugInventory(v, orgid)
  972. if !kou {
  973. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  974. tmp := models.XtErrs{
  975. OrgId: orgid,
  976. Route: "creater:" + config.ToString(creater),
  977. Parameter: parameter,
  978. Text_err: err.Error(),
  979. }
  980. XTWriteDB().Create(&tmp)
  981. return
  982. } else {
  983. tmps, _ := json.Marshal(advice) //
  984. parameter = parameter + string(tmps) //
  985. }
  986. //扣减库存
  987. err = FenStock(orgid, creater, v)
  988. if err != nil {
  989. err = fmt.Errorf("!:%v", err)
  990. return
  991. }
  992. }
  993. }
  994. //修改状态
  995. errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id = ?", after).Updates(map[string]interface{}{
  996. "is_medicine": 1,
  997. "people": creater,
  998. "dispensing_time": time_now,
  999. "updated_time": time.Now().Unix(),
  1000. }).Error
  1001. if errs1 != nil {
  1002. return errs1
  1003. }
  1004. }
  1005. }
  1006. for _, v := range patient_id {
  1007. //生成明细记录
  1008. tmp_ph := models.Pharmary{
  1009. UserOrgId: orgid,
  1010. PatientId: v,
  1011. Ctime: time.Now().Unix(),
  1012. Mtime: time.Now().Unix(),
  1013. Status: 1,
  1014. RecordDate: time_now,
  1015. }
  1016. err = tx.Create(&tmp_ph).Error
  1017. if err != nil {
  1018. return err
  1019. }
  1020. }
  1021. return
  1022. }
  1023. // 获取领药人姓名(
  1024. func GetUserAdminName(id, orgID int64) string {
  1025. var tmp models.XTSgjUserAdminRole
  1026. XTReadDB().Model(&models.XTSgjUserAdminRole{}).Where("admin_user_id = ? and org_id = ?", id, orgID).Find(&tmp)
  1027. return tmp.UserName
  1028. }
  1029. // 根据id查询是否已发药,true已发药,false未发药gai
  1030. func GiveTheMedicine(id int64) bool {
  1031. var xue models.PharmacyDoctorAdvice
  1032. XTReadDB().Model(&models.PharmacyDoctorAdvice{}).Where("id = ?", id).Find(&xue)
  1033. if xue.IsMedicine == 1 {
  1034. return true
  1035. }
  1036. return false
  1037. }
  1038. // 查询改组中的药品是否包含已发药的,true已发药,false未发药
  1039. func GiveGroupMedicine(orgid, groupNo int64) bool {
  1040. var total int
  1041. XTReadDB().Model(&models.DoctorAdvice{}).Where("user_org_id = ? and groupno = ? and status = 1 and is_medicine = 1", orgid, groupNo).Count(&total)
  1042. if total > 0 {
  1043. return true
  1044. }
  1045. return false
  1046. }
  1047. // 查询处方中是否包含已发药的
  1048. func GiveChuMedicine(id int64) bool {
  1049. var total int
  1050. XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", id).Count(&total)
  1051. if total > 0 {
  1052. return true
  1053. }
  1054. return false
  1055. }
  1056. // 根据处方id查找该处方中是否存在已发药的药品
  1057. func IsChuIssuedDrugs(prescription_id int64) bool {
  1058. var total int
  1059. XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", prescription_id).Count(&total)
  1060. if total > 0 {
  1061. return true
  1062. }
  1063. return false
  1064. }
  1065. // 判断该药品是否通过药房管理出库,true是,false否
  1066. // id 药品id,org_id 机构id
  1067. func IsPharmacyDelivery(id, org_id int64) bool {
  1068. //判断药品是否通过药房管理
  1069. var total01 int
  1070. XTReadDB().Model(&models.PharmacyBaseDrug{}).Where("id = ? and is_pharmacy = 1", id).Count(&total01)
  1071. //判断该机构是否通过药房管理出库
  1072. var total02 int
  1073. XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and is_open = 1", org_id).Count(&total02)
  1074. if total01 > 0 && total02 > 0 {
  1075. return true
  1076. }
  1077. return false
  1078. }
  1079. // 判断机构是否通过药房管理出库
  1080. func IsPharmacyConfig(orgid int64) (bo bool) {
  1081. var total int
  1082. XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and is_open = 1 and status = 1", orgid).Count(&total)
  1083. if total > 0 {
  1084. return true
  1085. }
  1086. return false
  1087. }
  1088. // 根据患者id和发药时间,获取医生的id和姓名
  1089. func GetDoctorIds(id, recordtime, orgid int64) (doctor_id int64, doctor_name string, err error) {
  1090. var tmp []*models.TmpAdviceDoctor
  1091. //var tmp []int64
  1092. err = XTReadDB().Raw("select distinct advice_doctor from his_doctor_advice_info where "+
  1093. "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) "+
  1094. "union "+
  1095. "select distinct advice_doctor from xt_doctor_advice where "+
  1096. "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) ",
  1097. recordtime, orgid, id, orgid, recordtime, orgid, id, orgid).Scan(&tmp).Error
  1098. if err != nil || len(tmp) == 0 {
  1099. return
  1100. }
  1101. doctor_id = tmp[0].AdviceDoctor
  1102. doctor_name = GetUserAdminName(doctor_id, orgid)
  1103. return
  1104. }
  1105. func MapToJson(param map[string]interface{}) string {
  1106. dataType, _ := json.Marshal(param)
  1107. dataString := string(dataType)
  1108. return dataString
  1109. }
  1110. // 封装扣减库存
  1111. func FenStock(orgid, creater int64, v *models.HisDoctorAdviceInfo) (err error) {
  1112. err = HisDrugsDelivery(orgid, creater, v)
  1113. if err != nil {
  1114. err = fmt.Errorf("!:%v", err)
  1115. return
  1116. }
  1117. //查询默认仓库
  1118. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  1119. //查询默认仓库剩余多少库存
  1120. var sum_count int64
  1121. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  1122. for _, its := range stockInfo {
  1123. baseDrug, _ := GetBaseDrugMedical(its.DrugId)
  1124. if its.MaxUnit == baseDrug.MaxUnit {
  1125. its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
  1126. }
  1127. sum_count += its.StockMaxNumber + its.StockMinNumber
  1128. }
  1129. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  1130. //扣减库存
  1131. UpdateDrugStockCount(v.DrugId, v.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
  1132. over, _ := FindOverCount(v.DrugId, v.UserOrgId, storeHouseConfig.DrugStorehouseOut)
  1133. UpdateActOut(over.ID, over.SumInCount, over.FlushCount, over.SumCancelCount)
  1134. return
  1135. }
  1136. // 封装查询药品库存是否足够
  1137. func FenDrugInventory(item *models.HisDoctorAdviceInfo, orgid int64) bool {
  1138. var total int64
  1139. var prescribing_number_total int64
  1140. houseConfig, _ := GetAllStoreHouseConfig(orgid)
  1141. ////查询该药品是否有库存
  1142. list, _ := GetDrugTotalCountTwenty(item.DrugId, item.UserOrgId, houseConfig.DrugStorehouseOut)
  1143. ////查询改药品信息
  1144. medical, _ := GetBaseDrugMedical(item.DrugId)
  1145. ////判断单位是否相等
  1146. if medical.MaxUnit == item.PrescribingNumberUnit {
  1147. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1148. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1149. //转化为最小单位
  1150. total = list.Count*medical.MinNumber + list.StockMinNumber
  1151. prescribing_number_total = count * medical.MinNumber
  1152. }
  1153. if medical.MinUnit == item.PrescribingNumberUnit {
  1154. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1155. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1156. total = list.Count*medical.MinNumber + list.StockMinNumber
  1157. prescribing_number_total = count
  1158. }
  1159. if prescribing_number_total <= total {
  1160. //可以扣减
  1161. return true
  1162. } else {
  1163. //不可扣减
  1164. return false
  1165. }
  1166. }
  1167. func GetAdviceIds1(ids []string, orgid, execution_staff int64, thetime time.Time) (tmp []string, err error) {
  1168. var advice []*models.DoctorAdvice
  1169. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND status = 1", ids).Find(&advice).Error
  1170. if err != nil {
  1171. return
  1172. }
  1173. for _, v := range advice {
  1174. if !IsPharmacyDelivery(v.DrugId, orgid) {
  1175. tmp = append(tmp, config.ToString(v.ID))
  1176. } else {
  1177. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? ", v.ID).Updates(map[string]interface{}{
  1178. "execution_staff": execution_staff,
  1179. "execution_time": thetime.Unix(),
  1180. "updated_time": time.Now().Unix(),
  1181. }).Error
  1182. if err != nil {
  1183. return
  1184. }
  1185. }
  1186. }
  1187. return
  1188. }
  1189. func GetAdviceIds2(ids []string, orgid, execution_staff int64, thetime time.Time) (tmp []string, err error) {
  1190. var advice []*models.HisDoctorAdviceInfoL
  1191. err = readDb.Model(&models.HisDoctorAdviceInfoL{}).Where("id IN (?) AND status = 1", ids).Find(&advice).Error
  1192. if err != nil {
  1193. return
  1194. }
  1195. for _, v := range advice {
  1196. if !IsPharmacyDelivery(v.DrugId, orgid) {
  1197. tmp = append(tmp, config.ToString(v.ID))
  1198. } else {
  1199. err = XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? ", v.ID).Updates(map[string]interface{}{
  1200. "execution_staff": execution_staff,
  1201. "execution_time": thetime.Unix(),
  1202. "updated_time": time.Now().Unix(),
  1203. }).Error
  1204. if err != nil {
  1205. return
  1206. }
  1207. }
  1208. }
  1209. return
  1210. }
  1211. func GetAdviceId1(id, orgid, execution_staff int64, thetime time.Time) (bo bool, err error) {
  1212. var advice models.DoctorAdvice
  1213. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND status = 1", id).Find(&advice).Error
  1214. if err != nil {
  1215. return false, err
  1216. }
  1217. if IsPharmacyDelivery(advice.DrugId, orgid) {
  1218. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? ", id).Updates(map[string]interface{}{
  1219. "execution_staff": execution_staff,
  1220. "execution_time": thetime.Unix(),
  1221. "updated_time": time.Now().Unix(),
  1222. }).Error
  1223. if err != nil {
  1224. return false, err
  1225. }
  1226. return true, err
  1227. }
  1228. return false, err
  1229. }
  1230. func GetAdviceId2(id, orgid, execution_staff int64, thetime time.Time) (bo bool, err error) {
  1231. var advice models.HisDoctorAdviceInfoL
  1232. err = readDb.Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? AND status = 1", id).Find(&advice).Error
  1233. if err != nil {
  1234. return false, err
  1235. }
  1236. if IsPharmacyDelivery(advice.DrugId, orgid) {
  1237. err = XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? ", id).Updates(map[string]interface{}{
  1238. "execution_staff": execution_staff,
  1239. "execution_time": thetime.Unix(),
  1240. "updated_time": time.Now().Unix(),
  1241. }).Error
  1242. if err != nil {
  1243. return false, err
  1244. }
  1245. return true, err
  1246. }
  1247. return false, err
  1248. }
  1249. // 判断药品是否零用
  1250. func PettyCash(id int64) bool {
  1251. drug := models.XtBaseDrug{}
  1252. XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug)
  1253. if drug.IsUse == 1 {
  1254. return true
  1255. }
  1256. return false
  1257. }
  1258. // 初始化
  1259. func InitDrugidIsNil(orgid, stime, etime int64) {
  1260. var advice []*models.DoctorAdvice
  1261. 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)
  1262. for _, v := range advice {
  1263. 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)
  1264. }
  1265. }
  1266. // 改变处方状态(发药
  1267. func ChangeHisPrescription(tmp []int64) (err error) {
  1268. var advice_info []*models.HisDoctorAdviceInfo
  1269. err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", tmp).Find(&advice_info).Error
  1270. if err != nil {
  1271. if err == gorm.ErrRecordNotFound {
  1272. return nil
  1273. }
  1274. return
  1275. }
  1276. tmp_id := make(map[int64]int64)
  1277. for _, v := range advice_info {
  1278. tmp_id[v.PrescriptionId] = v.PrescriptionId
  1279. }
  1280. for k, _ := range tmp_id {
  1281. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", k).Updates(map[string]interface{}{
  1282. "is_medicine": 1,
  1283. "mtime": time.Now().Unix(),
  1284. }).Error
  1285. if err != nil {
  1286. return
  1287. }
  1288. }
  1289. return
  1290. }
  1291. // 改变处方状态(退药
  1292. func ChangeHisPrescriptionT(tmp []int64) (err error) {
  1293. var advice_info []*models.HisDoctorAdviceInfo
  1294. err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", tmp).Find(&advice_info).Error
  1295. if err != nil {
  1296. if err == gorm.ErrRecordNotFound {
  1297. return nil
  1298. }
  1299. return
  1300. }
  1301. tmp_id := make(map[int64]int64)
  1302. for _, v := range advice_info {
  1303. tmp_id[v.PrescriptionId] = v.PrescriptionId
  1304. }
  1305. for k, _ := range tmp_id {
  1306. var total int
  1307. //查询退药的
  1308. XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", k).Count(&total)
  1309. if total == 0 {
  1310. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", k).Updates(map[string]interface{}{
  1311. "is_medicine": 0,
  1312. "mtime": time.Now().Unix(),
  1313. }).Error
  1314. if err != nil {
  1315. return
  1316. }
  1317. }
  1318. }
  1319. return
  1320. }
  1321. // 改变处方状态
  1322. func ChangeHisPrescriptionid(id string) (err error) {
  1323. var advice models.HisDoctorAdviceInfo
  1324. err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and status = 1", id).Find(&advice).Error
  1325. if err != nil {
  1326. if err == gorm.ErrRecordNotFound {
  1327. return nil
  1328. }
  1329. return
  1330. }
  1331. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", advice.PrescriptionId).Updates(map[string]interface{}{
  1332. "is_medicine": 1,
  1333. "mtime": time.Now().Unix(),
  1334. }).Error
  1335. if err != nil {
  1336. return
  1337. }
  1338. return
  1339. }
  1340. // 获取药品规格
  1341. func ReplacementDrugs(orgid int64, special bool) (list []*models.ReplacementDrugs, err error) {
  1342. var g errgroup.Group
  1343. var lib []*models.BaseDrugLib
  1344. var lock sync.Mutex
  1345. if special {
  1346. 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
  1347. } else {
  1348. err = readDb.Model(&models.BaseDrugLib{}).Where("org_id = ? and drug_status not like '%停用%' AND status = 1 ", orgid).Find(&lib).Error
  1349. }
  1350. if err != nil {
  1351. return
  1352. }
  1353. for _, v := range lib {
  1354. v := v
  1355. g.Go(func() error {
  1356. var tmp models.Manufacturer
  1357. err := readDb.Model(&models.Manufacturer{}).Where("id = ?", v.Manufacturer).Find(&tmp).Error
  1358. if err != nil {
  1359. return err
  1360. }
  1361. lock.Lock()
  1362. if orgid == 10206 {
  1363. list = append(list, &models.ReplacementDrugs{
  1364. Id: v.ID,
  1365. Name: v.DrugName + " " + v.DrugSpec + " " + v.Number,
  1366. })
  1367. lock.Unlock()
  1368. } else {
  1369. list = append(list, &models.ReplacementDrugs{
  1370. Id: v.ID,
  1371. Name: v.DrugName + " " + v.Dose + v.DoseUnit + "*" + config.ToString(v.MinNumber) + v.MinUnit + "/" + v.MaxUnit + " " + tmp.ManufacturerName,
  1372. })
  1373. lock.Unlock()
  1374. }
  1375. return err
  1376. })
  1377. }
  1378. if errs := g.Wait(); errs != nil {
  1379. err = errs
  1380. return
  1381. }
  1382. return
  1383. }
  1384. // 获取药品规格,不考虑特殊病
  1385. func ReplacementDrugsT(orgid int64) (list []*models.ReplacementDrugs, err error) {
  1386. var lib []*models.BaseDrugLib
  1387. err = readDb.Model(&models.BaseDrugLib{}).Where("org_id = ? AND status = 1 and drug_status not like '%停用%' ", orgid).Find(&lib).Error
  1388. if err != nil {
  1389. return
  1390. }
  1391. for _, v := range lib {
  1392. var tmp models.Manufacturer
  1393. err = readDb.Model(&models.Manufacturer{}).Where("id = ?", v.Manufacturer).Find(&tmp).Error
  1394. if err != nil {
  1395. return
  1396. }
  1397. if orgid == 10206 {
  1398. list = append(list, &models.ReplacementDrugs{
  1399. Id: v.ID,
  1400. Name: v.DrugName + " " + v.DrugSpec + " " + v.Number,
  1401. })
  1402. } else {
  1403. list = append(list, &models.ReplacementDrugs{
  1404. Id: v.ID,
  1405. Name: v.DrugName + " " + v.Dose + v.DoseUnit + "*" + config.ToString(v.MinNumber) + v.MinUnit + "/" + v.PrescribingNumberUnit + " " + tmp.ManufacturerName,
  1406. })
  1407. }
  1408. }
  1409. return
  1410. }
  1411. // 根据药品id获取药品名字
  1412. func IdToDrugName(id int64) (name string) {
  1413. var lib models.BaseDrugLib
  1414. readDb.Model(&models.BaseDrugLib{}).Where("id = ?", id).Find(&lib)
  1415. return lib.DrugName
  1416. }
  1417. // 获取项目和耗材和套餐
  1418. func ProjectConsumables3(orgid int64) (list []*models.DropDownList, err error) {
  1419. var project []*models.XtHisProject
  1420. project, err = GetHisProject(orgid)
  1421. if err != nil {
  1422. return
  1423. }
  1424. for _, v := range project {
  1425. list = append(list, &models.DropDownList{
  1426. Id: "p" + config.ToString(v.ID),
  1427. Name: v.ProjectName,
  1428. })
  1429. }
  1430. var good []*models.GoodInfo
  1431. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? and status = 1", orgid).Find(&good).Error
  1432. if err != nil {
  1433. return
  1434. }
  1435. for _, v := range good {
  1436. list = append(list, &models.DropDownList{
  1437. Id: "i" + config.ToString(v.ID),
  1438. Name: v.GoodName,
  1439. })
  1440. }
  1441. var team []*models.XtHisProjectTeam
  1442. team, err = GetAllProjectTeam(orgid)
  1443. if err != nil {
  1444. return
  1445. }
  1446. for _, v := range team {
  1447. list = append(list, &models.DropDownList{
  1448. Id: "h" + config.ToString(v.ID),
  1449. Name: v.ProjectTeam,
  1450. })
  1451. }
  1452. return
  1453. }
  1454. // 获取项目和耗材
  1455. func ProjectConsumables2(orgid int64, special bool) (list []*models.DropDownList, err error) {
  1456. var project []*models.XtHisProject
  1457. if special {
  1458. err = XTReadDB().Model(&project).Where("user_org_id = ? and medical_status != 1 and status = 1 and disease_directory = 1", orgid).Find(&project).Error
  1459. } else {
  1460. err = XTReadDB().Model(&project).Where("user_org_id = ? and medical_status != 1 and status = 1", orgid).Find(&project).Error
  1461. }
  1462. if err != nil {
  1463. return
  1464. }
  1465. for _, v := range project {
  1466. list = append(list, &models.DropDownList{
  1467. Id: "p" + config.ToString(v.ID),
  1468. Name: v.ProjectName,
  1469. })
  1470. }
  1471. var good []*models.GoodInfo
  1472. if special {
  1473. 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
  1474. } else {
  1475. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? and good_status not like '%停用%' and status = 1", orgid).Find(&good).Error
  1476. }
  1477. if err != nil {
  1478. return
  1479. }
  1480. for _, v := range good {
  1481. list = append(list, &models.DropDownList{
  1482. Id: "i" + config.ToString(v.ID),
  1483. Name: v.GoodName + " " + v.SpecificationName + " " + v.GoodNumber,
  1484. })
  1485. }
  1486. return
  1487. }
  1488. // 获取项目和耗材不考虑特殊病
  1489. func ProjectConsumables2T(orgid int64) (list []*models.DropDownList, err error) {
  1490. var project []*models.XtHisProject
  1491. err = XTReadDB().Model(&project).Where("user_org_id = ? and status = 1 and medical_status != 1", orgid).Find(&project).Error
  1492. if err != nil {
  1493. return
  1494. }
  1495. for _, v := range project {
  1496. list = append(list, &models.DropDownList{
  1497. Id: "p" + config.ToString(v.ID),
  1498. Name: v.ProjectName,
  1499. })
  1500. }
  1501. var good []*models.GoodInfo
  1502. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? and status = 1 and good_status != 1", orgid).Find(&good).Error
  1503. if err != nil {
  1504. return
  1505. }
  1506. for _, v := range good {
  1507. list = append(list, &models.DropDownList{
  1508. Id: "i" + config.ToString(v.ID),
  1509. Name: v.GoodName + " " + v.SpecificationName + " " + v.GoodNumber,
  1510. })
  1511. }
  1512. return
  1513. }
  1514. // 查询药品
  1515. func FindPatientDrug(orgid, patient_id, drugid, mode int64) bool {
  1516. var total int
  1517. XTReadDB().Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and status = 1 and "+
  1518. "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 ("+
  1519. "select id from his_prescription_template where status= 1 and user_org_id = ? and mode = ?))", orgid, patient_id, drugid, orgid, orgid, mode).Count(&total)
  1520. if total > 0 {
  1521. return true
  1522. }
  1523. return false
  1524. }
  1525. // 查询项目
  1526. func FindPatientXiang(orgid, patient_id, drugid, mode int64) bool {
  1527. var total int
  1528. XTReadDB().Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and status = 1 and type = 2 and "+
  1529. "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 ("+
  1530. "select id from his_prescription_template where status= 1 and user_org_id = ? and mode = ?))", orgid, patient_id, drugid, orgid, orgid, mode).Count(&total)
  1531. if total > 0 {
  1532. return true
  1533. }
  1534. return false
  1535. }
  1536. // 查询耗材
  1537. func FindPatientXiang2(orgid, patient_id, drugid, mode int64) bool {
  1538. var total int
  1539. XTReadDB().Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and status = 1 and type = 3 and "+
  1540. "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 ("+
  1541. "select id from his_prescription_template where status= 1 and user_org_id = ? and mode = ?))", orgid, patient_id, drugid, orgid, orgid, mode).Count(&total)
  1542. if total > 0 {
  1543. return true
  1544. }
  1545. return false
  1546. }
  1547. // types处方类型,1.药品 2.项目,model透析模式
  1548. func GetHisInfoTempalteId(model, orgid, types int64) (tp []int64) {
  1549. type tmpid struct {
  1550. Id int64
  1551. }
  1552. var tmp []*tmpid
  1553. XTReadDB().Raw("select id from his_prescription_info_template where status = 1 and user_org_id = ? and type = ? and p_template_id in ("+
  1554. "select id from his_prescription_template where status = 1 and user_org_id = ? and mode = ?)", orgid, types, orgid, model).Scan(&tmp)
  1555. for _, v := range tmp {
  1556. tp = append(tp, v.Id)
  1557. }
  1558. return
  1559. }
  1560. // 替换药品处方模板
  1561. func ReplaceDrugPrescriptionTemplate(orgid, patient_id, drugid int64, ids []int64, s models.HisPrescriptionAdviceTemplate, tx *gorm.DB) (err error) {
  1562. err = tx.Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and "+
  1563. "status = 1 and prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
  1564. "drug_id": s.DrugId,
  1565. "advice_name": s.AdviceName,
  1566. "single_dose": s.SingleDose,
  1567. "single_dose_unit": s.SingleDoseUnit,
  1568. "delivery_way": s.DeliveryWay,
  1569. "execution_frequency": s.ExecutionFrequency,
  1570. "day": s.Day,
  1571. "prescribing_number": s.PrescribingNumber,
  1572. "prescribing_number_unit": s.PrescribingNumberUnit,
  1573. "price": s.Price,
  1574. "remark": s.Remark,
  1575. "updated_time": time.Now().Unix(),
  1576. }).Error
  1577. return err
  1578. }
  1579. // 替换项目模板
  1580. func ReplaceProjectPrescriptionTemplate(orgid, patient_id, drugid int64, ids []int64, s models.HisPrescriptionProjectTemplate, tx *gorm.DB) (err error) {
  1581. err = tx.Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and "+
  1582. "status = 1 and prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
  1583. "project_id": s.ProjectId,
  1584. "type": s.Type,
  1585. "frequency_type": s.FrequencyType,
  1586. "day_count": s.DayCount,
  1587. "week_day": s.WeekDay,
  1588. "price": s.Price,
  1589. "count": s.Count,
  1590. "single_dose": s.SingleDose,
  1591. "delivery_way": s.DeliveryWay,
  1592. "execution_frequency": s.ExecutionFrequency,
  1593. "remark": s.Remark,
  1594. "day": s.Day,
  1595. "unit": s.Unit,
  1596. "mtime": time.Now().Unix(),
  1597. }).Error
  1598. return err
  1599. }
  1600. // 删除项目模板
  1601. func DeleteProjectTemplate(orgid, patient_id, drugid int64, ids []int64, tx *gorm.DB) (err error) {
  1602. err = tx.Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and "+
  1603. "prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
  1604. "status": 0,
  1605. "mtime": time.Now().Unix(),
  1606. }).Error
  1607. return err
  1608. }
  1609. // 删除处方模板
  1610. func DeletePrescriptionTemplate(orgid, patient_id, drugid int64, ids []int64, tx *gorm.DB) (err error) {
  1611. err = tx.Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and "+
  1612. "prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
  1613. "status": 0,
  1614. "updated_time": time.Now().Unix(),
  1615. }).Error
  1616. return err
  1617. }
  1618. // 根据透析模式和患者id获取该患者的处方内容
  1619. func PTemplateInformation(orgid, mode_id, patient_id int64) (tmp interface{}, err error) {
  1620. //获取处方id
  1621. var tt []*models.HisPrescriptionTemplate
  1622. 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
  1623. if len(tt) > 1 {
  1624. err = fmt.Errorf("数据有误")
  1625. return
  1626. }
  1627. x := make(map[int64]string, 0) //项目
  1628. h := make(map[int64]string, 0) //耗材
  1629. for _, v := range tt {
  1630. var prescriptions []*models.HisPrescriptionInfoTemplateL
  1631. prescriptions, err = GetHisPrescriptionTemplateL(v.ID, orgid)
  1632. if err != nil {
  1633. return
  1634. }
  1635. for i := 0; i < len(prescriptions); i++ {
  1636. for j := 0; j < len(prescriptions[i].HisPrescriptionProjectTemplate); j++ {
  1637. if prescriptions[i].HisPrescriptionProjectTemplate[j].Type == 2 {
  1638. //项目
  1639. if v, ok := x[prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.StatisticalClassification]; ok {
  1640. prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.Translate = v
  1641. } else {
  1642. prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.Translate, err = TranslateZu(prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.StatisticalClassification, orgid, "统计分类")
  1643. if err != nil {
  1644. return
  1645. }
  1646. x[prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.StatisticalClassification] = prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.Translate
  1647. }
  1648. }
  1649. if prescriptions[i].HisPrescriptionProjectTemplate[j].Type == 3 {
  1650. //耗材
  1651. if v, ok := h[prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.GoodKind]; ok {
  1652. prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.Translate = v
  1653. } else {
  1654. prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.Translate = "耗材"
  1655. h[prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.GoodKind] = prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.Translate
  1656. }
  1657. }
  1658. }
  1659. }
  1660. tmp = prescriptions
  1661. }
  1662. return
  1663. }
  1664. func DeleteOne(types, id int64) (err error) {
  1665. if types == 1 {
  1666. //药品
  1667. err = XTWriteDB().Model(&models.HisPrescriptionAdviceTemplate{}).Where("id = ?", id).Updates(map[string]interface{}{
  1668. "status": 0,
  1669. "updated_time": time.Now().Unix(),
  1670. }).Error
  1671. if err != nil {
  1672. return
  1673. }
  1674. } else if types == 2 {
  1675. //项目
  1676. err = XTWriteDB().Model(&models.HisPrescriptionProjectTemplateL{}).Where("id = ?", id).Updates(map[string]interface{}{
  1677. "status": 0,
  1678. "mtime": time.Now().Unix(),
  1679. }).Error
  1680. if err != nil {
  1681. return
  1682. }
  1683. } else {
  1684. err = fmt.Errorf("参数错误")
  1685. return
  1686. }
  1687. return
  1688. }
  1689. // 收尾工作
  1690. func Scavenger(orgid int64) (err error) {
  1691. //开事务
  1692. tx := XTWriteDB().Begin()
  1693. defer func() {
  1694. if err != nil {
  1695. utils.ErrorLog("事务失败,原因为: %v", err)
  1696. tx.Rollback()
  1697. } else {
  1698. tx.Commit()
  1699. }
  1700. }()
  1701. //查询该机构的所有模板
  1702. var tmp1 []*models.HisPrescriptionTemplate
  1703. var tmp2 []*models.HisPrescriptionInfoTemplate
  1704. deltmp1 := make([]int64, 0) //清除的数据
  1705. deltmp2 := make([]int64, 0) //清除的数据
  1706. err = tx.Model(&models.HisPrescriptionTemplate{}).Where("user_org_id = ? and status = 1", orgid).Find(&tmp1).Error
  1707. if err != nil {
  1708. return
  1709. }
  1710. for _, v := range tmp1 {
  1711. var total1 int
  1712. tx.Model(&models.HisPrescriptionInfoTemplate{}).Where("user_org_id = ? and p_template_id = ? and status = 1", orgid, v.ID).Count(&total1)
  1713. if total1 == 0 {
  1714. deltmp1 = append(deltmp1, v.ID)
  1715. }
  1716. }
  1717. err = tx.Model(&models.HisPrescriptionInfoTemplate{}).Where("user_org_id = ? and status = 1", orgid).Find(&tmp2).Error
  1718. if err != nil {
  1719. return
  1720. }
  1721. for _, v := range tmp2 {
  1722. if v.Type == 1 {
  1723. var total2 int
  1724. tx.Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and prescription_id = ? and status = 1", orgid, v.ID).Count(&total2)
  1725. if total2 == 0 {
  1726. deltmp2 = append(deltmp2, v.ID)
  1727. }
  1728. }
  1729. if v.Type == 2 {
  1730. var total3 int
  1731. tx.Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and prescription_id = ? and status = 1", orgid, v.ID).Count(&total3)
  1732. if total3 == 0 {
  1733. deltmp2 = append(deltmp2, v.ID)
  1734. }
  1735. }
  1736. }
  1737. //清除1
  1738. err = tx.Model(&models.HisPrescriptionTemplate{}).Where("id in (?)", deltmp1).Updates(map[string]interface{}{
  1739. "status": 0,
  1740. "mtime": time.Now().Unix(),
  1741. }).Error
  1742. if err != nil {
  1743. return
  1744. }
  1745. //清除1
  1746. err = tx.Model(&models.HisPrescriptionInfoTemplate{}).Where("id in (?)", deltmp2).Updates(map[string]interface{}{
  1747. "status": 0,
  1748. "mtime": time.Now().Unix(),
  1749. }).Error
  1750. if err != nil {
  1751. return
  1752. }
  1753. return
  1754. }
  1755. // 分区和收费(患者发药
  1756. func PartitionAndLayout(stime, etime, orgid, shift, partition int64, flist []*models.TmpPatient) (tmp []*models.TmpPatient, err error) {
  1757. //获取排班
  1758. var sch []*models.XtScheduleTwo
  1759. ma := make(map[int64]int64)
  1760. s := "user_org_id = ? and status = 1 and schedule_date >= ? and schedule_date <= ?"
  1761. if shift != 0 {
  1762. s = s + " and schedule_type = " + config.ToString(shift)
  1763. }
  1764. if partition != 0 {
  1765. s = s + " and partition_id = " + config.ToString(partition)
  1766. }
  1767. if shift == 0 && partition == 0 {
  1768. return flist, nil
  1769. }
  1770. err = XTReadDB().Model(&models.XtScheduleTwo{}).Where(s, orgid, stime, etime).Find(&sch).Error
  1771. if err != nil {
  1772. return
  1773. }
  1774. for _, v := range sch {
  1775. ma[v.PatientId] = v.PatientId
  1776. }
  1777. for _, v := range flist {
  1778. if _, ok := ma[v.PatientID]; ok {
  1779. tmp = append(tmp, v)
  1780. }
  1781. }
  1782. return
  1783. }
  1784. // 分区和收费(药品发药
  1785. func PartitionAndLayoutDrug(deliveryway string, stime, etime, orgid, shift, partition int64, flist []*models.PatientInformation) (tmp []*models.PatientInformation, err error) {
  1786. //获取排班
  1787. var sch []*models.XtScheduleTwo
  1788. ma := make(map[int64]int64)
  1789. s := "user_org_id = ? and status = 1 and schedule_date >= ? and schedule_date <= ?"
  1790. if shift != 0 {
  1791. s = s + " and schedule_type = " + config.ToString(shift)
  1792. }
  1793. if partition != 0 {
  1794. s = s + " and partition_id = " + config.ToString(partition)
  1795. }
  1796. if shift == 0 && partition == 0 {
  1797. for _, v := range flist {
  1798. ma[v.PatientId] = v.PatientId
  1799. }
  1800. for _, v := range flist {
  1801. if _, ok := ma[v.PatientId]; ok && (v.Usage == deliveryway || deliveryway == "全部") {
  1802. tmp = append(tmp, v)
  1803. }
  1804. }
  1805. return tmp, nil
  1806. }
  1807. err = XTWriteDB().Model(&models.XtScheduleTwo{}).Where(s, orgid, stime, etime).Find(&sch).Error
  1808. if err != nil {
  1809. return
  1810. }
  1811. for _, v := range sch {
  1812. ma[v.PatientId] = v.PatientId
  1813. }
  1814. for _, v := range flist {
  1815. if _, ok := ma[v.PatientId]; ok && (v.Usage == deliveryway || deliveryway == "全部") {
  1816. tmp = append(tmp, v)
  1817. }
  1818. }
  1819. return
  1820. }
  1821. // 给药途径
  1822. func Administration(deliveryway string, orgid int64, flist []*models.ListOfDrugs) (tmp []*models.ListOfDrugs, err error) {
  1823. //查询药品
  1824. var pp []*models.PharmacyBaseDrug
  1825. s := "org_id = ? and status = 1"
  1826. if deliveryway == "全部" {
  1827. tmp = flist
  1828. return
  1829. } else {
  1830. s = s + " and delivery_way = '" + deliveryway + "'"
  1831. }
  1832. err = XTReadDB().Model(&models.PharmacyBaseDrug{}).Where(s, orgid).Find(&pp).Error
  1833. if err != nil {
  1834. return
  1835. }
  1836. ma := make(map[int64]int64)
  1837. for _, v := range pp {
  1838. ma[v.ID] = v.ID
  1839. }
  1840. for _, v := range flist {
  1841. if _, ok := ma[v.ID]; ok {
  1842. tmp = append(tmp, v)
  1843. }
  1844. }
  1845. return
  1846. }
  1847. // 计算总量
  1848. func CalculateTheTotalAmount(tmp []*models.PatientInformation, drug_id int64) (total string, err error) {
  1849. //获取药品信息
  1850. var t models.SpBaseDrug
  1851. err = XTReadDB().Model(&models.SpBaseDrug{}).Where("id = ?", drug_id).Find(&t).Error
  1852. if err != nil {
  1853. return
  1854. }
  1855. terr, maxunit, minunit := int64(0), int64(0), int64(0) //terr异常的单位数量,maxunit包装单位数量,minunit拆零单位数量
  1856. minmunber := t.MinNumber //拆零数量
  1857. terrUnit := ""
  1858. for _, v := range tmp {
  1859. fmt.Println("v2332233232322323", v.Quantity)
  1860. switch v.Unit {
  1861. case t.MaxUnit:
  1862. maxunit = maxunit + v.Quantity
  1863. case t.MinUnit:
  1864. minunit = minunit + v.Quantity
  1865. default:
  1866. terr = terr + v.Quantity
  1867. terrUnit = v.Unit
  1868. }
  1869. }
  1870. if minunit >= minmunber {
  1871. if minmunber == 0 {
  1872. err = fmt.Errorf("拆零数量不能为零")
  1873. return
  1874. }
  1875. maxunit = maxunit + minunit/minmunber
  1876. minunit = minunit % minmunber
  1877. }
  1878. if terr > 0 {
  1879. total = total + config.ToString(terr) + terrUnit
  1880. }
  1881. if maxunit > 0 {
  1882. total = total + config.ToString(maxunit) + t.MaxUnit
  1883. }
  1884. if minunit > 0 {
  1885. total = total + config.ToString(minunit) + t.MinUnit
  1886. }
  1887. return
  1888. }
  1889. func GetAllValidDeviceZones02(orgID int64) ([]*models.DeviceZone, error) {
  1890. var zones []*models.DeviceZone
  1891. err := readDb.Model(&models.DeviceZone{}).Where("org_id = ? and status = 1", orgID).Find(&zones).Error
  1892. if err != nil {
  1893. return nil, err
  1894. }
  1895. return zones, nil
  1896. }
  1897. func FindeHisAdviceDocAdvice(orgid int64, patient_id int64, stime int64, etime int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  1898. db := XTReadDB().Model(&advice).Where("status= 1 and is_medicine = 0")
  1899. if orgid > 0 {
  1900. db = db.Where("user_org_id = ?", orgid)
  1901. }
  1902. if patient_id > 0 {
  1903. db = db.Where("patient_id = ?", patient_id)
  1904. }
  1905. if stime > 0 {
  1906. db = db.Where("advice_date >=?", stime)
  1907. }
  1908. if etime > 0 {
  1909. db = db.Where("advice_date<=?", etime)
  1910. }
  1911. err = db.Find(&advice).Error
  1912. return advice, err
  1913. }
  1914. func GetFlow(drug_id int64, system_time int64, org_id int64, patient_id int64) (flow []*models.DrugFlow, err error) {
  1915. 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
  1916. return flow, err
  1917. }
  1918. func UpdateAdviceFlow(id int64) error {
  1919. err := XTWriteDB().Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and status = 1 and is_medicine =0", id).Update(map[string]interface{}{"is_medicine": 1}).Error
  1920. return err
  1921. }