<?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.WxBusinessMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxBusiness">
		<id column="id" jdbcType="INTEGER" property="id" />
		<result column="title" jdbcType="VARCHAR" property="title" />
		<result column="type" jdbcType="INTEGER" property="type" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
	</resultMap>
	
	<sql id="allColumns">
	    `id`,`title`,`type`,`create_date`,`update_date`
	</sql>

	<sql id="dynamicWhereConditions">
	        where 1 = 1
	    
		<if test=" null != id "> 
		and `id` = #{id}
		</if>
	    
		<if test=" null != title "> 
		and `title`  like concat('%', #{title},'%')
		</if>    
	    
		<if test=" null != type "> 
		and `type` = #{type}
		</if>    
	    
		<if test=" null != createDate "> 
		and `create_date` = #{createDate}
		</if>    
	    
		<if test=" null != updateDate "> 
		and `update_date` = #{updateDate}
		</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.WxBusiness" resultMap="BaseResultMap">
		select <include refid="allColumns" /> from wx_business
		<include refid="dynamicWhereConditions" />
	</select>

	<select id="findListAll"  resultMap="BaseResultMap">
		select <include refid="allColumns" /> from wx_business
		order by create_date asc
	</select>

	<select id="findListForMerchant"  resultMap="BaseResultMap">
		select <include refid="allColumns" />
		from wx_business b
		where
			(select count(*) from wx_merchant m
				where
				m.tenant_id = #{tenantId}
				and b.id = m.business_id
				and m.status = 1
				and m.is_public = 1) > 0
		order by create_date asc
	</select>

	<select id="findListForCoupon" resultMap="BaseResultMap">
		select <include refid="allColumns" />
		from wx_business b
		where
			(select count(*) from wx_coupon_channel cc
				where
				cc.tenant_id = #{tenantId}
				and b.id = cc.business
				and cc.status = 0
				and cc.target_ad = 1) > 0
		order by create_date asc
	</select>

	<select id="businessDataList" parameterType="com.iformall.domain.po.WxBusiness" resultType="hashmap">
		select b.id,b.title,

		round(
		(select if(sum(mtd.trade_amt) is null,0,sum(mtd.trade_amt))
		from wx_merchant m
		left join wx_merchant_trade_daily mtd on mtd.merchant_id = m.id where m.business_id = b.id and m.tenant_id=#{tenantId} and STR_TO_DATE(mtd.report_date,'%Y-%m-%d') between #{startdate} and #{enddate}  and m.status = 1 and m.is_del = 0 )
		/100,2) volume,
		
		round(
		(select if(sum(mtd.trade_amt) is null,0,sum(mtd.trade_amt))
		from wx_merchant m
		left join wx_merchant_trade_daily mtd on mtd.merchant_id = m.id where m.business_id = b.id and m.tenant_id=#{tenantId} and STR_TO_DATE(mtd.report_date,'%Y-%m-%d') between #{startdate} and #{enddate}  and m.status = 1 and m.is_del = 0 )
		/
		(select if(sum(ws.operation_area) is null,0,sum(ws.operation_area))
		from wx_merchant m
		left join wx_merchant_shop mtd on mtd.merchant_id = m.id
		left join wx_shop ws on(mtd.shop_id = ws.id)
		where m.business_id = b.id and m.tenant_id=#{tenantId}  and m.status = 1 and m.is_del = 0 )
		/100,2) sale,
	 
		(select ifnull(sum(rent_area),0) from wx_rent_contract where tenant_id=#{tenantId} and business_id=b.id and status in (2,3,4)) area,
		
		round(
		(select if(sum(mtd.trade_amt) is null,0,sum(mtd.trade_amt))
		from wx_merchant m
		left join wx_merchant_trade_daily mtd on mtd.merchant_id = m.id where m.business_id = b.id and m.tenant_id=#{tenantId} and STR_TO_DATE(mtd.report_date,'%Y-%m-%d') between #{startdate} and #{enddate}  and m.status = 1 and m.is_del = 0 ) /
		(select sum(mtd.trade_count)
		from wx_merchant m
		left join wx_merchant_trade_daily mtd on mtd.merchant_id = m.id where m.business_id = b.id and m.tenant_id=#{tenantId} and STR_TO_DATE(mtd.report_date,'%Y-%m-%d') between #{startdate} and #{enddate}  and m.status = 1 and m.is_del = 0 )
		/100,2) comPrice
		
		from wx_business b
	</select>

	<select id="devoteList" parameterType="com.iformall.domain.po.WxBusiness" resultType="hashmap">
		select mm.name merchantName,

		round(
		(select if(sum(mtd.trade_amt) is null,0,sum(mtd.trade_amt))
		from wx_merchant m
		left join wx_merchant_trade_daily mtd on mtd.merchant_id = m.id
		where m.business_id = #{id}
		and m.tenant_id=#{tenantId}
		and mtd.report_date between #{startdate} and #{enddate}
		and m.status = 1 and m.is_del = 0 and m.id = mm.id)
		/
		(select if(sum(ws.operation_area) is null,0,sum(ws.operation_area))
		from wx_merchant m
		left join wx_merchant_shop mtd on mtd.merchant_id = m.id
		left join wx_shop ws on(mtd.shop_id = ws.id)
		where m.status = 1 and m.is_del = 0 and m.business_id = #{id}
		and m.tenant_id=#{tenantId}
		and m.id = mm.id)
		/100,2) sale,

		round(

		round(
		(select if(sum(mtd.trade_amt) is null,0,sum(mtd.trade_amt))
		from wx_merchant m
		left join wx_merchant_trade_daily mtd on mtd.merchant_id = m.id
		where m.business_id = #{id}
		and m.tenant_id=#{tenantId}
		and mtd.report_date between #{startdate} and #{enddate}
		and m.status = 1 and m.is_del = 0 and m.id = mm.id)/
		(select if(sum(ws.operation_area) is null,0,sum(ws.operation_area))
		from wx_merchant m
		left join wx_merchant_shop mtd on mtd.merchant_id = m.id
		left join wx_shop ws on(mtd.shop_id = ws.id)
		where m.status = 1 and m.is_del = 0 and m.business_id = #{id}
		and m.tenant_id=#{tenantId}
		and m.id = mm.id)/100,2)
		/
		round(
		(select if(sum(mtd.trade_amt) is null,0,sum(mtd.trade_amt))
		from wx_merchant m
		left join wx_merchant_trade_daily mtd on mtd.merchant_id = m.id
		where m.business_id = #{id}
		and m.tenant_id=#{tenantId}
		and mtd.report_date between #{startdate} and #{enddate}
		and m.status = 1 and m.is_del = 0 )
		/
		(select if(sum(ws.operation_area) is null,0,sum(ws.operation_area))
		from wx_merchant m
		left join wx_merchant_shop mtd on mtd.merchant_id = m.id
		left join wx_shop ws on(mtd.shop_id = ws.id)
		where m.business_id = #{id} and m.tenant_id=#{tenantId} and m.status = 1 and m.is_del = 0 )
		/100,2)

		*100,2) devoteRate

		from wx_merchant mm
		left join wx_merchant_trade_daily mtd on mtd.merchant_id = mm.id
		left join wx_merchant_shop wmt on wmt.merchant_id = mm.id
		left join wx_shop ws on(wmt.shop_id = ws.id)
		where mm.business_id=#{id} and mm.tenant_id=#{tenantId}
		and mm.status = 1 and mm.is_del = 0
		and mtd.report_date between #{startdate} and #{enddate}
		group by mm.id
		order by devoteRate desc

		<if test="null != limitStart and null != limitEnd">
			limit #{limitStart},#{limitEnd}
		</if>
	</select>

</mapper>
