<?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.agenda.dao.AgendaMapper">

    <resultMap id="resultMap" type="com.cftech.workshop.agenda.model.Agenda">
        <id column="id" property="id"/>
        <result column="number" property="number"/>
        <result column="agenda_time" property="agenda_time"/>
        <result column="workshop_id" property="workshop_id"/>
        <result column="content_agenda" property="content_agenda"/>
        <result column="content_agenda_en" property="content_agenda_en"/>
        <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="agenda_title" property="agenda_title"/>
        <result column="agenda_title_en" property="agenda_title_en"/>
        <result column="agenda_type" property="agenda_type"/>
        <result column="speech_id" property="speech_id"/>
        <result column="customiz_id" property="customiz_id"/>
        <result column="sort" property="sort"/>
    </resultMap>

    <resultMap id="resultMapDtoB" type="com.cftech.workshop.agenda.model.AgendaDtoB">
        <id column="id" property="id"/>
        <result column="number" property="number"/>
        <result column="agenda_time" property="agenda_time"/>
        <result column="workshop_id" property="workshop_id"/>
        <result column="content_agenda" property="content_agenda"/>
        <result column="content_agenda_en" property="content_agenda_en"/>
        <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="agenda_title" property="agenda_title"/>
        <result column="agenda_title_en" property="agenda_title_en"/>
        <result column="agenda_type" property="agenda_type"/>
        <result column="speech_id" property="speech_id"/>
        <result column="customiz_id" property="customiz_id"/>
        <result column="sort" property="sort"/>
        <result column="speech_name" property="speech_name"/>
        <result column="speech_name_en" property="speech_name_en"/>
    </resultMap>

    <resultMap id="resultMapDto" type="com.cftech.workshop.agenda.model.AgendaDto">
        <id column="id" property="id"/>
        <result column="agenda_title" property="agenda_title"/>
        <result column="agenda_title_en" property="agenda_title_en"/>
        <result column="workshop_id" property="workshop_id"/>
        <result column="content_agenda" property="content_agenda"/>
        <result column="content_agenda_en" property="content_agenda_en"/>
        <result column="score" property="score"/>
        <result column="agenda_time" property="agenda_time"/>
        <result column="title" property="title"/>
        <result column="title_en" property="title_en"/>
        <result column="member_uid" property="member_uid"/>
        <result column="agenda_type" property="agenda_type"/>
    </resultMap>

    <resultMap id="resultMapAgendaSpeech" type="com.cftech.workshop.agenda.model.AgendaSpeech">
        <id column="id" property="id"/>
        <result column="speech_id" property="speech_id"/>
        <result column="agenda_title" property="agenda_title"/>
        <result column="agenda_title_en" property="agenda_title_en"/>
        <result column="speech_name" property="speech_name"/>
        <result column="speech_name_en" property="speech_name_en"/>
        <result column="content_agenda" property="content_agenda"/>
        <result column="content_agenda_en" property="content_agenda_en"/>
    </resultMap>

    <resultMap id="resultAgendaCustomiz" type="com.cftech.workshop.agenda.model.AgendaCustomiz">
        <id column="id" property="id"/>
        <result column="agenda_title" property="agenda_title"/>
        <result column="agenda_time" property="agenda_time"/>
        <result column="sort" property="sort"/>
        <result column="customiz_id" property="customiz_id"/>
        <result column="customiz_name" property="customiz_name"/>
        <result column="workshop_id" property="workshop_id"/>
    </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,
                                                agenda_title,
                                                agenda_title_en,
                                                agenda_time,
                                                workshop_id,
                                                content_agenda,
                                                content_agenda_en,
                                                accounts_id,
                                                del_flag,
                                                status,
                                                create_time,
                                                update_time,
                                                description,
                                                create_by,
                                                update_by,
                                                agenda_type,
                                                speech_id,
                                                customiz_id,
                                                sort
                        </sql>


    <insert id="save" parameterType="com.cftech.workshop.agenda.model.Agenda" useGeneratedKeys="true"
            keyProperty="id">
        insert into wx_mp_meeting_agenda
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{number, jdbcType=VARCHAR},
        #{agenda_title},
        #{agenda_title_en},
        #{agenda_time},
        #{workshop_id, jdbcType=BIGINT},
        #{content_agenda, jdbcType=CLOB},
        #{content_agenda_en, jdbcType=CLOB},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT},
        #{agenda_type},
        #{speech_id},
        #{customiz_id},
        #{sort}
        )
    </insert>

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

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

    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM wx_mp_meeting_agenda
        <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.agenda.model.Agenda">
        update wx_mp_meeting_agenda
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="number != null">
                number = #{number, jdbcType=VARCHAR},
            </if>
            <if test="agenda_title!=null">
                agenda_title = #{agenda_title},
            </if>
            <if test="agenda_title_en!=null">
                agenda_title_en = #{agenda_title_en},
            </if>
            <if test="agenda_time != null">
                agenda_time = #{agenda_time, jdbcType=TIMESTAMP},
            </if>
            <if test="workshop_id != null">
                workshop_id = #{workshop_id, jdbcType=BIGINT},
            </if>
            <if test="content_agenda != null">
                content_agenda = #{content_agenda, jdbcType=CLOB},
            </if>
            <if test="content_agenda_en != null">
                content_agenda_en = #{content_agenda_en, jdbcType=CLOB},
            </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="agenda_type!=null">
                agenda_type = #{agenda_type},
            </if>
            <if test="speech_id!=null">
                speech_id = #{speech_id},
            </if>
            <if test="customiz_id != null">
                customiz_id = #{customiz_id},
            </if>
            <if test="sort != null">
                sort = #{sort},
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

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

    <select id="selectagendascor" parameterType="java.util.Map" resultMap="resultMapDto">
        SELECT
        a.id,
        a.agenda_time,
        a.agenda_title,
        a.workshop_id,
        a.agenda_title_en,
        a.content_agenda,
        a.content_agenda_en,
        m.title,
        m.title_en,
        (
        SELECT
        ROUND(AVG(score), 1)
        FROM
        wx_mp_meeting_agendascore
        WHERE
        agenda_id = a.id
        AND del_flag = 0
        ) score
        FROM
        wx_mp_meeting_agenda a
        LEFT JOIN wx_mp_meeting m ON m.id = a.workshop_id
        <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="selectagendascore1" parameterType="java.util.Map" resultMap="resultMapDto">
        SELECT
        a.id,
        a.agenda_title,
        a.agenda_title_en,
        a.agenda_time,
        a.content_agenda,
        a.agenda_type,
        a.content_agenda_en,
        a.workshop_id,
        ada.score,
        ada.member_uid
        FROM
        wx_mp_meeting_agenda a
        LEFT JOIN wx_mp_meeting_agendascore ada ON ada.agenda_id = a.id
        <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="selectagendacustomiz" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM wx_mp_meeting_agenda
        <include refid="sqlWhere"/>
        <if test="limit>0">limit #{offset},#{limit}</if>
        GROUP BY agenda_time
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
    </select>

    <select id="selectagendaspeech" parameterType="java.util.Map" resultMap="resultMapAgendaSpeech">
        SELECT
        a.id,
        a.speech_id,
        a.content_agenda,
        a.content_agenda_en,
        a.agenda_title,
        a.agenda_title_en,
        s.speech_name,
        s.speech_name_en
        FROM
        wx_mp_meeting_agenda a
        LEFT JOIN wx_mp_meeting_speech s ON s.id = a.speech_id
        LEFT JOIN wx_mp_meeting_agenda_customiz ac ON ac.id = a.customiz_id
        <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="agendaCustomizs" parameterType="java.util.Map" resultMap="resultAgendaCustomiz">
        SELECT
	    a.id,
	    a.agenda_title,
	    a.agenda_time,
	    a.sort,
	    c.customiz_name,
        c.id customiz_id,
        a.workshop_id
        FROM
	    wx_mp_meeting_agenda a
        LEFT JOIN wx_mp_meeting_agenda_customiz c ON a.customiz_id = c.id AND c.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="agendaCustomizcount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT
        COUNT(1)
        FROM
        wx_mp_meeting_agenda a
        LEFT JOIN wx_mp_meeting_agenda_customiz c ON a.customiz_id = c.id AND c.del_flag = 0
        <include refid="sqlWhere"/>
    </select>

    <select id="selectspeechb" parameterType="java.util.Map" resultMap="resultMapDtoB">
        SELECT
	a.id,
	a.number,
	a.agenda_title,
	a.agenda_title_en,
	a.agenda_time,
	a.workshop_id,
	a.content_agenda,
	a.content_agenda_en,
	a.accounts_id,
	a.del_flag,
	a.STATUS,
	a.create_time,
	a.update_time,
	a.description,
	a.create_by,
	a.update_by,
	a.agenda_type,
	a.speech_id,
	a.customiz_id,
	a.sort,
	s.speech_name,
    s.speech_name_en
FROM
	wx_mp_meeting_agenda a
LEFT JOIN wx_mp_meeting_speech s ON s.id = speech_id AND s.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>
</mapper>