<?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.TextbookMapper">
    <select id="getPage" parameterType="com.xjrsoft.module.textbook.dto.TextbookPageDto"
            resultType="com.xjrsoft.module.textbook.vo.TextbookPageVo">
        SELECT t1.id,t1.id AS textbookId,t1.sort_code,t1.issn,t1.book_name,t1.publishing_house,t1.editor_in_chief,
        t1.subject_group_id,t2.group_name,t3.name AS semester_name,t4.name AS course_name,t7.name AS
        is_textbook_plan_cn,
        t5.name AS
        textbook_type_cn,t1.specifications_models,t1.appraisal_price,t1.price,t1.discount,t1.subtotal,t1.stock,
        (SELECT GROUP_CONCAT(c2.name) FROM textbook_class_relation c1
        LEFT JOIN base_class c2 ON c1.class_id = c2.id
        WHERE c1.textbook_id = t1.id) AS use_class,
        t6.name AS use_grade,t1.version,
        t1.use_type,t1.publishing_date,t1.is_secd,t1.category,t1.plan_batch,t1.work_total_count,t1.textbook_category
        FROM textbook t1
        LEFT JOIN subject_group t2 ON t1.subject_group_id = t2.id
        LEFT JOIN base_semester t3 ON t1.base_semester_id = t3.id
        LEFT JOIN base_course_subject t4 ON t1.course_subject_id = t4.id
        LEFT JOIN xjr_dictionary_detail t5 ON t1.textbook_type = t5.code AND t5.item_id = 1739209191193636865
        LEFT JOIN base_grade t6 ON t1.grade_id = t6.id
        LEFT JOIN xjr_dictionary_detail t7 ON t1.is_textbook_plan = t7.code AND t7.item_id = 1737360269850038273
        LEFT JOIN xjr_dictionary_detail t8 ON t1.textbook_category = t8.code
        WHERE t1.delete_mark = 0
        <if test="dto.textbookType != null and dto.textbookType != ''">
            and t1.textbook_type = #{dto.textbookType}
        </if>
        <!--        <if test="dto.baseSemesterId != null and dto.baseSemesterId > 0">-->
        <!--            and t1.base_semester_id = #{dto.baseSemesterId}-->
        <!--        </if>-->
        <if test="dto.subjectGroupId != null and dto.subjectGroupId > 0">
            and t1.subject_group_id = #{dto.subjectGroupId}
        </if>
        <if test="dto.courseSubjectId != null and dto.courseSubjectId > 0">
            and t1.course_subject_id = #{dto.courseSubjectId}
        </if>
        <if test="dto.bookName != null and dto.bookName != ''">
            and t1.book_name like concat('%',#{dto.bookName}, '%')
        </if>
        <if test="dto.isTextbookPlan != null and dto.isTextbookPlan != ''">
            and t1.is_textbook_plan = #{dto.isTextbookPlan}
        </if>
        <if test="dto.issn != null and dto.issn != ''">
            and t1.issn like concat('%', #{dto.issn}, '%')
        </if>
        <if test="dto.editorInChief != null and dto.editorInChief != ''">
            and t1.editor_in_chief like concat('%', #{dto.editorInChief}, '%')
        </if>
        <if test="dto.publishingHouse != null and dto.publishingHouse != ''">
            and t1.publishing_house like concat('%', #{dto.publishingHouse}, '%')
        </if>
        order by t3.name desc
    </select>

    <select id="subscriptionList" resultType="com.xjrsoft.module.textbook.vo.TextbookSubscriptionRecordVo">
        SELECT t2.create_date,
               t4.name AS applicant_user,
               t8.issn,
               t8.book_name,
               t8.publishing_house,
               t8.editor_in_chief,
               t8.appraisal_price,
               t5.name AS is_textbook_plan_cn,
               t6.name AS course_name,
               t3.student_subscription_number,
               t3.teacher_subscription_number,
               t3.teacher_reference_number,
               t3.class_ids
        FROM textbook_subscription_record t1
                 LEFT JOIN wf_textbook_subscription t2 ON t1.wf_textbook_subscription_id = t2.id
                 LEFT JOIN wf_textbook_subscription_item t3 ON t3.id = t1.wf_textbook_subscription_item_id
                 LEFT JOIN xjr_user t4 ON t2.applicant_user_id = t4.id
                 LEFT JOIN textbook t8 ON t8.id = t1.textbook_id
                 LEFT JOIN xjr_dictionary_detail t5
                           ON t8.is_textbook_plan = t5.code AND t5.item_id = 1737360269850038273
                 LEFT JOIN base_course_subject t6 ON t8.course_subject_id = t6.id
        WHERE t1.delete_mark = 0
          AND t1.textbook_id = #{id}
    </select>

    <select id="exerciseBookSubscriptionList" resultType="com.xjrsoft.module.textbook.vo.TextbookSubscriptionRecordVo">
        SELECT t2.create_date,
               t4.name                AS applicant_user,
               t5.book_name,
               t3.specifications_models,
               t6.name                as useClass,
               t3.appraisal_price,
               t3.subscription_number as studentSubscriptionNumber
        FROM textbook_subscription_record t1
                 LEFT JOIN wf_exercise_book t2 ON t1.wf_textbook_subscription_id = t2.id
                 LEFT JOIN wf_exercise_book_item t3 ON t3.wf_exercise_book_id = t2.id
                 LEFT JOIN xjr_user t4 ON t2.applicant_user_id = t4.id
                 LEFT JOIN textbook t5 ON t5.id = t1.textbook_id
                 LEFT JOIN base_class t6 ON t6.id = t2.class_id
        WHERE t1.delete_mark = 0
          AND t1.textbook_id = #{id}
    </select>

    <select id="getClassInfo" resultType="com.xjrsoft.module.textbook.vo.TextbookSubscriptionClassVo">
        SELECT NAME FROM base_class WHERE id IN
        <foreach item="classId" index="index" collection="ids" open="(" close=")" separator=",">
            #{classId}
        </foreach>
    </select>

    <select id="getInfoByissn" resultType="com.xjrsoft.module.textbook.vo.TextbookVo">
        SELECT *
        FROM textbook
        WHERE delete_mark = 0
          AND issn = #{issn}
        order by create_date desc
        limit 0,1
    </select>

    <select id="warehouseList" resultType="com.xjrsoft.module.textbook.vo.TextbookWarehouseRecordListVo">
        SELECT t1.create_date AS warehouse_date, t2.name AS warehouse_user, t1.warehouse_number, t3.name as source
        FROM textbook_warehouse_record t1
                 LEFT JOIN xjr_user t2 ON t1.create_user_id = t2.id
                 LEFT JOIN xjr_dictionary_detail t3 ON t1.source = t3.code and item_id = 1739821114335490050
        WHERE t1.delete_mark = 0
          AND t1.textbook_id = #{id}
        order by t1.sort_code
    </select>

    <select id="issueList" resultType="com.xjrsoft.module.textbook.vo.TextbookIssueRecordListVo">
        SELECT t1.create_date                        AS issue_date,
               t2.name                               AS issue_user,
               t1.issue_number,
               t6.name                               AS issue_mode,
               CONCAT(ifnull(t4.name, ''), ifnull(t5.name, ''), ' ', ifnull(t7.name, ''),
                      ifnull(t9.recede_address, '')) as remark
        FROM textbook_issue_record t1
                 LEFT JOIN xjr_user t2 ON t1.create_user_id = t2.id
                 left join base_student_school_roll t3 on t3.user_id = t1.receive_user_id
                 left join base_class t4 on t4.id = t3.class_id
                 left join base_class t5 on t5.teacher_id = t1.issue_user_id
                 LEFT JOIN xjr_dictionary_detail t6 ON t6.code = t1.issue_mode AND t6.item_id = 1739821685805215745
                 left join xjr_user t7 on t1.receive_user_id = t7.id
                 left join wf_textbook_recede t9 on t9.id = t1.data_id
        WHERE t1.delete_mark = 0
          AND t1.textbook_id = #{id}
        ORDER BY t1.create_date;
    </select>

    <select id="getClassRelation" resultType="com.xjrsoft.module.textbook.entity.TextbookClassRelation">
        SELECT *
        FROM textbook_class_relation
        WHERE delete_mark = 0
          AND textbook_id = #{id}
    </select>

    <select id="listTextbookStandingExportQuery"
            parameterType="com.xjrsoft.module.textbook.dto.TextbookStandingExportQuerytDto"
            resultType="com.xjrsoft.module.textbook.vo.TextbookStandingExportQueryVo">
        SELECT t1.issn,
        t1.book_name,
        t1.publishing_house,
        t1.editor_in_chief,
        t1.version,
        t2.group_name,
        t4.name AS course_name,
        t7.name AS is_textbook_plan_cn,
        t5.name AS textbook_type_cn,
        t1.specifications_models,
        t1.appraisal_price,
        t1.price,
        t1.discount,
        t1.subtotal,
        t1.stock
        FROM textbook t1
        LEFT JOIN subject_group t2 ON t1.subject_group_id = t2.id
        LEFT JOIN base_course_subject t4 ON t1.course_subject_id = t4.id
        LEFT JOIN xjr_dictionary_detail t5 ON t1.textbook_type = t5.code AND t5.item_id = 1739209191193636865
        LEFT JOIN base_grade t6 ON t1.grade_id = t6.id
        LEFT JOIN xjr_dictionary_detail t7 ON t1.is_textbook_plan = t7.code AND t7.item_id = 1737360269850038273
        LEFT JOIN base_semester t8 ON t1.base_semester_id = t8.id
        WHERE t1.delete_mark = 0
        <if test="dto.textbookType != null and dto.textbookType != ''">
            and t1.textbook_type = #{dto.textbookType}
        </if>
        <if test="dto.baseSemesterId != null and dto.baseSemesterId > 0">
            and t1.base_semester_id = #{dto.baseSemesterId}
        </if>
        <if test="dto.subjectGroupId != null and dto.subjectGroupId > 0">
            and t1.subject_group_id = #{dto.subjectGroupId}
        </if>
        <if test="dto.courseSubjectId != null and dto.courseSubjectId > 0">
            and t1.course_subject_id = #{dto.courseSubjectId}
        </if>
        <if test="dto.bookName != null and dto.bookName != ''">
            and t1.book_name like concat('%',#{dto.bookName}, '%')
        </if>
        <if test="dto.isTextbookPlan != null and dto.isTextbookPlan != ''">
            and t1.is_textbook_plan = #{dto.isTextbookPlan}
        </if>
        <if test="dto.issn != null and dto.issn != ''">
            and t1.issn like concat('%', #{dto.issn}, '%')
        </if>
        order by t6.name desc
    </select>

    <select id="listTextbookSubscriptionExportQuery"
            parameterType="com.xjrsoft.module.textbook.dto.TextbookSubscriptionExportQueryDto"
            resultType="com.xjrsoft.module.textbook.vo.TextbookSubscriptionExportQueryVo">
        SELECT t3.issn,
        t3.book_name,
        t3.publishing_house,
        t3.editor_in_chief,
        t3.appraisal_price,
        t5.name AS is_textbook_plan_cn,
        t6.name AS course_name,
        t9.name as useGrade,
        t3.class_ids,
        t3.student_subscription_number,
        t3.teacher_subscription_number,
        t3.teacher_reference_number,
        t10.group_name,
        t7.name AS is_support_resources_cn
        FROM textbook_subscription_record t1
        LEFT JOIN wf_textbook_subscription t2 ON t1.wf_textbook_subscription_id = t2.id
        LEFT JOIN wf_textbook_subscription_item t3 ON t3.id = t1.wf_textbook_subscription_item_id
        LEFT JOIN xjr_dictionary_detail t5
        ON t3.is_textbook_plan = t5.code AND t5.item_id = 1737360269850038273
        LEFT JOIN base_course_subject t6 ON t3.course_subject_id = t6.id
        LEFT JOIN xjr_dictionary_detail t7
        ON t3.is_support_resources = t7.code AND t7.item_id = 1737360269850038273
        LEFT JOIN textbook t8 ON t8.id = t1.textbook_id
        LEFT JOIN base_grade t9 ON t9.id = t8.grade_id
        LEFT JOIN subject_group t10 ON t10.id = t2.subject_group_id
        WHERE t1.delete_mark = 0
        <if test="dto.textbookType != null and dto.textbookType != ''">
            and t8.textbook_type = #{dto.textbookType}
        </if>
        <if test="dto.baseSemesterId != null and dto.baseSemesterId > 0">
            and t8.base_semester_id = #{dto.baseSemesterId}
        </if>
        <if test="dto.subjectGroupId != null and dto.subjectGroupId > 0">
            and t8.subject_group_id = #{dto.subjectGroupId}
        </if>
        <if test="dto.courseSubjectId != null and dto.courseSubjectId > 0">
            and t8.course_subject_id = #{dto.courseSubjectId}
        </if>
        <if test="dto.bookName != null and dto.bookName != ''">
            and t8.book_name like concat('%',#{dto.bookName}, '%')
        </if>
        <if test="dto.isTextbookPlan != null and dto.isTextbookPlan != ''">
            and t8.is_textbook_plan = #{dto.isTextbookPlan}
        </if>
        <if test="dto.issn != null and dto.issn != ''">
            and t8.issn like concat('%', #{dto.issn}, '%')
        </if>
    </select>

    <select id="listTextbookClaimExportQuery"
            parameterType="com.xjrsoft.module.textbook.dto.TextbookClaimExportQueryDto"
            resultType="com.xjrsoft.module.textbook.vo.TextbookClaimExportQueryVo">
        with textbook_class as
        (select t.base_semester_id,
        t.class_id,
        t1.textbook_id,
        sum(t2.stock) as num,
        sum(t1.issue_number) as claimNum
        from wf_textbook_claim t
        inner join wf_textbook_claim_item t1 on t1.wf_textbook_claim_id = t.id
        left join textbook t2 on t2.id = t1.textbook_id
        where claim_type = 'claim_student'
        AND t.status = 1
        AND t2.delete_mark = 0
        <if test="dto.baseSemesterId != null and dto.baseSemesterId > 0">
            and t.base_semester_id = #{dto.baseSemesterId}
        </if>
        <if test="dto.classId != null and dto.classId > 0">
            and t.class_id = #{dto.classId}
        </if>
        <if test="dto.textbookId != null and dto.textbookId > 0">
            and t1.textbook_id = #{dto.textbookId}
        </if>
        group by t.base_semester_id, t.class_id, t1.textbook_id
        ),
        textbook_recode as
        (select t.base_semester_id,
        t.class_id,
        t1.textbook_id,
        sum(t1.number) as recedeNum
        from wf_textbook_recede t
        inner join wf_textbook_recede_item t1 on t1.wf_textbook_recede_id = t.id
        left join textbook_class t2
        on t2.base_semester_id = t.base_semester_id and t2.class_id = t.class_id and
        t2.textbook_id = t1.textbook_id
        where t.recede_type = 'recede_deans_office'
        AND t.status = 1
        group by t.base_semester_id, t.class_id, t1.textbook_id)
        select t.base_semester_id,
        t3.name as baseSemesterIdCn,
        t.class_id,
        t4.name as classIdCn,
        t5.name as headTeacherName,
        t6.name as classRoomName,
        t.textbook_id,
        t2.name as textbookTypeCn,
        t1.book_name,
        t1.editor_in_chief,
        t1.publishing_house,
        t1.issn,
        t1.price,
        t1.subtotal,
        t.num,
        t.claimNum,
        ifnull(t7.recedeNum, 0) as recedeNum,
        (t.claimNum - ifnull(t7.recedeNum, 0)) as studentClaimNum
        from textbook_class t
        left join textbook t1 on t1.id = t.textbook_id
        left join xjr_dictionary_detail t2 on t2.code = t1.textbook_type
        left join base_semester t3 on t3.id = t.base_semester_id
        left join base_class t4 on t4.id = t.class_id
        left join xjr_user t5 on t5.id = t4.teacher_id
        left join base_classroom t6 on t6.id = t4.classroom_id
        left join textbook_recode t7 on t7.base_semester_id = t.base_semester_id and t7.class_id = t.class_id and
        t7.textbook_id = t.textbook_id
        where t1.delete_mark = 0
        <if test="dto.textbookType != null and dto.textbookType != ''">
            and t1.textbook_type = #{dto.textbookType}
        </if>
        <if test="dto.subjectGroupId != null and dto.subjectGroupId > 0">
            and t1.subject_group_id = #{dto.subjectGroupId}
        </if>
        <if test="dto.courseSubjectId != null and dto.courseSubjectId > 0">
            and t1.course_subject_id = #{dto.courseSubjectId}
        </if>
        <if test="dto.bookName != null and dto.bookName != ''">
            and t1.book_name like concat('%',#{dto.bookName}, '%')
        </if>
        <if test="dto.isTextbookPlan != null and dto.isTextbookPlan != ''">
            and t1.is_textbook_plan = #{dto.isTextbookPlan}
        </if>
        <if test="dto.issn != null and dto.issn != ''">
            and t1.issn like concat('%', #{dto.issn}, '%')
        </if>
        order by t2.name desc
    </select>

    <select id="getSubscriptionPageByClass"
            parameterType="com.xjrsoft.module.textbook.dto.TextbookSubscriptionListDto"
            resultType="com.xjrsoft.module.textbook.vo.TextbookSubscriptionListVo">
        SELECT t3.id, t3.id AS textbookId, t2.name AS courseName, t3.issn, t3.book_name,
        t3.publishing_house, t3.editor_in_chief, t3.course_subject_id, t3.version,
        t3.textbook_type, t3.use_type FROM base_class_course t
        LEFT JOIN base_class t1 ON (t1.id = t.class_id)
        LEFT JOIN base_course_subject t2 ON (t2.id = t.course_id)
        LEFT JOIN textbook t3 ON (t3.id = t.textbook_id)
        LEFT JOIN base_semester t4 ON (t4.id = t.base_semester_id)
        WHERE t.delete_mark = 0 AND t3.delete_mark = 0
        AND (t.base_semester_id = #{dto.baseSemesterId} AND t.class_id in
        <foreach item="classId" index="index" collection="dto.classIdList" open="(" close=")" separator=",">
            #{classId}
        </foreach>
        )
    </select>
</mapper>