<?php
namespace app\admin\controller\raise;

use app\admin\controller\BaseController;
use app\admin\server\raise\RaiseOldGoodsOrderServer;

class RaiseOldGoodsOrder extends BaseController
{

    /**
     * Notes:订单列表
     * @return \support\Response
     * User: ZQ
     * Date: 2022/10/14
     */
    public function getOrderList()
    {
        [$page, $limit] = $this->getPage();
        $keywords = $this->request->get('keywords');//订单号
        $result         = RaiseOldGoodsOrderServer::getOrderList($page, $limit, $keywords);
        return json_success($result, '成功');
    }

    /**
     * Notes:订单详情
     * @return \support\Response
     * User: ZQ
     * Date: 2022/11/1
     */
    public function getOrderInfo()
    {
        $order_id = $this->request->get('order_id');
        $result         = RaiseOldGoodsOrderServer::getOrderInfo($order_id);
        if ($result){
            return json_success($result, '获取成功');
        }else{
            throw new \Exception('获取失败！');
        }

    }

    /**
     * Notes:修改订单
     * @return \support\Response
     * User: ZQ
     * Date: 2022/11/1
     */
    public function updateOrder()
    {
        $order_id = $this->request->post('order_id');
        $order_address = $this->request->post('order_address');
        $order_address_mobile = $this->request->post('order_address_mobile');
        $order_address_nickname = $this->request->post('order_address_nickname');
        $order_remke = $this->request->post('order_remke','');
        $order_express = $this->request->post('order_express');
        $order_express_name = $this->request->post('order_express_name');
        $order = RaiseOldGoodsOrderServer::getOrderInfo($order_id);
        if (empty($order)){
            throw new \Exception('订单不存在！');
        }
        if ($order['order_pay_status'] == 0){
            throw new \Exception('订单未支付！');
        }elseif ($order['order_pay_status'] == 2){
            throw new \Exception('订单支付失败！');
        }elseif ($order['order_pay_status'] == 3){
            throw new \Exception('订单已取消！');
        }elseif ($order['order_deliver_id'] == 1 && $order['order_status'] == 1){
            throw new \Exception('订单已发货！');
        }
        $result = RaiseOldGoodsOrderServer::orderUpdate($order_id,$order_address,$order_address_mobile,$order_address_nickname,$order_remke,$order_express,$order_express_name);
        if ($result){
            return json_success($result, '发货成功');
        }else{
            throw new \Exception('发货失败！');
        }

    }

    /**
     * Notes:修改订单支付状态
     * @return \support\Response
     * User: YCP
     * Date: 2022/11/28
     */
    public function updatePayStatus()
    {
        $order_id = $this->request->post('order_id');
        $order_pay_status = $this->request->post('order_pay_status');
        if(!$order_id){
            throw new \Exception('订单ID不能为空');
        }
        $result       = RaiseOldGoodsOrderServer::updatePayStatus($order_id, $order_pay_status);
        return json_success($result, '修改成功');
    }

}
