<?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.dgtly.goods.mapper.GoodsTypeMapper">
    
    <resultMap type="GoodsType" id="GoodsTypeResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="remark"    column="remark"    />
        <collection  property="goodsTypeAttributes"   javaType="java.util.List"       resultMap="GoodsTypeAttributeResult" />
    </resultMap>

    <resultMap type="GoodsTypeAttribute" id="GoodsTypeAttributeResult">
        <result property="id"    column="attributeId"    />
        <result property="typeId"    column="type_id"    />
        <result property="attributeName"    column="attribute_name"    />
        <result property="sort"    column="sort"    />
    </resultMap>

    <sql id="selectGoodsTypeVo">
        select gt.id, gt.name, gt.parent_id, gt.ancestors, gt.create_time, gt.create_by, gt.update_time, gt.update_by, gt.remark,
                gta.id attributeId,gta.type_id,gta.attribute_name,gta.sort
        from goods_type gt
        left join goods_type_attribute gta on gta.type_id = gt.id
    </sql>

    <select id="selectGoodsTypeList" parameterType="GoodsType" resultMap="GoodsTypeResult">
        <include refid="selectGoodsTypeVo"/>
        <where>  
            <if test="name != null  and name != ''"> and gt.name like concat('%', #{name}, '%')</if>
            <if test="parentId != null "> and gt.parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''"> and gt.ancestors = #{ancestors}</if>
        </where>
    </select>
    
    <select id="selectGoodsTypeById" parameterType="Long" resultMap="GoodsTypeResult">
        <include refid="selectGoodsTypeVo"/>
        where gt.id = #{id}
    </select>
        
    <insert id="insertGoodsType" parameterType="GoodsType" useGeneratedKeys="true" keyProperty="id">
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
            select LAST_INSERT_ID();
        </selectKey>
        insert into goods_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null  and name != ''">name,</if>
            <if test="parentId != null ">parent_id,</if>
            <if test="ancestors != null  and ancestors != ''">ancestors,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="createBy != null  and createBy != ''">create_by,</if>
            <if test="updateTime != null ">update_time,</if>
            <if test="updateBy != null  and updateBy != ''">update_by,</if>
            <if test="remark != null  and remark != ''">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null  and name != ''">#{name},</if>
            <if test="parentId != null ">#{parentId},</if>
            <if test="ancestors != null  and ancestors != ''">#{ancestors},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="createBy != null  and createBy != ''">#{createBy},</if>
            <if test="updateTime != null ">#{updateTime},</if>
            <if test="updateBy != null  and updateBy != ''">#{updateBy},</if>
            <if test="remark != null  and remark != ''">#{remark},</if>
         </trim>
    </insert>

    <update id="updateGoodsType" parameterType="GoodsType">
        update goods_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null  and name != ''">name = #{name},</if>
            <if test="parentId != null ">parent_id = #{parentId},</if>
            <if test="ancestors != null  and ancestors != ''">ancestors = #{ancestors},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
            <if test="createBy != null  and createBy != ''">create_by = #{createBy},</if>
            <if test="updateTime != null ">update_time = #{updateTime},</if>
            <if test="updateBy != null  and updateBy != ''">update_by = #{updateBy},</if>
            <if test="remark != null  and remark != ''">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteGoodsTypeById" parameterType="Long">
        delete from goods_type where id = #{id}
    </delete>

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

    <select id="selectGoodsTypeCount" parameterType="GoodsType" resultType="int" >
        select  count(1) from goods_type
        <where>
            <if test="name != null  and name != ''"> and name = #{name}</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors like concat('%', #{ancestors}, '%')</if>
        </where>
    </select>
    
</mapper>