<?php

namespace frontend\controllers;

use common\libs\MyLib;
use common\models\AppointmentHistoryT;
use common\models\AppointmentT;
use common\models\CarBT;
use common\models\CarCT;
use common\models\MeetT;
use common\models\CarDT;
use common\models\CarET;
use common\models\CarInvalidT;
use common\models\CarT;
use common\models\InvalidT;
use common\models\OrderT;
use common\models\UserT;
use Yii;
use yii\data\Pagination;
use yii\web\Response;

class AppointmentController extends BaseController
{
    public $enableCsrfValidation = false;
    public $layout = 'blue-main';

    public function actionIndex()
    {
        $request = Yii::$app->request;
        $car_id = $request->get('car_id');
        $page = $request->get('page',1);

        $query = AppointmentHistoryT::find()
            ->where(['car_id'=>$car_id])
            ->orderBy('id DESC');

        $total = $query->count();

        $pagination = new Pagination(['totalCount' => $total,'pageSize'=>20]);
        $pagination->setPage($page-1);

        $query = $query->offset($pagination->offset)->limit($pagination->limit);
        $items = $query->all();

        $page_info = MyLib::getPageInfo($pagination);
        return $this->render('index',[
            'car_id' => $car_id,
            'items' => $items,
            'page' => $page,
            'page_info' => $page_info
        ]);
    }

    public function actionAjaxIndex()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $car_id = $request->get('car_id');
        $page = $request->get('page',1);

        $result = array();
        $result['success'] = false;
        $result['msg'] = '读取失败';

        $pdate = date("Y-m-d",strtotime("-6 months"));
        $query = AppointmentHistoryT::find()
            ->where(['car_id'=>$car_id])
            ->andWhere('pdate>"'.$pdate.'"')
            ->orderBy('pdate DESC,id DESC');
//echo $query->createCommand()->rawSql;
        $total = $query->count();

        $pagination = new Pagination(['totalCount' => $total,'pageSize'=>20]);
        $pagination->setPage($page-1);

        $query = $query->offset($pagination->offset)->limit($pagination->limit);
        $items = $query->all();

        $page_info = MyLib::getAjaxPageInfo($pagination,'appointment_list');
        $html = $this->renderPartial('ajax-index',[
            'car_id' => $car_id,
            'items' => $items,
            'page' => $page,
            'page_info' => $page_info
        ]);
        $result['success'] = true;
        $result['html'] = $html;
        return $result;
    }

    public function actionIndexJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $car_id = $request->get('car_id');
        $offset = $request->get('offset',0);
        $limit = $request->get('limit', 10);

        $map = [
            'and',
            ['=', 'car_id', $car_id],
            ['>', 'pdate', date("Y-m-d",strtotime("-6 months"))],
        ];
        $order = [
            'pdate' => SORT_DESC,
            'id' => SORT_DESC,
        ];

        $query = AppointmentHistoryT::find()
            ->where($map)
            ->orderBy($order);
        $total = $query->count();
        $query = $query->offset($offset)->limit($limit);
        $items = $query->all();

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($items as $item) {
            $row = $item->toArray();
            $row['show_name'] = $item->user ? $item->user->getShowName() : '';
            $row['business_group_txt'] = $item->businessGroup ? $item->businessGroup->name : '';
            $data['rows'][] = $row;
        }
        return $data;
    }

    public function actionHistoryJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $car_id = $request->get('car_id');
        $offset = $request->get('offset', 0);
        $limit = $request->get('limit', 10);

        $pdate = date("Y-m-d",strtotime("-6 months"));
        $query = AppointmentHistoryT::find()
            ->where(['car_id'=>$car_id])
            ->andWhere('pdate>"'.$pdate.'"')
            ->orderBy('pdate DESC,id DESC');
