<?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.WxCreditHistoryMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxCreditHistory">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId" />
		<result column="c_user_id" jdbcType="BIGINT" property="cUserId" />
		<result column="credit_amount" jdbcType="INTEGER" property="creditAmount" />
		<result column="credit_num" jdbcType="INTEGER" property="creditNum" />
		<result column="credit_type" jdbcType="INTEGER" property="creditType" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="receipt_url" jdbcType="VARCHAR" property="receiptUrl" />
		<result column="operator_type" jdbcType="INTEGER" property="operatorType" />
		<result column="operator_id" jdbcType="BIGINT" property="operatorId" />
		<result column="merchant_id" jdbcType="BIGINT" property="merchantId" />
		<result column="coupon_id" jdbcType="BIGINT" property="couponId" />
		<result column="business_id" jdbcType="BIGINT" property="businessId" />
		<result column="spend" jdbcType="INTEGER" property="spend" />
		<result column="change_purpose" jdbcType="VARCHAR" property="changePurpose" />
	</resultMap>
	
	<sql id="allColumns">
	    `id`,`tenant_id`,`c_user_id`,`credit_amount`,`credit_num`,`credit_type`,`credate_date`,`receipt_url`,`operator_type`,`operator_id`,`merchant_id`,`coupon_id`,`business_id`,`spend`,`change_purpose`
	</sql>

	<sql id="dynamicWhereConditions">
	        where 1 = 1
		<if test=" null != id ">
		    and `id` = #{id}
		</if>    
		<if test=" null != cUserId ">
		    and `c_user_id` = #{cUserId}
		</if>    
		<if test=" null != creditAmount ">
		    and `credit_amount` = #{creditAmount}
		</if>    
		<if test=" null != creditNum ">
		    and `credit_num` = #{creditNum}
		</if>    
		<if test=" null != creditType ">
		    and `credit_type` = #{creditType}
		</if>
		<if test=" null != receiptUrl ">
		    and `receipt_url`  like concat('%', #{receiptUrl},'%')
		</if>    
		<if test=" null != operatorType ">
		    and `operator_type` = #{operatorType}
		</if>    
		<if test=" null != operatorId ">
		    and `operator_id` = #{operatorId}
		</if>    
		<if test=" null != merchantId ">
		    and `merchant_id` = #{merchantId}
		</if>    
		<if test=" null != couponId ">
		    and `coupon_id` = #{couponId}
		</if>    
		<if test=" null != businessId ">
		    and `business_id` = #{businessId}
		</if>    
		<if test=" null != spend ">
		    and `spend` = #{spend}
		</if>    
		<if test=" null != changePurpose ">
		    and `change_purpose` = #{changePurpose}
		</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>

	<sql id="customWhereConditions">
		where 1 = 1
		<if test=" null != name and '' != name">
			and basic.nick_name like concat('%', #{name},'%')
		</if>
		<if test=" null != phone and '' != phone">
			and basic.phone like concat('%', #{phone},'%')
		</if>
		<if test=" null != startTime ">
			and credit.create_date &gt;= #{startTime}
		</if>
		<if test=" null != endTime">
			and credit.create_date &lt; #{endTime}
		</if>
		<if test=" null != tenantId ">
			and credit.tenant_id = #{tenantId}
		</if>
		<if test=" null != operatorId ">
			and `operator_id` = #{operatorId}
		</if>
		<if test=" null != creditType ">
			and `credit_type` = #{creditType}
		</if>
		<if test=" null != cUserId ">
			and `c_user_id` = #{cUserId}
		</if>
	</sql>

	<select id="findList" parameterType="com.iformall.domain.po.WxCreditHistory" resultMap="BaseResultMap">
		select <include refid="allColumns" /> from wx_credit_history
		<include refid="dynamicWhereConditions" />
	</select>

	<select id="findListMore" parameterType="com.iformall.domain.po.WxCreditHistory" resultType="com.iformall.domain.vo.WxCreditHistoryVo">
		SELECT
			credit.id creditId,
			basic.nick_name name,
			basic.phone phone,
			basic.sex sex,
			credit.tenant_id tenantId,
			credit.c_user_id cUserId,
			credit.credit_amount creditAmount,
			credit.credit_num creditNum,
			credit.create_date createDate,
			credit.credit_type creditType,
			credit.receipt_url receiptUrl,
			credit.operator_type operatorType,
			credit.operator_id operatorId,
			merchant.NAME merchantName
		FROM
			wx_c_user_basic_info basic
			INNER JOIN wx_credit_history credit ON basic.id = credit.c_user_id
			LEFT JOIN wx_merchant merchant ON credit.merchant_id = merchant.id
		<include refid="customWhereConditions"/>
		ORDER BY
			credit.id DESC
	</select>

	<select id="loginCount" parameterType="com.iformall.domain.po.WxCreditHistory" resultType="java.lang.Integer">
		select COUNT(0) from wx_credit_history
		where 1=1
		<if test=" null != tenantId ">
			and `tenant_id` = #{tenantId}
		</if>
		<if test=" null != cUserId ">
			and `c_user_id` = #{cUserId}
		</if>
		<if test=" null != creditType ">
			and `credit_type` = #{creditType}
		</if>
		and DATEDIFF(`create_date`,now())=0
	</select>

	<select id="countTodayList" parameterType="com.iformall.domain.po.WxCreditHistory" resultType="java.lang.Integer">
		select count(0)
		from wx_credit_history
		where 1=1
		<if test=" null != tenantId ">
			and `tenant_id` = #{tenantId}
		</if>
		<if test=" null != cUserId ">
			and `c_user_id` = #{cUserId}
		</if>
		<if test=" null != creditType ">
			and `credit_type` = #{creditType}
		</if>
		and DATEDIFF(`create_date`,now())=0
	</select>

	<select id="creditAmount" parameterType="java.lang.Long" resultType="java.lang.Integer">
		SELECT credit_amount FROM wx_credit_history where c_user_id = #{cUserId} ORDER BY id desc limit 1
	</select>

	<select id="countList" parameterType="com.iformall.domain.po.WxCreditHistory" resultType="java.lang.Integer">
		select count(0)
		from wx_credit_history
		where 1=1
		<if test=" null != tenantId ">
			and `tenant_id` = #{tenantId}
		</if>
		<if test=" null != cUserId ">
			and `c_user_id` = #{cUserId}
		</if>
		<if test=" null != creditType ">
			and `credit_type` = #{creditType}
		</if>
	</select>
</mapper>
