<?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.liquidnet.service.goblin.mapper.GoblinStoreOrderMapper">

    <resultMap id="exportMallOrderDaoResult" type="com.liquidnet.service.goblin.dto.MallOrdertDao">
        <result column="order_id" property="orderId"/>
        <result column="order_code" property="orderCode"/>
        <result column="user_mobile" property="userMobile"/>
        <result column="price_express" property="priceExpress"/>
        <result column="price_coupon" property="priceCoupon"/>
        <result column="store_price_coupon" property="storePriceCoupon"/>
        <result column="express_contacts" property="expressContacts"/>
        <result column="express_phone" property="expressPhone"/>
        <result column="express_detail_address" property="expressDetailAddress"/>
        <result column="spu_id" property="spuId"/>
        <result column="name" property="name"/>
        <result column="cate1_name" property="cate1Name"/>
        <result column="cate2_name" property="cate2Name"/>
        <result column="sku_name" property="skuName"/>
        <result column="num" property="num"/>
        <result column="sku_price" property="skuPrice"/>
        <result column="sku_price_actual" property="skuPriceActual"/>
        <result column="order_sku_id" property="orderSkuId"/>
        <result column="pay_type" property="payType"/>
        <result column="pay_time" property="payTime"/>
        <result column="created_at" property="createdAt"/>
        <result column="logistics_company" property="logisticsCompany"/>
        <result column="mail_no" property="mailNo"/>
    </resultMap>

    <select id="exportMallOrder" resultMap="exportMallOrderDaoResult">
        select
        gso.order_id             as order_id,
        gso.order_code             as order_code,
        gso.user_mobile            as user_mobile,
        gso.price_express          as price_express,
        gso.price_coupon           as price_coupon,
        gso.store_price_coupon     as store_price_coupon,
        goa.express_contacts       as express_contacts,
        goa.express_phone          as express_phone,
        CONCAT(goa.express_address,' ',goa.express_address_detail)       as express_detail_address,
        gso.pay_type               as pay_type,
        gso.pay_time               as pay_time,
        gso.created_at             as created_at,
        gm.logistics_company       as logistics_company,
        gm.mail_no                 as mail_no,
        gos.spu_id                 as spu_id,
        gg.name                    as name,
        gg.cate1_name              as cate1_name,
        gg.cate2_name              as cate2_name,
        gos.sku_name               as sku_name,
        gos.num                    as num,
        gos.sku_price              as sku_price,
        gos.sku_price_actual       as sku_price_actual,
        gos.order_sku_id           as order_sku_id
        from goblin_store_order as gso
        inner join goblin_order_sku as gos on gos.order_id = gso.order_id
        inner join (select spu_id, gg.name, gsgc1.name as cate1_name, gsgc2.name as cate2_name
        from goblin_goods as gg
        left join goblin_self_goods_category as gsgc1 on gsgc1.cate_id = gg.cate_fid
        left join goblin_self_goods_category as gsgc2 on gsgc2.cate_id = gg.cate_sid) as gg
        on gos.spu_id = gg.spu_id
        inner join goblin_order_attr as goa on goa.order_id = gso.order_id
        left join goblin_mail as gm on gm.order_id = gso.order_id
        <where>
                and gso.store_id = #{storeId}
            <if test="beginTime != null and endTime != null">
                and gso.created_at between #{beginTime} and #{endTime}
            </if>
            <if test="states != null">
                and gso.status in
                <foreach collection="states" item="item" index="item" open="(" separator="," close=")">
                    ${item}
                </foreach>
            </if>
            <if test="mailType != null and mailType == 1">
                and gm.mail_no is null
            </if>
            <if test="mailType != null and mailType == 2">
                and gm.mail_no is not null
            </if>
        </where>
        group by gos.order_sku_id
    </select>
    <select id="exportZhengzaiMarketOrder" resultType="com.liquidnet.service.goblin.dto.GoblinZhengzaiMarketOrderExcelDto">
        select gos.spu_name                                       spuName,
               gsm.name                                           marketName,
               gsi.store_name                                     storeName,
               gso.order_code                                     orderCode,
               gso.pay_code                                       payCode,
               if(gso.status in (2, 3, 4), '已付款', '已退款')          orderStatus,
               gso.created_at                                     createdAt,
               gos.push_time                                      pushTime,
               if(gso.device_from = 'micropay', 'pos机订单', '线上订单') deviceFrom,
               (case gso.pay_type
                    when 'wepay' then '微信支付'
                    when 'alipay' then '支付宝支付'
                    when 'huifu' then '汇付'
                    when 'unionpay' then '云闪付'
                    when 'pos_crash' then '现金支付'
                    else '0元付' end)                               payType,
               gos.num                                            nums,
               gos.sku_price                                      skuPrice,
               gos.sku_price_actual                               priceActual,
               gos.price_voucher                                  priceVoucher
        from goblin_store_order gso
                 inner join goblin_order_sku gos on gos.order_id = gso.order_id
                 inner join goblin_store_info gsi on gsi.store_id = gso.store_id
                 inner join goblin_self_marketing gsm on gsm.self_market_id = SUBSTRING_INDEX(gso.market_id, 'ZZ', -1)
        <where>
            gso.status not in (0, 5) and gso.store_id = #{storeId,jdbcType=VARCHAR} and gso.market_id = #{marketId,jdbcType=VARCHAR}
            <if test="beginTime != null">
                and gso.created_at <![CDATA[>=]]> #{beginTime,jdbcType=TIMESTAMP}
            </if>
            <if test="endTime != null">
                and gso.created_at <![CDATA[<=]]> #{endTime,jdbcType=TIMESTAMP}
            </if>
        </where>
        order by gso.created_at desc
    </select>

    <select id="exportMarketGoods" resultType="com.liquidnet.service.goblin.dto.GoblinMarketGoodsExcelDto">
        SELECT
        c.name AS 'marketName',
        c.store_name AS 'storeName',
        DATE_FORMAT(c.created_at,'%Y-%m-%d') as 'date',
        spu_no AS 'spuNo' ,
        b.sku_bar_code AS 'skuBarCode' ,
        a.`name` AS 'spuName' ,
        b.`name` AS 'skuName' ,
        gg.cate1_name AS 'gg1' ,
        gg.cate2_name AS 'gg2' ,
        gg.cate3_name AS 'gg3' ,
        b.price AS 'skuPrice' ,
        sum(CASE WHEN c. STATUS not IN(0 , 5) THEN c.num ELSE 0 END) AS 'saleCount' ,
        sum(case when c.status in (6,7) then c.num else 0 end) 'refundCount',
        sum(CASE WHEN c. STATUS not IN(0 , 5) THEN c.sku_price_actual ELSE 0 END ) AS 'salePrice' ,
        sum(CASE WHEN c. STATUS IN(6 , 7) THEN c.sku_price_actual ELSE 0 END) AS 'refundPrice' ,
        sum(CASE WHEN c. STATUS IN(2 , 3 , 4) THEN c.price_voucher ELSE 0 END ) AS 'voucherPrice',
        sum(CASE WHEN c. STATUS IN(2 , 3 , 4) THEN c.num ELSE 0 END ) AS 'count' ,
        sum(CASE WHEN c. STATUS IN(2 , 3 , 4) THEN c.sku_price_actual ELSE 0 END) AS 'price'
        FROM
        goblin_goods AS a
        INNER JOIN goblin_goods_sku AS b ON a.spu_id = b.spu_id
        LEFT JOIN(
        SELECT
        c.sku_id ,
        d.market_id ,
        c. STATUS ,
        c.num ,
        c.sku_price_actual ,
        c.price_voucher,
        c.created_at,
        gsi.store_name,
        gsm.name
        FROM
        goblin_order_sku AS c
        INNER JOIN goblin_store_order AS d ON d.order_id = c.order_id
        inner join goblin_store_info gsi on gsi.store_id = d.store_id
        inner join goblin_self_marketing gsm on gsm.self_market_id = SUBSTRING_INDEX(d.market_id, 'ZZ', -1)
        ) AS c ON SUBSTRING_INDEX(c.sku_id, 'ZZ', 1) = b.sku_id
        INNER JOIN(
        SELECT
        spu_id ,
        gg. NAME ,
        gsgc1. NAME AS cate1_name ,
        gsgc2. NAME AS cate2_name,
        gsgc3. NAME AS cate3_name
        FROM
        goblin_goods AS gg
        LEFT JOIN goblin_self_goods_category AS gsgc1 ON gsgc1.cate_id = gg.cate_fid
        LEFT JOIN goblin_self_goods_category AS gsgc2 ON gsgc2.cate_id = gg.cate_sid
        LEFT JOIN goblin_self_goods_category AS gsgc3 ON gsgc3.cate_id = gg.cate_tid
        ) AS gg ON a.spu_id = gg.spu_id
        <where>
            c.market_id = #{marketId,jdbcType=VARCHAR}
            and a.store_id = #{storeId,jdbcType=VARCHAR}
            and c.created_at  <![CDATA[>=]]> #{beginTime,jdbcType=TIMESTAMP}
            and c.created_at  <![CDATA[<]]> #{endTime,jdbcType=TIMESTAMP}
        </where>
        GROUP BY
        date,
        b.sku_id;
    </select>

    <select id="exportMarketGoodsUnDate" resultType="com.liquidnet.service.goblin.dto.GoblinMarketGoodsUnDateExcelDto">
        SELECT
        c.name AS 'marketName',
        c.store_name AS 'storeName',
        spu_no AS 'spuNo' ,
        b.sku_bar_code AS 'skuBarCode' ,
        a.`name` AS 'spuName' ,
        b.`name` AS 'skuName' ,
        gg.cate1_name AS 'gg1' ,
        gg.cate2_name AS 'gg2' ,
        gg.cate3_name AS 'gg3' ,
        b.price AS 'skuPrice' ,
        sum(CASE WHEN c. STATUS not IN(0 , 5) THEN c.num ELSE 0 END) AS 'saleCount' ,
        sum(case when c.status in (6,7) then c.num else 0 end) 'refundCount',
        sum(CASE WHEN c. STATUS not IN(0 , 5) THEN c.sku_price_actual ELSE 0 END ) AS 'salePrice' ,
        sum(CASE WHEN c. STATUS IN(6 , 7) THEN c.sku_price_actual ELSE 0 END) AS 'refundPrice' ,
        sum(CASE WHEN c. STATUS IN(2 , 3 , 4) THEN c.price_voucher ELSE 0 END ) AS 'voucherPrice',
        sum(CASE WHEN c. STATUS IN(2 , 3 , 4) THEN c.num ELSE 0 END ) AS 'count' ,
        sum(CASE WHEN c. STATUS IN(2 , 3 , 4) THEN c.sku_price_actual ELSE 0 END) AS 'price'
        FROM
        goblin_goods AS a
        INNER JOIN goblin_goods_sku AS b ON a.spu_id = b.spu_id
        LEFT JOIN(
        SELECT
        c.sku_id ,
        d.market_id ,
        c. STATUS ,
        c.num ,
        c.sku_price_actual ,
        c.price_voucher,
        c.created_at,
        gsi.store_name,
        gsm.name
        FROM
        goblin_order_sku AS c
        INNER JOIN goblin_store_order AS d ON d.order_id = c.order_id
        inner join goblin_store_info gsi on gsi.store_id = d.store_id
        inner join goblin_self_marketing gsm on gsm.self_market_id = SUBSTRING_INDEX(d.market_id, 'ZZ', -1)
        ) AS c ON SUBSTRING_INDEX(c.sku_id, 'ZZ', 1) = b.sku_id
        INNER JOIN(
        SELECT
        spu_id ,
        gg. NAME ,
        gsgc1. NAME AS cate1_name ,
        gsgc2. NAME AS cate2_name,
        gsgc3. NAME AS cate3_name
        FROM
        goblin_goods AS gg
        LEFT JOIN goblin_self_goods_category AS gsgc1 ON gsgc1.cate_id = gg.cate_fid
        LEFT JOIN goblin_self_goods_category AS gsgc2 ON gsgc2.cate_id = gg.cate_sid
        LEFT JOIN goblin_self_goods_category AS gsgc3 ON gsgc3.cate_id = gg.cate_tid
        ) AS gg ON a.spu_id = gg.spu_id
        <where>
            c.market_id = #{marketId,jdbcType=VARCHAR}
            and a.store_id = #{storeId,jdbcType=VARCHAR}
            and c.created_at  <![CDATA[>=]]> #{beginTime,jdbcType=TIMESTAMP}
            and c.created_at  <![CDATA[<]]> #{endTime,jdbcType=TIMESTAMP}
        </where>
        GROUP BY
        b.sku_id;
    </select>

</mapper>