//echo $query->createCommand()->rawSql;
        $total = $query->count();

        $query = $query->offset($offset)->limit($limit);
        $items = $query->all();

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($items as $item) {
            $row = $item->toArray();
            $row['user'] = $item->user?$item->user->getShowName():'';
            $data['rows'][] = $row;
        }

        return $data;
    }

    public function actionAdd()
    {
        $request = Yii::$app->request;
        $car_id = $request->get('car_id',0);

        return $this->render('add', [
            'car_id' => $car_id
        ]);
    }

    public function actionSave()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '保存失败';

        if($request->isPost) {
            $car_id = $request->post('car_id',0);
            $pdate = $request->post('pdate');
            $ptime = $request->post('ptime');
            $remark = $request->post('remark');
            $ptype = $request->post('ptype',0);
            $business_group_id = $request->post('business_group_id', 0);

            if($pdate == '') {
                $result['msg'] = '请选择预约日期!';
                return $result;
            }
            if($ptime == '') {
                $result['msg'] = '请选择预约时间!';
                return $result;
            }

            $tran = AppointmentT::getDb()->beginTransaction();
            try {
                $car_user_info = null;
                $car_info = CarT::findOne(['id'=>$car_id]);
                switch($car_info->location) {
                    case 2:
                        $car_user_info = CarBT::findOne(['id'=>$car_id]);
                        break;
                    case 3:
                        $car_user_info = CarCT::findOne(['id'=>$car_id]);
                        break;
                    case 4:
                        $car_user_info = CarDT::findOne(['id'=>$car_id]);
                        break;
                    case 5:
                        $car_user_info = CarInvalidT::findOne(['id'=>$car_id]);
                        break;
                    case 6:
                        $car_user_info = CarET::findOne(['id'=>$car_id]);
                        break;
                    default:
                        $name = '';
                }
                if(!$car_user_info) {
                    $result['msg'] = '该车辆未分配';
                    return $result;
                }
//                dd($car_user_info->user_id);
                if($car_user_info->user_id != $this->my->id) {
                    $result['msg'] = '该车辆未分配给'.$this->my->username.', 无法预约';
                    return $result;
                }

                $row = AppointmentT::find()
                    ->where('car_id='.$car_id)
                    ->one();
                if(empty($row)) {
                    $row = new AppointmentT();
                    $row->user_id = $this->my->id;
                }
                $old_user_id = 0;
                if($row->user_id != $this->my->id)
                    $old_user_id = $row->user_id;
                $row->car_id = $car_id;
                $row->user_id = $this->my->id;
                $row->pdate = $pdate;
                $row->ptime = $ptime;
                $row->ptype = $ptype;
                $row->remark = $remark;
                $row->business_group_id = $business_group_id;
                $row->is_first = 0;
                if(!$row->save()) {
                    throw new \Exception(print_r($row->getErrors(), true));
                }

                $h_row = new AppointmentHistoryT();
                $h_row->car_id = $car_id;
                $h_row->user_id = $this->my->id;
                $h_row->pdate = $pdate;
                $h_row->ptime = $ptime;
                $h_row->ptype = $ptype;
                $h_row->remark = $remark;
                $h_row->business_group_id = $business_group_id;
                if(!$h_row->save()) {
                    throw new \Exception(print_r($h_row->getErrors(), true));
                }

                $log_txt = '';
                if($old_user_id > 0) {
                    $log_txt = '旧业务员是:'.$old_user_id.' ';
                }
                $this->addLog($car_id,$log_txt.'预约客户到'.$pdate.' '.$ptime.',预约内容为:'.$remark,1);

                $tran->commit();

                $result['success'] = true;
                $result['msg'] = '保存成功';
            } catch(\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }

        }
        return $result;
    }

    public function actionAjaxNext()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $session = Yii::$app->session;
        $request = Yii::$app->request;

        $index = $request->get('index');
        $type = $request->get('type');

        $sql = null;
        if($type == 1) {
            $sql = $session->get('appointment_first',array());
        }
        if($type == 2) {
            $sql = $session->get('appointment_today',array());
        }
        if($type == 3) {
            $sql = $session->get('appointment_all',array());
        }
        $car_id = 0;
        $item = Yii::$app->db->createCommand($sql.' limit '.$index.',1')
            ->queryOne();
        if($item) {
            $car_id = $item['car_id'];
        }

        $result = array();
        $result['success'] = true;
        $result['car_id'] = $car_id;

        return $result;
    }

    //首拨
    public function actionFirst() {
        $invalid_items = InvalidT::getTree();
        $user_items = $this->my->getChildren();

        return $this->render('first',[
            'invalid_items' => $invalid_items,
            'user_items' => $user_items
        ]);
    }
    public function actionFirstJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $session = Yii::$app->session;
        $request = Yii::$app->request;
        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $username = $request->get('username');
        $sort_key = $request->get('sort_key','appointment_t.pdate');
        $sort_value = $request->get('sort_value','ASC');
        $offset = $request->get('offset',0);
        $limit = $request->get('limit', 10);

        $query = AppointmentT::find()
            ->leftJoin('car_t','`car_t`.`id`=`appointment_t`.`car_id`')
            ->where('appointment_t.is_first=1 and car_t.location=2');

        if($username == '') {
            $user_ids = $this->getChildrenUserIDs();
            $query->andWhere(['in','appointment_t.user_id',$user_ids]);
        }

        if($car_man != '') {
            $query->andWhere('car_t.car_man like "'.$car_man.'"');
        }
        if($phone != '') {
            $query->andWhere('car_t.car_man_phone like "'.$phone.'"');
        }
        if($car_no != '') {
            $query->andWhere('car_t.car_no like "'.$car_no.'"');
        }
        if($username != '') {
            $user_query = UserT::find()
                ->where('username="'.$username.'"')
                ->all();
            foreach($user_query as $user_info) {
                $user_ids[] = $user_info->id;
            }
            if(count($user_ids) > 0)
                $query->andWhere(['in','appointment_t.user_id',$user_ids]);
        }
        if($sort_key != '') {
            $query->orderBy($sort_key.' '.$sort_value.',appointment_t.id '.$sort_value);
        }
        $sql = $query->createCommand()->rawSql;
