<?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.cftech.module.activity.core.dao.ActivityBaseResultMapper">

    <resultMap id="resultMap" type="com.cftech.module.activity.core.model.ActivityBaseResultEntity">
        <id column="id" property="id"/>
        <result column="from" property="from"/>
        <result column="activity_id" property="activityId"/>
        <result column="title" property="title"/>
        <result column="open_id" property="openId"/>
        <result column="prize" property="prize"/>
        <result column="qty" property="qty"/>
        <result column="prize_detail" property="prizeDetail"/>
        <result column="prize_type" property="prizeType"/>
        <result column="prize_amount" property="prizeAmount"/>
        <result column="accounts_id" property="accountsId"/>
        <result column="del_flag" property="delFlag"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="description" property="description"/>
        <result column="create_by" property="createBy"/>
        <result column="update_by" property="updateBy"/>
    </resultMap>

    <resultMap id="resultMapDto" type="com.cftech.module.activity.core.model.ActivityBaseResultDto">
        <result column="from" property="from"/>
        <result column="activity_id" property="activityId"/>
        <result column="title" property="title"/>
        <result column="open_id" property="openId"/>
        <result column="prize" property="prize"/>
        <result column="qty" property="qty"/>
        <result column="prize_detail" property="prizeDetail"/>
        <result column="prize_type" property="prizeType"/>
        <result column="prize_amount" property="prizeAmount"/>
        <result column="create_time" property="createTime"/>
    </resultMap>

    <sql id="sqlWhere">
        <if test="conds!=null">
            <trim prefix="WHERE" prefixOverrides="AND|OR">
                <foreach collection="conds.conds" index="index" item="cond">
                    ${cond.linkType}
                    <if test="cond.condType == 'EQUAL'">${cond.param} = #{cond.value}</if>
                    <if test="cond.condType == 'NOTEQUAL'">${cond.param} &lt;&gt; #{cond.value}</if>
                    <if test="cond.condType == 'GREATEQUAL'">${cond.param} &gt;= #{cond.value}</if>
                    <if test="cond.condType == 'GREATTHAN'">${cond.param} &gt; #{cond.value}</if>
                    <if test="cond.condType == 'LESSEQUAL'">${cond.param} &lt;= #{cond.value}</if>
                    <if test="cond.condType == 'LESSTHAN'">${cond.param} &lt; #{cond.value}</if>
                    <if test="cond.condType == 'BETWEEN'">${cond.param} BETWEEN #{cond.startValue} AND
                        #{cond.endValue}
                    </if>
                    <if test="cond.condType == 'ISNULL'">${cond.param} IS NULL</if>
                    <if test="cond.condType == 'NOTNULL'">${cond.param} IS NOT NULL</if>
                    <if test="cond.condType == 'LIKE'">${cond.param} LIKE #{cond.value}</if>
                    <if test="cond.condType == 'IN'">${cond.param} IN
                        <foreach item="item" index="index" collection="cond.value" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </if>
                </foreach>
            </trim>
        </if>
    </sql>

    <sql id="sqlColumns">
                                    id,
                                                `from`,
                                                activity_id,
                                                title,
                                                open_id,
                                                prize,
                                                qty,
                                                prize_detail,
                                                prize_type,
                                                prize_amount,
                                                accounts_id,
                                                del_flag,
                                                status,
                                                create_time,
                                                update_time,
                                                description,
                                                create_by,
                                                update_by
                        </sql>


    <insert id="save" parameterType="com.cftech.module.activity.core.model.ActivityBaseResultEntity"
            useGeneratedKeys="true"
            keyProperty="id">
        insert into wx_mp_activity_result
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{from, jdbcType=VARCHAR},
        #{activityId, jdbcType=BIGINT},
        #{title, jdbcType=VARCHAR},
        #{openId, jdbcType=VARCHAR},
        #{prize, jdbcType=CLOB},
        #{qty, jdbcType=INTEGER},
        #{prizeDetail, jdbcType=VARCHAR},
        #{prizeType, jdbcType=VARCHAR},
        #{prizeAmount, jdbcType=VARCHAR},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT}
        )
    </insert>

    <select id="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM wx_mp_activity_result t
        WHERE t.id=#{id}
    </select>

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM wx_mp_activity_result result
        LEFT JOIN wx_mp_activity_wheel wheel ON wheel.id = result.activity_id
        AND result.`from` = 'Wheel'
        LEFT JOIN wx_activity_eggs eggs ON eggs.id = result.activity_id
        AND result.`from` = 'Eggs'
        LEFT JOIN wx_mp_activity_giftbox giftbox ON giftbox.id = result.activity_id
        AND result.`from`='Giftbox'
        LEFT JOIN wx_mp_activity_scratch scratch ON scratch.id = result.activity_id
        AND result.`from`='Scratch'
        <include refid="sqlWhere"/>
        <if test="userId!=null">
            and (wheel.create_by=#{userId} or eggs.create_by=#{userId} or giftbox.create_by=#{userId} or scratch.create_by=#{userId})
        </if>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        result.id,
        result.`from`,
        result.activity_id,
        result.title,
        result.open_id,
        result.prize,
        result.qty,
        result.prize_detail,
        result.prize_type,
        result.prize_amount,
        result.accounts_id,
        result.del_flag,
        result.STATUS,
        result.create_time,
        result.update_time,
        result.description,
        result.create_by,
        result.update_by
        FROM
        wx_mp_activity_result result
        LEFT JOIN wx_mp_activity_wheel wheel ON wheel.id = result.activity_id
        AND result.`from` = 'Wheel'
        LEFT JOIN wx_activity_eggs eggs ON eggs.id = result.activity_id
        AND result.`from` = 'Eggs'
        LEFT JOIN wx_mp_activity_giftbox giftbox ON giftbox.id = result.activity_id
        AND result.`from`='Giftbox'
        LEFT JOIN wx_mp_activity_scratch scratch ON scratch.id = result.activity_id
        AND result.`from`='Scratch'
        <include refid="sqlWhere"/>
        <if test="userId!=null">
            and (wheel.create_by=#{userId} or eggs.create_by=#{userId} or giftbox.create_by=#{userId} or scratch.create_by=#{userId})
        </if>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>
    
    <select id="selectmywin" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        result.id,
        result.`from`,
        result.activity_id,
        result.title,
        result.open_id,
        result.prize,
        result.qty,
        result.prize_detail,
        result.prize_type,
        result.prize_amount,
        result.accounts_id,
        result.del_flag,
        result.STATUS,
        result.create_time,
        result.update_time,
        result.description,
        result.create_by,
        result.update_by
        FROM
        wx_mp_activity_result result
        LEFT JOIN wx_mp_activity_wheel wheel ON wheel.id = result.activity_id
        AND result.`from` = 'Wheel'
        LEFT JOIN wx_activity_eggs eggs ON eggs.id = result.activity_id
        AND result.`from` = 'Eggs'
        LEFT JOIN wx_mp_activity_giftbox giftbox ON giftbox.id = result.activity_id
        AND result.`from`='Giftbox'
        LEFT JOIN wx_mp_activity_scratch scratch ON scratch.id = result.activity_id
        AND result.`from`='Scratch'
        <include refid="sqlWhere"/>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <select id="fetchSearchByPageExport" parameterType="java.util.Map" resultMap="resultMapDto">
        SELECT
        <include refid="sqlColumns"/>
        FROM wx_mp_activity_result
        <include refid="sqlWhere"/>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <update id="update" parameterType="com.cftech.module.activity.core.model.ActivityBaseResultEntity">
        update wx_mp_activity_result
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="from != null">
                `from` = #{from, jdbcType=VARCHAR},
            </if>
            <if test="activityId != null">
                activity_id = #{activityId, jdbcType=BIGINT},
            </if>
            <if test="title != null">
                title = #{title, jdbcType=VARCHAR},
            </if>
            <if test="openId != null">
                open_id = #{openId, jdbcType=VARCHAR},
            </if>
            <if test="prize != null">
                prize = #{prize, jdbcType=CLOB},
            </if>
            <if test="qty != null">
                qty = #{qty, jdbcType=INTEGER},
            </if>
            <if test="prizeDetail != null">
                prize_detail = #{prizeDetail, jdbcType=VARCHAR},
            </if>
            <if test="prizeType != null">
                prize_type = #{prizeType, jdbcType=VARCHAR},
            </if>
            <if test="prizeAmount != null">
                prize_amount = #{prizeAmount, jdbcType=VARCHAR},
            </if>
            <if test="accountsId != null">
                accounts_id = #{accountsId, jdbcType=BIGINT},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag, jdbcType=TINYINT},
            </if>
            <if test="status != null">
                status = #{status, jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime, jdbcType=TIMESTAMP},
            </if>
            <if test="description != null">
                description = #{description, jdbcType=VARCHAR},
            </if>
            <if test="createBy != null">
                create_by = #{createBy, jdbcType=BIGINT},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy, jdbcType=BIGINT},
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

    <update id="delete" parameterType="java.lang.Long">
        update wx_mp_activity_result set del_flag=1 where id=#{id,jdbcType=BIGINT}
    </update>

    <insert id="prizeLog" parameterType="java.util.Map"
            useGeneratedKeys="true"
            keyProperty="id">
        insert into wx_mp_activity_result
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{from, jdbcType=VARCHAR},
        #{activityId, jdbcType=BIGINT},
        #{title, jdbcType=VARCHAR},
        #{openId, jdbcType=VARCHAR},
        #{prize, jdbcType=CLOB},
        #{qty, jdbcType=INTEGER},
        #{prizeDetail, jdbcType=VARCHAR},
        #{prizeType, jdbcType=VARCHAR},
        #{prizeAmount, jdbcType=VARCHAR},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT}
        )
    </insert>
</mapper>