<?php

namespace frontend\controllers;

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

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

        return $this->renderPartial('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');

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

            $tran = AppointmentT::getDb()->beginTransaction();
            try {
                $row = AppointmentT::find()
                    ->where('car_id='.$car_id)
                    ->one();
                if(empty($row)) {
                    $row = new AppointmentT();
                }

                $row->car_id = $car_id;
                $row->user_id = $this->my->id;
                $row->pdate = $pdate;
                $row->ptime = $ptime;
                $row->remark = $remark;
                $row->is_first = 0;
                $row->save();

                $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->remark = $remark;
                $h_row->save();

                $this->addLog($car_id,'预约客户到'.$pdate.' '.$ptime.',预约内容为:'.$remark,1);

                $tran->commit();

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

            $result['success'] = true;
            $result['msg'] = '保存成功';
        }
        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('track_first',array());
        }
        if($type == 2) {
            $sql = $session->get('track_today',array());
        }
        if($type == 3) {
            $sql = $session->get('track_all',array());
        }
        if(!$sql){
            return array(
                'success' => true,
                'car_id' => 0
            );
        }
        $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()
    {
        $session = Yii::$app->session;
        $request = Yii::$app->request;
        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $page = $request->get('page',1);
        $sort_key = $request->get('sort_key','appointment_t.pdate');
        $sort_value = $request->get('sort_value','ASC');
        $username = $request->get('username');
        $type = $request->get('sort_type');
        
        switch($type){
            case 0:
                $sort_type = '(car_t.location=3 or car_t.location=6)';
                break;  
            case 1:
                $sort_type = 'car_t.location=3';
                break;
            case 2:
                $sort_type = 'car_t.location=6';
                break;
            default:
                $sort_type = '(car_t.location=3 or car_t.location=6)';
        }
        $query = AppointmentT::find()
            ->leftJoin('car_t','`car_t`.`id`=`appointment_t`.`car_id`')
            ->where('appointment_t.is_first=1 and '.$sort_type)
            ->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.phone="'.$phone.'"');
        }
        if($car_no != '') {
            $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
        }
        if($username != '') {
            $user_info = UserT::find()
                ->where('username="'.$username.'"')
                ->one();
            if($user_info)
                $query = $query->andWhere('appointment_t.user_id='.$user_info->id);
        }
        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('track_first');
        $session->set('track_first',$sql);

        $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);
        $invalid_items = InvalidT::getTree();

        return $this->render('first',[
            'items' => $items,
            'page_info' => $page_info,
            'car_man' => $car_man,
            'phone' => $phone,
            'car_no' => $car_no,
            'sort_type' => $type,
            'page' => $page,
            'sort_key' => $sort_key,
            'sort_value' => $sort_value,
            'type' => 1,
            'username' => $username,
            'invalid_items' => $invalid_items
        ]);
    }
     //针对从c库分配好的数据,经过c到e自动化而使数据而无法自动化
    public function actionFirstOne()
    {
        $session = Yii::$app->session;
        $request = Yii::$app->request;
        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $page = $request->get('page',1);
        $sort_key = $request->get('sort_key','appointment_t.pdate');
        $sort_value = $request->get('sort_value','ASC');
        $username = $request->get('username');
        $type = $request->get('sort_type');

        switch($type){
            case 0:
                $sort_type = '(car_t.location=3 or car_t.location=6)';
                break;
            case 1:
                $sort_type = 'car_t.location=3';
                break;
            case 2:
                $sort_type = 'car_t.location=6';
                break;
            default:
                $sort_type = '(car_t.location=3 or car_t.location=6)';
        }
        $query = AppointmentT::find()
            ->leftJoin('car_t','`car_t`.`id`=`appointment_t`.`car_id`')
            ->where('appointment_t.is_first=1 and '.$sort_type)
            ->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.phone="'.$phone.'"');
        }
        if($car_no != '') {
            $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
        }
        if($username != '') {
            $user_info = UserT::find()
                ->where('username="'.$username.'"')
                ->one();
            if($user_info)
                $query = $query->andWhere('appointment_t.user_id='.$user_info->id);
        }
        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('track_first');
        $session->set('track_first',$sql);

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

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

        foreach($items as $vs){
//            dd($vs);
          $carE_info=  CarET::findOne(['id'=>$vs->car_id]);
            $carE_info->user_id=$vs->user_id;
            $carE_info->save();
            $this->addLog($vs->car_id,'修改跟踪操作员',1);
//            dd($carE_info);


        }


        $page_info = MyLib::getPageInfo($pagination);
        $invalid_items = InvalidT::getTree();

        return $this->render('first',[
            'items' => $items,
            'page_info' => $page_info,
            'car_man' => $car_man,
            'phone' => $phone,
            'car_no' => $car_no,
            'sort_type' => $type,
            'page' => $page,
            'sort_key' => $sort_key,
            'sort_value' => $sort_value,
            'type' => 1,
            'username' => $username,
            'invalid_items' => $invalid_items
        ]);
    }

    //今日预约
    public function actionToday()
    {
        $session = Yii::$app->session;
        $request = Yii::$app->request;
        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $page = $request->get('page',1);
        $sort_key = $request->get('sort_key','appointment_t.pdate');
        $sort_value = $request->get('sort_value','ASC');
        $username = $request->get('username');

        $user_id = $request->get('user_id');
        $ptype = $request->get('ptype');
        $type = $request->get('sort_type');
        $date_begin = $request->get('date_begin');
        $date_end = $request->get('date_end');
        
        switch($type){
            case 0:
                $sort_type = '(car_t.location=3 or car_t.location=6)';
                break;  
            case 1:
                $sort_type = 'car_t.location=3';
                break;
            case 2:
                $sort_type = 'car_t.location=6';
                break;
            default:
                $sort_type = '(car_t.location=3 or car_t.location=6)';
        }

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

//        $user_ids = $this->getChildrenUserIDs();
//        $query = $query->andWhere(['in','appointment_t.user_id',$user_ids]);
        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.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);
        }
        if($date_begin != '') {
            $query = $query->andWhere('appointment_t.pdate>="'.$date_begin.'"');
        }
        if($date_end != '') {
            $query = $query->andWhere('appointment_t.pdate<="'.$date_end.'"');
        }
