<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载:https://gitee.com/likeshop_gitee
// | github下载:https://github.com/likeshop-github
// | 访问官网:https://www.likeshop.cn
// | 访问社区:https://home.likeshop.cn
// | 访问手册:http://doc.likeshop.cn
// | 微信公众号:likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
// |  likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------


namespace app\shop\controller\order;

//use app\shop\logic\order\OrderExportLogic;
use app\common\basics\ShopBase;
use app\common\model\order\OrderLog;
use app\common\model\order\Order as OrderModel;
use app\common\server\JsonServer;
use app\shop\logic\order\OrderLogic;
use app\common\model\Client_;
use app\common\enum\PayEnum;
use app\shop\validate\order\OrderPrintValidate;
use app\shop\validate\order\VirtualDeliveryValidate;

/**
 * 订单管理
 * Class Goods
 */
class Order extends ShopBase
{

    /**
     * @notes 订单列表
     * @return \think\response\Json|\think\response\View
     * @author suny
     * @date 2021/7/14 10:15 上午
     */
    public function lists()
    {

        if ($this->request->isAjax()) {
            $get = $this->request->get();
            return JsonServer::success('', OrderLogic::statistics($get, $this->shop_id));
        }

        $data = OrderLogic::statistics([], $this->shop_id);

        // 订单状态
        $order_status = OrderModel::getOrderStatus(true, $this->shop_id);
        $order_status = OrderLogic::getStat($order_status, $this->shop_id); //拼装数量统计
        // 订单类型
        $order_type = OrderModel::getOrderType(true);
        // 订单来源
        $order_source = Client_::getClient(true);
        // 支付方式
        $pay_way = PayEnum::getPayWay(true);
        // 配送方式
        $delivery_type = OrderModel::getDeliveryType(true);
        $all = OrderLogic::getAll($this->shop_id);
        return view('', [
            'all' => $all,
            'statistics' => $data,
            'order_status' => $order_status,
            'order_type' => $order_type,
            'order_source' => $order_source,
            'pay_way' => $pay_way,
            'delivery_type' => $delivery_type
        ]);
    }

    public function totalCount()
    {
        if ($this->request->isAjax()) {
            return JsonServer::success('获取成功', OrderLogic::totalCount($this->shop_id));
        }
    }

    /**
     * @notes 订单详情
     * @return \think\response\View
     * @author suny
     * @date 2021/7/14 10:15 上午
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\db\exception\DataNotFoundException
     */

    public function detail()
    {

        $id = $this->request->get('id');
        $detail = OrderLogic::getDetail($id);
        $order_log = OrderLog::getOrderLog($id);
        return view('', [
            'detail' => $detail,
            'logs' => $order_log
        ]);
    }

    /**
     * @notes 物流信息
     * @return \think\response\View
     * @author suny
     * @date 2021/7/14 10:15 上午
     */
    public function express()
    {

        $id = $this->request->get('id');
        $detail = OrderLogic::getDetail($id);
        $detail['shipping'] = OrderLogic::shippingInfo($detail['id']);
        return view('', [
            'detail' => $detail
        ]);
    }

    /**
     * @notes 发货
     * @return \think\response\View
     * @author suny
     * @date 2021/7/14 10:15 上午
     */
    public function delivery()
    {

        $id = $this->request->get('id');
        $detail = OrderLogic::getDetail($id);
        $express = OrderLogic::express();
        return view('', [
            'detail' => $detail,
            'express' => $express
        ]);
    }

    /**
     * @notes 发货操作
     * @return \think\response\Json
     * @author suny
     * @date 2021/7/14 10:15 上午
     * @throws \think\exception\DbException
     */
    public function deliveryHandle()
    {

        if ($this->request->isAjax()) {
            $post = $this->request->post();
            (OrderLogic::checkDelivery($post));
            OrderLogic::deliveryHandle($post, $this->admin_id);
            return JsonServer::success('发货成功');
        }
    }

