<?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.zcloud.mapper.datasource.specheck.SpeCheckTaskMapper">

    <!--表名 -->
    <sql id="tableName">
        specheck_task
    </sql>


    <!-- 字段 -->
    <sql id="Field">
        f.specheckTaskId,
        f.taskId,
        f.companyCode,
        f.taskName,
        f.taskStartTime,
        f.taskEndTime,
        f.taskTypeName,
        f.checkType,
        f.taskContent,
        f.deleted,
        f.createDate,
        f.corpinfoId,
        f.updateDate
    </sql>

    <!-- 字段用于新增 -->
    <sql id="Field2">
        specheckTaskId,
        taskId,
        companyCode,
        taskName,
        taskStartTime,
        taskEndTime,
        taskTypeName,
        checkType,
        taskContent,
        deleted,
        createDate,
        corpinfoId,
        updateDate
    </sql>

    <!-- 字段值 -->
    <sql id="FieldValue">
        #{specheckTaskId}
        ,
        #{taskId},
        #{companyCode},
        #{taskName},
        #{taskStartTime},
        #{taskEndTime},
        #{taskTypeName},
        #{checkType},
        #{taskContent},
        #{deleted},
        #{createDate},
        #{corpinfoId},
        #{updateDate}
    </sql>

    <!-- 新增-->
    <insert id="save" parameterType="pd">
        insert into
        <include refid="tableName"></include>
        (
        <include refid="Field2"></include>
        ) values (
        <include refid="FieldValue"></include>
        )
    </insert>
    <insert id="saveByBatch">
        INSERT INTO specheck_task (<include refid="Field2"></include>)
        values
        <foreach collection="list" item="item" separator=",">
            (
            #{item.specheckTaskId},
            #{item.taskId},
            #{item.companyCode},
            #{item.taskName},
            #{item.taskStartTime},
            #{item.taskEndTime},
            #{item.taskTypeName},
            #{item.checkType},
            #{item.taskContent},
            #{item.deleted},
            #{item.createDate},
            #{item.corpinfoId},
            #{item.updateDate}
            )
        </foreach>
        ON DUPLICATE KEY UPDATE taskId = taskId;
    </insert>
    <update id="finishTask">
        UPDATE specheck_task
        SET
            reportDate = #{reportDate}
        WHERE specheckTaskId = #{SPECHECK_TASK_ID}
    </update>


    <!-- 通过ID获取数据 -->
    <select id="findById" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        f
        where
        f.taskId = #{taskId}
    </select>

    <!-- 列表 -->
    <select id="datalistPage" parameterType="page" resultType="pd">
        select
        <include refid="Field"></include>,
        t.taskCount,
        IFNULL(c.reportCount,0) reportCount
        from
        <include refid="tableName"></include>
        f
        LEFT JOIN ( SELECT corpinfoId, specheckTaskId, COUNT( 1 ) taskCount FROM `specheck_source` WHERE isdelete = 0 GROUP BY corpinfoId, specheckTaskId ) t ON f.corpinfoId = t.corpinfoId
        AND t.specheckTaskId = f.specheckTaskId
        LEFT JOIN ( SELECT specheckTaskId, COUNT( 1 ) reportCount FROM specheck_record WHERE deleted = 0 AND batchStatus = 2 GROUP BY specheckTaskId ) c ON f.specheckTaskId = c.specheckTaskId
        where f.deleted = 0 and f.corpinfoId = #{pd.corpinfoId}
        <if test="pd.taskName != null and pd.taskName != ''"><!-- 关键词检索 -->
            and
            (
             f.taskName LIKE CONCAT(CONCAT('%', #{pd.taskName}),'%')
            )
        </if>
        <if test="pd.taskStartTime != null and pd.taskStartTime != ''"><!-- 关键词检索 -->
            AND ( #{pd.taskStartTime} >= f.taskStartTime and #{pd.taskEndTime} &lt;= f.taskEndTime )
        </if>
        <if test="pd.checkType != null and pd.checkType != ''"><!-- 关键词检索 -->
            and f.checkType = #{pd.checkType}
        </if>
        order by f.taskId,f.checkType
    </select>

    <!-- 列表(全部) -->
    <select id="listAll" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        <where>
            <if test="taskId != null and taskId != ''">
                and taskId = #{taskId}
            </if>
            <if test="companyCode != null and companyCode != ''">
                and companyCode = #{companyCode}
            </if>
        </where>
    </select>
    <select id="getCheckCount" resultType="com.zcloud.entity.PageData">
        SELECT
        taskId,
        COUNT(*) AS reportAllCount,
        SUM( CASE WHEN batchStatus = 2 THEN 1 ELSE 0 END ) AS reportSuccessCount
        FROM
        specheck_record
        WHERE
        taskId IN
        <foreach collection="taskIdList" item="item" open="(" separator="," close=")">#{item}</foreach>
        GROUP BY
        taskId;
    </select>
    <select id="listAllTask" resultType="com.zcloud.entity.PageData">
        select *
        from specheck_task
        where deleted = 0
          and corpinfoId = #{corpinfoId}
    </select>
    <select id="getCheckSourcelistPage" resultType="com.zcloud.entity.PageData">
        SELECT
            t.taskId,
            t.taskName,
            t.companyCode,
            t.checkType,
            t.taskTypeName,
            s.*,
            r.RISKUNITNAME,
            r.SOURCE_ID,
            c.batchId,
            c.batchStatus,
            c.failReason,
            c.resultTime
        FROM
            specheck_source s
                LEFT JOIN bus_riskunit r ON s.riskunitId = r.RISKUNIT_ID
                left join specheck_task t on s.specheckTaskId = t.specheckTaskId
                LEFT JOIN specheck_record c ON c.specheckRecordId = s.specheckRecordId
                AND r.ISDELETE = '0'
        WHERE
            s.ISDELETE = 0
          AND s.corpinfoId = #{pd.CORPINFO_ID}
          AND s.specheckTaskId = #{pd.specheckTaskId}
        order by r.RISKUNITNAME
    </select>


</mapper>