//        if($username == '') {
//            $user_ids = $this->getChildrenUserIDs();
//            $query = $query->andWhere(['in','appointment_t.user_id',$user_ids]);
//        }
        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 = $query->andWhere(['in','appointment_t.user_id',$user_ids]);
        }
        $sql = $query->createCommand()->rawSql;
//        echo $query->createCommand()->rawSql;
        $total = $query->count();
        $session->remove('track_today');
        $session->set('track_today',$sql);

        $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);
        
        $list = MeetT::getAllData(2);
        $user_items = $this->my->getChildren();
        return $this->render('today',[
            'items' => $items,
            'page_info' => $page_info,
            'car_man' => $car_man,
            'phone' => $phone,
            'ptype' => $ptype,
            'list'  => $list,
            'sort_type' => $type,
            'car_no' => $car_no,
            'page' => $page,
            'sort_key' => $sort_key,
            'sort_value' => $sort_value,
            'type' => 2,
            'user_id' => $user_id,
            'user_items' => $user_items,
            'username' => $username,
            'date_begin' => $date_begin,
            'date_end' => $date_end
        ]);
    }

    //全部预约
    public function actionAll()
    {
        $session = Yii::$app->session;
        $request = Yii::$app->request;
        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $page = $request->get('page',1);
        $sort_key = $request->get('sort_key','appointment_t.pdate');
        $sort_value = $request->get('sort_value','ASC');
        $username = $request->get('username');
        $date_begin = $request->get('date_begin');
        $date_end = $request->get('date_end');
        $ptype = $request->get('ptype');
        $type = $request->get('sort_type');
        $user_id = $request->get('user_id');
        
        switch($type){
            case 0:
                $sort_type = '(car_t.location=3 or car_t.location=6)';
                break;  
            case 1:
                $sort_type = 'car_t.location=3';
                break;
            case 2:
                $sort_type = 'car_t.location=6';
                break;
            default:
                $sort_type = '(car_t.location=3 or car_t.location=6)';
        }

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

        $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.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);
        }
        if($date_begin != '') {
            $query = $query->andWhere('insurer1_date>="'.$date_begin.'"');
        }
        if($date_end != '') {
            $query = $query->andWhere('insurer1_date<="'.$date_end.'"');
        }
        if($username != '') {
            $user_info = UserT::find()
                ->where('username="'.$username.'"')
                ->one();
            if($user_info)
                $query = $query->andWhere('appointment_t.user_id='.$user_info->id);
        }
        $sql = $query->createCommand()->rawSql;