    /**
     * @notes 确认收货
     * @return \think\response\Json
     * @author suny
     * @date 2021/7/14 10:16 上午
     */
    public function confirm()
    {

        if ($this->request->isAjax()) {
            $post = $this->request->post('');
            OrderLogic::confirm($post['order_id'], $this->admin_id);
            return JsonServer::success('确认成功');
        }
    }

    /**
     * @notes 取消订单
     * @return \think\response\Json
     * @author suny
     * @date 2021/7/14 10:16 上午
     */
    public function cancel()
    {

        if ($this->request->isAjax()) {
            $post = $this->request->post('');
            (OrderLogic::checkCancel($post));
            OrderLogic::cancel($post['order_id'], $this->admin_id);
            return JsonServer::success('取消成功');
        }
    }

    /**
     * @notes 删除订单
     * @return \think\response\Json
     * @author suny
     * @date 2021/7/14 10:16 上午
     */
    public function del()
    {

        if ($this->request->isAjax()) {
            $post = $this->request->post('');
            (OrderLogic::checkDel($post));
            OrderLogic::del($post['order_id'], $this->admin_id);
            return JsonServer::success('删除成功');
        }
    }

    /**
     * @notes 商家备注
     * @return \think\response\Json
     * @throws \think\Exception
     * @author suny
     * @date 2021/7/14 10:16 上午
     */
    public function remarks()
    {

        // 获取的
        if ($this->request->isAjax() && $this->request->isGet()) {
            $get = $this->request->get();
            $detail = OrderLogic::remarks($get, 'get');
            return JsonServer::success('获取成功', [$detail]);
        }
        // 提交的
        if ($this->request->isAjax() && $this->request->isPost()) {
            $post = $this->request->post();
            $result = OrderLogic::remarks($post, 'post');
            return json(['code' => 1, 'show' => 0, 'msg' => '修改成功', 'data' => $result]);
        }
    }


    /**
     * @notes 小票打印
     * @return \think\response\Json
     * @author 段誉
     * @date 2022/1/20 11:15
     */
    public function orderPrint()
    {
        $post = $this->request->post();
        $post['shop_id'] = $this->shop_id;
        (new OrderPrintValidate())->goCheck('', $post);
        $result = OrderLogic::orderPrint($post['id'], $this->shop_id);
        if (true === $result) {
            return JsonServer::success('打印成功,如未出小票,请检查打印机是否在线');
        }
        return JsonServer::error($result);
    }

    /**
     * @notes 修改地址
     * @return \think\response\View
     * @author suny
     * @date 2021/7/14 10:16 上午
     */
    public function change_address()
    {

        // 获取的
        $get = $this->request->get();
        $data = OrderLogic::change_address($get);
        $id = json_decode($data['info'], true)['id'];
        return view('', [
            'id' => $id,
            'info' => $data['info'],
            'address_tree' => $data['address_tree']
        ]);
    }

    /**
     * @notes 修改地址提交
     * @return \think\response\Json
     * @author suny
     * @date 2021/7/14 10:16 上午
     */
    public function change_address_post()
    {

        // 提交的
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            OrderLogic::change_address_post($post);
            return JsonServer::success('修改地址成功');
        }
    }


    /**
     * @notes 虚拟发货
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 段誉
     * @date 2022/4/7 17:47
     */
    public function virtualDelivery()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            (new VirtualDeliveryValidate())->goCheck();
            $result = OrderLogic::virtualDelivery($post, $this->admin_id);
            if (false == $result) {
                return JsonServer::error(OrderLogic::getError() ?: '发货失败');
            }
            return JsonServer::success('发货成功');
        }
    }


    /**
     * @notes 导出Excel
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 段誉
     * @date 2022/4/24 10:20
     */
    public function export()
    {
        $params = $this->request->get();
        $result = OrderLogic::statistics($params, $this->shop_id, true);
        if(false === $result) {
            return JsonServer::error(OrderLogic::getError() ?: '导出失败');
        }
        return JsonServer::success('', $result);
    }

}