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

    <resultMap type="com.zhongzheng.modules.order.domain.Order" id="OrderResult">
        <result property="orderId" column="order_id"/>
        <result property="orderSn" column="order_sn"/>
        <result property="userId" column="user_id"/>
        <result property="orderPrice" column="order_price"/>
        <result property="orderGeneral" column="order_general"/>
        <result property="orderStatus" column="order_status"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="payTime" column="pay_time"/>
        <result property="payType" column="pay_type"/>
        <result property="transid" column="transid"/>
        <result property="cancelTime" column="cancel_time"/>
        <result property="finishTime" column="finish_time"/>
        <result property="orderType" column="order_type"/>
        <result property="payPrice" column="pay_price"/>
        <result property="chargingType" column="charging_type"/>
        <result property="orderFrom" column="order_from"/>
        <result property="createUsername" column="create_username"/>
        <result property="createSysUserId" column="create_sys_user_id"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.order.vo.OrderListVo" id="OrderResultVo">
        <result property="orderId" column="order_id"/>
        <result property="orderSn" column="order_sn"/>
        <result property="userId" column="user_id"/>
        <result property="orderPrice" column="order_price"/>
        <result property="orderGeneral" column="order_general"/>
        <result property="orderStatus" column="order_status"/>
        <result property="createTime" column="create_time"/>
        <result property="payTime" column="pay_time"/>
        <result property="payType" column="pay_type"/>
        <result property="transid" column="transid"/>
        <result property="cancelTime" column="cancel_time"/>
        <result property="finishTime" column="finish_time"/>
        <result property="orderType" column="order_type"/>
        <result property="payPrice" column="pay_price"/>
        <result property="chargingType" column="charging_type"/>
        <result property="orderFrom" column="order_from"/>
        <result property="status" column="status"/>

        <result property="goodsName" column="goods_name"/>
        <result property="goodsId" column="goods_id"/>
        <result property="year" column="year"/>
        <result property="coverUrl" column="cover_url"/>

        <result property="educationName" column="education_name"/>
        <result property="projectName" column="project_name"/>
        <result property="businessName" column="business_name"/>
        <result property="schoolName" column="school_name"/>
        <result property="categoryName" column="category_name"/>

        <result property="goodsPrice" column="goods_price"/>
        <result property="goodsRealPrice" column="goods_real_price"/>
        <result property="orderGoodsStatus" column="order_goods_status"/>

        <result property="idCard" column="id_card"/>
        <result property="telphone" column="telphone"/>
        <result property="realname" column="realname"/>

        <result property="goodsReceived" column="goods_received"/>
        <result property="refundStatus" column="refund_status"/>
        <result property="payStatus" column="pay_status"/>
        <result property="orderGoodsId" column="order_goods_id"/>
    </resultMap>

    <select id="selectList" parameterType="com.zhongzheng.modules.order.bo.OrderQueryBo" resultMap="OrderResultVo">
        SELECT
            o.*,
            g.goods_id,
            g.goods_name,
            g.year,
            g.cover_url,
            og.goods_price,
            og.order_goods_id,
            og.goods_real_price,
            og.status as order_goods_status,
            u.telphone,
            u.realname,
            u.id_card,
            cet.education_name,
            cpt.project_name,
            cb.business_name,
            s.school_name,
            m.category_name,
            og.goods_received,
            og.refund_status,
            og.pay_status
        FROM
            `order` o
                LEFT JOIN order_goods og ON o.order_sn = og.order_sn
                LEFT JOIN goods g ON og.goods_id = g.goods_id
                LEFT JOIN `user` u ON o.user_id = u.user_id
                LEFT JOIN course_education_type cet ON g.education_type_id = cet.id
                LEFT JOIN course_project_type cpt ON g.project_id = cpt.id
                LEFT JOIN course_business cb ON g.business_id = cb.id
                LEFT JOIN school s ON s.id = g.school_id
                LEFT JOIN major m ON g.major_id = m.id
        WHERE 1=1
        <if test="educationTypeId != null and educationTypeId != ''">
            AND g.education_type_id = #{educationTypeId}
        </if>
        <if test="businessId != null and businessId != ''">
            AND g.business_id = #{businessId}
        </if>
        <if test="schoolId != null and schoolId != ''">
            AND g.school_id = #{schoolId}
        </if>
        <if test="majorId != null and majorId != ''">
            AND g.major_id = #{majorId}
        </if>
        <if test="orderStatus != null ">
            AND o.order_status in
            <foreach collection="orderStatus" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="status != null ">
            AND o.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="startTime != null">
            AND o.create_time >= #{startTime}
        </if>
        <if test="endTime != null">
            AND #{endTime} >= o.create_time
        </if>
        <if test="payStatus != null">
            AND og.pay_status = #{payStatus}
        </if>
        <if test="refundStatus != null">
            AND og.refund_status = #{refundStatus}
        </if>
        <if test="goodsType != null">
            AND g.goods_type = #{goodsType}
        </if>
        <if test="userId != null">
            AND u.user_id = #{userId}
        </if>
        ORDER BY o.order_id DESC
    </select>

    <select id="selectUserClass" parameterType="map" resultType="integer">
        SELECT
            COUNT(cg.grade_id)
        FROM
            class_grade_goods cgg
                LEFT JOIN class_grade_user cgu ON cgu.grade_id = cgg.grade_id
                LEFT JOIN class_grade cg ON cg.grade_id = cgg.grade_id
        WHERE cgg.goods_id =#{goodsId}
          and cgu.user_id=#{userId}
          and cg.`status` =1
    </select>
    <select id="selectListApp" parameterType="com.zhongzheng.modules.order.bo.OrderQueryBo" resultMap="OrderResultVo">
        SELECT
            o.*

        FROM
            `order` o where 1=1
        <if test="userId != null">
            AND o.user_id = #{userId}
        </if>
        <if test="orderStatus != null">
            AND o.order_status in
            <foreach collection="orderStatus" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="status != null">
            AND o.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="startTime != null">
            AND o.create_time >= #{startTime}
        </if>
        ORDER BY o.order_id DESC
    </select>

</mapper>
