pharmacy_service.go 74KB

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