( SELECT DISTINCT master_id, COUNT ( * ) total_back_num FROM Archive_Master_Following WHERE following_type = 9 GROUP BY master_id ) a
LEFT JOIN Archive_Master m ON m.ID= a.master_id
LEFT JOIN ( SELECT code, name FROM emr_dictionary WHERE parent_id = ( SELECT id FROM emr_dictionary WHERE typeCode = 'dept_code' ) ) d ON m.dept_name= d.code
LEFT JOIN emr_fault_detail e ON e.archive_detail_id= m.ID
LEFT JOIN ( SELECT COUNT ( id ) as bnum,archive_detail_id FROM emr_fault_detail WHERE priceTwo = '乙' GROUP BY archive_detail_id ) l ON
l.archive_detail_id=m.ID
LEFT JOIN ( SELECT COUNT ( id ) as cnum,archive_detail_id FROM emr_fault_detail WHERE priceTwo = '丙' GROUP BY archive_detail_id ) l1 ON
l1.archive_detail_id=m.ID
LEFT JOIN ( SELECT sum(cast(priceTwo AS DECIMAL (12,2)))as scoreSum,archive_detail_id FROM emr_fault_detail WHERE priceTwo !='乙' AND priceTwo !='丙' GROUP BY archive_detail_id ) l2 on l2.archive_detail_id=m.ID
<where>
1 = 1
AND m.ArchiveState NOT IN ( '作废', '已封存', '' )
AND m.ArchiveState IS NOT NULL
AND m.Is_Valid!= 1
AND e.priceTwo IS NOT NULL
<iftest="sysflag == 1">
AND l.bnum is null
</if>
<iftest="sysflag == 1">
AND l.bnum is null
</if>
<iftest="sysflag == 2">
AND l.bnum>1 OR l1.cnum is not NULL OR l2.scoreSum>10
</if>
<!--<if test="sysflag != null and sysflag != '' and sysflag = '1'">-->
<!--AND l.bnum is NULL or 2>l.bnum-->
<!--</if>-->
<!--<if test="sysflag != null and sysflag != '' and sysflag = '1'">-->
<!--AND l1.cnum is NULL-->
<!--</if>-->
<!--<if test="sysflag != null and sysflag != '' and sysflag = '1'">-->
<!--AND l2.scoreSum is null-->
<!--</if>-->
<!--<if test="sysflag != null and sysflag != '' and sysflag = '1'">-->
<!--AND 10>scoreSum-->
<!--</if>-->
<!--<if test="sysflag != null and sysflag != '' and sysflag = '2'">-->
<!--AND l.bnum is NULL or 2>l.bnum-->
<!--</if>-->
<!--<if test="sysflag != null and sysflag != '' and sysflag = '2'">-->
<!--AND l1.cnum is NULL-->
<!--</if>-->
<!--<if test="sysflag != null and sysflag != '' and sysflag = '2'">-->
<!--AND l2.scoreSum is null-->
<!--</if>-->
<!--<if test="sysflag != null and sysflag != '' and sysflag = '2'">-->
<!--AND 10>scoreSum-->
<!--</if>-->
<iftest="inpNo != null and inpNo != ''">
and m.inp_no like '%'+#{inpNo,jdbcType=NCHAR}+'%'
</if>
<iftest="visitId != null and visitId != ''">
and m.visit_id=#{visitId,jdbcType=NCHAR}
</if>
<iftest="name != null and name != ''">
and m.name like '%'+#{name,jdbcType=NCHAR}+'%'
</if>
<iftest="archivestate!= null and archivestate!= ''">
(SELECT COUNT(id) FROM emr_fault_detail WHERE priceTwo='乙' AND archive_detail_id= #{masterId}) bnum,
(SELECT COUNT(id) FROM emr_fault_detail WHERE priceTwo='丙' AND archive_detail_id= #{masterId}) cnum,
(SELECT sum(cast(priceTwo AS DECIMAL (12,2))) FROM emr_fault_detail WHERE priceTwo !='乙' AND priceTwo !='丙' AND archive_detail_id= #{masterId}) AS priceSum
FROM
emr_fault_detail
WHERE
archive_detail_id= #{masterId} and priceTwo !='乙' AND priceTwo !='丙'