# 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_jd = u"""
SELECT  s.jd_order_no '京东订单号',	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 100 THEN '京东' ELSE s.supplier_id	END	) '商户名称',	(CASE s.order_status WHEN 0 THEN '待发货'	WHEN 1 THEN	 '已发货'	WHEN 2 THEN	 '拒单待确认' WHEN 3 THEN 	'已取消' WHEN 4 THEN '申请退货'	WHEN 5 THEN  '已审核' WHEN 6 THEN			'等待用户寄回'		WHEN 7 THEN	 '已验收'		WHEN 8 THEN	 '已评判'		WHEN 9 THEN	 '退货关闭'    WHEN 10 THEN	'用户取消'    WHEN 11 THEN	'供应商取消'	WHEN 12 THEN	 '风控取消' WHEN 11 THEN  '供应商取消' WHEN 13 THEN  '退货成功'  ELSE 	s.order_status END	) '订单状态',
(
      CASE s.payment_type
      	WHEN 1 THEN
      	    '现金支付'
      	WHEN 0 THEN
      	    '白条支付'
     END
	) '支付方式'
 FROM	supplier_order s WHERE	s.payment_type=1 AND  s.supplier_id = 100 AND s.created_at >= "%s" AND s.created_at <"%s" ORDER BY	s.supplier_id ASC,	s.mall_order_time ASC;
"""

sql_query_jd_change = """
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 100 THEN
			"京东"
		ELSE
			s.supplier_id
		END
	) "商户名称",
	(
		CASE s.order_status
		WHEN 0 THEN
			"待发货"
		WHEN 1 THEN
			"已发货"
		WHEN 2 THEN
			"拒单待确认"
		WHEN 3 THEN
			"已取消"
		WHEN 4 THEN
			"申请退货"
		WHEN 5 THEN
			"已审核"
		WHEN 6 THEN
			"等待用户寄回"
		WHEN 7 THEN
			"已验收"
		WHEN 8 THEN
			"已评判"
		WHEN 9 THEN
			"退货关闭"
    WHEN 10 THEN
			"用户取消"
    WHEN 11 THEN
			"供应商取消"
		WHEN 12 THEN
			"风控取消"
		WHEN 11 THEN
			"供应商取消"
		WHEN 13 THEN
			"退货成功"
		ELSE
			s.order_status
		END
	) "订单状态",
	(
      CASE s.payment_type
      	WHEN 1 THEN
      	    "现金支付"
      	WHEN 0 THEN
      	    "白条支付"
      	END
	) "支付方式"
FROM
	supplier_order s
INNER JOIN supplier_order_status_history h ON s.id = h.supplier_order_id
WHERE
s.payment_type=1 AND
s.supplier_id = 100 AND
h.created_at >= "%s" AND h.created_at <"%s"
ORDER BY

	s.mall_order_time ASC,
s.mall_order_no ;
"""

excel_file = pd.ExcelWriter('京东订单%s_%s.xlsx' % (dates[0], dates[1]))
df = pd.read_sql(sql=sql_query_jd % (dates[0], dates[1]), con=engine_merchant)
df.to_excel(excel_file,sheet_name="订单", columns=['京东订单号', '姓名', '手机号', '商品名称', '下单时间', '进货价', '售价', '订单号', '商户名称', '订单状态','支付方式'],
            index=None,encoding='utf8')

df_change = pd.read_sql(sql=sql_query_jd_change % (dates[0], dates[1]), con=engine_merchant)
df_change.to_excel(excel_file, sheet_name="订单变化", columns=['京东订单号', '姓名', '手机号', '商品名称', '下单时间', '进货价', '售价', '订单号', '商户名称', '订单状态','支付方式'],
            index=None,encoding='utf8')
excel_file.save()
