Order.php 9.0 KB
<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 订单管理
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{
    
    /**
     * Order模型对象
     * @var \app\admin\model\Order
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Order;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("statusList1", $this->model->setStatusList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['user','teacher'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['user','teacher'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                $contype = Db::name('territory')
                    ->alias('a')
                    ->join('contype b','a.contype_id = b.id')
                    ->where('a.id',$row['territory_id'])
                    ->find();
                if($contype['type'] == 1){
                    $type = '经营管理';
                }else{
                    $type = '职业发展';
                }
                $row['territory_id'] = $type.':'.$contype['title'];
                $row->getRelation('user')->visible(['nickname']);
				$row->getRelation('teacher')->visible(['name','thumbnail']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            if ($params) {
                $params = $this->preExcludeFields($params);
                $result = false;
                if($params['order_status'] == 1){
                    //查出该项目老师设定的金额   以及该订单的佣金比例
                    $money = Db::name('order')
                        ->alias('a')
                        ->join('territory b','a.territory_id = b.id')
                        ->field('a.teacher_id,a.num,a.order_status,a.commission,b.money')
                        ->where('a.id',$ids)
                        ->find();

                    //本身状态就为1的情况
                    if($money['order_status'] != 1){
                        //查出老师现在有的余额
                        $teacher = Db::name('teacher')
                            ->where('id',$money['teacher_id'])
                            ->field('id,balance,user_id,income')
                            ->find();
                        //添加佣金明细表
                        $record['money'] = $money['money'] - $money['money'] * ($money['commission']/100);
                        $record['type'] = 1;
                        $record['order_num'] = $money['num'];
                        $record['createtime'] = time();
                        $record['teacher_id'] = $money['teacher_id'];
                        $record['user_id'] = $teacher['user_id'];
                        Db::name('record')->insertGetId($record);

                        $gold = $teacher['balance'] + ($money['money'] - $money['money'] * ($money['commission']/100));
                        $income = $teacher['income'] + ($money['money'] - $money['money'] * ($money['commission']/100));
                        Db::name('teacher')->where('id',$money['teacher_id'])->update(['balance'=>$gold,'income'=>$income]);
                        Db::name('order')->where('id',$ids)->update(['finish_status'=>5,'xian'=>3]);
                    }

                }
                if($params['order_status'] == 2){
                    $order = Db::name('order')
                        ->where('id',$ids)
                        ->find();
                    if(empty($params['nopass'])){
                        $this->error('请填写未通过理由');
                    }
                    if($order['is_complaint'] == 1){
                        Db::name('order')->where('id',$ids)->update(['finish_status'=>4,'xian'=>3]);
                    }else{
                        Db::name('order')->where('id',$ids)->update(['finish_status'=>5,'xian'=>3]);
                    }
                }
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validateFailException(true)->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    //学生评价
    public function detail($ids)
    {
        $data = Db::name('comment')
            ->alias('a')
            ->join('user b','a.user_id = b.id')
            ->where('a.order_id',$ids)
            ->field('a.*,b.nickname')
            ->find();
        if(empty($data)){
            $this->error('暂无评价','index');
        }else{
            $data['createtime'] = date('Y-m-d H:i:s',$data['createtime']);
            $this->assign('data',$data);
            return $this->view->fetch();
        }

    }

    //投诉详情
    public function complaint($ids)
    {
        $data = Db::name('complaint')
            ->where('order_id',$ids)
            ->find();
        if(empty($data)){
            $this->error('暂无评价','index');
        }else{
            $data['createtime'] = date('Y-m-d H:i:s',$data['createtime']);
            $this->assign('data',$data);
            return $this->view->fetch();
        }
    }

    //老师总结、
    public function summary($ids)
    {
        $data = Db::name('summarize')
            ->where('order_id',$ids)
            ->find();
        if(empty($data)){
            $this->error('暂无总结','',false);
        }else{
            $name = Db::name('order')
                ->alias('a')
                ->join('teacher b','a.teacher_id = b.id')
                ->where('a.id',$ids)
                ->field('b.name')
                ->find();
            $data['teacher_name'] = $name['name'];
            $data['createtime'] = date('Y-m-d H:i:s',$data['createtime']);
            $this->assign('data',$data);
            return $this->view->fetch();
        }
    }

    //问题描述
    public function content($ids)
    {
        $data = Db::name('order')
            ->where('id',$ids)
            ->field('id,content')
            ->find();
        $this->assign('data',$data);
        return $this->view->fetch();
    }
}