<?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.ym.mec.biz.dal.dao.SysSuggestionDao">

    <resultMap type="com.ym.mec.biz.dal.entity.SysSuggestion" id="SysSuggestion">
        <result column="id_" property="id"/>
        <result column="mobile_no_" property="mobileNo"/>
        <result column="title_" property="title"/>
        <result column="content_" property="content"/>
        <result column="url_" property="url"/>
        <result column="user_id_" property="userId"/>
        <result column="username_" property="username"/>
        <result column="create_time_" property="createTime"/>
        <result column="client_type_" property="clientType"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="tenant_id_" property="tenantId"/>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="SysSuggestion">
		SELECT * FROM sys_suggestion WHERE id_ = #{id}
	</select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="SysSuggestion">
		SELECT * FROM sys_suggestion where tenant_id_ = #{tenantId} ORDER BY id_
	</select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.SysSuggestion" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO sys_suggestion (mobile_no_,title_,content_,user_id_,create_time_,client_type_,type_,url_,tenant_id_)
        VALUES(#{mobileNo},#{title},#{content},#{userId},now(),#{clientType},#{type, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{url},#{tenantId})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.SysSuggestion">
        UPDATE sys_suggestion
        <set>
            <if test="url != null">
                url_ = #{url},
            </if>
            <if test="clientType != null">
                client_type_ = #{clientType},
            </if>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="title != null">
                title_ = #{title},
            </if>
            <if test="content != null">
                content_ = #{content},
            </if>
            <if test="mobileNo != null">
                mobile_no_ = #{mobileNo},
            </if>
            <if test="type != null">
                type_ = #{type, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
        </set>
        WHERE id_ = #{id} ans tenant_id_ = #{tenantId}
    </update>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
		DELETE FROM sys_suggestion WHERE id_ = #{id} 
	</delete>

    <sql id="queryCondition">
        <where>
        	ss.tenant_id_ = #{tenantId}
            <if test="type!=null">
                AND ss.type_ = #{type, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="clientType!=null">
                AND ss.client_type_ = #{clientType}
            </if>
            <if test="startTime!=null and startTime!=''">
                AND DATE_FORMAT(ss.create_time_,'%Y-%m-%d') &gt;= #{startTime}
            </if>
            <if test="endTime!=null and endTime!=''">
                AND DATE_FORMAT(ss.create_time_,'%Y-%m-%d') &lt;= #{endTime}
            </if>
            <if test="search!=null and search!=''">
                AND (su.real_name_ LIKE CONCAT('%', #{search}, '%') OR su.username_ LIKE CONCAT('%', #{search}, '%') OR ss.user_id_=#{search} OR su.phone_=#{search})
            </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="SysSuggestion" parameterType="map">
        SELECT ss.*,CASE WHEN ss.client_type_='STUDENT' THEN su.username_ ELSE su.real_name_ END username_ FROM sys_suggestion ss
        LEFT JOIN sys_user su ON su.id_ = ss.user_id_
        <include refid="queryCondition" />
        ORDER BY ss.id_ DESC
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(ss.id_) FROM sys_suggestion ss
        LEFT JOIN sys_user su ON su.id_ = ss.user_id_
        <include refid="queryCondition" />
	</select>
</mapper>
