<?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.ruoyi.sy.mapper.SyMajorMapper">

    <resultMap type="SyMajor" id="SyMajorResult">
        <result property="id"    column="id"    />
        <result property="code"    column="code"    />
        <result property="parentCode"    column="parent_code"    />
        <result property="name"    column="name"    />
        <result property="examType"    column="exam_type"    />
        <result property="type"    column="type"    />
        <result property="level"    column="level"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="childCount"    column="child_count"    />
        <result property="grandchildCount"    column="grandchild_count"    />
        <result property="learnYearArab"    column="learn_year_arab"    />
    </resultMap>

    <sql id="selectSyMajorVo">
        select id, code, parent_code, name, exam_type, type, level, ancestors, child_count, grandchild_count, learn_year_arab from sy_major
    </sql>

    <select id="selectSyMajorList" parameterType="SyMajor" resultMap="SyMajorResult">
        <include refid="selectSyMajorVo"/>
        <where>
            <if test="code != null  and code != ''"> and code = #{code}</if>
            <if test="parentCode != null  and parentCode != ''"> and parent_code = #{parentCode}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="examType != null  and examType != ''"> and exam_type = #{examType}</if>
            <if test="type != null  and type != ''"> and type = #{type}</if>
            <if test="level != null "> and level &lt;= #{level}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="childCount != null "> and child_count = #{childCount}</if>
            <if test="grandchildCount != null "> and grandchild_count = #{grandchildCount}</if>
        </where>
        order by code
    </select>



    <select id="selectSyMajorAndChildrenByCode" parameterType="SyMajor" resultMap="SyMajorResult">
        <include refid="selectSyMajorVo"/>
        <where>
            <if test="level != null"> and level &gt; #{level}</if>
            <if test="examType != null"> and exam_type = #{examType}</if>
            <if test="code != null and code != ''">
                and ( code=#{code} or find_in_set(#{code}, ancestors) )
            </if>
        </where>
    </select>

    <select id="selectSyMajorById" parameterType="Long" resultMap="SyMajorResult">
        <include refid="selectSyMajorVo"/>
        where id = #{id}
    </select>

    <select id="selectSyMajorByLevel" resultMap="SyMajorResult">
        <include refid="selectSyMajorVo"/>
        where level = #{level} and exam_type = #{examType}
    </select>

    <select id="selectSyMajorByCode" resultMap="SyMajorResult">
        <include refid="selectSyMajorVo"/>
        where exam_type = #{examType} and code = #{code}
    </select>

    <insert id="insertSyMajor" parameterType="SyMajor" useGeneratedKeys="true" keyProperty="id">
        insert into sy_major
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null">code,</if>
            <if test="parentCode != null">parent_code,</if>
            <if test="name != null">name,</if>
            <if test="examType != null">exam_type,</if>
            <if test="type != null">type,</if>
            <if test="level != null">level,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="childCount != null">child_count,</if>
            <if test="grandchildCount != null">grandchild_count,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null">#{code},</if>
            <if test="parentCode != null">#{parentCode},</if>
            <if test="name != null">#{name},</if>
            <if test="examType != null">#{examType},</if>
            <if test="type != null">#{type},</if>
            <if test="level != null">#{level},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="childCount != null">#{childCount},</if>
            <if test="grandchildCount != null">#{grandchildCount},</if>
         </trim>
    </insert>

    <update id="updateSyMajor" parameterType="SyMajor">
        update sy_major
        <trim prefix="SET" suffixOverrides=",">
            <if test="code != null">code = #{code},</if>
            <if test="parentCode != null">parent_code = #{parentCode},</if>
            <if test="name != null">name = #{name},</if>
            <if test="examType != null">exam_type = #{examType},</if>
            <if test="type != null">type = #{type},</if>
            <if test="level != null">level = #{level},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="childCount != null">child_count = #{childCount},</if>
            <if test="grandchildCount != null">grandchild_count = #{grandchildCount},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSyMajorById" parameterType="Long">
        delete from sy_major where id = #{id}
    </delete>

    <delete id="deleteSyMajorByIds" parameterType="String">
        delete from sy_major where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectSyMajorByPlan" resultMap="SyMajorResult">
        SELECT mp.id, m.`type`, m.`code`, CONCAT(m.`name`, IF(mp.`majorDirection` = '', '', CONCAT('(', mp.`majorDirection`, ')'))) `name`, CONCAT(m2.`name`, '&gt;', m1.`name`) `ancestors`
        FROM `a_marjor_plan` mp
                 JOIN `sy_major` m ON mp.`majorName` = m.`name` AND mp.`level` = m.`type` AND IF(mp.`examineeType` = '职高对口升学', '职高对口升学', '单招（应届普高）') = m.`exam_type`
                 LEFT JOIN `sy_major` m1 ON m1.`code` = m.`parent_code` AND m1.`exam_type` = m.`exam_type` AND m1.type = m.type AND m1.level + 1 = m.level
                 LEFT JOIN `sy_major` m2 ON m2.`code` = m1.`parent_code` AND m1.`exam_type` = m.`exam_type` AND m2.type = m1.type AND m2.level + 1 = m1.level
        WHERE mp.`year` = #{year} AND mp.`examineeType` = #{examType} AND mp.`universityId` = #{universityId}  AND mp.`location` = #{location}
        ORDER BY m.`type`, m.`code`
    </select>

    <select id="selectMajorsByPlan" resultMap="SyMajorResult">
        SELECT DISTINCT majorName name from a_marjor_plan where year=(SELECT max(year) from a_marjor_plan where LOCATION=#{location})
    </select>

</mapper>
