pharmacy_service.go 67KB

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