<?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="cn.qihangerp.mapper.OOrderItemMapper">

    <resultMap id="BaseResultMap" type="cn.qihangerp.model.entity.OOrderItem">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="shopId" column="shop_id" jdbcType="BIGINT"/>
            <result property="shopType" column="shop_type" jdbcType="INTEGER"/>
            <result property="orderId" column="order_id" jdbcType="BIGINT"/>
            <result property="orderNum" column="order_num" jdbcType="VARCHAR"/>
            <result property="subOrderNum" column="sub_order_num" jdbcType="VARCHAR"/>
            <result property="productId" column="product_id" jdbcType="VARCHAR"/>
            <result property="skuId" column="sku_id" jdbcType="VARCHAR"/>
            <result property="goodsId" column="goods_id" jdbcType="BIGINT"/>
            <result property="goodsSkuId" column="goods_sku_id" jdbcType="BIGINT"/>
            <result property="goodsTitle" column="goods_title" jdbcType="VARCHAR"/>
            <result property="goodsImg" column="goods_img" jdbcType="VARCHAR"/>
            <result property="goodsNum" column="goods_num" jdbcType="VARCHAR"/>
            <result property="goodsSpec" column="goods_spec" jdbcType="VARCHAR"/>
            <result property="skuNum" column="sku_num" jdbcType="VARCHAR"/>
            <result property="goodsPrice" column="goods_price" jdbcType="DOUBLE"/>
            <result property="goodsAmount" column="goods_amount" jdbcType="DOUBLE"/>
            <result property="itemAmount" column="item_amount" jdbcType="DOUBLE"/>
            <result property="sellerDiscount" column="seller_discount" jdbcType="DOUBLE"/>
            <result property="platformDiscount" column="platform_discount" jdbcType="DOUBLE"/>
            <result property="payDiscount" column="pay_discount" jdbcType="DOUBLE"/>
            <result property="changeAmount" column="change_amount" jdbcType="DOUBLE"/>

            <result property="payment" column="payment" jdbcType="DOUBLE"/>
            <result property="quantity" column="quantity" jdbcType="INTEGER"/>
            <result property="remark" column="remark" jdbcType="VARCHAR"/>
            <result property="refundCount" column="refund_count" jdbcType="INTEGER"/>
            <result property="refundStatus" column="refund_status" jdbcType="INTEGER"/>
            <result property="shipper" column="shipper" jdbcType="BIGINT"/>
            <result property="shipType" column="ship_type" jdbcType="INTEGER"/>
            <result property="shipStatus" column="ship_status" jdbcType="INTEGER"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
            <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
            <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
            <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,shop_id,shop_type,
        order_id,order_num,sub_order_num,
        product_id,sku_id,goods_id,goods_sku_id,
        goods_title,goods_img,goods_num,
        goods_spec,sku_num,goods_price,goods_amount,
        item_amount,seller_discount,platform_discount,pay_discount,change_amount,payment,
        quantity,remark,refund_count,refund_status,
          shipper,ship_type,ship_status,create_time,
        create_by,update_time,update_by
    </sql>

    <select id="selectPageVo" parameterType="cn.qihangerp.model.bo.OrderItemListBo" resultType="cn.qihangerp.model.vo.OrderItemListVo">
        SELECT
        oi.*,o.shop_id,o.order_create_time as order_time,gs.outer_erp_sku_id
        FROM o_order_item as oi
        left join o_order as o on o.id = oi.order_id
        left join o_goods_sku as gs on gs.id = oi.goods_sku_id
        WHERE 1=1
        <if test="qw.shopId!=null">
            and o.shop_id = #{qw.shopId}
        </if>
        <if test="qw.orderNum!=null and qw.orderNum !=''">
            and oi.order_num = #{qw.orderNum}
        </if>
        <if test="qw.subOrderNum != null and qw.subOrderNum !=''">
            and oi.sub_order_num = #{qw.subOrderNum}
        </if>
        <if test="qw.refundStatus!=null">
            and oi.refund_status = #{qw.refundStatus}
        </if>
        <if test="qw.startTime!=null and qw.startTime != ''">
            and o.order_create_time &gt;= #{qw.startTime}
        </if>
        <if test="qw.endTime!=null and qw.endTime != ''">
            and o.order_create_time &lt;= #{qw.endTime}
        </if>
        <if test="qw.hasLink != null and qw.hasLink == 0">
            AND (oi.goods_sku_id is NULL or oi.goods_sku_id = 0)
        </if>
        <if test="qw.hasLink != null and qw.hasLink == 1">
            AND oi.goods_sku_id is NOT NULL and oi.goods_sku_id != 0
        </if>

    </select>


    <select id="selectTopSku" resultType="cn.qihangerp.model.vo.SalesTopSkuVo">
        SELECT
            sku_num as `name`,
            SUM(quantity) AS `value`
        FROM
            o_order_item
        WHERE
            create_time BETWEEN #{startDate} AND #{endDate}
          AND sku_num IS NOT NULL AND sku_num != ''
        GROUP BY
            sku_num
        ORDER BY
            `value` DESC
            LIMIT 10;
    </select>
</mapper>
