<?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.api.manage.dao.basicArchives.UserCarDao">
    <select id="list" parameterType="com.api.model.basicArchives.SearchUserCar" resultType="com.api.vo.basicArchives.VoUserCar">
        select uc.*,cps.code as parking_space_code,concat_ws('-',cb.no,cbu.no,cbue.room_number) as room_name
        from cpm_building_unit_estate as cbue,cpm_building_unit as cbu,cpm_building as cb,
        user_car as uc left join cpm_parking_space as cps
        on uc.parking_space_id = cps.id
        <where>
            cbue.id = uc.building_unit_estate_id
            and cbu.id = cbue.building_unit_id
            and cb.id = cbu.building_id
            <if test="code != null and code != ''">
                and uc.code like concat('%',#{code},'%')
            </if>
            <if test="tel != null and tel != ''">
                and uc.tel like concat('%',#{tel},'%')
            </if>
            <if test="parkingSpaceCode != null and parkingSpaceCode != ''">
                and cps.code like concat('%',#{parkingSpaceCode},'%')
            </if>
        </where>
    </select>

    <insert id="insert" parameterType="com.api.model.basicArchives.UserCar">
        insert into
        <trim prefix="user_car(" suffix=")" suffixOverrides=",">
            id,
            building_unit_estate_id,
            code,
            status,
            owner,

            <if test="idType != null">
                id_type,
            </if>
            <if test="idNumber != null and idNumber != ''">
                id_number,
            </if>
            <if test="tel != null and tel != ''">
                tel,
            </if>
            <if test="parkingSpaceId != null">
                parking_space_id,
            </if>
            <if test="type != null">
                type,
            </if>
            <if test="brand != null and brand != ''">
                brand,
            </if>
            <if test="model != null and model != ''">
                model,
            </if>
            <if test="color != null">
                color,
            </if>
            create_id,
            create_date,
            <if test="effectiveTimeStart != null">
                effective_time_start,
            </if>
            <if test="effectiveTimeEnd != null">
                effective_time_end,
            </if>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            #{id},
            #{buildingUnitEstateId},
            #{code},
            #{status},
            #{owner},
            <if test="idType != null">
                #{idType},
            </if>
            <if test="idNumber != null and idNumber != ''">
                #{idNumber},
            </if>
            <if test="tel != null and tel != ''">
                #{tel},
            </if>
            <if test="parkingSpaceId != null">
                #{parkingSpaceId},
            </if>
            <if test="type != null">
                #{type},
            </if>
            <if test="brand != null and brand != ''">
                #{brand},
            </if>
            <if test="model != null and model != ''">
                #{model},
            </if>
            <if test="color != null">
                #{color},
            </if>
            #{createId},
            #{createDate},
            <if test="effectiveTimeStart != null">
                #{effectiveTimeStart},
            </if>
            <if test="effectiveTimeEnd != null">
                #{effectiveTimeEnd},
            </if>
        </trim>
    </insert>

    <select id="findById" parameterType="java.lang.Integer" resultType="com.api.vo.basicArchives.VoUserCarFindById">
        select uc.*,cps.code as parking_space_code
        from user_car as uc
        left join cpm_parking_space as cps
        on uc.parking_space_id = cps.id
        where uc.id = #{id}
    </select>

    <update id="update" parameterType="com.api.model.basicArchives.UserCar">
        update user_car
        <set>
            <if test="buildingUnitEstateId != null">
                building_unit_estate_id = #{buildingUnitEstateId},
            </if>
            <if test="code != null and code != ''">
                code = #{code},
            </if>
            <if test="parkingSpaceId != null">
                parking_space_id = #{parkingSpaceId},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="owner != null and owner != ''">
                owner = #{owner},
            </if>
            <if test="idType != null">
                id_type = #{idType},
            </if>
            <if test="idNumber != null and idNumber != ''">
                id_number = #{idNumber},
            </if>
            <if test="tel != null and tel != ''">
                tel = #{tel},
            </if>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="brand != null and brand != ''">
                brand = #{brand},
            </if>
            <if test="model != null and model != ''">
                model = #{model},
            </if>
            <if test="color != null">
                color = #{color},
            </if>
                modify_id = #{modifyId},
                modify_date = #{modifyDate},
            <if test="effectiveTimeStart != null">
                effective_time_start = #{effectiveTimeStart},
            </if>
            <if test="effectiveTimeEnd != null">
                effective_time_end = #{effectiveTimeEnd},
            </if>
        </set>
        where id = #{id}
    </update>

    <delete id="delete" parameterType="java.lang.Integer">
        delete from user_car
        where id = #{id}
    </delete>

    <select id="findByCode" parameterType="java.lang.String" resultType="com.api.model.basicArchives.UserCar">
        select * from user_car where code = #{code}
    </select>
</mapper>