<?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.huashe.park.core.mapper.ConsUnitMachineRelMapper">

    <resultMap type="ConsUnitMachineRel" id="ConsUnitMachineRelResult">
        <result property="id" column="id"/>
        <result property="consUnitId" column="cons_unit_id"/>
        <result property="machineId" column="machine_id"/>
        <result property="unitAncestors" column="unit_ancestors"/>
        <result property="updateTime" column="update_time"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateBy" column="update_by"/>
    </resultMap>

    <sql id="selectConsUnitMachineRelVo">
        select id,
               cons_unit_id,
               machine_id,
               unit_ancestors,
               update_time,
               create_time,
               create_by,
               update_by
        from cons_cons_unit_machine_rel
    </sql>

    <select id="selectConsUnitMachineRelList" parameterType="ConsUnitMachineRel" resultMap="ConsUnitMachineRelResult">
        <include refid="selectConsUnitMachineRelVo"/>
        <where>
        </where>
    </select>

    <select id="selectConsUnitMachineRelById" parameterType="Long" resultMap="ConsUnitMachineRelResult">
        <include refid="selectConsUnitMachineRelVo"/>
        where id = #{id}
    </select>

    <insert id="insertConsUnitMachineRel" parameterType="ConsUnitMachineRel" useGeneratedKeys="true" keyProperty="id">
        insert into cons_cons_unit_machine_rel
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="consUnitId != null">cons_unit_id,</if>
            <if test="machineId != null">machine_id,</if>
            <if test="unitAncestors != null">unit_ancestors,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="consUnitId != null">#{consUnitId},</if>
            <if test="machineId != null">#{machineId},</if>
            <if test="unitAncestors != null">#{unitAncestors},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
        </trim>
    </insert>

    <update id="updateConsUnitMachineRel" parameterType="ConsUnitMachineRel">
        update cons_cons_unit_machine_rel
        <trim prefix="SET" suffixOverrides=",">
            <if test="consUnitId != null">cons_unit_id = #{consUnitId},</if>
            <if test="machineId != null">machine_id = #{machineId},</if>
            <if test="unitAncestors != null">unit_ancestors = #{unitAncestors},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteConsUnitMachineRelById" parameterType="Long">
        delete
        from cons_cons_unit_machine_rel
        where id = #{id}
    </delete>

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

    <insert id="insertConsUnitMachineRelList" parameterType="List">
        INSERT INTO cons_cons_unit_machine_rel (cons_unit_id,
        machine_id,
        unit_ancestors,
        update_time,
        create_time)
        VALUES
        <foreach collection="consUnitMachines" item="item" separator=",">
            (
            #{item.consUnitId},
            #{item.machineId},
            #{item.unitAncestors},
            now(),
            now()
            )
        </foreach>
    </insert>
    <delete id="deleteConsUnitMachineRelByUnitId" parameterType="Long">
        delete
        from cons_cons_unit_machine_rel
        where cons_unit_id = #{id}
    </delete>

    <select id="qryConsUnitMachineRelByUnitId" parameterType="Long" resultType="Long">
        select machine_id
        from cons_cons_unit_machine_rel
        where cons_unit_id = #{id}
    </select>
</mapper>