pharmacy_service.go 39KB

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