<?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.ruoyi.mall.mapper.MallGoodsClassMapper">

    <resultMap type="MallGoodsClass" id="MallGoodsClassResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="status"    column="status"    />
        <result property="isHome"    column="is_home"    />
        <result property="icon"    column="icon"    />
        <result property="parentId"    column="parent_id"    />
        <result property="sort"    column="sort"    />
        <result property="createAt"    column="create_at"    />
        <result property="updateAt"    column="update_at"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="showStatus"    column="show_status"    />
    </resultMap>

    <resultMap type="com.ruoyi.mall.domain.vo.GoodsClassVO" id="GoodsClassVOResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="icon"    column="icon"    />
    </resultMap>

    <sql id="selectMallGoodsClassVo">
        select id, name, status, is_home, icon, parent_id, sort, create_at, update_at, del_flag, show_status from mall_goods_class
    </sql>

    <select id="selectMallGoodsClassList" parameterType="MallGoodsClass" resultMap="MallGoodsClassResult">
        <include refid="selectMallGoodsClassVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="parentId != null"> and parent_id = #{parentId}</if>
            <if test="showStatus != null"> and show_status = #{showStatus}</if>
             and del_flag = '0'
        </where>
        order by sort asc
    </select>

    <select id="selectMallGoodsClassById" parameterType="Long" resultMap="MallGoodsClassResult">
        <include refid="selectMallGoodsClassVo"/>
        where id = #{id}
    </select>
    <select id="selectDefaultMallGoodsClasses" parameterType="long" resultMap="GoodsClassVOResult">
        select id, name, icon
        from mall_goods_class
        <where>
            and del_flag = 0
            and status = '0'
            and show_status = 0
            <if test="parentId != null">and parent_id = #{parentId}</if>
        </where>
        order by sort asc
    </select>

    <select id="getClassSelectList" parameterType="long" resultMap="MallGoodsClassResult">
        <include refid="selectMallGoodsClassVo"/>
        <where>
            and del_flag = 0
            and status = '0'
            <if test="parentId != null">and parent_id = #{parentId}</if>
        </where>
        order by sort asc
    </select>

    <insert id="insertMallGoodsClass" parameterType="MallGoodsClass" useGeneratedKeys="true" keyProperty="id">
        insert into mall_goods_class
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="status != null">status,</if>
            <if test="isHome != null">is_home,</if>
            <if test="icon != null">icon,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="sort != null">sort,</if>
            <if test="createAt != null">create_at,</if>
            <if test="updateAt != null">update_at,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="showStatus != null">show_status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="status != null">#{status},</if>
            <if test="isHome != null">#{isHome},</if>
            <if test="icon != null">#{icon},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="sort != null">#{sort},</if>
            <if test="createAt != null">#{createAt},</if>
            <if test="updateAt != null">#{updateAt},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="showStatus != null">#{showStatus},</if>
         </trim>
    </insert>

    <update id="updateMallGoodsClass" parameterType="MallGoodsClass">
        update mall_goods_class
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="status != null">status = #{status},</if>
            <if test="isHome != null">is_home = #{isHome},</if>
            <if test="icon != null">icon = #{icon},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="createAt != null">create_at = #{createAt},</if>
            <if test="updateAt != null">update_at = #{updateAt},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="showStatus != null">show_status = #{showStatus},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMallGoodsClassById" parameterType="Long">
        delete from mall_goods_class where id = #{id}
    </delete>

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

    <select id="getDropdownClassList" parameterType="MallGoodsClass" resultMap="MallGoodsClassResult">
        <include refid="selectMallGoodsClassVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="classGrade != null  and status != '' and classGrade == 1">
                and parent_id = #{parentId}
            </if>
            <if test="classGrade != null  and status != '' and classGrade == 2">
                and parent_id != 0
            </if>
            and del_flag = '0'
        </where>
        order by sort asc, create_at desc
    </select>
</mapper>
