<?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.SupplierDao">

    <resultMap type="com.kmall.admin.entity.SupplierEntity" id="supplierMap">
        <result property="id" column="id"/>
        <result property="levelMerchSn" column="level_merch_sn"/>
        <result property="levelMerchFlag" column="level_merch_flag"/>
        <result property="childSupplierName" column="child_supplier_name"/>
        <result property="childSupplierFlag" column="child_supplier_flag"/>
        <result property="sortOrder" column="sort_order"/>
        <result property="isShow" column="is_show"/>
		<result property="thirdPartyMerchCode" column="third_party_merch_code"/>
    </resultMap>

	<select id="queryObject" resultType="com.kmall.admin.entity.SupplierEntity">
		select
			`id`,
			`level_merch_sn`,
			`level_merch_flag`,
			`child_supplier_name`,
			`child_supplier_flag`,
			`sort_order`,
			`is_show`,
			third_party_merch_code
		from mall_supplier
		where id = #{id}
	</select>

	<select id="queryObjectByName" resultType="com.kmall.admin.entity.SupplierEntity">
		select
		`id`,
		`level_merch_sn`,
		`level_merch_flag`,
		`child_supplier_name`,
		`child_supplier_flag`,
		`sort_order`,
		`is_show`,
		third_party_merch_code
		from mall_supplier
		where child_supplier_name = #{supplierName}
		<if test="merchSn != null and merchSn.trim() != ''">
			AND level_merch_sn = #{merchSn}
		</if>
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode.trim() != ''">
			AND third_party_merch_code = #{thirdPartyMerchCode}
		</if>
	</select>

	<select id="queryList" resultType="com.kmall.admin.entity.SupplierEntity">
		select
		s.`id`,
		s.`level_merch_sn`,
		s.`level_merch_flag`,
		s.`child_supplier_name`,
		s.`child_supplier_flag`,
		s.`sort_order`,
		s.`is_show`,s.third_party_merch_code
		from mall_supplier s left join third_merchant_biz t on s.third_party_merch_code = t.third_party_merch_code
		WHERE 1=1
		<if test="name != null and name.trim() != ''">
			AND s.child_supplier_name like concat('%',#{name},'%')
		</if>
		<if test="merchSn != null and merchSn.trim() != ''">
			AND s.level_merch_sn = #{merchSn}
		</if>
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode.trim() != ''">
			AND s.third_party_merch_code = #{thirdPartyMerchCode}
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn.trim() != ''">
			AND t.third_merch_sn = #{thirdMerchSn}
		</if>
		<if test="supplierId != null and supplierId != ''">
			AND s.id = #{supplierId}
		</if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by s.id desc
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>
	
 	<select id="queryTotal" resultType="int">
		select count(*) from mall_supplier
		WHERE 1=1
		<if test="name != null and name.trim() != ''">
			AND child_supplier_name like concat('%',#{name},'%')
		</if>
		<if test="merchSn != null and merchSn.trim() != ''">
			AND level_merch_sn = #{merchSn}
		</if>
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode.trim() != ''">
			AND third_party_merch_code = #{thirdPartyMerchCode}
		</if>
	</select>
	 
	<insert id="save" parameterType="com.kmall.admin.entity.SupplierEntity">
		insert into mall_supplier(
			`level_merch_sn`,
			`level_merch_flag`,
			`child_supplier_name`,
			`child_supplier_flag`,
			`sort_order`,
			`is_show`,third_party_merch_code)
		values(
			#{levelMerchSn},
			#{levelMerchFlag},
			#{childSupplierName},
			#{childSupplierFlag},
			#{sortOrder},
			#{isShow},#{thirdPartyMerchCode})
	</insert>
	 
	<update id="update" parameterType="com.kmall.admin.entity.SupplierEntity">
		update mall_supplier 
		<set>
			<if test="levelMerchSn != null">`level_merch_sn` = #{levelMerchSn}, </if>
			<if test="levelMerchFlag != null">`level_merch_flag` = #{levelMerchFlag}, </if>
			<if test="childSupplierName != null">`child_supplier_name` = #{childSupplierName}, </if>
			<if test="childSupplierFlag != null">`child_supplier_flag` = #{childSupplierFlag}, </if>
			<if test="sortOrder != null">`sort_order` = #{sortOrder}, </if>
			<if test="isShow != null">`is_show` = #{isShow},</if>
			<if test="thirdPartyMerchCode != null">`third_party_merch_code` = #{thirdPartyMerchCode}</if>
		</set>
		where id = #{id}
	</update>
	
	<delete id="delete">
		delete from mall_supplier where id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mall_supplier where id in 
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

</mapper>