pharmacy_service.go 45KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  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. if !IsPharmacyDelivery(v.DrugId, orgid) {
  691. continue
  692. }
  693. err = DrugAutoAddCancelInfo(v, creater)
  694. if err != nil {
  695. err = fmt.Errorf("!:%v", err)
  696. return
  697. }
  698. drug, _ := FindBaseDrugLibRecordSeven(orgid, v.DrugId)
  699. //查询默认仓库
  700. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  701. //查询默认仓库剩余多少库存
  702. var sum_count int64
  703. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  704. for _, its := range stockInfo {
  705. sum_count += its.StockMaxNumber*drug.MinNumber + its.StockMinNumber
  706. }
  707. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  708. }
  709. 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 "+
  710. "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
  711. if err != nil {
  712. return
  713. }
  714. xid := make([]int64, 0)
  715. for _, v := range xids {
  716. xid = append(xid, v.Id)
  717. map_time[v.DispensingTime] = v.DispensingTime
  718. }
  719. //修改状态
  720. errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id in (?)", xid).Updates(map[string]interface{}{
  721. "is_medicine": 0,
  722. "people": creater,
  723. "dispensing_time": 0,
  724. "updated_time": time.Now().Unix(),
  725. }).Error
  726. if errs1 != nil {
  727. return errs1
  728. }
  729. var advice []*models.HisDoctorAdviceInfo
  730. err = tx.Raw("select * from xt_doctor_advice where id in (?) and status = 1", xid).Scan(&advice).Error
  731. if err != nil {
  732. return
  733. }
  734. for _, v := range advice {
  735. //扣减库存
  736. if !IsPharmacyDelivery(v.DrugId, orgid) {
  737. continue
  738. }
  739. err = DrugAutoAddCancelInfo(v, creater)
  740. if err != nil {
  741. err = fmt.Errorf("!:%v", err)
  742. return
  743. }
  744. drug, _ := FindBaseDrugLibRecordSeven(orgid, v.DrugId)
  745. //查询默认仓库
  746. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  747. //查询默认仓库剩余多少库存
  748. var sum_count int64
  749. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  750. for _, its := range stockInfo {
  751. sum_count += its.StockMaxNumber*drug.MinNumber + its.StockMinNumber
  752. }
  753. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  754. }
  755. //删除掉发药明细
  756. for k, _ := range map_time {
  757. err = tx.Model(&models.Pharmary{}).Where("user_org_id = ? and record_date = ? and patient_id = ? ", orgid, k, patient_id).Updates(map[string]interface{}{
  758. "status": 0,
  759. "mtime": time.Now().Unix(),
  760. }).Error
  761. if err != nil {
  762. return
  763. }
  764. }
  765. return
  766. }
  767. //获取患者名称
  768. func FindUserName(patient_id int64) (name string) {
  769. var tmp models.GetHisName
  770. XTReadDB().Model(&models.GetHisName{}).Where("id = ?", patient_id).Find(&tmp)
  771. name = tmp.Name
  772. return
  773. }
  774. //获取药品名称
  775. func FindDrugsName(drug_id int64) (name string) {
  776. var tmp models.Drug
  777. XTReadDB().Model(&models.Drug{}).Where("id = ?", drug_id).Find(&tmp)
  778. name = tmp.DrugName
  779. return
  780. }
  781. //药品发药按钮点击(
  782. func MedicineDeparture(ids string, creater, orgid int64) (err error) {
  783. //开事务
  784. tx := XTWriteDB().Begin()
  785. defer func() {
  786. if err != nil {
  787. utils.ErrorLog("事务失败,原因为: %v", err)
  788. tx.Rollback()
  789. } else {
  790. tx.Commit()
  791. }
  792. }()
  793. //处理下字符串
  794. t_ids := ""
  795. if ids[len(ids)-1] == 44 {
  796. t_ids = ids[:len(ids)-1]
  797. } else {
  798. t_ids = ids
  799. }
  800. time_now := time.Now().Unix()
  801. tmp_id := strings.Split(t_ids, ",")
  802. var parameter string //测试
  803. patient_id := make(map[int64]int64)
  804. for _, v := range tmp_id {
  805. front := v[:1]
  806. after := v[1:]
  807. if front == "h" {
  808. var advice_info []*models.HisDoctorAdviceInfo
  809. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and status = 1", after).Find(&advice_info).Error
  810. if err != nil {
  811. return
  812. }
  813. for _, v := range advice_info {
  814. if _, ok := patient_id[v.PatientId]; !ok {
  815. patient_id[v.PatientId] = v.PatientId
  816. }
  817. tmp_bool := IsPharmacyConfig(orgid)
  818. if tmp_bool {
  819. if PettyCash(v.DrugId) {
  820. continue
  821. }
  822. kou := FenDrugInventory(v, orgid)
  823. if !kou {
  824. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  825. tmp := models.XtErrs{
  826. OrgId: orgid,
  827. Route: "creater:" + config.ToString(creater),
  828. Parameter: parameter,
  829. Text_err: err.Error(),
  830. }
  831. XTWriteDB().Create(&tmp)
  832. return
  833. } else {
  834. tmps, _ := json.Marshal(advice_info) //
  835. parameter = parameter + string(tmps) //
  836. }
  837. //扣减库存
  838. err = FenStock(orgid, creater, v)
  839. if err != nil {
  840. err = fmt.Errorf("!:%v", err)
  841. return
  842. }
  843. }
  844. }
  845. //修改状态
  846. errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ?", after).Updates(map[string]interface{}{
  847. "is_medicine": 1,
  848. "people": creater,
  849. "dispensing_time": time_now,
  850. "updated_time": time.Now().Unix(),
  851. }).Error
  852. if errs != nil {
  853. return errs
  854. }
  855. err1 := ChangeHisPrescriptionid(after)
  856. if err1 != nil {
  857. return err1
  858. }
  859. } else {
  860. var advice []*models.HisDoctorAdviceInfo
  861. 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
  862. if err != nil {
  863. return
  864. }
  865. for _, v := range advice {
  866. if _, ok := patient_id[v.PatientId]; !ok {
  867. patient_id[v.PatientId] = v.PatientId
  868. }
  869. tmp_bool := IsPharmacyConfig(orgid)
  870. if tmp_bool {
  871. if PettyCash(v.DrugId) {
  872. continue
  873. }
  874. kou := FenDrugInventory(v, orgid)
  875. if !kou {
  876. err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
  877. tmp := models.XtErrs{
  878. OrgId: orgid,
  879. Route: "creater:" + config.ToString(creater),
  880. Parameter: parameter,
  881. Text_err: err.Error(),
  882. }
  883. XTWriteDB().Create(&tmp)
  884. return
  885. } else {
  886. tmps, _ := json.Marshal(advice) //
  887. parameter = parameter + string(tmps) //
  888. }
  889. //扣减库存
  890. err = FenStock(orgid, creater, v)
  891. if err != nil {
  892. err = fmt.Errorf("!:%v", err)
  893. return
  894. }
  895. }
  896. }
  897. //修改状态
  898. errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id = ?", after).Updates(map[string]interface{}{
  899. "is_medicine": 1,
  900. "people": creater,
  901. "dispensing_time": time_now,
  902. "updated_time": time.Now().Unix(),
  903. }).Error
  904. if errs1 != nil {
  905. return errs1
  906. }
  907. }
  908. }
  909. for _, v := range patient_id {
  910. //生成明细记录
  911. tmp_ph := models.Pharmary{
  912. UserOrgId: orgid,
  913. PatientId: v,
  914. Ctime: time.Now().Unix(),
  915. Mtime: time.Now().Unix(),
  916. Status: 1,
  917. RecordDate: time_now,
  918. }
  919. err = tx.Create(&tmp_ph).Error
  920. if err != nil {
  921. return err
  922. }
  923. }
  924. return
  925. }
  926. //获取领药人姓名(
  927. func GetUserAdminName(id, orgID int64) string {
  928. var tmp models.XTSgjUserAdminRole
  929. XTReadDB().Model(&models.XTSgjUserAdminRole{}).Where("admin_user_id = ? and org_id = ?", id, orgID).Find(&tmp)
  930. return tmp.UserName
  931. }
  932. //根据id查询是否已发药,true已发药,false未发药
  933. func GiveTheMedicine(id int64) bool {
  934. var xue models.PharmacyDoctorAdvice
  935. XTReadDB().Model(&models.PharmacyDoctorAdvice{}).Where("id = ?", id).Find(&xue)
  936. if xue.IsMedicine == 1 {
  937. return true
  938. }
  939. return false
  940. }
  941. //查询改组中的药品是否包含已发药的,true已发药,false未发药
  942. func GiveGroupMedicine(orgid, groupNo int64) bool {
  943. var total int
  944. XTReadDB().Model(&models.DoctorAdvice{}).Where("user_org_id = ? and groupno = ? and status = 1 and is_medicine = 1", orgid, groupNo).Count(&total)
  945. if total > 0 {
  946. return true
  947. }
  948. return false
  949. }
  950. //查询处方中是否包含已发药的
  951. func GiveChuMedicine(id int64) bool {
  952. var total int
  953. XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", id).Count(&total)
  954. if total > 0 {
  955. return true
  956. }
  957. return false
  958. }
  959. //根据处方id查找该处方中是否存在已发药的药品
  960. func IsChuIssuedDrugs(prescription_id int64) bool {
  961. var total int
  962. XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", prescription_id).Count(&total)
  963. if total > 0 {
  964. return true
  965. }
  966. return false
  967. }
  968. //判断该药品是否通过药房管理出库,true是,false否
  969. //id 药品id,org_id 机构id
  970. func IsPharmacyDelivery(id, org_id int64) bool {
  971. //判断药品是否通过药房管理
  972. var total01 int
  973. XTReadDB().Model(&models.PharmacyBaseDrug{}).Where("id = ? and is_pharmacy = 1", id).Count(&total01)
  974. //判断该机构是否通过药房管理出库
  975. var total02 int
  976. XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and is_open = 1", org_id).Count(&total02)
  977. if total01 > 0 && total02 > 0 {
  978. return true
  979. }
  980. return false
  981. }
  982. //判断机构是否通过药房管理出库
  983. func IsPharmacyConfig(orgid int64) (bo bool) {
  984. var total int
  985. XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and is_open = 1 and status = 1", orgid).Count(&total)
  986. if total > 0 {
  987. return true
  988. }
  989. return false
  990. }
  991. //根据患者id和发药时间,获取医生的id和姓名
  992. func GetDoctorIds(id, recordtime, orgid int64) (doctor_id int64, doctor_name string, err error) {
  993. var tmp []*models.TmpAdviceDoctor
  994. //var tmp []int64
  995. err = XTReadDB().Raw("select distinct advice_doctor from his_doctor_advice_info where "+
  996. "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) "+
  997. "union "+
  998. "select distinct advice_doctor from xt_doctor_advice where "+
  999. "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) ",
  1000. recordtime, orgid, id, orgid, recordtime, orgid, id, orgid).Scan(&tmp).Error
  1001. if err != nil || len(tmp) == 0 {
  1002. return
  1003. }
  1004. doctor_id = tmp[0].AdviceDoctor
  1005. doctor_name = GetUserAdminName(doctor_id, orgid)
  1006. return
  1007. }
  1008. func MapToJson(param map[string]interface{}) string {
  1009. dataType, _ := json.Marshal(param)
  1010. dataString := string(dataType)
  1011. return dataString
  1012. }
  1013. //封装扣减库存
  1014. func FenStock(orgid, creater int64, v *models.HisDoctorAdviceInfo) (err error) {
  1015. err = HisDrugsDelivery(orgid, creater, v)
  1016. if err != nil {
  1017. err = fmt.Errorf("!:%v", err)
  1018. return
  1019. }
  1020. drug, _ := FindBaseDrugLibRecordSeven(orgid, v.DrugId)
  1021. //查询默认仓库
  1022. storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
  1023. //查询默认仓库剩余多少库存
  1024. var sum_count int64
  1025. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
  1026. for _, its := range stockInfo {
  1027. sum_count += its.StockMaxNumber*drug.MinNumber + its.StockMinNumber
  1028. }
  1029. UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
  1030. return
  1031. }
  1032. //封装查询药品库存是否足够
  1033. func FenDrugInventory(item *models.HisDoctorAdviceInfo, orgid int64) bool {
  1034. var total int64
  1035. var prescribing_number_total int64
  1036. houseConfig, _ := GetAllStoreHouseConfig(orgid)
  1037. ////查询该药品是否有库存
  1038. list, _ := GetDrugTotalCountTwenty(item.DrugId, item.UserOrgId, houseConfig.DrugStorehouseOut)
  1039. ////查询改药品信息
  1040. medical, _ := GetBaseDrugMedical(item.DrugId)
  1041. ////判断单位是否相等
  1042. if medical.MaxUnit == item.PrescribingNumberUnit {
  1043. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1044. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1045. //转化为最小单位
  1046. total = list.Count*medical.MinNumber + list.StockMinNumber
  1047. prescribing_number_total = count * medical.MinNumber
  1048. }
  1049. if medical.MinUnit == item.PrescribingNumberUnit {
  1050. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  1051. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1052. total = list.Count*medical.MinNumber + list.StockMinNumber
  1053. prescribing_number_total = count
  1054. }
  1055. if prescribing_number_total <= total {
  1056. //可以扣减
  1057. return true
  1058. } else {
  1059. //不可扣减
  1060. return false
  1061. }
  1062. }
  1063. func GetAdviceIds1(ids []string, orgid, execution_staff int64, thetime time.Time) (tmp []string, err error) {
  1064. var advice []*models.DoctorAdvice
  1065. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND status = 1", ids).Find(&advice).Error
  1066. if err != nil {
  1067. return
  1068. }
  1069. for _, v := range advice {
  1070. if !IsPharmacyDelivery(v.DrugId, orgid) {
  1071. tmp = append(tmp, config.ToString(v.ID))
  1072. } else {
  1073. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? ", v.ID).Updates(map[string]interface{}{
  1074. "execution_staff": execution_staff,
  1075. "execution_time": thetime.Unix(),
  1076. "updated_time": time.Now().Unix(),
  1077. }).Error
  1078. if err != nil {
  1079. return
  1080. }
  1081. }
  1082. }
  1083. return
  1084. }
  1085. func GetAdviceIds2(ids []string, orgid, execution_staff int64, thetime time.Time) (tmp []string, err error) {
  1086. var advice []*models.HisDoctorAdviceInfoL
  1087. err = readDb.Model(&models.HisDoctorAdviceInfoL{}).Where("id IN (?) AND status = 1", ids).Find(&advice).Error
  1088. if err != nil {
  1089. return
  1090. }
  1091. for _, v := range advice {
  1092. if !IsPharmacyDelivery(v.DrugId, orgid) {
  1093. tmp = append(tmp, config.ToString(v.ID))
  1094. } else {
  1095. err = XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? ", v.ID).Updates(map[string]interface{}{
  1096. "execution_staff": execution_staff,
  1097. "execution_time": thetime.Unix(),
  1098. "updated_time": time.Now().Unix(),
  1099. }).Error
  1100. if err != nil {
  1101. return
  1102. }
  1103. }
  1104. }
  1105. return
  1106. }
  1107. func GetAdviceId1(id, orgid, execution_staff int64, thetime time.Time) (bo bool, err error) {
  1108. var advice models.DoctorAdvice
  1109. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND status = 1", id).Find(&advice).Error
  1110. if err != nil {
  1111. return false, err
  1112. }
  1113. if IsPharmacyDelivery(advice.DrugId, orgid) {
  1114. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? ", id).Updates(map[string]interface{}{
  1115. "execution_staff": execution_staff,
  1116. "execution_time": thetime.Unix(),
  1117. "updated_time": time.Now().Unix(),
  1118. }).Error
  1119. if err != nil {
  1120. return false, err
  1121. }
  1122. return true, err
  1123. }
  1124. return false, err
  1125. }
  1126. func GetAdviceId2(id, orgid, execution_staff int64, thetime time.Time) (bo bool, err error) {
  1127. var advice models.HisDoctorAdviceInfoL
  1128. err = readDb.Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? AND status = 1", id).Find(&advice).Error
  1129. if err != nil {
  1130. return false, err
  1131. }
  1132. if IsPharmacyDelivery(advice.DrugId, orgid) {
  1133. err = XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? ", id).Updates(map[string]interface{}{
  1134. "execution_staff": execution_staff,
  1135. "execution_time": thetime.Unix(),
  1136. "updated_time": time.Now().Unix(),
  1137. }).Error
  1138. if err != nil {
  1139. return false, err
  1140. }
  1141. return true, err
  1142. }
  1143. return false, err
  1144. }
  1145. //判断药品是否零用
  1146. func PettyCash(id int64) bool {
  1147. drug := models.XtBaseDrug{}
  1148. XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug)
  1149. if drug.IsUse == 1 {
  1150. return true
  1151. }
  1152. return false
  1153. }
  1154. //初始化
  1155. func InitDrugidIsNil(orgid, stime, etime int64) {
  1156. var advice []*models.DoctorAdvice
  1157. 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)
  1158. for _, v := range advice {
  1159. 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)
  1160. }
  1161. }
  1162. //改变处方状态(发药
  1163. func ChangeHisPrescription(tmp []int64) (err error) {
  1164. var advice_info []*models.HisDoctorAdviceInfo
  1165. err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", tmp).Find(&advice_info).Error
  1166. if err != nil {
  1167. if err == gorm.ErrRecordNotFound {
  1168. return nil
  1169. }
  1170. return
  1171. }
  1172. tmp_id := make(map[int64]int64)
  1173. for _, v := range advice_info {
  1174. tmp_id[v.PrescriptionId] = v.PrescriptionId
  1175. }
  1176. for k, _ := range tmp_id {
  1177. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", k).Updates(map[string]interface{}{
  1178. "is_medicine": 1,
  1179. "mtime": time.Now().Unix(),
  1180. }).Error
  1181. if err != nil {
  1182. return
  1183. }
  1184. }
  1185. return
  1186. }
  1187. //改变处方状态(退药
  1188. func ChangeHisPrescriptionT(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. var total int
  1203. //查询退药的
  1204. XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", k).Count(&total)
  1205. if total == 0 {
  1206. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", k).Updates(map[string]interface{}{
  1207. "is_medicine": 0,
  1208. "mtime": time.Now().Unix(),
  1209. }).Error
  1210. if err != nil {
  1211. return
  1212. }
  1213. }
  1214. }
  1215. return
  1216. }
  1217. //改变处方状态
  1218. func ChangeHisPrescriptionid(id string) (err error) {
  1219. var advice models.HisDoctorAdviceInfo
  1220. err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and status = 1", id).Find(&advice).Error
  1221. if err != nil {
  1222. if err == gorm.ErrRecordNotFound {
  1223. return nil
  1224. }
  1225. return
  1226. }
  1227. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", advice.PrescriptionId).Updates(map[string]interface{}{
  1228. "is_medicine": 1,
  1229. "mtime": time.Now().Unix(),
  1230. }).Error
  1231. if err != nil {
  1232. return
  1233. }
  1234. return
  1235. }