<?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.questionnaire.core.paper.dao.PaperMapper">

    <resultMap id="resultMap" type="com.cftech.questionnaire.core.paper.model.PaperBaseEntity">
        <id column="id" property="id"/>
        <result column="paper_type" property="paperType"/>
        <result column="paper_title" property="paperTitle"/>
        <result column="paper_category" property="paperCategory"/>
        <result column="questions" property="questions"/>
        <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"/>
        <result column="paper_pic" property="paperPic"/>
        <result column="paper_title_en" property="paperTitleEn"/>
        <result column="activity_id" property="activity_id"/>
        <result column="activity_type" property="activity_type"/>
        <result column="act_start_pic" property="act_start_pic"/>
        <result column="content_des" property="content_des"/>
        <result column="content_des_en" property="content_des_en"/>
        <result column="reading" property="reading"/>
    </resultMap>

    <resultMap id="resultMapSelect" type="com.cftech.questionnaire.core.paper.model.SelectActivity">
        <id column="id" property="id"/>
        <result column="title" property="title"/>
        <result column="type" property="type"/>
        <result column="create_time" property="create_time"/>
        <result column="create_by" property="create_by"/>
    </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,
        paper_type,
        paper_title,
        paper_pic,
        paper_category,
        questions,
        del_flag,
        status,
        create_time,
        update_time,
        description,
        create_by,
        update_by,
        paper_title_en,
        activity_id,
        activity_type,
        act_start_pic,
        content_des,
        content_des_en,
        reading
    </sql>


    <insert id="save" parameterType="com.cftech.questionnaire.core.paper.model.PaperBaseEntity" useGeneratedKeys="true"
            keyProperty="id">
        insert into t_paper
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{paperType, jdbcType=VARCHAR},
        #{paperTitle, jdbcType=VARCHAR},
        #{paperPic, jdbcType=VARCHAR},
        #{paperCategory, jdbcType=BIGINT},
        #{questions, jdbcType=CLOB},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT},
        #{paperTitleEn, jdbcType=VARCHAR},
        #{activity_id,jdbcType=BIGINT },
        #{activity_type,jdbcType=VARCHAR},
        #{act_start_pic},
        #{content_des},
        #{content_des_en},
        #{reading}
        )
    </insert>

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

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT
        COUNT(1)
        FROM
        t_paper paper
        LEFT JOIN t_paper_category category ON category.id = paper.paper_category
        <include refid="sqlWhere"/>
        <if test="pId > 0">
            and instr(category.parent_ids,',${pId},')>0
        </if>

    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM t_paper
        <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="fetchSearchVOByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        paper.id,
        paper.paper_title paperTitle,
        paper.paper_title_en,
        paper.paper_type paperType,
        paper.create_time createTime,
        paper.del_flag delFlag
        FROM
        t_paper paper

        <include refid="sqlWhere"/>
        <if test="pId > 0">
            and instr(category.parent_ids,',${pId},')>0
        </if>
        <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.questionnaire.core.paper.model.PaperBaseEntity">
        update t_paper
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="paperType != null">
                paper_type = #{paperType, jdbcType=VARCHAR},
            </if>
            <if test="paperTitle != null">
                paper_title = #{paperTitle, jdbcType=VARCHAR},
            </if>
            <if test="paperCategory != null">
                paper_category = #{paperCategory, jdbcType=BIGINT},
            </if>
            <if test="questions != null">
                questions = #{questions, jdbcType=CLOB},
            </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>
            <if test="paperTitleEn != null">
                paper_title_en = #{paperTitleEn, jdbcType=VARCHAR},
            </if>
            <if test="activity_id != null">
                activity_id = #{activity_id , jdbcType=BIGINT},
            </if>
            <if test="activity_type != null">
                activity_type = #{activity_type,jdbcType=VARCHAR},
            </if>
            <if test="act_start_pic!=null">
                act_start_pic = #{act_start_pic},
            </if>
            <if test="content_des!=null">
                content_des =#{content_des},
            </if>
            <if test="content_des_en!=null">
                content_des_en =#{content_des_en},
            </if>
            <if test="reading!=null">
                reading = #{reading},
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

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

    <insert id="saveSubject" parameterType="com.cftech.questionnaire.core.paper.model.PaperSubject"
            useGeneratedKeys="true"
            keyProperty="id">
        insert into t_paper_subject
        (
        id,subject_name,subject_name_en,paper_id,del_flag,type,max,min,status,create_time,update_time,description,create_by,update_by
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{subjectName, jdbcType=VARCHAR},
        #{subjectNameEn, jdbcType=VARCHAR},
        #{paperId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{type, jdbcType=VARCHAR},
        #{max, jdbcType=VARCHAR},
        #{min, jdbcType=VARCHAR},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT}
        )
    </insert>

    <update id="updatePaperSubject" parameterType="com.cftech.questionnaire.core.paper.model.PaperSubject">
        update t_paper_subject
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="paperId != null">
                paper_id = #{paperId, jdbcType=BIGINT},
            </if>
            <if test="subjectName != null">
                subject_name = #{subjectName, jdbcType=VARCHAR},
            </if>
            <if test="type != null">
                type = #{type, jdbcType=VARCHAR},
            </if>
            <if test="max != null">
                max = #{max, jdbcType=VARCHAR},
            </if>
            <if test="min != null">
                min = #{min, jdbcType=VARCHAR},
            </if>
            <if test="subjectNameEn != null">
                subject_name_en = #{subjectNameEn, jdbcType=VARCHAR},
            </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="updatePaperOption" parameterType="com.cftech.questionnaire.core.paper.model.PaperSubject">
        update t_paper_option
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="paperId != null">
                paper_id = #{paperId, jdbcType=BIGINT},
            </if>
            <if test="optionName != null">
                option_name = #{optionName, jdbcType=VARCHAR},
            </if>
            <if test="label != null">
                label = #{label, jdbcType=VARCHAR},
            </if>
            <if test="subjectId != null">
                subject_id = #{subjectId, jdbcType=BIGINT},
            </if>
            <if test="optionNameEn != null">
                option_name_en = #{optionNameEn, jdbcType=VARCHAR},
            </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="updateClearPaperSubject" parameterType="java.lang.Long">
        update t_paper_subject set del_flag = 1 where paper_id = #{paperId}
    </update>

    <update id="updateClearOption" parameterType="java.lang.Long">
        update t_paper_option set del_flag = 1 where paper_id = #{paperId}
    </update>

    <insert id="saveOption" parameterType="com.cftech.questionnaire.core.paper.model.PaperOption"
            useGeneratedKeys="true"
            keyProperty="id">
        insert into t_paper_option
        (
       id,option_name,subject_id,paper_id,option_name_en,del_flag,status,create_time,update_time,description,create_by,update_by,label
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{optionName, jdbcType=VARCHAR},
        #{subjectId, jdbcType=VARCHAR},
        #{paperId, jdbcType=BIGINT},
        #{optionNameEn, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT},
        #{label, jdbcType=VARCHAR}
        )
    </insert>

    <select id="searchActivity" parameterType="java.util.Map" resultMap="resultMapSelect">
        SELECT
        *
        FROM
        (
        SELECT
        id,
        title,
        '0' type,
        create_time,
        create_by
        FROM
        wx_mp_activity_wheel
        UNION
        SELECT
        id,
        title,
        '1' type,
        create_time,
        create_by
        FROM
        wx_activity_eggs
        UNION
        SELECT
        id,
        title,
        '2' type,
        create_time,
        create_by
        FROM
        wx_mp_activity_scratch
        UNION
        SELECT
        id,
        title,
        '3' type,
        create_time,
        create_by
        FROM
        wx_mp_activity_giftbox
        ) a
        <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="countActivity" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT
        COUNT(1)
        FROM
        (
        SELECT
        id,
        title,
        '0' type,
        create_time,
        create_by
        FROM
        wx_mp_activity_wheel
        UNION
        SELECT
        id,
        title,
        '1' type,
        create_time,
        create_by
        FROM
        wx_activity_eggs
        UNION
        SELECT
        id,
        title,
        '2' type,
        create_time,
        create_by
        FROM
        wx_mp_activity_scratch
        UNION
        SELECT
        id,
        title,
        '3' type,
        create_time,
        create_by
        FROM
        wx_mp_activity_giftbox
        ) a
        <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="selectPaperSubject" parameterType="java.util.Map" resultType="com.cftech.questionnaire.core.paper.model.PaperSubject">
        select * from t_paper_subject
        <include refid="sqlWhere"/>
    </select>
</mapper>