//        echo $query->createCommand()->rawSql;
        $total = $query->count();
        $session->remove('appointment_first');
        $session->set('appointment_first',$sql);

        $query = $query->offset($offset)->limit($limit);
        $items = $query->all();

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($items as $item) {
            $car_info = $item->car;
            $row = $item->toArray();
            $row['car_id'] = $car_info->id;
            $row['car_no'] = $car_info->car_no;
            $row['car_man'] = $car_info->car_man;
            $row['register_date'] = $car_info->register_date;
            $row['insurer1_date'] = $car_info->insurer1_date;
            $row['insurer2_date'] = $car_info->insurer2_date;
            $row['user_name'] = $item->user ? $item->user->getShowName():'';
            $data['rows'][] = $row;
        }
        return $data;
    }

    //今日预约
    public function actionToday() {
        $invalid_items = InvalidT::getTree();
        $user_items = $this->my->getChildren();

        return $this->render('today',[
            'invalid_items' => $invalid_items,
            'user_items' => $user_items
        ]);
    }
    public function actionTodayJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $session = Yii::$app->session;
        $request = Yii::$app->request;
        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $sort_key = $request->get('sort_key','appointment_t.pdate');
        $sort_value = $request->get('sort_value','ASC');
        $ptype = $request->get('ptype');
        $offset = $request->get('offset',0);
        $limit = $request->get('limit', 10);

        $query = AppointmentT::find()
            ->leftJoin('car_t','`car_t`.`id`=`appointment_t`.`car_id`')
            ->where('appointment_t.is_first=0 and car_t.location=2')
            ->andWhere('appointment_t.pdate<="'.date('Y-m-d').'"');

        $user_ids = $this->getChildrenUserIDs();
        $query = $query->andWhere(['in','appointment_t.user_id',$user_ids]);

        if($car_man != '') {
            $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
        }
        if($phone != '') {
            $query = $query->andWhere('car_t.car_man_phone="'.$phone.'"');
        }
        if($ptype > 0) {
            $query = $query->andWhere('appointment_t.ptype='.$ptype);
        }
        if($car_no != '') {
            $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
        }
        if($sort_key != '') {
            $query = $query->orderBy($sort_key.' '.$sort_value.', appointment_t.id '.$sort_value);
        }
        $sql = $query->createCommand()->rawSql;
