<?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.meeting.dao.MeetingMapper">

    <resultMap id="resultMap" type="com.cftech.workshop.meeting.model.Meeting">
        <id column="id" property="id"/>
        <result column="number" property="number"/>
        <result column="title" property="title"/>
        <result column="title_en" property="titleEn"/>
        <result column="cover" property="cover"/>
        <result column="wx_cover" property="wxCover"/>
        <result column="content_brief_info" property="contentBriefInfo"/>
        <result column="content_brief_en_info" property="contentBriefEnInfo"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="qr_code" property="qrCode"/>
        <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="service" property="service" />
        <result column="closing_date" property="closing_date"/>
        <result column="address" property="address"/>
        <result column="address_en" property="address_en"/>
        <result column="is_questions" property="isQuestions"/>
    </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,
                                                title,
                                                title_en,
                                                cover,
                                                wx_cover,
                                                content_brief_info,
                                                start_time,
                                                end_time,
                                                qr_code,
                                                service,
                                                address,
                                                address_en,
                                                closing_date,
                                                accounts_id,
                                                del_flag,
                                                status,
                                                create_time,
                                                update_time,
                                                description,
                                                create_by,
                                                update_by,
                                                content_brief_en_info,
                                                is_questions
                        </sql>


    <insert id="save" parameterType="com.cftech.workshop.meeting.model.Meeting" useGeneratedKeys="true"
            keyProperty="id">
        insert into wx_mp_meeting
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{number, jdbcType=VARCHAR},
        #{title, jdbcType=VARCHAR},
        #{titleEn, jdbcType=VARCHAR},
        #{cover, jdbcType=VARCHAR},
        #{wxCover, jdbcType=VARCHAR},
        #{contentBriefInfo, jdbcType=VARCHAR},
        #{startTime, jdbcType=DATE},
        #{endTime, jdbcType=DATE},
        #{qrCode, jdbcType=VARCHAR},
        #{service},
        #{address},
        #{address_en},
        #{closing_date},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT},
        #{contentBriefEnInfo, jdbcType=VARCHAR},
        #{isQuestions, jdbcType=VARCHAR}
        )
    </insert>

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

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

    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM wx_mp_meeting
        <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.meeting.model.Meeting">
        update wx_mp_meeting
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="number != null">
                number = #{number, jdbcType=VARCHAR},
            </if>
            <if test="title != null">
                title = #{title, jdbcType=VARCHAR},
            </if>
            <if test="titleEn != null">
                title_en = #{titleEn, jdbcType=VARCHAR},
            </if>
            <if test="cover != null">
                cover = #{cover, jdbcType=VARCHAR},
            </if>
            <if test="wxCover != null">
                wx_cover = #{wxCover, jdbcType=VARCHAR},
            </if>
            <if test="contentBriefInfo != null">
                content_brief_info = #{contentBriefInfo, jdbcType=VARCHAR},
            </if>
            <if test="startTime != null">
                start_time = #{startTime, jdbcType=DATE},
            </if>
            <if test="endTime != null">
                end_time = #{endTime, jdbcType=DATE},
            </if>
            <if test="qrCode != null">
                qr_code = #{qrCode, jdbcType=VARCHAR},
            </if>
            <if test="service != null">
                service = #{service},
            </if>
            <if test="closing_date!= null">
                closing_date = #{closing_date},
            </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="contentBriefEnInfo != null">
                content_brief_en_info = #{contentBriefEnInfo, jdbcType=VARCHAR},
            </if>
            <if test="address!=null">
                address = #{address},
            </if>
            <if test="address_en!=null">
                address_en = #{address_en},
            </if>
            <if test="isQuestions!=null">
                is_questions = #{isQuestions}
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

    <select id="fetchMeetingMobileMenu" parameterType="java.util.Map" resultType="java.util.Map">
        select agenda_count,speech_count,sample_count,takedrive_count,activity_count,paper_count,album_count,is_questions
        from wx_mp_meeting m
        where m.id = #{id}
    </select>

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

    <update id="synMobileMenu">
update wx_mp_meeting m set
agenda_count = (select count(1) from wx_mp_meeting_agenda agenda where agenda.workshop_id = m.id and agenda.del_flag=0),
speech_count = (select count(1) from wx_mp_meeting_speech speech where speech.workshop_id = m.id and speech.del_flag=0),
sample_count =(select count(1) from wx_mp_meeting_sample sample where sample.workshop_id = m.id and sample.del_flag=0),
takedrive_count = (select count(1) from wx_mp_meeting_takedrive takedrive where takedrive.meetingId = m.id and takedrive.del_flag=0),
activity_count = (select count(1) from wx_mp_meeting_activ activ where activ.workshop_id=m.id and activ.del_flag = 0 and activ.activ_type!=4),
paper_count = (select count(1) from wx_mp_meeting_activ activ where activ.workshop_id=m.id and activ.del_flag = 0 and activ.activ_type=4),
album_count = (select count(1) from wx_mp_meeting_album album where album.workshop_id=m.id and album.del_flag = 0)
where m.del_flag=0
    </update>
</mapper>