<?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="orderGoodsId" column="order_goods_id"/>
        <result property="orderSn" column="order_sn"/>
        <result property="goodsId" column="goods_id"/>
        <result property="goodsInputData" column="goods_input_data"/>
        <result property="goodsPrice" column="goods_price"/>
        <result property="goodsRealPrice" column="goods_real_price"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
        <result property="goodsReceived" column="goods_received"/>
        <result property="refundStatus" column="refund_status"/>
        <result property="payStatus" column="pay_status"/>
        <result property="businessOwner" column="business_owner"/>
        <result property="dept" column="dept"/>
        <result property="source" column="source"/>
        <result property="school" column="school"/>
        <result property="address" column="address"/>
        <result property="company" column="company"/>
        <result property="remark" column="remark"/>
        <result property="gradeId" column="grade_id"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.order.vo.OrderGoodsVo" id="OrderGoodsResultVo">
        <result property="orderGoodsId" column="order_goods_id"/>
        <result property="orderSn" column="order_sn"/>
        <result property="goodsId" column="goods_id"/>
        <result property="goodsInputData" column="goods_input_data"/>
        <result property="goodsPrice" column="goods_price"/>
        <result property="goodsRealPrice" column="goods_real_price"/>
        <result property="status" column="status"/>
        <result property="goodsReceived" column="goods_received"/>
        <result property="refundStatus" column="refund_status"/>
        <result property="payStatus" column="pay_status"/>
        <result property="businessOwner" column="business_owner"/>
        <result property="dept" column="dept"/>
        <result property="source" column="source"/>
        <result property="school" column="school"/>
        <result property="address" column="address"/>
        <result property="company" column="company"/>
        <result property="createTime" column="create_time"/>
        <result property="remark" column="remark"/>
        <result property="userId" column="user_id"/>

        <result property="code" column="code"/>
        <result property="goodsName" column="goods_name"/>
        <result property="educationName" column="education_name"/>
        <result property="projectName" column="project_name"/>
        <result property="businessName" column="business_name"/>
        <result property="createBy" column="create_by"/>
        <result property="goodsType" column="goods_type"/>
        <result property="gradeId" column="grade_id"/>
        <result property="coverUrl" column="cover_url"/>
    </resultMap>

    <select id="selectList" parameterType="com.zhongzheng.modules.order.bo.OrderGoodsQueryBo" resultMap="OrderGoodsResultVo">
        SELECT
            og.*,
            g.`code`,
            g.goods_name,
            g.goods_type,
               g.cover_url,
            cet.education_name,
            cpt.project_name,
            cb.business_name
        FROM
            order_goods og
                LEFT JOIN goods g ON og.goods_id = g.goods_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
        WHERE
            1 = 1
            <if test="orderSn != null and orderSn != ''">
                AND og.order_sn =  #{orderSn}
            </if>
            <if test="orderGoodsIds != null and orderGoodsIds.size()!=0 ">
                AND og.order_goods_id in
                <foreach collection="orderGoodsIds" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
    </select>

    <select id="selectDetail" parameterType="com.zhongzheng.modules.order.bo.OrderGoodsQueryBo" resultMap="OrderGoodsResultVo">
        SELECT
        og.*,
        g.`code`,
        g.goods_name,
        g.goods_type,
        cet.education_name,
        cpt.project_name,
        cb.business_name,
        o.create_username as create_by
        FROM
        order_goods og
        LEFT JOIN goods g ON og.goods_id = g.goods_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 `order` o ON og.order_sn = o.order_sn
        WHERE
         og.order_sn =  #{orderSn} AND og.goods_id =  #{goodsId}

    </select>

    <select id="selectOrderGoods" parameterType="com.zhongzheng.modules.order.bo.OrderGoodsQueryBo" resultMap="OrderGoodsResultVo">
        SELECT
        og.*,
        o.user_id
        FROM
        `order` o
        LEFT JOIN order_goods og on o.order_sn = og.order_sn
        where 1=1
        and og.`status` = 1
        and og.refund_status in (0,1,3)
        and og.pay_status in (2,3)
        AND (SELECT COUNT(1) FROM inform_user iu where iu.user_id = o.user_id and iu.goods_id = og.goods_id and system_status = 1) &lt; 1
    </select>

    <select id="selectOrderTwoGoods" parameterType="com.zhongzheng.modules.order.bo.OrderGoodsQueryBo" resultMap="OrderGoodsResultVo">
        SELECT
        og.*,
        o.user_id
        FROM
        `order` o
        LEFT JOIN order_goods og on o.order_sn = og.order_sn
        where 1=1
        and og.`status` = 1
        and og.refund_status in (0,1,3)
        and og.pay_status in (2,3)
        AND (SELECT COUNT(1) FROM inform_user iu where iu.user_id = o.user_id and iu.goods_id = og.goods_id and system_status = 3) &lt; 1

    </select>

    <select id="sendGoods"  resultMap="OrderGoodsResultVo">
        SELECT
        og.*,
        o.user_id
        FROM
        `order` o
        LEFT JOIN order_goods og on o.order_sn = og.order_sn
        where 1=1
        and og.`status` = 1
        and og.refund_status in (0,1,3)
        and og.pay_status in (2,3)
        and (SELECT COUNT(1) FROM user_exam_goods ueg where ueg.user_id = o.user_id and og.goods_id = ueg.goods_id) &lt; 1
        and (SELECT COUNT(1) FROM exam_number en LEFT JOIN exam_number_goods eng on en.exam_number_id = eng.exam_number_id where eng.goods_id = og.goods_id and en.`status` = 1) > 0
    </select>

    <select id="getBuyGoodsHistory" parameterType="com.zhongzheng.modules.order.bo.OrderGoodsQueryBo" resultType="Long">
        SELECT
            ifnull(count(*),0) num
        FROM
            `order` o
                LEFT JOIN order_goods og ON o.order_sn = og.order_sn
        WHERE
            o.user_id = #{userId}
          AND og.goods_id = #{goodsId}
          AND o.order_status > 0

    </select>
    <select id="getBuyGoodsReadyPay" parameterType="com.zhongzheng.modules.order.bo.OrderGoodsQueryBo" resultType="Long">
        SELECT
            ifnull(count(*),0) num
        FROM
            `order` o
                LEFT JOIN order_goods og ON o.order_sn = og.order_sn
        WHERE
            o.user_id = #{userId}
          AND og.goods_id = #{goodsId}
          AND o.order_status = 0

    </select>
</mapper>
