<?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.order.mapper.OrderGoodsMapper">

    <resultMap type="com.zhongzheng.modules.order.domain.OrderGoods" id="OrderGoodsResult">
        <result property="id" column="id"/>
        <result property="orderSn" column="order_sn"/>
        <result property="goodsType" column="goods_type"/>
        <result property="goodsId" column="goods_id"/>
        <result property="goodsPrice" column="goods_price"/>
        <result property="num" column="num"/>
        <result property="createTime" column="create_time"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="goodsName" column="goods_name"/>
        <result property="categoryName" column="category_name"/>
    </resultMap>

    <select id="queryOrderGoodsList" resultMap="OrderGoodsResult">
        SELECT
            temp.*,
            mc.category_name
        FROM
            (
                SELECT
                    og.*,
                    CASE

                        WHEN og.goods_type = 1 THEN
                            c.course_name
                        WHEN og.goods_type = 2 THEN
                            qb.bank_name
                        WHEN og.goods_type = 3 THEN
                            en.`name` ELSE '充值商品'
                        END goods_name,
                    CASE

                        WHEN og.goods_type = 1 THEN
                            c.category_id
                        WHEN og.goods_type = 2 THEN
                            qb.category_id
                        WHEN og.goods_type = 3 THEN
                            en.category_id ELSE 0
                        END category_id,
                    CASE

                        WHEN og.goods_type = 1 THEN
                            c.cover_url
                        WHEN og.goods_type = 2 THEN
                            qb.cover_url
                        WHEN og.goods_type = 3 THEN
                            en.cover_url ELSE NULL
                        END cover_url
                FROM
                    order_goods og
                        LEFT JOIN course c ON og.goods_id = c.course_id
                        AND og.goods_type = 1
                        LEFT JOIN question_bank qb ON og.goods_id = qb.bank_id
                        AND og.goods_type = 2
                        LEFT JOIN exam_note en ON og.goods_id = en.file_id
                        AND og.goods_type = 3
            ) temp
                LEFT JOIN major_category mc ON temp.category_id = mc.category_id
        <if test="orderSn != null and orderSn != ''" >
            WHERE
            temp.order_sn = #{orderSn}
        </if>
    </select>

</mapper>
