<?php
/**
 *
 * ============================================================================
 * * 版权所有 蜘蛛出行 * *
 * 网站地址: http://www.zhizhuchuxing.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author By: 倪宗锋
 * PhpStorm OrderController.php
 * Create By 2017/3/9 17:48 $
 */


namespace admin\controllers\order;


use admin\service\order\OrderService;
use admin\util\AdminUtil;
use common\Controller\AdminController;
use common\util\Util;

class OrderController extends AdminController
{
    public $service = null;

    /**
     * Des:获取service
     * Name: service
     * @return OrderService|null
     * @author 倪宗锋
     */
    public function service()
    {
        if ($this->service == null) {
            $this->service = new OrderService();
        }
        return $this->service;
    }

    /**
     * Des:获取订单列表
     * Name: actionGetOrderList
     * @return string
     * @author 倪宗锋
     */
    public function actionGetOrderList()
    {
        $params = [
            'order_id' => $this->_post('order_id', ''),//订单ID
            'category_id' => $this->_post('order_type_id', ''),//类型
            'create_time_start' => $this->_post('start_time', ''),//预定时间 开始
            'create_time_end' => $this->_post('end_time', ''),//预定时间 结束
            'order_status' => $this->_post('order_status_id', ''),//订单状态
            'current_page' => $this->_post('current_page', 1),//显示页编号
            'page_size' => $this->_post('page_size', 20),//每页显示数量
            'fx_uid' => $this->_post('fx_uid', ''),//分销用户ID
            'pay_type' => $this->_post('pay_type', ''),//支付方式 1微信 2支付宝 3余额
            'start_date_start' => $this->_post('start_date_start', ''),//出发日期-起始日期
            'start_date_end' => $this->_post('start_date_end', ''),//出发日期-结束日期
            'main_search' => $this->_post('main_search', ''),//运营负责人
            'contacts_name' => $this->_post('contacts_name', ''),//联系人
            'contacts_phone' => $this->_post('contacts_phone', ''),//联系人手机号
            'user_area' => $this->_post('user_area', ''),//分销商所属区域
        ];
        $result = $this->service()->getOrderList($params);
        return Util::returnJsSu('', $result);
    }

    /**
     * Des:导出订单列表
     * Name: actionExportOrderList
     * @return string
     * @author 付泓程
     */
    public function actionExportOrderList()
    {

        $params = [
            'order_id' => $this->_get('order_id', ''),//订单ID
            'category_id' => $this->_get('order_type_id', ''),//类型
            'create_time_start' => $this->_get('start_time', ''),//预定时间 开始
            'create_time_end' => $this->_get('end_time', ''),//预定时间 结束
            'order_status' => $this->_get('order_status_id', ''),//订单状态
            'current_page' => $this->_get('current_page', 1),//显示页编号
            'page_size' => $this->_get('page_size', 99999),//每页显示数量
            'fx_uid' => $this->_get('fx_uid', ''),//分销用户ID
            'pay_type' => $this->_get('pay_type', ''),//支付方式 1微信 2支付宝 3余额
            'start_date_start' => $this->_get('start_date_start', ''),//出发日期-起始日期
            'start_date_end' => $this->_get('start_date_end', ''),//出发日期-结束日期
            'main_search' => $this->_get('main_search', ''),//运营负责人
            'contacts_name' => $this->_get('contacts_name', ''),//联系人
            'contacts_phone' => $this->_get('contacts_phone', ''),//联系人手机号
            'user_area' => $this->_get('user_area', ''),//分销商所属区域
        ];
        $result = $this->service()->getOrderList($params);
        $order_list = $result['order_list'];
        //设置内存占用
        set_time_limit(0);
        ini_set('memory_limit', '512M');
        //为fputcsv()函数打开文件句柄
        $output = fopen('php://output', 'w') or die("can't open php://output");
        //告诉浏览器这是一个CSV文件
        $filename = "订单列表" . date('Y-m-d', time());
        $filename = iconv("utf-8", "gb2312", $filename);
        setlocale(LC_ALL, 'zh_CN');
        header('Content-Type:application/csv');
        header("Content-Disposition:attachment;filename={$filename}.csv");
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        $table_head = ["订单编号", "预订时间", "订单类型", "预订产品", "产品数量", "支付金额", "出发时间", "联系人", "状态", "支付方式", "订单来源", "分销商区域"];
        foreach ($table_head as $key => $val) {
            $table_head[$key] = iconv("utf-8", "gb2312", $val);
        }
        fputcsv($output, $table_head);
        foreach ($order_list as $k => $v) {
            $v['order_come'] = str_replace('<br/>', ' ', $v['order_come']);
            $e = [];
            $e['order_id'] = $v['order_id'];
            $e['create_order_time'] = $v['create_order_time'];
            $e['order_type_des'] = iconv("utf-8", "gb2312//IGNORE", $v['order_type_des']);
            $book_product = $v['book_product'];
            $book_product = str_replace('——', '-', $book_product);
            $book_product = str_replace('—', '-', $book_product);
            $e['book_product'] = iconv("utf-8", "gb2312//IGNORE", $book_product);
            $e['prod_cnt'] = iconv("utf-8", "gb2312//IGNORE", $v['prod_cnt']);
            $e['total_money'] = iconv("utf-8", "gb2312//IGNORE", "￥") . $v['total_money'];
            $e['start_time'] = $v['start_time'];
            $e['contacts_name'] = iconv("utf-8", "gbk//IGNORE", $v['contacts_name'] . " " . $v['contacts_phone']);
            $e['order_status_des'] = iconv("utf-8", "gb2312//IGNORE", $v['order_status_des']);
            $e['pay_type_des'] = iconv("utf-8", "gb2312//IGNORE", $v['pay_type_des']);
            $e['order_come'] = iconv("utf-8", "gb2312//IGNORE", $v['order_come'] == "" ? "-" : $v['order_come']);
            $e['user_area_name'] = iconv("utf-8", "gb2312//IGNORE", $v['user_area_name'] == "" ? "-" : $v['user_area_name']);
            $v['user_area_name'];
            //输出内容
            fputcsv($output, array_values($e));
        }
        fclose($output) or die("can't close php://output");
        exit();
    }

