<?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.xjrsoft.module.textbook.mapper.TextbookStudentClaimMapper">
    <select id="getTextbookClaimList" parameterType="com.xjrsoft.module.textbook.dto.TextbookClaimStudentConfirmDto"
            resultType="com.xjrsoft.module.textbook.vo.TextbookClaimVO">
        SELECT t.id                   AS textbookStudentClaimId,
        t1.price * t1.discount/10 AS discountPrice,
        t.sort_code as sortCode,
        t.textbook_id as textbookId,
        t.is_claim as isClaim,
        t1.book_name as bookName,
        t1.price as price
        FROM textbook_student_claim t
        LEFT JOIN textbook t1 ON (t1.id = t.textbook_id)
        WHERE t.delete_mark = 0
        AND (t.student_user_id = #{dto.studentUserId})
        AND (t.base_semester_id = #{dto.baseSemesterId})
        <if test="dto.showOrConfirm != null and dto.showOrConfirm == 2">
            and t.is_claim = 0
        </if>
        ORDER BY t.id DESC
    </select>

<select id="getTeacherCheckByclassList" parameterType="com.xjrsoft.module.textbook.dto.TeacherCheckByclassDto"
        resultType="com.xjrsoft.module.textbook.vo.TeacherCheckByclassVo">
    WITH textbook_claim_counts AS (
    SELECT t.class_id, t1.textbook_id, count(t1.issue_number) AS received_num
    FROM wf_textbook_claim t
    LEFT JOIN wf_textbook_claim_item t1 ON t1.wf_textbook_claim_id = t.id
    GROUP BY t.class_id, t1.textbook_id
    ),
    actual_claim_counts AS (
    SELECT class_id, textbook_id, count(is_claim) AS claim_num
    FROM textbook_student_claim
    WHERE is_claim = 1
    GROUP BY class_id, textbook_id
    )
    SELECT a.class_id,
    b.id AS textbookId,
    b.book_name,
    c.name,
    d.total_student AS classStudentNum,
    tcc.received_num AS actualReceivedNum,
    acc.claim_num AS actualClaimNum
    FROM textbook b
    INNER JOIN (
    SELECT class_id, textbook_id
    FROM textbook_student_claim
    GROUP BY class_id, textbook_id
    ) a ON a.textbook_id = b.id
    INNER JOIN base_class c ON a.class_id = c.id
    INNER JOIN base_class_major_set d ON c.id = d.class_id
    LEFT JOIN textbook_claim_counts tcc ON tcc.class_id = a.class_id AND tcc.textbook_id = a.textbook_id
    LEFT JOIN actual_claim_counts acc ON acc.class_id = a.class_id AND acc.textbook_id = a.textbook_id
    WHERE a.class_id &lt;&gt; 0
    <if test="dto.classIdList != null and dto.classIdList.size() > 0">
        AND a.class_id IN
        <foreach item="classId" index="index" collection="dto.classIdList" open="(" close=")"
                 separator=",">
            #{classId}
        </foreach>
    </if>
    <if test="dto.claimStatus != null and dto.claimStatus == 2">
        AND tcc.received_num = acc.claim_num
    </if>
    <if test="dto.claimStatus != null and dto.claimStatus == 3">
        AND tcc.received_num > acc.claim_num
    </if>
</select>

    <select id="getTeacherCheckStuClaimList" parameterType="com.xjrsoft.module.textbook.dto.TeacherCheckStuClaimDto"
            resultType="com.xjrsoft.module.textbook.vo.TeacherCheckStuClaimVo">
        SELECT
            t.id as textbookStudentClaimId,
            t.textbook_id,
               t1.book_name as bookName,
               t.student_user_id,
               t2.student_id as studentId,
               t3.name as studentName,
               t.is_claim
        FROM textbook_student_claim t
                 LEFT JOIN textbook t1 ON (t1.id = t.textbook_id)
                 LEFT JOIN base_student t2 ON (t2.user_id = t.student_user_id)
                 LEFT JOIN xjr_user t3 ON (t3.id = t.student_user_id)
        WHERE t.delete_mark = 0
            and t.class_id = #{dto.classId} and  t.textbook_id = #{dto.textbookId}
        ORDER BY t.is_claim
    </select>

    <select id="getTeacherCheckByStuList" parameterType="com.xjrsoft.module.textbook.dto.TeacherCheckByStuDto"
            resultType="com.xjrsoft.module.textbook.vo.TeacherCheckByStuVo">
        SELECT
        t.id as studentUserId,
        t.name as studentUserIdCN,
        t1.student_id as studentId
        FROM xjr_user t
        LEFT JOIN base_student t1 ON (t1.user_id = t.id)
        where t.id in (SELECT distinct student_user_id
        FROM textbook_student_claim t
        WHERE delete_mark = 0
        <if test="dto.claimStatus != null and dto.claimStatus == 3">
            and (select count(*) from textbook_student_claim where is_claim = 1 and student_user_id = t.student_user_id) != (select count(*) from textbook_student_claim where student_user_id = t.student_user_id)
        </if>
        <if test="dto.claimStatus != null and dto.claimStatus == 2">
            and (select count(*) from textbook_student_claim where is_claim = 1 and student_user_id = t.student_user_id) = (select count(*) from textbook_student_claim where student_user_id = t.student_user_id)
        </if>
        <if test="dto.classIdList != null and dto.classIdList.size() > 0">
            and class_id in
            <foreach item="classId" index="index" collection="dto.classIdList" open="(" close=")"
                     separator=",">
                #{classId}
            </foreach>
        </if>)
        <if test="dto.studentUserId != null and dto.studentUserId != 0 and dto.studentUserId != ''">
            and t.id = #{dto.studentUserId}
        </if>
    </select>

    <select id="getTextbookClaimVOList"
            resultType="com.xjrsoft.module.textbook.vo.TextbookClaimVO">
        SELECT
            t.id as textbookStudentClaimId,
            t.textbook_id,
            t1.book_name as bookName,
            t.is_claim
        FROM textbook_student_claim t
                 LEFT JOIN textbook t1 ON (t1.id = t.textbook_id)
        where t.student_user_id = #{studentUserId}
        ORDER BY t.is_claim
    </select>
</mapper>