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

    <resultMap id="BaseResultMap" type="org.xyzh.api.workcase.dto.TbWorkcaseDTO">
        <id column="workcase_id" property="workcaseId" jdbcType="VARCHAR"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="user_id" property="userId" jdbcType="VARCHAR"/>
        <result column="username" property="username" jdbcType="VARCHAR"/>
        <result column="phone" property="phone" jdbcType="VARCHAR"/>
        <result column="type" property="type" jdbcType="VARCHAR"/>
        <result column="device" property="device" jdbcType="VARCHAR"/>
        <result column="device_code" property="deviceCode" jdbcType="VARCHAR"/>
        <result column="imgs" property="imgs" jdbcType="ARRAY" typeHandler="org.xyzh.common.jdbc.handler.StringArrayTypeHandler"/>
        <result column="emergency" property="emergency" jdbcType="VARCHAR"/>
        <result column="status" property="status" jdbcType="VARCHAR"/>
        <result column="processor" property="processor" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <sql id="Base_Column_List">
        workcase_id, optsn, user_id, username, phone, type, device, device_code, imgs,
        emergency, status, processor, creator, create_time, update_time, delete_time, deleted
    </sql>

    <insert id="insertWorkcase" parameterType="org.xyzh.api.workcase.dto.TbWorkcaseDTO">
        INSERT INTO workcase.tb_workcase (
            optsn, workcase_id, user_id, username, phone, type, device, device_code, creator
            <if test="imgs != null">, imgs</if>
            <if test="emergency != null">, emergency</if>
            <if test="status != null">, status</if>
            <if test="processor != null">, processor</if>
        ) VALUES (
            #{optsn}, #{workcaseId}, #{userId}, #{username}, #{phone}, #{type}, #{device}, #{deviceCode}, #{creator}
            <if test="imgs != null">, #{imgs, typeHandler=org.xyzh.common.jdbc.handler.StringArrayTypeHandler}</if>
            <if test="emergency != null">, #{emergency}</if>
            <if test="status != null">, #{status}</if>
            <if test="processor != null">, #{processor}</if>
        )
    </insert>

    <update id="updateWorkcase" parameterType="org.xyzh.api.workcase.dto.TbWorkcaseDTO">
        UPDATE workcase.tb_workcase
        <set>
            <if test="userId != null and userId != ''">user_id = #{userId},</if>
            <if test="username != null and username != ''">username = #{username},</if>
            <if test="phone != null and phone != ''">phone = #{phone},</if>
            <if test="type != null and type != ''">type = #{type},</if>
            <if test="device != null and device != ''">device = #{device},</if>
            <if test="deviceCode != null and deviceCode != ''">device_code = #{deviceCode},</if>
            <if test="imgs != null">imgs = #{imgs, typeHandler=org.xyzh.common.jdbc.handler.StringArrayTypeHandler},</if>
            <if test="emergency != null and emergency != ''">emergency = #{emergency},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="processor != null">processor = #{processor},</if>
            update_time = now()
        </set>
        WHERE workcase_id = #{workcaseId} AND deleted = false
    </update>

    <update id="deleteWorkcase" parameterType="org.xyzh.api.workcase.dto.TbWorkcaseDTO">
        UPDATE workcase.tb_workcase
        SET deleted = true, delete_time = now()
        WHERE workcase_id = #{workcaseId} AND deleted = false
    </update>

    <select id="selectWorkcaseById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_workcase
        WHERE workcase_id = #{workcaseId} AND deleted = false
    </select>

    <select id="selectWorkcaseList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_workcase
        <where>
            <if test="filter.workcaseId != null and filter.workcaseId != ''">
                AND workcase_id = #{filter.workcaseId}
            </if>
            <if test="filter.userId != null and filter.userId != ''">
                AND user_id = #{filter.userId}
            </if>
            <if test="filter.username != null and filter.username != ''">
                AND username LIKE CONCAT('%', #{filter.username}, '%')
            </if>
            <if test="filter.phone != null and filter.phone != ''">
                AND phone = #{filter.phone}
            </if>
            <if test="filter.type != null and filter.type != ''">
                AND type = #{filter.type}
            </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.emergency != null and filter.emergency != ''">
                AND emergency = #{filter.emergency}
            </if>
            <if test="filter.status != null and filter.status != ''">
                AND status = #{filter.status}
            </if>
            <if test="filter.processor != null and filter.processor != ''">
                AND processor = #{filter.processor}
            </if>
            AND deleted = false
        </where>
        ORDER BY create_time DESC
    </select>

    <select id="selectWorkcasePage" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_workcase
        <where>
            <if test="filter.workcaseId != null and filter.workcaseId != ''">
                AND workcase_id = #{filter.workcaseId}
            </if>
            <if test="filter.userId != null and filter.userId != ''">
                AND user_id = #{filter.userId}
            </if>
            <if test="filter.username != null and filter.username != ''">
                AND username LIKE CONCAT('%', #{filter.username}, '%')
            </if>
            <if test="filter.phone != null and filter.phone != ''">
                AND phone = #{filter.phone}
            </if>
            <if test="filter.type != null and filter.type != ''">
                AND type = #{filter.type}
            </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.emergency != null and filter.emergency != ''">
                AND emergency = #{filter.emergency}
            </if>
            <if test="filter.status != null and filter.status != ''">
                AND status = #{filter.status}
            </if>
            <if test="filter.processor != null and filter.processor != ''">
                AND processor = #{filter.processor}
            </if>
            AND deleted = false
        </where>
        ORDER BY create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countWorkcases" resultType="long">
        SELECT COUNT(*)
        FROM workcase.tb_workcase
        <where>
            <if test="filter.workcaseId != null and filter.workcaseId != ''">
                AND workcase_id = #{filter.workcaseId}
            </if>
            <if test="filter.userId != null and filter.userId != ''">
                AND user_id = #{filter.userId}
            </if>
            <if test="filter.username != null and filter.username != ''">
                AND username LIKE CONCAT('%', #{filter.username}, '%')
            </if>
            <if test="filter.phone != null and filter.phone != ''">
                AND phone = #{filter.phone}
            </if>
            <if test="filter.type != null and filter.type != ''">
                AND type = #{filter.type}
            </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.emergency != null and filter.emergency != ''">
                AND emergency = #{filter.emergency}
            </if>
            <if test="filter.status != null and filter.status != ''">
                AND status = #{filter.status}
            </if>
            <if test="filter.processor != null and filter.processor != ''">
                AND processor = #{filter.processor}
            </if>
            AND deleted = false
        </where>
    </select>

</mapper>
