<?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.ie.mapper.AMarjorPlanMapper">
    
    <resultMap type="AMarjorPlan" id="AMarjorPlanResult">
        <result property="id"    column="id"    />
        <result property="year"    column="year"    />
        <result property="universityId"    column="universityId"    />
        <result property="universityName"    column="universityName"    />
        <result property="level"    column="level"    />
        <result property="majorGroup"    column="majorGroup"    />
        <result property="majorName"    column="majorName"    />
        <result property="majorDirection"    column="majorDirection"    />
        <result property="enrollCode"    column="enrollCode"    />
        <result property="majorEnrollCode"    column="majorEnrollCode"    />
        <result property="examineeType"    column="examineeType"    />
        <result property="planTotal"    column="planTotal"    />
        <result property="lengthOfSchooling"    column="lengthOfSchooling"    />
        <result property="score"    column="score"    />
        <result property="xuefei"    column="xuefei"    />
        <result property="culturalScore"    column="culturalScore"    />
        <result property="profScore"    column="profScore"    />
        <result property="schoolScore"    column="schoolScore"    />
        <result property="chinessScore"    column="chinessScore"    />
        <result property="mathScore"    column="mathScore"    />
        <result property="englishScore"    column="englishScore"    />
        <result property="custCond"    column="custCond"    />
        <result property="comment"    column="comment"    />
        <result property="company"    column="company"    />
        <result property="test"    column="test"    />
        <result property="enrollFormula"    column="enrollFormula"    />
    </resultMap>

    <sql id="selectAMarjorPlanVo">
        select id, year, universityId, universityName, level, majorGroup, majorName, majorDirection, enrollCode, majorEnrollCode, examineeType, planTotal, xuefei, lengthOfSchooling, score, culturalScore, profScore, schoolScore, chinessScore, mathScore, englishScore, custCond, comment, company, test,enrollFormula from a_marjor_plan
    </sql>


    <select id="selectMajorForUniversity" parameterType="Map" resultMap="AMarjorPlanResult">
        SELECT MIN(mp.id) id, mp.`majorGroup`, mp.`majorName`, COUNT(DISTINCT sl.`student_id`) planTotal, COUNT(DISTINCT ts.`id`) xuefei
          FROM `dz_teacher_class` tc JOIN `learn_student` sl ON tc.`class_id` = sl.`class_id`
          LEFT JOIN `learn_test_student` ts ON ts.`batch_id` = #{batchId} and ts.build_type = #{buildType} and ts.subject_id = #{subjectId}
              and ts.`student_id` = sl.`student_id` and ts.class_id = sl.class_id
          JOIN `a_marjor_plan` mp ON sl.`major_plan_id` = mp.`id`
          JOIN `sys_user` u ON sl.`student_id` = u.`user_id`
         <where>tc.teacher_id = #{teacherId} and sl.`university_id` = #{universityId} and mp.year = #{year}
             <if test="examType != null  and examType != ''">  AND u.`exam_type` = #{examType}</if>
         </where>
         GROUP BY mp.`majorGroup`, mp.`majorName`
         ORDER BY mp.`majorGroup`, mp.`majorName`
    </select>

    <select id="selectListByRuleCond" parameterType="Map" resultMap="AMarjorPlanResult">
      SELECT m1.code majorCode, p.id, p.year, p.universityId, p.universityName, p.level, majorGroup, majorName, majorDirection, enrollCode, majorEnrollCode, examineeType, planTotal, xuefei, lengthOfSchooling, score, culturalScore, profScore, schoolScore, chinessScore, mathScore, englishScore, custCond, comment, company
        FROM `a_marjor_plan` p
        JOIN `b_busi_wish_universities` u ON p.`universityId` = u.`id`
        LEFT JOIN `sy_major` m1 ON m1.`name` = p.`majorName` and m1.`exam_type` = p.examineeType
        LEFT JOIN `sy_major` m2 ON m1.`parent_code` = m2.`code` and m2.`exam_type` = p.examineeType
        LEFT JOIN `sy_major` m3 ON m2.`parent_code` = m3.`code` and m3.`exam_type` = p.examineeType
        <where>
            <if test="examineeType != null and examineeType != '' "> and (p.`examineeType` IS NULL OR p.`examineeType` = '' OR p.`examineeType` = #{examineeType})</if>
            <if test="year != null "> and p.year = #{year}</if>
            <if test="universityCode != null and universityCode != ''"> and u.`code` = #{universityCode}</if>
            <if test="majorEnrollCodes != null"> AND p.`id` in <foreach item="o" collection="majorEnrollCodes" open="(" separator="," close=")">#{o}</foreach></if>
            <choose>
                <when test="majorCodes != null"> AND m1.`code` in <foreach item="o" collection="majorCodes" open="(" separator="," close=")">#{o}</foreach></when>
                <when test="majorTypes != null"> AND m2.`code` in <foreach item="o" collection="majorTypes" open="(" separator="," close=")">#{o}</foreach></when>
                <when test="majorCategory != null and majorCategory != ''"> AND m3.`code` = #{majorCategory} </when>
            </choose>
            <if test="universityName != null and universityName != ''"> AND u.`name` LIKE CONCAT('%', #{universityName}, '%')</if>
            <if test="universityLevel != null"> AND u.`level` in <foreach item="o" collection="universityLevel" open="(" separator="," close=")">#{o}</foreach></if>
            <if test="universityType != null"> AND u.`type` in <foreach item="o" collection="universityType" open="(" separator="," close=")">#{o}</foreach></if>
            <if test="universityNatureType != null"> AND u.`natureTypeCN` in <foreach item="o" collection="universityNatureType" open="(" separator="," close=")">#{o}</foreach></if>
            <if test="universityLocation != null"> AND u.`location` in <foreach item="o" collection="universityLocation" open="(" separator="," close=")">#{o}</foreach></if>
        </where>
        order by p.universityId,majorName, majorDirection
    </select>

    <select id="selectAMarjorPlanList" parameterType="AMarjorPlan" resultMap="AMarjorPlanResult">
        <include refid="selectAMarjorPlanVo"/>
        <where>  
            <if test="year != null "> and year = #{year}</if>
            <if test="universityId != null "> and universityId = #{universityId}</if>
            <if test="universityName != null  and universityName != ''"> and universityName like concat('%', #{universityName}, '%')</if>
            <if test="level != null  and level != ''"> and level = #{level}</if>
            <if test="majorGroup != null  and majorGroup != ''"> and majorGroup = #{majorGroup}</if>
            <if test="majorName != null  and majorName != ''"> and majorName like concat('%', #{majorName}, '%')</if>
            <if test="majorDirection != null  and majorDirection != ''"> and majorDirection = #{majorDirection}</if>
            <if test="enrollCode != null  and enrollCode != ''"> and enrollCode = #{enrollCode}</if>
            <if test="majorEnrollCode != null  and majorEnrollCode != ''"> and majorEnrollCode = #{majorEnrollCode}</if>
            <if test="examineeType != null  and examineeType != ''"> and examineeType = #{examineeType}</if>
            <if test="planTotal != null "> and planTotal = #{planTotal}</if>
            <if test="lengthOfSchooling != null "> and lengthOfSchooling = #{lengthOfSchooling}</if>
            <if test="score != null "> and score = #{score}</if>
            <if test="culturalScore != null "> and culturalScore = #{culturalScore}</if>
            <if test="profScore != null "> and profScore = #{profScore}</if>
            <if test="schoolScore != null "> and schoolScore = #{schoolScore}</if>
            <if test="chinessScore != null "> and chinessScore = #{chinessScore}</if>
            <if test="mathScore != null "> and mathScore = #{mathScore}</if>
            <if test="englishScore != null "> and englishScore = #{englishScore}</if>
            <if test="custCond != null  and custCond != ''"> and custCond = #{custCond}</if>
            <if test="comment != null  and comment != ''"> and comment = #{comment}</if>
            <if test="company != null  and company != ''"> and company = #{company}</if>
        </where>
    </select>
    
    <select id="selectAMarjorPlanById" parameterType="Long" resultMap="AMarjorPlanResult">
        <include refid="selectAMarjorPlanVo"/>
        where id = #{id}
    </select>

    <select id="selectAMarjorPlanByIds" parameterType="Long" resultMap="AMarjorPlanResult">
        <include refid="selectAMarjorPlanVo"/>
        where id in <foreach item="id" collection="array" open="(" separator="," close=")">#{id}</foreach>
    </select>

    <select id="selectMajorCodesByIds" parameterType="String" resultMap="AMarjorPlanResult"> <!--  and m1.`exam_type` = p.examineeType -->
        SELECT distinct p.majorGroup majorGroup, m1.code majorCode
        FROM `a_marjor_plan` p
        JOIN `sy_major` m1 ON m1.`name` = p.`majorName`
        where p.year = #{year} and p.id in <foreach item="id" collection="ids" open="(" separator="," close=")">#{id}</foreach>
    </select>
        
    <insert id="insertAMarjorPlan" parameterType="AMarjorPlan" useGeneratedKeys="true" keyProperty="id">
        insert into a_marjor_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="year != null">year,</if>
            <if test="universityId != null">universityId,</if>
            <if test="universityName != null">universityName,</if>
            <if test="level != null">level,</if>
            <if test="majorGroup != null">majorGroup,</if>
            <if test="majorName != null">majorName,</if>
            <if test="majorDirection != null">majorDirection,</if>
            <if test="enrollCode != null">enrollCode,</if>
            <if test="majorEnrollCode != null">majorEnrollCode,</if>
            <if test="examineeType != null">examineeType,</if>
            <if test="planTotal != null">planTotal,</if>
            <if test="xuefei != null">xuefei,</if>
            <if test="lengthOfSchooling != null">lengthOfSchooling,</if>
            <if test="score != null">score,</if>
            <if test="culturalScore != null">culturalScore,</if>
            <if test="profScore != null">profScore,</if>
            <if test="schoolScore != null">schoolScore,</if>
            <if test="chinessScore != null">chinessScore,</if>
            <if test="mathScore != null">mathScore,</if>
            <if test="englishScore != null">englishScore,</if>
            <if test="custCond != null">custCond,</if>
            <if test="comment != null">comment,</if>
            <if test="company != null">company,</if>
            <if test="test != null">test,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="year != null">#{year},</if>
            <if test="universityId != null">#{universityId},</if>
            <if test="universityName != null">#{universityName},</if>
            <if test="level != null">#{level},</if>
            <if test="majorGroup != null">#{majorGroup},</if>
            <if test="majorName != null">#{majorName},</if>
            <if test="majorDirection != null">#{majorDirection},</if>
            <if test="enrollCode != null">#{enrollCode},</if>
            <if test="majorEnrollCode != null">#{majorEnrollCode},</if>
            <if test="examineeType != null">#{examineeType},</if>
            <if test="planTotal != null">#{planTotal},</if>
            <if test="xuefei != null">#{xuefei},</if>
            <if test="lengthOfSchooling != null">#{lengthOfSchooling},</if>
            <if test="score != null">#{score},</if>
            <if test="culturalScore != null">#{culturalScore},</if>
            <if test="profScore != null">#{profScore},</if>
            <if test="schoolScore != null">#{schoolScore},</if>
            <if test="chinessScore != null">#{chinessScore},</if>
            <if test="mathScore != null">#{mathScore},</if>
            <if test="englishScore != null">#{englishScore},</if>
            <if test="custCond != null">#{custCond},</if>
            <if test="comment != null">#{comment},</if>
            <if test="company != null">#{company},</if>
            <if test="test != null">#{test},</if>
         </trim>
    </insert>

    <update id="updateAMarjorPlan" parameterType="AMarjorPlan">
        update a_marjor_plan
        <trim prefix="SET" suffixOverrides=",">
            <if test="year != null">year = #{year},</if>
            <if test="universityId != null">universityId = #{universityId},</if>
            <if test="universityName != null">universityName = #{universityName},</if>
            <if test="level != null">level = #{level},</if>
            <if test="majorGroup != null">majorGroup = #{majorGroup},</if>
            <if test="majorName != null">majorName = #{majorName},</if>
            <if test="majorDirection != null">majorDirection = #{majorDirection},</if>
            <if test="enrollCode != null">enrollCode = #{enrollCode},</if>
            <if test="majorEnrollCode != null">majorEnrollCode = #{majorEnrollCode},</if>
            <if test="examineeType != null">examineeType = #{examineeType},</if>
            <if test="planTotal != null">planTotal = #{planTotal},</if>
            <if test="xuefei != null">xuefei = #{xuefei},</if>
            <if test="lengthOfSchooling != null">lengthOfSchooling = #{lengthOfSchooling},</if>
            <if test="score != null">score = #{score},</if>
            <if test="culturalScore != null">culturalScore = #{culturalScore},</if>
            <if test="profScore != null">profScore = #{profScore},</if>
            <if test="schoolScore != null">schoolScore = #{schoolScore},</if>
            <if test="chinessScore != null">chinessScore = #{chinessScore},</if>
            <if test="mathScore != null">mathScore = #{mathScore},</if>
            <if test="englishScore != null">englishScore = #{englishScore},</if>
            <if test="custCond != null">custCond = #{custCond},</if>
            <if test="comment != null">comment = #{comment},</if>
            <if test="company != null">company = #{company},</if>
            <if test="test != null">test = #{test},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAMarjorPlanById" parameterType="Long">
        delete from a_marjor_plan where id = #{id}
    </delete>

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