海南医院帆软报表 最终版本SQL

时间:2023-03-09 19:22:10
海南医院帆软报表 最终版本SQL

1. 挂号微信收入明细

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

2.挂号患者信息查询

-- -----------------------
-- 患者信息查询
-- -----------------------
select DISTINCT
CASE a.appointment_state WHEN -1 THEN '是' ELSE '否' END AS returnFlag, -- 退号标志
CASE a.is_appoint_resource WHEN 0 THEN '否' WHEN 1 THEN '是' END as isAppointResource, -- 指定标志
a.create_time as guaHaoTime, -- 挂号时间
p.create_time as createDocTime, -- 建档时间
-- 挂号实收金额
round((f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount,2) AS realFee,
round(t1.realFee,2) as menZenRealMoney, -- 门诊实收金额
j.outpatient_number as blNumber, -- 病历号
p.name as patientName, -- 患者姓名
a.dept_name, -- 科室名称
a.appointment_doctor_name as doctorName, -- 医生
CASE a.subsequent_visit -- 初复诊
WHEN 0 THEN '初诊'
WHEN 1 THEN '复诊'
WHEN 2 THEN '转诊'
WHEN 3 THEN '急诊'
WHEN 4 THEN '体检'
WHEN 5 THEN '简易'
WHEN 6 THEN '疫苗'
WHEN 7 THEN '团队体检'
END AS isReVisit, -- 初复诊
channel.name as sourceName, -- 信息(渠道)来源
p.birthday, -- 出生日期
if(i.id_no is null, if(i.other_type is null, null,
(select e.name from `thc_warehouse`.`sys_type_info` e
JOIN `thc_warehouse`.`sys_type` f ON e.sys_type_id = f.id
WHERE f.`code` = 'THC_WH_PERSON_CARD' and e.value = i.other_type)
), '身份证') as cardType, -- 证件类型
if(i.id_no is null,i.other_no,i.id_no) as cardNo, -- 证件号码
CONCAT(t2.province,t2.city,t2.county,t2.addressDetail) as liveAddr, -- 住址
CONCAT(t2.province2,t2.city2,t2.county2,t2.addressDetail2) as bornAddr, -- 户籍
if(locate('区',t2.county2) > 0,t2.county2,'') as qu, -- 区
a.patient_phone,
(select t.docname from (
SELECT u.id AS docid,u1.property_value AS docname
FROM thc_warehouse.staff_record u
LEFT JOIN thc_warehouse.staff_record_property u1 ON u1.property_code = 'SXX000083' AND u.id = u1.staff_record_id
) t where t.docid = a.creator) AS creater,
-- a.appointment_starttime,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime, -- 预约时间
-- CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
a.referrer_dept_name, -- 爱院科室
a.referrer_name, -- 爱院人
a.description -- 备注
FROM `thc_arrange`.`bpm_appointment` a
INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
INNER JOIN `thc_passport`.`patient` p on a.patient_id = p.id
INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
LEFT JOIN `thc_c_union`.`member_channel` channel on a.channel_id = channel.id
LEFT JOIN `thc_passport`.`contact` i on a.patient_id = i.patient_id
LEFT JOIN `thc_passport`.`patient_org` j on a.patient_id = j.patient_id left join
(
select
aa.id,
round((ff.preFee - ff.discountFee - ff.promotionBenefitFee - ff.couponFee - ff.itemBenefitFee - ff.memberCardBenefitFee - ff.itemComInvBenefitFee) * ff.discount,2) AS realFee
from `thc_arrange`.`bpm_appointment` aa
inner join `thc_sob`.`bpm_service_order` bb on aa.orderId = bb.id
inner join `thc_sob`.`bpm_service_order_item` cc on cc.service_order_id = bb.id and cc.id = aa.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` ee on dd.id = ee.AccountBillId and ee.itemClass = 1 and ee.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` ff on ff.accountBillID = dd.id and ff.accountBillDetailID = ee.id
inner join `thc_rcm`.`Cs_Settlement` gg on gg.id = ff.settlementID and gg.`isDelete` = 0 and gg.returnFlag = 0
and gg.settlementType=1 -- 门诊收费
where 1=1 and aa.del_flag = 0
) t1 on a.id = t1.id left join
(
select
aa.id,
-- 住址
(select city.name from `thc_passport`.`city` where city.id =
(if(p.address is null,null, TRIM(BOTH '"' FROM JSON_EXTRACT(p.address,'$.province'))))) as province,
(select city.name from `thc_passport`.`city` where city.id =
(if(p.address is null,null, TRIM(BOTH '"' FROM JSON_EXTRACT(p.address,'$.city'))))) as city,
(select city.name from `thc_passport`.`city` where city.id =
(if(p.address is null,null, TRIM(BOTH '"' FROM JSON_EXTRACT(p.address,'$.county'))))) as county, if(p.address is null,null, TRIM(BOTH '"' FROM JSON_EXTRACT(p.address,'$.addressDetail'))) as addressDetail, -- 户籍
(select city.name from `thc_passport`.`city` where city.id = (if(p.household is null,null, TRIM(BOTH '"' FROM JSON_EXTRACT(p.household,'$.province'))))) as province2,
(select city.name from `thc_passport`.`city` where city.id =
(if(p.household is null,null, TRIM(BOTH '"' FROM JSON_EXTRACT(p.household,'$.city'))))) as city2,
(select city.name from `thc_passport`.`city` where city.id =
(if(p.household is null,null, TRIM(BOTH '"' FROM JSON_EXTRACT(p.household,'$.county'))))) as county2,
if(p.household is null,null, TRIM(BOTH '"' FROM JSON_EXTRACT(p.household,'$.addressDetail'))) as addressDetail2,
round((ff.preFee - ff.discountFee - ff.promotionBenefitFee - ff.couponFee - ff.itemBenefitFee - ff.memberCardBenefitFee - ff.itemComInvBenefitFee) * ff.discount,2) AS realFee
from `thc_arrange`.`bpm_appointment` aa
inner join `thc_sob`.`bpm_service_order` bb on aa.orderId = bb.id
inner join `thc_sob`.`bpm_service_order_item` cc on cc.service_order_id = bb.id and cc.id = aa.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` ee on dd.id = ee.AccountBillId and ee.itemClass = 1 and ee.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` ff on ff.accountBillID = dd.id and ff.accountBillDetailID = ee.id
inner join `thc_rcm`.`Cs_Settlement` gg on gg.id = ff.settlementID and gg.`isDelete` = 0 and gg.returnFlag = 0 -- 门诊挂号
and gg.settlementType=2 -- 非挂号消费类型
inner join `thc_passport`.`patient` p on aa.patient_id = p.id
where 1=1 and aa.del_flag = 0 ) t2 on a.id = t2.id WHERE 1=1
AND a.del_flag = 0
AND d.isDelete = 0
AND d.orderSource = 1
AND d.orderType = 3
AND d.returnFlag = 0
AND e.itemClass = 1
AND e.returnFlag is NULL
AND g.settlementType=2
AND g.isDelete = 0
AND g.returnFlag = 0 -- 门诊挂号 ${if(len(startTime) == 0,"","AND a.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND a.create_time <= '" + endTime + "'")}
-- 爱院标志
${if(loveYuan == 0,"and a.appointment_state is null","")}
${if(loveYuan == 1,"and a.appointment_state is not null","")} ${if(len(deptId) == 0,"","AND a.dept_id = '" + deptId + "'")} -- 部门
${if(len(doctor) == 0,"","AND a.appointment_doctor_id = '" + doctor + "'")} -- 医生
${if(len(channelId) == 0,"","AND a.channel_id = '" + channelId + "'")} -- 信息来源
${if(len(patientName) == 0,"","AND p.name like '%" + patientName + "%'")} -- 姓名
${if(len(blNumber) == 0,"","AND j.outpatient_number like '%" + blNumber + "%'")} -- 病历号
${if(len(phone) == 0,"","AND a.patient_phone like '%" + phone + "%'")} -- 电话号
${if(len(subVisit) == 0,"","AND a.subsequent_visit = '" + subVisit + "'")} -- 初复诊
${if(len(doctor) == 0,"","and a.appointment_doctor_id = '" + doctor + "'")} -- 接诊员
${if(isReturn == -1,"and a.appointment_state = -1","")} -- 退号标志
${if(isReturn == 0,"and a.appointment_state != -1","")} -- 退号标志

 3.挂号按来源统计

select
channel.name as name,
DATE_FORMAT(a.appointment_date,${if(dateType == 1,"'%Y-%m'","'%Y-%m-%d'")}) as date, -- 按日期还是月份展示
IFNULL(count(1),0) as num
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
left join `thc_c_union`.`member_channel` channel on a.channel_id = channel.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
) and DATE_FORMAT(a.appointment_date,'%Y-%m-%d') >= DATE_FORMAT(${if(len(startTime) == 0, "'0000-01-01'" , "'" + startTime + "'" )},'%Y-%m-%d')
and DATE_FORMAT(a.appointment_date,'%Y-%m-%d') <= DATE_FORMAT(${if(len(endTime) == 0,"'9999-01-01'","'" + endTime + "'")},'%Y-%m-%d')
${if(len(subVisit) == 0,"","and a.subsequent_visit = '" + subVisit + "'")}
-- and channel.name is not null
group by date_format(a.appointment_date, ${if(dateType == 1,"'%Y-%m'","'%Y-%m-%d'")}), channel.name -- 按日期还是月份分组查询
order by a.appointment_date DESC, data_source asc

