<?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.srs.comprehensive.mapper.SrsStuYearMapper">

    <resultMap type="SrsStuYear" id="SrsStuYearResult">
        <result property="id"    column="id"    />
        <result property="stuYearName"    column="stu_year_name"    />
        <result property="xndm"    column="xndm"    />
        <result property="moduleTags" column="module_tags"/>
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>
    <sql id="selectSrsStuYearVo">
        select id,xndm, stu_year_name,module_tags, start_time, end_time, del_flag,status, create_by, create_time, update_by, update_time from srs_stu_year
    </sql>

    <select id="selectSrsStuYearList" parameterType="SrsStuYear" resultMap="SrsStuYearResult">
        <include refid="selectSrsStuYearVo"/>
        <where>
            <if test="stuYearName != null  and stuYearName != ''"> and stu_year_name like concat('%', #{stuYearName}, '%')</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
        </where>
        order by stu_year_name desc
    </select>

    <select id="selectSrsStuYearLists" parameterType="SrsStuYear" resultMap="SrsStuYearResult">
        <include refid="selectSrsStuYearVo"/>
    </select>

    <select id="listEnableYear" resultType="com.srs.comprehensive.domain.SrsStuYear">
        select * from srs_stu_year where status = 1
    </select>

    <select id="selectStuYearIdName" resultType="com.srs.comprehensive.domain.Vo.StuYearIdName">
        select id,stu_year_name from srs_stu_year
    </select>

    <select id="selectSrsStuYearById" parameterType="Long" resultMap="SrsStuYearResult">
        <include refid="selectSrsStuYearVo"/>
        where id = #{id}
    </select>
    <select id="selectQiYongList" resultType="com.srs.comprehensive.domain.SrsStuYear">
        select id,stu_year_name from srs_stu_year where status in (1,3) order by stu_year_name desc
    </select>
    <!-- 根据标签查询学年 -->
    <!-- 根据标签查询学年（精确匹配） -->
    <select id="selectYearsByTag" parameterType="string" resultMap="SrsStuYearResult">
        <include refid="selectSrsStuYearVo"/>
        where module_tags = #{tag}
        or module_tags like concat(#{tag}, ',%')
        or module_tags like concat('%,', #{tag}, ',%')
        or module_tags like concat('%,', #{tag})
    </select>

    <!-- 获取包含指定标签的所有学年（用于标签移除） -->
    <select id="getYearsContainingTag" parameterType="string" resultMap="SrsStuYearResult">
        <include refid="selectSrsStuYearVo"/>
        where module_tags = #{tag}
        or module_tags like concat(#{tag}, ',%')
        or module_tags like concat('%,', #{tag}, ',%')
        or module_tags like concat('%,', #{tag})
    </select>

    <!-- 直接移除指定标签 -->
    <update id="removeTagDirectly" parameterType="string">
        UPDATE srs_stu_year
        SET module_tags = CASE
                              WHEN module_tags = #{tag} THEN NULL
                              WHEN module_tags LIKE concat(#{tag}, ',%') THEN REPLACE(module_tags, concat(#{tag}, ','), '')
                              WHEN module_tags LIKE concat('%,', #{tag}) THEN REPLACE(module_tags, concat(',', #{tag}), '')
                              WHEN module_tags LIKE concat('%,', #{tag}, ',%') THEN REPLACE(module_tags, concat(',', #{tag}, ','), ',')
                              ELSE module_tags
            END
        WHERE module_tags = #{tag}
           OR module_tags LIKE concat(#{tag}, ',%')
           OR module_tags LIKE concat('%,', #{tag})
           OR module_tags LIKE concat('%,', #{tag}, ',%')
    </update>

    <!-- 获取所有包含标签的学年 -->
    <select id="getYearsWithTags" resultMap="SrsStuYearResult">
        <include refid="selectSrsStuYearVo"/>
        where module_tags is not null and module_tags != ''
    </select>

    <insert id="insertSrsStuYear" parameterType="SrsStuYear">
        insert into srs_stu_year
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="xndm != null">xndm,</if>
            <if test="stuYearName != null">stu_year_name,</if>
            <if test="moduleTags != null ">module_tags,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="status != null">`status`,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="xndm != null">#{xndm},</if>
            <if test="stuYearName != null">#{stuYearName},</if>
            <if test="moduleTags != null">#{moduleTags},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="status != null">#{status},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateSrsStuYear" parameterType="SrsStuYear">
        update srs_stu_year
        <trim prefix="SET" suffixOverrides=",">
            <if test="xndm != null">xndm = #{xndm},</if>
            <if test="stuYearName != null">stu_year_name = #{stuYearName},</if>
            <if test="moduleTags != null">module_tags = #{moduleTags},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="status != null">`status` = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSrsStuYearById" parameterType="Long">
        delete from srs_stu_year where id = #{id}
    </delete>

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

</mapper>
