<?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.reg.server.registration.RegistrationMapper">

    <resultMap id="registrationResultMap" type="com.reg.server.registration.Registration">
        <id property="id" column="id"/>
        <result property="registerTime" column="register_time"/>
        <result property="customerName" column="customer_name"/>
        <result property="contact" column="contact"/>
        <result property="serviceType" column="service_type"/>
        <result property="powerType" column="power_type"/>
        <result property="note" column="note"/>
        <result property="status" column="status"/>
        <result property="fileName" column="file_name"/>
        <result property="urlPath" column="url_path" />
        <result property="msgId" column="msg_id" />
    </resultMap>

    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO registrations (register_time, customer_name, contact, service_type, power_type, note, status)
        VALUES (#{registerTime}, #{customerName}, #{contact}, #{serviceType}, #{powerType}, #{note}, #{status})
    </insert>

    <select id="findById" resultMap="registrationResultMap">
        SELECT id, register_time, customer_name, contact, service_type, power_type, note, status, file_name,url_path,msg_id
        FROM registrations WHERE id = #{id}
    </select>

    <select id="findAll" resultMap="registrationResultMap">
        SELECT id, register_time, customer_name, contact, service_type, power_type, note, status, file_name,url_path,msg_id
        FROM registrations ORDER BY register_time DESC
    </select>

    <update id="update">
        UPDATE registrations
        SET register_time = #{registerTime},
            customer_name = #{customerName},
            contact = #{contact},
            service_type = #{serviceType},
            power_type = #{powerType},
            note = #{note},
            status = #{status},
            file_name = #{fileName},
        url_path  = #{urlPath},
        msg_id = #{msgId}
        WHERE id = #{id}
    </update>

    <delete id="delete">
        DELETE FROM registrations WHERE id = #{id}
    </delete>

    <select id="findByCondition" resultMap="registrationResultMap">
        SELECT id, register_time, customer_name, contact, service_type, power_type, note, status, file_name
        FROM registrations
        <where>
            <if test="customerName != null and customerName != ''">
                AND customer_name LIKE CONCAT('%', #{customerName}, '%')
            </if>
            <if test="contact != null and contact != ''">
                AND contact LIKE CONCAT('%', #{contact}, '%')
            </if>
            <if test="serviceType != null and serviceType != ''">
                AND service_type = #{serviceType}
            </if>
            <if test="powerType != null and powerType != ''">
                AND power_type = #{powerType}
            </if>
            <if test="status != null and status != ''">
                AND status = #{status}
            </if>
            <if test="urlPath != null and urlPath != ''">
                AND url_path = #{urlPath}
            </if>
        </where>
        ORDER BY register_time DESC
    </select>

</mapper>