<?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.SrsKnrdApprovalRecordMapper">

    <resultMap type="SrsKnrdApprovalRecord" id="SrsKnrdApprovalRecordResult">
        <result property="id"    column="id"    />
        <result property="step"    column="step"    />
        <result property="approver"    column="approver"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="remark"    column="remark"    />
        <result property="applyId"    column="apply_id"    />
        <result property="knrd"    column="knrd"    />
    </resultMap>

    <sql id="selectSrsKnrdApprovalRecordVo">
        select id, step, approver, status, create_time, remark, apply_id from srs_knrd_approval_record
    </sql>

    <select id="selectSrsKnrdApprovalRecordList" parameterType="SrsKnrdApprovalRecord" resultMap="SrsKnrdApprovalRecordResult">
        <include refid="selectSrsKnrdApprovalRecordVo"/>
        <where>
            <if test="step != null "> and step = #{step}</if>
            <if test="approver != null  and approver != ''"> and approver = #{approver}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="applyId != null "> and apply_id = #{applyId}</if>
        </where>
    </select>

    <select id="selectSrsKnrdApprovalRecordById" parameterType="Long" resultMap="SrsKnrdApprovalRecordResult">
        <include refid="selectSrsKnrdApprovalRecordVo"/>
        where id = #{id}
    </select>
    <select id="listView" parameterType="CphSearch" resultType="SrsKnrdApply">
    select a.*, d.major_type_name, g.step as latest_step, g.status as latest_status
    from srs_knrd_apply as a
    left join srs_student as b on a.xh = b.stu_no
    left join srs_class as c on b.class_id = c.class_id
    left join srs_majors as d on c.major_id = d.major_id
    left join cph_teacher as e on c.teacher_id = e.teacher_id
    left join sys_dept_map as f on d.college_id = f.old_dept_id
    left join (
        SELECT r1.*
        FROM srs_knrd_approval_record r1
        INNER JOIN (
            SELECT apply_id, MAX(id) as max_id
            FROM srs_knrd_approval_record
            GROUP BY apply_id
        ) r2 ON r1.apply_id = r2.apply_id AND r1.id = r2.max_id
    ) as g on g.apply_id = a.id
    where a.step != 0 and a.step != 1
        <if test="collegeId != null"> and d.college_id = #{collegeId}</if>
        <if test="tNo != null"> and e.employee_id = #{tNo}</if>
        <if test="deptId != null"> and f.new_dept_id = #{deptId}</if>
        <if test="stuNo != null"> and a.xh = #{stuNo}</if>
        <if test="sfzhm != null"> and a.sfzhm = #{sfzhm}</if>
        <if test="xm != null"> and a.xm like concat('%',#{xm},'%')</if>
        <if test="xh != null"> and a.xh like concat('%',#{xh},'%')</if>
        <if test="yj != null"> and (a.bjpyyj = #{yj} or a.ejxyldqmyj = #{yj} or a.xsqmyj = #{yj})</if>
        <if test="applyYear != null"> and a.apply_year like CONCAT('%',#{applyYear},'%')</if>
    order by a.id desc
</select>

    <select id="selectSrsKnrdApprovalRecordByIds" resultType="SrsKnrdApprovalRecord">
        select * from srs_knrd_approval_record where apply_id=#{id}
    </select>

    <insert id="insertSrsKnrdApprovalRecord" parameterType="SrsKnrdApprovalRecord" useGeneratedKeys="true" keyProperty="id">
        insert into srs_knrd_approval_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="step != null">step,</if>
            <if test="approver != null">approver,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="remark != null">remark,</if>
            <if test="applyId != null">apply_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="step != null">#{step},</if>
            <if test="approver != null">#{approver},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="applyId != null">#{applyId},</if>
         </trim>
    </insert>

    <insert id="batchInsertSrsKnrdApprovalRecord" parameterType="SrsKnrdApprovalRecord" useGeneratedKeys="true" keyProperty="id">
        <if test="step == 3 || step == 4">
           select bjpyyj into @status from srs_knrd_apply where id = #{applyId};
        </if>

        <if test="step ==2">
            set @status := ${status};
        </if>

        insert into srs_knrd_approval_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="step != null">step,</if>
            <if test="approver != null">approver,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="remark != null">remark,</if>
            <if test="applyId != null">apply_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="step != null">#{step},</if>
            <if test="approver != null">#{approver},</if>
            <if test="status != null">@status,</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="applyId != null">#{applyId},</if>
        </trim>
    </insert>

    <update id="updateSrsKnrdApprovalRecord" parameterType="SrsKnrdApprovalRecord">
        update srs_knrd_approval_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="step != null">step = #{step},</if>
            <if test="approver != null">approver = #{approver},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="knrd != null">knrd = #{knrd},</if>
            <if test="applyId != null">apply_id = #{applyId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSrsKnrdApprovalRecordById" parameterType="Long">
        delete from srs_knrd_approval_record where id = #{id}
    </delete>

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