//        echo $query->createCommand()->rawSql;
        $total = $query->count();
        $session->remove('appointment_today');
        $session->set('appointment_today',$sql);

        $query = $query->offset($offset)->limit($limit);
        $items = $query->all();

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($items as $item) {
            $car_info = $item->car;
            $row = $item->toArray();
            $row['car_id'] = $car_info->id;
            $row['car_no'] = $car_info->car_no;
            $row['car_man'] = $car_info->car_man;
            $row['register_date'] = $car_info->register_date;
            $row['insurer1_date'] = $car_info->insurer1_date;
            $row['insurer2_date'] = $car_info->insurer2_date;
            $row['user_name'] = $item->user ? $item->user->getShowName():'';
            $data['rows'][] = $row;
        }
        return $data;
    }

    //全部预约
    public function actionAll() {
        $invalid_items = InvalidT::getTree();
        $user_items = $this->my->getChildren();

        return $this->render('all',[
            'invalid_items' => $invalid_items,
            'user_items' => $user_items
        ]);
    }
    public function actionAllJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $session = Yii::$app->session;
        $request = Yii::$app->request;
        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $username = $request->get('username');
        $sort_key = $request->get('sort_key','appointment_t.pdate');
        $sort_value = $request->get('sort_value','ASC');
        $user_id = $request->get('user_id');
        $ptype = $request->get('ptype');
        $offset = $request->get('offset',0);
        $limit = $request->get('limit', 10);

        $query = AppointmentT::find()
            ->leftJoin('car_t','`car_t`.`id`=`appointment_t`.`car_id`')
            ->where('appointment_t.is_first=0 and car_t.location=2');

        if($username == '') {
            $user_ids = $this->getChildrenUserIDs();
            $query = $query->andWhere(['in','appointment_t.user_id',$user_ids]);
        }

        if($car_man != '') {
            $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
        }
        if($phone != '') {
            $query = $query->andWhere('car_t.car_man_phone="'.$phone.'"');
        }
        if($ptype > 0) {
            $query = $query->andWhere('appointment_t.ptype='.$ptype);
        }
        if($car_no != '') {
            $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
        }
        if($username != '') {
            $user_ids = array();
            $user_query = UserT::find()
                ->where('username="'.$username.'"')
                ->all();
            foreach($user_query as $user_info) {
                $user_ids[] = $user_info->id;
            }
            if(count($user_ids) > 0)
                $query = $query->andWhere(['in','appointment_t.user_id',$user_ids]);
            else
                $query = $query->andWhere('appointment_t.user_id=-1');
        }
        if($sort_key != '') {
            $query = $query->orderBy($sort_key.' '.$sort_value.',appointment_t.id '.$sort_value);
        }
        $sql = $query->createCommand()->rawSql;
