OrderController.php 6.9 KB
<?php
/**
 * Created by PhpStorm.
 * User: yhbr
 * Date: 2018/9/1
 * Time: 9:26
 */

namespace app\admin\controller;
use Think\Db;
use cmf\controller\AdminBaseController;

class OrderController extends AdminBaseController
{

    public function index()
    {
        $where = [];
        $order_sn = request()->param('order_sn');
        if ($order_sn != null) {
            $where['order_sn'] = ['like', "%$order_sn%"];
        }
        $status = request()->param('status');
        if ($status != null) {
            $where['status'] = ['eq', $status];
        }
        $res = Db::name('order_info')->alias('o')
            ->field('o.order_sn,o.id as oid,a.name,s.start_time,s.end_time,o.status,u.user_nickname')
            ->join('activity a', 'a.id=o.activity_id')
            ->join('activity_schedule s', 's.id=o.schedule_id')
            ->join('user u', 'u.id=o.user_id')
            ->where($where)
            ->order('add_time DESC')
            ->paginate(20, false, ['query' => request()->param()]);
        foreach ($res as $k => $v) {
            $v['status'] = getOrderStatusText($v['status']);
            $res[$k] = $v;
        }
        return $this->fetch('', [
            'posts' => $res,
            'page' => $res->render(),
            'order_sn' => $order_sn,
            'status' => $status
        ]);
    }

    public function detail()
    {
        $id = request()->param('id');
        if (request()->isPost()) {
            $act = request()->param('act');
            $status = 0;
            if ($act == 'down_payment') {
                $status = 2;
            } elseif ($act == 'final_payment') {
                $status = 4;
            } else {
                $this->error('操作失败');
            }
            $data = [
                'id' => $id,
                'status' => $status
            ];
            if (Db::name('order_info')->update($data)) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            //订单基本信息
            $orderInfo = Db::name('order_info')->alias('o')
                ->field('o.*,u.user_nickname,a.name,s.start_time,s.end_time')
                ->join('activity a ', 'a.id=o.activity_id')
                ->join('user u', 'u.id=o.user_id')
                ->join('activity_schedule s', 's.id=o.schedule_id')
                ->where(['o.id' => $id])
                ->find();
            if ($orderInfo['is_use_discount_coupon'] == 1 && !empty($orderInfo['discount_coupon_id'])) {
                $orderInfo['coupons'] = Db::name('discount_coupon')->where(['id' => $orderInfo['discount_coupon_id']])->value('discount_coupon_name');
            } else {
                $orderInfo['coupons'] = '未使用';
            }
            $orderInfo['status_text'] = getOrderStatusText($orderInfo['status']);
            //订单详情信息
            $orderDetail = Db::name('order_detail')->alias('d')
                ->field('d.*,e.name,e.tel,e.sex,e.identity,e.wechat')
                ->join('escort e', 'e.id=d.escort_id')
                ->where(['oid' => $id])
                ->select();
            foreach ($orderDetail as $k => $v) {
                if (!empty($v['refund_time'])) {
                    $v['refund_time'] = date('Y-m-d', $v['refund_time']);
                }
                $orderDetail[$k] = $v;
            }
            //订单金额信息
            $orderLog = Db::name('order_log')->field('order_amount,type')->where(['oid' => $id])->select();
            return $this->fetch('', [
                'order_info' => $orderInfo,
                'order_detail' => $orderDetail,
                'order_log' => $orderLog
            ]);
        }
    }

    /**
     * 同意退款
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function agree()
    {
        $request = request();
        if ($request->isPost()) {
            $refund_amount = $request->param('refund_amount');
            $id = $request->param('id');
            if ((Db::name('order_detail')->where(['id' => $id])->value('status')) != 1) {
                $this->error('该退款需求已经处理');
            } else {
                //改变此条状态->给支付人返还指定金额->我的钱包记录退款
                Db::startTrans();
                $orderDetail = [
                    'id' => $id,
                    'status' => 2,
                    'refund_amount' => $request->param('refund_amount'),
                    'refund_time' => time()
                ];
                if (Db::name('order_detail')->update($orderDetail)) {
                    $userId = Db::name('order_detail')->alias('s')
                        ->join('order_info o', 'o.id=s.oid')
                        ->value('user_id');
                    if (Db::name('user')->where(['id' => 2])->setInc('balance', $refund_amount)) {
                        $log = [
                            'user_id' => $userId,
                            'type' => 2,
                            'cost' => $refund_amount,
                            'create_time' => time()
                        ];
                        if (Db::name('my_wallet')->insert($log)) {
                            Db::commit();
                            $this->success('退款成功', url('Order/detail', ['id' => Db::name('order_detail')->where(['id' => $id])->value('oid')]));
                        } else {
                            Db::rollback();
                            $this->error('未知错误');
                        }
                    } else {
                        Db::rollback();
                        $this->error('未知错误');
                    }
                } else {
                    Db::rollback();
                    $this->error('未知错误');
                }
            }
        } else {
            return $this->fetch('', [
                'id' => $request->param('id')
            ]);
        }
    }


    public function disagree()
    {
        $request = request();
        $id = $request->param('id');
        if ($request->isPost()) {
            if ((Db::name('order_detail')->where(['id' => $id])->value('status')) != 1) {
                $this->error('该退款需求已经处理');
            } else {
                $data = [
                    'id' => $id,
                    'status' => 3,
                    'reason' => $request->param('reason')
                ];
                if (Db::name('order_detail')->update($data)) {
                    $this->success('已拒绝退款', url('Order/detail', ['id' => Db::name('order_detail')->where(['id' => $id])->value('oid')]));
                } else {
                    $this->error('未知错误');
                }
            }
        } else {
            return $this->fetch('', [
                'id' => $request->param('id')
            ]);
        }
    }

}