<?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.zehong.system.mapper.TRepairOrderMapper">
    
    <resultMap type="TRepairOrderVo" id="TRepairOrderResult">
        <result property="id"    column="id"    />
        <result property="repairName"    column="repair_name"    />
        <result property="repairCode"    column="repair_code"    />
        <result property="deviceId"    column="device_id"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="deviceName"    column="device_name"    />
        <result property="deviceType"    column="device_type"    />
        <result property="troubleDescribe"    column="trouble_describe"    />
        <result property="fileUrl"    column="file_url"    />
        <result property="orderStatus"    column="order_status"    />
        <result property="isDel"    column="is_del"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remarks"    column="remarks"    />
    </resultMap>

    <select id="getRepairOrderCode" resultType="String">
        select nextval('seq_repair_order_code') as order_code
    </select>

    <sql id="selectTRepairOrderVo">
        select t.id, t.repair_name, t.repair_code, t.device_id, t.trouble_describe, t.file_url, t.order_status, t.is_del, t.create_time, t.update_time, t.remarks,
        d.device_name, d.device_code, d.device_type
        from t_repair_order t
        left join t_device_info d on t.device_id = d.id
    </sql>

    <select id="selectTRepairOrderList" parameterType="TRepairOrder" resultMap="TRepairOrderResult">
        <include refid="selectTRepairOrderVo"/>
        <where>  t.is_del = '0'
            <if test="repairName != null  and repairName != ''"> and t.repair_name like concat('%', #{repairName}, '%')</if>
            <if test="repairCode != null  and repairCode != ''"> and t.repair_code like concat('%', #{repairCode}, '%')</if>
            <if test="deviceId != null  and deviceId != ''"> and t.device_id = #{deviceId}</if>
            <if test="troubleDescribe != null  and troubleDescribe != ''"> and t.trouble_describe = #{troubleDescribe}</if>
            <if test="orderStatus != null  and orderStatus != ''"> and t.order_status = #{orderStatus}</if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                AND date_format(t.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                AND date_format(t.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
            </if>
        </where>
    </select>
    
    <select id="selectTRepairOrderById" parameterType="Long" resultMap="TRepairOrderResult">
        <include refid="selectTRepairOrderVo"/>
        where t.id = #{id}
    </select>
        
    <insert id="insertTRepairOrder" parameterType="TRepairOrder" useGeneratedKeys="true" keyProperty="id">
        insert into t_repair_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="repairName != null">repair_name,</if>
            <if test="repairCode != null">repair_code,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="troubleDescribe != null">trouble_describe,</if>
            <if test="fileUrl != null">file_url,</if>
            <if test="orderStatus != null">order_status,</if>
            <if test="isDel != null">is_del,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remarks != null">remarks,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="repairName != null">#{repairName},</if>
            <if test="repairCode != null">#{repairCode},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="troubleDescribe != null">#{troubleDescribe},</if>
            <if test="fileUrl != null">#{fileUrl},</if>
            <if test="orderStatus != null">#{orderStatus},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remarks != null">#{remarks},</if>
         </trim>
    </insert>

    <update id="updateTRepairOrder" parameterType="TRepairOrder">
        update t_repair_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="repairName != null">repair_name = #{repairName},</if>
            <if test="repairCode != null">repair_code = #{repairCode},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="troubleDescribe != null">trouble_describe = #{troubleDescribe},</if>
            <if test="fileUrl != null">file_url = #{fileUrl},</if>
            <if test="orderStatus != null">order_status = #{orderStatus},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTRepairOrderById" parameterType="Long">
        delete from t_repair_order where id = #{id}
    </delete>

    <delete id="deleteTRepairOrderByIds" parameterType="String">
        delete from t_repair_order where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>