//        echo $query->createCommand()->rawSql;
        $total = $query->count();
        $session->remove('appointment_all');
        $session->set('appointment_all',$sql);

        $query = $query->offset($offset)->limit($limit);
        $items = $query->all();

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($items as $item) {
            $car_info = $item->car;
            $row = $item->toArray();
            $row['car_id'] = $car_info->id;
            $row['car_no'] = $car_info->car_no;
            $row['car_man'] = $car_info->car_man;
            $row['register_date'] = $car_info->register_date;
            $row['insurer1_date'] = $car_info->insurer1_date;
            $row['insurer2_date'] = $car_info->insurer2_date;
            $row['user_name'] = $item->user ? $item->user->getShowName():'';
            $data['rows'][] = $row;
        }
        return $data;
    }

    public function actionCount()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '失败';

        if($request->isPost) {
            $car_id = $request->post('car_id',0);
            $pdate = $request->post('pdate');

            $query = AppointmentT::find()
                ->where('pdate="'.$pdate.'" and user_id='.$this->my->id);
            $total = $query->count();

            $result['success'] = true;
            $result['msg'] = '成功';
            $result['count'] = $total;
        }
        return $result;
    }

    public function actionInvalidA()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = $request->post('ids',array());
            if(count($ids) == 0) {
                $result['msg'] = '请先钩选车辆';
                return $result;
            }

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($ids as $id) {
                    $car_info = CarT::findOne(['id'=>$id]);
                    if($car_info->location == 2) {
                        $car_info->location = 1;
                        $car_info->op_user1 = '';
                    }
                    if($car_info->location == 3 || $car_info->location == 4) {
                        if($car_info->location == 3)
                            $car_info->op_user2 = '';
                        else
                            $car_info->op_user3 = '';
                        $car_info->location = 3;
                    }
                    $car_info->save();

                    CarInvalidT::deleteAll('id='.$id);
                    CarBT::deleteAll('id='.$id);
                    CarCT::deleteAll('id='.$id);
                    CarDT::deleteAll('id='.$id);
                    
                    if($car_info->location == 3) {
                    	$c_info = new CarCT();
                    	$c_info->id = $id;
                    	$c_info->user_id = 0;
                    	$c_info->save();
                      $this->addLog($car_info->id,'回收车辆数据到C库',1);
                  } else {
	                    $this->addLog($car_info->id,'回收车辆数据到A库',1);
                    }

                    //删除预约
                    AppointmentT::deleteAll('car_id='.$car_info->id);

                    //删除保单
                    OrderT::deleteAll('car_id='.$car_info->id.' and status_id<2');

                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }

    public function actionAssignOther()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = $request->post('ids',array());
            $user_id = $request->post('user_id');
            if($user_id == 0) {
                $result['msg'] = '请选择业务员';
                return $result;
            }
            if(count($ids) == 0) {
                $result['msg'] = '请先钩选车辆';
                return $result;
            }

            $user_info = UserT::findOne(['id'=>$user_id]);

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($ids as $id) {
                    $car_info = CarT::findOne(['id'=>$id]);

                    $old_user_info = '';
                    if($car_info->location == 2) {
                        $b_info = CarBT::findOne(['id'=>$car_info->id]);
                        $old_user_info = $b_info->user;
                        $b_info->user_id = $user_id;
                        $b_info->save();
                        $user =$user_info;
//                        dd($user->getShowName());
                        if($user) {
                            $car_info->op_user1 = $user->getShowName();
                        }
                    }
                    if($car_info->location == 3) {
                        $c_info = CarCT::findOne(['id'=>$car_info->id]);
                        $old_user_info = $c_info->user;
                        $c_info->user_id = $user_id;
                        $c_info->save();
                        $user = $user_info;
                        if($user) {
                            $car_info->op_user2 = $user->getShowName();
                        }
                    }
                    if($car_info->location == 4) {
                        $d_info = CarDT::findOne(['id'=>$car_info->id]);
                        $old_user_info = $d_info->user;
                        $d_info->user_id = $user_id;
                        $d_info->save();
                        $user = $user_info;
                        if($user) {
                            $car_info->op_user3 = $user->getShowName();
                        }
                    }
                    //库保存
                    $car_info->save();
                    $this->addLog($car_info->id,'从'.$old_user_info->getShowName().' 平移车辆给 '.$user_info->getShowName(),1);

                    //添加保单
                    OrderT::deleteAll('car_id='.$car_info->id.' and status_id<2');
                    $order_row = new OrderT();
                    $order_row->car_id = $car_info->id;
                    $order_row->car_no = $car_info->car_no;
                    $order_row->engine_no = $car_info->engine_no;
                    $order_row->car_frame_no = $car_info->car_frame_no;
                    $order_row->car_man = $car_info->car_man;
                    $order_row->user_id = $user_id;
                    $order_row->status_id = 1;
                    $order_row->id_man = $car_info->car_man;
                    $order_row->link_man = $car_info->car_man;
                    $order_row->link_phone = $car_info->phone;
                    $order_row->save();

                    //添加预约
                    AppointmentT::deleteAll('car_id='.$car_info->id);
                    $row = new AppointmentT();
                    $row->car_id = $car_info->id;
                    $row->user_id = $user_id;
                    $row->pdate = date('Y-m-d');
                    $row->ptime = '09:00';
                    $row->remark = '首次分配';
                    $row->is_first = 1;
                    $row->save();

                    //添加历史预约
                    $h_row = new AppointmentHistoryT();
                    $h_row->car_id = $car_info->id;
                    $h_row->user_id = $user_id;
                    $h_row->pdate = date('Y-m-d');
                    $h_row->ptime = '09:00';
                    $h_row->remark = '首次分配';
                    $h_row->save();
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                throw $e;
            }
        }
        return $result;
    }
    //分配跟踪业务员
    public function actionAssignCpei()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = $request->post('ids',array());
