<?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.OrderMapper">
    <resultMap id="OrderMap" type="cn.wisenergy.model.app.OrderInfo">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="item_id" property="itemId"/>
        <result column="pay_type" property="payType"/>
        <result column="youzan_update_time" property="youzanUpdateTime"/>
        <result column="tid" property="tid"/>
        <result column="created" property="created"/>
        <result column="team_type" property="teamType"/>
        <result column="pay_time" property="payTime"/>
        <result column="pay_type_str" property="payTypeStr"/>
        <result column="close_type" property="closeType"/>
        <result column="refund_state" property="refundState"/>
        <result column="success_time" property="successTime"/>
        <result column="payment" property="payment"/>
        <result column="rebate_status" property="rebateStatus"/>
        <result column="level_status" property="levelStatus"/>
        <result column="month_order_status" property="monthOrderStatus"/>
        <result column="monthly_task_status" property="monthlyTaskStatus"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

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

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

    <sql id="cols_exclude_id">
        user_id,item_id,pay_type,youzan_update_time,tid,created,team_type,pay_time,pay_type_str,close_type,refund_state,
        success_time,payment,rebate_status,level_status,month_order_status,monthly_task_status,create_time,update_time
    </sql>

    <sql id="vals">
        #{userId},#{itemId},#{payType},#{youzanUpdateTime}, #{tid},#{created},#{teamType},#{payTime},#{payTypeStr},
        #{closeType},#{refundState},#{successTime},#{payment},
        #{rebateStatus},#{levelStatus},#{monthOrderStatus},#{monthlyTaskStatus},now(),now()
    </sql>

    <sql id="updateCondition">
        <if test="userId != null">user_id = #{userId},</if>
        <if test="itemId != null">item_id =#{itemId},</if>
        <if test="payType != null">pay_type =#{payType},</if>
        <if test="youzanUpdateTime != null">youzan_update_time =#{youzanUpdateTime},</if>
        <if test="tid != null">tid =#{tid},</if>
        <if test="created != null">created =#{created},</if>
        <if test="teamType != null">team_type =#{teamType},</if>
        <if test="payTime != null">pay_time =#{payTime},</if>
        <if test="payTypeStr != null">pay_type_str =#{payTypeStr},</if>
        <if test="closeType != null">close_type =#{closeType},</if>
        <if test="refundState != null">refund_state =#{refundState},</if>
        <if test="successTime != null">success_time =#{successTime},</if>
        <if test="payment != null">payment =#{payment},</if>
        <if test="rebateStatus != null">rebate_status =#{rebateStatus},</if>
        <if test="levelStatus != null">level_status =#{levelStatus},</if>
        <if test="monthOrderStatus != null">month_order_status =#{monthOrderStatus},</if>
        <if test="monthlyTaskStatus != null">monthly_task_status =#{monthlyTaskStatus},</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="itemId != null">and item_id =#{itemId}</if>
        <if test="payType != null">and pay_type =#{payType}</if>
        <if test="youzanUpdateTime != null">youzan_update_time =#{youzanUpdateTime}</if>
        <if test="tid != null">and tid =#{tid}</if>
        <if test="created != null">and created =#{created}</if>
        <if test="teamType != null">and team_type =#{teamType}</if>
        <if test="payTime != null">and pay_time =#{payTime}</if>
        <if test="payTypeStr != null">and pay_type_str =#{payTypeStr}</if>
        <if test="closeType != null">and close_type =#{closeType}</if>
        <if test="refundState != null">and refund_state =#{refundState}</if>
        <if test="successTime != null">and success_time =#{successTime}</if>
        <if test="payment != null">and payment =#{payment}</if>
        <if test="rebateStatus != null">and rebate_status =#{rebateStatus}</if>
        <if test="levelStatus != null">and level_status =#{levelStatus}</if>
        <if test="monthOrderStatus != null">and month_order_status =#{monthOrderStatus}</if>
        <if test="monthlyTaskStatus != null">and monthly_task_status =#{monthlyTaskStatus}</if>
        <if test="createTime != null">and create_time &gt;= #{createTime}</if>
        <if test="updateTime != null">and #{updateTime} &gt;= update_time</if>
    </sql>

    <select id="getByRebateStatus" resultType="cn.wisenergy.model.app.OrderInfo">
        SELECT
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            rebateStatus=#{rebateStatus}
        </where>
    </select>

    <insert id="insertYouZanOrders" parameterType="cn.wisenergy.model.app.OrderInfo" keyProperty="id"
            useGeneratedKeys="true">
        insert into
        <include refid="table"/>
        (<include refid="cols_exclude_id"/>)
        value
        (<include refid="vals"/>)
    </insert>

    <select id="getByTid" resultType="cn.wisenergy.model.app.OrderInfo">
        SELECT
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            tid=#{tid}
        </where>
    </select>

    <select id="getPerformanceByUserid" resultType="java.math.BigDecimal">
        select
        sum(payment)
        from
        <include refid="table"/>
        <where>
            user_id=#{userid}
        </where>
    </select>

    <select id="getListBySuccessTime" resultType="cn.wisenergy.model.app.OrderInfo">
        SELECT
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            rebate_status=0
            <if test="successTime != null ">
                AND(
                YEAR(success_time) = YEAR(#{successTime})
                AND MONTH(success_time) = MONTH(#{successTime}))
            </if>
        </where>
    </select>

    <select id="getByCreateTime" resultType="cn.wisenergy.model.app.OrderInfo">
        SELECT
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            <if test="created != null ">
                YEAR(created) = YEAR(#{created})
                AND MONTH(created) = MONTH(#{created})
            </if>
        </where>
    </select>

    <select id="getByLevelStatus" resultType="cn.wisenergy.model.app.OrderInfo">
        SELECT
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            level_status=#{levelStatus}
        </where>
    </select>

    <select id="getByMonthOrderStatus" resultType="cn.wisenergy.model.app.OrderInfo">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        where
        month_order_status = #{monthOrderStatus}
    </select>

</mapper>