<?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.keao.edu.user.dao.SubjectDao">

    <resultMap type="com.keao.edu.user.entity.Subject" id="Subject">
        <result column="id_" property="id"/>
        <result column="name_" property="name"/>
        <result column="code_" property="code"/>
        <result column="parent_subject_id_" property="parentSubjectId"/>
        <result column="parent_name_" property="parentName"/>
        <result column="img_" property="img"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="del_flag_" property="delFlag"/>
        <result column="tenant_id_" property="tenantId"/>
    </resultMap>

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

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

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.keao.edu.user.entity.Subject" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO subject (id_,name_,code_,parent_subject_id_,img_,create_time_,update_time_,tenant_id_)
        VALUES(#{id},#{name},#{code},#{parentSubjectId},#{img},now(),now(),#{tenantId})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.keao.edu.user.entity.Subject">
        UPDATE subject
        <set>
            <if test="delFlag != null">
                del_flag_ = #{delFlag},
            </if>
            <if test="parentSubjectId != null">
                parent_subject_id_ = #{parentSubjectId},
            </if>
            <if test="code != null">
                code_ = #{code},
            </if>
            <if test="img != null">
                img_ = #{img},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="name != null">
                name_ = #{name},
            </if>
            <if test="tenantId!=null">
                tenant_id_ = #{tenantId},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

    <!-- 根据主键删除一条记录 -->
    <update id="delete">
        UPDATE `subject` SET del_flag_ = 1 WHERE id_ = #{id} OR parent_subject_id_ = #{id}
	</update>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="Subject" parameterType="map">
        SELECT s.*,sp.name_ parent_name_ FROM subject s
        LEFT JOIN `subject` sp ON s.parent_subject_id_ = sp.id_
        <include refid="querySubPageSql"/>
        ORDER BY s.id_
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM subject s <include refid="querySubPageSql"/>
	</select>

    <select id="findByParentId" resultMap="Subject">
        SELECT * FROM subject s <include refid="querySubPageSql"/>
    </select>

    <select id="findBySubIds" resultType="string">
        SELECT name_ FROM `subject` WHERE del_flag_ = 0 AND FIND_IN_SET (id_,#{subIds})
    </select>

    <select id="queryNameByIds" resultType="java.util.Map">
        select id_ `key`,name_ `value` FROM `subject` s
        WHERE FIND_IN_SET(s.id_,#{subjectIds}) AND s.del_flag_ = 0
        AND tenant_id_ = #{tenantId}
    </select>

    <select id="findSubSubjects" resultMap="Subject">
        SELECT * FROM `subject`
        WHERE parent_subject_id_ != 0 AND del_flag_ = 0
        <if test="tenantId != null and tenantId != 0">
            AND tenant_id_ = #{tenantId}
        </if>
    </select>

    <sql id="querySubPageSql">
        <where>
                s.del_flag_ = 0
            <if test="tenantId != null">
                AND s.tenant_id_ = #{tenantId}
            </if>
            <if test="parentId != null">
                AND s.parent_subject_id_ = #{parentId}
            </if>
            <if test="parentId == null">
                AND s.parent_subject_id_ != 0
            </if>
        </where>
    </sql>

    <select id="findBySubjectByIdList" resultMap="Subject">
        SELECT * FROM `subject` WHERE del_flag_ = 0 AND FIND_IN_SET(id_,#{subjectIdList})
    </select>

    <select id="findNames" resultType="java.lang.String">
        SELECT GROUP_CONCAT(DISTINCT s.name_) FROM `subject` s WHERE FIND_IN_SET(s.id_,#{subjectIdList})
    </select>

    <select id="findBySubjectIds" resultMap="Subject">
      SELECT * FROM subject WHERE id_ IN
      <foreach collection="subjectIds" item="subjectId" separator="," open="(" close=")">
          #{subjectId}
      </foreach>
    </select>
    <select id="countExamSubject" resultType="java.lang.Integer">
        SELECT COUNT(0) FROM exam_subject es
        WHERE es.subject_id_ IN (SELECT s.id_ FROM `subject` s WHERE s.id_ = #{id});
    </select>
</mapper>
