<?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.PaperLogMapper">

    <resultMap id="resultMap" type="com.cftech.questionnaire.core.paper.model.PaperLog">
        <id column="id" property="id"/>
        <result column="paper_id" property="paperId"/>
        <result column="accounts_id" property="accountsId"/>
        <result column="bo_id" property="boId"/>
        <result column="answers" property="answers"/>
        <result column="palyer_id" property="palyerId"/>
        <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="subject_id" property="subject_id"/>
        <result column="option_con" property="option_con"/>
        <result column="option_id" property="option_id"/>
    </resultMap>

    <resultMap id="resultPaperAnswer" type="com.cftech.questionnaire.core.paper.model.PaperAnswer">
        <id column="id" property="id"/>
        <result column="paper_id" property="paper_id"/>
        <result column="option_name" property="option_name"/>
        <result column="label" property="label"/>
        <result column="subject_id" property="subject_id"/>
        <result column="subject_name" property="subject_name"/>
        <result column="Percentage" property="Percentage"/>
        <result column="counts" property="counts"/>
    </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_id,
                                                bo_id,
                                                answers,
                                                palyer_id,
                                                accounts_id,
                                                del_flag,
                                                status,
                                                create_time,
                                                update_time,
                                                description,
                                                create_by,
                                                update_by,
                                                subject_id,
                                                option_con,
                                                option_id
                        </sql>


    <insert id="save" parameterType="com.cftech.questionnaire.core.paper.model.PaperLog" useGeneratedKeys="true"
            keyProperty="id">
        insert into t_paper_log
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{paperId, jdbcType=BIGINT},
        #{boId, jdbcType=BIGINT},
        #{answers, jdbcType=CLOB},
        #{palyerId, jdbcType=VARCHAR},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT},
        #{subject_id},
        #{option_con},
        #{option_id}
        )
    </insert>

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

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM t_paper_log
        <include refid="sqlWhere"/>

    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM t_paper_log
        <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.questionnaire.core.paper.model.PaperLog">
        update t_paper_log
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="paperId != null">
                paper_id = #{paperId, jdbcType=BIGINT},
            </if>
            <if test="accountsId != null">
                accounts_id = #{accountsId, jdbcType=BIGINT},
            </if>
            <if test="boId != null">
                bo_id = #{boId, jdbcType=BIGINT},
            </if>
            <if test="answers != null">
                answers = #{answers, jdbcType=LONGTEXT},
            </if>
            <if test="palyerId != null">
                palyer_id = #{palyerId, 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 t_paper_log set del_flag=1 where id=#{id,jdbcType=BIGINT}
    </update>


    <select id="fetchPaperLogList" parameterType="java.util.Map"
            resultType="com.cftech.questionnaire.core.paper.model.PaperLogDto">
        SELECT
        p.id paperId,
        p.paper_title paperTitle,
        s.subject_name subjectName,
        case s.type
        when 'BLANKFILL' then pl.option_con
        else
        o.option_name
        end optionName,
        s.type subjectType,
        m. NAME memberName,
        m.surname surMemberName ,
        pl.create_time  createTime,
        w.sort ,
        w.peopletype
        FROM
        t_paper_log pl
        LEFT JOIN t_paper_option o ON o.id = pl.option_id
        LEFT JOIN t_paper_subject s ON s.id = pl.subject_id
        LEFT JOIN t_paper p ON p.id = s.paper_id
        LEFT JOIN wx_mp_member m ON m.open_id = pl.palyer_id
        LEFT JOIN wx_mp_meeting_activ a ON a.activ_id = p.id
        AND a.activ_type = 4 and a.del_flag = 0
        LEFT JOIN wx_mp_workshop w ON w.member_uid = m.member_uid
        AND w.wsid = a.workshop_id
        AND w.del_flag = 0
        <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="fetchPaperLogListCount" parameterType="java.util.Map"
            resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        t_paper_log pl
        LEFT JOIN t_paper_option o ON o.id = pl.option_id
        LEFT JOIN t_paper_subject s ON s.id = pl.subject_id
        LEFT JOIN t_paper p ON p.id = s.paper_id
        LEFT JOIN wx_mp_member m ON m.open_id = pl.palyer_id
        LEFT JOIN wx_mp_meeting_activ a ON a.activ_id = p.id
        AND a.activ_type = 4 and a.del_flag = 0
        LEFT JOIN wx_mp_workshop w ON w.member_uid = m.member_uid
        AND w.wsid = a.workshop_id
        AND w.del_flag = 0
        <include refid="sqlWhere"/>
    </select>

    <select id="searchpaperanswer" parameterType="java.util.Map" resultMap="resultPaperAnswer">
        SELECT
        po.paper_id,
        po.option_name,
        po.label,
        po.subject_id,
        ps.subject_name,
        ( SELECT COUNT(1) FROM
        (
        SELECT
        *
        FROM
        t_paper_log
        WHERE
        paper_id = #{paperId}
        group by palyer_id,option_id
        ) a
        WHERE
        a.option_id = po.id
        ) counts,
        CONCAT(
        ROUND(
        (
        (
        SELECT
        COUNT(1)
        FROM
        (
        SELECT
        *
        FROM
        t_paper_log
        WHERE
        paper_id = #{paperId}
        group by palyer_id,option_id
        ) a
        WHERE
        a.option_id = po.id
        ) / (
        SELECT
        COUNT(1)
        FROM
        (
        SELECT
        *
        FROM
        t_paper_log
        WHERE
        paper_id =#{paperId}
        GROUP BY
        palyer_id
        ) b
        )
        ) * 100,
        0
        ),
        '%'
        ) Percentage
        FROM
        t_paper_option po
        LEFT JOIN t_paper_subject ps ON ps.id = po.subject_id
        AND ps.del_flag = 0
        <include refid="sqlWhere"/>
    </select>

</mapper>