pharmacy_service.go 65KB

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