<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.siwei.rsmonitoring.mapper.QkZtMapper">

    <resultMap type="QkCountVo" id="QkCountVoResult">
        <result property="countNumber"    column="count_number"    />
        <result property="countSum"    column="count_sum"    />
    </resultMap>

    <select id="selectYpdtbcfQkZtCount"  resultMap="QkCountVoResult">
        select coalesce(count(*),0) count_number,coalesce(sum(dkmj::numeric), 0) count_sum from (
        select t1.* from (
        select  id,dkmj from t_dkjbxx  where (split_type != '0' or split_type is null)
        <if test="dataCountVo.xzqdms != null  and !dataCountVo.xzqdms.isEmpty()">
            and xzqdm in (
            <foreach collection="dataCountVo.xzqdms" item="xzq"  separator=",">
                #{xzq}
            </foreach>
            )
        </if>
        <if test="dataCountVo.startTime != null  and dataCountVo.startTime != ''"> and hsx &gt;= #{dataCountVo.startTime}</if>
        <if test="dataCountVo.endTime != null  and dataCountVo.endTime != ''"> and hsx &lt;= #{dataCountVo.endTime}</if>
        union all
        select id,dkmj from t_zdkjbxx where id in (
        select  id from t_dkjbxx  where split_type = '0'
        <if test="dataCountVo.xzqdms != null  and !dataCountVo.xzqdms.isEmpty()">
            and xzqdm in (
            <foreach collection="dataCountVo.xzqdms" item="xzq"  separator=",">
                #{xzq}
            </foreach>
            )
        </if>
        <if test="dataCountVo.startTime != null  and dataCountVo.startTime != ''"> and hsx &gt;= #{dataCountVo.startTime}</if>
        <if test="dataCountVo.endTime != null  and dataCountVo.endTime != ''"> and hsx &lt;= #{dataCountVo.endTime}</if>
        )
        )t1 left join t_dkjbxx_state t2 on t1.id = t2.id
        <where>
            <if test="dataCountVo.auditflowList != null  and !dataCountVo.auditflowList.isEmpty()">
                AND (
                <foreach collection="dataCountVo.auditflowList" item="audit"  separator="or">
                    (
                    <if test="audit.auditflowStep != null  and audit.auditflowStep != ''">t2.step_code = #{audit.auditflowStep} </if>
                    <if test="audit.successType != null  and audit.successType != ''"> and t2.success_type = #{audit.successType}</if>
                    )
                </foreach>
                )
            </if>
        </where>
        )t2
    </select>

    <select id="selectQkZtCount"  resultMap="QkCountVoResult">
        select count(*) count_number,coalesce(sum(dkmj::numeric), 0) count_sum from (
        select t1.* from (
        select  * from t_dkjbxx
        <where>
        <if test="dataCountVo.xzqdms != null  and !dataCountVo.xzqdms.isEmpty()">
            and xzqdm in (
            <foreach collection="dataCountVo.xzqdms" item="xzq"  separator=",">
                #{xzq}
            </foreach>
            )
        </if>
        </where>
        )t1 left join t_dkjbxx_state t2 on t1.id = t2.id
        <where>
            <if test="dataCountVo.startTime != null  and dataCountVo.startTime != ''"> and t1.hsx &gt;= #{dataCountVo.startTime}</if>
            <if test="dataCountVo.endTime != null  and dataCountVo.endTime != ''"> and t1.hsx &lt;= #{dataCountVo.endTime}</if>
            <if test="dataCountVo.auditflowList != null  and !dataCountVo.auditflowList.isEmpty()">
                AND (
                <foreach collection="dataCountVo.auditflowList" item="audit"  separator="or">
                    (
                    <if test="audit.auditflowStep != null  and audit.auditflowStep != ''">t2.step_code = #{audit.auditflowStep} </if>
                    <if test="audit.successType != null  and audit.successType != ''"> and t2.success_type = #{audit.successType}</if>
                    )
                </foreach>
                )
            </if>
        </where>
        )t2
    </select>

    <select id="selectSqlCount"  resultType="map">
        select  ${v2_sql} from (
        select  ${v1_sql} from (
        select t1.*,t2.step_code,t2.success_type from (
        select  * from t_dkjbxx
        <where>
            <if test="dataCountVo.xzqdms != null  and !dataCountVo.xzqdms.isEmpty()">
                and xzqdm in (
                <foreach collection="dataCountVo.xzqdms" item="xzq"  separator=",">
                    #{xzq}
                </foreach>
                )
            </if>
            <if test="dataCountVo.startTime != null  and dataCountVo.startTime != ''"> and hsx &gt;= #{dataCountVo.startTime}</if>
            <if test="dataCountVo.endTime != null  and dataCountVo.endTime != ''"> and hsx &lt;= #{dataCountVo.endTime}</if>
        </where>
        )t1 left join t_dkjbxx_state t2 on t1.id = t2.id
        )v1)v2

    </select>


    <select id="selectPdtuSqlCount"  resultType="map">
        select  ${v2_sql} from (
        select  ${v1_sql} from (
        select t1.*,t2.step_code,t2.success_type,v0.decide,v0.decide_type  from (
        select  id,dkmj,fieldwork_id from t_dkjbxx  where (split_type != '0' or split_type is null)
        <if test="dataCountVo.xzqdms != null  and !dataCountVo.xzqdms.isEmpty()">
            and xzqdm in (
            <foreach collection="dataCountVo.xzqdms" item="xzq"  separator=",">
                #{xzq}
            </foreach>
            )
        </if>
        <if test="dataCountVo.startTime != null  and dataCountVo.startTime != ''"> and hsx &gt;= #{dataCountVo.startTime}</if>
        <if test="dataCountVo.endTime != null  and dataCountVo.endTime != ''"> and hsx &lt;= #{dataCountVo.endTime}</if>
        union all
        select id,dkmj,fieldwork_id from t_zdkjbxx where id in (
        select  id from t_dkjbxx  where split_type = '0'
        <if test="dataCountVo.xzqdms != null  and !dataCountVo.xzqdms.isEmpty()">
            and xzqdm in (
            <foreach collection="dataCountVo.xzqdms" item="xzq"  separator=",">
                #{xzq}
            </foreach>
            )
        </if>
        <if test="dataCountVo.startTime != null  and dataCountVo.startTime != ''"> and hsx &gt;= #{dataCountVo.startTime}</if>
        <if test="dataCountVo.endTime != null  and dataCountVo.endTime != ''"> and hsx &lt;= #{dataCountVo.endTime}</if>
        )
        )t1
        left join t_dkjbxx_state t2 on t1.id = t2.id
        left join t_dkjbxx_hgx v0 on v0.id = t1.fieldwork_id
        <where>
            <if test="dataCountVo.auditflowList != null  and !dataCountVo.auditflowList.isEmpty()">
                AND (
                <foreach collection="dataCountVo.auditflowList" item="audit"  separator="or">
                    (
                    <if test="audit.auditflowStep != null  and audit.auditflowStep != ''">t2.step_code = #{audit.auditflowStep} </if>
                    <if test="audit.successType != null  and audit.successType != ''"> and t2.success_type = #{audit.successType}</if>
                    )
                </foreach>
                )
            </if>
        </where>
        )v1)v2
    </select>



</mapper>