<?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.tuoheng.admin.mapper.InspectionFileMapper">
    
    <resultMap type="com.tuoheng.admin.entity.InspectionFile" id="InspectionFileResult">
        <result property="id"    column="id"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="fileCode"    column="file_code"    />
        <result property="inspectionId"    column="inspection_id"    />
        <result property="fileType"    column="file_type"    />
        <result property="fileName"    column="file_name"    />
        <result property="fileThumbnail"    column="file_thumbnail"    />
        <result property="fileOriginal"    column="file_original"    />
        <result property="fileImage"    column="file_image"    />
        <result property="fileSize"    column="file_size"    />
        <result property="latitude"    column="latitude"    />
        <result property="longitude"    column="longitude"    />
        <result property="location"    column="location"    />
        <result property="gaodeLongitude"    column="gaode_longitude"    />
        <result property="gaodeLatitude"    column="gaode_latitude"    />
        <result property="gaodeAddress"    column="gaode_address"    />
        <result property="questionCode"    column="question_code"    />
        <result property="source"    column="source"    />
        <result property="questionName"    column="question_name"    />
        <result property="questionDesc"    column="question_desc"    />
        <result property="status"    column="status"    />
        <result property="checkUser"    column="check_user"    />
        <result property="checkTime"    column="check_time"    />
        <result property="createUser"    column="create_user"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateUser"    column="update_user"    />
        <result property="updateTime"    column="update_time"    />
        <result property="mark"    column="mark"    />
    </resultMap>

    <resultMap type="com.tuoheng.admin.entity.InspectionFileExtend" id="InspectionFileResult2">
        <result property="id"    column="id"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="fileCode"    column="file_code"    />
        <result property="inspectionId"    column="inspection_id"    />
        <result property="fileType"    column="file_type"    />
        <result property="fileName"    column="file_name"    />
        <result property="fileThumbnail"    column="file_thumbnail"    />
        <result property="fileOriginal"    column="file_original"    />
        <result property="fileImage"    column="file_image"    />
        <result property="fileSize"    column="file_size"    />
        <result property="latitude"    column="latitude"    />
        <result property="longitude"    column="longitude"    />
        <result property="location"    column="location"    />
        <result property="gaodeLongitude"    column="gaode_longitude"    />
        <result property="gaodeLatitude"    column="gaode_latitude"    />
        <result property="gaodeAddress"    column="gaode_address"    />
        <result property="questionCode"    column="question_code"    />
        <result property="source"    column="source"    />
        <result property="questionName"    column="question_name"    />
        <result property="questionDesc"    column="question_desc"    />
        <result property="status"    column="status"    />
        <result property="checkUser"    column="check_user"    />
        <result property="checkTime"    column="check_time"    />
        <result property="createUser"    column="create_user"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateUser"    column="update_user"    />
        <result property="updateTime"    column="update_time"    />
        <result property="mark"    column="mark"    />
        <result property="inspectionCode"    column="inspection_code"    />
        <result property="inspectionName"    column="inspection_name"    />
        <result property="channelId"    column="channel_id"    />
        <result property="deptId"    column="dept_id"    />
    </resultMap>

    <resultMap type="com.tuoheng.admin.entity.InspectionFileDistribution" id="InspectionFileDistributionResult">
        <result property="id"    column="id"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="fileType"    column="file_type"    />
        <result property="fileName"    column="file_name"    />
        <result property="fileThumbnail"    column="file_thumbnail"    />
        <result property="fileOriginal"    column="file_original"    />
        <result property="fileImage"    column="file_image"    />
        <result property="fileSize"    column="file_size"    />
        <result property="latitude"    column="latitude"    />
        <result property="longitude"    column="longitude"    />
        <result property="location"    column="location"    />
        <result property="gaodeLongitude"    column="gaode_longitude"    />
        <result property="gaodeLatitude"    column="gaode_latitude"    />
        <result property="gaodeAddress"    column="gaode_address"    />
        <result property="questionCode"    column="question_code"    />
        <result property="questionName"    column="question_name"    />
        <result property="status"    column="status"    />
        <result property="inspectionCode"    column="inspection_code"    />
        <result property="inspectionName"    column="inspection_name"    />
    </resultMap>

    <sql id="Base_Column_List">
        id, tenant_id, file_code, inspection_id, file_type, file_name, file_thumbnail, file_original, file_image, file_size, latitude, longitude, location, gaode_longitude, gaode_latitude, gaode_address, question_code, source, question_name, question_desc, status, check_user, check_time, create_user, create_time, update_user, update_time, mark
    </sql>

    <sql id="selectThInspectionFileVo">
        select id, tenant_id, file_code, inspection_id, file_type, file_name, file_thumbnail, file_original, file_image, file_size, latitude, longitude, location, gaode_longitude, gaode_latitude, gaode_address, question_code, source, question_name, question_desc, status, check_user, check_time, create_user, create_time, update_user, update_time, mark from th_inspection_file
    </sql>

    <sql id="Base_Extend_Column_List">
        tif.id, tif.tenant_id, tif.file_code, tif.inspection_id, tif.file_type, tif.file_name, tif.file_thumbnail, tif.file_original, tif.file_image, tif.file_size,
        tif.latitude, tif.longitude, tif.location, tif.gaode_longitude, tif.gaode_latitude, tif.gaode_address, tif.source, tif.question_code, tif.question_name, tif.question_desc, tif.status,
        tif.check_user, tif.check_time, tif.create_user, tif.create_time, tif.update_user, tif.update_time, tif.mark,
        ti.code as inspection_code, ti.name as inspection_name, ti.channel_id, ti.dept_id
    </sql>

    <sql id="Base_Distribution_Column_List">
        tif.id, tif.tenant_id, tif.file_type, tif.file_name, tif.file_thumbnail, tif.file_original, tif.file_image, tif.file_size,
        tif.latitude, tif.longitude, tif.location, tif.gaode_longitude, tif.gaode_latitude, tif.gaode_address, tif.question_code, tif.question_name,
        tif.status, ti.code as inspection_code, ti.name as inspection_name
    </sql>

    <!-- 批量新增问题图片 -->
    <insert id="addBatch" parameterType="com.tuoheng.admin.entity.InspectionFile">
        insert into th_inspection_file (id,file_code, inspection_id, file_type, file_name,
        file_thumbnail, file_original, file_image, file_size, latitude, longitude, `location`, gaode_longitude, gaode_latitude,question_id,
        gaode_address, `source`, question_code, question_name, question_desc, `status`, check_user, check_time, create_user, tenant_id,mark)
        values
        <foreach collection="list" item="item" separator=",">
            (
            #{item.id,jdbcType=VARCHAR},
            #{item.fileCode,jdbcType=VARCHAR},
            #{item.inspectionId,jdbcType=INTEGER},
            #{item.fileType,jdbcType=BOOLEAN},
            #{item.fileName,jdbcType=VARCHAR},
            #{item.fileThumbnail,jdbcType=VARCHAR},
            #{item.fileOriginal,jdbcType=VARCHAR},
            #{item.fileImage,jdbcType=VARCHAR},
            #{item.fileSize},
            #{item.latitude,jdbcType=VARCHAR},
            #{item.longitude,jdbcType=VARCHAR},
            #{item.location,jdbcType=VARCHAR},
            #{item.gaodeLongitude,jdbcType=VARCHAR},
            #{item.gaodeLatitude,jdbcType=VARCHAR},
            #{item.questionId,jdbcType=VARCHAR},
            #{item.gaodeAddress,jdbcType=VARCHAR},
            #{item.source,jdbcType=BOOLEAN},
            #{item.questionCode,jdbcType=VARCHAR},
            #{item.questionName,jdbcType=VARCHAR},
            #{item.questionDesc,jdbcType=VARCHAR},
            #{item.status,jdbcType=TINYINT},
            #{item.checkUser,jdbcType=INTEGER},
            #{item.checkTime,jdbcType=TIMESTAMP},
            #{item.createUser,jdbcType=INTEGER},
            #{item.tenantId,jdbcType=INTEGER},
            #{item.mark,jdbcType=TINYINT}
            )
        </foreach>
    </insert>

    <update id="deleteLogicByMap" parameterType="hashmap">
        update th_inspection_file
        <trim prefix="SET" suffixOverrides=",">
            <if test="updateUser != null and updateUser != ''"> update_user = #{updateUser}, </if>
            <if test="updateTime != null"> update_time = #{updateTime}, </if>
            <if test="mark != null"> mark = #{mark},</if>
        </trim>
        <where>
            <if test="inspectionId != null and inspectionId != ''"> inspection_id = #{inspectionId} </if>
        </where>
    </update>

    <delete id="deleteByInspectionId" parameterType="String">
        delete from th_inspection_file where inspection_id = #{id}
    </delete>

    <select id="selectListByInspectIdList" resultType="list" resultMap="InspectionFileResult">
        select <include refid="Base_Column_List"/>
        from th_inspection_file
        where mark = 1 and status in(15, 20, 25) and
        inspection_id in
        <foreach collection="list" item="inspectionId" separator="," open="(" close=")">
            #{inspectionId}
        </foreach>
    </select>

    <select id="selectPageListByInspectionId" parameterType="com.tuoheng.admin.request.inspectionfile.QueryInspectionFilePageListByInspectionIdRequest" resultMap="InspectionFileResult">
        select <include refid="Base_Column_List"/>
        from th_inspection_file
        <where>
            <if test="1 == 1"> and mark = 1 </if>
            <if test="request.inspectionId != null and request.inspectionId != ''"> and inspection_id = #{request.inspectionId} </if>
            <if test="request.questionCode != null and request.questionCode != ''"> and question_Code = #{request.questionCode} </if>
            <choose>
                <when test="request.status != null and request.status != 0">
                    and status = #{request.status}
                </when>
                <otherwise>
                    and status in (5, 10, 15, 20, 25)
                </otherwise>
            </choose>
        </where>
        order by create_time desc
    </select>

    <select id="selectPageList" parameterType="com.tuoheng.admin.request.inspectionfile.QueryInspectionFilePageListRequest" resultMap="InspectionFileResult2">
        select <include refid="Base_Extend_Column_List"/>
        from th_inspection_file tif
        left join th_inspection ti on tif.inspection_id = ti.id
        <where>
            <if test="1 == 1"> and tif.mark = 1 </if>
            <if test="request.key != null and request.key != ''"> and (ti.code like concat('%', #{request.key}, '%') or ti.name like concat('%', #{request.key}, '%')) </if>
            <if test="request.tenantId != null and request.tenantId != ''"> and tif.tenant_id = #{request.tenantId} </if>
            <if test="request.questionCode != null and request.questionCode != ''"> and tif.question_Code = #{request.questionCode} </if>
            <choose>
                <when test="request.status != null and request.status != 0">
                    and tif.status = #{request.status}
                </when>
                <otherwise>
                    and tif.status in (15, 20, 25)
                </otherwise>
            </choose>
            <if test="request.deptIdList != null and request.deptIdList.size() > 0">
                and ti.dept_id in
                <foreach item="deptId" collection="request.deptIdList" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>
            <if test="request.beginTime != null">
                and tif.check_time &gt;= #{request.beginTime}
            </if>
            <if test="request.endTime != null">
                and tif.check_time &lt;= #{request.endTime}
            </if>
        </where>
        order by tif.create_time desc
    </select>

    <select id="selectDistributionList" parameterType="com.tuoheng.admin.request.inspectionfile.QueryInspectionFileDistributionListRequest" resultMap="InspectionFileDistributionResult">
        select <include refid="Base_Distribution_Column_List"/>
        from th_inspection_file tif
        left join th_inspection ti on tif.inspection_id = ti.id
        <where>
            <if test="1 == 1"> and tif.mark = 1 </if>
            <if test="request.key != null and request.key != ''"> and (ti.code like concat('%', #{request.key}, '%') or ti.name like concat('%', #{request.key}, '%')) </if>
            <if test="request.tenantId != null and request.tenantId != ''"> and tif.tenant_id = #{request.tenantId} </if>
            <if test="request.questionCode != null and request.questionCode != ''"> and tif.question_code = #{request.questionCode} </if>
            <if test="request.status != null">
                <choose>
                    <when test="request.status != 0">
                        and tif.status = #{request.status}
                    </when>
                    <otherwise>
                        and tif.status in (15, 20, 25)
                    </otherwise>
                </choose>
            </if>
            <if test="request.deptIdList != null and request.deptIdList.size() > 0">
                and ti.dept_id in
                <foreach item="deptId" collection="request.deptIdList" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>
        </where>
        order by tif.create_time desc
    </select>

    <update id="updateByIdList" parameterType="hashmap">
        update th_inspection_file
        <trim prefix="SET" suffixOverrides=",">
            <if test="status != null"> status = #{status}, </if>
            <if test="updateUser != null and updateUser != ''"> update_user = #{updateUser}, </if>
            <if test="updateTime != null"> update_time = #{updateTime}, </if>
            <if test="checkUser != null and checkUser != ''"> check_user = #{checkUser}, </if>
            <if test="checkTime != null"> check_time = #{checkTime}, </if>
        </trim>
        <where>
            <if test="idList != null and idList.size() > 0">
                and id in
                <foreach collection="idList" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </update>
</mapper>