<?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.workshop.statistics.dao.StatisticsMapper">

    <resultMap id="resultMap" type="com.cftech.workshop.statistics.model.Statistics">
        <id column="id" property="id"/>
        <result column="number" property="number"/>
        <result column="ws_title" property="ws_title"/>
        <result column="ws_peoplenum" property="ws_peoplenum"/>
        <result column="ws_signup" property="ws_signup"/>
        <result column="ws_signin" property="ws_signin"/>
        <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"/>
        <result column="meetingId" property="meetingId"/>
    </resultMap>

    <resultMap id="resultMapWs" type="com.cftech.workshop.statistics.model.WorkshopType">
        <id column="id" property="id"/>
        <result column="peopletype" property="peopletype"/>
        <result column="counts" property="counts"/>
    </resultMap>

    <resultMap id="resultMapSampleclick" type="com.cftech.workshop.statistics.model.Sampleclick">
        <id column="id" property="id"/>
        <result column="workshop_id" property="workshop_id" />
        <result column="sample_title" property="sample_title" />
        <result column="clicks" property="clicks" />
        <result column="counts" property="counts" />
    </resultMap>

    <resultMap id="resultMapSpeechclick" type="com.cftech.workshop.statistics.model.Speechclik">
        <id column="id" property="id"/>
        <result column="workshop_id" property="workshop_id" />
        <result column="speech_name" property="speech_name" />
        <result column="clicks" property="clicks" />
        <result column="counts" property="counts" />
    </resultMap>

    <resultMap id="resultMapAgendascore" type="com.cftech.workshop.statistics.model.Agendascore">
        <id column="id" property="id"/>
        <result column="agenda_title" property="agenda_title"/>
        <result column="workshop_id" property="workshop_id"/>
        <result column="score" property="score"/>
    </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,
                                                number,
                                                ws_title,
                                                ws_peoplenum,
                                                ws_signup,
                                                ws_signin,
                                                accounts_id,
                                                del_flag,
                                                status,
                                                create_time,
                                                update_time,
                                                description,
                                                create_by,
                                                update_by,
                                                meetingId
                        </sql>


    <insert id="save" parameterType="com.cftech.workshop.statistics.model.Statistics" useGeneratedKeys="true"
            keyProperty="id">
        insert into wx_mp_meeting_statistics
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{number, jdbcType=VARCHAR},
        #{ws_title, jdbcType=VARCHAR},
        #{ws_peoplenum, jdbcType=BIGINT},
        #{ws_signup, jdbcType=BIGINT},
        #{ws_signin, jdbcType=BIGINT},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT},
        #{meetingId}
        )
    </insert>

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

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

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        m.id meetingId,
        m.title ws_title,
        (
        SELECT
        COUNT(1)
        FROM
        wx_mp_workshop
        WHERE
        wsid = m.id AND del_flag =0
        ) ws_peoplenum,
        (
        SELECT
        COUNT(1)
        FROM
        wx_mp_workshop
        WHERE
        STATUS IN (1, 2) AND del_flag =0
        AND wsid = m.id
        ) ws_signup,
        (
        SELECT
        COUNT(1)
        FROM
        wx_mp_workshop
        WHERE
        STATUS = 2
        AND wsid = m.id
        AND del_flag = 0
        ) ws_signin,
        m.create_time,
        m.accounts_id,
        m.del_flag
        FROM
        wx_mp_meeting m
        <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.workshop.statistics.model.Statistics">
        update wx_mp_meeting_statistics
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="number != null">
                number = #{number, jdbcType=VARCHAR},
            </if>
            <if test="ws_title != null">
                ws_title = #{ws_title, jdbcType=VARCHAR},
            </if>
            <if test="ws_peoplenum != null">
                ws_peoplenum = #{ws_peoplenum, jdbcType=BIGINT},
            </if>
            <if test="ws_signup != null">
                ws_signup = #{ws_signup, jdbcType=BIGINT},
            </if>
            <if test="ws_signin != null">
                ws_signin = #{ws_signin, jdbcType=BIGINT},
            </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>
            <if test="meetingId!=null">
                meetingId = #{meetingId},
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

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

    <select id="countsampleclicks" parameterType="java.util.Map" resultMap="resultMapSampleclick">
        SELECT
        s.id,
        s.workshop_id,
        s.sample_title,
        s.clicks,
        ROUND(
        (
        (
        SELECT
        count(1)
        FROM
        wx_mp_meeting_sampleclick
        WHERE
        sample_id = s.id
        ) / (
        SELECT
        count(1)
        FROM
        wx_mp_workshop
        WHERE
        STATUS = 2
        AND del_flag = 0
        AND wsid = s.workshop_id
        )
        ) * 100,
        2
        ) counts
        FROM
        wx_mp_meeting_sample s
        LEFT JOIN wx_mp_meeting_sampleclick sc ON s.id = sc.sample_id
        <include refid="sqlWhere"/>
        GROUP BY
        s.id
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <select id="selecttypeworkshop" parameterType="java.util.Map" resultMap="resultMapWs">
        select peopletype,IFNULL(a.counts,'0') counts  from (
        SELECT
        peopletype,
        COUNT(*) AS counts
        FROM
        wx_mp_workshop
        <include refid="sqlWhere"/>
        GROUP BY
	    peopletype) a
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <select id="countspeechclicks" parameterType="java.util.Map" resultMap="resultMapSpeechclick">
        SELECT
	s.id,
	s.workshop_id,
	s.speech_name,
	s.clicks,
	ROUND(
		(
			(
				SELECT
					count(1)
				FROM
					wx_mp_meeting_speechclick
				WHERE
					speech_id = s.id
			) / (
				SELECT
					count(1)
				FROM
					wx_mp_workshop
				WHERE
					STATUS = 2
				AND del_flag = 0
				AND wsid = s.workshop_id
			)
		) * 100,
		2
	) counts
FROM
	wx_mp_meeting_speech s
        LEFT JOIN wx_mp_meeting_speechclick sc ON s.id = sc.speech_id
        <include refid="sqlWhere"/>
GROUP BY
	s.id
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <select id="countagendascore" parameterType="java.util.Map" resultMap="resultMapAgendascore">
        SELECT
        a.id,
        a.agenda_title,
        a.workshop_id,
        IFNULL((
        SELECT
        ROUND(AVG(score), 1)
        FROM
        wx_mp_meeting_agendascore
        WHERE
        agenda_id = a.id
        AND del_flag = 0
        ),0) score
        FROM
        wx_mp_meeting_agenda a
        <include refid="sqlWhere"/>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

</mapper>