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

    <resultMap id="resultMap" type="com.cftech.workshop.workshop.model.Workshop">
        <id column="id" property="id"/>
        <result column="number" property="number"/>
        <result column="surname" property="surname"/>
        <result column="first_name" property="first_name"/>
        <result column="gender" property="gender"/>
        <result column="organization" property="organization"/>
        <result column="email" property="email"/>
        <result column="phone" property="phone"/>
        <result column="position" property="position"/>
        <result column="wsid" property="wsid"/>
        <result column="invitcode" property="invitcode"/>
        <result column="member_uid" property="member_uid"/>
        <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="peopletype" property="peopletype"/>
        <result column="name_en" property="name_en"/>
        <result column="organization_en" property="organization_en"/>
        <result column="sort" property="sort"/>
        <result column="table_number" property="table_number"/>
        <result column="factory_number" property="factory_number"/>
        <result column="drive_number" property="drive_number"/>
        <result column="del_flag" property="del_flag" />
        <result column="accounts_id" property="accounts_id" />
    </resultMap>

    <resultMap id="resultMapDto" type="com.cftech.workshop.workshop.model.WorkshopDto">
        <id column="id" property="id"/>
        <result column="first_name" property="first_name"/>
        <result column="gender" property="gender"/>
        <result column="organization" property="organization"/>
        <result column="email" property="email"/>
        <result column="position" property="position"/>
        <result column="phone" property="phone"/>
        <result column="invitcode" property="invitcode"/>
        <result column="mname" property="mname"/>
        <result column="mphone" property="mphone"/>
        <result column="morganization" property="morganization"/>
        <result column="surname" property="surname"/>
        <result column="msurname" property="msurname"/>
        <result column="create_time" property="create_time"/>
        <result column="wsid" property="wsid"/>
        <result column="status" property="status"/>
        <result column="peopletype" property="peopletype"/>
        <result column="member_uid" property="member_uid"/>
        <result column="name_en" property="name_en"/>
        <result column="organization_en" property="organization_en"/>
        <result column="imageurl" property="imageurl"/>
        <result column="sort" property="sort"/>
        <result column="table_number" property="table_number"/>
        <result column="factory_number" property="factory_number"/>
        <result column="drive_number" property="drive_number"/>
        <result column="open_id" property="open_id"/>
        <result column="del_flag" property="del_flag" />
        <result column="accounts_id" property="accounts_id"/>
        <result column="title" property="title"/>
    </resultMap>

    <resultMap id="resultMapRSVP" type="com.cftech.workshop.workshop.model.WorkshopRSVP">
        <id column="id" property="id"/>
        <result column="start_time" property="start_time"/>
        <result column="end_time" property="end_time"/>
        <result column="service" property="service"/>
        <result column="surname" property="surname"/>
        <result column="first_name" property="first_name"/>
        <result column="gender" property="gender"/>
        <result column="organization" property="organization"/>
        <result column="email" property="email"/>
        <result column="position" property="position"/>
        <result column="phone" property="phone"/>
        <result column="invitcode" property="invitcode"/>
        <result column="create_time" property="create_time"/>
        <result column="status" property="status"/>
        <result column="wsid" property="wsid"/>
        <result column="msurname" property="msurname"/>
        <result column="mname" property="mname"/>
        <result column="mphone" property="mphone"/>
        <result column="morganization" property="morganization"/>
        <result column="detailsinfo" property="detailsinfo"/>
        <result column="sort" property="sort"/>
        <result column="del_flag" property="del_flag"/>
    </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,
                                                first_name,
                                                surname,
                                                gender,
                                                organization,
                                                email,
                                                phone,
                                                position,
                                                peopletype,
                                                wsid,
                                                invitcode,
                                                member_uid,
                                                accounts_id,
                                                del_flag,
                                                status,
                                                create_time,
                                                update_time,
                                                description,
                                                create_by,
                                                update_by,
                                                name_en,
                                                organization_en,
                                                sort,
                                                table_number,
                                                factory_number,
                                                drive_number
                        </sql>


    <insert id="save" parameterType="com.cftech.workshop.workshop.model.Workshop" useGeneratedKeys="true"
            keyProperty="id">
        insert into wx_mp_workshop
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{number, jdbcType=VARCHAR},
        #{first_name, jdbcType=VARCHAR},
        #{surname},
        #{gender, jdbcType=VARCHAR},
        #{organization, jdbcType=VARCHAR},
        #{email, jdbcType=VARCHAR},
        #{phone, jdbcType=VARCHAR},
        #{position, jdbcType=VARCHAR},
        #{peopletype},
        #{wsid, jdbcType=BIGINT},
        #{invitcode, jdbcType=VARCHAR},
        #{member_uid, jdbcType=VARCHAR},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT},
        #{name_en},
        #{organization_en},
        #{sort},
        #{table_number},
        #{factory_number},
        #{drive_number}
        )
    </insert>

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

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM wx_mp_workshop w
        LEFT JOIN wx_mp_member m ON w.member_uid = m.member_uid
        <include refid="sqlWhere"/>
    </select>

    <select id="selectworkshop" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM wx_mp_workshop
        <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="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM
        wx_mp_workshop w
        <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="searchmembershop" parameterType="java.util.Map" resultMap="resultMapDto">
        SELECT
        w.sort,
        w.id,
        w.surname,
        w.first_name,
        w.gender,
        w.organization,
        w.organization_en,
        w.name_en,
        w.table_number,
        w.factory_number,
        w.drive_number,
        w.email,
        w.position,
        w.phone,
        w.invitcode,
        w.create_time,
        w. STATUS,
        m.surname msurname,
        m.`name` mname,
        m.phone mphone,
        m.compan morganization,
        w.wsid,
        w.peopletype,
        m.member_uid,
        m.imageurl,
        m.open_id,
        w.del_flag
        FROM
        wx_mp_workshop w
        LEFT JOIN wx_mp_member m ON w.member_uid = m.member_uid
        <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="delworkshop" parameterType="java.util.Map" resultMap="resultMapDto">
        SELECT
        *
        FROM
        (
        SELECT
        t.title,
        w.sort,
        w.del_flag,
        w.id,
        w.surname,
        w.first_name,
        w.gender,
        w.organization,
        w.organization_en,
        w.name_en,
        w.table_number,
        w.factory_number,
        w.drive_number,
        w.email,
        w.position,
        w.phone,
        w.invitcode,
        w.create_time,
        w. STATUS,
        m.surname msurname,
        m.`name` mname,
        m.phone mphone,
        m.compan morganization,
        w.wsid,
        w.peopletype,
        m.member_uid,
        m.imageurl,
        m.open_id,
        w.accounts_id
        FROM
        wx_mp_workshop w
        LEFT JOIN wx_mp_member m ON w.member_uid = m.member_uid
        LEFT JOIN wx_mp_meeting t ON w.wsid = t.id AND t.del_flag = 0
        ORDER BY
        del_flag ASC,
        create_time DESC
        ) s
        <include refid="sqlWhere"/>
        GROUP BY
        sort,s.wsid
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>

    </select>

    <select id="delnum" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT
	count(1)