//        echo $query->createCommand()->rawSql;
        $total = $query->count();
        $session->remove('track_all');
        $session->set('track_all',$sql);

        $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);
        $invalid_items = InvalidT::getTree();
        
        $list = MeetT::getAllData(2);
        $user_items = $this->my->getChildren();

        return $this->render('all',[
            'items' => $items,
            'page_info' => $page_info,
            'car_man' => $car_man,
            'phone' => $phone,
            'ptype' => $ptype,
            'sort_type' => $type,
            'list'  => $list,
            'car_no' => $car_no,
            'page' => $page,
            'sort_key' => $sort_key,
            'sort_value' => $sort_value,
            'type' => 3,
            'username' => $username,
            'user_items' => $user_items,
            'user_id' => $user_id,
            'invalid_items' => $invalid_items,
            'date_begin' => $date_begin,
            'date_end' => $date_end,
        ]);
    }
    //领近保险快到期90天
    public function actionEndAll()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '读取失败';
        $query = AppointmentT::find()
            ->leftJoin('car_t','`car_t`.`id`=`appointment_t`.`car_id`')
            ->where('appointment_t.is_first=0 and car_t.location=6');
        $user_ids = $this->getChildrenUserIDs();
        $query = $query->andWhere(['in','appointment_t.user_id',$user_ids]);
        $bgstr= date("Y-m-d",strtotime("+90 day"));

        //结束时间
        $endstr=date("Y-m-d",strtotime("+97 day"));
        $query = $query->andWhere('insurer1_date>"'.$bgstr.'"');

        $query = $query->andWhere('insurer1_date<="'.$endstr.'"');
        $data=$query->count();
        if($data>=1){
            $result['success'] = true;
            $result['num'] = $data;
            $result['msg'] = '读取成功';
        }else{
            $result['msg'] = '没有数据';
        }
//        var_dump($data);
//        die;
        return $result;
    }
    public function actionEndAllList()
    {

        $session = Yii::$app->session;
        $request = Yii::$app->request;
//        $car_man = $request->get('car_man');
//        $phone = $request->get('phone');
//        $car_no = $request->get('car_no');
        $page = $request->get('page',1);
        $day = $request->get('day',1);
//        $sort_key = $request->get('sort_key','appointment_t.pdate');
//        $sort_value = $request->get('sort_value','ASC');
//        $username = $request->get('username');
//        $date_begin = $request->get('date_begin');
//        $date_end = $request->get('date_end');
//        $ptype = $request->get('ptype');
//        $type = $request->get('sort_type');
//        $user_id = $request->get('user_id');

//        switch($type){
//            case 0:
//                $sort_type = '(car_t.location=3 or car_t.location=6)';
//                break;
//            case 1:
//                $sort_type = 'car_t.location=3';
//                break;
//            case 2:
//                $sort_type = 'car_t.location=6';
//                break;
//            default:
//                $sort_type = '(car_t.location=3 or car_t.location=6)';
//        }

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

        $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.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);
//        }
        $bgstr= date("Y-m-d",strtotime("+90 day"));

        //结束时间
        $endstr=date("Y-m-d",strtotime("+97 day"));
        $query = $query->andWhere('insurer1_date>"'.$bgstr.'"');

        $query = $query->andWhere('insurer1_date<="'.$endstr.'"');



//        if($username != '') {
//            $user_info = UserT::find()
//                ->where('username="'.$username.'"')
//                ->one();
//            if($user_info)
//                $query = $query->andWhere('appointment_t.user_id='.$user_info->id);
//        }
        $sql = $query->createCommand()->rawSql;
