<?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.WxScoreHistoryMapper">
    <resultMap id="BaseResultMap" type="com.iformall.domain.po.WxScoreHistory">
        <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="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="score_type" jdbcType="INTEGER" property="scoreType"/>
        <result column="valid_date" jdbcType="TIMESTAMP" property="validDate"/>
        <result column="order_id" jdbcType="BIGINT" property="orderId"/>
        <result column="pay_type" jdbcType="INTEGER" property="payType"/>
        <result column="pay_amount" jdbcType="INTEGER" property="payAmount"/>
        <result column="score_amount" jdbcType="INTEGER" property="scoreAmount"/>
        <result column="reason" jdbcType="VARCHAR" property="reason"/>
    </resultMap>

    <sql id="allColumns">
	    `id`,`tenant_id`,`c_user_id`,`create_date`,`score_type`,`valid_date`,`order_id`,`pay_type`,`pay_amount`,`score_amount`,`reason`
	</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 != cUserId ">
            and `c_user_id` = #{cUserId}
        </if>

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

        <if test=" null != scoreType ">
            and `score_type` = #{scoreType}
        </if>

        <if test=" null != validDate ">
            and `valid_date` = #{validDate}
        </if>

        <if test=" null != orderId ">
            and `order_id` = #{orderId}
        </if>

        <if test=" null != payType ">
            and `pay_type` = #{payType}
        </if>

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

        <if test=" null != scoreAmount ">
            and `score_amount` = #{scoreAmount}
        </if>
        <if test=" null != reason ">
            and `reason` = #{reason}
        </if>
        <if test=" null != startdate and  null!=enddate">
            and `create_date` between #{startdate} and #{enddate}
        </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.WxScoreHistory" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from wx_score_history
        <include refid="dynamicWhereConditions"/>
    </select>

    <select id="countTodayList" parameterType="com.iformall.domain.po.WxScoreHistory" resultType="java.lang.Integer">
        select count(*)
        from wx_score_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 != scoreType ">
            and `score_type` = #{scoreType}
        </if>
        and DATEDIFF(`create_date`,now())=0
    </select>

    <select id="countList" parameterType="com.iformall.domain.po.WxScoreHistory" resultType="java.lang.Integer">
        select count(*)
        from wx_score_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 != scoreType ">
            and `score_type` = #{scoreType}
        </if>
    </select>

    <update id="updateNewId" parameterType="com.iformall.domain.vo.WxScoreHistoryNewIdVo">
		update wx_score_history
		set c_user_id=#{newUserId}
		where tenant_id=#{tenantId}
		and c_user_id = #{cUserId}
	</update>

</mapper>
