pharmacy_service.go 68KB

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