<?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.zhongzheng.modules.goods.mapper.GoodsAttachedMapper">

    <resultMap type="com.zhongzheng.modules.goods.domain.GoodsAttached" id="GoodsAttachedResult">
        <result property="id" column="id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="majorId" column="major_id"/>
        <result property="sort" column="sort"/>
        <result property="type" column="type"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.goods.vo.GoodsAttachedVo" id="GoodsAttachedResultVo">
        <result property="id" column="id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="majorId" column="major_id"/>
        <result property="sort" column="sort"/>
        <result property="type" column="type"/>
        <result property="name" column="name"/>
        <result property="subjectName" column="subject_name"/>
    </resultMap>

    <select id="selectList" parameterType="com.zhongzheng.modules.goods.bo.GoodsAttachedQueryBo" resultMap="GoodsAttachedResultVo">
        SELECT
            ga.*,
            CASE

                WHEN ga.type = 1 THEN
                    qm.module_name
                WHEN ga.type = 2 THEN
                    qc.`name`
                WHEN ga.type = 3 THEN
                    e.exam_name
                END name,
            CASE

                WHEN ga.type = 1 THEN
                    cs.subject_name
                WHEN ga.type = 2 THEN
                    cs1.subject_name
                WHEN ga.type = 3 THEN
                    cs2.subject_name
                END subject_name
        FROM
            goods_attached ga
                LEFT JOIN question_module qm ON ga.major_id = qm.module_exam_id
                AND ga.type = 1
                LEFT JOIN question_business qb ON qb.major_id = qm.module_exam_id
                AND qb.type = 4
                LEFT JOIN course_subject cs ON qb.subject_id = cs.id
                LEFT JOIN question_chapter qc ON ga.major_id = qc.chapter_exam_id
                AND ga.type = 2
                LEFT JOIN question_business qb1 ON qb1.major_id = qc.chapter_exam_id
                AND qb1.type = 3
                LEFT JOIN course_subject cs1 ON qb1.subject_id = cs1.id
                LEFT JOIN exam e ON ga.major_id = e.exam_id
                AND ga.type = 3
                LEFT JOIN question_business qb2 ON qb2.major_id = e.exam_id
                AND qb2.type = 2
                LEFT JOIN course_subject cs2 ON qb2.subject_id = cs2.id
        WHERE
            ga.goods_id = #{goodsId}
    </select>
</mapper>
