<?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.SpeCheckHiddenMapper">

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


    <!-- 字段 -->
    <sql id="Field">
        f.dangerId,
        f.companyCode,
        f.specheckRecordId,
        f.specheckSourceId,
        f.specheckTaskId,
        f.taskId,
        f.checkItemId,
        f.checkScoreId,
        f.checkType,
        f.checkDate,
        f.checkPay,
        f.harmType,
        f.deleted,
        f.createBy,
        f.createDate,
        f.updateBy,
        f.updateDate,
        f.batchId,
        f.batchStatus,
        f.checkScore,
        f.checkContent,
        f.checkBasis,
        f.applicablePlace,
        f.deductPoint,
        f.failReason,
        f.resultTime,
        f.deductIllustrate
    </sql>

    <!-- 字段用于新增 -->
    <sql id="Field2">
        dangerId,
        companyCode,
        specheckRecordId,
        specheckSourceId,
        specheckTaskId,
        taskId,
        checkItemId,
        checkScoreId,
        checkType,
        checkDate,
        checkPay,
        harmType,
        deleted,
        createBy,
        createDate,
        updateBy,
        updateDate,
        batchId,
        batchStatus,
        checkScore,
        checkContent,
        checkBasis,
        applicablePlace,
        deductPoint,
        failReason,
        resultTime,
        deductIllustrate
    </sql>
    <sql id="FieldValue2">
        #{item.dangerId},
        #{item.companyCode},
        #{item.specheckRecordId},
        #{item.specheckSourceId},
        #{item.specheckTaskId},
        #{item.taskId},
        #{item.checkItemId},
        #{item.checkScoreId},
        #{item.checkType},
        #{item.checkDate},
        #{item.checkPay},
        #{item.harmType},
        #{item.deleted},
        #{item.createBy},
        #{item.createDate},
        #{item.updateBy},
        #{item.updateDate},
        #{item.batchId},
        #{item.batchStatus},
        #{item.checkScore},
        #{item.checkContent},
        #{item.checkBasis},
        #{item.applicablePlace},
        #{item.deductPoint},
        #{item.failReason},
        #{item.resultTime},
        #{item.deductIllustrate}
    </sql>
    <!-- 字段值 -->
    <sql id="FieldValue">
        dangerId,
        companyCode,
        specheckRecordId,
        specheckSourceId,
        specheckTaskId,
        taskId,
        checkItemId,
        checkScoreId,
        checkType,
        checkDate,
        checkPay,
        harmType,
        deleted,
        createBy,
        createDate,
        updateBy,
        updateDate,
        batchId,
        batchStatus,
        checkScore,
        checkContent,
        checkBasis,
        applicablePlace,
        deductPoint,
        failReason,
        resultTime,
        deductIllustrate
    </sql>

    <!-- 新增-->
    <insert id="save" parameterType="pd">
        insert into
        <include refid="tableName"></include>
        (
        <include refid="Field2"></include>
        ) values (
        <include refid="FieldValue"></include>
        )
    </insert>
    <insert id="saveBatch">
        insert into
        <include refid="tableName"></include>
        (<include refid="Field2"></include>) values
        <foreach item="item" index="index" collection="list" separator=",">
            (<include refid="FieldValue2"></include>)
        </foreach>
    </insert>
    <insert id="saveHiddenByStaging">
        insert into
        <include refid="tableName"></include>
        (
        dangerId,
        taskId,
        companyCode,
        specheckRecordId,
        specheckSourceId,
        specheckTaskId,
        checkType,
        checkItemId,
        checkScoreId,
        checkDate,
        checkPay,
        harmType,
        deleted,
        createBy,
        createDate,
        updateBy,
        updateDate,
        checkContent,
        checkBasis,
        isReject,
        applicablePlace,
        checkScore,
        deductIllustrate,
        deductPoint
        ) values (
        #{HIDDEN_ID},
        #{TASK_ID},
        #{COMPANY_CODE},
        #{specheckRecordId},
        #{SPECHECK_SOURCE_ID},
        #{SPECHECK_TASK_ID},
        #{CHECK_TYPE},
        #{CHECK_ITEM_ID},
        #{CHECK_SCORE_ID},
        #{CREATTIME},
        #{CHECK_PAY},
        #{HARM_TYPE},
        #{deleted},
        #{createBy},
        #{createDate},
        #{updateBy},
        #{updateDate},
        #{CHECK_CONTENT},
        #{CHECK_BASIS},
        #{IS_REJECT},
        #{applicablePlace},
        #{checkScore},
        #{DEDUCTILLUSTRATE},
        #{DEDUCT_POINT}
        )

    </insert>
    <update id="addRecordIdByHiddenId">
        UPDATE specheck_hidden
        SET `specheckRecordId` = #{specheckRecordId}
        WHERE deleted = 0 and `dangerId` in
        <foreach collection="hiddenList" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
    <update id="saveReportedResult">
        UPDATE specheck_hidden
        SET `batchStatus` = #{batchStatus},
            `resultTime`  = #{resultTime}
        WHERE `batchId` = #{batchId}
    </update>
    <update id="changebatchStatus">
        UPDATE specheck_hidden
        SET `batchStatus` = #{batchStatus},
            `batchId`     = #{batchId}
        WHERE `dangerId` = #{dangerId}
    </update>
    <update id="removeHiddenStaging">
        update specheck_hidden
        set deleted = 1
        where dangerId = #{HIDDEN_ID}
    </update>
    <update id="editHiddenByStaging">
        UPDATE specheck_hidden
        SET checkPay         = #{CHECK_PAY},
            deductIllustrate = #{DEDUCTILLUSTRATE},
            createBy         = #{createBy},
            createDate       = #{createDate},
            updateBy         = #{updateBy},
            updateDate       = #{updateDate}
        WHERE dangerId = #{HIDDEN_ID}

    </update>
    <update id="removeChangeHidden">
        update specheck_hidden
        set deleted = '1'
        where
        ( specheckTaskId = #{SPECHECK_TASK_ID} and specheckSourceId = #{specheckSourceId}
        <if test="hiddenList != null and hiddenList.size() > 0">
            and dangerId not in
            <foreach collection="hiddenList" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        )
    </update>
    <update id="removeNoHasRecordId">
        update specheck_hidden
        set deleted = 1
        where (specheckRecordId is null or specheckRecordId = '')
          and specheckSourceId = #{specheckSourceId}
          and specheckTaskId = #{specheckTaskId}
    </update>


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

    <!-- 列表 -->
    <select id="datalistPage" parameterType="page" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        f
        where f.deleted = '0'
        <if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
            and
            (
            <!--	根据需求自己加检索条件
                字段1 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
                 or
                字段2 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            -->
            )
        </if>
    </select>

    <!-- 列表(全部) -->
    <select id="listAll" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        f
        where 1=1
        <if test="dangerId != null and dangerId != ''">
            and dangerId = #{dangerId}
        </if>
    </select>
    <select id="listAllByTaskId" resultType="com.zcloud.entity.PageData">
        select * from specheck_hidden where deleted = 0
        <if test="tastId != null and tastId != ''">
            and tastId = #{taskId}
        </if>
        <if test="checkItemId != null and checkItemId != ''">
            and checkItemId = #{checkItemId}
        </if>
        <if test="specheckRecordId != null and specheckRecordId != ''">
            and specheckRecordId = #{specheckRecordId}
        </if>
    </select>
    <select id="listAllByRecordId" resultType="com.zcloud.entity.PageData">
        select *
        from specheck_hidden
        where deleted = 0
          and specheckRecordId = #{specheckRecordId}
    </select>
    <select id="getDangerIdsBySpecheckRecordIds" resultType="com.zcloud.entity.PageData">
        select
        <include refid="Field2"></include>
        from
        <include refid="tableName"></include>
        f
        where f.deleted = 0
        and f.specheckRecordId = #{specheckRecordId}
    </select>
</mapper>
