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

namespace admin\service\order;

use admin\util\AdminUtil;
use common\models\BaseCategory;
use common\models\FxCommissionApply;
use common\models\FxCommissionInfo;
use common\models\FxUser;
use common\models\FxUserAccount;
use common\models\FxUserArea;
use common\models\MainCorpUser;
use common\models\OrderMain;
use common\util\OrderUtil;
use common\util\Util;
use yii\helpers\ArrayHelper;
use Yii;

class OrderService
{
    /**
     * Des:获取订单列表
     * Name: getOrderList
     * @param $params
     * @return mixed
     * @author 倪宗锋
     */
    public function getOrderList($params)
    {
        $orderMain = new OrderMain();
        $orderCount = (int)$orderMain->getOrderListAdm($params, 1);
        $info = $orderMain->getOrderListAdm($params, 3);
        $orderValidity = (int)$info['cnt'];
        $totalMoney = number_format($info['total_money'], 2);
        $return['order_list'] = $orderMain->getOrderListAdm($params, 2);
        $return['order_validity_count'] = $orderValidity;
        $return['GMV'] = $totalMoney;
        $return['page'] = [
            'current_page' => $params['current_page'],
            'total_pages' => ceil($orderCount / $params['page_size']),
            'total_count' => $orderCount,
            'page_size' => $params['page_size']
        ];
        return $return;
    }

    /**
     * Des:获取初始化参数
     * Name: getInitialize
     * @return array
     * @author 倪宗锋
     */
    public function getInitialize()
    {
        $baseCategory = new BaseCategory();
        $return['order_type_list'] = ArrayHelper::merge(
            [
                [
                    "order_type_id" => "",
                    "order_type_des" => "全部"
                ]
            ],
            $baseCategory->getShowList()
        );
        $return["order_status_list"] = [
            [
                "order_status_id" => "",
                "order_status_des" => "全部"
            ],
            [
                "order_status_id" => "2",
                "order_status_des" => "已支付"
            ],
            [
                "order_status_id" => "3",
                "order_status_des" => "已完成"
            ],
            [
                "order_status_id" => "4",
                "order_status_des" => "已退款"
            ],
        ];
        $fxUser = new FxUser();
        $param["current_page"] = "1";
        $param['page_size'] = PHP_INT_MAX;
        //订单来源
        $list = $fxUser->getUserList($param);
        $return['fx_user_list'] = $list['list'];
        //运营负责人
        $mainCorp = new MainCorpUser();
        $return['main_corp_list'] = $mainCorp->getListForUserMainCorp();
        //分销商区域
        $fxUserArea = new FxUserArea();
        $return['user_area_list'] = $fxUserArea->getUserAreaList();
        return $return;
    }

    /**
     * Des:取消订单入口
     * Name: canCel
     * @param $order_id
     * @param $is_check
     * @return array
     * @author 倪宗锋
     */
    public function canCel($order_id, $is_check = 1)
    {
        if (Util::checkPattern('intVal', $order_id) == false) {
            return Util::returnArrEr('参数错误！');
        }
//        $orderMain  = new OrderMain();
//        $orderInfo = $orderMain->getOrderInfo($order_id);
//        if (empty($orderInfo['order_id'])) {
//            return Util::returnArrEr('订单不存在！');
//        }
//        if ($orderInfo['category_id'] == 3) {
//            return Util::returnArrEr('酒店订单不可以取消！');
//        }
        $addFlag = OrderUtil::applyCancelOrder($order_id, $is_check);//添加记录到取消订单表
        return $addFlag;
    }

    /**
     * Function Description:佣金列表数据获取
     * Function Name: GetCommissionList
     * @param $param
     *
     * @return array
     *
     * @author 娄梦宁
     */
    public function GetCommissionList($param)
    {
        $order_main = new OrderMain();
        $commissionList = $order_main->AdminCommissionList($param);
        $page_size = $param['page_size'];
        $total_count = $commissionList['count'];
        $total_pages = ceil($total_count / $page_size);
        $result = [
            'prod_list' => $commissionList['list'],
            'page' => [
                'current_page' => (string)$param['current_page'],
                'total_pages' => (string)$total_pages,
                'page_size' => (string)$page_size,
                'total_count' => (string)$total_count,
            ]
        ];
        return Util::returnArrSu('', $result);
    }

    /*
     * 佣金明细
     */
    public function GetCommissionDetail($order_id)
    {
        $fx_commission_info = new FxCommissionInfo();
        $CommissionInfo = $fx_commission_info->getInfoAdmin($order_id);
        $result = ['detail' => $CommissionInfo];
        return Util::returnArrSu('', $result);
    }

    /**
     * Function Description:后台提现申请列表
     * Function Name: GetApplyList
     * @param $param
     *
     * @return array
     *
     * @author 娄梦宁
     */
    public function GetApplyList($param)
    {
        $fx_commission_apply = new FxCommissionApply();
        $applyList = $fx_commission_apply->ApplyList($param);
        $page_size = $param['page_size'];
        $total_count = $applyList['count'];
        $total_pages = ceil($total_count / $page_size);
        $result = [
            'prod_list' => $applyList['list'],
            'page' => [
                'current_page' => (string)$param['current_page'],
                'total_pages' => (string)$total_pages,
                'page_size' => (string)$page_size,
                'total_count' => (string)$total_count,
            ]
        ];
        return Util::returnArrSu('', $result);
    }

    /**
     * Function Description:提现审核后的操作
     * Function Name: throughApply
     * @param $apply_id
     *
     * @return array
     *
     * @author 娄梦宁
     */
    public function throughApply($apply_id)
    {
        $fx_commission_apply = new FxCommissionApply();
        $fx_user_account = new FxUserAccount();
        $transaction = Yii::$app->db->beginTransaction();
        //提现表操作
        $applyChange = $fx_commission_apply->applyChange($apply_id);
        if ($applyChange == 1) {
            $changeAccount = $fx_user_account->changeAccount($apply_id);
            if ($changeAccount) {
                $transaction->commit();
                return Util::returnArrSu();
            }
        }
        $transaction->rollBack();
        return Util::returnArrEr('数据库错误');
    }

    /**
     * Des:修改订单出发时间
     * Name: changeStartTime
     * @param $params
     * @return array
     * @author 倪宗锋
     */
    public function changeStartTime($params)
    {
        $orderMain = new OrderMain();
        $orderInfo = $orderMain->getOrderInfo($params['order_id']);
        if (empty($orderInfo['order_id'])) {
            return Util::returnArrEr('订单不存在！');
        }
        if ($params['start_time'] == $orderInfo['start_date'] . ' ' . $orderInfo['start_time']) {
            return Util::returnArrSu();
        }
        $value = [
            'start_date' => date('Y-m-d', strtotime($params['start_time'])),
            'start_time' => date('H:i:s', strtotime($params['start_time']))
        ];
        $changeMoney = $orderMain->upOrder($value, [$params['order_id']]);
        if ($changeMoney) {
            $memo = '修改前：' . $orderInfo['start_date'] . ' ' . $orderInfo['start_time'] . '修改后:' . $params['start_time'];
            Util::addUserOrderLog('修改出游时间', $memo, AdminUtil::$uid,4, $params['order_id'], 2);
            return Util::returnArrSu();
        } else {
            return Util::returnArrEr('修改出发时间失败！');
        }
    }


}