    /**
     * Des:获取初始化参数
     * Name: actionGetInitialize
     * @return string
     * @author 倪宗锋
     */
    public function actionGetInitialize()
    {
        $result = $this->service()->getInitialize();
        return Util::returnJsSu('', $result);
    }

    /**
     * Des:取消订单
     * Name: actionCancel
     * @return string
     * @author 倪宗锋
     */
    public function actionCancel()
    {
        $order_id = $this->_post('order_id', '');
        $canCel = $this->service()->canCel($order_id, 0);
        return json_encode($canCel);
    }

    /*
     * 后台佣金页参数配置
     */
    public function actionGetBaseType()
    {
        $result = $this->service()->getInitialize();
        return json_encode($result);
    }

    /**
     * Function Description:佣金列表后台
     * Function Name: actionGetCommissionList
     *
     * @return string
     *
     * @author 娄梦宁
     */
    public function actionGetCommissionList()
    {
        $param = [
            'current_page' => $this->_post('current_page', 1),//当前页数
            'page_size' => $this->_post('current_size', 10),//每页条数
            'order_id' => $this->_post('order_id', ''),//订单号
            'order_type_id' => $this->_post('order_type_id', ''),//订单类型
            'create_order_start_date' => $this->_post('create_order_start_date', ''),//下单开始日期
            'create_order_end_date' => $this->_post('create_order_end_date', ''),//下单开始日期
            'order_status_id' => $this->_post('order_status_id', ''),//订单类型
            'settlement_start_date' => $this->_post('settlement_start_date', ''),//结算开始时间
            'settlement_end_date' => $this->_post('settlement_end_date', ''),//结算开始时间
            'fx_uid' => $this->_post('fx_uid', ''),//分销商id
        ];
        $result = $this->service()->GetCommissionList($param);
        return json_encode($result);
    }

    /*
     * 佣金明细
     */
    public function actionGetCommissionDetail()
    {
        $order_id = $this->_post('order_id', '');
        $result = $this->service()->GetCommissionDetail($order_id);
        return json_encode($result);
    }

    /**
     * Function Description:后台提现列表
     * Function Name: actionGetApplyList
     *
     * @return string
     *
     * @author 娄梦宁
     */
    public function actionGetApplyList()
    {
        $param = [
            'apply_user_name' => $this->_post('apply_user_name', ''),//用户名
            'apply_user_phone' => $this->_post('apply_user_phone', ''),//手机号
            'start_time' => $this->_post('start_time', ''),//开始时间
            'end_time' => $this->_post('end_time', ''),//结束时间
            'apply_status' => $this->_post('apply_status_id', ''),//提现状态
            'current_page' => $this->_post('current_page', 1),//当前页数
            'page_size' => $this->_post('page_size', 10),//每页显示个数
        ];
        $result = $this->service()->GetApplyList($param);
        return json_encode($result);
    }

    /*
     * 提现列表基础数据
     */
    public function actionApplyBaseType()
    {
        $type_arr = [
            'apply_status_list' => [
                0 => [
                    'apply_status_id' => '',
                    'apply_status_des' => '全部'
                ],
                1 => [
                    'apply_status_id' => '1',
                    'apply_status_des' => '待处理'
                ],
                2 => [
                    'apply_status_id' => '2',
                    'apply_status_des' => '已审核'
                ]
            ]
        ];
        return Util::returnJsSu('', $type_arr);
    }

    /*
     * 提现审核通过
     */
    public function actionThroughApply()
    {
        $uid = AdminUtil::$uid;
        if (in_array($uid, [269, 763, 832]) == false) {
            return Util::returnJsEr('您没有审核提现的操作权限');
        }
        $apply_id = $this->_post('apply_id');
        $result = $this->service()->throughApply($apply_id);
        return json_encode($result);
    }

