<?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.WxMerchantTradeDailyMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxMerchantTradeDaily">
		<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="b_user_id" jdbcType="BIGINT" property="bUserId" />
		<result column="trade_amt" jdbcType="DECIMAL" property="tradeAmt" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
		<result column="report_date" jdbcType="VARCHAR" property="reportDate" />
	</resultMap>
	
	<sql id="allColumns">
	    `id`,`tenant_id`,`merchant_id`,`b_user_id`,`trade_amt`,`create_date`,`update_date`,`report_date`
	</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 != bUserId ">
			and `b_user_id` = #{bUserId}

		</if>

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

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

		</if>
		<if test=" null != reportDate ">
			and `report_date` = #{reportDate}

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

	<select id="findListOfWeek" parameterType="com.iformall.domain.po.WxMerchantTradeDaily" resultMap="BaseResultMap">
		select <include refid="allColumns" /> from wx_merchant_trade_daily

		where 1=1
		<if test="reportDate!=null">
			and `report_date` != #{reportDate}
		</if>

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

		<if test=" null != merchantId ">
			and `merchant_id` = #{merchantId}
		</if>
		order by create_date desc LIMIT 7
	</select>



	<resultMap id="BaseResultMapVo" type="com.iformall.domain.vo.WxMerchantTradeDailyVo">
		<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="b_user_id" jdbcType="BIGINT" property="bUserId" />
		<result column="trade_amt" jdbcType="DECIMAL" property="tradeAmt" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
		<result column="report_date" jdbcType="VARCHAR" property="reportDate" />
		<result column="merchant_name" jdbcType="VARCHAR" property="merchantName" />
		<result column="b_user_name" jdbcType="VARCHAR" property="bUserName" />
		<result column="b_user_phone" jdbcType="VARCHAR" property="bUserPhone"/>
	</resultMap>

	<sql id="allColumnsVo">
	    mtd.id as id, m.tenant_id as tenant_id, m.id as merchant_id, mtd.b_user_id as b_user_id, mtd.trade_amt as trade_amt, mtd.create_date as create_date,mtd.update_date as update_date,mtd.report_date as report_date, m.name as merchant_name, mbu.name as b_user_name, mbu.phone as b_user_phone
	</sql>

	<select id="findListVo" parameterType="com.iformall.domain.vo.WxMerchantTradeDailyVo" resultMap="BaseResultMapVo">
		select <include refid="allColumnsVo" />
		from wx_merchant m
		left join wx_merchant_trade_daily mtd on mtd.merchant_id = m.id
		<if test=" reportDate!=null">
			and mtd.report_date = #{reportDate}
		</if>
		left join wx_merchant_b_user mbu on mtd.b_user_id = mbu.id

		where 1=1
			and m.status = 1
		<if test=" null != merchantName and ''!=merchantName">
			and m.name  like concat('%', #{merchantName},'%')
		</if>

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

		<if test=" null != merchantId ">
			and m.id = #{merchantId}
		</if>

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

	<select id="findListVoOfMonth" parameterType="com.iformall.domain.po.WxMerchantTradeDaily" resultMap="BaseResultMapVo">
		select <include refid="allColumnsVo" />
		from wx_merchant_trade_daily mtd
		inner join wx_merchant m on mtd.merchant_id = m.id
		left join wx_merchant_b_user mbu on mtd.b_user_id = mbu.id
		where 1 = 1

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

		<if test=" null != merchantId ">
			and mtd.merchant_id = #{merchantId}
		</if>
		order by mtd.create_date desc LIMIT 30
	</select>


	<resultMap id="BaseTotalResultMap" type="com.iformall.domain.po.WxMerchantTradeDaily">
		<result column="trade_amt" jdbcType="DECIMAL" property="tradeAmt" />
		<result column="report_date" jdbcType="VARCHAR" property="reportDate" />
	</resultMap>

	<select id="findTotalListOfMonth" parameterType="com.iformall.domain.po.WxMerchantTradeDaily" resultMap="BaseTotalResultMap">
		select report_date, IFNULL(sum(mtd.trade_amt),0) as trade_amt
		from wx_merchant_trade_daily mtd
		where 1 = 1

		<if test=" null != tenantId ">
			and mtd.tenant_id = #{tenantId}
		</if>
		<if test=" null != reportDate ">
			and mtd.report_date = #{reportDate}
		</if>
		group by mtd.report_date
		order by mtd.report_date desc
		LIMIT 30
	</select>
	
</mapper>
