his_charge_service.go 57KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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) (patients []*models.NewChargeDetail, err error) {
  140. if len(keyword) == 0 {
  141. switch item_type {
  142. case 0:
  143. err = readDb.Table("his_order_info as oi").Select("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").
  144. 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).
  145. Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id").
  146. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  147. Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id").
  148. 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
  149. break
  150. case 1:
  151. err = readDb.Table("his_order_info as oi").Select("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. break
  158. }
  159. } else {
  160. keyword := "%" + keyword + "%"
  161. switch item_type {
  162. case 0:
  163. err = readDb.Table("his_order_info as oi").Select("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").
  164. 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).
  165. Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id").
  166. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  167. Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id").
  168. 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
  169. break
  170. case 1:
  171. err = readDb.Table("his_order_info as oi").Select("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").
  172. 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).
  173. Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id").
  174. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  175. Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id").
  176. 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
  177. break
  178. }
  179. }
  180. return
  181. }
  182. func GetNewAllPatientProjectAndGoodChargeDetails(org_id int64, start_time_str string, end_time_str string, start_time int64, end_time int64, keyword string, item_type int64) (patients []*models.NewChargeDetail, err error) {
  183. if len(keyword) == 0 {
  184. switch item_type {
  185. case 0:
  186. err = readDb.Table("his_order_info as oi").Select("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").
  187. 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).
  188. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id ").
  189. Joins(" left join xt_his_project project on p.project_id = project.id").
  190. Joins(" left join xt_good_information good on p.project_id = good.id").
  191. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  192. 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
  193. //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 {
  194. // return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id").
  195. // Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  196. // 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").
  197. // Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  198. // return db.Preload("Drug", "status = 1").Where("status = 1")
  199. // }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  200. // return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  201. // return db.Select("id,project_name,unit").Where("status = 1 ")
  202. // }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  203. // return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  204. // }).Where("status = 1 ")
  205. // }).Where("status = 1")
  206. // }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id)
  207. //}).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  208. break
  209. case 2:
  210. err = readDb.Table("his_order_info as oi").Select("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").
  211. 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).
  212. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  213. Joins("left join xt_his_project project on p.project_id = project.id").
  214. Joins("left join xt_good_information good on p.project_id = good.id").
  215. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  216. 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
  217. break
  218. case 3:
  219. err = readDb.Table("his_order_info as oi").Select("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").
  220. 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).
  221. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 3").
  222. Joins("left join xt_his_project project on p.project_id = project.id").
  223. Joins("left join xt_good_information good on p.project_id = good.id").
  224. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id").
  225. 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
  226. break
  227. }
  228. } else {
  229. keyword := "%" + keyword + "%"
  230. switch item_type {
  231. case 0:
  232. err = readDb.Table("his_order_info as oi").Select("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").
  233. 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).
  234. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id ").
  235. Joins("left join xt_his_project project on p.project_id = project.id").
  236. Joins("left join xt_good_information good on p.project_id = good.id").
  237. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  238. 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
  239. break
  240. case 2:
  241. err = readDb.Table("his_order_info as oi").Select("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").
  242. 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).
  243. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2").
  244. Joins("left join xt_his_project project on p.project_id = project.id").
  245. Joins("left join xt_good_information good on p.project_id = good.id").
  246. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  247. 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
  248. break
  249. case 3:
  250. err = readDb.Table("his_order_info as oi").Select("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").
  251. 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).
  252. Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 3").
  253. Joins("left join xt_his_project project on p.project_id = project.id").
  254. Joins("left join xt_good_information good on p.project_id = good.id").
  255. Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword).
  256. 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
  257. break
  258. }
  259. }
  260. return
  261. }
  262. func GetAllPatientChargeSettle(org_id int64, start_time int64, end_time int64, keyword string, item_type int64) (patients []*models.SettlePatient, err error) {
  263. if len(keyword) == 0 {
  264. switch item_type {
  265. case 0:
  266. 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 {
  267. return db.Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id).Order("ctime")
  268. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  269. break
  270. case 1:
  271. 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 {
  272. return db.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")
  273. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  274. break
  275. case 2:
  276. 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 {
  277. return db.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")
  278. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  279. break
  280. case 3:
  281. 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 {
  282. return db.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")
  283. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  284. break
  285. case 4:
  286. 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 {
  287. return db.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")
  288. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  289. break
  290. case 5:
  291. 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 {
  292. return db.Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 ", start_time, end_time, org_id).Order("ctime")
  293. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  294. break
  295. case 6:
  296. 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 {
  297. return db.Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id).Order("ctime")
  298. }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error
  299. break
  300. }
  301. } else {
  302. keyword = "%" + keyword + "%"
  303. switch item_type {
  304. case 0:
  305. 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 {
  306. return db.Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id).Order("ctime")
  307. }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error
  308. break
  309. case 1:
  310. 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 {
  311. return db.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")
  312. }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error
  313. break
  314. case 2:
  315. 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 {
  316. return db.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")
  317. }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error
  318. break
  319. case 3:
  320. 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 {
  321. return db.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")
  322. }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error
  323. break
  324. case 4:
  325. 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 {
  326. return db.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")
  327. }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error
  328. break
  329. }
  330. }
  331. return
  332. }
  333. 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) {
  334. offset := (page - 1) * limit
  335. db := readDb.Model(&models.HisLabelPrintInfo{})
  336. db = db.Preload("HisProjectTeam", "status = 1")
  337. if is_print > 0 {
  338. db = db.Where("is_print = ?", is_print)
  339. }
  340. if len(keywors) > 0 {
  341. keywors = "%" + keywors + "%"
  342. db = db.Where("patient_name Like ?", keywors)
  343. }
  344. if tube_color > 0 {
  345. 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)
  346. //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)
  347. }
  348. 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
  349. return
  350. }
  351. 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) {
  352. offset := (page - 1) * limit
  353. db := readDb.Model(&models.HisLabelPrintInfo{})
  354. db = db.Preload("HisProjectTeam", "status = 1")
  355. if is_print > 0 {
  356. db = db.Where("is_print = ?", is_print)
  357. }
  358. if len(keywors) > 0 {
  359. keywors = "%" + keywors + "%"
  360. db = db.Where("patient_name Like ?", keywors)
  361. }
  362. if tube_color > 0 {
  363. //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)
  364. 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)
  365. }
  366. 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
  367. return
  368. }
  369. func GetLabelPrintListThree(user_org_id int64, record_time int64, is_print int64, keywors string, tube_color int64) (labels []*models.HisLabelPrintInfo, total int64, err error) {
  370. db := readDb.Model(&models.HisLabelPrintInfo{})
  371. db = db.Preload("HisProjectTeam", "status = 1")
  372. if is_print > 0 {
  373. db = db.Where("is_print = ?", is_print)
  374. }
  375. if len(keywors) > 0 {
  376. keywors = "%" + keywors + "%"
  377. db = db.Where("patient_name Like ?", keywors)
  378. }
  379. //if tube_color > 0 {
  380. // 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)
  381. //}
  382. 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
  383. return
  384. }
  385. //func GetLabelPrintListFour(user_org_id int64, record_time int64, is_print int64, keywors string, tube_color int64) (labels []*models.HisLabelPrintInfo, total int64, err error) {
  386. // db := readDb.Model(&models.HisLabelPrintInfo{})
  387. // db = db.Preload("HisProjectTeam", "status = 1")
  388. //
  389. // if is_print > 0 {
  390. // db = db.Where("is_print = ?", is_print)
  391. // }
  392. // if len(keywors) > 0 {
  393. // keywors = "%" + keywors + "%"
  394. // db = db.Where("patient_name Like ?", keywors)
  395. //
  396. // }
  397. // //if tube_color > 0 {
  398. // // //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)
  399. // // 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)
  400. // //}
  401. //
  402. // 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
  403. // return
  404. //}
  405. func GetLabelPrintInfo(id int64) (labels models.HisLabelPrintInfo, err error) {
  406. db := readDb.Model(&models.HisLabelPrintInfo{})
  407. err = db.Where("id = ?", id).First(&labels).Error
  408. return
  409. }
  410. func AddSigleFapiaoRecord(dealer *models.HisFapiaoRecord) (err error, record *models.HisFapiaoRecord) {
  411. err = writeDb.Create(&dealer).Error
  412. return err, dealer
  413. }
  414. func ModifyFapiao(mesick *models.HisFapiaoRecord) error {
  415. err := writeDb.Save(&mesick).Error
  416. return err
  417. }
  418. func UpdateFapiaoIsUse(org_id int64) {
  419. writeDb.Model(&models.HisFapiaoRecord{}).Where("user_org_id = ?", org_id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "is_use": 0})
  420. }
  421. func FindAllFapiaoList(orgId int64, page int64, limit int64) (list []*models.HisFapiaoRecord, total int64, err error) {
  422. offset := (page - 1) * limit
  423. db := readDb.Model(&models.HisFapiaoRecord{})
  424. db = db.Where("user_org_id = ? AND status = 1", orgId)
  425. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  426. return
  427. }
  428. func DeleteFapiaoById(id int64) error {
  429. err := writeDb.Model(&models.HisFapiaoRecord{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  430. return err
  431. }
  432. func FindFapiaoById(id int64) (*models.HisFapiaoRecord, error) {
  433. dealer := &models.HisFapiaoRecord{}
  434. err := readDb.Model(&models.HisFapiaoRecord{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  435. return dealer, err
  436. }
  437. func FindFapiaoByIsUse(org_id int64) (models.HisFapiaoRecord, error) {
  438. record := models.HisFapiaoRecord{}
  439. err := readDb.Model(&models.HisFapiaoRecord{}).Where("user_org_id = ? AND status = 1 AND is_use = 1", org_id).First(&record).Error
  440. return record, err
  441. }
  442. func FindHisYidiClearRecord(org_id int64) (records []*models.HisYidiClearRecord, err error) {
  443. err = readDb.Model(&models.HisYidiClearRecord{}).Where("user_org_id = ? AND status = 1", org_id).Find(&records).Error
  444. return
  445. }
  446. func GetHisYidiClearRecordById(org_id int64, id int64) (record models.HisYidiClearRecord, err error) {
  447. err = readDb.Model(&models.HisYidiClearRecord{}).Where("user_org_id = ? AND status = 1 AND id = ?", org_id, id).First(&record).Error
  448. return
  449. }
  450. func GetAllPatientTwo(org_id int64) (patients []*models.ChargePatientTwo, err error) {
  451. err = readDb.Model(&models.ChargePatientTwo{}).Where("user_org_id = ? AND status = 1", org_id).Find(&patients).Error
  452. return
  453. }
  454. 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) {
  455. if len(keyword) == 0 {
  456. switch item_type {
  457. case 0:
  458. 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 {
  459. 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").
  460. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  461. return db.Preload("Drug", "status = 1").Where("status = 1")
  462. }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  463. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  464. return db.Select("id,project_name,unit").Where("status = 1 ")
  465. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  466. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  467. }).Where("status = 1 ")
  468. })
  469. }).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)
  470. break
  471. case 1:
  472. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  473. 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").
  474. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  475. return db.Preload("Drug", "status = 1").Where("status = 1")
  476. }).Where("status = 1 AND advice_id > 0 AND project_id = 0")
  477. }).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)
  478. break
  479. case 2:
  480. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  481. 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").
  482. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  483. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  484. return db.Select("id,project_name,unit").Where("status = 1 ")
  485. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  486. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  487. }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ")
  488. })
  489. }).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)
  490. break
  491. case 3:
  492. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  493. 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").
  494. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  495. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  496. return db.Select("id,project_name,unit").Where("status = 1 ")
  497. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  498. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  499. }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ")
  500. })
  501. }).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)
  502. break
  503. }
  504. } else {
  505. //keyword := "%" + keyword + "%"
  506. switch item_type {
  507. case 0:
  508. 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 {
  509. 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").
  510. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  511. return db.Preload("Drug", "status = 1").Where("status = 1")
  512. }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  513. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  514. return db.Select("id,project_name,unit").Where("status = 1 ")
  515. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  516. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  517. }).Where("status = 1 ")
  518. })
  519. }).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)
  520. break
  521. case 1:
  522. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  523. 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").
  524. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  525. return db.Preload("Drug", "status = 1").Where("status = 1")
  526. }).Where("status = 1 AND advice_id > 0 AND project_id = 0")
  527. }).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)
  528. break
  529. case 2:
  530. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  531. 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").
  532. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  533. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  534. return db.Select("id,project_name,unit").Where("status = 1 ")
  535. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  536. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  537. }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ")
  538. })
  539. }).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)
  540. break
  541. case 3:
  542. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  543. 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").
  544. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  545. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  546. return db.Select("id,project_name,unit").Where("status = 1 ")
  547. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  548. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  549. }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ")
  550. })
  551. }).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)
  552. break
  553. }
  554. }
  555. return
  556. }
  557. 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) {
  558. if len(keyword) == 0 {
  559. switch item_type {
  560. case 0:
  561. 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 {
  562. 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").
  563. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  564. return db.Preload("Drug", "status = 1").Where("status = 1")
  565. }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  566. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  567. return db.Select("id,project_name,unit").Where("status = 1 ")
  568. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  569. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  570. }).Where("status = 1 ")
  571. })
  572. }).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)
  573. break
  574. case 1:
  575. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  576. 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").
  577. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  578. return db.Preload("Drug", "status = 1").Where("status = 1")
  579. }).Where("status = 1 AND advice_id > 0 AND project_id = 0")
  580. }).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)
  581. break
  582. case 2:
  583. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  584. 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").
  585. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  586. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  587. return db.Select("id,project_name,unit").Where("status = 1 ")
  588. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  589. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  590. }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ")
  591. })
  592. }).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)
  593. break
  594. case 3:
  595. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  596. 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").
  597. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  598. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  599. return db.Select("id,project_name,unit").Where("status = 1 ")
  600. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  601. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  602. }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ")
  603. })
  604. }).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)
  605. break
  606. }
  607. } else {
  608. //keyword := "%" + keyword + "%"
  609. switch item_type {
  610. case 0:
  611. 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 {
  612. 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").
  613. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  614. return db.Preload("Drug", "status = 1").Where("status = 1")
  615. }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  616. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  617. return db.Select("id,project_name,unit").Where("status = 1 ")
  618. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  619. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  620. }).Where("status = 1 ")
  621. })
  622. }).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)
  623. break
  624. case 1:
  625. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  626. 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").
  627. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  628. return db.Preload("Drug", "status = 1").Where("status = 1")
  629. }).Where("status = 1 AND advice_id > 0 AND project_id = 0")
  630. }).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)
  631. break
  632. case 2:
  633. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  634. 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").
  635. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  636. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  637. return db.Select("id,project_name,unit").Where("status = 1 ")
  638. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  639. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  640. }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ")
  641. })
  642. }).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)
  643. break
  644. case 3:
  645. readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  646. 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").
  647. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  648. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  649. return db.Select("id,project_name,unit").Where("status = 1 ")
  650. }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  651. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  652. }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ")
  653. })
  654. }).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)
  655. break
  656. }
  657. }
  658. return
  659. }
  660. func GetAllChargeDetailsTwo(org_id int64, start_time int64, end_time int64) (patients []*models.HisChargeOrder, err error) {
  661. err = readDb2.Model(&models.HisChargeOrder{}).Preload("Patients", "status = 1").Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
  662. 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").
  663. Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  664. return db.Preload("Drug", "status = 1").Where("status = 1")
  665. }).
  666. Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB {
  667. return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").
  668. Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
  669. return db.Select("id,project_name,unit").Where("status = 1 ")
  670. }).
  671. Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
  672. return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ")
  673. }).Where("status = 1 ")
  674. }).Where("status = 1")
  675. }).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
  676. return
  677. }
  678. func GetAllLisDataFor10191(org_id int64) (list []models.HisLabelPrintStatusInfo, err error) {
  679. err = readDb.Model(&models.HisLabelPrintStatusInfo{}).Preload("Patient", "status = 1").Where("user_org_id = ? AND status = 1 and apply_code = ''", org_id).Find(&list).Error
  680. return
  681. }