<?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.WxProfitSharingOrderMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxProfitSharingOrder">
		<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="order_id" jdbcType="BIGINT" property="orderId" />
		<result column="pay_amount" jdbcType="INTEGER" property="payAmount" />
		<result column="pay_time_end" jdbcType="TIMESTAMP" property="payTimeEnd" />
		<result column="pay_time_start" jdbcType="TIMESTAMP" property="payTimeStart" />
		<result column="transaction_id" jdbcType="VARCHAR" property="transactionId" />
		<result column="sharing_order_no" jdbcType="VARCHAR" property="sharingOrderNo" />
		<result column="type" jdbcType="INTEGER" property="type" />
		<result column="receivers" jdbcType="VARCHAR" property="receivers" />
		<result column="error_msg" jdbcType="VARCHAR" property="errorMsg" />
		<result column="sharing_status" jdbcType="INTEGER" property="sharingStatus" />
		<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
		<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
	</resultMap>
	
	<sql id="allColumns">
	    `id`,`tenant_id`,`merchant_id`,`order_id`,`pay_amount`,`pay_time_end`,`pay_time_start`,`transaction_id`,`sharing_order_no`,`type`,`receivers`,`error_msg`,`sharing_status`,`update_time`,`create_time`
	</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 != orderId "> 
		and `order_id` = #{orderId} 
       	
		</if>

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

		</if>

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

		</if>    
	    
		<if test=" null != payTimeEnd "> 
		and `pay_time_end` = #{payTimeEnd} 
       	
		</if>    
	    
		<if test=" null != payTimeStart "> 
		and `pay_time_start` = #{payTimeStart} 
       	
		</if>    
	    
		<if test=" null != transactionId "> 
		and `transaction_id`  like concat('%', #{transactionId},'%')
       	
		</if>    
	    
		<if test=" null != sharingOrderNo ">
		and `sharing_order_id`  like concat('%', #{sharingOrderId},'%')
       	
		</if>    
	    
		<if test=" null != errorMsg "> 
		and `error_msg`  like concat('%', #{errorMsg},'%')
       	
		</if>    
	    
		<if test=" null != sharingStatus "> 
		and `sharing_status`  like concat('%', #{sharingStatus},'%')
       	
		</if>    
	    
		<if test=" null != updateTime "> 
		and `update_time` = #{updateTime} 
       	
		</if>    
	    
		<if test=" null != createTime "> 
		and `create_time` = #{createTime} 
       	
		</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.WxProfitSharingOrder" resultMap="BaseResultMap">
		select <include refid="allColumns" /> from wx_profit_sharing_order
		<include refid="dynamicWhereConditions" />
	</select>
	
	
	
	
	<resultMap id="orderMap" type="com.iformall.domain.vo.WxProfitSharingOrderVo">
		<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="merchant_name" jdbcType="VARCHAR" property="merchantName" />
		<result column="order_id" jdbcType="BIGINT" property="orderId" />
		<result column="pay_amount" jdbcType="INTEGER" property="payAmount" />
		<result column="pay_time_end" jdbcType="TIMESTAMP" property="payTimeEnd" />
		<result column="pay_time_start" jdbcType="TIMESTAMP" property="payTimeStart" />
		<result column="transaction_id" jdbcType="VARCHAR" property="transactionId" />
		<result column="sharing_order_no" jdbcType="VARCHAR" property="sharingOrderNo" />
		<result column="type" jdbcType="INTEGER" property="type" />
		<result column="receivers" jdbcType="VARCHAR" property="receivers" />
		<result column="error_msg" jdbcType="VARCHAR" property="errorMsg" />
		<result column="sharing_status" jdbcType="INTEGER" property="sharingStatus" />
		<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
		<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
	</resultMap>
	
	<select id="findOrderList" parameterType="com.iformall.domain.vo.WxProfitSharingOrderQueryVo" resultMap="orderMap">
		select pso.*,m.`name` merchant_name from wx_profit_sharing_order pso
		left join wx_merchant m on pso.merchant_id=m.id
		<where>
			<if test="tenantId!=null">
				and pso.tenant_id =#{tenantId}
			</if>
			<if test="id!=null">
				and pso.id =#{id}
			</if>
			<if test="merchantName!=null and ''!=merchantName">
				and m.`name` like concat('%',#{merchantName},'%')
			</if>
			<if test="startdate!=null and enddate!=null">
				and pso.create_time between #{startdate} and #{enddate}
			</if>
			<if test="sharingStatus!=null">
				and pso.sharing_status = #{sharingStatus}
			</if>
			<if test="type!=null">
				and pso.type = #{type}
			</if>

		</where>

		<if test=" null != sortColumns"> order by ${sortColumns} </if>
		<if test=" null == sortColumns">order by pso.id desc</if>
	</select>
	
	<select id="queryOrderForMerchantCount" resultType="Long" parameterType="hashmap">
		select count(*) merchantCount from (
		select distinct merchant_id from wx_profit_sharing_order
		<where>
			<if test="tenantId!=null">
				and tenant_id =#{tenantId}
			</if>
			<if test="startdate!=null and enddate!=null">
				and create_time between #{startdate} and #{enddate}
			</if>
		</where>
		) o
	</select>
	
	<select id="queryOrderForSum" resultType="Long" parameterType="hashmap">
		select ifnull(sum(pay_amount),0) payment from wx_profit_sharing_order
		<where>
			<if test="tenantId!=null">
				and tenant_id =#{tenantId}
			</if>
			<if test="startdate!=null and enddate!=null">
				and create_time between #{startdate} and #{enddate}
			</if>
		</where>
	</select>
	
	<resultMap id="orderDetailsMap" type="com.iformall.domain.vo.WxProfitSharingOrderDetailsVo">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId" />
		<result column="pay_amount" jdbcType="INTEGER" property="payAmount" />
		<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
		<result column="sharing_status" jdbcType="INTEGER" property="sharingStatus" />
		<result column="finish_time" jdbcType="VARCHAR" property="finishTime" />
		<result column="failed_reason" jdbcType="VARCHAR" property="failedReason" />
		<result column="description" jdbcType="VARCHAR" property="description" />
		<result column="receiver_type" jdbcType="INTEGER" property="receiverType" />
		<result column="receiver_account" jdbcType="VARCHAR" property="receiverAccount" />
		<result column="receiver_comments" jdbcType="VARCHAR" property="receiverComments" />
		<result column="sharing_type" jdbcType="INTEGER" property="sharingType" />
		<result column="status" jdbcType="VARCHAR" property="status" />
		
	</resultMap>
	
	<select id="findOrderDetails" resultMap="orderDetailsMap">
	select psr.id,psr.tenant_id,psr.pay_amount,psr.create_time,sharing_status,finish_time,failed_reason,description,
	psre.receiver_type,psre.receiver_account,psre.receiver_comments,psre.sharing_type,psre.`status`
	from wx_profit_sharing_result psr left join wx_profit_sharing_receiver psre on psr.sharing_receiver_id=psre.id
	where psr.sharing_order_id=#{value} and psr.sharing_receiver_id!=0
	</select>

</mapper>
