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

    <resultMap type="CphIam" id="CphIamResult">
        <result property="id" column="id"/>
        <result property="submitter" column="submitter"    />
        <result property="toAdd" column="to_add"    />
        <result property="material" column="material"/>
        <result property="description" column="description"/>
        <result property="auditStatus"    column="audit_status"    />
        <result property="iamScore"    column="iam_score"    />
        <result property="ruleId"    column="rule_id"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="stuYearId"    column="stu_year_id"    />
        <result property="projectName"    column="project_name"    />
        <result property="submitTime"    column="submit_time"    />
        <result property="className"    column="class_name"    />
        <result property="majorName"    column="major_name"    />
        <association property="rules"    column="to_add" javaType="CphRules" resultMap="CphRulesResult" />
        <association property="stuYearS"    column="stu_year_id" javaType="srsStuYear" resultMap="SrsStuYearResult" />
        <association property="user"    column="submitter" javaType="sysUser" resultMap="SysUserResult" />
        <association property="student" javaType="SrsStudent" resultMap="studentJoin" />

    </resultMap>
    <resultMap type="SrsStuYear" id="SrsStuYearResult">
        <result property="id"    column="id"    />
        <result property="stuYearName"    column="stu_year_name"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
    </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>
    <resultMap type="SysUser" id="SysUserResult">
        <id     property="userId"       column="user_id"      />
        <result property="deptId"       column="dept_id"      />
        <result property="userName"     column="user_name"    />
        <result property="nickName"     column="nick_name"    />
    </resultMap>
    <resultMap type="SrsStudent" id="studentJoin">
        <result property="stuId"    column="stu_id"    />
        <result property="name"    column="name"    />
        <result property="stuNo"    column="stu_no"    />
        <result property="gender"    column="gender"    />
        <result property="birthday"    column="birthday"    />
        <result property="idCard"    column="id_card"    />
        <result property="deptId"    column="dept_id"    />
        <result property="majorId"    column="major_id"    />
        <result property="classId"    column="class_id"    />
        <result property="phone"    column="phone"    />
        <result property="address"    column="address"    />
        <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"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <resultMap type="CphIdeologyScore" id="CphIamSumResult">
        <result property="stuNo"    column="stu_no"    />
        <result property="name"    column="name"    />
        <result property="stuYearId"    column="stu_year_id"    />
        <result property="stuYearName"    column="stu_year_name"    />
        <result property="score"    column="score"    />
        <result property="className" column="class_name"/>
        <result property="status"    column="status"    />
    </resultMap>


    <sql id="selectCphIamVo">
        select id, submitter, to_add, material, description,iam_score,
               rule_id, audit_status, del_flag, create_by,
               create_time, update_by, update_time, stu_year_id from cph_iam
    </sql>


    <sql id="selectCphIamJoin">
        select ci.id, ci.submitter, ci.to_add, ci.material, ci.description,ci.iam_score,
               ci.rule_id, ci.audit_status, ci.del_flag, ci.create_by,
               ci.create_time, ci.update_by, ci.update_time, ci.stu_year_id,
               r.*,sy.*,u.*,stu.*
        from cph_iam ci
                 left join cph_rules r on r.rule_id = ci.rule_id
                 left join srs_stu_year sy on sy.id = ci.stu_year_id
                 left join sys_user u on  u.user_id = ci.submitter
                 left join srs_student stu on stu.stu_id = ci.to_add
    </sql>


    <select id="selectCphIamList" parameterType="CphIam" resultMap="CphIamResult">
        select ci.id, ci.submitter, ci.to_add, ci.material, ci.description,ci.iam_score,
        ci.rule_id, ci.audit_status, ci.del_flag, ci.create_by,
        ci.create_time, ci.update_by, ci.update_time, ci.stu_year_id,ss.name,sy.stu_year_name,
        ss.stu_no,su.nick_name,su.user_id,sc.class_name, e.major_name , f.dept_name,cr.rule_name
        from cph_iam ci
        left join  srs_student ss on ci.to_add=ss.stu_id
        left join srs_stu_year sy on ci.stu_year_id=sy.id
        left join sys_user su on su.user_id=ci.submitter
        left join cph_rules as cr on cr.rule_id = ci.rule_id
        left join srs_class as sc on ss.class_id = sc.class_id
        left join srs_majors as e on sc.major_id = e.major_id
        left join sys_dept as f on e.college_id = f.dept_id
        <where>
            <if test="submitter != null "> and submitter = #{submitter}</if>
            <if test="toAdd != null "> and to_add = #{toAdd}</if>
            <if test="material != null  and material != ''"> and material = #{material}</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="auditStatus != null  and auditStatus != ''"> and audit_status = #{auditStatus}</if>
            <if test="params.stuNo != null and params.stuNo !='' " > and ss.stu_no=#{params.stuNo}</if>
            <if test="stuYearId != null "> and ci.stu_year_id = #{stuYearId}</if>
        </where>
    </select>

    <!-- 查询思想品德学生总分/-->
    <select id="selectCphIamSumList" parameterType="CphIdeologyScore" resultMap="CphIamSumResult">
        SELECT ide.stu_no, stu.name, sy.id stu_year_id, sy.stu_year_name,sc.class_name,stu.status,
        CASE WHEN (IFNULL(t1.sum_opt_score,0) + IFNULL(ide.score,0)) &gt; 100 THEN 100
        WHEN (IFNULL(t1.sum_opt_score,0) + IFNULL(ide.score,0)) &lt;  0 THEN 0
        ELSE (IFNULL(t1.sum_opt_score,0) + IFNULL(ide.score,0)) END score
        FROM  cph_ideology_score ide LEFT JOIN
        (
        SELECT rec.stu_no, SUM(record_score) sum_opt_score,stu_year_id
        FROM srs_add_record rec
        WHERE rule_id not in (2, 3, 4, 5)
        GROUP BY rec.stu_no,stu_year_id
        ) t1   ON  ide.stu_no=t1.stu_no  and ide.stu_year_id=t1.stu_year_id
        LEFT JOIN srs_student stu ON ide.stu_no = stu.stu_no
        left join srs_class sc on stu.class_id=sc.class_id
        LEFT JOIN srs_stu_year sy ON sy.id = ide.stu_year_id
        <where>
            <if test="stuYearId != null "> and ide.stu_year_id = #{stuYearId}</if>
            <if test="classId != null "> and stu.class_id = #{classId}</if>
            <if test="stuNo != null and stuNo!=''"> and stu.stu_no = #{stuNo}</if>
            <if test="name != null and name!=''"> and stu.name like #{name}</if>
        </where>
    </select>

    <!-- 连表查询所有相关信息/-->
    <select id="selectCphIamListJoin" parameterType="CphIam" resultMap="CphIamResult">
        <include refid="selectCphIamJoin"/>
        <where>
            <if test="submitter != null "> and ci.submitter = #{submitter}</if>
            <if test="toAdd != null "> and ci.to_add = #{toAdd}</if>
            <if test="material != null  and material != ''"> and ci.material = #{material}</if>
            <if test="description != null  and description != ''"> and ci.description = #{description}</if>
            <if test="auditStatus != null  and auditStatus != ''"> and ci.audit_status = #{auditStatus}</if>
            <if test="stuYearId != null "> and ci.stu_year_id = #{stuYearId}</if>
            <if test="student.classId != null "> and stu.class_id = #{student.classId}</if>
            <if test="student.stuNo != null and student.stuNo!=''"> and stu.stu_no = #{student.stuNo}</if>
            <if test="student.name != null "> and stu.name like #{student.name}</if>
        </where>
    </select>

    <select id="listOwnProcessed" resultType="com.srs.comprehensive.domain.Vo.CphApply" parameterType="CphSearch">
        select b.`name` as stu_name, '思想品德' as project_name,
        c.rule_name as rule_name,a.create_time as submit_time,a.audit_status as audit_status,
        d.class_name, e.major_name , f.dept_name , a.description , a.material , g.stu_year_name,
        a.iam_score as operate_score,b.stu_no,a.id
        from cph_iam as a
        left join srs_student as b on a.to_add = b.stu_id
        left join cph_rules as c on a.rule_id = c.rule_id
        left join srs_class as d on b.class_id = d.class_id
        left join srs_majors as e on d.major_id = e.major_id
        left join sys_dept as f on e.college_id = f.dept_id
        left join srs_stu_year as g on a.stu_year_id = g.id
        left join cph_teacher as h on h.teacher_id = d.teacher_id
        <where>
            h.employee_id = #{tNo} and audit_status not in (1,13)
            <if test="stuNo != null and stuNo!=''"> and b.stu_no = #{stuNo}</if>
            <if test="stuName != null and stuName!=''"> and b.name like #{stuName}</if>
        </where>
        ORDER BY a.create_time desc,b.name asc
    </select>

    <select id="listOwnApply" resultType="com.srs.comprehensive.domain.Vo.CphApply" >
        select b.`name` as stu_name, '思想品德' as project_name,
               c.rule_name as rule_name,a.create_time as submit_time,a.audit_status as audit_status,
               d.class_name, e.major_name , f.dept_name , a.description , a.material , g.stu_year_name,
               a.iam_score as operate_score , h.remarks,c.rule_id,a.stu_year_id,a.id
        from cph_iam as a
                 left join srs_student as b on a.to_add = b.stu_id
                 left join cph_rules as c on a.rule_id = c.rule_id
                 left join srs_class as d on b.class_id = d.class_id
                 left join srs_majors as e on d.major_id = e.major_id
                 left join sys_dept as f on e.college_id = f.dept_id
                 left join srs_stu_year as g on a.stu_year_id = g.id
                left join cph_audit_details as h on a.id = h.project_id
        where b.stu_no = #{stuNo} and audit_status not in (13)
        ORDER BY a.create_time desc
    </select>

    <select id="listIamByStuNoYear" parameterType="com.srs.comprehensive.domain.Vo.CphSearch" resultType="com.srs.comprehensive.domain.Vo.CphIamSearchVo">
        SELECT a.iam_score,f.stu_year_name,g.nick_name as submitter ,
               b.`name` as stu_name,a.description,a.audit_status,a.material,a.id as iam_id,
               c.class_name,d.major_name,e.dept_name,h.rule_name
        from cph_iam as a
                 left join srs_student as b on a.to_add = b.stu_id
                 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 sys_dept as e on d.college_id = e.dept_id
                 left join srs_stu_year as f on a.stu_year_id = f.id
                 left join sys_user as g on a.submitter = g.user_id
                 left join cph_rules as h on a.rule_id = h.rule_id
        where b.stu_no = #{stuNo} and a.stu_year_id = #{stuYearId}
    </select>

    <select id="getOwnRecord" resultType="com.srs.comprehensive.domain.Vo.CphRecord">
        select a.iam_score as record_score , b.stu_no ,c.rule_name,d.stu_year_name,a.create_time,a.description
        from cph_iam as a
                 left join srs_student as b on a.to_add = b.stu_id
                 left join cph_rules as c on a.rule_id = c.rule_id
                 left join srs_stu_year as d on a.stu_year_id = d.id
        where a.audit_status = 6 and b.stu_no = #{stuNo} and d.stu_year_name =  #{year}
    </select>


    <select id="selectCphIamById" parameterType="Long" resultMap="CphIamResult">
        <include refid="selectCphIamJoin"/>
        where ci.id = #{id}
    </select>

    <insert id="insertCphIam" parameterType="CphIam" useGeneratedKeys="true" keyProperty="id">
        insert into cph_iam
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="submitter != null">submitter,</if>
            <if test="toAdd != null">to_add,</if>
            <if test="material != null">material,</if>
            <if test="description != null">description,</if>
            <if test="auditStatus != null and auditStatus != ''">audit_status,</if>
            <if test="iamScore != null">iam_score,</if>
            <if test="ruleId != null">rule_id,</if>
            <if test="delFlag != null and delFlag != ''">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>
            <if test="stuYearId != null">stu_year_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="submitter != null">#{submitter},</if>
            <if test="toAdd != null">#{toAdd},</if>
            <if test="material != null">#{material},</if>
            <if test="description != null">#{description},</if>
            <if test="auditStatus != null and auditStatus != ''">#{auditStatus},</if>
            <if test="iamScore != null">#{iamScore},</if>
            <if test="ruleId != null">#{ruleId},</if>
            <if test="delFlag != null and delFlag != ''">#{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>
            <if test="stuYearId != null">#{stuYearId},</if>
        </trim>
    </insert>

    <update id="updateCphIam" parameterType="CphIam">
        update cph_iam
        <trim prefix="SET" suffixOverrides=",">
            <if test="submitter != null">submitter = #{submitter},</if>
            <if test="toAdd != null">to_add = #{toAdd},</if>
            <if test="material != null">material = #{material},</if>
            <if test="description != null">description = #{description},</if>
            <if test="auditStatus != null and auditStatus != ''">audit_status = #{auditStatus},</if>
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="iamScore != null">iam_score = #{iamScore},</if>
            <if test="ruleId != null">rule_id = #{ruleId},</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>
            <if test="stuYearId != null">stu_year_id = #{stuYearId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCphIamById" parameterType="Long">
        delete from cph_iam where id = #{id}
    </delete>

    <delete id="deleteCphIamByIds" parameterType="String">
        delete from cph_iam where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="executeTestIam" statementType="CALLABLE">
        {call test_iam(#{in_stu_year_id,mode=IN,jdbcType=INTEGER}, #{in_stu_no,mode=IN,jdbcType=VARCHAR})}
    </select>

    <select id="listErrorAddRecord" parameterType="CphSearch" resultType="map">
        select a.*, b.rule_name, c.stu_year_name
        from srs_add_record as a
                 left join cph_rules as b on a.rule_id = b.rule_id
                 left join srs_stu_year as c on a.stu_year_id = c.id
        where iam_id in (SELECT iam_id
                         FROM srs_add_record
                         where iam_id is not null
                           and stu_no = #{stuNo}
                           and stu_year_id = #{stuYearId}
                         GROUP BY iam_id
                         HAVING COUNT(*) > 1)
        order by iam_id;

    </select>

</mapper>