pharmacy_service.go 64KB

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