his_charge_service.go 95KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. package service
  2. import (
  3. "XT_New/models"
  4. "github.com/jinzhu/gorm"
  5. "time"
  6. )
  7. func GetAllPatientChargeDetails(org_id int64, start_time int64, end_time int64, keyword string, item_type int64) (patients []*models.NewChargePatient, err error) {
  8. if len(keyword) == 0 {
  9. switch item_type {
  10. case 0:
  11. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB {
  12. return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id").
  13. Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  14. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  15. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  16. return db.Preload("Drug", "status = 1").Where("status = 1")
  17. }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  18. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  19. return db.Select("id,project_name,unit").Where("status = 1 ")
  20. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  21. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  22. }).Where("status = 1 ")
  23. }).Where("status = 1")
  24. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id)
  25. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  26. break
  27. case 1:
  28. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB {
  29. return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id").
  30. Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  31. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").Where("advice_id > 0 AND project_id = 0").Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  32. return db.Preload("Drug", "status = 1").Where("status = 1")
  33. }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  34. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  35. return db.Select("id,project_name,unit").Where("status = 1 ")
  36. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  37. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  38. }).Where("status = 1 ")
  39. }).Where("status = 1")
  40. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id)
  41. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  42. break
  43. case 2:
  44. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB {
  45. return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id").
  46. Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  47. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").Where("advice_id = 0 AND project_id > 0").Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  48. return db.Preload("Drug", "status = 1").Where("status = 1")
  49. }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  50. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  51. return db.Select("id,project_name,unit").Where("status = 1 ")
  52. }).Where("status = 1 ")
  53. }).Where("status = 1")
  54. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id)
  55. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  56. break
  57. case 3:
  58. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB {
  59. return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id").
  60. Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  61. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").Where("advice_id = 0 AND project_id > 0").Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  62. return db.Preload("Drug", "status = 1").Where("status = 1")
  63. }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  64. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  65. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  66. }).Where("status = 1 ")
  67. }).Where("status = 1")
  68. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id)
  69. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  70. break
  71. }
  72. } else {
  73. keyword := "%" + keyword + "%"
  74. switch item_type {
  75. case 0:
  76. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB {
  77. return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id").
  78. Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  79. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  80. return db.Preload("Drug", "status = 1").Where("status = 1")
  81. }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  82. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  83. return db.Select("id,project_name,unit").Where("status = 1 ")
  84. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  85. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  86. }).Where("status = 1 ")
  87. }).Where("status = 1")
  88. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id)
  89. }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error
  90. break
  91. case 1:
  92. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB {
  93. return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id").
  94. Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  95. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").Where("advice_id > 0 AND project_id = 0").Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  96. return db.Preload("Drug", "status = 1").Where("status = 1")
  97. }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  98. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  99. return db.Select("id,project_name,unit").Where("status = 1 ")
  100. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  101. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  102. }).Where("status = 1 ")
  103. }).Where("status = 1")
  104. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id)
  105. }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error
  106. break
  107. case 2:
  108. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB {
  109. return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id").
  110. Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  111. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").Where("advice_id = 0 AND project_id > 0").Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  112. return db.Preload("Drug", "status = 1").Where("status = 1")
  113. }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  114. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  115. return db.Select("id,project_name,unit").Where("status = 1 ")
  116. }).Where("status = 1 ")
  117. }).Where("status = 1")
  118. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id)
  119. }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error
  120. break
  121. case 3:
  122. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB {
  123. return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id").
  124. Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  125. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").Where("advice_id = 0 AND project_id > 0").Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  126. return db.Preload("Drug", "status = 1").Where("status = 1")
  127. }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  128. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  129. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  130. }).Where("status = 1 ")
  131. }).Where("status = 1")
  132. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id)
  133. }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error
  134. break
  135. }
  136. }
  137. return
  138. }
  139. func GetNewAllPatientDrugChargeDetails(org_id int64, start_time_str string, end_time_str string, start_time int64, end_time int64, keyword string, item_type int64, time_type string) (patients []*models.NewChargeDetail, err error) {
  140. if len(keyword) == 0 {
  141. switch item_type {
  142. case 0:
  143. if time_type == "0" {
  144. err = readDb.Table("his_order_info as oi").Select("-1000 as cost_classify, oi.advice_id as advice_id, 0 as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,drug.drug_name as item_name,drug.dose as dose,drug.dose_unit as dose_unit, drug.min_number as min_number,drug.min_unit as min_unit, drug.max_unit as max_unit,drug.id as item_id").
  145. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  146. Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id").
  147. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  148. Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id").
  149. Where("oi.advice_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  150. } else {
  151. err = readDb.Table("his_order_info as oi").Select("-1000 as cost_classify,oi.advice_id as advice_id, 0 as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,drug.drug_name as item_name,drug.dose as dose,drug.dose_unit as dose_unit, drug.min_number as min_number,drug.min_unit as min_unit, drug.max_unit as max_unit,drug.id as item_id").
  152. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  153. Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id").
  154. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  155. Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id").
  156. Where("oi.advice_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  157. }
  158. break
  159. case 1:
  160. if time_type == "0" {
  161. err = readDb.Table("his_order_info as oi").Select("-1000 as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,oi.advice_id as advice_id, 0 as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,drug.drug_name as item_name,drug.dose as dose,drug.dose_unit as dose_unit, drug.min_number as min_number,drug.min_unit as min_unit, drug.max_unit as max_unit,drug.id as item_id").
  162. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  163. Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id").
  164. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  165. Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id").
  166. Where("oi.advice_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  167. } else {
  168. err = readDb.Table("his_order_info as oi").Select("-1000 as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,oi.advice_id as advice_id, 0 as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,drug.drug_name as item_name,drug.dose as dose,drug.dose_unit as dose_unit, drug.min_number as min_number,drug.min_unit as min_unit, drug.max_unit as max_unit,drug.id as item_id").
  169. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  170. Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id").
  171. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  172. Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id").
  173. Where("oi.advice_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  174. }
  175. break
  176. }
  177. } else {
  178. keyword := "%" + keyword + "%"
  179. switch item_type {
  180. case 0:
  181. if time_type == "0" {
  182. err = readDb.Table("his_order_info as oi").Select("-1000 as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,oi.advice_id as advice_id, 0 as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,drug.drug_name as item_name,drug.dose as dose,drug.dose_unit as dose_unit, drug.min_number as min_number,drug.min_unit as min_unit, drug.max_unit as max_unit,drug.id as item_id").
  183. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  184. Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id").
  185. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  186. Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id").
  187. Where("oi.advice_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  188. } else {
  189. err = readDb.Table("his_order_info as oi").Select("-1000 as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,oi.advice_id as advice_id, 0 as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,drug.drug_name as item_name,drug.dose as dose,drug.dose_unit as dose_unit, drug.min_number as min_number,drug.min_unit as min_unit, drug.max_unit as max_unit,drug.id as item_id").
  190. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  191. Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id").
  192. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  193. Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id").
  194. Where("oi.advice_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  195. }
  196. break
  197. case 1:
  198. if time_type == "0" {
  199. err = readDb.Table("his_order_info as oi").Select("-1000 as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,oi.advice_id as advice_id, 0 as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,drug.drug_name as item_name,drug.dose as dose,drug.dose_unit as dose_unit, drug.min_number as min_number,drug.min_unit as min_unit, drug.max_unit as max_unit,drug.id as item_id").
  200. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  201. Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id").
  202. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  203. Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id").
  204. Where("oi.advice_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  205. } else {
  206. err = readDb.Table("his_order_info as oi").Select("-1000 as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,oi.advice_id as advice_id, 0 as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,drug.drug_name as item_name,drug.dose as dose,drug.dose_unit as dose_unit, drug.min_number as min_number,drug.min_unit as min_unit, drug.max_unit as max_unit,drug.id as item_id").
  207. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  208. Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id").
  209. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  210. Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id").
  211. Where("oi.advice_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  212. }
  213. break
  214. }
  215. }
  216. return
  217. }
  218. func GetNewAllPatientProjectAndGoodChargeDetails(org_id int64, start_time_str string, end_time_str string, start_time int64, end_time int64, keyword string, item_type int64, time_type string) (patients []*models.NewChargeDetail, err error) {
  219. if len(keyword) == 0 {
  220. switch item_type {
  221. case 0:
  222. if time_type == "0" {
  223. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  224. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  225. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id ").
  226. Joins(" left join xt_his_project project on p.project_id = project.id").
  227. Joins(" left join xt_good_information good on p.project_id = good.id").
  228. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  229. Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  230. } else {
  231. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  232. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  233. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id ").
  234. Joins(" left join xt_his_project project on p.project_id = project.id").
  235. Joins(" left join xt_good_information good on p.project_id = good.id").
  236. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  237. Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  238. }
  239. //err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB {
  240. // return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id").
  241. // Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  242. // return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  243. // Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  244. // return db.Preload("Drug", "status = 1").Where("status = 1")
  245. // }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  246. // return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  247. // return db.Select("id,project_name,unit").Where("status = 1 ")
  248. // }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  249. // return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  250. // }).Where("status = 1 ")
  251. // }).Where("status = 1")
  252. // }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id)
  253. //}).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  254. break
  255. case 2:
  256. if time_type == "0" {
  257. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  258. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str).
  259. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  260. Joins("left join xt_his_project project on p.project_id = project.id").
  261. Joins("left join xt_good_information good on p.project_id = good.id").
  262. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  263. Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  264. } else {
  265. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  266. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str).
  267. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  268. Joins("left join xt_his_project project on p.project_id = project.id").
  269. Joins("left join xt_good_information good on p.project_id = good.id").
  270. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  271. Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  272. }
  273. break
  274. case 3:
  275. if time_type == "0" {
  276. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  277. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str).
  278. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 3").
  279. Joins("left join xt_his_project project on p.project_id = project.id").
  280. Joins("left join xt_good_information good on p.project_id = good.id").
  281. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  282. Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  283. } else {
  284. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  285. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str).
  286. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 3").
  287. Joins("left join xt_his_project project on p.project_id = project.id").
  288. Joins("left join xt_good_information good on p.project_id = good.id").
  289. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  290. Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  291. }
  292. break
  293. case 4:
  294. if time_type == "0" {
  295. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  296. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str).
  297. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  298. Joins("left join xt_his_project project on p.project_id = project.id").
  299. Joins("left join xt_good_information good on p.project_id = good.id").
  300. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  301. Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 and project.cost_classify = 3", org_id, start_time, end_time).Scan(&patients).Error
  302. } else {
  303. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  304. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str).
  305. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  306. Joins("left join xt_his_project project on p.project_id = project.id").
  307. Joins("left join xt_good_information good on p.project_id = good.id").
  308. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  309. Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 and project.cost_classify = 3 ", org_id, start_time, end_time).Scan(&patients).Error
  310. }
  311. break
  312. case 5:
  313. if time_type == "0" {
  314. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  315. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str).
  316. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  317. Joins("left join xt_his_project project on p.project_id = project.id").
  318. Joins("left join xt_good_information good on p.project_id = good.id").
  319. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  320. Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 and project.cost_classify = 2", org_id, start_time, end_time).Scan(&patients).Error
  321. } else {
  322. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  323. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str).
  324. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  325. Joins("left join xt_his_project project on p.project_id = project.id").
  326. Joins("left join xt_good_information good on p.project_id = good.id").
  327. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  328. Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 and project.cost_classify = 2", org_id, start_time, end_time).Scan(&patients).Error
  329. }
  330. break
  331. case 6:
  332. if time_type == "0" {
  333. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  334. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str).
  335. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  336. Joins("left join xt_his_project project on p.project_id = project.id").
  337. Joins("left join xt_good_information good on p.project_id = good.id").
  338. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  339. Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 and project.cost_classify = 6", org_id, start_time, end_time).Scan(&patients).Error
  340. } else {
  341. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  342. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str).
  343. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  344. Joins("left join xt_his_project project on p.project_id = project.id").
  345. Joins("left join xt_good_information good on p.project_id = good.id").
  346. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  347. Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 and project.cost_classify = 6", org_id, start_time, end_time).Scan(&patients).Error
  348. }
  349. break
  350. case 7:
  351. if time_type == "0" {
  352. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  353. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str).
  354. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  355. Joins("left join xt_his_project project on p.project_id = project.id").
  356. Joins("left join xt_good_information good on p.project_id = good.id").
  357. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  358. Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 and project.cost_classify = 11", org_id, start_time, end_time).Scan(&patients).Error
  359. } else {
  360. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  361. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str).
  362. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  363. Joins("left join xt_his_project project on p.project_id = project.id").
  364. Joins("left join xt_good_information good on p.project_id = good.id").
  365. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  366. Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 and project.cost_classify = 11", org_id, start_time, end_time).Scan(&patients).Error
  367. }
  368. break
  369. }
  370. } else {
  371. keyword := "%" + keyword + "%"
  372. switch item_type {
  373. case 0:
  374. if time_type == "0" {
  375. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  376. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  377. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id ").
  378. Joins("left join xt_his_project project on p.project_id = project.id").
  379. Joins("left join xt_good_information good on p.project_id = good.id").
  380. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  381. Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  382. } else {
  383. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  384. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  385. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id ").
  386. Joins("left join xt_his_project project on p.project_id = project.id").
  387. Joins("left join xt_good_information good on p.project_id = good.id").
  388. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  389. Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  390. }
  391. break
  392. case 2:
  393. if time_type == "0" {
  394. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  395. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  396. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  397. Joins("left join xt_his_project project on p.project_id = project.id").
  398. Joins("left join xt_good_information good on p.project_id = good.id").
  399. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  400. Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  401. } else {
  402. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  403. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  404. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  405. Joins("left join xt_his_project project on p.project_id = project.id").
  406. Joins("left join xt_good_information good on p.project_id = good.id").
  407. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  408. Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  409. }
  410. break
  411. case 3:
  412. if time_type == "0" {
  413. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  414. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  415. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 3").
  416. Joins("left join xt_his_project project on p.project_id = project.id").
  417. Joins("left join xt_good_information good on p.project_id = good.id").
  418. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  419. Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  420. } else {
  421. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  422. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  423. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 3").
  424. Joins("left join xt_his_project project on p.project_id = project.id").
  425. Joins("left join xt_good_information good on p.project_id = good.id").
  426. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  427. Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error
  428. }
  429. case 4:
  430. if time_type == "0" {
  431. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  432. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  433. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  434. Joins("left join xt_his_project project on p.project_id = project.id").
  435. Joins("left join xt_good_information good on p.project_id = good.id").
  436. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  437. Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 and project.cost_classify = 3 ", org_id, start_time, end_time).Scan(&patients).Error
  438. } else {
  439. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  440. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  441. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  442. Joins("left join xt_his_project project on p.project_id = project.id").
  443. Joins("left join xt_good_information good on p.project_id = good.id").
  444. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  445. Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 and project.cost_classify = 3 ", org_id, start_time, end_time).Scan(&patients).Error
  446. }
  447. case 5:
  448. if time_type == "0" {
  449. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  450. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  451. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  452. Joins("left join xt_his_project project on p.project_id = project.id").
  453. Joins("left join xt_good_information good on p.project_id = good.id").
  454. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  455. Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 and project.cost_classify = 2", org_id, start_time, end_time).Scan(&patients).Error
  456. } else {
  457. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  458. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  459. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  460. Joins("left join xt_his_project project on p.project_id = project.id").
  461. Joins("left join xt_good_information good on p.project_id = good.id").
  462. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  463. Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 and project.cost_classify = 2 ", org_id, start_time, end_time).Scan(&patients).Error
  464. }
  465. case 6:
  466. if time_type == "0" {
  467. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  468. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  469. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  470. Joins("left join xt_his_project project on p.project_id = project.id").
  471. Joins("left join xt_good_information good on p.project_id = good.id").
  472. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  473. Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 and project.cost_classify = 10", org_id, start_time, end_time).Scan(&patients).Error
  474. } else {
  475. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  476. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  477. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  478. Joins("left join xt_his_project project on p.project_id = project.id").
  479. Joins("left join xt_good_information good on p.project_id = good.id").
  480. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  481. Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 and project.cost_classify = 10", org_id, start_time, end_time).Scan(&patients).Error
  482. }
  483. case 7:
  484. if time_type == "0" {
  485. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  486. Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  487. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  488. Joins("left join xt_his_project project on p.project_id = project.id").
  489. Joins("left join xt_good_information good on p.project_id = good.id").
  490. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  491. Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 and project.cost_classify = 11", org_id, start_time, end_time).Scan(&patients).Error
  492. } else {
  493. err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id").
  494. Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str).
  495. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  496. Joins("left join xt_his_project project on p.project_id = project.id").
  497. Joins("left join xt_good_information good on p.project_id = good.id").
  498. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  499. Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 and project.cost_classify = 11", org_id, start_time, end_time).Scan(&patients).Error
  500. }
  501. break
  502. }
  503. }
  504. return
  505. }
  506. func GetAllPatientChargeSettle(org_id int64, start_time int64, end_time int64, keyword string, item_type int64) (patients []*models.SettlePatient, err error) {
  507. if len(keyword) == 0 {
  508. switch item_type {
  509. case 0:
  510. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB {
  511. return db.Preload("OrderHisPatient", "status = 1").Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id).Order("ctime")
  512. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  513. break
  514. case 1:
  515. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB {
  516. return db.Preload("OrderHisPatient", "status = 1").Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND is_medicine_insurance = 1 ", start_time, end_time, org_id).Order("ctime")
  517. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  518. break
  519. case 2:
  520. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB {
  521. return db.Preload("OrderHisPatient", "status = 1").Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND is_medicine_insurance = 0", start_time, end_time, org_id).Order("ctime")
  522. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  523. break
  524. case 3:
  525. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB {
  526. return db.Preload("OrderHisPatient", "status = 1").Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND med_type = 14", start_time, end_time, org_id).Order("ctime")
  527. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  528. break
  529. case 4:
  530. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB {
  531. return db.Preload("OrderHisPatient", "status = 1").Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND med_type = 11", start_time, end_time, org_id).Order("ctime")
  532. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  533. break
  534. case 5:
  535. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2 AND orders.insutype = 390", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB {
  536. return db.Preload("OrderHisPatient", "status = 1").Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 ", start_time, end_time, org_id).Order("ctime")
  537. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  538. break
  539. case 6:
  540. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2 AND orders.insutype = 310 ", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB {
  541. return db.Preload("OrderHisPatient", "status = 1").Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id).Order("ctime")
  542. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  543. break
  544. }
  545. } else {
  546. keyword = "%" + keyword + "%"
  547. switch item_type {
  548. case 0:
  549. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB {
  550. return db.Preload("OrderHisPatient", "status = 1").Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id).Order("ctime")
  551. }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error
  552. break
  553. case 1:
  554. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB {
  555. return db.Preload("OrderHisPatient", "status = 1").Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND is_medicine_insurance = 1 ", start_time, end_time, org_id).Order("ctime")
  556. }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error
  557. break
  558. case 2:
  559. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB {
  560. return db.Preload("OrderHisPatient", "status = 1").Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND is_medicine_insurance = 0", start_time, end_time, org_id).Order("ctime")
  561. }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error
  562. break
  563. case 3:
  564. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB {
  565. return db.Preload("OrderHisPatient", "status = 1").Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND med_type = 14", start_time, end_time, org_id).Order("ctime")
  566. }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error
  567. break
  568. case 4:
  569. err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB {
  570. return db.Preload("OrderHisPatient", "status = 1").Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND med_type = 11", start_time, end_time, org_id).Order("ctime")
  571. }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error
  572. break
  573. }
  574. }
  575. return
  576. }
  577. func GetLabelPrintList(page int64, limit int64, user_org_id int64, record_time int64, is_print int64, keywors string, tube_color int64) (labels []*models.HisLabelPrintInfo, total int64, err error) {
  578. offset := (page - 1) * limit
  579. db := readDb.Model(&models.HisLabelPrintInfo{})
  580. db = db.Preload("HisProjectTeam", "status = 1")
  581. if is_print > 0 {
  582. db = db.Where("is_print = ?", is_print)
  583. }
  584. if len(keywors) > 0 {
  585. keywors = "%" + keywors + "%"
  586. db = db.Where("patient_name Like ?", keywors)
  587. }
  588. if tube_color > 0 {
  589. db = db.Joins("Right join xt_his_project_team as team on team.id = his_label_print_info.item_id AND his_label_print_info.item_id <> 0 and team.status = 1 and team.tube_color = ?", tube_color)
  590. //db = db.Joins("Right join xt_his_project as pro on pro.id = his_label_print_info.project_id AND his_label_print_info.item_id = 0 and his_label_print_info.project_id > 0 and pro.status = 1 and pro.tube_color = ?",tube_color)
  591. }
  592. err = db.Where("his_label_print_info.user_org_id = ? AND his_label_print_info.record_date = ? AND his_label_print_info.status = 1 ", user_org_id, record_time).Count(&total).Offset(offset).Limit(limit).Find(&labels).Error
  593. return
  594. }
  595. func GetLabelPrintListTwo(page int64, limit int64, user_org_id int64, record_time int64, is_print int64, keywors string, tube_color int64) (labels []*models.HisLabelPrintInfo, total int64, err error) {
  596. offset := (page - 1) * limit
  597. db := readDb.Model(&models.HisLabelPrintInfo{})
  598. db = db.Preload("HisProjectTeam", "status = 1")
  599. if is_print > 0 {
  600. db = db.Where("is_print = ?", is_print)
  601. }
  602. if len(keywors) > 0 {
  603. keywors = "%" + keywors + "%"
  604. db = db.Where("patient_name Like ?", keywors)
  605. }
  606. if tube_color > 0 {
  607. //db = db.Joins("Right join xt_his_project_team as team on team.id = his_label_print_info.item_id AND his_label_print_info.item_id <> 0 and team.status = 1 and team.tube_color = ?",tube_color)
  608. db = db.Joins("Right join xt_his_project as pro on pro.id = his_label_print_info.project_id AND his_label_print_info.item_id = 0 and his_label_print_info.project_id > 0 and pro.status = 1 and pro.tube_color = ?", tube_color)
  609. }
  610. err = db.Where("his_label_print_info.user_org_id = ? AND his_label_print_info.record_date = ? AND his_label_print_info.status = 1 ", user_org_id, record_time).Count(&total).Offset(offset).Limit(limit).Find(&labels).Error
  611. return
  612. }
  613. func GetLabelPrintListThree(user_org_id int64, record_time int64, is_print int64, keywors string, tube_color int64) (labels []*models.HisLabelPrintInfo, total int64, err error) {
  614. db := readDb.Model(&models.HisLabelPrintInfo{})
  615. db = db.Preload("HisProjectTeam", "status = 1")
  616. if is_print > 0 {
  617. db = db.Where("is_print = ?", is_print)
  618. }
  619. if len(keywors) > 0 {
  620. keywors = "%" + keywors + "%"
  621. db = db.Where("patient_name Like ?", keywors)
  622. }
  623. //if tube_color > 0 {
  624. // db = db.Joins("Right join xt_his_project_team as team on team.id = his_label_print_info.item_id AND his_label_print_info.item_id <> 0 and team.status = 1 and team.tube_color = ?", tube_color)
  625. //}
  626. err = db.Where("his_label_print_info.user_org_id = ? AND his_label_print_info.record_date = ? AND his_label_print_info.status = 1 ", user_org_id, record_time).Count(&total).Find(&labels).Error
  627. return
  628. }
  629. //func GetLabelPrintListFour(user_org_id int64, record_time int64, is_print int64, keywors string, tube_color int64) (labels []*models.HisLabelPrintInfo, total int64, err error) {
  630. // db := readDb.Model(&models.HisLabelPrintInfo{})
  631. // db = db.Preload("HisProjectTeam", "status = 1")
  632. //
  633. // if is_print > 0 {
  634. // db = db.Where("is_print = ?", is_print)
  635. // }
  636. // if len(keywors) > 0 {
  637. // keywors = "%" + keywors + "%"
  638. // db = db.Where("patient_name Like ?", keywors)
  639. //
  640. // }
  641. // //if tube_color > 0 {
  642. // // //db = db.Joins("Right join xt_his_project_team as team on team.id = his_label_print_info.item_id AND his_label_print_info.item_id <> 0 and team.status = 1 and team.tube_color = ?",tube_color)
  643. // // db = db.Joins("Right join xt_his_project as pro on pro.id = his_label_print_info.project_id AND his_label_print_info.item_id = 0 and his_label_print_info.project_id > 0 and pro.status = 1 and pro.tube_color = ?", tube_color)
  644. // //}
  645. //
  646. // err = db.Where("his_label_print_info.user_org_id = ? AND his_label_print_info.record_date = ? AND his_label_print_info.status = 1 ", user_org_id, record_time).Count(&total).Find(&labels).Error
  647. // return
  648. //}
  649. func GetLabelPrintInfo(id int64) (labels models.HisLabelPrintInfo, err error) {
  650. db := readDb.Model(&models.HisLabelPrintInfo{})
  651. err = db.Where("id = ?", id).First(&labels).Error
  652. return
  653. }
  654. func AddSigleFapiaoRecord(dealer *models.HisFapiaoRecord) (err error, record *models.HisFapiaoRecord) {
  655. err = writeDb.Create(&dealer).Error
  656. return err, dealer
  657. }
  658. func ModifyFapiao(mesick *models.HisFapiaoRecord) error {
  659. err := writeDb.Save(&mesick).Error
  660. return err
  661. }
  662. func UpdateFapiaoIsUse(org_id int64) {
  663. writeDb.Model(&models.HisFapiaoRecord{}).Where("user_org_id = ?", org_id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "is_use": 0})
  664. }
  665. func FindAllFapiaoList(orgId int64, page int64, limit int64) (list []*models.HisFapiaoRecord, total int64, err error) {
  666. offset := (page - 1) * limit
  667. db := readDb.Model(&models.HisFapiaoRecord{})
  668. db = db.Where("user_org_id = ? AND status = 1", orgId)
  669. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  670. return
  671. }
  672. func DeleteFapiaoById(id int64) error {
  673. err := writeDb.Model(&models.HisFapiaoRecord{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  674. return err
  675. }
  676. func FindFapiaoById(id int64) (*models.HisFapiaoRecord, error) {
  677. dealer := &models.HisFapiaoRecord{}
  678. err := readDb.Model(&models.HisFapiaoRecord{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  679. return dealer, err
  680. }
  681. func FindFapiaoByIsUse(org_id int64) (models.HisFapiaoRecord, error) {
  682. record := models.HisFapiaoRecord{}
  683. err := readDb.Model(&models.HisFapiaoRecord{}).Where("user_org_id = ? AND status = 1 AND is_use = 1", org_id).First(&record).Error
  684. return record, err
  685. }
  686. func FindHisYidiClearRecord(org_id int64) (records []*models.HisYidiClearRecord, err error) {
  687. err = readDb.Model(&models.HisYidiClearRecord{}).Where("user_org_id = ? AND status = 1", org_id).Find(&records).Error
  688. return
  689. }
  690. func GetHisYidiClearRecordById(org_id int64, id int64) (record models.HisYidiClearRecord, err error) {
  691. err = readDb.Model(&models.HisYidiClearRecord{}).Where("user_org_id = ? AND status = 1 AND id = ?", org_id, id).First(&record).Error
  692. return
  693. }
  694. func GetAllPatientTwo(org_id int64) (patients []*models.ChargePatientTwo, err error) {
  695. err = readDb.Model(&models.ChargePatientTwo{}).Where("user_org_id = ? AND status = 1", org_id).Find(&patients).Error
  696. return
  697. }
  698. func GetPatientChargeDetails(patient_id int64, org_id int64, start_time int64, end_time int64, keyword string, item_type int64, settle_type int64) (patients []*models.HisChargeOrderTwo, err error) {
  699. if len(keyword) == 0 {
  700. switch item_type {
  701. case 0:
  702. readDb2.Model(&models.HisChargeOrderTwo{}).Joins("join his_patient his on his.number = his_order.mdtrt_id AND his.patient_id = ").Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  703. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  704. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  705. return db.Preload("Drug", "status = 1").Where("status = 1")
  706. }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  707. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  708. return db.Select("id,project_name,unit").Where("status = 1 ")
  709. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  710. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  711. }).Where("status = 1 ")
  712. })
  713. }).Where("status = 1 AND settle_accounts_date >= ? AND settle_accounts_date <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type=?", start_time, end_time, org_id, patient_id, settle_type)
  714. break
  715. case 1:
  716. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  717. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  718. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  719. return db.Preload("Drug", "status = 1").Where("status = 1")
  720. }).Where("status = 1 AND advice_id > 0 AND project_id = 0")
  721. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type=?", start_time, end_time, org_id, patient_id, settle_type)
  722. break
  723. case 2:
  724. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  725. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  726. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  727. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  728. return db.Select("id,project_name,unit").Where("status = 1 ")
  729. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  730. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  731. }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ")
  732. })
  733. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type = ?", start_time, end_time, org_id, patient_id, settle_type)
  734. break
  735. case 3:
  736. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  737. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  738. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  739. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  740. return db.Select("id,project_name,unit").Where("status = 1 ")
  741. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  742. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  743. }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ")
  744. })
  745. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type = ?", start_time, end_time, org_id, patient_id, settle_type)
  746. break
  747. }
  748. } else {
  749. //keyword := "%" + keyword + "%"
  750. switch item_type {
  751. case 0:
  752. readDb2.Model(&models.HisChargeOrderTwo{}).Joins("join his_patient his on his.number = his_order.mdtrt_id AND his.patient_id = ").Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  753. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  754. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  755. return db.Preload("Drug", "status = 1").Where("status = 1")
  756. }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  757. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  758. return db.Select("id,project_name,unit").Where("status = 1 ")
  759. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  760. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  761. }).Where("status = 1 ")
  762. })
  763. }).Where("status = 1 AND settle_accounts_date >= ? AND settle_accounts_date <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type=?", start_time, end_time, org_id, patient_id, settle_type)
  764. break
  765. case 1:
  766. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  767. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  768. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  769. return db.Preload("Drug", "status = 1").Where("status = 1")
  770. }).Where("status = 1 AND advice_id > 0 AND project_id = 0")
  771. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type=?", start_time, end_time, org_id, patient_id, settle_type)
  772. break
  773. case 2:
  774. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  775. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  776. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  777. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  778. return db.Select("id,project_name,unit").Where("status = 1 ")
  779. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  780. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  781. }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ")
  782. })
  783. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type = ?", start_time, end_time, org_id, patient_id, settle_type)
  784. break
  785. case 3:
  786. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  787. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  788. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  789. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  790. return db.Select("id,project_name,unit").Where("status = 1 ")
  791. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  792. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  793. }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ")
  794. })
  795. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type = ?", start_time, end_time, org_id, patient_id, settle_type)
  796. break
  797. }
  798. }
  799. return
  800. }
  801. func GetPatientGather(patient_id int64, org_id int64, start_time int64, end_time int64, keyword string, item_type int64, settle_type int64) (patients []*models.HisChargeOrderTwo, err error) {
  802. if len(keyword) == 0 {
  803. switch item_type {
  804. case 0:
  805. readDb2.Model(&models.HisChargeOrderTwo{}).Joins("join his_patient his on his.number = his_order.mdtrt_id AND his.patient_id = ").Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  806. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  807. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  808. return db.Preload("Drug", "status = 1").Where("status = 1")
  809. }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  810. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  811. return db.Select("id,project_name,unit").Where("status = 1 ")
  812. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  813. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  814. }).Where("status = 1 ")
  815. })
  816. }).Where("status = 1 AND settle_accounts_date >= ? AND settle_accounts_date <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type=?", start_time, end_time, org_id, patient_id, settle_type)
  817. break
  818. case 1:
  819. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  820. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  821. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  822. return db.Preload("Drug", "status = 1").Where("status = 1")
  823. }).Where("status = 1 AND advice_id > 0 AND project_id = 0")
  824. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type=?", start_time, end_time, org_id, patient_id, settle_type)
  825. break
  826. case 2:
  827. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  828. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  829. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  830. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  831. return db.Select("id,project_name,unit").Where("status = 1 ")
  832. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  833. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  834. }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ")
  835. })
  836. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type = ?", start_time, end_time, org_id, patient_id, settle_type)
  837. break
  838. case 3:
  839. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  840. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  841. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  842. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  843. return db.Select("id,project_name,unit").Where("status = 1 ")
  844. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  845. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  846. }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ")
  847. })
  848. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type = ?", start_time, end_time, org_id, patient_id, settle_type)
  849. break
  850. }
  851. } else {
  852. //keyword := "%" + keyword + "%"
  853. switch item_type {
  854. case 0:
  855. readDb2.Model(&models.HisChargeOrderTwo{}).Joins("join his_patient his on his.number = his_order.mdtrt_id AND his.patient_id = ").Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  856. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  857. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  858. return db.Preload("Drug", "status = 1").Where("status = 1")
  859. }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  860. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  861. return db.Select("id,project_name,unit").Where("status = 1 ")
  862. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  863. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  864. }).Where("status = 1 ")
  865. })
  866. }).Where("status = 1 AND settle_accounts_date >= ? AND settle_accounts_date <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type=?", start_time, end_time, org_id, patient_id, settle_type)
  867. break
  868. case 1:
  869. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  870. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  871. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  872. return db.Preload("Drug", "status = 1").Where("status = 1")
  873. }).Where("status = 1 AND advice_id > 0 AND project_id = 0")
  874. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type=?", start_time, end_time, org_id, patient_id, settle_type)
  875. break
  876. case 2:
  877. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  878. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  879. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  880. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  881. return db.Select("id,project_name,unit").Where("status = 1 ")
  882. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  883. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  884. }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ")
  885. })
  886. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type = ?", start_time, end_time, org_id, patient_id, settle_type)
  887. break
  888. case 3:
  889. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  890. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  891. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  892. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  893. return db.Select("id,project_name,unit").Where("status = 1 ")
  894. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  895. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  896. }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ")
  897. })
  898. }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type = ?", start_time, end_time, org_id, patient_id, settle_type)
  899. break
  900. }
  901. }
  902. return
  903. }
  904. func GetAllChargeDetailsTwo(org_id int64, start_time int64, end_time int64) (patients []*models.HisChargeOrder, err error) {
  905. err = readDb2.Model(&models.HisChargeOrder{}).Preload("Patients", "status = 1").Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  906. return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").
  907. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  908. return db.Preload("Drug", "status = 1").Where("status = 1")
  909. }).
  910. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  911. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").
  912. Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  913. return db.Select("id,project_name,unit").Where("status = 1 ")
  914. }).
  915. Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  916. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  917. }).Where("status = 1 ")
  918. }).Where("status = 1")
  919. }).Where("status = 1 AND user_org_id = ? AND settle_accounts_date >= ? AND settle_accounts_date <= ? AND order_status = 2", org_id, start_time, end_time).Group("id").Find(&patients).Error
  920. return
  921. }
  922. func GetAllLisDataFor10191(org_id int64) (list []models.HisLabelPrintStatusInfo, err error) {
  923. err = readDb.Model(&models.HisLabelPrintStatusInfo{}).Preload("Patient", "status = 1").Where("user_org_id = ? AND status = 1 and apply_code = ''", org_id).Find(&list).Error
  924. return
  925. }
  926. func GetHisPrescriptionDrugDetails(org_id int64, start_time_str string, end_time_str string, start_time int64, end_time int64, patient_id int64, item_type int64) (patients []*models.NewPDetail, err error) {
  927. db := readDb.Table("his_doctor_advice_info as oi").Select("oo.mdtrt_id as mdtrt_id,oi.advice_doctor as advice_doctor,oo.setl_time as setl_time, drug.id as item_id,1 as p_type,oi.id as advice_id,0 as project_id,pp.id as p_id,pp.name as p_name,FROM_UNIXTIME(o.`record_date`, '%Y-%m-%d') as record_date, drug.drug_type as item_cost_type,o.med_type as med_type,oi.`prescribing_number` as cnt,oi.`price` as pric,drug.drug_name as item_name,CONCAT(drug.dose, \"\", drug.dose_unit, \"*\", drug.min_number, \"\", drug.min_unit, \"/\", drug.max_unit) as spec,o.order_status as order_status,oi.`prescribing_number_unit` as unit").
  928. Joins("JOIN xt_base_drug drug ON oi.drug_id = drug.id ")
  929. if patient_id == 0 {
  930. db = db.Joins("JOIN xt_patients pp on oi.patient_id = pp.id")
  931. } else {
  932. db = db.Joins("JOIN xt_patients pp on oi.patient_id = pp.id and pp.id = ?", patient_id)
  933. }
  934. if item_type == 0 {
  935. db = db.Joins("JOIN `his_prescription` o ON oi.`prescription_id` = o.id and FROM_UNIXTIME(o.record_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.status = 1", start_time_str, end_time_str)
  936. } else {
  937. if item_type == 2 {
  938. db = db.Joins("JOIN `his_prescription` o ON oi.`prescription_id` = o.id and FROM_UNIXTIME(o.record_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.status = 1 and o.order_status = 2", start_time_str, end_time_str)
  939. } else {
  940. db = db.Joins("JOIN `his_prescription` o ON oi.`prescription_id` = o.id and FROM_UNIXTIME(o.record_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.status = 1 and o.order_status <> 2", start_time_str, end_time_str)
  941. }
  942. }
  943. db = db.Joins("left JOIN `his_order` oo on oo.`number` = o.`batch_number` and oo.user_org_id = ?", org_id)
  944. err = db.Where(" oi.user_org_id = ? AND o.record_date >= ? AND o.record_date <= ? AND oi.status = 1", org_id, start_time, end_time).Group("oi.id").Scan(&patients).Error
  945. return
  946. }
  947. func GetHisPrescriptionProjectDetails(org_id int64, start_time_str string, end_time_str string, start_time int64, end_time int64, patient_id int64, item_type int64) (patients []*models.NewPDetail, err error) {
  948. db := readDb.Table("`his_prescription_project` AS oi").Select("oo.mdtrt_id as mdtrt_id,oi.doctor as advice_doctor,oo.setl_time as setl_time,(case oi.type\n\t\t\tWHEN 2 THEN\n\t\t\tproject.id\n\t\t\tWHEN 3 THEN\n\t\t\tgood.id END) AS item_id,oi.type as p_type,oi.id as project_id,0 as advice_id,pp.id as p_id,(case oi.type\n\t\t\tWHEN 2 THEN\n\t\t\tproject.`cost_classify`\n\t\t\tWHEN 3 THEN\n\t\t\t-100 END) AS item_cost_type,\n\t o.med_type as med_type,\n\t oi.count AS cnt,\n \t oi.price AS pric,\n \t pp.name AS p_name, \n \tFROM_UNIXTIME(o.`record_date`,'%Y-%m-%d') as record_date,\n\t\t (case oi.type\n\tWHEN 2 THEN\n\tproject.project_name\n\tWHEN 3 THEN\n\tgood.good_name END) AS item_name,\n\t\t (case oi.type\n\tWHEN 2 THEN\n\t\"\"\n\tWHEN 3 THEN\n\tgood.`specification_name` END) AS spec,\n\toi.`unit` as unit,o.order_status as order_status")
  949. db = db.Joins("LEFT JOIN xt_his_project project\n\tON oi.project_id = project.id").
  950. Joins("LEFT JOIN xt_good_information good\n\tON oi.project_id = good.id\n")
  951. //Joins("JOIN his_order ord ON o.batch_number = ord.number ")
  952. if patient_id > 0 {
  953. db = db.Joins("JOIN xt_patients pp on oi.patient_id = pp.id and pp.id = ?", patient_id)
  954. } else {
  955. db = db.Joins("JOIN xt_patients pp on oi.patient_id = pp.id")
  956. }
  957. if item_type > 0 {
  958. if item_type == 2 {
  959. db = db.Joins("JOIN `his_prescription` o ON oi.`prescription_id` = o.id and FROM_UNIXTIME(o.record_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.status = 1 and o.order_status = 2", start_time_str, end_time_str)
  960. } else {
  961. db = db.Joins("JOIN `his_prescription` o ON oi.`prescription_id` = o.id and FROM_UNIXTIME(o.record_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.status = 1 and o.order_status <> 2", start_time_str, end_time_str)
  962. }
  963. } else {
  964. db = db.Joins("JOIN `his_prescription` o ON oi.`prescription_id` = o.id and FROM_UNIXTIME(o.record_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.status = 1", start_time_str, end_time_str)
  965. }
  966. db = db.Joins("left JOIN `his_order` oo on oo.`number` = o.`batch_number` and oo.user_org_id = ?", org_id)
  967. err = db.Where(" oi.user_org_id = ? AND o.record_date >= ? AND o.record_date <= ? AND oi.status = 1", org_id, start_time, end_time).Group("oi.id").Scan(&patients).Error
  968. return
  969. }