pharmacy_service.go 67KB

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