1. 挂号微信收入明细

  1. -- WANTED-1283 门诊查询统计报表-挂号微信收入明细
  2. select
  3. p.name as patientName, -- 患者姓名
  4. p.birthday, -- 出生日期
  5. bl.outpatient_number as blNumber, -- 病历号
  6. case p.sex WHEN 1 THEN '男' WHEN 2 THEN '女' END as sex, -- 性别
  7. round((f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount,2) AS guaHaoMoney,
  8. h.type,
  9. h.pay_method,
  10. i.name as payMethod,
  11. h.flow_fee as weiXinMoney, -- 微信到账
  12. h.charge as weiXinCharge, -- 微信手续
  13. h.pay_time as payTime, -- 缴费时间
  14. a.create_time, -- 挂号时间
  15. a.dept_name, -- 挂号科室名称
  16. a.appointment_doctor_name as doctorName, -- 挂号医生
  17. (select t.docname from (
  18. SELECT u.id AS docid,u1.property_value AS docname
  19. FROM thc_warehouse.staff_record u
  20. LEFT JOIN thc_warehouse.staff_record_property u1 ON u1.property_code = 'SXX000083' AND u.id = u1.staff_record_id
  21. ) t where t.docid = a.creator) AS creater,
  22. CASE a.subsequent_visit
  23. WHEN 0 THEN '初诊'
  24. WHEN 1 THEN '复诊'
  25. WHEN 2 THEN '转诊'
  26. WHEN 3 THEN '急诊'
  27. WHEN 4 THEN '体检'
  28. WHEN 5 THEN '简易'
  29. WHEN 6 THEN '疫苗'
  30. WHEN 7 THEN '团队体检'
  31. END AS isReVisit -- 初复诊
  32. from `thc_arrange`.`bpm_appointment` a
  33. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  34. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  35. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  36. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  37. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  38. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  39. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  40.  
  41. INNER JOIN `thc_passport`.`patient` p on a.patient_id = p.id
  42.  
  43. left join `thc_passport`.`patient_org` bl on a.patient_id = bl.patient_id
  44. left JOIN `thc_rcm`.`pay_payment` i on h.pay_method = i.value and g.clinicID = i.clinicid
  45. where 1=1
  46. and a.del_flag = 0
  47. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  48. and e.itemClass = 1 and e.returnFlag is NULL
  49. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  50. and h.type = 1 and h.pay_method = 4 -- 微信支付
  51. ${if(len(creator) == 0,"","and a.creator = '" + creator + "'")}
  52. ${if(len(dept) == 0,"","and a.dept_id = '" + dept + "'")}
  53. ${if(len(startTime) == 0,"","and h.pay_time >= '" + startTime + "'")}
  54. ${if(len(endTime) == 0,"","and h.pay_time <= '" + endTime + "'")}
  55.  
  56. ORDER BY g.createtime desc,a.create_time desc

2.挂号患者信息查询

  1. -- -----------------------
  2. -- 患者信息查询
  3. -- -----------------------
  4. select DISTINCT
  5. CASE a.appointment_state WHEN -1 THEN '是' ELSE '否' END AS returnFlag, -- 退号标志
  6. CASE a.is_appoint_resource WHEN 0 THEN '否' WHEN 1 THEN '是' END as isAppointResource, -- 指定标志
  7. a.create_time as guaHaoTime, -- 挂号时间
  8. p.create_time as createDocTime, -- 建档时间
  9. -- 挂号实收金额
  10. round((f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount,2) AS realFee,
  11. round(t1.realFee,2) as menZenRealMoney, -- 门诊实收金额
  12. j.outpatient_number as blNumber, -- 病历号
  13. p.name as patientName, -- 患者姓名
  14. a.dept_name, -- 科室名称
  15. a.appointment_doctor_name as doctorName, -- 医生
  16. CASE a.subsequent_visit -- 初复诊
  17. WHEN 0 THEN '初诊'
  18. WHEN 1 THEN '复诊'
  19. WHEN 2 THEN '转诊'
  20. WHEN 3 THEN '急诊'
  21. WHEN 4 THEN '体检'
  22. WHEN 5 THEN '简易'
  23. WHEN 6 THEN '疫苗'
  24. WHEN 7 THEN '团队体检'
  25. END AS isReVisit, -- 初复诊
  26. channel.name as sourceName, -- 信息(渠道)来源
  27. p.birthday, -- 出生日期
  28. if(i.id_no is null, if(i.other_type is null, null,
  29. (select e.name from `thc_warehouse`.`sys_type_info` e
  30. JOIN `thc_warehouse`.`sys_type` f ON e.sys_type_id = f.id
  31. WHERE f.`code` = 'THC_WH_PERSON_CARD' and e.value = i.other_type)
  32. ), '身份证') as cardType, -- 证件类型
  33. if(i.id_no is null,i.other_no,i.id_no) as cardNo, -- 证件号码
  34. CONCAT(t2.province,t2.city,t2.county,t2.addressDetail) as liveAddr, -- 住址
  35. CONCAT(t2.province2,t2.city2,t2.county2,t2.addressDetail2) as bornAddr, -- 户籍
  36. if(locate('区',t2.county2) > 0,t2.county2,'') as qu, --
  37. a.patient_phone,
  38. (select t.docname from (
  39. SELECT u.id AS docid,u1.property_value AS docname
  40. FROM thc_warehouse.staff_record u
  41. LEFT JOIN thc_warehouse.staff_record_property u1 ON u1.property_code = 'SXX000083' AND u.id = u1.staff_record_id
  42. ) t where t.docid = a.creator) AS creater,
  43. -- a.appointment_starttime,
  44. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime, -- 预约时间
  45. -- CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  46. a.referrer_dept_name, -- 爱院科室
  47. a.referrer_name, -- 爱院人
  48. a.description -- 备注
  49. FROM `thc_arrange`.`bpm_appointment` a
  50. INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  51. INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
  52. INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  53. INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  54. INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
  55. INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  56. INNER JOIN `thc_passport`.`patient` p on a.patient_id = p.id
  57. INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  58. LEFT JOIN `thc_c_union`.`member_channel` channel on a.channel_id = channel.id
  59. LEFT JOIN `thc_passport`.`contact` i on a.patient_id = i.patient_id
  60. LEFT JOIN `thc_passport`.`patient_org` j on a.patient_id = j.patient_id
  61.  
  62. left join
  63. (
  64. select
  65. aa.id,
  66. round((ff.preFee - ff.discountFee - ff.promotionBenefitFee - ff.couponFee - ff.itemBenefitFee - ff.memberCardBenefitFee - ff.itemComInvBenefitFee) * ff.discount,2) AS realFee
  67. from `thc_arrange`.`bpm_appointment` aa
  68. inner join `thc_sob`.`bpm_service_order` bb on aa.orderId = bb.id
  69. inner join `thc_sob`.`bpm_service_order_item` cc on cc.service_order_id = bb.id and cc.id = aa.`order_item_id`
  70. inner join `thc_rcm`.`Cs_AccountBill` dd on dd.orderID = bb.id and dd.`isDelete` = 0 and dd.orderSource = 1 and dd.orderType = 3 and dd.returnFlag = 0
  71. inner join `thc_rcm`.`Cs_AccountBillDetail` ee on dd.id = ee.AccountBillId and ee.itemClass = 1 and ee.returnFlag is NULL
  72. inner join `thc_rcm`.`Cs_SettlementDetail` ff on ff.accountBillID = dd.id and ff.accountBillDetailID = ee.id
  73. inner join `thc_rcm`.`Cs_Settlement` gg on gg.id = ff.settlementID and gg.`isDelete` = 0 and gg.returnFlag = 0
  74. and gg.settlementType=1 -- 门诊收费
  75. where 1=1 and aa.del_flag = 0
  76. ) t1 on a.id = t1.id
  77.  
  78. left join
  79. (
  80. select
  81. aa.id,
  82. -- 住址
  83. (select city.name from `thc_passport`.`city` where city.id =
  84. (if(p.address is null,null, TRIM(BOTH '"' FROM JSON_EXTRACT(p.address,'$.province'))))) as province,
  85. (select city.name from `thc_passport`.`city` where city.id =
  86. (if(p.address is null,null, TRIM(BOTH '"' FROM JSON_EXTRACT(p.address,'$.city'))))) as city,
  87. (select city.name from `thc_passport`.`city` where city.id =
  88. (if(p.address is null,null, TRIM(BOTH '"' FROM JSON_EXTRACT(p.address,'$.county'))))) as county,
  89.  
  90. if(p.address is null,null, TRIM(BOTH '"' FROM JSON_EXTRACT(p.address,'$.addressDetail'))) as addressDetail,
  91.  
  92. -- 户籍
  93. (select city.name from `thc_passport`.`city` where city.id =
  94.  
  95. (if(p.household is null,null, TRIM(BOTH '"' FROM JSON_EXTRACT(p.household,'$.province'))))) as province2,
  96. (select city.name from `thc_passport`.`city` where city.id =
  97. (if(p.household is null,null, TRIM(BOTH '"' FROM JSON_EXTRACT(p.household,'$.city'))))) as city2,
  98. (select city.name from `thc_passport`.`city` where city.id =
  99. (if(p.household is null,null, TRIM(BOTH '"' FROM JSON_EXTRACT(p.household,'$.county'))))) as county2,
  100. if(p.household is null,null, TRIM(BOTH '"' FROM JSON_EXTRACT(p.household,'$.addressDetail'))) as addressDetail2,
  101. round((ff.preFee - ff.discountFee - ff.promotionBenefitFee - ff.couponFee - ff.itemBenefitFee - ff.memberCardBenefitFee - ff.itemComInvBenefitFee) * ff.discount,2) AS realFee
  102. from `thc_arrange`.`bpm_appointment` aa
  103. inner join `thc_sob`.`bpm_service_order` bb on aa.orderId = bb.id
  104. inner join `thc_sob`.`bpm_service_order_item` cc on cc.service_order_id = bb.id and cc.id = aa.`order_item_id`
  105. inner join `thc_rcm`.`Cs_AccountBill` dd on dd.orderID = bb.id and dd.`isDelete` = 0 and dd.orderSource = 1 and dd.orderType = 3 and dd.returnFlag = 0
  106. inner join `thc_rcm`.`Cs_AccountBillDetail` ee on dd.id = ee.AccountBillId and ee.itemClass = 1 and ee.returnFlag is NULL
  107. inner join `thc_rcm`.`Cs_SettlementDetail` ff on ff.accountBillID = dd.id and ff.accountBillDetailID = ee.id
  108. inner join `thc_rcm`.`Cs_Settlement` gg on gg.id = ff.settlementID and gg.`isDelete` = 0 and gg.returnFlag = 0 -- 门诊挂号
  109. and gg.settlementType=2 -- 非挂号消费类型
  110. inner join `thc_passport`.`patient` p on aa.patient_id = p.id
  111. where 1=1 and aa.del_flag = 0
  112.  
  113. ) t2 on a.id = t2.id
  114.  
  115. WHERE 1=1
  116. AND a.del_flag = 0
  117. AND d.isDelete = 0
  118. AND d.orderSource = 1
  119. AND d.orderType = 3
  120. AND d.returnFlag = 0
  121. AND e.itemClass = 1
  122. AND e.returnFlag is NULL
  123. AND g.settlementType=2
  124. AND g.isDelete = 0
  125. AND g.returnFlag = 0 -- 门诊挂号
  126.  
  127. ${if(len(startTime) == 0,"","AND a.create_time >= '" + startTime + "'")}
  128. ${if(len(endTime) == 0,"","AND a.create_time <= '" + endTime + "'")}
  129. -- 爱院标志
  130. ${if(loveYuan == 0,"and a.appointment_state is null","")}
  131. ${if(loveYuan == 1,"and a.appointment_state is not null","")}
  132.  
  133. ${if(len(deptId) == 0,"","AND a.dept_id = '" + deptId + "'")} -- 部门
  134. ${if(len(doctor) == 0,"","AND a.appointment_doctor_id = '" + doctor + "'")} -- 医生
  135. ${if(len(channelId) == 0,"","AND a.channel_id = '" + channelId + "'")} -- 信息来源
  136. ${if(len(patientName) == 0,"","AND p.name like '%" + patientName + "%'")} -- 姓名
  137. ${if(len(blNumber) == 0,"","AND j.outpatient_number like '%" + blNumber + "%'")} -- 病历号
  138. ${if(len(phone) == 0,"","AND a.patient_phone like '%" + phone + "%'")} -- 电话号
  139. ${if(len(subVisit) == 0,"","AND a.subsequent_visit = '" + subVisit + "'")} -- 初复诊
  140. ${if(len(doctor) == 0,"","and a.appointment_doctor_id = '" + doctor + "'")} -- 接诊员
  141. ${if(isReturn == -1,"and a.appointment_state = -1","")} -- 退号标志
  142. ${if(isReturn == 0,"and a.appointment_state != -1","")} -- 退号标志

 3.挂号按来源统计

  1. select
  2. channel.name as name,
  3. DATE_FORMAT(a.appointment_date,${if(dateType == 1,"'%Y-%m'","'%Y-%m-%d'")}) as date, -- 按日期还是月份展示
  4. IFNULL(count(1),0) as num
  5. from `thc_arrange`.`bpm_appointment` a
  6. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  7. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  8. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  9. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  10. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  11. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  12. left join `thc_c_union`.`member_channel` channel on a.channel_id = channel.id
  13. where 1=1
  14. and a.del_flag = 0
  15. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  16. and e.itemClass = 1 and e.returnFlag is NULL
  17. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  18. and g.id in (
  19. SELECT DISTINCT g.id
  20. from `thc_arrange`.`bpm_appointment` a
  21. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  22. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  23. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  24. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  25. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  26. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  27. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  28. where 1=1
  29. and a.del_flag = 0
  30. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  31. and e.itemClass = 1 and e.returnFlag is NULL
  32. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  33. and h.type = 1 and h.pay_state = 1
  34. )
  35.  
  36. and DATE_FORMAT(a.appointment_date,'%Y-%m-%d') >= DATE_FORMAT(${if(len(startTime) == 0, "'0000-01-01'" , "'" + startTime + "'" )},'%Y-%m-%d')
  37. and DATE_FORMAT(a.appointment_date,'%Y-%m-%d') <= DATE_FORMAT(${if(len(endTime) == 0,"'9999-01-01'","'" + endTime + "'")},'%Y-%m-%d')
  38. ${if(len(subVisit) == 0,"","and a.subsequent_visit = '" + subVisit + "'")}
  39. -- and channel.name is not null
  40. group by date_format(a.appointment_date, ${if(dateType == 1,"'%Y-%m'","'%Y-%m-%d'")}), channel.name -- 按日期还是月份分组查询
  41. order by a.appointment_date DESC, data_source asc

3.1有交易流水的条件

  1. AND g.id IN (
  2. SELECT DISTINCT g.id
  3. FROM `thc_arrange`.`bpm_appointment` a
  4. INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  5. INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
  6. INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  7. INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  8. INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
  9. INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  10. INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  11. WHERE 1=1
  12. AND a.del_flag = 0
  13. AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
  14. AND e.itemClass = 1 AND e.returnFlag is NULL
  15. AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
  16. AND h.type = 1
  17. )

4.挂号按医生统计

  1. select
  2. t0.deptName,
  3. t0.doctorName,
  4. t0.deptId,
  5. t0.doctorId,
  6. t0.startTime,
  7. t0.endTime,
  8. IFNULL(t0.num,0) as num0,
  9. IFNULL(t1.num,0) as num1,
  10. IFNULL(t2.num,0) as num2,
  11. IFNULL(t3.num,0) as num3,
  12. IFNULL(t4.num,0) as num4,
  13. IFNULL(t5.num,0) as num5,
  14. IFNULL(t6.num,0) as num6,
  15. IFNULL(t7.num,0) as num7,
  16. IFNULL(t8.num,0) as num8,
  17. IFNULL(t9.num,0) as num9,
  18. IFNULL(t10.num,0) as num10,
  19. IFNULL(t11.num,0) as num11,
  20. IFNULL(t12.num,0) as num12,
  21. IFNULL(t13.num,0) as num13,
  22. IFNULL(t14.num,0) as num14
  23. from
  24. -- ------------
  25. -- 总挂号数
  26. -- ------------
  27. (
  28. select t.startTime, t.endTime, count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
  29. (
  30. select
  31. a.data_source,
  32. a.is_appoint_resource,
  33. a.subsequent_visit,
  34. a.appointment_state,
  35. a.dept_id as deptId,
  36. a.appointment_doctor_id as doctorId,
  37. a.dept_name as deptName,
  38. a.create_time,
  39. a.appointment_doctor_name as doctorName,
  40. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  41. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  42. f.preFee, -- 原价
  43. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  44. f.discount
  45. from `thc_arrange`.`bpm_appointment` a
  46. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  47. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
  48. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  49. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  50. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  51. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  52. where a.del_flag = 0
  53. AND g.id IN (
  54. SELECT DISTINCT g.id
  55. FROM `thc_arrange`.`bpm_appointment` a
  56. INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  57. INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
  58. INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  59. INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  60. INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
  61. INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  62. INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  63. WHERE 1=1
  64. AND a.del_flag = 0
  65. AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
  66. AND e.itemClass = 1 AND e.returnFlag is NULL
  67. AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
  68. AND h.type = 1 and h.pay_state = 1 -- 支付成功
  69. )
  70. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  71. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  72. ) t
  73. where 1=1
  74. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  75. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  76. group by t.deptId,t.doctorId
  77. ) t0
  78.  
  79. -- ------------
  80. -- 退号
  81. -- ------------
  82. left join
  83. (
  84. select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
  85. (
  86. select
  87. a.data_source,
  88. a.is_appoint_resource,
  89. a.subsequent_visit,
  90. a.dept_id as deptId,
  91. a.appointment_doctor_id as doctorId,
  92. a.appointment_state,
  93. a.dept_name as deptName,
  94. a.create_time,
  95. a.appointment_doctor_name as doctorName,
  96. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  97. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  98. f.preFee, -- 原价
  99. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  100. f.discount
  101. from `thc_arrange`.`bpm_appointment` a
  102. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  103. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
  104. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  105. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  106. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  107. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  108. where 1=1 and a.del_flag = 0
  109. AND g.id IN (
  110. SELECT DISTINCT g.id
  111. FROM `thc_arrange`.`bpm_appointment` a
  112. INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  113. INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
  114. INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  115. INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  116. INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
  117. INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  118. INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  119. WHERE 1=1
  120. AND a.del_flag = 0
  121. AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
  122. AND e.itemClass = 1 AND e.returnFlag is NULL
  123. AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
  124. AND h.type = 1 and h.pay_state = 1 -- 支付成功
  125. )
  126. and a.appointment_state = -1
  127. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  128. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  129. ) t
  130. where 1=1
  131. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  132. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  133. group by t.deptId,t.doctorId
  134. ) t1
  135. on t0.deptId = t1.deptId and t0.doctorId = t1.doctorId
  136.  
  137. -- ------------
  138. -- 实际挂号数 = 初诊数 + 复诊数 + 转诊数
  139. -- ------------
  140. left join
  141. (
  142. select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
  143. (
  144. select
  145. a.data_source,
  146. a.is_appoint_resource,
  147. a.subsequent_visit,
  148. a.dept_id as deptId,
  149. a.appointment_doctor_id as doctorId,
  150. a.appointment_state,
  151. a.dept_name as deptName,
  152. a.create_time,
  153. a.appointment_doctor_name as doctorName,
  154. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  155. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  156. f.preFee, -- 原价
  157. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  158. f.discount
  159. from `thc_arrange`.`bpm_appointment` a
  160. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  161. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
  162. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  163. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  164. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  165. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  166. where a.del_flag = 0 and a.subsequent_visit in (0,1,2)
  167. AND g.id IN (
  168. SELECT DISTINCT g.id
  169. FROM `thc_arrange`.`bpm_appointment` a
  170. INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  171. INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
  172. INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  173. INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  174. INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
  175. INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  176. INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  177. WHERE 1=1
  178. AND a.del_flag = 0
  179. AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
  180. AND e.itemClass = 1 AND e.returnFlag is NULL
  181. AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
  182. AND h.type = 1 and h.pay_state = 1 -- 支付成功
  183. )
  184. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  185. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  186. ) t
  187. where 1=1
  188. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  189. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  190. group by t.deptId,t.doctorId
  191. ) t2
  192. on t0.deptId = t2.deptId and t0.doctorId = t2.doctorId
  193.  
  194. -- ------------
  195. -- 指定
  196. -- ------------
  197. left join
  198. (
  199. select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
  200. (
  201. select
  202. a.data_source,
  203. a.is_appoint_resource,
  204. a.subsequent_visit,
  205. a.dept_id as deptId,
  206. a.appointment_doctor_id as doctorId,
  207. a.appointment_state,
  208. a.dept_name as deptName,
  209. a.create_time,
  210. a.appointment_doctor_name as doctorName,
  211. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  212. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  213. f.preFee, -- 原价
  214. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  215. f.discount
  216. from `thc_arrange`.`bpm_appointment` a
  217. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  218. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
  219. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  220. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  221. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  222. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  223. where a.del_flag = 0 and a.is_appoint_resource = 1
  224. AND g.id IN (
  225. SELECT DISTINCT g.id
  226. FROM `thc_arrange`.`bpm_appointment` a
  227. INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  228. INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
  229. INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  230. INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  231. INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
  232. INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  233. INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  234. WHERE 1=1
  235. AND a.del_flag = 0
  236. AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
  237. AND e.itemClass = 1 AND e.returnFlag is NULL
  238. AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
  239. AND h.type = 1 and h.pay_state = 1 -- 支付成功
  240. )
  241. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  242. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  243. ) t
  244. where 1=1
  245. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  246. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  247. group by t.deptId,t.doctorId
  248. ) t3
  249. on t0.deptId = t3.deptId and t0.doctorId = t3.doctorId
  250.  
  251. -- ------------
  252. -- 初诊
  253. -- ------------
  254. left join
  255. (
  256. select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
  257. (
  258. select
  259. a.data_source,
  260. a.is_appoint_resource,
  261. a.subsequent_visit,
  262. a.dept_id as deptId,
  263. a.appointment_doctor_id as doctorId,
  264. a.appointment_state,
  265. a.dept_name as deptName,
  266. a.create_time,
  267. a.appointment_doctor_name as doctorName,
  268. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  269. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  270. f.preFee, -- 原价
  271. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  272. f.discount
  273. from `thc_arrange`.`bpm_appointment` a
  274. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  275. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
  276. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  277. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  278. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  279. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  280. where a.del_flag = 0 and a.subsequent_visit = 0
  281. AND g.id IN (
  282. SELECT DISTINCT g.id
  283. FROM `thc_arrange`.`bpm_appointment` a
  284. INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  285. INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
  286. INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  287. INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  288. INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
  289. INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  290. INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  291. WHERE 1=1
  292. AND a.del_flag = 0
  293. AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
  294. AND e.itemClass = 1 AND e.returnFlag is NULL
  295. AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
  296. AND h.type = 1 and h.pay_state = 1 -- 支付成功
  297. )
  298. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  299. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  300. ) t
  301. where 1=1
  302. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  303. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  304. group by t.deptId,t.doctorId
  305. ) t4
  306. on t0.deptId = t4.deptId and t0.doctorId = t4.doctorId
  307.  
  308. -- ------------
  309. -- 复诊
  310. -- ------------
  311. left join
  312. (
  313. select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
  314. (
  315. select
  316. a.data_source,
  317. a.is_appoint_resource,
  318. a.subsequent_visit,
  319. a.dept_id as deptId,
  320. a.appointment_doctor_id as doctorId,
  321. a.appointment_state,
  322. a.dept_name as deptName,
  323. a.create_time,
  324. a.appointment_doctor_name as doctorName,
  325. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  326. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  327. f.preFee, -- 原价
  328. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  329. f.discount
  330. from `thc_arrange`.`bpm_appointment` a
  331. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  332. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
  333. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  334. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  335. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  336. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  337. where a.del_flag = 0 and a.subsequent_visit = 1
  338. AND g.id IN (
  339. SELECT DISTINCT g.id
  340. FROM `thc_arrange`.`bpm_appointment` a
  341. INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  342. INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
  343. INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  344. INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  345. INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
  346. INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  347. INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  348. WHERE 1=1
  349. AND a.del_flag = 0
  350. AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
  351. AND e.itemClass = 1 AND e.returnFlag is NULL
  352. AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
  353. AND h.type = 1 and h.pay_state = 1 -- 支付成功
  354. )
  355. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  356. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  357. ) t
  358. where 1=1
  359. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  360. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  361. group by t.deptId,t.doctorId
  362. ) t5
  363. on t0.deptId = t5.deptId and t0.doctorId = t5.doctorId
  364.  
  365. -- ------------
  366. -- 体检
  367. -- ------------
  368. left join
  369. (
  370. select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
  371. (
  372. select
  373. a.data_source,
  374. a.is_appoint_resource,
  375. a.subsequent_visit,
  376. a.dept_id as deptId,
  377. a.appointment_doctor_id as doctorId,
  378. a.appointment_state,
  379. a.dept_name as deptName,
  380. a.create_time,
  381. a.appointment_doctor_name as doctorName,
  382. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  383. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  384. f.preFee, -- 原价
  385. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  386. f.discount
  387. from `thc_arrange`.`bpm_appointment` a
  388. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  389. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
  390. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  391. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  392. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  393. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  394. where a.del_flag = 0 and a.subsequent_visit = 4
  395. AND g.id IN (
  396. SELECT DISTINCT g.id
  397. FROM `thc_arrange`.`bpm_appointment` a
  398. INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  399. INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
  400. INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  401. INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  402. INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
  403. INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  404. INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  405. WHERE 1=1
  406. AND a.del_flag = 0
  407. AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
  408. AND e.itemClass = 1 AND e.returnFlag is NULL
  409. AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
  410. AND h.type = 1 and h.pay_state = 1 -- 支付成功
  411. )
  412. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  413. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  414. ) t
  415. where 1=1
  416. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  417. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  418. group by t.deptId,t.doctorId
  419. ) t6
  420. on t0.deptId = t6.deptId and t0.doctorId = t6.doctorId
  421.  
  422. -- ------------
  423. -- 团队
  424. -- ------------
  425. left join
  426. (
  427. select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
  428. (
  429. select
  430. a.data_source,
  431. a.is_appoint_resource,
  432. a.subsequent_visit,
  433. a.dept_id as deptId,
  434. a.appointment_doctor_id as doctorId,
  435. a.appointment_state,
  436. a.dept_name as deptName,
  437. a.create_time,
  438. a.appointment_doctor_name as doctorName,
  439. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  440. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  441. f.preFee, -- 原价
  442. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  443. f.discount
  444. from `thc_arrange`.`bpm_appointment` a
  445. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  446. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
  447. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  448. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  449. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  450. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  451. where a.del_flag = 0 and a.subsequent_visit = 7
  452. AND g.id IN (
  453. SELECT DISTINCT g.id
  454. FROM `thc_arrange`.`bpm_appointment` a
  455. INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  456. INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
  457. INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  458. INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  459. INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
  460. INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  461. INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  462. WHERE 1=1
  463. AND a.del_flag = 0
  464. AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
  465. AND e.itemClass = 1 AND e.returnFlag is NULL
  466. AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
  467. AND h.type = 1 and h.pay_state = 1 -- 支付成功
  468. )
  469. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  470. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  471. ) t
  472. where 1=1
  473. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  474. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  475. group by t.deptId,t.doctorId
  476. ) t7
  477. on t0.deptId = t7.deptId and t0.doctorId = t7.doctorId
  478.  
  479. -- ------------
  480. -- 简易
  481. -- ------------
  482. left join
  483. (
  484. select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
  485. (
  486. select
  487. a.data_source,
  488. a.is_appoint_resource,
  489. a.subsequent_visit,
  490. a.dept_id as deptId,
  491. a.appointment_doctor_id as doctorId,
  492. a.appointment_state,
  493. a.dept_name as deptName,
  494. a.create_time,
  495. a.appointment_doctor_name as doctorName,
  496. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  497. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  498. f.preFee, -- 原价
  499. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  500. f.discount
  501. from `thc_arrange`.`bpm_appointment` a
  502. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  503. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
  504. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  505. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  506. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  507. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  508. where a.del_flag = 0 and a.subsequent_visit = 5
  509. AND g.id IN (
  510. SELECT DISTINCT g.id
  511. FROM `thc_arrange`.`bpm_appointment` a
  512. INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  513. INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
  514. INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  515. INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  516. INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
  517. INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  518. INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  519. WHERE 1=1
  520. AND a.del_flag = 0
  521. AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
  522. AND e.itemClass = 1 AND e.returnFlag is NULL
  523. AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
  524. AND h.type = 1 and h.pay_state = 1 -- 支付成功
  525. )
  526. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  527. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  528. ) t
  529. where 1=1
  530. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  531. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  532. group by t.deptId,t.doctorId
  533. ) t8
  534. on t0.deptId = t8.deptId and t0.doctorId = t8.doctorId
  535.  
  536. -- ------------
  537. -- 转诊
  538. -- ------------
  539. left join
  540. (
  541. select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
  542. (
  543. select
  544. a.data_source,
  545. a.is_appoint_resource,
  546. a.subsequent_visit,
  547. a.dept_id as deptId,
  548. a.appointment_doctor_id as doctorId,
  549. a.appointment_state,
  550. a.dept_name as deptName,
  551. a.create_time,
  552. a.appointment_doctor_name as doctorName,
  553. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  554. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  555. f.preFee, -- 原价
  556. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  557. f.discount
  558. from `thc_arrange`.`bpm_appointment` a
  559. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  560. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
  561. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  562. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  563. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  564. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  565. where a.del_flag = 0 and a.subsequent_visit = 2
  566. AND g.id IN (
  567. SELECT DISTINCT g.id
  568. FROM `thc_arrange`.`bpm_appointment` a
  569. INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  570. INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
  571. INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  572. INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  573. INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
  574. INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  575. INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  576. WHERE 1=1
  577. AND a.del_flag = 0
  578. AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
  579. AND e.itemClass = 1 AND e.returnFlag is NULL
  580. AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
  581. AND h.type = 1 and h.pay_state = 1 -- 支付成功
  582. )
  583. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  584. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  585. ) t
  586. where 1=1
  587. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  588. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  589. group by t.deptId,t.doctorId
  590. ) t9
  591. on t0.deptId = t9.deptId and t0.doctorId = t9.doctorId
  592.  
  593. -- ------------
  594. -- 疫苗
  595. -- ------------
  596. left join
  597. (
  598. select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
  599. (
  600. select
  601. a.data_source,
  602. a.is_appoint_resource,
  603. a.subsequent_visit,
  604. a.dept_id as deptId,
  605. a.appointment_doctor_id as doctorId,
  606. a.appointment_state,
  607. a.dept_name as deptName,
  608. a.create_time,
  609. a.appointment_doctor_name as doctorName,
  610. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  611. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  612. f.preFee, -- 原价
  613. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  614. f.discount
  615. from `thc_arrange`.`bpm_appointment` a
  616. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  617. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
  618. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  619. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  620. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  621. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  622. where a.del_flag = 0 and a.subsequent_visit = 2
  623. AND g.id IN (
  624. SELECT DISTINCT g.id
  625. FROM `thc_arrange`.`bpm_appointment` a
  626. INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  627. INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
  628. INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  629. INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  630. INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
  631. INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  632. INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  633. WHERE 1=1
  634. AND a.del_flag = 0
  635. AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
  636. AND e.itemClass = 1 AND e.returnFlag is NULL
  637. AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
  638. AND h.type = 1 and h.pay_state = 1 -- 支付成功
  639. )
  640. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  641. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  642. ) t
  643. where 1=1
  644. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  645. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  646. group by t.deptId,t.doctorId
  647. ) t10
  648. on t0.deptId = t10.deptId and t0.doctorId = t10.doctorId
  649.  
  650. -- ------------
  651. -- 微信数
  652. -- ------------
  653. left join
  654. (
  655. select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
  656. (
  657. select
  658. a.data_source,
  659. a.is_appoint_resource,
  660. a.subsequent_visit,
  661. a.dept_id as deptId,
  662. a.appointment_doctor_id as doctorId,
  663. a.appointment_state,
  664. a.dept_name as deptName,
  665. a.create_time,
  666. a.appointment_doctor_name as doctorName,
  667. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  668. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  669. f.preFee, -- 原价
  670. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  671. f.discount
  672. from `thc_arrange`.`bpm_appointment` a
  673. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  674. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
  675. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  676. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  677. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  678. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  679. where a.del_flag = 0 and a.data_source = 2
  680. AND g.id IN (
  681. SELECT DISTINCT g.id
  682. FROM `thc_arrange`.`bpm_appointment` a
  683. INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  684. INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
  685. INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  686. INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  687. INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
  688. INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  689. INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  690. WHERE 1=1
  691. AND a.del_flag = 0
  692. AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
  693. AND e.itemClass = 1 AND e.returnFlag is NULL
  694. AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
  695. AND h.type = 1 and h.pay_state = 1 -- 支付成功
  696. )
  697. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  698. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  699. ) t
  700. where 1=1
  701. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  702. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  703. group by t.deptId,t.doctorId
  704. ) t11
  705. on t0.deptId = t11.deptId and t0.doctorId = t11.doctorId
  706.  
  707. -- ------------
  708. -- 微信金额
  709. -- ------------
  710. left join
  711. (
  712. select round(sum(t.preFee),2) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
  713. (
  714. select
  715. a.data_source,
  716. a.is_appoint_resource,
  717. a.subsequent_visit,
  718. a.dept_id as deptId,
  719. a.appointment_doctor_id as doctorId,
  720. a.appointment_state,
  721. a.dept_name as deptName,
  722. a.create_time,
  723. a.appointment_doctor_name as doctorName,
  724. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  725. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  726. f.preFee, -- 原价
  727. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  728. f.discount
  729. from `thc_arrange`.`bpm_appointment` a
  730. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  731. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
  732. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  733. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  734. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  735. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  736. where a.del_flag = 0 and a.data_source = 2
  737. AND g.id IN (
  738. SELECT DISTINCT g.id
  739. FROM `thc_arrange`.`bpm_appointment` a
  740. INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  741. INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
  742. INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  743. INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  744. INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
  745. INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  746. INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  747. WHERE 1=1
  748. AND a.del_flag = 0
  749. AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
  750. AND e.itemClass = 1 AND e.returnFlag is NULL
  751. AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
  752. AND h.type = 1 and h.pay_state = 1 AND h.pay_method = 4 -- 微信支付成功
  753. )
  754. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  755. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  756. ) t
  757. where 1=1
  758. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  759. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  760. group by t.deptId,t.doctorId
  761. ) t12
  762. on t0.deptId = t12.deptId and t0.doctorId = t12.doctorId
  763.  
  764. -- ------------
  765. -- 应收金额 t12
  766. -- ------------
  767. left join
  768. (
  769. select round(sum(t.preFee),2) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
  770. (
  771. select
  772. a.data_source,
  773. a.is_appoint_resource,
  774. a.subsequent_visit,
  775. a.dept_id as deptId,
  776. a.appointment_doctor_id as doctorId,
  777. a.appointment_state,
  778. a.dept_name as deptName,
  779. a.create_time,
  780. a.appointment_doctor_name as doctorName,
  781. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  782. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  783. f.preFee, -- 原价
  784. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  785. f.discount
  786. from `thc_arrange`.`bpm_appointment` a
  787. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  788. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
  789. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  790. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  791. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  792. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  793. where a.del_flag = 0
  794. AND g.id IN (
  795. SELECT DISTINCT g.id
  796. FROM `thc_arrange`.`bpm_appointment` a
  797. INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  798. INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
  799. INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  800. INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  801. INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
  802. INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  803. INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  804. WHERE 1=1
  805. AND a.del_flag = 0
  806. AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
  807. AND e.itemClass = 1 AND e.returnFlag is NULL
  808. AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
  809. AND h.type = 1 and h.pay_state = 1 -- 支付成功
  810. )
  811. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  812. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  813. ) t
  814. where 1=1
  815. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  816. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  817. group by t.deptId,t.doctorId
  818. ) t13
  819. on t0.deptId = t13.deptId and t0.doctorId = t13.doctorId
  820.  
  821. -- ------------
  822. -- 实收金额 t13
  823. -- ------------
  824. left join
  825. (
  826. select round(sum(t.realFee),2) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
  827. (
  828. select
  829. a.data_source,
  830. a.is_appoint_resource,
  831. a.subsequent_visit,
  832. a.dept_id as deptId,
  833. a.appointment_doctor_id as doctorId,
  834. a.appointment_state,
  835. a.dept_name as deptName,
  836. a.create_time,
  837. a.appointment_doctor_name as doctorName,
  838. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  839. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  840. f.preFee,
  841. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  842. f.discount
  843. from `thc_arrange`.`bpm_appointment` a
  844. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  845. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
  846. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  847. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  848. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  849. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  850. where a.del_flag = 0
  851. AND g.id IN (
  852. SELECT DISTINCT g.id
  853. FROM `thc_arrange`.`bpm_appointment` a
  854. INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  855. INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
  856. INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  857. INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  858. INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
  859. INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  860. INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  861. WHERE 1=1
  862. AND a.del_flag = 0
  863. AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
  864. AND e.itemClass = 1 AND e.returnFlag is NULL
  865. AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
  866. AND h.type = 1 and h.pay_state = 1 -- 支付成功
  867. )
  868. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  869. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  870. ) t
  871. where 1=1
  872. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  873. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  874. group by t.deptId,t.doctorId
  875. ) t14
  876. on t0.deptId = t14.deptId and t0.doctorId = t14.doctorId
  877.  
  878. order by t0.startTime asc

5.挂号按科室统计

  1. select
  2. t0.deptName,
  3. t0.deptId,
  4. t0.startTime,
  5. t0.endTime,
  6. IFNULL(t0.num,0) as num0,
  7. IFNULL(t1.num,0) as num1,
  8. IFNULL(t2.num,0) as num2,
  9. IFNULL(t4.num,0) as num4,
  10. IFNULL(t5.num,0) as num5,
  11. IFNULL(t6.num,0) as num6,
  12. IFNULL(t7.num,0) as num7,
  13. IFNULL(t8.num,0) as num8,
  14. IFNULL(t9.num,0) as num9,
  15. IFNULL(t10.num,0) as num10,
  16. IFNULL(t11.num,0) as num11,
  17. IFNULL(t13.num,0) as num13,
  18. IFNULL(t14.num,0) as num14,
  19. IFNULL(t15.num,0) as num15,
  20. IFNULL(t16.num,0) as num16,
  21. IFNULL(t17.num,0) as num17,
  22. IFNULL(t18.num,0) as num18,
  23. IFNULL(t19.num,0) as num19,
  24. IFNULL(t20.num,0) as num20,
  25. IFNULL(t21.num,0) as num21,
  26. IFNULL(t22.num,0) as num22
  27. from
  28. -- ------------
  29. -- 总挂号数
  30. -- ------------
  31. (
  32. select t.startTime, t.endTime, count(1) as num,t.deptId, t.deptName,t.create_time from
  33. (
  34. select
  35. a.data_source,
  36. a.is_appoint_resource,
  37. a.subsequent_visit,
  38. a.appointment_state,
  39. a.dept_id as deptId,
  40.  
  41. a.dept_name as deptName,
  42. a.create_time,
  43. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  44. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  45. f.preFee, -- 原价
  46. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  47. f.discount
  48. from `thc_arrange`.`bpm_appointment` a
  49. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  50. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  51. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  52. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  53. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  54. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  55. where 1=1 and a.del_flag = 0
  56. and g.id in (
  57. SELECT DISTINCT g.id
  58. from `thc_arrange`.`bpm_appointment` a
  59. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  60. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  61. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  62. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  63. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  64. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  65. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  66. where 1=1
  67. and a.del_flag = 0
  68. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  69. and e.itemClass = 1 and e.returnFlag is NULL
  70. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  71. and h.type = 1 and h.pay_state = 1
  72. )
  73. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  74. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  75. ) t
  76. where 1=1
  77. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  78. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  79. group by t.deptId
  80. ) t0
  81.  
  82. -- ------------
  83. -- 退号
  84. -- ------------
  85. left join
  86. (
  87. select count(1) as num,t.deptId, t.deptName,t.create_time from
  88. (
  89. select
  90. a.data_source,
  91. a.is_appoint_resource,
  92. a.subsequent_visit,
  93. a.dept_id as deptId,
  94. a.appointment_state,
  95. a.dept_name as deptName,
  96. a.create_time,
  97. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  98. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  99. f.preFee, -- 原价
  100. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  101. f.discount
  102. from `thc_arrange`.`bpm_appointment` a
  103. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  104. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  105. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  106. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  107. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  108. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  109. where 1=1 and a.del_flag = 0 AND a.appointment_state = -1
  110. and g.id in (
  111. SELECT DISTINCT g.id
  112. from `thc_arrange`.`bpm_appointment` a
  113. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  114. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  115. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  116. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  117. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  118. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  119. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  120. where 1=1
  121. and a.del_flag = 0
  122. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  123. and e.itemClass = 1 and e.returnFlag is NULL
  124. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  125. and h.type = 1 and h.pay_state = 1
  126. )
  127. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  128. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  129. ) t
  130. where 1=1
  131. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  132. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  133. group by t.deptId
  134. ) t1
  135. on t0.deptId = t1.deptId
  136.  
  137. -- ------------
  138. -- 实际挂号数 = 初诊数 + 复诊数 + 转诊数
  139. -- ------------
  140. left join
  141. (
  142. select count(1) as num,t.deptId, t.deptName,t.create_time from
  143. (
  144. select
  145. a.data_source,
  146. a.is_appoint_resource,
  147. a.subsequent_visit,
  148. a.dept_id as deptId,
  149.  
  150. a.appointment_state,
  151. a.dept_name as deptName,
  152. a.create_time,
  153. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  154. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  155. f.preFee, -- 原价
  156. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  157. f.discount
  158. from `thc_arrange`.`bpm_appointment` a
  159. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  160. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  161. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  162. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  163. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  164. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  165. where 1=1 and a.del_flag = 0 AND a.subsequent_visit in (0,1,2)
  166. and g.id in (
  167. SELECT DISTINCT g.id
  168. from `thc_arrange`.`bpm_appointment` a
  169. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  170. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  171. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  172. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  173. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  174. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  175. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  176. where 1=1
  177. and a.del_flag = 0
  178. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  179. and e.itemClass = 1 and e.returnFlag is NULL
  180. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  181. and h.type = 1 and h.pay_state = 1
  182. )
  183. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  184. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  185. ) t
  186. where 1=1
  187. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  188. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  189. group by t.deptId
  190. ) t2
  191. on t0.deptId = t2.deptId
  192.  
  193. -- ------------
  194. -- 初诊
  195. -- ------------
  196. left join
  197. (
  198. select count(1) as num,t.deptId, t.deptName,t.create_time from
  199. (
  200. select
  201. a.data_source,
  202. a.is_appoint_resource,
  203. a.subsequent_visit,
  204. a.dept_id as deptId,
  205.  
  206. a.appointment_state,
  207. a.dept_name as deptName,
  208. a.create_time,
  209. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  210. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  211. f.preFee, -- 原价
  212. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  213. f.discount
  214. from `thc_arrange`.`bpm_appointment` a
  215. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  216. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  217. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  218. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  219. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  220. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  221. where 1=1 and a.del_flag = 0 AND a.subsequent_visit = 0
  222. and g.id in (
  223. SELECT DISTINCT g.id
  224. from `thc_arrange`.`bpm_appointment` a
  225. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  226. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  227. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  228. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  229. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  230. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  231. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  232. where 1=1
  233. and a.del_flag = 0
  234. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  235. and e.itemClass = 1 and e.returnFlag is NULL
  236. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  237. and h.type = 1 and h.pay_state = 1
  238. )
  239. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  240. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  241. ) t
  242. where 1=1
  243. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  244. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  245. group by t.deptId
  246. ) t4
  247. on t0.deptId = t4.deptId
  248.  
  249. -- ------------
  250. -- 复诊
  251. -- ------------
  252. left join
  253. (
  254. select count(1) as num,t.deptId, t.deptName,t.create_time from
  255. (
  256. select
  257. a.data_source,
  258. a.is_appoint_resource,
  259. a.subsequent_visit,
  260. a.dept_id as deptId,
  261.  
  262. a.appointment_state,
  263. a.dept_name as deptName,
  264. a.create_time,
  265. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  266. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  267. f.preFee, -- 原价
  268. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  269. f.discount
  270. from `thc_arrange`.`bpm_appointment` a
  271. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  272. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  273. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  274. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  275. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  276. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  277. where 1=1 and a.del_flag = 0 AND a.subsequent_visit = 1
  278. and g.id in (
  279. SELECT DISTINCT g.id
  280. from `thc_arrange`.`bpm_appointment` a
  281. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  282. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  283. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  284. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  285. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  286. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  287. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  288. where 1=1
  289. and a.del_flag = 0
  290. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  291. and e.itemClass = 1 and e.returnFlag is NULL
  292. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  293. and h.type = 1 and h.pay_state = 1
  294. )
  295. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  296. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  297. ) t
  298. where 1=1
  299. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  300. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  301. group by t.deptId
  302. ) t5
  303. on t0.deptId = t5.deptId
  304.  
  305. -- ------------
  306. -- 体检
  307. -- ------------
  308. left join
  309. (
  310. select count(1) as num,t.deptId, t.deptName,t.create_time from
  311. (
  312. select
  313. a.data_source,
  314. a.is_appoint_resource,
  315. a.subsequent_visit,
  316. a.dept_id as deptId,
  317.  
  318. a.appointment_state,
  319. a.dept_name as deptName,
  320. a.create_time,
  321. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  322. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  323. f.preFee, -- 原价
  324. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  325. f.discount
  326. from `thc_arrange`.`bpm_appointment` a
  327. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  328. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  329. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  330. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  331. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  332. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  333. where 1=1 and a.del_flag = 0 AND a.subsequent_visit = 4
  334. and g.id in (
  335. SELECT DISTINCT g.id
  336. from `thc_arrange`.`bpm_appointment` a
  337. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  338. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  339. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  340. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  341. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  342. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  343. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  344. where 1=1
  345. and a.del_flag = 0
  346. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  347. and e.itemClass = 1 and e.returnFlag is NULL
  348. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  349. and h.type = 1 and h.pay_state = 1
  350. )
  351. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  352. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  353. ) t
  354. where 1=1
  355. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  356. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  357. group by t.deptId
  358. ) t6
  359. on t0.deptId = t6.deptId
  360.  
  361. -- ------------
  362. -- 团队
  363. -- ------------
  364. left join
  365. (
  366. select count(1) as num,t.deptId, t.deptName,t.create_time from
  367. (
  368. select
  369. a.data_source,
  370. a.is_appoint_resource,
  371. a.subsequent_visit,
  372. a.dept_id as deptId,
  373.  
  374. a.appointment_state,
  375. a.dept_name as deptName,
  376. a.create_time,
  377. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  378. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  379. f.preFee, -- 原价
  380. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  381. f.discount
  382. from `thc_arrange`.`bpm_appointment` a
  383. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  384. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  385. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  386. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  387. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  388. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  389. where 1=1 and a.del_flag = 0 AND a.subsequent_visit = 7
  390. and g.id in (
  391. SELECT DISTINCT g.id
  392. from `thc_arrange`.`bpm_appointment` a
  393. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  394. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  395. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  396. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  397. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  398. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  399. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  400. where 1=1
  401. and a.del_flag = 0
  402. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  403. and e.itemClass = 1 and e.returnFlag is NULL
  404. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  405. and h.type = 1 and h.pay_state = 1
  406. )
  407. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  408. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  409. ) t
  410. where 1=1
  411. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  412. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  413. group by t.deptId
  414. ) t7
  415. on t0.deptId = t7.deptId
  416.  
  417. -- ------------
  418. -- 简易
  419. -- ------------
  420. left join
  421. (
  422. select count(1) as num,t.deptId, t.deptName,t.create_time from
  423. (
  424. select
  425. a.data_source,
  426. a.is_appoint_resource,
  427. a.subsequent_visit,
  428. a.dept_id as deptId,
  429.  
  430. a.appointment_state,
  431. a.dept_name as deptName,
  432. a.create_time,
  433. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  434. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  435. f.preFee, -- 原价
  436. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  437. f.discount
  438. from `thc_arrange`.`bpm_appointment` a
  439. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  440. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  441. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  442. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  443. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  444. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  445. where 1=1 and a.del_flag = 0 AND a.subsequent_visit = 5
  446. and g.id in (
  447. SELECT DISTINCT g.id
  448. from `thc_arrange`.`bpm_appointment` a
  449. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  450. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  451. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  452. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  453. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  454. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  455. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  456. where 1=1
  457. and a.del_flag = 0
  458. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  459. and e.itemClass = 1 and e.returnFlag is NULL
  460. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  461. and h.type = 1 and h.pay_state = 1
  462. )
  463. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  464. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  465. ) t
  466. where 1=1
  467. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  468. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  469. group by t.deptId
  470. ) t8
  471. on t0.deptId = t8.deptId
  472.  
  473. -- ------------
  474. -- 转诊
  475. -- ------------
  476. left join
  477. (
  478. select count(1) as num,t.deptId, t.deptName,t.create_time from
  479. (
  480. select
  481. a.data_source,
  482. a.is_appoint_resource,
  483. a.subsequent_visit,
  484. a.dept_id as deptId,
  485.  
  486. a.appointment_state,
  487. a.dept_name as deptName,
  488. a.create_time,
  489. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  490. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  491. f.preFee, -- 原价
  492. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  493. f.discount
  494. from `thc_arrange`.`bpm_appointment` a
  495. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  496. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  497. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  498. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  499. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  500. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  501. where 1=1 and a.del_flag = 0 AND a.subsequent_visit = 2
  502. and g.id in (
  503. SELECT DISTINCT g.id
  504. from `thc_arrange`.`bpm_appointment` a
  505. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  506. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  507. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  508. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  509. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  510. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  511. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  512. where 1=1
  513. and a.del_flag = 0
  514. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  515. and e.itemClass = 1 and e.returnFlag is NULL
  516. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  517. and h.type = 1 and h.pay_state = 1
  518. )
  519. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  520. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  521. ) t
  522. where 1=1
  523. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  524. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  525. group by t.deptId
  526. ) t9
  527. on t0.deptId = t9.deptId
  528.  
  529. -- ------------
  530. -- 疫苗
  531. -- ------------
  532. left join
  533. (
  534. select count(1) as num,t.deptId, t.deptName,t.create_time from
  535. (
  536. select
  537. a.data_source,
  538. a.is_appoint_resource,
  539. a.subsequent_visit,
  540. a.dept_id as deptId,
  541.  
  542. a.appointment_state,
  543. a.dept_name as deptName,
  544. a.create_time,
  545. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  546. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  547. f.preFee, -- 原价
  548. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  549. f.discount
  550. from `thc_arrange`.`bpm_appointment` a
  551. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  552. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  553. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  554. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  555. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  556. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  557. where 1=1 and a.del_flag = 0 AND a.subsequent_visit = 2
  558. and g.id in (
  559. SELECT DISTINCT g.id
  560. from `thc_arrange`.`bpm_appointment` a
  561. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  562. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  563. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  564. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  565. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  566. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  567. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  568. where 1=1
  569. and a.del_flag = 0
  570. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  571. and e.itemClass = 1 and e.returnFlag is NULL
  572. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  573. and h.type = 1 and h.pay_state = 1
  574. )
  575. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  576. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  577. ) t
  578. where 1=1
  579. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  580. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  581. group by t.deptId
  582. ) t10
  583. on t0.deptId = t10.deptId
  584.  
  585. -- ------------
  586. -- t11 微信数
  587. -- ------------
  588. left join
  589. (
  590. select count(1) as num,t.deptId, t.deptName,t.create_time from
  591. (
  592. select
  593. a.data_source,
  594. a.is_appoint_resource,
  595. a.subsequent_visit,
  596. a.dept_id as deptId,
  597.  
  598. a.appointment_state,
  599. a.dept_name as deptName,
  600. a.create_time,
  601. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  602. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  603. f.preFee, -- 原价
  604. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  605. f.discount
  606. from `thc_arrange`.`bpm_appointment` a
  607. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  608. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  609. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  610. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  611. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  612. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  613. where 1=1 and a.del_flag = 0 AND a.data_source = 2
  614. and g.id in (
  615. SELECT DISTINCT g.id
  616. from `thc_arrange`.`bpm_appointment` a
  617. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  618. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  619. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  620. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  621. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  622. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  623. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  624. where 1=1
  625. and a.del_flag = 0
  626. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  627. and e.itemClass = 1 and e.returnFlag is NULL
  628. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  629. and h.type = 1 and h.pay_state = 1
  630. )
  631. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  632. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  633. ) t
  634. where 1=1
  635. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  636. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  637. group by t.deptId
  638. ) t11
  639. on t0.deptId = t11.deptId
  640.  
  641. -- ------------
  642. -- 挂号费 t13
  643. -- ------------
  644. left join
  645. (
  646. select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
  647. (
  648. select
  649. a.data_source,
  650. a.is_appoint_resource,
  651. a.subsequent_visit,
  652. a.dept_id as deptId,
  653.  
  654. a.appointment_state,
  655. a.dept_name as deptName,
  656. a.create_time,
  657. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  658. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  659. f.preFee, -- 原价
  660. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  661. f.discount
  662. from `thc_arrange`.`bpm_appointment` a
  663. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  664. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  665. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  666. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  667. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  668. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  669. where 1=1 and a.del_flag = 0
  670. and g.id in (
  671. SELECT DISTINCT g.id
  672. from `thc_arrange`.`bpm_appointment` a
  673. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  674. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  675. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  676. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  677. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  678. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  679. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  680. where 1=1
  681. and a.del_flag = 0
  682. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  683. and e.itemClass = 1 and e.returnFlag is NULL
  684. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  685. and h.type = 1 and h.pay_state = 1
  686. )
  687. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  688. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  689. ) t
  690. where 1=1
  691. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  692. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  693. group by t.deptId
  694. ) t13
  695. on t0.deptId = t13.deptId
  696.  
  697. -- ------------
  698. -- 急诊费 t14
  699. -- ------------
  700. left join
  701. (
  702. select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
  703. (
  704. select
  705. a.data_source,
  706. a.is_appoint_resource,
  707. a.subsequent_visit,
  708. a.dept_id as deptId,
  709.  
  710. a.appointment_state,
  711. a.dept_name as deptName,
  712. a.create_time,
  713. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  714. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  715. f.preFee, -- 原价
  716. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  717. f.discount
  718. from `thc_arrange`.`bpm_appointment` a
  719. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id and b.del_flag = 0
  720. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id` AND c.del_flag = 0
  721. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0
  722. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.isDelete = 0
  723. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id and e.isDelete = 0
  724. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 -- 门诊挂号
  725. where 1=1 and a.del_flag = 0 AND e.itemCode = 'jiajifei' -- 加急费:急诊费
  726. and g.id in (
  727. SELECT DISTINCT g.id
  728. from `thc_arrange`.`bpm_appointment` a
  729. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  730. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  731. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  732. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  733. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  734. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  735. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  736. where 1=1
  737. and a.del_flag = 0
  738. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  739. and e.itemClass = 1 and e.returnFlag is NULL
  740. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  741. and h.type = 1 and h.pay_state = 1
  742. )
  743. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  744. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  745. ) t
  746. where 1=1
  747. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  748. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  749. group by t.deptId
  750. ) t14
  751. on t0.deptId = t14.deptId
  752.  
  753. -- ------------
  754. -- 工本费 t15
  755. -- ------------
  756. left join
  757. (
  758. select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
  759. (
  760. select
  761. a.data_source,
  762. a.is_appoint_resource,
  763. a.subsequent_visit,
  764. a.dept_id as deptId,
  765.  
  766. a.appointment_state,
  767. a.dept_name as deptName,
  768. a.create_time,
  769. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  770. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  771. f.preFee, -- 原价
  772. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  773. f.discount
  774. from `thc_arrange`.`bpm_appointment` a
  775. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  776. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  777. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  778. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  779. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  780. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  781. where 1=1 and a.del_flag = 0 AND e.itemCode = 'bingliben' -- 病历本:工本费
  782. and g.id in (
  783. SELECT DISTINCT g.id
  784. from `thc_arrange`.`bpm_appointment` a
  785. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  786. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  787. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  788. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  789. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  790. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  791. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  792. where 1=1
  793. and a.del_flag = 0
  794. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  795. and e.itemClass = 1 and e.returnFlag is NULL
  796. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  797. and h.type = 1 and h.pay_state = 1
  798. )
  799. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  800. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  801. ) t
  802. where 1=1
  803. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  804. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  805. ) t15
  806. on t0.deptId = t15.deptId
  807.  
  808. -- ------------
  809. -- 卡费 t16
  810. -- ------------
  811. left join
  812. (
  813. select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
  814. (
  815. select
  816. a.data_source,
  817. a.is_appoint_resource,
  818. a.subsequent_visit,
  819. a.dept_id as deptId,
  820.  
  821. a.appointment_state,
  822. a.dept_name as deptName,
  823. a.create_time,
  824. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  825. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  826. f.preFee, -- 原价
  827. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  828. f.discount
  829. from `thc_arrange`.`bpm_appointment` a
  830. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  831. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  832. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  833. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  834. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  835. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  836. where 1=1 and a.del_flag = 0 AND e.itemCode = 'jiuzhenka' -- 就诊卡:卡费
  837. and g.id in (
  838. SELECT DISTINCT g.id
  839. from `thc_arrange`.`bpm_appointment` a
  840. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  841. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  842. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  843. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  844. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  845. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  846. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  847. where 1=1
  848. and a.del_flag = 0
  849. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  850. and e.itemClass = 1 and e.returnFlag is NULL
  851. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  852. and h.type = 1 and h.pay_state = 1
  853. )
  854. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  855. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  856. ) t
  857. where 1=1
  858. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  859. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  860. group by t.deptId
  861. ) t16
  862. on t0.deptId = t16.deptId
  863.  
  864. -- ------------
  865. -- 现金 t17
  866. -- ------------
  867. left join
  868. (
  869. select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
  870. (
  871. select
  872. a.data_source,
  873. a.is_appoint_resource,
  874. a.subsequent_visit,
  875. a.dept_id as deptId,
  876.  
  877. a.appointment_state,
  878. a.dept_name as deptName,
  879. a.create_time,
  880. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  881. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  882. f.preFee, -- 原价
  883. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  884. f.discount,
  885. h.flow_fee,
  886. h.`charge`,
  887. h.`pay_method`
  888. from `thc_arrange`.`bpm_appointment` a
  889. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  890. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  891. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  892. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  893. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  894. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  895. inner join `thc_rcm`.`pay_trade_log` h on h.`settlement_id` = g.id and h.type = 1
  896. where 1=1 and a.del_flag = 0 -- 门诊
  897. and h.type = 1 and h.pay_state = 1 and h.pay_method = 1 -- 银行卡
  898. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  899. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  900. ) t
  901. where 1=1
  902. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  903. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  904. group by t.deptId
  905. ) t17
  906. on t0.deptId = t17.deptId
  907.  
  908. -- ------------
  909. -- 银行卡 t18
  910. -- ------------
  911. left join
  912. (
  913. select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
  914. (
  915. select
  916. a.data_source,
  917. a.is_appoint_resource,
  918. a.subsequent_visit,
  919. a.dept_id as deptId,
  920.  
  921. a.appointment_state,
  922. a.dept_name as deptName,
  923. a.create_time,
  924. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  925. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  926. f.preFee, -- 原价
  927. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  928. f.discount,
  929. h.flow_fee,
  930. h.`charge`,
  931. h.`pay_method`
  932. from `thc_arrange`.`bpm_appointment` a
  933. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  934. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  935. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  936. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  937. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  938. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  939. inner join `thc_rcm`.`pay_trade_log` h on h.`settlement_id` = g.id and h.type = 1
  940. where 1=1 and a.del_flag = 0
  941. and h.type = 1 and h.pay_state = 1 and h.pay_method = 2 -- 银行卡
  942. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  943. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  944. ) t
  945. where 1=1
  946. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  947. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  948. group by t.deptId
  949. ) t18
  950. on t0.deptId = t18.deptId
  951.  
  952. -- ------------
  953. -- 预检 t19
  954. -- ------------
  955. left join
  956. (
  957. select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
  958. (
  959. select
  960. a.data_source,
  961. a.is_appoint_resource,
  962. a.subsequent_visit,
  963. a.dept_id as deptId,
  964.  
  965. a.appointment_state,
  966. a.dept_name as deptName,
  967. a.create_time,
  968. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  969. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  970. f.preFee, -- 原价
  971. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  972. f.discount
  973. from `thc_arrange`.`bpm_appointment` a
  974. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  975. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  976. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  977. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  978. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  979. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  980.  
  981. where 1=1 and a.del_flag = 0 and e.itemCode = 'yujian' -- 预检:预存款
  982. and g.id in (
  983. SELECT DISTINCT g.id
  984. from `thc_arrange`.`bpm_appointment` a
  985. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  986. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  987. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  988. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  989. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  990. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  991. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  992. where 1=1
  993. and a.del_flag = 0
  994. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  995. and e.itemClass = 1 and e.returnFlag is NULL
  996. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  997. and h.type = 1 and h.pay_state = 1
  998. )
  999. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  1000. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  1001. ) t
  1002. where 1=1
  1003. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  1004. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  1005. group by t.deptId
  1006. ) t19
  1007. on t0.deptId = t19.deptId
  1008.  
  1009. -- ------------
  1010. -- 微信金额t20
  1011. -- ------------
  1012. left join
  1013. (
  1014. select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
  1015. (
  1016. select
  1017. a.data_source,
  1018. a.is_appoint_resource,
  1019. a.subsequent_visit,
  1020. a.dept_id as deptId,
  1021.  
  1022. a.appointment_state,
  1023. a.dept_name as deptName,
  1024. a.create_time,
  1025. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  1026. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  1027. f.preFee, -- 原价
  1028. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  1029. f.discount
  1030. from `thc_arrange`.`bpm_appointment` a
  1031. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  1032. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  1033. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  1034. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  1035. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  1036. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  1037. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  1038. where 1=1 and a.del_flag = 0 and a.data_source = 2
  1039. and h.type = 1 and h.pay_state = 1 and h.pay_method=4
  1040. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  1041. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  1042. ) t
  1043. where 1=1
  1044. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  1045. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  1046. group by t.deptId
  1047. ) t20
  1048. on t0.deptId = t20.deptId
  1049.  
  1050. -- ------------
  1051. -- 应收金额 t21
  1052. -- ------------
  1053. left join
  1054. (
  1055. select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
  1056. (
  1057. select
  1058. a.data_source,
  1059. a.is_appoint_resource,
  1060. a.subsequent_visit,
  1061. a.dept_id as deptId,
  1062.  
  1063. a.appointment_state,
  1064. a.dept_name as deptName,
  1065. a.create_time,
  1066. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  1067. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  1068. f.preFee, -- 原价
  1069. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  1070. f.discount
  1071. from `thc_arrange`.`bpm_appointment` a
  1072. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  1073. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  1074. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  1075. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  1076. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  1077. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  1078. where 1=1 and a.del_flag = 0
  1079. and g.id in (
  1080. SELECT DISTINCT g.id
  1081. from `thc_arrange`.`bpm_appointment` a
  1082. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  1083. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  1084. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  1085. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  1086. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  1087. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  1088. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  1089. where 1=1
  1090. and a.del_flag = 0
  1091. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  1092. and e.itemClass = 1 and e.returnFlag is NULL
  1093. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  1094. and h.type = 1 and h.pay_state = 1
  1095. )
  1096. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  1097. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  1098. ) t
  1099. where 1=1
  1100. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  1101. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  1102. group by t.deptId
  1103. ) t21
  1104. on t0.deptId = t21.deptId
  1105.  
  1106. -- ------------
  1107. -- 实收金额 t22
  1108. -- ------------
  1109. left join
  1110. (
  1111. select round(sum(t.realFee),2) as num,t.deptId,t.deptName,t.create_time from
  1112. (
  1113. select
  1114. a.data_source,
  1115. a.is_appoint_resource,
  1116. a.subsequent_visit,
  1117. a.dept_id as deptId,
  1118.  
  1119. a.appointment_state,
  1120. a.dept_name as deptName,
  1121. a.create_time,
  1122. CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
  1123. CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
  1124. f.preFee,
  1125. (f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
  1126. f.discount
  1127. from `thc_arrange`.`bpm_appointment` a
  1128. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  1129. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
  1130. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  1131. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
  1132. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  1133. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  1134. where 1=1 and a.del_flag = 0
  1135. and g.id in (
  1136. SELECT DISTINCT g.id
  1137. from `thc_arrange`.`bpm_appointment` a
  1138. inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
  1139. inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
  1140. inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
  1141. inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
  1142. inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
  1143. inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
  1144. inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
  1145. where 1=1
  1146. and a.del_flag = 0
  1147. and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
  1148. and e.itemClass = 1 and e.returnFlag is NULL
  1149. and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
  1150. and h.type = 1 and h.pay_state = 1
  1151. )
  1152. ${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
  1153. ${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
  1154. ) t
  1155. where 1=1
  1156. ${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
  1157. ${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
  1158. group by t.deptId
  1159. ) t22
  1160. on t0.deptId = t22.deptId
  1161.  
  1162. order by t0.startTime asc

海南医院帆软报表 最终版本SQL的更多相关文章

  1. 帆软报表FineReport SQLServer数据库连接失败常见解决方案

    1. 问题描述 帆软报表FineReport客户端连接SQLServer(2000.2005等),常常会出现如下错误:com.microsoft.sqlserver.jdbc.SQLServerExc ...

  2. 帆软报表FineReport中数据连接之Websphere配置JNDI连接

    以oracle9i数据源制作的模板jndi.cpt为例来说明如何在FineReport中的Websphere配置JNDI连接.由于常用服务器的JNDI驱动过大,帆软报表FineReport中没有自带, ...

  3. 帆软报表FineReport中数据连接之Tomcat配置JNDI连接

    1. 问题描述 在帆软报表FineReport中,通过JNDI方式定义数据连接,首先在Tomcat服务器配置好JNDI,然后在设计器中直接调用JNDI的名字,即可成功使用JNDI连接,连接步骤如下: ...

  4. JEECG与帆软报表集成

    将FineReport 集成到自己的web项目中,生成报表,可以方便快捷的和自己的项目融合在一起.简化了利用poi的导出遇到的问题. 1.首先在FR中建立好一个模板      例如:我的这张模板是连接 ...

  5. 帆软报表FineReport中数据连接之Weblogic配置JNDI连接

    1. 制作报表的原理 在帆软报表FineReport设计器中先用JDBC连接到数据库,建立数据库连接,然后用SQL或者其他方法创建数据集,使用数据集制作报表,然后把建立的数据库连接从JDBC连接改成J ...

  6. 帆软报表FineReport中数据连接之Jboss配置JNDI连接

    使用sqlsever 2000数据库数据源来做实例讲解,帆软报表FineReport数据连接中Jboss配置JNDI大概的过程和WEBSPHERE以及WEBLOGIC基本相同,用JDBC连接数据库制作 ...

  7. 帆软报表FineReport中数据连接的JDBC连接池属性问题

    连接池原理 在帆软报表FineReport中,连接池主要由三部分组成:连接池的建立.连接池中连接使用的治理.连接池的关闭.下面就着重讨论这三部分及连接池的配置问题. 1. 连接池原理 连接池技术的核心 ...

  8. 帆软报表(finereport)单元格中各颜色标识的含义

    帆软报表(finereport)单元格中,可根据单元格角标的颜色判断单元格进行的操作 过滤:单元格左下角黄色三角形 条件属性:单元格左上角红色三角形.  控件:单元格右侧中间的各种矩形.  左父格:单 ...

  9. 帆软报表(finereport)安装/配置

    1.首先是安装帆软报表软件 下载地址:http://www.finereport.com/product/download           激活码注册格账号就有了 2.启动软件,新建连接数据库 点 ...

随机推荐

  1. IT项目管理——《人月神话》读后感

    这也许是和候红老师的最后的几节课了吧,侯老师是一个很有思想深度,很关心同学的好老师. 一开学就布置了阅读<人月神话>的作业,说实话,我没有看,以我的速度可能2.3个小时就看完了,但是我觉得 ...

  2. BZOJ3566: [SHOI2014]概率充电器 树形+概率dp

    3566: [SHOI2014]概率充电器 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1888  Solved: 857[Submit][Stat ...

  3. 如何设置 ssh secure shell 支持中文

    只需要设置下/etc/sysconfig/i18n 文件内容如清单 1 所示. 清单 1. 文件内容 1 2 3 LANG="zh_CN.GB18030" SUPPORTED=&q ...

  4. Linux:不同文件相同列字符合并文件(awk函数)

    存在file1.txt,其内容如下: H aa 0 0 1 -9 H bb 0 0 2 -9 H cc 0 0 2 -9 存在file2.txt,其内容如下: H aa 0 0 0 -9 asd qw ...

  5. 第二十四节,TensorFlow下slim库函数的使用以及使用VGG网络进行预训练、迁移学习(附代码)

    在介绍这一节之前,需要你对slim模型库有一些基本了解,具体可以参考第二十二节,TensorFlow中的图片分类模型库slim的使用.数据集处理,这一节我们会详细介绍slim模型库下面的一些函数的使用 ...

  6. ideaJ+maven+javaweb实践: sevlet实现upload&download,javaIO代码

    因为工作的机器不让拷贝出来也不让发邮件出来也不让访问外网,所以文件两个PC挪来挪去很麻烦. 决定写一个网页,只有upload和download ideaJ,maven,java,tomcat 写一个j ...

  7. curl与wget

    curl 和wget 区别 使用方法 可参考 curl vs Wget 1.curl是libcurl这个库支持的,wget是一个纯粹的命令行命令.2.curl支持更多的协议.curl supports ...

  8. 使用rdbtools工具来解析redis rdb文件

    工欲善其事必先利其器,日常工作中,好的工具能够高效的协助我们工作:今天介绍一款用来解析redis rdb文件的工具,非常好用.会之,受用无穷! 一.rdbtools工具介绍 源码地址:https:// ...

  9. 这3周以来的面试总结(C#/.net 智能硬件/物联网)

    2017.3找工作面试记录-第一周 2017.3找工作面试记录-第一周(2) 2017.3找工作面试记录-第二周 2017.4找工作面试记录-第三周 2017.4找工作面试记录-第三周(2)--金蝶 ...

  10. 关于web-root和web-inf的用处

    web-inf下的页面不能直接反问,必须通过servlet页面进行跳转,安全性高. web-root下的页面能直接通过地址栏来访问,安全性低. 哪些页面放在web-root下,哪些放在web-inf下 ...