<?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.screen.dao.ScreenMapper">

    <resultMap id="resultMap" type="com.cftech.workshop.screen.model.Screen">
        <id column="id" property="id"/>
        <result column="number" property="number"/>
        <result column="customiz_id" property="customiz_id"/>
        <result column="topic_id" property="topic_id"/>
        <result column="meeting_id" property="meetingId"/>
        <result column="question" property="question"/>
        <result column="open_id" property="open_id"/>
        <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="nickname" property="nickname"/>
    </resultMap>

    <resultMap id="resultMapDto" type="com.cftech.workshop.screen.model.ScreenDto">
        <id column="id" property="id"/>
        <result column="customiz" property="customiz"/>
        <result column="topic" property="topic"/>
        <result column="question" property="question"/>
        <result column="surname" property="surname"/>
        <result column="name" property="name"/>
        <result column="title" property="title"/>
        <result column="create_time" property="create_time"/>
        <result column="phone" property="phone"/>
        <result column="compan" property="compan"/>
        <result column="status" property="status"/>
        <result column="agenda_id" property="agenda_id"/>
        <result column="nickname" property="nickname"/>
        <result column="nicknames" property="nicknames"/>
    </resultMap>

    <resultMap id="resultScreenTopic" type="com.cftech.workshop.screen.model.ScreenTopic">
        <id column="id" property="id"/>
        <result column="agenda_title" property="agenda_title"/>
        <result column="agenda_title_en" property="agenda_title_en"/>
        <result column="customiz_name" property="customiz_name"/>
        <result column="customiz_name_en" property="customiz_name_en"/>
        <result column="customiz_id" property="customiz_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,
                                                customiz_id,
                                                topic_id,
                                                meeting_id,
                                                question,
                                                open_id,
                                                accounts_id,
                                                del_flag,
                                                status,
                                                create_time,
                                                update_time,
                                                description,
                                                create_by,
                                                update_by,
                                                nickname
                        </sql>


    <insert id="save" parameterType="com.cftech.workshop.screen.model.Screen" useGeneratedKeys="true"
            keyProperty="id">
        insert into wx_mp_meeting_screen
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{number, jdbcType=VARCHAR},
        #{customiz_id},
        #{topic_id},
        #{meetingId, jdbcType=BIGINT},
        #{question, jdbcType=VARCHAR},
        #{open_id, jdbcType=VARCHAR},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT},
        #{nickname}
        )
    </insert>

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

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1)  FROM
        wx_mp_meeting_screen s
        LEFT JOIN wx_mp_meeting_agenda_customiz c ON c.id = s.customiz_id AND c.del_flag = 0
        LEFT JOIN wx_mp_member m ON s.open_id = m.open_id
        LEFT JOIN wx_mp_meeting_agenda a ON a.id= s.topic_id AND a.del_flag = 0
        LEFT JOIN wx_mp_meeting t ON t.id = s.meeting_id AND t.del_flag =0
        <include refid="sqlWhere"/>

    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMapDto">
        SELECT
        s.id,
        c.id customiz_id,
        c.customiz_name customiz,
        a.id agenda_id,
        a.agenda_title topic,
        m.surname,
        m.`name`,
        t.title,
        s.create_time,
        s.question,
        m.phone,
        m.compan,
        s.`status`,
        f.nickname
        FROM
        wx_mp_meeting_screen s
        LEFT JOIN wx_mp_meeting_agenda_customiz c ON c.id = s.customiz_id
        AND c.del_flag = 0
        LEFT JOIN wx_mp_member m ON s.open_id = m.open_id
        LEFT JOIN wx_mp_meeting_agenda a ON a.id = s.topic_id
        AND a.del_flag = 0
        LEFT JOIN wx_mp_meeting t ON t.id = s.meeting_id
        AND t.del_flag = 0
        LEFT JOIN wx_mp_fanss f ON f.openid = s.open_id AND f.delflag = 0

        <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.screen.model.Screen">
        update wx_mp_meeting_screen
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="number != null">
                number = #{number, jdbcType=VARCHAR},
            </if>
            <if test="customiz_id != null">
                customiz_id = #{customiz_id},
            </if>
            <if test="topic_id != null">
                topic_id = #{topic_id},
            </if>
            <if test="meetingId != null">
                meeting_id = #{meetingId, jdbcType=BIGINT},
            </if>
            <if test="question != null">
                question = #{question, jdbcType=VARCHAR},
            </if>
            <if test="open_id != null">
                open_id = #{open_id, 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>
            <if test="nickname!=null">
                nickname = #{nickname},
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

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

    <select id="selectscreentopic" parameterType="java.util.Map" resultMap="resultScreenTopic">
        SELECT
        a.id,
        a.agenda_title,
        a.agenda_title_en,
        a.customiz_id,
        c.customiz_name,
        c.customiz_name_en,
        a.`status`
        FROM
	    wx_mp_meeting_agenda a
        LEFT JOIN wx_mp_meeting_agenda_customiz c ON c.id = a.customiz_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="countscreentopic" 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 c.id = a.customiz_id
        AND c.del_flag = 0
        <include refid="sqlWhere"/>
    </select>

    <select id="selectbyconds" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM wx_mp_meeting_screen
        <include refid="sqlWhere"/>
    </select>

    <select id="selectquestion" parameterType="java.util.Map" resultMap="resultMapDto">
        SELECT
        s.id,
        c.id customiz_id,
        a.id agenda_id,
        a.agenda_title topic,
        m.surname,
        m.`name`,
        t.title,
        s.create_time,
        s.question,
        m.phone,
        m.compan,
        s.`status`,
        f.nickname,
        s.nickname nicknames
        FROM
        wx_mp_meeting_screen s
        LEFT JOIN wx_mp_meeting_agenda_customiz c ON c.id = s.customiz_id
        AND c.del_flag = 0
        LEFT JOIN wx_mp_member m ON s.open_id = m.open_id
        LEFT JOIN wx_mp_meeting_agenda a ON a.id = s.topic_id
        AND a.del_flag = 0
        LEFT JOIN wx_mp_meeting t ON t.id = s.meeting_id
        AND t.del_flag = 0
        LEFT JOIN wx_mp_fanss f ON f.openid = s.open_id AND f.delflag = 0

        <include refid="sqlWhere"/>
    </select>
</mapper>