    public function actionTest()
    {
        $params = [
            'order_id' => $this->_get('order_id', ''),//订单ID
            'category_id' => $this->_get('order_type_id', ''),//类型
            'create_time_start' => $this->_get('start_time', ''),//预定时间 开始
            'create_time_end' => $this->_get('end_time', ''),//预定时间 结束
            'order_status' => $this->_get('order_status_id', ''),//订单状态
            'current_page' => $this->_get('current_page', 1),//显示页编号
            'page_size' => $this->_get('page_size', 99999),//每页显示数量
            'fx_uid' => $this->_get('fx_uid', ''),//分销用户ID
            'pay_type' => $this->_get('pay_type', ''),//支付方式 1微信 2支付宝 3余额
            'start_date_start' => $this->_get('start_date_start', ''),//出发日期-起始日期
            'start_date_end' => $this->_get('start_date_end', ''),//出发日期-结束日期
            'main_search' => $this->_get('main_search', ''),//运营负责人
            'contacts_name' => $this->_get('contacts_name', ''),//联系人
            'contacts_phone' => $this->_get('contacts_phone', ''),//联系人手机号
            'user_area' => $this->_get('user_area', ''),//分销商所属区域
        ];

        //设置内存占用
        set_time_limit(0);
        ini_set('memory_limit', '512M');

        //告诉浏览器这是一个CSV文件
        $filename = "订单列表" . date('Y-m-d', time());
        $filename = iconv("utf-8", "gb2312", $filename);
        setlocale(LC_ALL, 'zh_CN');
        header('Content-Type:application/csv');
        header("Content-Disposition:attachment;filename={$filename}.csv");
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        $table_head = ["订单编号", "预订时间", "订单类型", "预订产品", "支付金额", "出发时间", "联系人", "状态", "支付方式", "订单来源", "分销商区域"];
        foreach ($table_head as $key => $val) {
            $table_head[$key] = iconv("utf-8", "gb2312", $val);
        }
        $output = fopen('php://output', 'w') or die("can't open php://output");
        fputcsv($output, $table_head);
        ob_flush();
        flush();
        $result = $this->service()->getOrderList($params);
        $order_list = $result['order_list'];
        for ($i = 0; $i < 100; $i++) {
            //为fputcsv()函数打开文件句柄
            foreach ($order_list as $k => $v) {
                $v['order_come'] = str_replace('<br/>', ' ', $v['order_come']);
                $e = [];
                $e['order_id'] = $v['order_id'];
                $e['create_order_time'] = $v['create_order_time'];
                $e['order_type_des'] = iconv("utf-8", "gb2312//IGNORE", $v['order_type_des']);
                $book_product = $v['book_product'];
                $book_product = str_replace('——', '-', $book_product);
                $book_product = str_replace('—', '-', $book_product);
                $e['book_product'] = iconv("utf-8", "gb2312//IGNORE", $book_product);
                $e['total_money'] = iconv("utf-8", "gb2312//IGNORE", "￥") . $v['total_money'];
                $e['start_time'] = $v['start_time'];
                $e['contacts_name'] = iconv("utf-8", "gbk//IGNORE", $v['contacts_name'] . " " . $v['contacts_phone']);
                $e['order_status_des'] = iconv("utf-8", "gb2312//IGNORE", $v['order_status_des']);
                $e['pay_type_des'] = iconv("utf-8", "gb2312//IGNORE", $v['pay_type_des']);
                $e['order_come'] = iconv("utf-8", "gb2312//IGNORE", $v['order_come'] == "" ? "-" : $v['order_come']);
                $e['user_area_name'] = $v['user_area_name'];
                //输出内容
                fputcsv($output, array_values($e));
                ob_flush();
                flush();
            }
        }
        fclose($output) or die("can't close php://output");
        exit();
    }

    public function actionCe()
    {
        $array = range(100, 200);
        $i = 0;
        $newArr = [];
        for ($j = 0; $j < count($array); $j++) {
            $newArr[$i][] = $array[$j];
            if (($j + 1) % 7 == 0) {
                $i++;
            }
        }
        return json_encode($newArr);
    }

    /**
     * Des:修改出发时间
     * Name: changeStartTime
     * @author 倪宗锋
     */
    public function actionChangeStartTime()
    {
        $params = [
            'order_id' => $this->_post('order_id', ''),//订单ID
            'start_time' => $this->_post('start_time', '')
        ];
        if (Util::checkPattern('intVal', $params['order_id']) == false) {
            return Util::returnJsEr('order_id error!');
        }
        $reg = '/^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/';
        if (!Util::checkPattern('', $params['start_time'], $reg)) {
            return Util::returnJsEr('start_time error!');
        }

        $result = $this->service()->changeStartTime($params);
        return json_encode($result);
    }
}