<?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.WxMerchantSubsidyMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxMerchantSubsidy">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="order_id" jdbcType="BIGINT" property="orderId" />
		<result column="order_type" jdbcType="INTEGER" property="orderType" />
		<result column="merchant_id" jdbcType="BIGINT" property="merchantId" />
		<result column="coupon_order_id" jdbcType="BIGINT" property="couponOrderId" />
		<result column="coupon_type" jdbcType="INTEGER" property="couponType" />
		<result column="order_payment" jdbcType="INTEGER" property="orderPayment" />
		<result column="receiver_payment" jdbcType="INTEGER" property="receiverPayment" />
		<result column="real_payment" jdbcType="INTEGER" property="realPayment" />
		<result column="subsidy" jdbcType="INTEGER" property="subsidy" />
		<result column="real_subsidy" jdbcType="INTEGER" property="realSubsidy" />
		<result column="status" jdbcType="INTEGER" property="status" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
	</resultMap>
	
	<sql id="allColumns">
	    `id`,`order_id`,`order_type`,`merchant_id`,`coupon_order_id`,`coupon_type`,`order_payment`,`receiver_payment`,`real_payment`,`subsidy`,`real_subsidy`,`status`,`create_date`,`update_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 != orderId ">
		    and `order_id` = #{orderId}
		</if>    
		<if test=" null != orderType ">
		    and `order_type` = #{orderType}
		</if>    
		<if test=" null != merchantId ">
		    and `merchant_id` = #{merchantId}
		</if>    
		<if test=" null != couponOrderId ">
		    and `coupon_order_id` = #{couponOrderId}
		</if>    
		<if test=" null != couponType ">
		    and `coupon_type` = #{couponType}
		</if>    
		<if test=" null != orderPayment ">
		    and `order_payment` = #{orderPayment}
		</if>    
		<if test=" null != receiverPayment ">
		    and `receiver_payment` = #{receiverPayment}
		</if>    
		<if test=" null != realPayment ">
		    and `real_payment` = #{realPayment}
		</if>    
		<if test=" null != subsidy ">
		    and `subsidy` = #{subsidy}
		</if>    
		<if test=" null != realSubsidy ">
		    and `real_subsidy` = #{realSubsidy}
		</if>    
		<if test=" null != status ">
		    and `status` = #{status}
		</if>    
		<if test=" null != createDate ">
		    and `create_date` = #{createDate}
		</if>    
		<if test=" null != updateDate ">
		    and `update_date` = #{updateDate}
		</if>
		<if test=" null != startdate and  null!=enddate">
			and `create_date` between #{startdate} and #{enddate}
		</if>
		<if test=" null != statusS ">
			and `status` in
			<foreach collection="statusS" index="index" item="sItem" open="(" separator="," close=")">
				#{sItem}
			</foreach>
		</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.WxMerchantSubsidy" resultMap="BaseResultMap">
		select <include refid="allColumns" /> from wx_merchant_subsidy
		<include refid="dynamicWhereConditions" />
	</select>
	
	<update id="updateByCond" parameterType="com.iformall.domain.po.WxMerchantSubsidy">
		update wx_merchant_subsidy
		set `status` = #{status}
		where `tenant_id`=#{tenantId} and merchant_id=#{merchantId}
		and id in (${idStr})
	</update>


	<resultMap id="VoBaseResultMap" type="com.iformall.domain.vo.WxMerchantSubsidyVo">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="order_id" jdbcType="BIGINT" property="orderId" />
		<result column="order_type" jdbcType="INTEGER" property="orderType" />
		<result column="merchant_id" jdbcType="BIGINT" property="merchantId" />
		<result column="coupon_order_id" jdbcType="BIGINT" property="couponOrderId" />
		<result column="coupon_type" jdbcType="INTEGER" property="couponType" />
		<result column="order_payment" jdbcType="INTEGER" property="orderPayment" />
		<result column="receiver_payment" jdbcType="INTEGER" property="receiverPayment" />
		<result column="real_payment" jdbcType="INTEGER" property="realPayment" />
		<result column="subsidy" jdbcType="INTEGER" property="subsidy" />
		<result column="real_subsidy" jdbcType="INTEGER" property="realSubsidy" />
		<result column="status" jdbcType="INTEGER" property="status" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />

		<result column="cover_img" jdbcType="VARCHAR" property="coverImg" />
		<result column="title" jdbcType="VARCHAR" property="title" />
		<result column="sub_title" jdbcType="VARCHAR" property="subTitle" />
		<result column="detail" jdbcType="VARCHAR" property="detail" />
		<result column="unit" jdbcType="INTEGER" property="unit" />
		<result column="remark" jdbcType="VARCHAR" property="remark" />
		<result column="name" jdbcType="VARCHAR" property="merchantName" />

		<result column="nick_name" jdbcType="VARCHAR" property="nickName" />
		<result column="phone" jdbcType="VARCHAR" property="phone" />
	</resultMap>

	<sql id="allVoColumns">
	    ms.`id`,ms.`tenant_id`,ms.`order_id`,ms.`order_type`,ms.`merchant_id`,ms.`coupon_order_id`,ms.`coupon_type`,
	    ms.`order_payment`,ms.`receiver_payment`,ms.`real_payment`,
	    ms.`subsidy`,ms.`real_subsidy`,ms.`status`,ms.`create_date`,ms.`update_date`,
	    c.`title`,c.`sub_title`,c.`unit`,m.`name`,
	    cu.`nick_name`, cu.`phone`
	</sql>

	<sql id="dynamicVoWhereConditions">
		<if test=" null != id ">
			and t.`id` = #{id}
		</if>
		<if test=" null != tenantId ">
			and t.`tenant_id` = #{tenantId}
		</if>
		<if test=" null != orderId ">
			and t.`order_id` = #{orderId}
		</if>
		<if test=" null != orderType ">
			and t.`order_type` = #{orderType}
		</if>
		<if test=" null != merchantId ">
			and t.`merchant_id` = #{merchantId}
		</if>
		<if test=" null != couponOrderId ">
			and t.`coupon_order_id` = #{couponOrderId}
		</if>
		<if test=" null != couponType ">
			and t.`coupon_type` = #{couponType}
		</if>
		<if test=" null != orderPayment ">
			and t.`order_payment` = #{orderPayment}
		</if>
		<if test=" null != receiverPayment ">
			and t.`receiver_payment` = #{receiverPayment}
		</if>
		<if test=" null != realPayment ">
			and t.`real_payment` = #{realPayment}
		</if>
		<if test=" null != subsidy ">
			and t.`subsidy` = #{subsidy}
		</if>
		<if test=" null != realSubsidy ">
			and t.`real_subsidy` = #{realSubsidy}
		</if>
		<if test=" null != status ">
			and t.`status` = #{status}
		</if>
		<if test=" null != createDate ">
			and t.`create_date` = #{createDate}
		</if>
		<if test=" null != updateDate ">
			and t.`update_date` = #{updateDate}
		</if>
		<if test=" null != startdate and  null!=enddate">
			and t.`create_date` between #{startdate} and #{enddate}
		</if>
		<if test=" null != statusS ">
			and t.`status` in
			<foreach collection="statusS" index="index" item="sItem" open="(" separator="," close=")">
				#{sItem}
			</foreach>
		</if>
		<if test=" null != ids ">
			and t.`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="findVoList" parameterType="com.iformall.domain.po.WxMerchantSubsidy" resultMap="VoBaseResultMap">
		select * from
		(
			select <include refid="allVoColumns" />
			from wx_merchant_subsidy ms
			left join wx_coupon_order co on co.id = ms.coupon_order_id
			left join wx_coupon c on c.id = co.coupon_id
			left join wx_merchant m on m.id = ms.merchant_id
			left join wx_card_spend cs on cs.order_id = ms.order_id
			left join wx_c_user cu on cu.id = cs.owner_id
			where ms.order_type = 3
			union
			select <include refid="allVoColumns" />
			from wx_merchant_subsidy ms
			left join wx_coupon_order co on co.id = ms.coupon_order_id
			left join wx_coupon c on c.id = co.coupon_id
			left join wx_merchant m on m.id = ms.merchant_id
			left join wx_order o on o.id = ms.order_id
			left join wx_c_user cu on cu.id = o.c_user_id
			where ms.order_type = 0
		) t
		where 1=1
		<include refid="dynamicVoWhereConditions" />
	</select>

	<select id="sumVoList" parameterType="com.iformall.domain.po.WxMerchantSubsidy" resultType="hashmap">
		select ms.`merchant_id` merchantId, m.`name` as merchantName,
		       IFNULL(sum(ms.`order_payment`),0) as sumOrderPayment,
		       IFNULL(sum(ms.`receiver_payment`),0) as sumReceiverPayment,
			   IFNULL(sum(ms.`subsidy`),0) as sumSubsidy,
		       IFNULL(sum(ms.`real_subsidy`),0) as sumRealSubsidy
		from wx_merchant_subsidy ms
		left join wx_coupon_order co on co.id = ms.coupon_order_id
		left join wx_coupon c on c.id = co.coupon_id
		left join wx_merchant m on m.id = ms.merchant_id
		<where>
			<if test=" null != status ">
				and ms.`status` = #{status}
			</if>
			<if test=" null != merchantId ">
				and ms.`merchant_id` = #{merchantId}
			</if>
			<if test=" null != statusS ">
				and ms.`status` in
				<foreach collection="statusS" index="index" item="sItem" open="(" separator="," close=")">
					#{sItem}
				</foreach>
			</if>
			<if test=" null != startdate and  null!=enddate">
				and ms.`create_date` between #{startdate} and #{enddate}
			</if>
		</where>
		group by ms.`merchant_id`
	</select>
	
	
	
	
	
	
</mapper>
