<?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.WxCouponMerchantMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxCouponMerchant">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="product_id" jdbcType="BIGINT" property="productId" />
		<result column="merchant_id" jdbcType="BIGINT" property="merchantId" />
		<result column="parameter" jdbcType="VARCHAR" property="parameter" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
		<result column="status" jdbcType="INTEGER" property="status" />
	</resultMap>
	
	<sql id="allColumns">
	    `id`,`product_id`,`merchant_id`,`parameter`,`create_date`,`update_date`,`status`
	</sql>

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

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

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

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

		</if>

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

		</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.WxCouponMerchant" resultMap="BaseResultMap">
		select <include refid="allColumns" /> from wx_coupon_merchant
		<include refid="dynamicWhereConditions" />
	</select>

	<sql id="allMerchantVoColumns">
        m.id,
		m.img_url,m.name,m.link_phone,m.status,m.business_id,
		s.addr,s.shop_number,s.baidu_poi,
		mb.building_name,mf.floor_name,
		cm.parameter
	</sql>

	<resultMap id="MerchantVoResultMap" type="com.iformall.domain.vo.WxMerchantVo">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="img_url" jdbcType="VARCHAR" property="merchantImgUrl"/>
		<result column="name" jdbcType="VARCHAR" property="merchantName"/>
		<result column="link_phone" jdbcType="VARCHAR" property="merchantLinkPhone"/>
		<result column="status" jdbcType="VARCHAR" property="merchantStatus"/>
		<result column="business_id" jdbcType="INTEGER" property="businessId"/>
		<result column="parameter" jdbcType="VARCHAR" property="parameter"/>

		<result column="addr" jdbcType="VARCHAR" property="addr"/>
		<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="baidu_poi" jdbcType="VARCHAR" property="baiduPoi"/>
	</resultMap>

	<select id="findMerchantVoList" parameterType="java.util.Map" resultMap="MerchantVoResultMap">
		select
		<include refid="allMerchantVoColumns"/>
		from wx_coupon_merchant cm
		left join wx_merchant m on m.id = cm.merchant_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
		where cm.status = 0
		and cm.product_id = #{productId}
	</select>

	<sql id="allMerchantNameColumns">
        m.id,m.name,m.status
	</sql>

	<sql id="allMerchantBaseColumns">
        m.id,
		m.img_url,m.name,m.link_phone,m.status,m.business_id,
		cm.parameter
	</sql>

	<select id="findMerchantNameList" parameterType="java.util.Map" resultMap="MerchantVoResultMap">
		select
		<include refid="allMerchantNameColumns"/>
		from wx_coupon_merchant cm
		left join wx_merchant m on m.id = cm.merchant_id
		where cm.status = 0
		and cm.product_id = #{productId}
	</select>

	<select id="findMerchantBaseList" parameterType="java.util.Map" resultMap="MerchantVoResultMap">
		select
		<include refid="allMerchantBaseColumns"/>
		from wx_coupon_merchant cm
		left join wx_merchant m on m.id = cm.merchant_id
		where cm.status = 0
		and cm.product_id = #{productId}
	</select>


</mapper>