3.1有交易流水的条件

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

4.挂号按医生统计

 select
t0.deptName,
t0.doctorName,
t0.deptId,
t0.doctorId,
t0.startTime,
t0.endTime,
IFNULL(t0.num,0) as num0,
IFNULL(t1.num,0) as num1,
IFNULL(t2.num,0) as num2,
IFNULL(t3.num,0) as num3,
IFNULL(t4.num,0) as num4,
IFNULL(t5.num,0) as num5,
IFNULL(t6.num,0) as num6,
IFNULL(t7.num,0) as num7,
IFNULL(t8.num,0) as num8,
IFNULL(t9.num,0) as num9,
IFNULL(t10.num,0) as num10,
IFNULL(t11.num,0) as num11,
IFNULL(t12.num,0) as num12,
IFNULL(t13.num,0) as num13,
IFNULL(t14.num,0) as num14
from
-- ------------
-- 总挂号数
-- ------------
(
select t.startTime, t.endTime, count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.appointment_state,
a.dept_id as deptId,
a.appointment_doctor_id as doctorId,
a.dept_name as deptName,
a.create_time,
a.appointment_doctor_name as doctorName,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where a.del_flag = 0
AND g.id IN (
SELECT DISTINCT g.id
FROM `thc_arrange`.`bpm_appointment` a
INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
WHERE 1=1
AND a.del_flag = 0
AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
AND e.itemClass = 1 AND e.returnFlag is NULL
AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
AND h.type = 1 and h.pay_state = 1 -- 支付成功
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId,t.doctorId
) t0 -- ------------
-- 退号
-- ------------
left join
(
select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId,
a.appointment_doctor_id as doctorId,
a.appointment_state,
a.dept_name as deptName,
a.create_time,
a.appointment_doctor_name as doctorName,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0
AND g.id IN (
SELECT DISTINCT g.id
FROM `thc_arrange`.`bpm_appointment` a
INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
WHERE 1=1
AND a.del_flag = 0
AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
AND e.itemClass = 1 AND e.returnFlag is NULL
AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
AND h.type = 1 and h.pay_state = 1 -- 支付成功
)
and a.appointment_state = -1
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId,t.doctorId
) t1
on t0.deptId = t1.deptId and t0.doctorId = t1.doctorId -- ------------
-- 实际挂号数 = 初诊数 + 复诊数 + 转诊数
-- ------------
left join
(
select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId,
a.appointment_doctor_id as doctorId,
a.appointment_state,
a.dept_name as deptName,
a.create_time,
a.appointment_doctor_name as doctorName,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where a.del_flag = 0 and a.subsequent_visit in (0,1,2)
AND g.id IN (
SELECT DISTINCT g.id
FROM `thc_arrange`.`bpm_appointment` a
INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
WHERE 1=1
AND a.del_flag = 0
AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
AND e.itemClass = 1 AND e.returnFlag is NULL
AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
AND h.type = 1 and h.pay_state = 1 -- 支付成功
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId,t.doctorId
) t2
on t0.deptId = t2.deptId and t0.doctorId = t2.doctorId -- ------------
-- 指定
-- ------------
left join
(
select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId,
a.appointment_doctor_id as doctorId,
a.appointment_state,
a.dept_name as deptName,
a.create_time,
a.appointment_doctor_name as doctorName,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where a.del_flag = 0 and a.is_appoint_resource = 1
AND g.id IN (
SELECT DISTINCT g.id
FROM `thc_arrange`.`bpm_appointment` a
INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
WHERE 1=1
AND a.del_flag = 0
AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
AND e.itemClass = 1 AND e.returnFlag is NULL
AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
AND h.type = 1 and h.pay_state = 1 -- 支付成功
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId,t.doctorId
) t3
on t0.deptId = t3.deptId and t0.doctorId = t3.doctorId -- ------------
-- 初诊
-- ------------
left join
(
select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId,
a.appointment_doctor_id as doctorId,
a.appointment_state,
a.dept_name as deptName,
a.create_time,
a.appointment_doctor_name as doctorName,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where a.del_flag = 0 and a.subsequent_visit = 0
AND g.id IN (
SELECT DISTINCT g.id
FROM `thc_arrange`.`bpm_appointment` a
INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
WHERE 1=1
AND a.del_flag = 0
AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
AND e.itemClass = 1 AND e.returnFlag is NULL
AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
AND h.type = 1 and h.pay_state = 1 -- 支付成功
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId,t.doctorId
) t4
on t0.deptId = t4.deptId and t0.doctorId = t4.doctorId -- ------------
-- 复诊
-- ------------
left join
(
select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId,
a.appointment_doctor_id as doctorId,
a.appointment_state,
a.dept_name as deptName,
a.create_time,
a.appointment_doctor_name as doctorName,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where a.del_flag = 0 and a.subsequent_visit = 1
AND g.id IN (
SELECT DISTINCT g.id
FROM `thc_arrange`.`bpm_appointment` a
INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
WHERE 1=1
AND a.del_flag = 0
AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
AND e.itemClass = 1 AND e.returnFlag is NULL
AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
AND h.type = 1 and h.pay_state = 1 -- 支付成功
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId,t.doctorId
) t5
on t0.deptId = t5.deptId and t0.doctorId = t5.doctorId -- ------------
-- 体检
-- ------------
left join
(
select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId,
a.appointment_doctor_id as doctorId,
a.appointment_state,
a.dept_name as deptName,
a.create_time,
a.appointment_doctor_name as doctorName,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where a.del_flag = 0 and a.subsequent_visit = 4
AND g.id IN (
SELECT DISTINCT g.id
FROM `thc_arrange`.`bpm_appointment` a
INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
WHERE 1=1
AND a.del_flag = 0
AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
AND e.itemClass = 1 AND e.returnFlag is NULL
AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
AND h.type = 1 and h.pay_state = 1 -- 支付成功
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId,t.doctorId
) t6
on t0.deptId = t6.deptId and t0.doctorId = t6.doctorId -- ------------
-- 团队
-- ------------
left join
(
select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId,
a.appointment_doctor_id as doctorId,
a.appointment_state,
a.dept_name as deptName,
a.create_time,
a.appointment_doctor_name as doctorName,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where a.del_flag = 0 and a.subsequent_visit = 7
AND g.id IN (
SELECT DISTINCT g.id
FROM `thc_arrange`.`bpm_appointment` a
INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
WHERE 1=1
AND a.del_flag = 0
AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
AND e.itemClass = 1 AND e.returnFlag is NULL
AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
AND h.type = 1 and h.pay_state = 1 -- 支付成功
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId,t.doctorId
) t7
on t0.deptId = t7.deptId and t0.doctorId = t7.doctorId -- ------------
-- 简易
-- ------------
left join
(
select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId,
a.appointment_doctor_id as doctorId,
a.appointment_state,
a.dept_name as deptName,
a.create_time,
a.appointment_doctor_name as doctorName,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where a.del_flag = 0 and a.subsequent_visit = 5
AND g.id IN (
SELECT DISTINCT g.id
FROM `thc_arrange`.`bpm_appointment` a
INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
WHERE 1=1
AND a.del_flag = 0
AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
AND e.itemClass = 1 AND e.returnFlag is NULL
AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
AND h.type = 1 and h.pay_state = 1 -- 支付成功
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId,t.doctorId
) t8
on t0.deptId = t8.deptId and t0.doctorId = t8.doctorId -- ------------
-- 转诊
-- ------------
left join
(
select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId,
a.appointment_doctor_id as doctorId,
a.appointment_state,
a.dept_name as deptName,
a.create_time,
a.appointment_doctor_name as doctorName,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where a.del_flag = 0 and a.subsequent_visit = 2
AND g.id IN (
SELECT DISTINCT g.id
FROM `thc_arrange`.`bpm_appointment` a
INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
WHERE 1=1
AND a.del_flag = 0
AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
AND e.itemClass = 1 AND e.returnFlag is NULL
AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
AND h.type = 1 and h.pay_state = 1 -- 支付成功
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId,t.doctorId
) t9
on t0.deptId = t9.deptId and t0.doctorId = t9.doctorId -- ------------
-- 疫苗
-- ------------
left join
(
select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId,
a.appointment_doctor_id as doctorId,
a.appointment_state,
a.dept_name as deptName,
a.create_time,
a.appointment_doctor_name as doctorName,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where a.del_flag = 0 and a.subsequent_visit = 2
AND g.id IN (
SELECT DISTINCT g.id
FROM `thc_arrange`.`bpm_appointment` a
INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
WHERE 1=1
AND a.del_flag = 0
AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
AND e.itemClass = 1 AND e.returnFlag is NULL
AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
AND h.type = 1 and h.pay_state = 1 -- 支付成功
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId,t.doctorId
) t10
on t0.deptId = t10.deptId and t0.doctorId = t10.doctorId -- ------------
-- 微信数
-- ------------
left join
(
select count(1) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId,
a.appointment_doctor_id as doctorId,
a.appointment_state,
a.dept_name as deptName,
a.create_time,
a.appointment_doctor_name as doctorName,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where a.del_flag = 0 and a.data_source = 2
AND g.id IN (
SELECT DISTINCT g.id
FROM `thc_arrange`.`bpm_appointment` a
INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
WHERE 1=1
AND a.del_flag = 0
AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
AND e.itemClass = 1 AND e.returnFlag is NULL
AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
AND h.type = 1 and h.pay_state = 1 -- 支付成功
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId,t.doctorId
) t11
on t0.deptId = t11.deptId and t0.doctorId = t11.doctorId -- ------------
-- 微信金额
-- ------------
left join
(
select round(sum(t.preFee),2) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId,
a.appointment_doctor_id as doctorId,
a.appointment_state,
a.dept_name as deptName,
a.create_time,
a.appointment_doctor_name as doctorName,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where a.del_flag = 0 and a.data_source = 2
AND g.id IN (
SELECT DISTINCT g.id
FROM `thc_arrange`.`bpm_appointment` a
INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
WHERE 1=1
AND a.del_flag = 0
AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
AND e.itemClass = 1 AND e.returnFlag is NULL
AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
AND h.type = 1 and h.pay_state = 1 AND h.pay_method = 4 -- 微信支付成功
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId,t.doctorId
) t12
on t0.deptId = t12.deptId and t0.doctorId = t12.doctorId -- ------------
-- 应收金额 t12
-- ------------
left join
(
select round(sum(t.preFee),2) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId,
a.appointment_doctor_id as doctorId,
a.appointment_state,
a.dept_name as deptName,
a.create_time,
a.appointment_doctor_name as doctorName,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where a.del_flag = 0
AND g.id IN (
SELECT DISTINCT g.id
FROM `thc_arrange`.`bpm_appointment` a
INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
WHERE 1=1
AND a.del_flag = 0
AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
AND e.itemClass = 1 AND e.returnFlag is NULL
AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
AND h.type = 1 and h.pay_state = 1 -- 支付成功
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId,t.doctorId
) t13
on t0.deptId = t13.deptId and t0.doctorId = t13.doctorId -- ------------
-- 实收金额 t13
-- ------------
left join
(
select round(sum(t.realFee),2) as num,t.deptId, doctorId,t.deptName,t.create_time,t.doctorName from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId,
a.appointment_doctor_id as doctorId,
a.appointment_state,
a.dept_name as deptName,
a.create_time,
a.appointment_doctor_name as doctorName,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee,
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where a.del_flag = 0
AND g.id IN (
SELECT DISTINCT g.id
FROM `thc_arrange`.`bpm_appointment` a
INNER JOIN `thc_sob`.`bpm_service_order` b on a.orderId = b.id
INNER JOIN `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id AND c.id = a.order_item_id
INNER JOIN `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
INNER JOIN `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
INNER JOIN `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id AND f.accountBillDetailID = e.id
INNER JOIN `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
INNER JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
WHERE 1=1
AND a.del_flag = 0
AND d.isDelete = 0 AND d.orderSource = 1 AND d.orderType = 3 AND d.returnFlag = 0
AND e.itemClass = 1 AND e.returnFlag is NULL
AND g.settlementType=2 AND g.`isDelete` = 0 AND g.returnFlag = 0 -- 门诊挂号
AND h.type = 1 and h.pay_state = 1 -- 支付成功
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId,t.doctorId
) t14
on t0.deptId = t14.deptId and t0.doctorId = t14.doctorId order by t0.startTime asc

5.挂号按科室统计

 select
t0.deptName,
t0.deptId,
t0.startTime,
t0.endTime,
IFNULL(t0.num,0) as num0,
IFNULL(t1.num,0) as num1,
IFNULL(t2.num,0) as num2,
IFNULL(t4.num,0) as num4,
IFNULL(t5.num,0) as num5,
IFNULL(t6.num,0) as num6,
IFNULL(t7.num,0) as num7,
IFNULL(t8.num,0) as num8,
IFNULL(t9.num,0) as num9,
IFNULL(t10.num,0) as num10,
IFNULL(t11.num,0) as num11,
IFNULL(t13.num,0) as num13,
IFNULL(t14.num,0) as num14,
IFNULL(t15.num,0) as num15,
IFNULL(t16.num,0) as num16,
IFNULL(t17.num,0) as num17,
IFNULL(t18.num,0) as num18,
IFNULL(t19.num,0) as num19,
IFNULL(t20.num,0) as num20,
IFNULL(t21.num,0) as num21,
IFNULL(t22.num,0) as num22
from
-- ------------
-- 总挂号数
-- ------------
(
select t.startTime, t.endTime, count(1) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.appointment_state,
a.dept_id as deptId, a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t0 -- ------------
-- 退号
-- ------------
left join
(
select count(1) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId,
a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0 AND a.appointment_state = -1
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t1
on t0.deptId = t1.deptId -- ------------
-- 实际挂号数 = 初诊数 + 复诊数 + 转诊数
-- ------------
left join
(
select count(1) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0 AND a.subsequent_visit in (0,1,2)
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t2
on t0.deptId = t2.deptId -- ------------
-- 初诊
-- ------------
left join
(
select count(1) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0 AND a.subsequent_visit = 0
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t4
on t0.deptId = t4.deptId -- ------------
-- 复诊
-- ------------
left join
(
select count(1) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0 AND a.subsequent_visit = 1
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t5
on t0.deptId = t5.deptId -- ------------
-- 体检
-- ------------
left join
(
select count(1) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0 AND a.subsequent_visit = 4
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t6
on t0.deptId = t6.deptId -- ------------
-- 团队
-- ------------
left join
(
select count(1) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0 AND a.subsequent_visit = 7
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t7
on t0.deptId = t7.deptId -- ------------
-- 简易
-- ------------
left join
(
select count(1) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0 AND a.subsequent_visit = 5
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t8
on t0.deptId = t8.deptId -- ------------
-- 转诊
-- ------------
left join
(
select count(1) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0 AND a.subsequent_visit = 2
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t9
on t0.deptId = t9.deptId -- ------------
-- 疫苗
-- ------------
left join
(
select count(1) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0 AND a.subsequent_visit = 2
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t10
on t0.deptId = t10.deptId -- ------------
-- t11 微信数
-- ------------
left join
(
select count(1) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0 AND a.data_source = 2
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t11
on t0.deptId = t11.deptId -- ------------
-- 挂号费 t13
-- ------------
left join
(
select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t13
on t0.deptId = t13.deptId -- ------------
-- 急诊费 t14
-- ------------
left join
(
select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id and b.del_flag = 0
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
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.isDelete = 0
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id and e.isDelete = 0
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0 AND e.itemCode = 'jiajifei' -- 加急费:急诊费
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t14
on t0.deptId = t14.deptId -- ------------
-- 工本费 t15
-- ------------
left join
(
select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0 AND e.itemCode = 'bingliben' -- 病历本:工本费
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
) t15
on t0.deptId = t15.deptId -- ------------
-- 卡费 t16
-- ------------
left join
(
select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0 AND e.itemCode = 'jiuzhenka' -- 就诊卡:卡费
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t16
on t0.deptId = t16.deptId -- ------------
-- 现金 t17
-- ------------
left join
(
select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount,
h.flow_fee,
h.`charge`,
h.`pay_method`
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
inner join `thc_rcm`.`pay_trade_log` h on h.`settlement_id` = g.id and h.type = 1
where 1=1 and a.del_flag = 0 -- 门诊
and h.type = 1 and h.pay_state = 1 and h.pay_method = 1 -- 银行卡
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t17
on t0.deptId = t17.deptId -- ------------
-- 银行卡 t18
-- ------------
left join
(
select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount,
h.flow_fee,
h.`charge`,
h.`pay_method`
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
inner join `thc_rcm`.`pay_trade_log` h on h.`settlement_id` = g.id and h.type = 1
where 1=1 and a.del_flag = 0
and h.type = 1 and h.pay_state = 1 and h.pay_method = 2 -- 银行卡
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t18
on t0.deptId = t18.deptId -- ------------
-- 预检 t19
-- ------------
left join
(
select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号 where 1=1 and a.del_flag = 0 and e.itemCode = 'yujian' -- 预检:预存款
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t19
on t0.deptId = t19.deptId -- ------------
-- 微信金额t20
-- ------------
left join
(
select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1 and a.del_flag = 0 and a.data_source = 2
and h.type = 1 and h.pay_state = 1 and h.pay_method=4
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t20
on t0.deptId = t20.deptId -- ------------
-- 应收金额 t21
-- ------------
left join
(
select round(sum(t.preFee),2) as num,t.deptId, t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee, -- 原价
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t21
on t0.deptId = t21.deptId -- ------------
-- 实收金额 t22
-- ------------
left join
(
select round(sum(t.realFee),2) as num,t.deptId,t.deptName,t.create_time from
(
select
a.data_source,
a.is_appoint_resource,
a.subsequent_visit,
a.dept_id as deptId, a.appointment_state,
a.dept_name as deptName,
a.create_time,
CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
f.preFee,
(f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount AS realFee,
f.discount
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.`order_item_id`
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
where 1=1 and a.del_flag = 0
and g.id in (
SELECT DISTINCT g.id
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID
inner JOIN `thc_rcm`.`pay_trade_log` h on h.settlement_id = g.id
where 1=1
and a.del_flag = 0
and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
and e.itemClass = 1 and e.returnFlag is NULL
and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
and h.type = 1 and h.pay_state = 1
)
${if(len(deptId) == 0,"","and a.dept_id = '" + deptId + "'")}
${if(len(doctorId) == 0,"","and a.appointment_doctor_id = '" + doctorId + "'")}
) t
where 1=1
${if(len(startTime) == 0,"","AND t.create_time >= '" + startTime + "'")}
${if(len(endTime) == 0,"","AND t.create_time <= '" + endTime + "'")}
group by t.deptId
) t22
on t0.deptId = t22.deptId order by t0.startTime asc