<?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="cn.wisenergy.mapper.VolunteerMapper">
    <resultMap id="userMap" type="cn.wisenergy.model.app.Volunteer">
        <id column="id" property="id"/>
        <result column="type" property="type"/>
        <result column="scheme_id" property="schemeId"/>
        <result column="major_name" property="majorName"/>
        <result column="academy" property="academy"/>
        <result column="course_demand" property="courseDemand"/>
        <result column="nature" property="nature"/>
        <result column="year_limit" property="yearLimit"/>
        <result column="plan_num" property="planNum"/>
        <result column="cast_archives_num" property="castArchivesNum"/>
        <result column="launch_num" property="launchNum"/>
        <result column="lowest_mark" property="lowestMark"/>
        <result column="lowest_rank" property="lowestRank"/>
        <result column="is_delete" property="isDelete"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <sql id="table">
        volunteer
    </sql>

    <sql id="cols_all">
        id,
        <include refid="cols_exclude_id"/>
    </sql>

    <sql id="cols_exclude_id">
        type,scheme_id,major_name,academy, course_demand,nature,year_limit,plan_num,
        cast_archives_num,launch_num,lowest_mark,
        lowest_rank,is_delete,create_time,update_time
    </sql>

    <sql id="vals">
        #{type},#{scheme},#{majorName},#{academy},#{courseDemand},#{nature},#{yearLimit},#{planNum},#{castArchivesNum},
        #{launchNum},
        #{lowestMark}, #{lowestRank},#{isDelete},now(),now()
    </sql>

    <sql id="updateCondition">
        <if test="type != null">type = #{type},</if>
        <if test="scheme != null">scheme = #{scheme},</if>
        <if test="majorName != null">major_name = #{majorName},</if>
        <if test="academy != null">academy =#{academy},</if>
        <if test="courseDemand != null">course_demand =#{courseDemand},</if>
        <if test="nature != null">nature =#{nature},</if>
        <if test="yearLimit != null">year_limit =#{yearLimit},</if>
        <if test="planNum != null">plan_num =#{planNum},</if>
        <if test="castArchivesNum != null">cast_archives_num = #{castArchivesNum},</if>
        <if test="launchNum != null">launch_num = #{launchNum},</if>
        <if test="lowestMark != null">lowest_mark = #{lowestMark},</if>
        <if test="lowestRank != null">lowestMark = #{lowestRank},</if>
        <if test="isDelete != null">is_delete = #{isDelete},</if>
        update_time =now()
    </sql>

    <sql id="criteria">
        <if test="id != null">id = #{id}</if>
        <if test="type != null">and type = #{type}</if>
        <if test="scheme != null">and scheme = #{scheme}</if>
        <if test="majorName != null">and major_name = #{majorName}</if>
        <if test="academy != null">and academy =#{academy}</if>
        <if test="courseDemand != null">and course_demand =#{courseDemand}</if>
        <if test="nature != null">and nature =#{nature}</if>
        <if test="yearLimit != null">and year_limit =#{yearLimit}</if>
        <if test="planNum != null">and plan_num =#{planNum}</if>
        <if test="castArchivesNum != null">and cast_archives_num = #{castArchivesNum}</if>
        <if test="launchNum != null">and launch_num = #{launchNum}</if>
        <if test="lowestMark != null">and lowest_mark = #{lowestMark}</if>
        <if test="lowestRank != null">and lowestMark = #{lowestRank}</if>
        <if test="isDelete != null">and is_delete = #{isDelete}</if>
        <if test="createTime != null">and create_time &gt;= #{createTime}</if>
        <if test="updateTime != null">and #{updateTime} &gt;= update_time</if>
    </sql>

    <insert id="add" parameterType="cn.wisenergy.model.app.Volunteer" keyProperty="id" useGeneratedKeys="true">
        insert into
        <include refid="table"/>
        (<include refid="cols_exclude_id"/>)
        value(
        <include refid="vals"/>
        )
    </insert>


    <update id="updateBySchemeId">
        UPDATE
        <include refid="table"/>
        <set>
            is_delete=1
        </set>
        <where>
            scheme_id = #{schemeId}
            and is_delete=0
        </where>
    </update>

    <select id="getFillList" resultType="cn.wisenergy.model.app.Volunteer">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            is_delete =0
            <if test="upGrade != null">
                and #{upGrade} > lowest_mark
            </if>
            <if test="downGrade != null">
                and lowest_mark >#{downGrade}
            </if>
            <if test="volunteerIds != null and volunteerIds.size > 0 ">
                and id NOT IN
                <foreach collection="volunteerIds" index="index" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
            order by lowest_mark desc
            limit #{number}
        </where>
    </select>

    <select id="getListByIds" resultType="cn.wisenergy.model.app.Volunteer">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            id IN
            <foreach collection="list" index="index" item="id" separator="," open="(" close=")">
                #{id}
            </foreach>
        </where>
    </select>

    <select id="getVolunteerList" resultType="cn.wisenergy.model.app.Volunteer">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            is_delete =0
            <if test="upGrade != null">
                and #{upGrade} > lowest_mark
            </if>
            <if test="downGrade != null">
                and lowest_mark >#{downGrade}
            </if>
            <if test="classNames != null">
                and
                <foreach collection="classNames" index="index" item="id" separator="or" open="(" close=")">
                    course_demand LIKE CONCAT('%',#{id},'%')
                </foreach>
            </if>
            <if test="professionNames != null">
                and
                <foreach collection="professionNames" index="index" item="id" separator="or" open="(" close=")">
                    major_name LIKE CONCAT('%',#{id},'%')
                </foreach>
            </if>
            order by lowest_mark desc
        </where>
    </select>

</mapper>