//            var_dump($ids);
//            die;
            $user_id = $request->post('user_id');
            if($user_id == 0) {
                $result['msg'] = '请选择业务员';
                return $result;
            }
            if(count($ids) == 0) {
                $result['msg'] = '请先钩选车辆';
                return $result;
            }

            $user_info = UserT::findOne(['id'=>$user_id]);
            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($ids as $id) {
                    $car_info = CarT::findOne(['id'=>$id]);

                    $old_user_info = '';

                    if($car_info->location == 6 || $car_info->location == 3) {
                        if($car_info->location == 6)
                            $e_info = CarET::findOne(['id'=>$car_info->id]);
                        else if($car_info->location == 3)
                            $e_info = CarCT::findOne(['id'=>$car_info->id]);
                        $old_user_info = $e_info->user;
                        $e_info->user_id = $user_id;
                        $e_info->save();
                        //由于事务进行的作用,程序没有进行到底,所执行的数据操作没有进行
                        //获取post参数的id
                        $user = $user_info;
//                        var_dump($user);
//                        die;

                        if($user) {
                            $car_info->op_user2 = $user->getShowName();
                        }
                        $car_info -> save();
//                        var_dump($car_info->op_user2);
//                        die;
                    } else {
                        var_dump($car_info);
                        exit;
                    }

                    $this->addLog($car_info->id,'从'.$old_user_info->getShowName().' 平移车辆给 '.$user_info->getShowName(),1);

                    //添加保单
                    OrderT::deleteAll('car_id='.$car_info->id.' and status_id<2');
                    $order_row = new OrderT();
                    $order_row->car_id = $car_info->id;
                    $order_row->car_no = $car_info->car_no;
                    $order_row->engine_no = $car_info->engine_no;
                    $order_row->car_frame_no = $car_info->car_frame_no;
                    $order_row->car_man = $car_info->car_man;
                    $order_row->user_id = $user_id;
                    $order_row->status_id = 1;
                    $order_row->id_man = $car_info->car_man;
                    $order_row->link_man = $car_info->car_man;
                    $order_row->link_phone = $car_info->phone;
                    $order_row->save();

                    //添加预约
                    AppointmentT::deleteAll('car_id='.$car_info->id);
                    $row = new AppointmentT();
                    $row->car_id = $car_info->id;
                    $row->user_id = $user_id;
                    $row->pdate = date('Y-m-d');
                    $row->ptime = '09:00';
                    $row->remark = '首次分配';
                    $row->is_first = 1;
                    $row->save();

                    //添加历史预约
                    $h_row = new AppointmentHistoryT();
                    $h_row->car_id = $car_info->id;
                    $h_row->user_id = $user_id;
                    $h_row->pdate = date('Y-m-d');
                    $h_row->ptime = '09:00';
                    $h_row->remark = '首次分配';
                    $h_row->save();
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                throw $e;
            }
        }
        return $result;

    }







}