<?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.api.manage.dao.butlerService.SysGambitDao">
    <select id="list" parameterType="com.api.model.butlerService.SearchGambit" resultType="com.api.vo.butlerService.VoGambit">
        select a.*
        from (select sg.*,(
        case sg.user_type
        when 1 then (select ur.name from user_resident as ur where ur.id = sg.create_id)
        when 2 then (select udp.name from user_decoration_personnel as udp where udp.id = sg.create_id)
        when 3 then (select su.actual_name from sys_user as su where su.id = sg.create_id) 	end
        ) as create_name
        from sys_gambit as sg) as a
        <where>
            <if test="title != null and title != ''">
                and a.title like concat('%',#{title},'%')
            </if>
            <if test="createName != null and createName != ''">
                and a.create_name like concat('%',#{createName},'%')
            </if>
            <if test="status != null">
                and a.status = #{status}
            </if>
            <if test="summary != null and summary != ''">
                and a.summary like concat('%',#{summary},'%')
            </if>
            <if test="creatBeginDate != null">
                and a.create_date <![CDATA[>]]> #{creatBeginDate}
            </if>
            <if test="creatEndDate != null">
                and a.end_date <![CDATA[<]]> #{creatEndDate}
            </if>
            and is_delete = 1
        </where>
    </select>

    <select id="countGambitNum" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select count(distinct a.create_id) as count_gambit_num
        from (
        select create_id from sys_gambit_theme where gambit_id = #{id}
        union all
        select create_id from sys_gambit_theme_comment where gambit_id = #{id}
        union all
        select create_id from sys_gambit_theme_like where gambit_id = #{id}) as a
    </select>

    <insert id="insert" parameterType="com.api.model.butlerService.SysGambit">
        insert into sys_gambit(
            title,
            summary,
            content,
            begin_date,
            end_date,
            status,
            is_public,
            is_rating,
            user_type,
            create_id,
            create_date,
            is_delete
        ) values (
            #{title},
            #{summary},
            #{content},
            #{beginDate},
            #{endDate},
            #{status},
            #{isPublic},
            #{isRating},
            #{userType},
            #{createId},
            #{createDate},
            #{isDelete}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>

    <select id="findById" parameterType="java.lang.Integer" resultType="com.api.vo.butlerService.VoFindByIdGambit">
        select * from sys_gambit where id = #{id} and is_delete = 1
    </select>

    <update id="update" parameterType="com.api.model.butlerService.SysGambit">
        update sys_gambit
        <set>
            <if test="title != null and title != ''">
                title = #{title},
            </if>
            <if test="summary != null and summary != ''">
                summary = #{summary},
            </if>
            <if test="content != null and content != ''">
                content = #{content},
            </if>
            <if test="beginDate != null">
                begin_date = #{beginDate},
            </if>
            <if test="endDate != null">
                end_date = #{endDate},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="isPublic != null">
                is_public = #{isPublic},
            </if>
            <if test="isRating != null">
                is_rating = #{isRating},
            </if>
            modify_id = #{modifyId},
            modify_date = #{modifyDate}
        </set>
        where id = #{id}
    </update>

    <update id="falseDelete" parameterType="java.lang.Integer">
        update sys_gambit set is_delete = 0 where id = #{id}
    </update>

    <update id="enableGambit" parameterType="java.lang.Integer">
        update sys_gambit set status = 1 where id = #{id}
    </update>

    <update id="disableGambit" parameterType="java.lang.Integer">
        update sys_gambit set status = 2 where id = #{id}
    </update>
</mapper>