<?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.iformall.mapper.WxLevelMerchantMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxLevelMerchant">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId" />
		<result column="merchant_id" jdbcType="BIGINT" property="merchantId" />
		<result column="level_id" jdbcType="BIGINT" property="levelId" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="discount" jdbcType="INTEGER" property="discount" />
	</resultMap>
	
	<sql id="allColumns">
	    `id`,`tenant_id`,`merchant_id`,`level_id`,`create_date`,`discount`
	</sql>

	<sql id="dynamicWhereConditions">
	        where 1 = 1
	    
		<if test=" null != id "> 
		and `id` = #{id} 
       	
		</if>    
	    
		<if test=" null != tenantId ">
		and `tenant_id` = #{tenantId}
       	
		</if>

		<if test=" null != merchantId ">
		and `merchant_id` = #{merchantId}

		</if>

		<if test=" null != levelId ">
		and `level_id` = #{levelId}

		</if>

		<if test=" null != createDate ">
		and `create_date`  = #{createDate}
       	
		</if>

		<if test=" null != discount ">
		and `discount`  = #{discount}

		</if>

		<if test=" null != ids ">
		 	and id in  
			<foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
			#{idItem}
			</foreach>
		</if>		
		<if test=" null != sortColumns"> order by ${sortColumns} </if>
	</sql>
	
	<select id="findList" parameterType="com.iformall.domain.po.WxLevelMerchant" resultMap="BaseResultMap">
		select <include refid="allColumns" /> from wx_level_merchant
		<include refid="dynamicWhereConditions" />
	</select>


	<resultMap id="CVoResultMap" type="com.iformall.domain.vo.WxLevelMerchantCVo">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="name" jdbcType="VARCHAR" property="merchantName"/>
		<result column="link_phone" jdbcType="VARCHAR" property="merchantLinkPhone"/>

		<result column="shop_number" jdbcType="VARCHAR" property="shopNumber"/>
		<result column="building_name" jdbcType="VARCHAR" property="buildingName"/>
		<result column="floor_name" jdbcType="VARCHAR" property="floorName"/>

		<result column="discount" jdbcType="INTEGER" property="discount" />
	</resultMap>

	<sql id="allCVoColumns">
	    m.id,m.name,m.link_phone,
		s.shop_number,
		mb.building_name,
		mf.floor_name,
		lm.discount
	</sql>

	<sql id="allCVoDynamicWhereConditions">
		where 1 = 1

		<if test=" null != tenantId ">
			and lm.tenant_id = #{tenantId}
		</if>

		<if test=" null != merchantId ">
			and lm.merchant_id = #{merchantId}
		</if>

		<if test=" null != levelId ">
			and lm.level_id = #{levelId}

		</if>

		<if test=" null != discount ">
			and lm.discount  = #{discount}
		</if>

		<if test=" null != sortColumns"> order by ${sortColumns} </if>
	</sql>

	<select id="findListCVo" parameterType="com.iformall.domain.po.WxLevelMerchant" resultMap="CVoResultMap">
		select <include refid="allCVoColumns" />
		from wx_level_merchant lm
		inner join wx_merchant m on m.status = 1 and lm.merchant_id = m.id
		left join wx_merchant_shop ms on ms.merchant_id = m.id and ms.is_del = 0
		left join wx_shop s on ms.shop_id = s.id
		left join wx_mall_building mb on s.building = mb.id
		left join wx_mall_floor mf on s.floor = mf.id
		<include refid="allCVoDynamicWhereConditions" />
	</select>

</mapper>
