<?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.mk.MkCenterMenuDao">

    <resultMap type="com.kmall.admin.entity.mk.MkCenterMenuEntity" id="mkCenterMenuMap">
        <result property="id" column="id"/>
        <result property="menuName" column="menu_name"/>
        <result property="menuDetail" column="menu_detail"/>
        <result property="picUrl" column="pic_url"/>
        <result property="menuUrl" column="menu_url"/>
        <result property="isValid" column="is_valid"/>
        <result property="note" column="note"/>
        <result property="createrSn" column="creater_sn"/>
        <result property="createTime" column="create_time"/>
        <result property="moderSn" column="moder_sn"/>
        <result property="modTime" column="mod_time"/>
        <result property="tstm" column="tstm"/>
		<result property="storeName" column="storeName"/>
    </resultMap>

	<select id="queryObject" resultType="com.kmall.admin.entity.mk.MkCenterMenuEntity">
		select
			`id`,
			`menu_name`,
			`menu_detail`,
			`pic_url`,
			`menu_url`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`
		from mk_center_menu
		where id = #{id}
	</select>
	<select id="queryList" resultType="com.kmall.admin.entity.mk.MkCenterMenuEntity">
		SELECT
		a.`id`,
		a.`menu_name`,
		a.`menu_detail`,
		a.`pic_url`,
		a.`menu_url`,
		a.`is_valid`,
		a.`note`,
		a.`creater_sn`,
		a.`create_time`,
		a.`moder_sn`,
		a.`mod_time`,
		a.`tstm`
		FROM
		mk_center_menu a
	</select>
	<select id="queryCenterByList" resultType="com.kmall.admin.entity.mk.MkCenterMenuEntity">
		SELECT
			a.`id`,
			a.`menu_name`,
			a.`menu_detail`,
			a.`pic_url`,
			a.`menu_url`,
			a.`is_valid`
		FROM
			mk_center_menu a
		WHERE
			1 = 1
		<if test="(thirdPartyMerchCode != null and thirdPartyMerchCode != '') or (storeId != null and storeId != '') or (thirdMerchSn != null and thirdMerchSn != '') ">
			AND id IN (
			SELECT
			r.center_menu_id
			FROM
			mk_center_menu_store_real r
			LEFT JOIN mall_store s ON r.store_id = s.id
			LEFT JOIN third_merchant_biz t ON r.third_merch_sn = t.third_merch_sn
			WHERE 1=1
			<if test="thirdPartyMerchCode != null and thirdPartyMerchCode != ''">
				AND t.third_party_merch_code = #{thirdPartyMerchCode}
			</if>
			<if test="thirdMerchSn != null and thirdMerchSn != ''">
				AND r.third_merch_sn = #{thirdMerchSn}
			</if>
			)
		</if>
		ORDER BY
			a.id DESC
	</select>

	<select id="queryListByStore" resultType="com.kmall.admin.entity.mk.MkCenterMenuEntity">
		SELECT
		a.`id`,
		a.`menu_name`,
		a.`menu_detail`,
		a.`pic_url`,
		a.`menu_url`,
		r.`store_id`,
		r.`third_merch_sn`,
		a.`is_valid`,
		a.`note`,
		a.`creater_sn`,
		a.`create_time`,
		a.`moder_sn`,
		a.`mod_time`,
		a.`tstm`,
		t.third_party_merch_name thirdPartyMerchName,
		s.store_name storeName,
		r.id menuStoreRealId
		FROM
		mk_center_menu_store_real r
		LEFT JOIN mk_center_menu a ON a.id = r.center_menu_id
		LEFT JOIN mall_store s ON r.store_id = s.id
		LEFT JOIN third_merchant_biz t ON r.third_merch_sn = t.third_merch_sn
		WHERE 1=1
		<if test="menuName != null and menuName.trim() != ''">
			AND menu_name LIKE concat('%',#{menuName},'%')
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn != ''">
			AND r.third_merch_sn = #{thirdMerchSn}
		</if>
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode != ''">
			AND t.third_party_merch_code = #{thirdPartyMerchCode}
		</if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by r.id desc
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>
	
 	<select id="queryTotal" resultType="int">
		select count(*)
		from mk_center_menu_store_real r
		LEFT JOIN mk_center_menu a ON a.id = r.center_menu_id
		LEFT JOIN mall_store s ON r.store_id = s.id
		LEFT JOIN third_merchant_biz t ON r.third_merch_sn = t.third_merch_sn
		WHERE 1=1
		<if test="menuName != null and menuName.trim() != ''">
			AND menu_name LIKE concat('%',#{menuName},'%')
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn != ''">
			AND r.third_merch_sn = #{thirdMerchSn}
		</if>
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode != ''">
			AND t.third_party_merch_code = #{thirdPartyMerchCode}
		</if>
	</select>
	 
	<insert id="save" parameterType="com.kmall.admin.entity.mk.MkCenterMenuEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mk_center_menu(
			`menu_name`,
			`menu_detail`,
			`pic_url`,
			`menu_url`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{menuName},
			#{menuDetail},
			#{picUrl},
			#{menuUrl},
			#{isValid},
			#{note},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>
	 
	<update id="update" parameterType="com.kmall.admin.entity.mk.MkCenterMenuEntity">
		update mk_center_menu 
		<set>
			<if test="menuName != null">`menu_name` = #{menuName}, </if>
			<if test="menuDetail != null">`menu_detail` = #{menuDetail}, </if>
			<if test="picUrl != null">`pic_url` = #{picUrl}, </if>
			<if test="menuUrl != null">`menu_url` = #{menuUrl}, </if>
			<if test="isValid != null">`is_valid` = #{isValid}, </if>
			<if test="note != null">`note` = #{note}, </if>
			<if test="createrSn != null">`creater_sn` = #{createrSn}, </if>
			<if test="createTime != null">`create_time` = #{createTime}, </if>
			<if test="moderSn != null">`moder_sn` = #{moderSn}, </if>
			<if test="modTime != null">`mod_time` = #{modTime}, </if>
			<if test="tstm != null">`tstm` = #{tstm}</if>
		</set>
		where id = #{id}
	</update>
	
	<delete id="delete">
		delete from mk_center_menu where id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mk_center_menu where id in 
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

</mapper>