//        echo $query->createCommand()->rawSql;
        $total = $query->count();
        $session->remove('track_all');
        $session->set('track_all',$sql);

        $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);


        $list = MeetT::getAllData(2);
        $user_items = $this->my->getChildren();

        if($day==1){
            $days=2;
        }else{
            $days=3;
        }

        return $this->render('all-list',[
            'items' => $items,
            'page_info' => $page_info,

            'list'  => $list,

            'page' => $page,

            'type' => $days,

            'user_items' => $user_items,



        ]);
    }
    public function actionInvalidC()
    {
        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]);
                    $car_info->location = 3;
                    $car_info->save();

                    $c_info = CarCT::findOne(['id'=>$id]);
                    $c_info->user_id = 0;
                    $c_info->save();

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

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

                    $this->addLog($car_info->id,'回收车辆数据到C库',1);
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

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

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

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

            if($id > 0) {
                $tran = CarCT::getDb()->beginTransaction();
                try {
                    $c_info = CarCT::findOne(['id'=>$id]);
                    if($c_info->user_id > 0)
                        $this->addLog($c_info->id,'业务员:'.$c_info->user->getShowName().'自动放弃跟踪数据',1);

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

                    $car_info = $c_info->car;
                    $car_info->user_id = 0;
                    $car_info->location = 4;
                    $car_info->save();

                    CarCT::deleteAll(['id'=>$car_info->id]);

                    $d_info = CarDT::findOne(['id'=>$car_info->id]);
                    if(!$d_info) {
                        $d_info = new CarDT();
                        $d_info->id = $car_info->id;
                        $d_info->user_id = 0;
                        $d_info->save();

                        $this->addLog($car_info->id,'从C库分配到D库',1);
                    }

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

                $result['success'] = true;
                $result['msg'] = '放弃成功';
            }
        }
        return $result;
    }

    //从e库放弃到a库
//    public function actionCarCancelE()
//    {
//        Yii::$app->response->format = Response::FORMAT_JSON;
//        $request = Yii::$app->request;
//        $result = array();
//        $result['success'] = false;
//        $result['msg'] = '放弃失败';
//
//        if($request->isPost) {
//            $id = $request->post('id',0);
//
//            if($id > 0) {
//                $tran = CarET::getDb()->beginTransaction();
//                try {
//                    $c_info = CarET::findOne(['id'=>$id]);
//                    if($c_info->user_id > 0)
//                        $this->addLog($c_info->id,'业务员:'.$c_info->user->getShowName().'自动放弃跟踪数据',1);
//
//                    //删除预约
//                    AppointmentT::deleteAll('user_id='.$c_info->user_id.' and car_id='.$c_info->id);
//
//                    $car_info = $c_info->car;
//                    $car_info->user_id = 0;
//                    $car_info->location = 1;
//                    $car_info->save();
//
//                    CarET::deleteAll(['id'=>$car_info->id]);
//
//                    $this->addLog($car_info->id,'从E库分配到A库',1);
//
//                    $tran->commit();
//                } catch(\Exception $e) {
//                    $tran->rollBack();
//                    throw $e;
//                }
//
//                $result['success'] = true;
//                $result['msg'] = '放弃成功';
//            }
//        }
//        return $result;
//    }

    /**
     * E库数据放弃到D库
     * xzz
     * 2017-12-21
     */
    public function actionCarCancelE()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;

        $result = [
            'success' => false,
            'msg' => '放弃失败'
        ];

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

            if($id > 0){
                $tran = CarET::getDb()->beginTransaction();

                try{
                    $c_info = CarET::findOne(['id' => $id]);

                    if($c_info->user_id > 0)
                        $this->addLog($c_info->id,'业务员:'.$c_info->user->getShowName().'自动放弃跟踪数据',1);

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

                    $car_info = $c_info->car;
                    $car_info->user_id = 0;
                    $car_info->location = 4;
                    $car_info->save();

                    CarET::deleteAll(['id' => $car_info->id]);

                    $d_info = CarDT::findOne(['id' => $car_info->id]);
                    if(!$d_info){
                        $d_info = new CarDT();
                        $d_info->id = $car_info->id;
                        $d_info->user_id = 0;
                        $d_info->save();

                        $this->addLog($car_info->id,'从E库分配到D库',1);
                    }

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

                $result = [
                    'success' => true,
                    'msg' => '放弃成功'
                ];
            }
        }

        return $result;
    }
}