pharmacy_service.go 67KB

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