# coding=utf-8
from __future__ import unicode_literals
import pandas as pd
from sqlalchemy import create_engine
import sys

reload(sys)
sys.setdefaultencoding('utf8')
engine_merchant = create_engine('mysql+mysqldb://bowang:in78u6ytgbhj56r@192.168.4.6:8066/merchant?charset=utf8',
                                echo=True)

'''
@author: 王博
功能：
'''

dates = ["2017-07-19", "2017-07-21"]

# 所有上传过的物流单号
sql_query_3c_upload = u"""
SELECT
	s.user_name "姓名",
	s.user_phone "手机号",
	s.goods_name "商品名称",
	s.mall_order_time "下单时间",
	s.supplier_amount "进货价",
	s.goods_amount "售价",
	s.mall_order_no "订单号",
	h.created_at "发货时间",
	(
		CASE s.supplier_id
		WHEN 1 THEN
			"隆途"
		WHEN 1 THEN
			"隆途"
		WHEN 2 THEN
			"中商"
		WHEN 3 THEN
			"易道时代"
		WHEN 4 THEN
			"网易考拉"
		WHEN 5 THEN
			"睿祺"
		WHEN 7 THEN
			"珍尚"
		WHEN 6 THEN
			"上海元趣"
		WHEN 8 THEN
			"新奢汇"
		WHEN 9 THEN
			"智博宏远"
		WHEN 10 THEN
			"创实天行"
		WHEN 11 THEN
			"宏威科锐"
		WHEN 13 THEN
			"阿尔卡特"
		WHEN 15 THEN
			"大有元吉"
		WHEN 17 THEN
			"云南世达"
		ELSE
			s.supplier_id
		END
	) "商户名称",
	(
      CASE s.payment_type
      	WHEN 1 THEN
      	    '现金支付'
      	WHEN 0 THEN
      	    '白条支付'
     END
	) '支付方式',
	h.logistics_order_no "物流单号",
	h.express_company "物流",
	s.supplier_id
FROM
	supplier_order s
INNER JOIN supplier_order_logistics h ON s.id = h.order_id
WHERE
s.payment_type=1
group by s.mall_order_no
ORDER BY
	s.supplier_id ASC,
	s.mall_order_time ASC;
"""

# 时间段内所有修改为退货成功的订单
sql_query_3c_refund = """
SELECT
	s.user_name "姓名",
	s.user_phone "手机号",
	s.goods_name "商品名称",
	s.mall_order_time "下单时间",
	s.supplier_amount "进货价",
	s.goods_amount "售价",
	s.mall_order_no "订单号",
	(
		CASE s.supplier_id
		WHEN 1 THEN
			"隆途"
		WHEN 1 THEN
			"隆途"
		WHEN 2 THEN
			"中商"
		WHEN 3 THEN
			"易道时代"
		WHEN 4 THEN
			"网易考拉"
		WHEN 5 THEN
			"睿祺"
		WHEN 7 THEN
			"珍尚"
		WHEN 6 THEN
			"上海元趣"
		WHEN 8 THEN
			"新奢汇"
		WHEN 9 THEN
			"智博宏远"
		WHEN 10 THEN
			"创实天行"
		WHEN 11 THEN
			"宏威科锐"
		WHEN 13 THEN
			"阿尔卡特"
		WHEN 15 THEN
			"大有元吉"
		WHEN 17 THEN
			"云南世达"
		ELSE
			s.supplier_id
		END
	) "商户名称",
	(
      CASE s.payment_type
      	WHEN 1 THEN
      	    '现金支付'
      	WHEN 0 THEN
      	    '白条支付'
     END
	) '支付方式',
	h.created_at "修改时间"
FROM
	supplier_order s
INNER JOIN supplier_order_status_history h ON s.id = h.supplier_order_id
WHERE

h.`status` = 13 AND s.payment_type=1
group by s.mall_order_no
ORDER BY
	s.supplier_id ASC,
	s.mall_order_time ASC;
"""

excel_file = pd.ExcelWriter('3c_.xlsx' )
df = pd.read_sql(sql=sql_query_3c_upload , con=engine_merchant)
df.to_excel(excel_file, sheet_name="上传过物流单号",
            columns=['姓名', '手机号', '商品名称', '下单时间', '进货价', '售价', '订单号', '发货时间', '商户名称', '物流单号', '物流','支付方式'],
            index=None, encoding='utf8')

df_refund = pd.read_sql(sql=sql_query_3c_refund, con=engine_merchant)
df_refund.to_excel(excel_file, sheet_name="退货成功",
                   columns=['姓名', '手机号', '商品名称', '下单时间', '进货价', '售价', '订单号', '发货时间', '商户名称', '修改时间','支付方式'],
                   index=None, encoding='utf8')

excel_file.save()
