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

    <resultMap type="CphAuditDetails" id="CphAuditDetailsResult">
        <result property="id"    column="id"    />
        <result property="projectName"    column="project_name"    />
        <result property="submitterId"    column="submitter_id"    />
        <result property="submitterName"    column="submitter_name"    />
        <result property="reviewedById"    column="reviewed_by_id"    />
        <result property="projectId"    column="project_id"    />
        <result property="projectTypeId"    column="project_type_id"    />
        <result property="statusCode"    column="status_code"    />
        <result property="createTime"    column="create_time"    />
        <result property="remarks"    column="remarks"    />
        <result property="stuNo"    column="stu_no"    />
        <result property="deptName"    column="dept_name"    />
        <result property="majorsName"    column="major_name"    />
        <result property="className"    column="class_name"    />
        <association property="rules" column="project_type_id" javaType="cphRules" resultMap="CphRulesResult"/>
    </resultMap>
    <resultMap type="CphRules" id="CphRulesResult">
        <result property="ruleId"    column="rule_id"    />
        <result property="ruleName"    column="rule_name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="maxScore"    column="max_score"    />
        <result property="minScore"    column="min_score"    />
    </resultMap>

    <sql id="selectCphAuditDetailsVo">
        select id, project_name, submitter_id, submitter_name, reviewed_by_id, project_id, project_type_id, status_code,
               ad.create_time, remarks,cr.*,s.stu_no,class_name,major_name,dept_name
        from cph_audit_details ad
                 left join  cph_rules cr on ad.project_type_id = cr.rule_id
                 left join srs_student s on   s.stu_id  = ad.submitter_id
                 left JOIN srs_class c ON s.class_id = c.class_id
                 left JOIN srs_majors m ON c.major_id = m.major_id
                 left JOIN sys_dept d ON m.college_id = d.dept_id

    </sql>

    <select id="selectCphAuditDetailsList" parameterType="CphAuditDetails" resultMap="CphAuditDetailsResult">
        <include refid="selectCphAuditDetailsVo"/>
        <where>
            <if test="projectName != null  and projectName != ''"> and project_name like concat('%', #{projectName}, '%')</if>
            <if test="submitterId != null "> and submitter_id = #{submitterId}</if>
            <if test="submitterName != null  and submitterName != ''"> and submitter_name like concat('%', #{submitterName}, '%')</if>
            <if test="reviewedById != null "> and reviewed_by_id = #{reviewedById}</if>
            <if test="projectId != null "> and project_id = #{projectId}</if>
            <if test="projectTypeId != null "> and project_type_id = #{projectTypeId}</if>
            <if test="statusCode != null "> and status_code = #{statusCode}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
            <if test="gradeId != null  "> and grade_id = #{gradeId}</if>
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="majorsName != null  and majorsName != ''"> and major_name like concat('%', #{majorsName}, '%')</if>
            <if test="className != null  and className != ''"> and class_name like concat('%', #{className}, '%')</if>
            <if test="stuNo != null  and stuNo != ''"> and s.stu_no like concat('%', #{stuNo}, '%')</if>
        </where>
        order by id desc
    </select>

    <select id="listMyProcessed" parameterType="CphAuditDetails"  resultMap="CphAuditDetailsResult">
        <include refid="selectCphAuditDetailsVo"/>
        <where>
            <if test="params.fdy != null ">
                and submitter_id in (select stu_id from v_stu_teacher where teacher_id= #{params.fdy})
                and status_code >= 2
            </if>
            <if test="params.jwc != null ">
                and submitter_id in  (select stu_id from v_stu_dept where user_id=#{params.jwc})
                and status_code >=3
            </if>
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="majorsName != null  and majorsName != ''"> and major_name like concat('%', #{majorsName}, '%')</if>
            <if test="className != null  and className != ''"> and class_name like concat('%', #{className}, '%')</if>
            <if test="stuNo != null  and stuNo != ''"> and stu_no like concat('%', #{stuNo}, '%')</if>
            <if test="gradeId != null  "> and c.grade_id = #{gradeId}</if>
        </where>
        order by id desc
    </select>

    <select id="searchCphAuditDetailsList"  parameterType="CphAuditDetails"  resultMap="CphAuditDetailsResult">
        <include refid="selectCphAuditDetailsVo"/>
        <where>
            <if test="stuNo != null  and stuNo != ''"> and stu_no like concat('%', #{stuNo}, '%')</if>
            <if test="stuName != null  and stuName != ''"> and submitter_name like concat('%', #{stuName}, '%')</if>
            <if test="statusCode != null "> and status_code = #{statusCode}</if>
            <if test="params.fdy != null ">
                and submitter_id in (select stu_id from v_stu_teacher where teacher_id= #{params.fdy})
            </if>
            <if test="params.jwc != null ">
                and submitter_id in  (select stu_id from v_stu_dept where dept_id=#{params.jwc})
            </if>
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="majorsName != null  and majorsName != ''"> and major_name like concat('%', #{majorsName}, '%')</if>
            <if test="className != null  and className != ''"> and class_name like concat('%', #{className}, '%')</if>
            <if test="gradeId != null  "> and c.grade_id = #{gradeId}</if>
        </where>
        order by stu_no,id desc
    </select>


    <select id="selectCphAuditDetailsById" parameterType="Long" resultMap="CphAuditDetailsResult">
        <include refid="selectCphAuditDetailsVo"/>
        where ad.id = #{id}
    </select>

    <select id="selectCphAuditDetailsByProjectId" parameterType="Long" resultMap="CphAuditDetailsResult">
        <include refid="selectCphAuditDetailsVo"/>
        where ad.project_id = #{id}
    </select>

    <insert id="insertCphAuditDetails" parameterType="CphAuditDetails" useGeneratedKeys="true" keyProperty="id">
        insert into cph_audit_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectName != null">project_name,</if>
            <if test="submitterId != null">submitter_id,</if>
            <if test="submitterName != null">submitter_name,</if>
            <if test="reviewedById != null">reviewed_by_id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="projectTypeId != null">project_type_id,</if>
            <if test="statusCode != null">status_code,</if>
            <if test="createTime != null">create_time,</if>
            <if test="remarks != null">remarks,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectName != null">#{projectName},</if>
            <if test="submitterId != null">#{submitterId},</if>
            <if test="submitterName != null">#{submitterName},</if>
            <if test="reviewedById != null">#{reviewedById},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="projectTypeId != null">#{projectTypeId},</if>
            <if test="statusCode != null">#{statusCode},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="remarks != null">#{remarks},</if>
         </trim>
    </insert>

    <update id="updateCphAuditDetails" parameterType="CphAuditDetails">
        update cph_audit_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectName != null">project_name = #{projectName},</if>
            <if test="submitterId != null">submitter_id = #{submitterId},</if>
            <if test="submitterName != null">submitter_name = #{submitterName},</if>
            <if test="reviewedById != null">reviewed_by_id = #{reviewedById},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="projectTypeId != null">project_type_id = #{projectTypeId},</if>
            <if test="statusCode != null">status_code = #{statusCode},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCphAuditDetailsById" parameterType="Long">
        delete from cph_audit_details where id = #{id}
    </delete>

    <delete id="deleteCphAuditDetailsByIds" parameterType="String">
        delete from cph_audit_details where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <update id="cancelAuditById" parameterType="Long">
        update   cph_iam set audit_status=1 where id=#{id}
    </update>
    <update id="cancelCphAudiDetailsById" parameterType="Long">
        update   cph_audit_details set status_code=1 where project_id=#{id}
    </update>

    <select id="listCancelRecord" resultType="SrsAddRecord">
        select a.*
        from srs_add_record as a
        left join cph_iam as b on a.iam_id = b.id
        where b.audit_status not in (6,12)
    </select>

    <select id="checkIamList" resultType="CheckIam">
        -- 先将两个查询结果作为子查询，再进行比较
        SELECT
            q2.stu_year_id,
            q2.stu_no,
            q2.iam_score AS q2_score,
            q1.iam_score AS q1_score,
            -- 标记差异类型
            CASE
                WHEN q1.iam_score IS NULL THEN '加分记录表中不存在'
                ELSE '分数不相等'
                END AS diff_type
        FROM
            (
                -- 第二个查询（作为子查询q2）
                SELECT
                    b.stu_year_id,
                    c.stu_no,
                    SUM(b.iam_score) AS iam_score
                FROM
                    cph_audit_details AS a
                        LEFT JOIN cph_iam AS b ON a.project_id = b.id
                        LEFT JOIN view_stu_info AS c ON b.to_add = c.stu_id
                WHERE
                    a.status_code = 6
                GROUP BY
                    b.stu_year_id,
                    c.stu_no
            ) AS q2
                LEFT JOIN (
                -- 第一个查询（作为子查询q1）
                SELECT
                    a.stu_year_id,
                    a.stu_no,
                    SUM(record_score) AS iam_score
                FROM
                    srs_add_record AS a
                GROUP BY
                    a.stu_year_id,
                    a.stu_no
            ) AS q1 ON q2.stu_year_id = q1.stu_year_id
                AND q2.stu_no = q1.stu_no
        WHERE
           -- 条件：要么q1中不存在，要么分数不相等
            q1.iam_score IS NULL
           OR q2.iam_score != q1.iam_score;
    </select>

    <select id="listCheckAudit" parameterType="CheckIam" resultType="CphAuditDetails">
        SELECT
            a.*,b.iam_score,b.audit_status,c.stu_no,b.stu_year_id
        FROM
            cph_audit_details AS a
                LEFT JOIN cph_iam AS b ON a.project_id = b.id
                LEFT JOIN view_stu_info AS c ON b.to_add = c.stu_id
        WHERE
            a.status_code = 6
          and c.stu_no = #{stuNo}
          and b.stu_year_id = #{stuYearId}
    </select>

    <select id="ListCheckRecord" parameterType="CheckIam" resultType="SrsAddRecord">
        select a.*,b.rule_name
        from srs_add_record as a
        left join cph_rules as b on a.rule_id = b.rule_id
        where stu_no = #{stuNo} and stu_year_id = #{stuYearId};
    </select>

</mapper>
