<?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.kmall.admin.dao.CategoryDao">

    <resultMap type="com.kmall.admin.entity.CategoryEntity" id="categoryMap">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
		<result property="storeId" column="store_id"/>
		<result property="merchSn" column="merch_sn"/>
        <result property="keywords" column="keywords"/>
        <result property="frontDesc" column="front_desc"/>
        <result property="parentId" column="parent_id"/>
        <result property="sortOrder" column="sort_order"/>
        <result property="showIndex" column="show_index"/>
        <result property="isShow" column="is_show"/>
        <result property="bannerUrl" column="banner_url"/>
        <result property="iconUrl" column="icon_url"/>
        <result property="imgUrl" column="img_url"/>
        <result property="wapBannerUrl" column="wap_banner_url"/>
        <result property="level" column="level"/>
        <result property="type" column="type"/>
        <result property="frontName" column="front_name"/>
		<result property="storeName" column="storeName"/>
		<result column="merchName" property="merchName" />
    </resultMap>

	<select id="queryObject" resultType="com.kmall.admin.entity.CategoryEntity">
		select
			`id`,
			`name`,
			`store_id`,
			`merch_sn`,
			`keywords`,
			`front_desc`,
			`parent_id`,
			`sort_order`,
			`show_index`,
			`is_show`,
			`banner_url`,
			`icon_url`,
			`img_url`,
			`wap_banner_url`,
			`level`,
			`type`,
			`front_name`,
			`is_show` as `show`
		from mall_category
		where id = #{id}
	</select>
	<select id="queryObjectByName" resultType="com.kmall.admin.entity.CategoryEntity">
		select
		`id`,
		`name`,
		`store_id`,
		`merch_sn`,
		`keywords`,
		`front_desc`,
		`parent_id`,
		`sort_order`,
		`show_index`,
		`is_show`,
		`banner_url`,
		`icon_url`,
		`img_url`,
		`wap_banner_url`,
		`level`,
		`type`,
		`front_name`,
		`is_show` as `show`
		from mall_category
		where name = #{cateGoryName}
		<if test="merchSn != null and merchSn.trim() != ''">
			AND merch_sn = #{merchSn}
		</if> and is_show = 1
	</select>

	<select id="queryObjectByStoreId" resultType="com.kmall.admin.entity.CategoryEntity">
		select *
		from mall_category
		where store_id = #{storeId}
	</select>

	<select id="queryList" resultType="com.kmall.admin.entity.CategoryEntity">
		select
    		b.id,
    		b.name,
			b.store_id,
			b.merch_sn,
    		`keywords`,
    		`front_desc`,
    		`parent_id`,
    		b.sort_order,
    		`show_index`,
    		`banner_url`,
    		`icon_url`,
    		`img_url`,
    		`wap_banner_url`,
    		`level`,
    		`type`,
    		`front_name`,
			b.is_show as `show`,
		s.store_name storeName,
		m.merch_name merchName
		from mall_category b
		left join mall_store s on b.store_id = s.id
		left join mall_merch m on b.merch_sn = m.merch_sn
		WHERE 1=1
		<if test="storeId != null and storeId != ''">
			AND b.store_id = #{storeId}
		</if>
		<if test="merchSn != null and merchSn.trim() != ''">
			AND b.merch_sn = #{merchSn}
		</if>
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode.trim() != ''">
			AND s.third_party_merch_code = #{thirdPartyMerchCode}
		</if>
		<if test="categoryName != null and categoryName.trim() != ''">
			AND b.name LIKE concat('%',#{categoryName},'%')
		</if>
		<if test="parentId != null and parentId != ''">
			AND `parent_id` = #{parentId}
		</if>
		<if test="isShow != null and isShow != ''">
			AND b.is_show = #{isShow}
		</if>
		<if test="isL2 != null and isL2 != ''">
			AND `parent_id` != #{isL2}
		</if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by b.sort_order
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>

 	<select id="queryTotal" resultType="int">
		select count(*) from mall_category b
		left join mall_store s on b.store_id = s.id
		WHERE 1=1
		<if test="storeId != null and storeId != ''">
			AND b.store_id = #{storeId}
		</if>
		<if test="merchSn != null and merchSn.trim() != ''">
			AND b.merch_sn = #{merchSn}
		</if>
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode.trim() != ''">
			AND s.third_party_merch_code = #{thirdPartyMerchCode}
		</if>
		<if test="categoryName != null and categoryName.trim() != ''">
			AND b.name LIKE concat('%',#{categoryName},'%')
		</if>
		<if test="parentId != null and parentId != ''">
			AND b.`parent_id` = #{parentId}
		</if>
		<if test="isShow != null and isShow != ''">
			AND b.`is_show` = #{isShow}
		</if>
		<if test="isL2 != null and isL2 != ''">
			AND b.`parent_id` != #{isL2}
		</if>
	</select>

	<insert id="save" parameterType="com.kmall.admin.entity.CategoryEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mall_category(
			`id`,
			`name`,
			`store_id`,
			`merch_sn`,
			`keywords`,
			`front_desc`,
			`parent_id`,
			`sort_order`,
			`show_index`,
			`is_show`,
			`banner_url`,
			`icon_url`,
			`img_url`,
			`wap_banner_url`,
			`level`,
			`type`,
			`front_name`)
		values(
			#{id},
			#{name},
			#{storeId},
			#{merchSn},
			#{keywords},
			#{frontDesc},
			#{parentId},
			#{sortOrder},
			#{showIndex},
			#{isShow},
			#{bannerUrl},
			#{iconUrl},
			#{imgUrl},
			#{wapBannerUrl},
			#{level},
			#{type},
			#{frontName})
	</insert>

	<update id="update" parameterType="com.kmall.admin.entity.CategoryEntity">
		update mall_category
		<set>
			<if test="name != null">`name` = #{name}, </if>
			<if test="storeId != null">`store_id` = #{storeId}, </if>
			<if test="merchSn != null">`merch_sn` = #{merchSn}, </if>
			<if test="keywords != null">`keywords` = #{keywords}, </if>
			<if test="frontDesc != null">`front_desc` = #{frontDesc}, </if>
			<if test="parentId != null">`parent_id` = #{parentId}, </if>
			<if test="sortOrder != null">`sort_order` = #{sortOrder}, </if>
			<if test="showIndex != null">`show_index` = #{showIndex}, </if>
			<if test="isShow != null">`is_show` = #{isShow}, </if>
			<if test="bannerUrl != null">`banner_url` = #{bannerUrl}, </if>
			<if test="iconUrl != null">`icon_url` = #{iconUrl}, </if>
			<if test="imgUrl != null">`img_url` = #{imgUrl}, </if>
			<if test="wapBannerUrl != null">`wap_banner_url` = #{wapBannerUrl}, </if>
			<if test="level != null">`level` = #{level}, </if>
			<if test="type != null">`type` = #{type}, </if>
			<if test="frontName != null">`front_name` = #{frontName}</if>
		</set>
		where id = #{id}
	</update>

	<delete id="delete">
		delete from mall_category where id = #{value}
	</delete>

	<delete id="deleteBatch">
		delete from mall_category where id in
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

</mapper>
