pharmacy_service.go 45KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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. "math"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. func SaveErrs(org_id int64, input *context.BeegoInput, err error) {
  16. dataBody := make(map[string]interface{}, 0)
  17. json.Unmarshal(input.RequestBody, &dataBody)
  18. route := input.Context.Request.RequestURI
  19. parameter := MapToJson(dataBody)
  20. tmp := models.XtErrs{
  21. org_id,
  22. route,
  23. parameter,
  24. err.Error(),
  25. }
  26. XTWriteDB().Create(&tmp)
  27. }
  28. //
  29. func SavePharmacy() (err error) {
  30. var advice_info []*models.HisDoctorAdviceInfo
  31. err = XTReadDB().Raw("select * from xt_doctor_advice where id > 21780 and id < 21790").Scan(&advice_info).Error
  32. if err != nil {
  33. return
  34. }
  35. return nil
  36. }
  37. //查询药房中某一天的患者人数,is_medicine:0未发,1已发(改)
  38. func GetTodayPharmacy(stime, etime, orgid, is_medicine int64) (num int, err error) {
  39. //orgid = 9675
  40. InitDrugidIsNil(orgid, stime, etime)
  41. var tmp []*models.TmpTTT
  42. if is_medicine == 0 {
  43. err = XTReadDB().Raw("select distinct patient_id from his_doctor_advice_info where "+
  44. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = 0 and "+
  45. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) "+
  46. "union "+
  47. "select distinct patient_id from xt_doctor_advice where "+
  48. "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 "+
  49. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) ",
  50. stime, etime, orgid, orgid, stime, etime, orgid, orgid).Scan(&tmp).Error
  51. if err != nil {
  52. return
  53. }
  54. } else {
  55. err = XTReadDB().Raw("select distinct patient_id from his_doctor_advice_info where "+
  56. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = 1 and "+
  57. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) "+
  58. "union "+
  59. "select distinct patient_id from xt_doctor_advice where "+
  60. "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 "+
  61. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) ",
  62. stime, etime, orgid, orgid, stime, etime, orgid, orgid).Scan(&tmp).Error
  63. if err != nil {
  64. return
  65. }
  66. }
  67. return len(tmp), err
  68. }
  69. //(改)
  70. func GetTodayDrug(stime, etime, orgid, is_medicine int64, keyword string) (list []*models.TmpPatient, err error) {
  71. InitDrugidIsNil(orgid, stime, etime)
  72. //病人
  73. var tmp []*models.TmpTTT
  74. //tmp := make([]string,0)
  75. if is_medicine == 0 {
  76. if keyword != "" {
  77. keyword = "%" + keyword + "%"
  78. err = XTReadDB().Raw("select a.* from(select distinct patient_id,dispensing_time from his_doctor_advice_info where "+
  79. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  80. "patient_id in (select id from xt_patients where user_org_id = ? and name like ? or dialysis_no like ?) and "+
  81. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) "+
  82. "union "+
  83. "select distinct patient_id,dispensing_time from xt_doctor_advice where "+
  84. "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  85. "patient_id in (select id from xt_patients where user_org_id = ? and name like ? or dialysis_no like ?) and "+
  86. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1))a order by dispensing_time desc ",
  87. stime, etime, orgid, is_medicine, orgid, keyword, keyword, orgid, stime, etime, orgid, is_medicine, orgid, keyword, keyword, orgid).Scan(&tmp).Error
  88. } else {
  89. err = XTReadDB().Raw("select a.* from(select distinct patient_id,dispensing_time from his_doctor_advice_info where "+
  90. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  91. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) "+
  92. "union "+
  93. "select distinct patient_id,dispensing_time from xt_doctor_advice where "+
  94. "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  95. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1))a order by dispensing_time desc",
  96. stime, etime, orgid, is_medicine, orgid, stime, etime, orgid, is_medicine, orgid).Scan(&tmp).Error
  97. }
  98. if err != nil {
  99. return
  100. }
  101. } else {
  102. if keyword != "" {
  103. keyword = "%" + keyword + "%"
  104. err = XTReadDB().Raw("select a.* from(select distinct patient_id,dispensing_time from his_doctor_advice_info where "+
  105. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  106. "patient_id in (select id from xt_patients where user_org_id = ? and name like ? or dialysis_no like ?) and "+
  107. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) "+
  108. "union "+
  109. "select distinct patient_id,dispensing_time from xt_doctor_advice where "+
  110. "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  111. "patient_id in (select id from xt_patients where user_org_id = ? and name like ? or dialysis_no like ?) and "+
  112. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1))a order by dispensing_time desc ",
  113. stime, etime, orgid, is_medicine, orgid, keyword, keyword, orgid, stime, etime, orgid, is_medicine, orgid, keyword, keyword, orgid).Scan(&tmp).Error
  114. } else {
  115. err = XTReadDB().Raw("select a.* from(select distinct patient_id,dispensing_time from his_doctor_advice_info where "+
  116. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  117. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) "+
  118. "union "+
  119. "select distinct patient_id,dispensing_time from xt_doctor_advice where "+
  120. "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  121. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1))a order by dispensing_time desc",
  122. stime, etime, orgid, is_medicine, orgid, stime, etime, orgid, is_medicine, orgid).Scan(&tmp).Error
  123. }
  124. if err != nil {
  125. return
  126. }
  127. }
  128. tmp_s := make([]int64, 0)
  129. s_map := make(map[int64]int64)
  130. for _, v := range tmp {
  131. if _, ok := s_map[v.PatientID]; !ok {
  132. s_map[v.PatientID] = v.PatientID
  133. tmp_s = append(tmp_s, v.PatientID)
  134. }
  135. }
  136. list, err = GetManyUsers(tmp_s)
  137. return
  138. }
  139. //查询患者(改)
  140. func GetManyUsers(tmp []int64) (list []*models.TmpPatient, err error) {
  141. for i := 0; i < len(tmp); i++ {
  142. var tt models.Patients
  143. err = XTReadDB().Model(&models.Patients{}).Where("id = ?", tmp[i]).Find(&tt).Error
  144. tmp_list := &models.TmpPatient{
  145. PatientID: tt.ID,
  146. Name: tt.Name,
  147. DialysisNo: tt.DialysisNo,
  148. }
  149. list = append(list, tmp_list)
  150. }
  151. return
  152. }
  153. //查询患者当天时间段中的药,is_medicine:0未发,1已发(改)
  154. func GetPatientMedication(orgid, patient_id, stime, etime, is_medicine int64) (pp []*models.PharmacyContent, err error) {
  155. InitDrugidIsNil(orgid, stime, etime)
  156. var tmp []*models.HisDoctorAdviceInfoL
  157. if is_medicine == 0 {
  158. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
  159. "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
  160. if err != nil {
  161. return pp, err
  162. }
  163. } else {
  164. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
  165. "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
  166. if err != nil {
  167. return pp, err
  168. }
  169. }
  170. for _, v := range tmp {
  171. pp = append(pp, &models.PharmacyContent{
  172. Name: v.AdviceName,
  173. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  174. Usage: v.DeliveryWay,
  175. Frequency: v.ExecutionFrequency,
  176. Days: config.ToString(v.Day) + "天",
  177. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  178. Doctor: GetAdminUserName(v.AdviceDoctor, orgid), //开立医生
  179. DataSources: "his处方",
  180. Remarks: v.Remark, //备注
  181. })
  182. }
  183. var tmp_advice []*models.XtDoctorAdviceL
  184. if is_medicine == 0 {
  185. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  186. "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
  187. if err != nil {
  188. return pp, err
  189. }
  190. } else {
  191. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  192. "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
  193. if err != nil {
  194. return pp, err
  195. }
  196. }
  197. for _, v := range tmp_advice {
  198. pp = append(pp, &models.PharmacyContent{
  199. Name: v.AdviceName,
  200. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  201. Usage: v.DeliveryWay,
  202. Frequency: v.ExecutionFrequency,
  203. Days: "-",
  204. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  205. Doctor: GetAdminUserName(v.AdviceDoctor, orgid),
  206. DataSources: "临时医嘱",
  207. Remarks: v.Remark,
  208. })
  209. }
  210. return
  211. }
  212. //获取创建者姓名(改)
  213. func GetAdminUserName(doctor, orgid int64) string {
  214. var tmp models.VmUserAdminRole
  215. XTReadDB().Model(&models.VmUserAdminRole{}).Where("admin_user_id = ? and org_id = ? and status = 1", doctor, orgid).Find(&tmp)
  216. return tmp.UserName
  217. }
  218. //查询该机构的药房配置,true: 通过药房发药,false:不通过药房发药。如果没有该机构的信息则生成一条数据
  219. func GetOrgIdPharmacyConfig(orgid int64) bool {
  220. var total int
  221. var tmp_pharmacy models.PharmacyConfig
  222. XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and status = 1", orgid).Find(&tmp_pharmacy).Count(&total)
  223. if total > 0 {
  224. //有记录
  225. if tmp_pharmacy.IsOpen == 1 {
  226. return true
  227. } else {
  228. return false
  229. }
  230. } else {
  231. //没有记录生成一条
  232. tmp := models.PharmacyConfig{
  233. UserOrgId: orgid,
  234. IsOpen: 2,
  235. Status: 1,
  236. Ctime: time.Now().Unix(),
  237. Mtime: time.Now().Unix(),
  238. }
  239. XTWriteDB().Create(&tmp)
  240. return false
  241. }
  242. }
  243. //修改该机构的药房配置
  244. func ModifyPharmacyConfig(orgid, isopen int64) (err error) {
  245. if isopen == 1 || isopen == 2 {
  246. err = XTWriteDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{
  247. "mtime": time.Now().Unix(),
  248. "is_open": isopen,
  249. }).Error
  250. } else {
  251. err = fmt.Errorf("类型解析错误")
  252. }
  253. return
  254. }
  255. //查询该药品是否通过药房发药.0否;1是
  256. func DrugAllocation(drug_id int64) (is_pharmacy int64, err error) {
  257. tmp := models.BaseDrugLib{}
  258. err = XTReadDB().Model(&models.BaseDrugLib{}).Where("id = ?", drug_id).Find(&tmp).Error
  259. return tmp.IsPharmacy, err
  260. }
  261. //根据id查询该药是否发药
  262. func MedicineState(id int64) (is_medicine models.HisDoctorAdviceInfoL, err error) {
  263. tmp := models.HisDoctorAdviceInfoL{}
  264. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ?", id).Find(&tmp).Error
  265. return tmp, err
  266. }
  267. //发药明细列表(
  268. func DispensingDetailsList(stime, etime, orgid, page, limit int64, keyword string) (dislist []*models.DispensingList, total int64, err error) {
  269. var fenye []*models.Pharmary //分页用的
  270. offset := (page - 1) * limit
  271. dbone := XTReadDB().Model(&models.Pharmary{}).Where("status = 1 and user_org_id = ? and record_date >= ? and record_date <= ?", orgid, stime, etime)
  272. if keyword != "" {
  273. var tmp_id []*models.TmpID
  274. keyword = "%" + keyword + "%"
  275. XTReadDB().Raw("select id from xt_patients where name like ? and user_org_id = ?", keyword, orgid).Scan(&tmp_id)
  276. tmp_ids := make([]int64, 0)
  277. for _, v := range tmp_id {
  278. tmp_ids = append(tmp_ids, v.Id)
  279. }
  280. dbone = dbone.Where("patient_id in (?)", tmp_ids)
  281. }
  282. //查询出分页用的数据
  283. err = dbone.Count(&total).Offset(offset).Order("mtime desc").Limit(limit).Find(&fenye).Error
  284. if err != nil {
  285. return
  286. }
  287. for _, v := range fenye {
  288. tmp_doctorid, tmp_doctorname, errs := GetDoctorIds(v.PatientId, v.RecordDate, orgid)
  289. if errs != nil {
  290. err = errs
  291. return
  292. }
  293. pat, _ := GetPatientName(v.PatientId)
  294. dislist = append(dislist, &models.DispensingList{
  295. PatientID: v.PatientId,
  296. Name: pat.Name,
  297. DoctorId: tmp_doctorid,
  298. DoctorName: tmp_doctorname,
  299. RecordTime: v.RecordDate,
  300. RecordDate: fmt.Sprintf(time.Unix(v.RecordDate, 0).Format("2006-01-02 15:04:05")),
  301. })
  302. }
  303. return
  304. }
  305. //处方详情//////////////////////
  306. func PrescriptionDetails(patient_id, record_date, orgid int64) (pre []*models.PrescripDetails, err error) {
  307. var tmp []*models.HisDoctorAdviceInfoL
  308. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
  309. "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
  310. if err != nil {
  311. return
  312. }
  313. for _, v := range tmp {
  314. pre = append(pre, &models.PrescripDetails{
  315. Drugname: v.AdviceName,
  316. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  317. Usage: v.DeliveryWay,
  318. Frequency: v.ExecutionFrequency,
  319. Days: config.ToString(v.Day) + "天",
  320. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  321. UnitPrice: config.ToString(v.Price) + "元",
  322. Remarks: v.Remark,
  323. })
  324. }
  325. var tmp_advice []*models.XtDoctorAdviceL
  326. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  327. "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
  328. if err != nil {
  329. return
  330. }
  331. for _, v := range tmp_advice {
  332. pre = append(pre, &models.PrescripDetails{
  333. Drugname: v.AdviceName,
  334. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  335. Usage: v.DeliveryWay,
  336. Frequency: v.ExecutionFrequency,
  337. Days: "-",
  338. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  339. UnitPrice: "-",
  340. Remarks: v.Remark,
  341. })
  342. }
  343. return
  344. }
  345. //查询病人的his id
  346. func GetHisID(prescription_id, orgid int64) (id int64) {
  347. var tmp models.HisPrintPrescription
  348. XTReadDB().Model(&models.HisPrintPrescription{}).Where("id = ?", prescription_id).Find(&tmp)
  349. var fin models.HisPrescriptionInfoTwo
  350. XTReadDB().Model(&models.HisPrescriptionInfoTwo{}).Where("prescription_number = ? and user_org_id = ?", tmp.PrescriptionNumber, orgid).Find(&fin)
  351. return fin.ID
  352. }
  353. //根据药品id获取药品信息
  354. func GetDrugNameTX(id int64, tx *gorm.DB) (tmp models.SpBaseDrug, err error) {
  355. err = tx.Model(&models.SpBaseDrug{}).Where("id = ? and status = 1", id).Find(&tmp).Error
  356. return
  357. }
  358. func GetXtManufacturer(id int64, tx *gorm.DB) (name string, err error) {
  359. var tmp models.Manufacturer
  360. err = tx.Model(&models.Manufacturer{}).Where("id = ? and status = 1", id).Find(&tmp).Error
  361. name = tmp.ManufacturerName
  362. return
  363. }
  364. //查询药品(使用keyword)
  365. func GetManyDrugs(orgid int64, keyword string) (map[int64]models.PharmacyBaseDrug, error) {
  366. var tmp []*models.PharmacyBaseDrug
  367. tt := make(map[int64]models.PharmacyBaseDrug)
  368. err := XTReadDB().Model(&models.PharmacyBaseDrug{}).Where(" org_id = ? ", orgid).Where(" drug_name like ? ", "%"+keyword+"%").Find(&tmp).Error
  369. for _, v := range tmp {
  370. tt[v.ID] = *v
  371. }
  372. return tt, err
  373. }
  374. //查询(
  375. func GetTodayMedicine(stime, etime, orgid, is_medicine int64, keyword string) (finlly []*models.ListOfDrugs, err error) {
  376. InitDrugidIsNil(orgid, stime, etime)
  377. var tmp []*models.TmpLLL
  378. if is_medicine == 0 {
  379. if keyword != "" {
  380. keyword = "%" + keyword + "%"
  381. err = XTReadDB().Raw("select distinct drug_id from his_doctor_advice_info where "+
  382. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  383. "drug_id in (select id from xt_base_drug where org_id = ? and drug_name like ? and is_pharmacy = 1) "+
  384. "union "+
  385. "select distinct drug_id from xt_doctor_advice where "+
  386. "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  387. "drug_id in (select id from xt_base_drug where org_id = ? and drug_name like ? and is_pharmacy = 1)",
  388. stime, etime, orgid, is_medicine, orgid, keyword, stime, etime, orgid, is_medicine, orgid, keyword).Scan(&tmp).Error
  389. } else {
  390. err = XTReadDB().Raw("select distinct drug_id from his_doctor_advice_info where "+
  391. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  392. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) "+
  393. "union "+
  394. "select distinct drug_id from xt_doctor_advice where "+
  395. "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  396. "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) ",
  397. stime, etime, orgid, is_medicine, orgid, stime, etime, orgid, is_medicine, orgid).Scan(&tmp).Error
  398. }
  399. if err != nil {
  400. return
  401. }
  402. } else {
  403. if keyword != "" {
  404. keyword = "%" + keyword + "%"
  405. err = XTReadDB().Raw("select distinct drug_id from his_doctor_advice_info where "+
  406. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  407. "drug_id in (select id from xt_base_drug where org_id = ? and drug_name like ? and is_pharmacy = 1) "+
  408. "union "+
  409. "select distinct drug_id from xt_doctor_advice where "+
  410. "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  411. "drug_id in (select id from xt_base_drug where org_id = ? and drug_name like ? and is_pharmacy = 1)",
  412. stime, etime, orgid, is_medicine, orgid, keyword, stime, etime, orgid, is_medicine, orgid, keyword).Scan(&tmp).Error
  413. } else {
  414. err = XTReadDB().Raw("select distinct drug_id from his_doctor_advice_info where "+
  415. "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  416. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) "+
  417. "union "+
  418. "select distinct drug_id from xt_doctor_advice where "+
  419. "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
  420. "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) ",
  421. stime, etime, orgid, is_medicine, orgid, stime, etime, orgid, is_medicine, orgid).Scan(&tmp).Error
  422. }
  423. if err != nil {
  424. return
  425. }
  426. }
  427. for _, v := range tmp {
  428. name, specifications := FindDrugSpecifications(v.DrugId)
  429. number, errs := GetInventoryQuantity(orgid, v.DrugId)
  430. if errs != nil {
  431. err = errs
  432. return
  433. }
  434. finlly = append(finlly, &models.ListOfDrugs{
  435. ID: v.DrugId,
  436. Name: name,
  437. Specifications: specifications,
  438. Stock: number,
  439. })
  440. }
  441. return
  442. }
  443. //根据药品id获取药品规格(
  444. func FindDrugSpecifications(id int64) (name, specifications string) {
  445. var tmp models.PharmacyBaseDrug
  446. XTReadDB().Model(&models.PharmacyBaseDrug{}).Where("id = ?", id).Find(&tmp)
  447. name = tmp.DrugName
  448. specifications = config.ToString(tmp.Dose) + tmp.DoseUnit + "*" + config.ToString(tmp.MinNumber) + tmp.MinUnit + "/" + config.ToString(tmp.MaxUnit)
  449. return
  450. }
  451. //根据药品id获取当前药品出库仓库的库存数量
  452. func GetInventoryQuantity(orgid, drugid int64) (number string, err error) {
  453. //获取药品拆零数量
  454. var phar models.PharmacyBaseDrug
  455. XTReadDB().Model(&models.PharmacyBaseDrug{}).Where("id = ?", drugid).Find(&phar)
  456. min_number := phar.MinNumber
  457. if min_number == 0 {
  458. err = fmt.Errorf("药品拆零数量不能为零!")
  459. return
  460. }
  461. storeconfig, _ := FindStorehouseConfig(orgid)
  462. var tmp []models.SpDrugWarehouseInfo
  463. XTReadDB().Model(&models.SpDrugWarehouseInfo{}).Where("org_id = ? and drug_id = ? and storehouse_id = ? and status = 1", orgid, drugid, storeconfig.DrugStorehouseOut).Find(&tmp)
  464. max, min := int64(0), int64(0)
  465. for _, v := range tmp {
  466. max += v.StockMaxNumber
  467. min += v.StockMinNumber
  468. }
  469. max += min / min_number
  470. min = min % min_number
  471. if max != 0 {
  472. number = config.ToString(max) + phar.MaxUnit
  473. }
  474. if min != 0 {
  475. number = number + config.ToString(min) + phar.MinUnit
  476. }
  477. if number == "" {
  478. number = "0"
  479. }
  480. //number = config.ToString(max)+phar.MaxUnit+config.ToString(min)+phar.MinUnit
  481. return
  482. }
  483. //获取该药品的病人信息(
  484. func FindMedicationList(orgid, drug_id, stime, etime, is_medicine int64) (pp []*models.PatientInformation, err error) { ///////////////
  485. InitDrugidIsNil(orgid, stime, etime)
  486. var tmp []*models.HisDoctorAdviceInfoL
  487. err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
  488. "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
  489. if err != nil {
  490. return pp, err
  491. }
  492. for _, v := range tmp {
  493. pp = append(pp, &models.PatientInformation{
  494. Id: "h" + config.ToString(v.ID),
  495. Name: FindUserName(v.PatientId),
  496. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  497. Usage: v.DeliveryWay,
  498. Frequency: v.ExecutionFrequency,
  499. Days: config.ToString(v.Day) + "天",
  500. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  501. DataSources: "his处方",
  502. People: GetUserAdminName(v.People, orgid), //领药人!!!!!!!!!!!!!!!!!!!!!!
  503. })
  504. }
  505. var tmp_advice []*models.XtDoctorAdviceL
  506. err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
  507. "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
  508. if err != nil {
  509. return pp, err
  510. }
  511. for _, v := range tmp_advice {
  512. pp = append(pp, &models.PatientInformation{
  513. Id: "x" + config.ToString(v.ID),
  514. Name: FindUserName(v.PatientId),
  515. SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
  516. Usage: v.DeliveryWay,
  517. Frequency: v.ExecutionFrequency,
  518. Days: "",
  519. Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
  520. DataSources: "临时医嘱",
  521. People: GetUserAdminName(v.People, orgid), //领药人
  522. })
  523. }
  524. return
  525. }
  526. //患者发药按钮点击(
  527. func DispensingMedicine(orgid, patient_id, stime, etime, creater int64) (err error) {
  528. //开事务
  529. tx := XTWriteDB().Begin()
  530. defer func() {
  531. if err != nil {
  532. utils.ErrorLog("事务失败,原因为: %v", err)
  533. tx.Rollback()
  534. } else {
  535. tx.Commit()
  536. }
  537. }()
  538. time_now := time.Now().Unix()
  539. var hids []*models.TmpID
  540. var xids []*models.TmpID
  541. err = tx.Raw("select id from his_doctor_advice_info where status = 1 and created_time >= ? and created_time <= ? "+
  542. "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
  543. if err != nil {
  544. return
  545. }
  546. hid := make([]int64, 0)
  547. for _, v := range hids {
  548. hid = append(hid, v.Id)
  549. }
  550. var advice_info []*models.HisDoctorAdviceInfo
  551. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", hid).Find(&advice_info).Error
  552. if err != nil {
  553. return
  554. }
  555. for _, v := range advice_info {
  556. tmp_bool := IsPharmacyConfig(orgid)
  557. if tmp_bool {
  558. if PettyCash(v.DrugId) {
  559. continue
  560. }
  561. kou := FenDrugInventory(v, orgid)
  562. if !kou {
  563. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  564. return
  565. }
  566. //扣减库存
  567. err = FenStock(orgid, creater, v)
  568. if err != nil {
  569. err = fmt.Errorf("!:%v", err)
  570. return
  571. }
  572. }
  573. }
  574. //修改状态
  575. errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id in (?)", hid).Updates(map[string]interface{}{
  576. "is_medicine": 1,
  577. "people": 0,
  578. "dispensing_time": time_now,
  579. "updated_time": time.Now().Unix(),
  580. }).Error
  581. if errs != nil {
  582. return errs
  583. }
  584. err1 := ChangeHisPrescription(hid)
  585. if err1 != nil {
  586. return err1
  587. }
  588. 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 "+
  589. "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
  590. if err != nil {
  591. return
  592. }
  593. xid := make([]int64, 0)
  594. for _, v := range xids {
  595. xid = append(xid, v.Id)
  596. }
  597. var advice []*models.HisDoctorAdviceInfo
  598. err = tx.Raw("select * from xt_doctor_advice where id in (?) and status = 1", xid).Scan(&advice).Error
  599. if err != nil {
  600. return
  601. }
  602. for _, v := range advice {
  603. tmp_bool := IsPharmacyConfig(orgid)
  604. if tmp_bool {
  605. if PettyCash(v.DrugId) {
  606. continue
  607. }
  608. kou := FenDrugInventory(v, orgid)
  609. if !kou {
  610. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  611. return
  612. }
  613. //扣减库存
  614. err = FenStock(orgid, creater, v)
  615. if err != nil {
  616. err = fmt.Errorf("!:%v", err)
  617. return
  618. }
  619. }
  620. }
  621. //修改状态
  622. errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id in (?)", xid).Updates(map[string]interface{}{
  623. "is_medicine": 1,
  624. "people": 0,
  625. "dispensing_time": time_now,
  626. "updated_time": time.Now().Unix(),
  627. }).Error
  628. if errs1 != nil {
  629. return errs1
  630. }
  631. //生成明细记录
  632. tmp_ph := models.Pharmary{
  633. UserOrgId: orgid,
  634. PatientId: patient_id,
  635. Ctime: time.Now().Unix(),
  636. Mtime: time.Now().Unix(),
  637. Status: 1,
  638. RecordDate: time_now,
  639. }
  640. err = tx.Create(&tmp_ph).Error
  641. return
  642. }
  643. //患者退药按钮点击
  644. func DrugWithdrawal(orgid, patient_id, stime, etime, creater int64) (err error) {
  645. //开事务
  646. tx := XTWriteDB().Begin()
  647. defer func() {
  648. if err != nil {
  649. utils.ErrorLog("事务失败,原因为: %v", err)
  650. tx.Rollback()
  651. } else {
  652. tx.Commit()
  653. }
  654. }()
  655. map_time := make(map[int64]int64)
  656. var hids []*models.TmpID
  657. var xids []*models.TmpID
  658. //var hid,xid []int64//查询已发药的药品
  659. err = tx.Raw("select id,dispensing_time from his_doctor_advice_info where status = 1 and created_time >= ? and created_time <= ? "+
  660. "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
  661. if err != nil {
  662. return
  663. }
  664. hid := make([]int64, 0)
  665. for _, v := range hids {
  666. hid = append(hid, v.Id)
  667. map_time[v.DispensingTime] = v.DispensingTime
  668. }
  669. //修改状态
  670. errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id in (?)", hid).Updates(map[string]interface{}{
  671. "is_medicine": 0,
  672. "people": creater,
  673. "dispensing_time": 0,
  674. "updated_time": time.Now().Unix(),
  675. }).Error
  676. if errs != nil {
  677. return errs
  678. }
  679. err1 := ChangeHisPrescriptionT(hid)
  680. if err1 != nil {
  681. return err1
  682. }
  683. var advice_info []*models.HisDoctorAdviceInfo
  684. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", hid).Find(&advice_info).Error
  685. if err != nil {
  686. return
  687. }
  688. for _, v := range advice_info {
  689. //扣减库存
  690. err = DrugAutoAddCancelInfo(v, creater)
  691. if err != nil {
  692. err = fmt.Errorf("!:%v", err)
  693. return
  694. }
  695. drug, _ := FindBaseDrugLibRecordSeven(orgid, v.DrugId)
  696. //查询默认仓库
  697. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  698. //查询默认仓库剩余多少库存
  699. var sum_count int64
  700. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  701. for _, its := range stockInfo {
  702. sum_count += its.StockMaxNumber*drug.MinNumber + its.StockMinNumber
  703. }
  704. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  705. }
  706. 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 "+
  707. "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
  708. if err != nil {
  709. return
  710. }
  711. xid := make([]int64, 0)
  712. for _, v := range xids {
  713. xid = append(xid, v.Id)
  714. map_time[v.DispensingTime] = v.DispensingTime
  715. }
  716. //修改状态
  717. errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id in (?)", xid).Updates(map[string]interface{}{
  718. "is_medicine": 0,
  719. "people": creater,
  720. "dispensing_time": 0,
  721. "updated_time": time.Now().Unix(),
  722. }).Error
  723. if errs1 != nil {
  724. return errs1
  725. }
  726. var advice []*models.HisDoctorAdviceInfo
  727. err = tx.Raw("select * from xt_doctor_advice where id in (?) and status = 1", xid).Scan(&advice).Error
  728. if err != nil {
  729. return
  730. }
  731. for _, v := range advice {
  732. //扣减库存
  733. err = DrugAutoAddCancelInfo(v, creater)
  734. if err != nil {
  735. err = fmt.Errorf("!:%v", err)
  736. return
  737. }
  738. drug, _ := FindBaseDrugLibRecordSeven(orgid, v.DrugId)
  739. //查询默认仓库
  740. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  741. //查询默认仓库剩余多少库存
  742. var sum_count int64
  743. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  744. for _, its := range stockInfo {
  745. sum_count += its.StockMaxNumber*drug.MinNumber + its.StockMinNumber
  746. }
  747. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  748. }
  749. //删除掉发药明细
  750. for k, _ := range map_time {
  751. err = tx.Model(&models.Pharmary{}).Where("user_org_id = ? and record_date = ? and patient_id = ? ", orgid, k, patient_id).Updates(map[string]interface{}{
  752. "status": 0,
  753. "mtime": time.Now().Unix(),
  754. }).Error
  755. if err != nil {
  756. return
  757. }
  758. }
  759. return
  760. }
  761. //获取患者名称
  762. func FindUserName(patient_id int64) (name string) {
  763. var tmp models.GetHisName
  764. XTReadDB().Model(&models.GetHisName{}).Where("id = ?", patient_id).Find(&tmp)
  765. name = tmp.Name
  766. return
  767. }
  768. //获取药品名称
  769. func FindDrugsName(drug_id int64) (name string) {
  770. var tmp models.Drug
  771. XTReadDB().Model(&models.Drug{}).Where("id = ?", drug_id).Find(&tmp)
  772. name = tmp.DrugName
  773. return
  774. }
  775. //药品发药按钮点击(
  776. func MedicineDeparture(ids string, creater, orgid int64) (err error) {
  777. //开事务
  778. tx := XTWriteDB().Begin()
  779. defer func() {
  780. if err != nil {
  781. utils.ErrorLog("事务失败,原因为: %v", err)
  782. tx.Rollback()
  783. } else {
  784. tx.Commit()
  785. }
  786. }()
  787. //处理下字符串
  788. t_ids := ""
  789. if ids[len(ids)-1] == 44 {
  790. t_ids = ids[:len(ids)-1]
  791. } else {
  792. t_ids = ids
  793. }
  794. time_now := time.Now().Unix()
  795. tmp_id := strings.Split(t_ids, ",")
  796. var parameter string //测试
  797. patient_id := make(map[int64]int64)
  798. for _, v := range tmp_id {
  799. front := v[:1]
  800. after := v[1:]
  801. if front == "h" {
  802. var advice_info []*models.HisDoctorAdviceInfo
  803. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and status = 1", after).Find(&advice_info).Error
  804. if err != nil {
  805. return
  806. }
  807. for _, v := range advice_info {
  808. if _, ok := patient_id[v.PatientId]; !ok {
  809. patient_id[v.PatientId] = v.PatientId
  810. }
  811. tmp_bool := IsPharmacyConfig(orgid)
  812. if tmp_bool {
  813. if PettyCash(v.DrugId) {
  814. continue
  815. }
  816. kou := FenDrugInventory(v, orgid)
  817. if !kou {
  818. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  819. tmp := models.XtErrs{
  820. OrgId: orgid,
  821. Route: "creater:" + config.ToString(creater),
  822. Parameter: parameter,
  823. Text_err: err.Error(),
  824. }
  825. XTWriteDB().Create(&tmp)
  826. return
  827. } else {
  828. tmps, _ := json.Marshal(advice_info) //
  829. parameter = parameter + string(tmps) //
  830. }
  831. //扣减库存
  832. err = FenStock(orgid, creater, v)
  833. if err != nil {
  834. err = fmt.Errorf("!:%v", err)
  835. return
  836. }
  837. }
  838. }
  839. //修改状态
  840. errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ?", after).Updates(map[string]interface{}{
  841. "is_medicine": 1,
  842. "people": creater,
  843. "dispensing_time": time_now,
  844. "updated_time": time.Now().Unix(),
  845. }).Error
  846. if errs != nil {
  847. return errs
  848. }
  849. err1 := ChangeHisPrescriptionid(after)
  850. if err1 != nil {
  851. return err1
  852. }
  853. } else {
  854. var advice []*models.HisDoctorAdviceInfo
  855. 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
  856. if err != nil {
  857. return
  858. }
  859. for _, v := range advice {
  860. if _, ok := patient_id[v.PatientId]; !ok {
  861. patient_id[v.PatientId] = v.PatientId
  862. }
  863. tmp_bool := IsPharmacyConfig(orgid)
  864. if tmp_bool {
  865. if PettyCash(v.DrugId) {
  866. continue
  867. }
  868. kou := FenDrugInventory(v, orgid)
  869. if !kou {
  870. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  871. tmp := models.XtErrs{
  872. OrgId: orgid,
  873. Route: "creater:" + config.ToString(creater),
  874. Parameter: parameter,
  875. Text_err: err.Error(),
  876. }
  877. XTWriteDB().Create(&tmp)
  878. return
  879. } else {
  880. tmps, _ := json.Marshal(advice) //
  881. parameter = parameter + string(tmps) //
  882. }
  883. //扣减库存
  884. err = FenStock(orgid, creater, v)
  885. if err != nil {
  886. err = fmt.Errorf("!:%v", err)
  887. return
  888. }
  889. }
  890. }
  891. //修改状态
  892. errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id = ?", after).Updates(map[string]interface{}{
  893. "is_medicine": 1,
  894. "people": creater,
  895. "dispensing_time": time_now,
  896. "updated_time": time.Now().Unix(),
  897. }).Error
  898. if errs1 != nil {
  899. return errs1
  900. }
  901. }
  902. }
  903. for _, v := range patient_id {
  904. //生成明细记录
  905. tmp_ph := models.Pharmary{
  906. UserOrgId: orgid,
  907. PatientId: v,
  908. Ctime: time.Now().Unix(),
  909. Mtime: time.Now().Unix(),
  910. Status: 1,
  911. RecordDate: time_now,
  912. }
  913. err = tx.Create(&tmp_ph).Error
  914. if err != nil {
  915. return err
  916. }
  917. }
  918. return
  919. }
  920. //获取领药人姓名(
  921. func GetUserAdminName(id, orgID int64) string {
  922. var tmp models.XTSgjUserAdminRole
  923. XTReadDB().Model(&models.XTSgjUserAdminRole{}).Where("admin_user_id = ? and org_id = ?", id, orgID).Find(&tmp)
  924. return tmp.UserName
  925. }
  926. //根据id查询是否已发药,true已发药,false未发药
  927. func GiveTheMedicine(id int64) bool {
  928. var xue models.PharmacyDoctorAdvice
  929. XTReadDB().Model(&models.PharmacyDoctorAdvice{}).Where("id = ?", id).Find(&xue)
  930. if xue.IsMedicine == 1 {
  931. return true
  932. }
  933. return false
  934. }
  935. //查询改组中的药品是否包含已发药的,true已发药,false未发药
  936. func GiveGroupMedicine(orgid, groupNo int64) bool {
  937. var total int
  938. XTReadDB().Model(&models.DoctorAdvice{}).Where("user_org_id = ? and groupno = ? and status = 1 and is_medicine = 1", orgid, groupNo).Count(&total)
  939. if total > 0 {
  940. return true
  941. }
  942. return false
  943. }
  944. //查询处方中是否包含已发药的
  945. func GiveChuMedicine(id int64) bool {
  946. var total int
  947. XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", id).Count(&total)
  948. if total > 0 {
  949. return true
  950. }
  951. return false
  952. }
  953. //根据处方id查找该处方中是否存在已发药的药品
  954. func IsChuIssuedDrugs(prescription_id int64) bool {
  955. var total int
  956. XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", prescription_id).Count(&total)
  957. if total > 0 {
  958. return true
  959. }
  960. return false
  961. }
  962. //判断该药品是否通过药房管理出库,true是,false否
  963. //id 药品id,org_id 机构id
  964. func IsPharmacyDelivery(id, org_id int64) bool {
  965. //判断药品是否通过药房管理
  966. var total01 int
  967. XTReadDB().Model(&models.PharmacyBaseDrug{}).Where("id = ? and is_pharmacy = 1", id).Count(&total01)
  968. //判断该机构是否通过药房管理出库
  969. var total02 int
  970. XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and is_open = 1", org_id).Count(&total02)
  971. if total01 > 0 && total02 > 0 {
  972. return true
  973. }
  974. return false
  975. }
  976. //判断机构是否通过药房管理出库
  977. func IsPharmacyConfig(orgid int64) (bo bool) {
  978. var total int
  979. XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and is_open = 1 and status = 1", orgid).Count(&total)
  980. if total > 0 {
  981. return true
  982. }
  983. return false
  984. }
  985. //根据患者id和发药时间,获取医生的id和姓名
  986. func GetDoctorIds(id, recordtime, orgid int64) (doctor_id int64, doctor_name string, err error) {
  987. var tmp []*models.TmpAdviceDoctor
  988. //var tmp []int64
  989. err = XTReadDB().Raw("select distinct advice_doctor from his_doctor_advice_info where "+
  990. "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) "+
  991. "union "+
  992. "select distinct advice_doctor from xt_doctor_advice where "+
  993. "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) ",
  994. recordtime, orgid, id, orgid, recordtime, orgid, id, orgid).Scan(&tmp).Error
  995. if err != nil || len(tmp) == 0 {
  996. return
  997. }
  998. doctor_id = tmp[0].AdviceDoctor
  999. doctor_name = GetUserAdminName(doctor_id, orgid)
  1000. return
  1001. }
  1002. func MapToJson(param map[string]interface{}) string {
  1003. dataType, _ := json.Marshal(param)
  1004. dataString := string(dataType)
  1005. return dataString
  1006. }
  1007. //封装扣减库存
  1008. func FenStock(orgid, creater int64, v *models.HisDoctorAdviceInfo) (err error) {
  1009. err = HisDrugsDelivery(orgid, creater, v)
  1010. if err != nil {
  1011. err = fmt.Errorf("!:%v", err)
  1012. return
  1013. }
  1014. drug, _ := FindBaseDrugLibRecordSeven(orgid, v.DrugId)
  1015. //查询默认仓库
  1016. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  1017. //查询默认仓库剩余多少库存
  1018. var sum_count int64
  1019. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  1020. for _, its := range stockInfo {
  1021. sum_count += its.StockMaxNumber*drug.MinNumber + its.StockMinNumber
  1022. }
  1023. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  1024. return
  1025. }
  1026. //封装查询药品库存是否足够
  1027. func FenDrugInventory(item *models.HisDoctorAdviceInfo, orgid int64) bool {
  1028. var total int64
  1029. var prescribing_number_total int64
  1030. houseConfig, _ := GetAllStoreHouseConfig(orgid)
  1031. ////查询该药品是否有库存
  1032. list, _ := GetDrugTotalCountTwenty(item.DrugId, item.UserOrgId, houseConfig.DrugStorehouseOut)
  1033. ////查询改药品信息
  1034. medical, _ := GetBaseDrugMedical(item.DrugId)
  1035. ////判断单位是否相等
  1036. if medical.MaxUnit == item.PrescribingNumberUnit {
  1037. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1038. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1039. //转化为最小单位
  1040. total = list.Count*medical.MinNumber + list.StockMinNumber
  1041. prescribing_number_total = count * medical.MinNumber
  1042. }
  1043. if medical.MinUnit == item.PrescribingNumberUnit {
  1044. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1045. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1046. total = list.Count*medical.MinNumber + list.StockMinNumber
  1047. prescribing_number_total = count
  1048. }
  1049. if prescribing_number_total <= total {
  1050. //可以扣减
  1051. return true
  1052. } else {
  1053. //不可扣减
  1054. return false
  1055. }
  1056. }
  1057. func GetAdviceIds1(ids []string, orgid, execution_staff int64, thetime time.Time) (tmp []string, err error) {
  1058. var advice []*models.DoctorAdvice
  1059. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND status = 1", ids).Find(&advice).Error
  1060. if err != nil {
  1061. return
  1062. }
  1063. for _, v := range advice {
  1064. if !IsPharmacyDelivery(v.DrugId, orgid) {
  1065. tmp = append(tmp, config.ToString(v.ID))
  1066. } else {
  1067. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? ", v.ID).Updates(map[string]interface{}{
  1068. "execution_staff": execution_staff,
  1069. "execution_time": thetime.Unix(),
  1070. "updated_time": time.Now().Unix(),
  1071. }).Error
  1072. if err != nil {
  1073. return
  1074. }
  1075. }
  1076. }
  1077. return
  1078. }
  1079. func GetAdviceIds2(ids []string, orgid, execution_staff int64, thetime time.Time) (tmp []string, err error) {
  1080. var advice []*models.HisDoctorAdviceInfoL
  1081. err = readDb.Model(&models.HisDoctorAdviceInfoL{}).Where("id IN (?) AND status = 1", ids).Find(&advice).Error
  1082. if err != nil {
  1083. return
  1084. }
  1085. for _, v := range advice {
  1086. if !IsPharmacyDelivery(v.DrugId, orgid) {
  1087. tmp = append(tmp, config.ToString(v.ID))
  1088. } else {
  1089. err = XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? ", v.ID).Updates(map[string]interface{}{
  1090. "execution_staff": execution_staff,
  1091. "execution_time": thetime.Unix(),
  1092. "updated_time": time.Now().Unix(),
  1093. }).Error
  1094. if err != nil {
  1095. return
  1096. }
  1097. }
  1098. }
  1099. return
  1100. }
  1101. func GetAdviceId1(id, orgid, execution_staff int64, thetime time.Time) (bo bool, err error) {
  1102. var advice models.DoctorAdvice
  1103. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND status = 1", id).Find(&advice).Error
  1104. if err != nil {
  1105. return false, err
  1106. }
  1107. if IsPharmacyDelivery(advice.DrugId, orgid) {
  1108. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? ", id).Updates(map[string]interface{}{
  1109. "execution_staff": execution_staff,
  1110. "execution_time": thetime.Unix(),
  1111. "updated_time": time.Now().Unix(),
  1112. }).Error
  1113. if err != nil {
  1114. return false, err
  1115. }
  1116. return true, err
  1117. }
  1118. return false, err
  1119. }
  1120. func GetAdviceId2(id, orgid, execution_staff int64, thetime time.Time) (bo bool, err error) {
  1121. var advice models.HisDoctorAdviceInfoL
  1122. err = readDb.Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? AND status = 1", id).Find(&advice).Error
  1123. if err != nil {
  1124. return false, err
  1125. }
  1126. if IsPharmacyDelivery(advice.DrugId, orgid) {
  1127. err = XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? ", id).Updates(map[string]interface{}{
  1128. "execution_staff": execution_staff,
  1129. "execution_time": thetime.Unix(),
  1130. "updated_time": time.Now().Unix(),
  1131. }).Error
  1132. if err != nil {
  1133. return false, err
  1134. }
  1135. return true, err
  1136. }
  1137. return false, err
  1138. }
  1139. //判断药品是否零用
  1140. func PettyCash(id int64) bool {
  1141. drug := models.XtBaseDrug{}
  1142. XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug)
  1143. if drug.IsUse == 1 {
  1144. return true
  1145. }
  1146. return false
  1147. }
  1148. //初始化
  1149. func InitDrugidIsNil(orgid, stime, etime int64) {
  1150. var advice []*models.DoctorAdvice
  1151. 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)
  1152. for _, v := range advice {
  1153. XTWriteDB().Exec("update xt_doctor_advice set drug_id = (select id from xt_base_drug where drug_name = ? and org_id = ?) where id = ?", v.AdviceName, v.UserOrgId, v.ID)
  1154. }
  1155. }
  1156. //改变处方状态(发药
  1157. func ChangeHisPrescription(tmp []int64) (err error) {
  1158. var advice_info []*models.HisDoctorAdviceInfo
  1159. err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", tmp).Find(&advice_info).Error
  1160. if err != nil {
  1161. if err == gorm.ErrRecordNotFound {
  1162. return nil
  1163. }
  1164. return
  1165. }
  1166. tmp_id := make(map[int64]int64)
  1167. for _, v := range advice_info {
  1168. tmp_id[v.PrescriptionId] = v.PrescriptionId
  1169. }
  1170. for k, _ := range tmp_id {
  1171. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", k).Updates(map[string]interface{}{
  1172. "is_medicine": 1,
  1173. "mtime": time.Now().Unix(),
  1174. }).Error
  1175. if err != nil {
  1176. return
  1177. }
  1178. }
  1179. return
  1180. }
  1181. //改变处方状态(退药
  1182. func ChangeHisPrescriptionT(tmp []int64) (err error) {
  1183. var advice_info []*models.HisDoctorAdviceInfo
  1184. err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", tmp).Find(&advice_info).Error
  1185. if err != nil {
  1186. if err == gorm.ErrRecordNotFound {
  1187. return nil
  1188. }
  1189. return
  1190. }
  1191. tmp_id := make(map[int64]int64)
  1192. for _, v := range advice_info {
  1193. tmp_id[v.PrescriptionId] = v.PrescriptionId
  1194. }
  1195. for k, _ := range tmp_id {
  1196. var total int
  1197. //查询退药的
  1198. XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", k).Count(&total)
  1199. if total == 0 {
  1200. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", k).Updates(map[string]interface{}{
  1201. "is_medicine": 0,
  1202. "mtime": time.Now().Unix(),
  1203. }).Error
  1204. if err != nil {
  1205. return
  1206. }
  1207. }
  1208. }
  1209. return
  1210. }
  1211. //改变处方状态
  1212. func ChangeHisPrescriptionid(id string) (err error) {
  1213. var advice models.HisDoctorAdviceInfo
  1214. err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and status = 1", id).Find(&advice).Error
  1215. if err != nil {
  1216. if err == gorm.ErrRecordNotFound {
  1217. return nil
  1218. }
  1219. return
  1220. }
  1221. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", advice.PrescriptionId).Updates(map[string]interface{}{
  1222. "is_medicine": 1,
  1223. "mtime": time.Now().Unix(),
  1224. }).Error
  1225. if err != nil {
  1226. return
  1227. }
  1228. return
  1229. }