<?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.SyMajorCategoryMapper">
    
    <resultMap type="SyMajorCategory" id="SyMajorCategoryResult">
        <result property="id"    column="id"    />
        <result property="majorCategoryCode"    column="major_category_code"    />
        <result property="majorCategoryName"    column="major_category_name"    />
        <result property="majorSubjectCode"    column="major_subject_code"    />
        <result property="majorSubjectName"    column="major_subject_name"    />
        <result property="majorCode"    column="major_code"    />
        <result property="majorName"    column="major_name"    />
        <result property="type"    column="type"    />
        <result property="level"    column="level"    />
        <result property="state"    column="state"    />
    </resultMap>

    <sql id="selectSyMajorCategoryVo">
        select id, major_category_code, major_category_name, major_subject_code, type, level from sy_major_category
    </sql>

    <select id="selectAllMajorList" parameterType="map" resultMap="SyMajorCategoryResult">
        SELECT ms.`subject_code` major_subject_code, ms.`subject_name` major_subject_name, mc.`major_category_code`, mc.`major_category_name`, mm.`major_code`, mm.`major_name`
          FROM `sy_major_subject` ms
          JOIN `sy_major_category` mc ON mc.`major_subject_code` = ms.`subject_code`
          JOIN `sy_major_major` mm ON mm.`major_category_code` = mc.`major_category_code`
    </select>
    <select id="selectSelectedMajorList" parameterType="map" resultMap="SyMajorCategoryResult">
        SELECT ms.`subject_code` major_subject_code, ms.`subject_name` major_subject_name, mc.`major_category_code`, mc.`major_category_name`, mm.`major_code`, mm.`major_name`, COUNT(*) `level`
        FROM `sy_test_select_category` sc
        JOIN `sy_major_category` mc ON mc.`major_category_code` = sc.`major_category_code`
        JOIN `sy_major_subject` ms ON ms.`subject_code` = mc.`major_subject_code`
        JOIN `sy_major_major` mm ON mm.`major_category_code` = mc.`major_category_code`
        WHERE sc.`examinee_id` IN <foreach item="id" collection="examineeIds" open="(" separator="," close=")">#{id}</foreach>
        GROUP BY ms.`subject_code`, ms.`subject_name`, mc.`major_category_code`, mc.`major_category_name`, mm.`major_code`, mm.`major_name`
    </select>


    <select id="selectSyMajorCategoryListByView" parameterType="map" resultMap="SyMajorCategoryResult">
        SELECT DISTINCT ms.`subject_code` major_subject_code, ms.`subject_name` major_subject_name, mc.`major_category_code`, mc.`major_category_name`
          FROM `sy_philosophy_view_major` m
          JOIN `sy_major_subject` ms ON ms.`subject_code` = m.`major_category_code`
          JOIN `sy_major_category` mc ON mc.`major_subject_code` = ms.`subject_code`
         WHERE m.`match_code` IN <foreach item="id" collection="matchCodes" open="(" separator="," close=")">#{id}</foreach>
    </select>
    <select id="selectSyMajorCategoryListByOccupation" parameterType="map" resultMap="SyMajorCategoryResult">
        SELECT DISTINCT ms.`subject_code` major_subject_code, ms.`subject_name` major_subject_name, mc.`major_category_code`, mc.`major_category_name`
          FROM `sy_occupation_interst_major` im
          JOIN `sy_major_category` mc ON mc.`major_category_code` = im.`major_category_code`
          JOIN `sy_major_subject` ms ON ms.`subject_code` = mc.`major_subject_code`
         WHERE im.`match_code` IN <foreach item="id" collection="matchCodes" open="(" separator="," close=")">#{id}</foreach>
    </select>
    <select id="selectSyMajorCategoryListByKnowledge" parameterType="map" resultMap="SyMajorCategoryResult">
      SELECT DISTINCT ms.`subject_code` major_subject_code, ms.`subject_name` major_subject_name, mc.`major_category_code`, mc.`major_category_name`
        FROM `sy_knowledge_interst_type` kt
        JOIN `sy_knowledge_interst_major` km ON kt.`code` = km.`match_code`
        JOIN `sy_major_category` mc ON mc.`major_category_code` = km.`major_category_code`
        JOIN `sy_major_subject` ms ON ms.`subject_code` = mc.`major_subject_code`
        WHERE kt.`id` IN <foreach item="id" collection="matchCodes" open="(" separator="," close=")">#{id}</foreach>
    </select>

    <select id="selectUserSyMajorCategory" parameterType="map" resultMap="SyMajorCategoryResult">
        SELECT mc.`major_category_code`, mc.`major_category_name` FROM `sy_test_select_category` sc
          JOIN `sy_major_category` mc ON sc.`major_category_code` = mc.`major_category_code`
         WHERE sc.`state` = 1 AND sc.`examinee_id` = #{examineeId}
    </select>

    <select id="selectUserSyMajorMajor" parameterType="map" resultMap="SyMajorCategoryResult">
        SELECT mm.code `major_code`, mm.name `major_name`
          FROM `sy_test_select_major` sm
          JOIN `sy_major` mm ON sm.`major_code` = mm.`code`
         WHERE sm.`state` = 1 AND sm.`customer_code` = #{customerCode}
    </select>

    <select id="selectSyMajorCategoryList" parameterType="SyMajorCategory" resultMap="SyMajorCategoryResult">
        <include refid="selectSyMajorCategoryVo"/>
        <where>  
            <if test="majorCategoryCode != null  and majorCategoryCode != ''"> and major_category_code = #{majorCategoryCode}</if>
            <if test="majorCategoryName != null  and majorCategoryName != ''"> and major_category_name like concat('%', #{majorCategoryName}, '%')</if>
            <if test="majorSubjectCode != null  and majorSubjectCode != ''"> and major_subject_code = #{majorSubjectCode}</if>
            <if test="type != null  and type != ''"> and type = #{type}</if>
            <if test="level != null "> and level = #{level}</if>
        </where>
    </select>
    
    <select id="selectSyMajorCategoryById" parameterType="Long" resultMap="SyMajorCategoryResult">
        <include refid="selectSyMajorCategoryVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertSyMajorCategory" parameterType="SyMajorCategory" useGeneratedKeys="true" keyProperty="id">
        insert into sy_major_category
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="majorCategoryCode != null">major_category_code,</if>
            <if test="majorCategoryName != null">major_category_name,</if>
            <if test="majorSubjectCode != null">major_subject_code,</if>
            <if test="type != null">type,</if>
            <if test="level != null">level,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="majorCategoryCode != null">#{majorCategoryCode},</if>
            <if test="majorCategoryName != null">#{majorCategoryName},</if>
            <if test="majorSubjectCode != null">#{majorSubjectCode},</if>
            <if test="type != null">#{type},</if>
            <if test="level != null">#{level},</if>
         </trim>
    </insert>

    <update id="updateSyMajorCategory" parameterType="SyMajorCategory">
        update sy_major_category
        <trim prefix="SET" suffixOverrides=",">
            <if test="majorCategoryCode != null">major_category_code = #{majorCategoryCode},</if>
            <if test="majorCategoryName != null">major_category_name = #{majorCategoryName},</if>
            <if test="majorSubjectCode != null">major_subject_code = #{majorSubjectCode},</if>
            <if test="type != null">type = #{type},</if>
            <if test="level != null">level = #{level},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSyMajorCategoryById" parameterType="Long">
        delete from sy_major_category where id = #{id}
    </delete>

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