<?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="org.xyzh.workcase.mapper.TbWorkcaseDeviceMapper">

    <resultMap id="BaseResultMap" type="org.xyzh.api.workcase.dto.TbWorkcaseDeviceDTO">
        <id column="workcase_id" property="workcaseId" jdbcType="VARCHAR"/>
        <id column="file_id" property="fileId" jdbcType="VARCHAR"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="device" property="device" jdbcType="VARCHAR"/>
        <result column="device_code" property="deviceCode" jdbcType="VARCHAR"/>
        <result column="file_name" property="fileName" jdbcType="VARCHAR"/>
        <result column="file_root_id" property="fileRootId" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        workcase_id, file_id, optsn, device, device_code, file_name, file_root_id
    </sql>

    <insert id="insertWorkcaseDevice" parameterType="org.xyzh.api.workcase.dto.TbWorkcaseDeviceDTO">
        INSERT INTO workcase.tb_workcase_device (
            optsn, workcase_id, device, file_id, file_name
            <if test="deviceCode != null">, device_code</if>
            <if test="fileRootId != null">, file_root_id</if>
        ) VALUES (
            #{optsn}, #{workcaseId}, #{device}, #{fileId}, #{fileName}
            <if test="deviceCode != null">, #{deviceCode}</if>
            <if test="fileRootId != null">, #{fileRootId}</if>
        )
    </insert>

    <update id="updateWorkcaseDevice" parameterType="org.xyzh.api.workcase.dto.TbWorkcaseDeviceDTO">
        UPDATE workcase.tb_workcase_device
        <set>
            <if test="device != null and device != ''">device = #{device},</if>
            <if test="deviceCode != null">device_code = #{deviceCode},</if>
            <if test="fileName != null and fileName != ''">file_name = #{fileName},</if>
            <if test="fileRootId != null">file_root_id = #{fileRootId},</if>
        </set>
        WHERE workcase_id = #{workcaseId} AND file_id = #{fileId}
    </update>

    <delete id="deleteWorkcaseDevice">
        DELETE FROM workcase.tb_workcase_device
        WHERE workcase_id = #{workcaseId} AND file_id = #{fileId}
    </delete>

    <select id="selectWorkcaseDeviceById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_workcase_device
        WHERE workcase_id = #{workcaseId} AND file_id = #{fileId}
    </select>

    <select id="selectWorkcaseDeviceList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_workcase_device
        <where>
            <if test="filter.workcaseId != null and filter.workcaseId != ''">
                AND workcase_id = #{filter.workcaseId}
            </if>
            <if test="filter.device != null and filter.device != ''">
                AND device LIKE CONCAT('%', #{filter.device}, '%')
            </if>
            <if test="filter.deviceCode != null and filter.deviceCode != ''">
                AND device_code = #{filter.deviceCode}
            </if>
            <if test="filter.fileId != null and filter.fileId != ''">
                AND file_id = #{filter.fileId}
            </if>
            <if test="filter.fileName != null and filter.fileName != ''">
                AND file_name LIKE CONCAT('%', #{filter.fileName}, '%')
            </if>
            <if test="filter.fileRootId != null and filter.fileRootId != ''">
                AND file_root_id = #{filter.fileRootId}
            </if>
        </where>
        ORDER BY workcase_id, file_id
    </select>

    <select id="selectWorkcaseDevicePage" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_workcase_device
        <where>
            <if test="filter.workcaseId != null and filter.workcaseId != ''">
                AND workcase_id = #{filter.workcaseId}
            </if>
            <if test="filter.device != null and filter.device != ''">
                AND device LIKE CONCAT('%', #{filter.device}, '%')
            </if>
            <if test="filter.deviceCode != null and filter.deviceCode != ''">
                AND device_code = #{filter.deviceCode}
            </if>
            <if test="filter.fileId != null and filter.fileId != ''">
                AND file_id = #{filter.fileId}
            </if>
            <if test="filter.fileName != null and filter.fileName != ''">
                AND file_name LIKE CONCAT('%', #{filter.fileName}, '%')
            </if>
            <if test="filter.fileRootId != null and filter.fileRootId != ''">
                AND file_root_id = #{filter.fileRootId}
            </if>
        </where>
        ORDER BY workcase_id, file_id
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countWorkcaseDevices" resultType="long">
        SELECT COUNT(*)
        FROM workcase.tb_workcase_device
        <where>
            <if test="filter.workcaseId != null and filter.workcaseId != ''">
                AND workcase_id = #{filter.workcaseId}
            </if>
            <if test="filter.device != null and filter.device != ''">
                AND device LIKE CONCAT('%', #{filter.device}, '%')
            </if>
            <if test="filter.deviceCode != null and filter.deviceCode != ''">
                AND device_code = #{filter.deviceCode}
            </if>
            <if test="filter.fileId != null and filter.fileId != ''">
                AND file_id = #{filter.fileId}
            </if>
            <if test="filter.fileName != null and filter.fileName != ''">
                AND file_name LIKE CONCAT('%', #{filter.fileName}, '%')
            </if>
            <if test="filter.fileRootId != null and filter.fileRootId != ''">
                AND file_root_id = #{filter.fileRootId}
            </if>
        </where>
    </select>

</mapper>
