<?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="cn.wisenergy.mapper.TradeRecordMapper">
    <resultMap id="tradeMap" type="cn.wisenergy.model.app.TradeRecord">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="trade_type" property="tradeType"/>
        <result column="trade_no" property="tradeNo"/>
        <result column="status" property="status"/>
        <result column="task_id" property="taskId"/>
        <result column="money" property="money"/>
        <result column="card_number" property="cardNumber"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <sql id="table">
        trade_record
    </sql>

    <sql id="cols_all">
        id,
        <include refid="cols_exclude_id"/>
    </sql>

    <sql id="cols_exclude_id">
        user_id,trade_type,trade_no,status,task_id,money,card_number,create_time,update_time
    </sql>

    <sql id="vals">
        #{userId},#{tradeType},#{tradeNo},#{status},#{taskId},#{money},#{cardNumber},now(),now()
    </sql>

    <sql id="updateCondition">
        <if test="userId != null">user_id = #{userId},</if>
        <if test="tradeType != null">trade_type =#{tradeType},</if>
        <if test="tradeNo != null">trade_no =#{tradeNo},</if>
        <if test="status != null">status =#{status},</if>
        <if test="taskId != null">task_id =#{taskId},</if>
        <if test="money != null">money =#{money},</if>
        <if test="cardNumber != null">card_number =#{cardNumber},</if>
        update_time =now()
    </sql>

    <sql id="criteria">
        <if test="id != null">id = #{id}</if>
        <if test="userId != null">and user_id = #{userId}</if>
        <if test="tradeType != null">and trade_type =#{tradeType}</if>
        <if test="tradeNo != null">and trade_no =#{tradeNo}</if>
        <if test="status != null">and status =#{status}</if>
        <if test="taskId != null">and task_id =#{taskId}</if>
        <if test="money != null">and money =#{money}</if>
        <if test="cardNumber != null">and card_number =#{cardNumber}</if>
        <if test="createTime != null">and create_time &gt;= #{createTime}</if>
        <if test="updateTime != null">and #{updateTime} &gt;= update_time</if>
    </sql>
    <insert id="add" parameterType="cn.wisenergy.model.app.TradeRecord" keyProperty="id" useGeneratedKeys="true">
        insert into
        <include refid="table"/>
        (<include refid="cols_exclude_id"/>)
        value(
        <include refid="vals"/>
        )
    </insert>

    <update id="edit" parameterType="cn.wisenergy.model.app.TradeRecord">
        UPDATE
        <include refid="table"/>
        <set>
            <include refid="updateCondition"/>
        </set>
        <where>
            id = #{id}
        </where>
    </update>

    <delete id="delById" parameterType="java.lang.Integer">
        delete from
        <include refid="table"/>
        where id = #{id}
    </delete>

    <select id="getByUserId" resultType="cn.wisenergy.model.app.TradeRecord">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            user_id=#{userId}
        </where>
    </select>

    <select id="getByUserIdAndTime" resultType="cn.wisenergy.model.app.TradeRecord">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            <if test="userId">
                user_id=#{userId}
            </if>
            <if test="yearMonth != null">
                AND(
                YEAR(create_time) = YEAR(#{yearMonth})
                AND MONTH(create_time) = MONTH(#{yearMonth}))
            </if>
        </where>
    </select>

    <select id="getSixMonthIncome" resultType="cn.wisenergy.model.vo.AccumulatedIncomeVo">
        SELECT user_id as userId,sum(money) as income ,
        date_format(create_time,'%Y-%m') as yearMonth
        FROM
        <include refid="table"/>
        WHERE date_format(create_time,'%Y-%m') &lt; date_format(now(),'%Y-%m')
        and date_format(create_time,'%Y-%m') >= date_format(now() - interval 7 month,'%Y-%m')
        and (status=1 or status=3)
        and user_id=#{userId}
        group by user_id ,create_time;
    </select>

    <select id="getWithdrawalRecord" resultType="cn.wisenergy.model.vo.WithdrawalRecordVo">
        select user_id as userId, status as status, money as money, update_time as moneyTime
        from
        <include refid="table"/>
        <where>
            (status=2 or status=3)
            <if test="userId">
                and user_id=#{userId}
            </if>
            <if test="yearMonth != null">
                AND(
                YEAR(update_time) = YEAR(#{yearMonth})
                AND MONTH(update_time) = MONTH(#{yearMonth}))
            </if>
        </where>
    </select>

    <select id="queryMonthAward" resultType="java.lang.Double">
        select sum(money)
        from
        <include refid="table"/>
        <where>
            (trade_type=4 or trade_type=7)
            and status=0
            <if test="userId">
                and user_id=#{userId}
            </if>
            <if test="yearMonth != null">
                AND(
                YEAR(create_time) = YEAR(#{yearMonth})
                AND MONTH(create_time) = MONTH(#{yearMonth}))
            </if>
        </where>
    </select>

    <select id="queryMonthGrow" resultType="java.lang.Double">
        select sum(money)
        from
        <include refid="table"/>
        <where>
            (trade_type=4 or trade_type=7)
            and status=0
            <if test="yearMonth != null">
                AND(
                YEAR(create_time) = YEAR(#{yearMonth})
                AND MONTH(create_time) = MONTH(#{yearMonth}))
            </if>
        </where>
    </select>

    <select id="queryAllAward" resultType="java.lang.Double">
        select sum(money)
        from
        <include refid="table"/>
        <where>
            (trade_type=4 or trade_type=7)
            and status=0
            <if test="yearMonth != null">
                and create_time &lt; #{yearMonth}
            </if>
        </where>
    </select>

    <select id="queryByUserLevel" resultType="java.lang.Double">
        select sum(t.money)
        from trade_record t,user_info u
        <where>
            (t.trade_type=4 or t.trade_type=7)
            and t.status=0
            and t.user_id=u.user_id
            <if test="yearMonth != null">
                and t.create_time &lt; #{yearMonth}
                AND(
                YEAR(t.create_time) = YEAR(#{yearMonth})
                AND MONTH(t.create_time) = MONTH(#{yearMonth}))
            </if>
            <if test="userLevel != null">
                and u.user_level=#{userLevel}
            </if>
        </where>
    </select>

    <select id="count" resultType="java.lang.Integer">
        select count(1)
        from
        <include refid="table"/>
        <where>
            <if test="userId != null and userId != ''">
                user_id = #{userId}
            </if>
        </where>
    </select>

    <select id="getList" resultType="cn.wisenergy.model.app.TradeRecord">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            <if test="userId != null and userId != ''">
                user_id = #{userId}
            </if>
        </where>
        order by create_time desc
        limit #{startNum},#{endNum}
    </select>

</mapper>