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

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


    <!-- 字段 -->
    <sql id="Field">
        f
        .
        specheckRecordId
        ,
        f.specheckSourceId,
        f.companyCode,
        f.hazardCode,
        f.specheckTaskId,
        f.taskId,
        f.checkType,
        f.checkDate,
        f.resultStatus,
        f.deleted,
        f.createBy,
        f.createDate,
        f.updateBy,
        f.updateDate,
        f.batchId,
        f.batchStatus,
        f.userId,
        f.departmentId,
        f.checkContent
    </sql>

    <!-- 字段用于新增 -->
    <sql id="Field2">
        specheckRecordId
        ,
        specheckSourceId,
        companyCode,
        hazardCode,
        specheckTaskId,
        taskId,
        checkType,
        checkDate,
        resultStatus,
        deleted,
        createBy,
        createDate,
        updateBy,
        updateDate,
        batchId,
        batchStatus,
        checkContent,
        departmentId,
        userId,
        checkDeptMent,
        checkUserName
    </sql>
    <sql id="FieldValue2">
        #{item.specheckRecordId}
        ,
        #{item.specheckSourceId},
        #{item.companyCode},
        #{item.hazardCode},
        #{item.specheckTaskId},
        #{item.taskId},
        #{item.checkType},
        #{item.checkDate},
        #{item.resultStatus},
        #{item.deleted},
        #{item.createBy},
        #{item.createDate},
        #{item.updateBy},
        #{item.updateDate},
        #{item.batchId},
        #{item.batchStatus},
        #{item.checkContent},
        #{item.departmentId},
        #{item.userId},
        #{item.checkDeptMent},
        #{item.checkUserName}
    </sql>
    <!-- 字段值 -->
    <sql id="FieldValue">
        #{specheckRecordId},
        #{specheckSourceId},
        #{companyCode},
        #{hazardCode},
        #{specheckTaskId},
        #{taskId},
        #{checkType},
        #{checkDate},
        #{resultStatus},
        #{deleted},
        #{createBy},
        #{createDate},
        #{updateBy},
        #{updateDate},
        #{batchId},
        #{batchStatus},
        #{checkContent},
        #{departmentId},
        #{userId},
        #{checkDeptMent},
        #{checkUserName}
    </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>
    <update id="saveReportedResult">
        UPDATE specheck_record
        SET `batchStatus` = #{batchStatus},
            `resultTime`  = #{resultTime}
        WHERE `batchId` = #{batchId}
    </update>
    <update id="changStatusByBatchSql">
        UPDATE ${tableName} SET `batchStatus` = #{batchStatus}, `resultTime` = #{resultTime}
        WHERE ${primaryKeyName} in
        <foreach collection="changeBatchStatus" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>
    <update id="changebatchStatus">
        UPDATE specheck_record
        SET `batchStatus` = #{batchStatus},
            `batchId`     = #{batchId}
        WHERE `specheckRecordId` = #{specheckRecordId}
    </update>
    <update id="edit">
        UPDATE specheck_record SET
        <if test="checkDate != null and checkDate != ''">
            checkDate = #{checkDate},
        </if>
        <if test="resultStatus != null and resultStatus != ''">
            resultStatus = #{resultStatus},
        </if>
        <if test="createBy != null and createBy != ''">
            createBy = #{createBy},
        </if>
        <if test="createDate != null and createDate != ''">
            createDate = #{createDate},
        </if>
        <if test="updateBy != null and updateBy != ''">
            updateBy = #{updateBy},
        </if>
        <if test="updateDate != null and updateDate != ''">
            updateDate = #{updateDate},
        </if>
        <if test="checkContent != null and checkContent != ''">
            checkContent = #{checkContent},
        </if>
        <if test="departmentId != null and departmentId != ''">
            departmentId = #{departmentId},
        </if>
        <if test="userId != null and userId != ''">
            userId = #{userId},
        </if>
        <if test="checkDeptMent != null and checkDeptMent != ''">
            checkDeptMent = #{checkDeptMent},
        </if>
        <if test="checkUserName != null and checkUserName != ''">
            checkUserName = #{checkUserName},
        </if>
        specheckRecordId = specheckRecordId
        where specheckRecordId = #{specheckRecordId}
    </update>


    <!-- 通过ID获取数据 -->
    <select id="findById" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        f
        where
        f.specheckRecordId = #{specheckRecordId}
    </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.taskId != null and pd.taskId != ''"><!-- 关键词检索 -->
            and f.taskId = #{pd.taskId}
        </if>
        order by f.createDate desc
    </select>

    <!-- 列表(全部) -->
    <select id="listAll" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        f
        where 1=1
        <if test="specheckRecordId != null and specheckRecordId != ''">
            and specheckRecordId = #{specheckRecordId}
        </if>
    </select>
    <select id="getRecordInfo" resultType="com.zcloud.entity.PageData">
        SELECT r.*,
               d.name departMentName,
               u.name userName,
               t.taskName,
               t.taskStartTime,
               t.taskEndTime,
               t.taskTypeName
        FROM specheck_record r
                 LEFT JOIN specheck_task t ON r.specheckTaskId = t.specheckTaskId
        left join oa_department d on r.departmentId = d.DEPARTMENT_ID
        left join sys_user u on r.userId = u.USER_ID
        WHERE r.deleted = 0
          and r.specheckRecordId = #{specheckRecordId}
    </select>
    <select id="getReportingTask" resultType="com.zcloud.entity.PageData">
        select *
        from specheck_record
        where deleted = 0
          and batchStatus = 1
    </select>
    <select id="getReportingHidden" resultType="com.zcloud.entity.PageData">
        select *
        from specheck_hidden
        where deleted = 0
          and batchStatus = 1
    </select>
    <select id="getCheckRecordByStId" resultType="com.zcloud.entity.PageData">
        select * from specheck_record where specheckTaskId = #{specheckTaskId} and deleted = 0
    </select>

</mapper>