FROM
	(
		SELECT
			*
		FROM
			(
				SELECT
					w.sort,
					w.del_flag,
					w.id,
					w.surname,
					w.first_name,
					w.gender,
					w.organization,
					w.organization_en,
					w.name_en,
					w.table_number,
					w.factory_number,
					w.drive_number,
					w.email,
					w.position,
					w.phone,
					w.invitcode,
					w.create_time,
					w. STATUS,
					m.surname msurname,
					m.`name` mname,
					m.phone mphone,
					m.compan morganization,
					w.wsid,
					w.peopletype,
					m.member_uid,
					m.imageurl,
					m.open_id,
                    w.accounts_id
				FROM
					wx_mp_workshop w
				LEFT JOIN wx_mp_member m ON w.member_uid = m.member_uid
				ORDER BY
					del_flag ASC, create_time DESC
			) s
        <include refid="sqlWhere"/>
		GROUP BY
			sort
	) ss
    </select>


    <update id="update" parameterType="com.cftech.workshop.workshop.model.Workshop">
        update wx_mp_workshop
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="number != null">
                number = #{number, jdbcType=VARCHAR},
            </if>
            <if test="first_name != null">
                first_name = #{first_name, jdbcType=VARCHAR},
            </if>
            <if test="surname != null">
                surname = #{surname},
            </if>
            <if test="gender != null">
                gender = #{gender, jdbcType=VARCHAR},
            </if>
            <if test="organization != null">
                organization = #{organization, jdbcType=VARCHAR},
            </if>
            <if test="email != null">
                email = #{email, jdbcType=VARCHAR},
            </if>
            <if test="phone != null">
                phone = #{phone, jdbcType=VARCHAR},
            </if>
            <if test="position != null">
                position = #{position, jdbcType=VARCHAR},
            </if>
            <if test="wsid != null">
                wsid = #{wsid, jdbcType=BIGINT},
            </if>
            <if test="invitcode != null">
                invitcode = #{invitcode, jdbcType=VARCHAR},
            </if>
            <if test="member_uid != null">
                member_uid = #{member_uid, 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="name_en!=null">
                name_en = #{name_en},
            </if>
            <if test="organization_en != null">
                organization_en = #{organization_en},
            </if>
            <if test="table_number!=null">
                table_number = #{table_number},
            </if>
            <if test="factory_number!=null">
                factory_number = #{factory_number},
            </if>
            <if test="drive_number!=null">
                drive_number = #{drive_number},
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

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

    <select id="exportRSVP" parameterType="java.util.Map" resultMap="resultMapRSVP">
        SELECT
        *
        FROM
        (
        SELECT
        w.del_flag,
        w.sort,
        meet.start_time,
        meet.end_time,
        meet.service,
        m.member_uid,
        w.id,
        w.surname,
        w.first_name,
        w.gender,
        w.organization,
        w.email,
        w.position,
        w.phone,
        w.invitcode,
        w.create_time,
        w. STATUS,
        w.wsid,
        m.surname msurname,
        m.`name` mname,
        m.phone mphone,
        m.compan morganization,
        d.detailsinfo
        FROM
        wx_mp_workshop w
        LEFT JOIN wx_mp_meeting meet ON meet.id = w.wsid
        LEFT JOIN wx_mp_member m ON w.member_uid = m.member_uid
        LEFT JOIN wx_mp_meeting_details d ON d.memberUid = m.member_uid and w.status =1
        AND d.meeting_id = w.wsid
        ORDER BY
        del_flag ASC, create_time DESC
        ) s
        <include refid="sqlWhere"/>
        GROUP BY sort
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>


    <select id="getTemplateWorkShopMessage" parameterType="java.util.Map"
            resultType="com.cftech.workshop.workshop.model.WorkShopMessageDto">
        SELECT
        m.surname,
        m.name firstName,
        w.gender,
        w.organization,
        w.wsid,
        d.detailsinfo,
        m.open_id openId,
        m.phone
        FROM
        wx_mp_workshop w
        LEFT JOIN wx_mp_meeting_details d ON w.member_uid = d.memberUid AND d.meeting_id = w.wsid
        left join wx_mp_member m on m.member_uid=w.member_uid
        <include refid="sqlWhere"/>
    </select>

</mapper>