<?php

namespace frontend\controllers;

use common\libs\MyLib;
use common\models\AppointmentHistoryT;
use common\models\AppointmentT;
use common\models\FiltrationT;
use common\models\BrandT;
use common\models\CarBT;
use common\models\CarCT;
use common\models\CarET;
use common\models\CarFT;
use common\models\CarF1T;
use common\models\CarF2T;
use common\models\CarF3T;
use common\models\CarF4T;
use common\models\CarF5T;
use common\models\CarF6T;
use common\models\CardinfoT;
use common\models\CarDT;
use common\models\CarInvalidT;
use common\models\CarMistakeT;
use common\models\CarLogT;
use common\models\CarT;
use common\models\CarTypeT;
use common\models\CarUseT;
use common\models\CleanT;
use common\models\CleanTongji;
use common\models\CleanFenpei;
use common\models\DisplacementT;
use common\models\InvalidT;
use common\models\OrderT;
use common\models\OrderUserT;
use common\models\PriceT;
use common\models\RightT;
use common\models\SeriesT;
use common\models\UserT;
use common\models\InvalidNew;
use Yii;
use yii\data\Pagination;
use yii\db\Exception;
use yii\web\Response;
use yii\db\Expression;

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

    //A库query
    //获取搜索条件
    private function getAQuery($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $engine_no = $request->post('engine_no');
            $car_frame_no = $request->post('car_frame_no');
            $register_begin = $request->post('register_begin');
            $register_end = $request->post('register_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $insurer1_month = $request->post('insurer1_month');
            $insurer1_day = $request->post('insurer1_day');
            $insurer1_month1 = $request->post('insurer1_month1');
            $insurer1_day1 = $request->post('insurer1_day1');
            $b_lib = $request->post('b_lib');
            $c_lib = $request->post('c_lib');
            $d_lib = $request->post('d_lib');
            $e_lib = $request->post('e_lib');
            $invalid_lib = $request->post('invalid_lib');
            $unsel_lib = $request->post('unsel_lib');
            $has_phone = $request->post('has_phone');
            $company = $request->post('company');
            $user1 = $request->post('user1');
            $no_insurerdate = $request->post('no_insurerdate');
            $clean_right = $request->post('clean_right');
            $biaozhu = $request->post('biaozhu');
            $car_use_id = $request->post('car_use_id');
        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $engine_no = $request->get('engine_no');
            $car_frame_no = $request->get('car_frame_no');
            $register_begin = $request->get('register_begin');
            $register_end = $request->get('register_end');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $insurer1_month = $request->get('insurer1_month');
            $insurer1_day = $request->get('insurer1_day');
            $insurer1_month1 = $request->get('insurer1_month1');
            $insurer1_day1 = $request->get('insurer1_day1');
            $b_lib = $request->get('b_lib');
            $c_lib = $request->get('c_lib');
            $d_lib = $request->get('d_lib');
            $e_lib = $request->get('e_lib');
            $invalid_lib = $request->get('invalid_lib');
            $unsel_lib = $request->get('unsel_lib');
            $has_phone = $request->get('has_phone');
            $company = $request->get('company');
            $user1 = $request->get('user1');
            $no_insurerdate = $request->get('no_insurerdate');
            $clean_right = $request->get('clean_right');
            $biaozhu = $request->get('biaozhu');
            $car_use_id = $request->get('car_use_id');
        }
        $params = array();
        $params['car_man'] = $car_man;
        $params['phone'] = $phone;
        $params['car_no'] = $car_no;
        $params['engine_no'] = $engine_no;
        $params['car_frame_no'] = $car_frame_no;
        $params['register_begin'] = $register_begin;
        $params['register_end'] = $register_end;
        $params['insurer1_begin'] = $insurer1_begin;
        $params['insurer1_end'] = $insurer1_end;
        $params['insurer1_month'] = $insurer1_month;
        $params['insurer1_day'] = $insurer1_day;
        $params['insurer1_month1'] = $insurer1_month1;
        $params['insurer1_day1'] = $insurer1_day1;
        $params['b_lib'] = $b_lib;
        $params['c_lib'] = $c_lib;
        $params['d_lib'] = $d_lib;
        $params['e_lib'] = $e_lib;
        $params['invalid_lib'] = $invalid_lib;
        $params['unsel_lib'] = $unsel_lib;
        $params['has_phone'] = $has_phone;
        $params['company'] = $company;
        $params['user1'] = $user1;
        $params['no_insurerdate'] = $no_insurerdate;
        $params['clean_right'] = $clean_right;
        $params['biaozhu'] = $biaozhu;
        $params['car_use_id'] = $car_use_id;

        $query = CarT::find();

        $libs = array();
        if($b_lib == 1) {
            $libs[] = 2;
        }
        if($c_lib == 1) {
            $libs[] = 3;
        }
        if($d_lib == 1) {
            $libs[] = 4;
        }
        if($e_lib == 1) {
            $libs[] = 6;
        }
        if($invalid_lib == 1) {
            $libs[] = 5;
        }
        if($unsel_lib == 1) {
            $libs[] = 1;
        }
        if(count($libs) > 0)
            $query->andWhere(['in','location',$libs]);

        if($car_man != '') {
            $query->andWhere('car_man like "'.$car_man.'"');
        }
        if($phone != '') {
            $query->andWhere('phone like "'.$phone.'"');
        }
        if($car_no != '') {
            $query->andWhere('car_no like "'.$car_no.'%"');
        }
        if($engine_no != '') {
            $query->andWhere('engine_no="'.$engine_no.'"');
        }
        if($car_frame_no != '') {
            $query->andWhere('car_frame_no="'.$car_frame_no.'"');
        }
        if($register_begin != '') {
            $query->andWhere('register_date>="'.$register_begin.'"');
        }
        if($register_end != '') {
            $query->andWhere('register_date<="'.$register_end.'"');
        }
        if($insurer1_begin != '') {
            $query->andWhere('insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query->andWhere('insurer1_date<="'.$insurer1_end.'"');
        }
        if($insurer1_month != '') {
            $query->andWhere('month(car_t.insurer1_date)>="'.$insurer1_month.'"');
        }
        if($insurer1_day != '') {
            $query->andWhere('day(car_t.insurer1_date)>="'.$insurer1_day.'"');
        }
        if($insurer1_month1 != '') {
            $query->andWhere('month(car_t.insurer1_date)<="'.$insurer1_month1.'"');
        }
        if($insurer1_day1 != '') {
            $query->andWhere('day(car_t.insurer1_date)<="'.$insurer1_day1.'"');
        }
        if($clean_right != '') {
            $query->andWhere('car_t.clean_right="'.$clean_right.'"');
        }
        if($biaozhu != '') {
            $query->andWhere('car_t.biaozhu="'.$biaozhu.'"');
        }

        if($has_phone == 1) {
            $query->andWhere('phone<>""');
        }
        if($no_insurerdate == 1) {
            $query->andWhere('insurer1_date="" or insurer1_date is null or insurer1_date="0000-00-00"');
        }
        if($company != '') {
            $query->andWhere('company like "'.$company.'%"');
        }
        if($user1 != '') {
            $query->andWhere('op_user1 like "'.$user1.'%"');
        }
        if($car_use_id > 0) {
            $query->andWhere('car_use_id='.$car_use_id);
        }
        return ['query'=>$query,'params'=>$params];
    }
    //A库列表页
    public function actionAIndex() {
        $invalid_items = InvalidT::getTree();
        $car_use_items = CarUseT::find()->all();
        return $this->render('a-index',[
            'invalid_items' => $invalid_items,
            'car_use_items' => $car_use_items
        ]);
    }
    //A库列表数据
    public function actionAIndexJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $offset = $request->get('offset',0);
        $limit = $request->get('limit', 10);

        $data = $this->getAQuery($request);

        $query = $data['query'];
//        $query->andWhere('location=1');

        $total = $query->count();

        $query->orderBy('id desc');
        $query = $query->offset($offset)->limit($limit);
        $items = $query->all();

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($items as $item) {
            $row = $item->toArray();
            $row['factory_model'] = MyLib::substr_cut($item->factory_model, 8);
            $row['car_man'] = MyLib::substr_cut($item->car_man, 8);
            $row['location_name'] = $item->getLocation();
            $data['rows'][] = $row;
        }
        return $data;
    }
    //钩选A库到B库
    public function actionAB()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = explode(',',$request->post('ids'));
            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 == 1) {
                        $car_info->location = 2;
                        if(!$car_info->save(false)) {
                            throw new Exception(print_r($car_info->getFirstErrors(),true));
                        }

                        $b_info = CarBT::findOne(['id'=>$car_info->id]);
                        if(!$b_info) {
                            $b_info = new CarBT(false);
                            $b_info->id = $car_info->id;
                            $b_info->user_id = 0;
                            if(!$b_info->save(false)) {
                                throw new Exception(print_r($b_info->getFirstErrors(),true));
                            }
                        }
                        $this->addLog($car_info->id,'从A库分配到B库',1);
                    }
                }

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

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }
    //批量A库到B库
    public function actionABAll()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        $data = $this->getAQuery($request);

        $query = $data['query'];
//        $query->andWhere('location=1');

        $tran = CarT::getDb()->beginTransaction();
        try {
            foreach($query->each(1000) as $car_info) {
                if($car_info->location == 1) {
                    $car_info->location = 2;
                    if(!$car_info->save(false)) {
                        throw new Exception(print_r($car_info->getFirstErrors(),true));
                    }

                    $b_info = CarBT::findOne(['id'=>$car_info->id]);
                    if(!$b_info) {
                        $b_info = new CarBT(false);
                        $b_info->id = $car_info->id;
                        $b_info->user_id = 0;
                        if(!$b_info->save(false)) {
                            throw new Exception(print_r($b_info->getFirstErrors(),true));
                        }
                    }
                    $this->addLog($car_info->id,'从A库分配到B库',1);
                }
            }

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

            $tran->commit();
        } catch (\Exception $e) {
            $tran->rollBack();
            $result['msg'] = $e->getMessage();
        }
        return $result;
    }
    //钩选A库到C库
    public function actionAC()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = explode(',', $request->post('ids'));
            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 == 1) {
                        $car_info->location = 3;
                        if(!$car_info->save(false)) {
                            throw new Exception(print_r($car_info->getFirstErrors(),true));
                        }

                        $c_info = CarCT::findOne(['id'=>$car_info->id]);
                        if(!$c_info) {
                            $c_info = new CarCT(false);
                            $c_info->id = $car_info->id;
                            $c_info->user_id = 0;
                            if(!$c_info->save(false)) {
                                throw new Exception(print_r($c_info->getFirstErrors(),true));
                            }
                        }
                        $this->addLog($car_info->id,'从A库分配到C库',1);
                    }
                }

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

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }

            return $result;
        }
    }
    //钩选A库到无效库
    public function actionAInvalid()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = explode(',',$request->post('ids'));
            $invalid_id = $request->post('invalid_id',0);
            if($invalid_id == 0) {
                $result['msg'] = '请选择无效理由';
                return $result;
            }
            if($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 == 1) {
                        $car_info->location = 5;
                        if(!$car_info->save(false)) {
                            throw new Exception(print_r($car_info->getFirstErrors(),true));
                        }

                        $invalid_info = CarInvalidT::findOne(['id'=>$car_info->id]);
                        if(!$invalid_info) {
                            $invalid_info = new CarInvalidT(false);
                            $invalid_info->id = $car_info->id;
                            $invalid_info->user_id = $this->my->id;
                            $invalid_info->invalid_id = $invalid_id;
                            if(!$invalid_info->save(false)) {
                                throw new Exception(print_r($invalid_info->getFirstErrors(),true));
                            }
                        }
                        $this->addLog($car_info->id,'从A库移动到无效库',1);
                    }
                }

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

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }

    //B库
    //获取搜索条件
    private function getBQuery($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $register_begin = $request->post('register_begin');
            $register_end = $request->post('register_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $insurer1_month = $request->post('insurer1_date');
            $insurer1_day = $request->post('insurer1_day');
            $insurer1_month1 = $request->post('insurer1_date1');
            $insurer1_day1 = $request->post('insurer1_day1');
            $username = $request->post('username');
            $status = $request->post('status');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
            $company = $request->post('company');
            $clean_right = $request->post('clean_right');

        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $register_begin = $request->get('register_begin');
            $register_end = $request->get('register_end');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $insurer1_month = $request->get('insurer1_date');
            $insurer1_day = $request->get('insurer1_day');
            $insurer1_month1 = $request->get('insurer1_date1');
            $insurer1_day1 = $request->get('insurer1_day1');
            $username = $request->get('username');
            $status = $request->get('status');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
            $company = $request->get('company');
            $clean_right = $request->get('clean_right');
        }
        $query = CarBT::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`car_b_t`.`id`');

        if($car_man != '') {
            $query->andWhere('car_t.car_man like "'.$car_man.'"');
        }
        if($phone != '') {
            $query->andWhere('car_t.phone like "'.$phone.'"');
        }
        if($car_no != '') {
            if($car_no == '非京'){
                $query->andWhere('car_no not like "京%"');
            }else{
                $query->andWhere('car_no like "'.$car_no.'%"');
            }
        }
        if($register_begin != '') {
            $query->andWhere('car_t.register_date>="'.$register_begin.'"');
        }
        if($register_end != '') {
            $query->andWhere('car_t.register_date<="'.$register_end.'"');
        }
        if($insurer1_begin != '') {
            $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }
        if($status == 1) {
            $query->andWhere('car_b_t.user_id=0');
        }
        if($status == 2) {
            $query->andWhere('car_b_t.user_id>0');
        }
        if($username != '') {
            $user_info = UserT::findOne(['username'=>$username]);
            if($user_info) {
                $query->andWhere('car_b_t.user_id='.$user_info->id);
            }
        }
        if($company != '') {
            $query->andWhere('company like "'.$company.'%"');
        }
        $query = $query->orderBy($sort_key.' '.$sort_value.', car_b_t.id '.$sort_value);
        return ['query'=>$query];
    }
    //B库列表页
    public function actionBIndex()
    {
        $user_items = $this->my->getChildren();
        $invalid_items = InvalidT::getTree();
        return $this->render('b-index',[
            'user_items' => $user_items,
            'invalid_items' => $invalid_items
        ]);
    }
    //B库列表数据
    public function actionBIndexJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $offset = $request->get('offset',0);
        $limit = $request->get('limit', 10);

        $data = $this->getBQuery($request);

        $query = $data['query'];
//        $query->andWhere('location=1');

        $total = $query->count();

        $query->orderBy('id desc');
        $query = $query->offset($offset)->limit($limit);
//        echo $query->createCommand()->rawSql;
        $items = $query->all();

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($items as $item) {
            $car_info = $item->car;
            if(!$car_info) continue;
            $row = $car_info->toArray();
            $row['factory_model'] = MyLib::substr_cut($car_info->factory_model, 8);
            $row['car_man'] = MyLib::substr_cut($car_info->car_man, 8);
            $row['op_user'] = $item->user?$item->user->getShowName():'';
            $data['rows'][] = $row;
        }
        return $data;
    }
    //勾选B库分配
    public function actionBAssign()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = explode(',',$request->post('ids'));
            $user_id = $request->post('user_id',0);
            //针对数据处理的数据
            if($user_id == 0) {
                $result['msg'] = '请先选择分配给哪个业务员';
                return $result;
            }
            if($ids[0] == '') {
                $result['msg'] = '请先钩选车辆';
                return $result;
            }
            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($ids as $id) {
                    $b_info = CarBT::findOne(['id'=>$id]);
                    if($b_info->user_id > 0) {
                        continue;
                    }
                    $b_info->user_id = $user_id;
                    if(!$b_info->save()) {
                        throw new \Exception(print_r($b_info->getErrors(),true));
                    }

                    //添加保单
                    $order_row = new OrderT();
                    $order_row->car_id = $b_info->id;
                    $order_row->car_no = $b_info->car->car_no;
                    $order_row->engine_no = $b_info->car->engine_no;
                    $order_row->car_frame_no = $b_info->car->car_frame_no;
                    $order_row->car_man = $b_info->car->car_man;
                    $order_row->car_man_number = $b_info->car->car_man_number;
                    $order_row->car_man_phone = $b_info->car->phone;
                    $order_row->user_id = $user_id;
                    $order_row->status_id = 1;
                    $order_row->id_man = $b_info->car->car_man;
                    $order_row->id_man_number = $b_info->car->id_number;
                    $order_row->link_man = $b_info->car->car_man;
                    $order_row->link_man_phone = $b_info->car->phone;
                    $order_row->link_man_number = $b_info->car->id_number;
                    if(!$order_row->save()) {
                        throw new \Exception(print_r($order_row->getErrors(),true));
                    }

                    //添加保单操作员
                    $order_user_info = new OrderUserT();
                    $order_user_info->user_id = $user_id;
                    $order_user_info->order_id = $order_row->id;
                    $order_user_info->type_id = 1;
                    $order_user_info->status = 0;
                    if(!$order_user_info->save()) {
                        throw new \Exception(print_r($order_user_info->getErrors(),true));
                    }

                    //添加预约
                    AppointmentT::deleteAll('car_id='.$b_info->id);
                    $row = new AppointmentT();
                    $row->car_id = $b_info->id;
                    $row->user_id = $user_id;
                    $row->pdate = date('Y-m-d');
                    $row->ptype = 0;
                    $row->ptime = '09:00';
                    $row->remark = '首次分配';
                    $row->is_first = 1;
                    $row->xdate = date('Y-m-d');
                    $row->xtime = '09:00';
                    $row->xbao = 2;
                    if(!$row->save()) {
                        throw new \Exception(print_r($row->getErrors(),true));
                    }

                    //添加历史预约
                    $h_row = new AppointmentHistoryT();
                    $h_row->car_id = $b_info->id;
                    $h_row->user_id = $user_id;
                    $h_row->pdate = date('Y-m-d');
                    $h_row->ptype = 0;
                    $h_row->ptime = '09:00';
                    $h_row->remark = '首次分配';
                    $h_row->xdate = date('Y-m-d');
                    $h_row->xtime = '09:00';
                    $h_row->xbao = 2;
                    if(!$h_row->save()) {
                        throw new \Exception(print_r($h_row->getErrors(),true));
                    }

                    $this->addLog($b_info->id,'从B库分配数据给业务员:'.$b_info->user->getShowName(),1);
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }
    //勾选B库回收
    public function actionBReturn()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = explode(',',$request->post('ids'));
            if($ids[0] == '') {
                $result['msg'] = '请先钩选需要操作的车辆';
                return $result;
            }
            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($ids as $id) {
                    $b_info = CarBT::findOne(['id'=>$id]);
                    $this->addLog($b_info->id,'从B库业务手中回收数据',1);

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

                    //删除保单 还没有正本生成的保单可以直接删除,正本生成后,需要退单操作
                    //删除操作人员
                    $orders = OrderT::find()
                        ->where('car_id='.$b_info->id.' and status_id<5')
                        ->all();
                    foreach($orders as $order_info) {
                        OrderUserT::deleteAll('order_id='.$order_info->id);
                        //删除保单
                        $order_info->delete();
                    }
                    $b_info->user_id = 0;
                    $b_info->save();
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }
    //批量B库分配
    public function actionBAllAssign()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $status = $request->post('status');
            $assign_users = explode(',',$request->post('assign_users'));
            $assign_count = $request->post('assign_count',0);
            $assign_user_ids = array();
            foreach($assign_users as $assign_user) {
                $user_info = UserT::findOne(['username'=>$assign_user,'is_delete'=>0]);
                if($user_info)
                    $assign_user_ids[] = $user_info->id;
            }
            if(count($assign_user_ids) == 0) {
                $result['msg'] = '请输入业务员工号,以英文逗号间隔';
                return $result;
            }
            if($assign_count == 0) {
                $result['msg'] = '请输入每个业务员分配记录数';
                return $result;
            }

            $data = $this->getBQuery($request);
            $query = $data['query'];
//            var_dump($query);
//            echo $query->createCommand()->rawSql;
//            echo $query->count();
//            exit;
            $tran = CarT::getDb()->beginTransaction();
            try {
                $page = 0;
                $total_pages = count($assign_user_ids);
                $index = 0;
                foreach($query->each(100) as $b_info) {
                    if($index >= ($page+1) * $assign_count) $page += 1;
                    if($page == $total_pages) break;
                    if($b_info->user_id > 0) continue;
                    $b_info->user_id = $assign_user_ids[$page];
                    if(!$b_info->save()) {
                        throw new \Exception(print_r($b_info->getErrors(),true));
                    }

                    $car_info = $b_info->car;
                    $car_info->op_user1 = $b_info->user->getShowName();
                    if(!$car_info->save(false)) {
                        throw new \Exception(print_r($car_info->getErrors(), true));
                    }

                    //添加保单
                    $order_row = new OrderT();
                    $order_row->car_id = $b_info->id;
                    $order_row->car_no = $b_info->car->car_no;
                    $order_row->engine_no = $b_info->car->engine_no;
                    $order_row->car_frame_no = $b_info->car->car_frame_no;
                    $order_row->car_man = $b_info->car->car_man;
                    $order_row->user_id = $b_info->user_id;
                    $order_row->status_id = 1;
                    $order_row->id_man = $b_info->car->car_man;
                    $order_row->link_man = $b_info->car->car_man;
                    $order_row->link_man_phone = $b_info->car->phone;
                    $order_row->link_man_number = $b_info->car->id_number;
                    $order_row->return_status_id = 0;
                    $order_row->return_remark = '';
                    if(!$order_row->save()) {
                        throw new \Exception(print_r($order_row->getErrors(),true));
                    }

                    //添加预约
                    AppointmentT::deleteAll('car_id='.$b_info->id);
                    $row = new AppointmentT();
                    $row->car_id = $b_info->id;
                    $row->user_id = $b_info->user_id;
                    $row->pdate = date('Y-m-d');
                    $row->ptime = '09:00';
                    $row->ptype = 0;
                    $row->remark = '首次分配';
                    $row->is_first = 1;
                    $row->xdate = date('Y-m-d');
                    $row->xtime = '09:00';
                    $row->xbao = 2;
                    if(!$row->save()) {
                        throw new \Exception(print_r($row->getErrors(),true));
                    }

                    //添加历史预约
                    $h_row = new AppointmentHistoryT();
                    $h_row->car_id = $b_info->id;
                    $h_row->user_id = $b_info->user_id;
                    $h_row->pdate = date('Y-m-d');
                    $h_row->ptime = '09:00';
                    $h_row->ptype = 0;
                    $h_row->remark = '首次分配';
                    $h_row->xdate = date('Y-m-d');
                    $h_row->xtime = '09:00';
                    $h_row->xbao = 2;
                    if(!$h_row->save()) {
                        throw new \Exception(print_r($h_row->getErrors(),true));
                    }

                    $this->addLog($b_info->id,'从B库分配数据给业务员:'.$b_info->user->getShowName(),1);
                    $index++;
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }
    //批量B库回收
    public function actionBReturnAllA()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {

            $status = $request->post('status');
            $register_begin = $request->post('register_begin');
            $register_end = $request->post('register_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');

            $query = CarBT::find()
                ->leftJoin('car_t','`car_t`.`id`=`car_b_t`.`id`');

            if($register_begin != '') {
                $query->andWhere('car_t.register_date>="'.$register_begin.'"');
            }
            if($register_end != '') {
                $query->andWhere('car_t.register_date<="'.$register_end.'"');
            }
            if($insurer1_begin != '') {
                $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
            }
            if($insurer1_end != '') {
                $query->andWhere('car_t.insurer1_end<="'.$insurer1_end.'"');
            }
            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(1000) as $item) {
                    //删除预约
                    AppointmentT::deleteAll('car_id='.$item->id);

                    //删除保单 还没有正本生成的保单可以直接删除,正本生成后,需要退单操作
                    //删除操作人员
                    $orders = OrderT::find()
                        ->where('car_id='.$item->id.' and status_id<5')
                        ->all();
                    foreach($orders as $order_info) {
                        OrderUserT::deleteAll('order_id='.$order_info->id);
                        //删除保单
                        $order_info->delete();
                    }

                    $item->user_id = 0;
                    $item->save();
//            FiltrationT
                    //删除预约拨打
//                    FiltrationT::deleteAll('car_id='.$c_info->id);
                    $this->addLog($item->id,'从B库业务员手中批量回收数据',1);
                }

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

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }
    //钩选B库到无效库
    public function actionBInvalid()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = explode(',',$request->post('ids'));
            $invalid_id = $request->post('invalid_id',0);
            if($invalid_id == 0) {
                $result['msg'] = '请选择无效理由';
                return $result;
            }
            if($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 = 5;
                        if(!$car_info->save(false)) {
                            throw new Exception(print_r($car_info->getFirstErrors(),true));
                        }

                        $invalid_info = CarInvalidT::findOne(['id'=>$car_info->id]);
                        if(!$invalid_info) {
                            $invalid_info = new CarInvalidT(false);
                            $invalid_info->id = $car_info->id;
                            $invalid_info->user_id = $this->my->id;
                            $invalid_info->invalid_id = $invalid_id;
                            if(!$invalid_info->save(false)) {
                                throw new Exception(print_r($invalid_info->getFirstErrors(),true));
                            }
                        }

                        $b_info = CarBT::findOne(['id'=>$id]);
                        if($b_info->user_id > 0) {
                            //删除预约
                            AppointmentT::deleteAll('car_id='.$b_info->id);

                            //删除保单 还没有正本生成的保单可以直接删除,正本生成后,需要退单操作
                            //删除操作人员
                            $orders = OrderT::find()
                                ->where('car_id='.$b_info->id.' and status_id<5')
                                ->all();
                            foreach($orders as $order_info) {
                                OrderUserT::deleteAll('order_id='.$order_info->id);
                                //删除保单
                                $order_info->delete();
                            }
                        }
                        $this->addLog($car_info->id,'从B库移动到无效库',1);
                    }
                }

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

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }

    //C库
    //C库列表页
    private function getCQuery($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $print_begin = $request->post('print_begin');
            $print_end = $request->post('print_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $insurer1_month = $request->post('insurer1_month');
            $insurer1_day = $request->post('insurer1_day');
            $insurer1_month1 = $request->post('insurer1_month1');
            $insurer1_day1 = $request->post('insurer1_day1');

            $status = $request->post('status');
            $username = $request->post('username');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
            $old_user = $request->post('old_user');
            $company = $request->post('company');
            $user1 = $request->post('user1');
            $clean_right = $request->post('clean_right');
        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $print_begin = $request->get('print_begin');
            $print_end = $request->get('print_end');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $insurer1_month = $request->get('insurer1_month');
            $insurer1_day = $request->get('insurer1_day');
            $insurer1_month1 = $request->get('insurer1_month1');
            $insurer1_day1 = $request->get('insurer1_day1');

            $status = $request->get('status');
            $username = $request->get('username');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
            $old_user = $request->get('old_user');
            $company = $request->get('company');
            $user1 = $request->get('user1');
            $clean_right = $request->get('clean_right');
        }
        $query = CarCT::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`car_c_t`.`id`');

        if($car_man != '') {
            $query->andWhere('car_t.car_man like "'.$car_man.'"');
        }
        if($phone != '') {
            $query->andWhere('car_t.phone like "'.$phone.'"');
        }
        if($car_no != '') {
            $query->andWhere('car_t.car_no like "'.$car_no.'"');
        }
        if($print_begin != '') {
            $query->andWhere('car_t.print_date>="'.$print_begin.'"');
        }
        if($print_end != '') {
            $query->andWhere('car_t.print_date<="'.$print_end.'"');
        }
        if($insurer1_begin != '') {
            $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }
        if($insurer1_month != '') {
            $query->andWhere('month(car_t.insurer1_date)>="'.$insurer1_month.'"');
        }
        if($insurer1_day != '') {
            $query->andWhere('day(car_t.insurer1_date)>="'.$insurer1_day.'"');
        }
        if($insurer1_month1 != '') {
            $query->andWhere('month(car_t.insurer1_date)<="'.$insurer1_month1.'"');
        }
        if($insurer1_day1 != '') {
            $query->andWhere('day(car_t.insurer1_date)<="'.$insurer1_day1.'"');
        }

        if($status == 1) {
            $query->andWhere('car_c_t.user_id=0');
        }
        if($status == 2) {
            $query->andWhere('car_c_t.user_id>0');
        }
        if($username != '') {
            $user_info = UserT::findOne(['username'=>$username]);
            if($user_info) {
                $query->andWhere('car_c_t.user_id='.$user_info->id);
            }
        }
        if($old_user != '') {
            $query->andWhere('car_t.old_user="'.$old_user.'"');
        }
        if($company != '') {
            $query->andWhere('company like"'.$company.'%"');
        }
        if($user1 != '') {
            $query->andWhere('op_user1 like "'.$user1.'%"');
        }
        if($clean_right != '') {
            $query->andWhere('car_t.clean_right="'.$clean_right.'"');
        }
        $query->orderBy($sort_key.' '.$sort_value.', car_c_t.id '.$sort_value);
        return ['query'=>$query];
    }
    public function actionCIndex()
    {
        $user_items = $this->my->getChildren();
        $invalid_items = InvalidT::getTree();
        return $this->render('c-index',[
            'user_items' => $user_items,
            'invalid_items' => $invalid_items
        ]);
    }
    public function actionCIndexJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $offset = $request->get('offset',0);
        $limit = $request->get('limit', 10);

        $data = $this->getCQuery($request);
        $query = $data['query'];
//        $query->andWhere('location=1');

        $total = $query->count();

        $query->orderBy('id desc');
        $query = $query->offset($offset)->limit($limit);
        $items = $query->all();

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($items as $item) {
            $car_info = $item->car;
            if(!$car_info) continue;
            $row = $car_info->toArray();
            $row['factory_model'] = MyLib::substr_cut($car_info->factory_model, 8);
            $row['car_man'] = MyLib::substr_cut($car_info->car_man, 8);
            $row['op_user'] = $item->user?$item->user->getShowName():'';
            $data['rows'][] = $row;
        }
        return $data;
    }
    public function actionCAssign()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = explode(',',$request->post('ids'));
            $user_id = $request->post('user_id',0);
            //针对数据处理的数据
            if($user_id == 0) {
                $result['msg'] = '请先选择分配给哪个业务员';
                return $result;
            }
            if($ids[0] == '') {
                $result['msg'] = '请先钩选车辆';
                return $result;
            }
            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($ids as $id) {
                    $c_info = CarCT::findOne(['id'=>$id]);
                    if($c_info->user_id > 0) {
                        continue;
                    }
                    $c_info->user_id = $user_id;
                    if(!$c_info->save()) {
                        throw new \Exception(print_r($c_info->getErrors(),true));
                    }

                    //添加保单
                    $order_row = new OrderT();
                    $order_row->car_id = $c_info->id;
                    $order_row->car_no = $c_info->car->car_no;
                    $order_row->engine_no = $c_info->car->engine_no;
                    $order_row->car_frame_no = $c_info->car->car_frame_no;
                    $order_row->car_man = $c_info->car->car_man;
                    $order_row->car_man_number = $c_info->car->car_man_number;
                    $order_row->car_man_phone = $c_info->car->phone;
                    $order_row->user_id = $user_id;
                    $order_row->status_id = 1;
                    $order_row->id_man = $c_info->car->car_man;
                    $order_row->id_man_number = $c_info->car->id_number;
                    $order_row->link_man = $c_info->car->car_man;
                    $order_row->link_man_phone = $c_info->car->phone;
                    $order_row->link_man_number = $c_info->car->id_number;
                    if(!$order_row->save()) {
                        throw new \Exception(print_r($order_row->getErrors(),true));
                    }

                    //添加保单操作员
                    $order_user_info = new OrderUserT();
                    $order_user_info->user_id = $user_id;
                    $order_user_info->order_id = $order_row->id;
                    $order_user_info->type_id = 1;
                    $order_user_info->status = 0;
                    if(!$order_user_info->save()) {
                        throw new \Exception(print_r($order_user_info->getErrors(),true));
                    }

                    //添加预约
                    AppointmentT::deleteAll('car_id='.$c_info->id);
                    $row = new AppointmentT();
                    $row->car_id = $c_info->id;
                    $row->user_id = $user_id;
                    $row->pdate = date('Y-m-d');
                    $row->ptype = 0;
                    $row->ptime = '09:00';
                    $row->remark = '首次分配';
                    $row->is_first = 1;
                    $row->xdate = date('Y-m-d');
                    $row->xtime = '09:00';
                    $row->xbao = 2;
                    if(!$row->save()) {
                        throw new \Exception(print_r($row->getErrors(),true));
                    }

                    //添加历史预约
                    $h_row = new AppointmentHistoryT();
                    $h_row->car_id = $c_info->id;
                    $h_row->user_id = $user_id;
                    $h_row->pdate = date('Y-m-d');
                    $h_row->ptype = 0;
                    $h_row->ptime = '09:00';
                    $h_row->remark = '首次分配';
                    $h_row->xdate = date('Y-m-d');
                    $h_row->xtime = '09:00';
                    $h_row->xbao = 2;
                    if(!$h_row->save()) {
                        throw new \Exception(print_r($h_row->getErrors(),true));
                    }

                    $this->addLog($c_info->id,'从C库分配数据给业务员:'.$c_info->user->getShowName(),1);
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }
    public function actionCReturn()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = explode(',',$request->post('ids'));

            if($ids[0] == '') {
                $result['msg'] = '请先钩选车辆';
                return $result;
            }
            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($ids as $id) {
                    $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;
                    if(!$car_info->save()) {
                        throw new \Exception(print_r($car_info->getErrors(),true));
                    }

                    $c_info->user_id = 0;
                    if(!$c_info->save()) {
                        throw new \Exception(print_r($c_info->getErrors(),true));
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }
    public function actionCAllAssign()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $status = $request->post('status');
            $assign_users = explode(',',$request->post('assign_users'));
            $assign_count = $request->post('assign_count',0);
            $assign_user_ids = array();
            foreach($assign_users as $assign_user) {
                $user_info = UserT::findOne(['username'=>$assign_user,'is_delete'=>0]);
                if($user_info)
                    $assign_user_ids[] = $user_info->id;
            }
            if(count($assign_user_ids) == 0) {
                $result['msg'] = '请输入业务员工号,以英文逗号间隔';
                return $result;
            }
            if($assign_count == 0) {
                $result['msg'] = '请输入每个业务员分配记录数';
                return $result;
            }

            $data = $this->getCQuery($request);
            $query = $data['query'];
//            var_dump($query);
//            echo $query->createCommand()->rawSql;
//            echo $query->count();
//            exit;
            $tran = CarT::getDb()->beginTransaction();
            try {
                $page = 0;
                $total_pages = count($assign_user_ids);
                $index = 0;
                foreach($query->each(100) as $c_info) {
                    if($index >= ($page+1) * $assign_count) $page += 1;
                    if($page == $total_pages) break;
                    if($c_info->user_id > 0) continue;
                    $c_info->user_id = $assign_user_ids[$page];
                    if(!$c_info->save()) {
                        throw new \Exception(print_r($c_info->getErrors(),true));
                    }

                    $car_info = $c_info->car;
                    $car_info->op_user1 = $c_info->user->getShowName();
                    if(!$car_info->save(false)) {
                        throw new \Exception(print_r($car_info->getErrors(), true));
                    }

                    //添加保单
                    $order_row = new OrderT();
                    $order_row->car_id = $c_info->id;
                    $order_row->car_no = $c_info->car->car_no;
                    $order_row->engine_no = $c_info->car->engine_no;
                    $order_row->car_frame_no = $c_info->car->car_frame_no;
                    $order_row->car_man = $c_info->car->car_man;
                    $order_row->car_man_number = $c_info->car->car_man_number;
                    $order_row->car_man_phone = $c_info->car->phone;
                    $order_row->user_id = $c_info->user_id;
                    $order_row->status_id = 1;
                    $order_row->id_man = $c_info->car->car_man;
                    $order_row->id_man_number = $c_info->car->id_number;
                    $order_row->link_man = $c_info->car->car_man;
                    $order_row->link_man_phone = $c_info->car->phone;
                    $order_row->link_man_number = $c_info->car->id_number;
                    if(!$order_row->save()) {
                        throw new \Exception(print_r($order_row->getErrors(),true));
                    }

                    //添加保单操作员
                    $order_user_info = new OrderUserT();
                    $order_user_info->user_id = $c_info->user_id;
                    $order_user_info->order_id = $order_row->id;
                    $order_user_info->type_id = 1;
                    $order_user_info->status = 0;
                    if(!$order_user_info->save()) {
                        throw new \Exception(print_r($order_user_info->getErrors(),true));
                    }

                    //添加预约
                    AppointmentT::deleteAll('car_id='.$c_info->id);
                    $row = new AppointmentT();
                    $row->car_id = $c_info->id;
                    $row->user_id = $c_info->user_id;
                    $row->pdate = date('Y-m-d');
                    $row->ptype = 0;
                    $row->ptime = '09:00';
                    $row->remark = '首次分配';
                    $row->is_first = 1;
                    $row->xdate = date('Y-m-d');
                    $row->xtime = '09:00';
                    $row->xbao = 2;
                    if(!$row->save()) {
                        throw new \Exception(print_r($row->getErrors(),true));
                    }

                    //添加历史预约
                    $h_row = new AppointmentHistoryT();
                    $h_row->car_id = $c_info->id;
                    $h_row->user_id = $c_info->user_id;
                    $h_row->pdate = date('Y-m-d');
                    $h_row->ptype = 0;
                    $h_row->ptime = '09:00';
                    $h_row->remark = '首次分配';
                    $h_row->xdate = date('Y-m-d');
                    $h_row->xtime = '09:00';
                    $h_row->xbao = 2;
                    if(!$h_row->save()) {
                        throw new \Exception(print_r($h_row->getErrors(),true));
                    }

                    $this->addLog($c_info->id,'从C库分配数据给业务员:'.$c_info->user->getShowName(),1);
                    $index++;
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }
    public function actionCReturnAll()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {

            $status = $request->post('status');
            $register_begin = $request->post('register_begin');
            $register_end = $request->post('register_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');

            $query = CarCT::find()
                ->leftJoin('car_t','`car_t`.`id`=`car_c_t`.`id`');

            if($register_begin != '') {
                $query->andWhere('car_t.register_date>="'.$register_begin.'"');
            }
            if($register_end != '') {
                $query->andWhere('car_t.register_date<="'.$register_end.'"');
            }
            if($insurer1_begin != '') {
                $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
            }
            if($insurer1_end != '') {
                $query->andWhere('car_t.insurer1_end<="'.$insurer1_end.'"');
            }
            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(1000) as $item) {
                    //删除预约
                    AppointmentT::deleteAll('car_id='.$item->id);

                    //删除保单 还没有正本生成的保单可以直接删除,正本生成后,需要退单操作
                    //删除操作人员
                    $orders = OrderT::find()
                        ->where('car_id='.$item->id.' and status_id<5')
                        ->all();
                    foreach($orders as $order_info) {
                        OrderUserT::deleteAll('order_id='.$order_info->id);
                        //删除保单
                        $order_info->delete();
                    }

                    $item->user_id = 0;
                    if(!$item->save()) {
                        throw new \Exception(print_r($item->getErrors(), true));
                    }
//            FiltrationT
                    //删除预约拨打
//                    FiltrationT::deleteAll('car_id='.$c_info->id);
                    $this->addLog($item->id,'从C库业务员手中批量回收数据',1);
                }

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

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }
    public function actionCD()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = explode(',',$request->post('ids'));
            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 == 3) {
                        $car_info->location = 4;
                        if(!$car_info->save(false)) {
                            throw new Exception(print_r($car_info->getFirstErrors(),true));
                        }

                        CarCT::deleteAll(['id'=>$car_info->id]);
                        $d_info = CarDT::findOne(['id'=>$car_info->id]);
                        if(!$d_info) {
                            $d_info = new CarDT(false);
                            $d_info->id = $car_info->id;
                            $d_info->user_id = 0;
                            if(!$d_info->save(false)) {
                                throw new Exception(print_r($d_info->getFirstErrors(),true));
                            }
                        }
                        $this->addLog($car_info->id,'从C库分配到D库',1);
                    }
                }

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

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }
    public function actionCAllD()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $data = $this->getCQuery($request);
            $query = $data['query'];
            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $c_info) {
                    $car_info = $c_info->car;
                    if($car_info->location == 3) {
                        $car_info->location = 4;
                        if(!$car_info->save(false)) {
                            throw new Exception(print_r($car_info->getFirstErrors(),true));
                        }

                        CarCT::deleteAll(['id'=>$car_info->id]);
                        $d_info = CarDT::findOne(['id'=>$car_info->id]);
                        if(!$d_info) {
                            $d_info = new CarDT(false);
                            $d_info->id = $car_info->id;
                            $d_info->user_id = 0;
                            if(!$d_info->save(false)) {
                                throw new Exception(print_r($d_info->getFirstErrors(),true));
                            }
                        }
                        $this->addLog($car_info->id,'从C库分配到D库',1);
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }
    //钩选C库到无效库
    public function actionCInvalid()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = explode(',',$request->post('ids'));
            $invalid_id = $request->post('invalid_id',0);
            if($invalid_id == 0) {
                $result['msg'] = '请选择无效理由';
                return $result;
            }
            if($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 == 3) {
                        $car_info->location = 5;
                        if(!$car_info->save(false)) {
                            throw new Exception(print_r($car_info->getFirstErrors(),true));
                        }

                        $invalid_info = CarInvalidT::findOne(['id'=>$car_info->id]);
                        if(!$invalid_info) {
                            $invalid_info = new CarInvalidT(false);
                            $invalid_info->id = $car_info->id;
                            $invalid_info->user_id = $this->my->id;
                            $invalid_info->invalid_id = $invalid_id;
                            if(!$invalid_info->save(false)) {
                                throw new Exception(print_r($invalid_info->getFirstErrors(),true));
                            }
                        }

                        $c_info = CarCT::findOne(['id'=>$id]);
                        if($c_info->user_id > 0) {
                            //删除预约
                            AppointmentT::deleteAll('car_id='.$c_info->id);

                            //删除保单 还没有正本生成的保单可以直接删除,正本生成后,需要退单操作
                            //删除操作人员
                            $orders = OrderT::find()
                                ->where('car_id='.$c_info->id.' and status_id<5')
                                ->all();
                            foreach($orders as $order_info) {
                                OrderUserT::deleteAll('order_id='.$order_info->id);
                                //删除保单
                                $order_info->delete();
                            }
                        }
                        $this->addLog($car_info->id,'从C库移动到无效库',1);
                    }
                }

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

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }

    //D库
    //D库列表页
    private function getDQuery($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $insurer1_month = $request->post('insurer1_month');
            $insurer1_day = $request->post('insurer1_day');
            $insurer1_month1 = $request->post('insurer1_month1');
            $insurer1_day1 = $request->post('insurer1_day1');
            $status = $request->post('status');
            $username = $request->post('username');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
            $old_user = $request->post('old_user');
            $user1 = $request->post('user1');
            $user2 = $request->post('user2');
            $company = $request->post('company');
        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $insurer1_month = $request->get('insurer1_month');
            $insurer1_day = $request->get('insurer1_day');
            $insurer1_month1 = $request->get('insurer1_month1');
            $insurer1_day1 = $request->get('insurer1_day1');
            $status = $request->get('status');
            $username = $request->get('username');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
            $old_user = $request->get('old_user');
            $user1 = $request->get('user1');
            $user2 = $request->get('user2');
            $company = $request->get('company');
        }
        $params = array();
        $params['car_man'] = $car_man;
        $params['phone'] = $phone;
        $params['car_no'] = $car_no;
        $params['insurer1_begin'] = $insurer1_begin;
        $params['insurer1_end'] = $insurer1_end;
        $params['insurer1_month'] = $insurer1_month;
        $params['insurer1_day'] = $insurer1_day;
        $params['insurer1_month1'] = $insurer1_month1;
        $params['insurer1_day1'] = $insurer1_day1;
        $params['status'] = $status;
        $params['username'] = $username;
        $params['sort_key'] = $sort_key;
        $params['sort_value'] = $sort_value;
        $params['old_user'] = $old_user;
        $params['user1'] = $user1;
        $params['user2'] = $user2;
        $params['company'] = $company;

        $query = CarDT::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`car_d_t`.`id`');
        if($car_man != '') {
            $query->andWhere('car_t.car_man="'.$car_man.'"');
        }
        if($phone != '') {
            $query->andWhere('car_t.phone like "'.$phone.'"');
        }
        if($car_no != '') {
            $query->andWhere('car_t.car_no like "'.$car_no.'"');
        }
        if($insurer1_begin != '') {
            $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }
        if($insurer1_month != '') {
            $query->andWhere('month(car_t.insurer1_date)>="'.$insurer1_month.'"');
        }
        if($insurer1_day != '') {
            $query->andWhere('day(car_t.insurer1_date)>="'.$insurer1_day.'"');
        }
        if($insurer1_month1 != '') {
            $query->andWhere('month(car_t.insurer1_date)<="'.$insurer1_month1.'"');
        }
        if($insurer1_day1 != '') {
            $query->andWhere('day(car_t.insurer1_date)<="'.$insurer1_day1.'"');
        }
        if($status == 1) {
            $query->andWhere('car_d_t.user_id=0');
        }
        if($status == 2) {
            $query = $query->andWhere('car_d_t.user_id>0');
        }
        if($username != '') {
            $user_info = UserT::findOne(['username'=>$username]);
            if($user_info) {
                $query->andWhere('car_d_t.user_id='.$user_info->id);
            }
        }
        if($old_user != '') {
            $query->andWhere('car_t.old_user like "'.$old_user.'%"');
        }
        if($user1 != '') {
            $query->andWhere('car_t.op_user1 like "'.$user1.'%"');
        }
        if($user2 != '') {
            $query->andWhere('car_t.op_user3 like "'.$user2.'%"');
        }
        if($company != '') {
            $query->andWhere('company like "'.$company.'%"');
        }
        $query->orderBy($sort_key.' '.$sort_value.',car_d_t.id '.$sort_value);
        return ['query'=>$query,'params'=>$params];
    }
    public function actionDIndex()
    {
        $user_items = $this->my->getChildren();
        $invalid_items = InvalidT::getTree();
        return $this->render('d-index',[
            'user_items' => $user_items,
            'invalid_items' => $invalid_items
        ]);
    }
    public function actionDIndexJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $offset = $request->get('offset',0);
        $limit = $request->get('limit', 10);

        $data = $this->getDQuery($request);
        $query = $data['query'];
//        $query->andWhere('location=1');
        $total = $query->count();

        $query->orderBy('id desc');
        $query = $query->offset($offset)->limit($limit);
        $items = $query->all();

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($items as $item) {
            $car_info = $item->car;
            if(!$car_info) continue;
            $row = $car_info->toArray();
            $row['factory_model'] = MyLib::substr_cut($car_info->factory_model, 8);
            $row['car_man'] = MyLib::substr_cut($car_info->car_man, 8);
            $row['op_user'] = $item->user?$item->user->getShowName():'';
            $data['rows'][] = $row;
        }
        return $data;
    }
    public function actionDAssign()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = explode(',',$request->post('ids'));
            $user_id = $request->post('user_id',0);
            //针对数据处理的数据
            if($user_id == 0) {
                $result['msg'] = '请先选择分配给哪个业务员';
                return $result;
            }
            if($ids[0] == '') {
                $result['msg'] = '请先钩选车辆';
                return $result;
            }
            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($ids as $id) {
                    $d_info = CarDT::findOne(['id'=>$id]);
                    if($d_info->user_id > 0) {
                        continue;
                    }
                    $d_info->user_id = $user_id;
                    if(!$d_info->save()) {
                        throw new \Exception(print_r($d_info->getErrors(),true));
                    }

                    //添加保单
                    $order_row = new OrderT();
                    $order_row->car_id = $d_info->id;
                    $order_row->car_no = $d_info->car->car_no;
                    $order_row->engine_no = $d_info->car->engine_no;
                    $order_row->car_frame_no = $d_info->car->car_frame_no;
                    $order_row->car_man = $d_info->car->car_man;
                    $order_row->car_man_number = $d_info->car->car_man_number;
                    $order_row->car_man_phone = $d_info->car->phone;
                    $order_row->user_id = $user_id;
                    $order_row->status_id = 1;
                    $order_row->id_man = $d_info->car->car_man;
                    $order_row->id_man_number = $d_info->car->id_number;
                    $order_row->link_man = $d_info->car->car_man;
                    $order_row->link_man_phone = $d_info->car->phone;
                    $order_row->link_man_number = $d_info->car->id_number;
                    if(!$order_row->save()) {
                        throw new \Exception(print_r($order_row->getErrors(),true));
                    }

                    //添加保单操作员
                    $order_user_info = new OrderUserT();
                    $order_user_info->user_id = $user_id;
                    $order_user_info->order_id = $order_row->id;
                    $order_user_info->type_id = 1;
                    $order_user_info->status = 0;
                    if(!$order_user_info->save()) {
                        throw new \Exception(print_r($order_user_info->getErrors(),true));
                    }

                    //添加预约
                    AppointmentT::deleteAll('car_id='.$d_info->id);
                    $row = new AppointmentT();
                    $row->car_id = $d_info->id;
                    $row->user_id = $user_id;
                    $row->pdate = date('Y-m-d');
                    $row->ptype = 0;
                    $row->ptime = '09:00';
                    $row->remark = '首次分配';
                    $row->is_first = 1;
                    $row->xdate = date('Y-m-d');
                    $row->xtime = '09:00';
                    $row->xbao = 2;
                    if(!$row->save()) {
                        throw new \Exception(print_r($row->getErrors(),true));
                    }

                    //添加历史预约
                    $h_row = new AppointmentHistoryT();
                    $h_row->car_id = $d_info->id;
                    $h_row->user_id = $user_id;
                    $h_row->pdate = date('Y-m-d');
                    $h_row->ptype = 0;
                    $h_row->ptime = '09:00';
                    $h_row->remark = '首次分配';
                    $h_row->xdate = date('Y-m-d');
                    $h_row->xtime = '09:00';
                    $h_row->xbao = 2;
                    if(!$h_row->save()) {
                        throw new \Exception(print_r($h_row->getErrors(),true));
                    }

                    $this->addLog($d_info->id,'从D库分配数据给业务员:'.$d_info->user->getShowName(),1);
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }
    public function actionDReturn()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = explode(',',$request->post('ids'));

            if($ids[0] == '') {
                $result['msg'] = '请先钩选车辆';
                return $result;
            }
            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($ids as $id) {
                    $d_info = CarDT::findOne(['id'=>$id]);
                    if($d_info->user_id > 0)
                        $this->addLog($d_info->id,'从业务员:'.$d_info->user->getShowName().'回收跟踪数据',1);
                    //删除预约
                    AppointmentT::deleteAll('user_id='.$d_info->user_id.' and car_id='.$d_info->id);

                    $car_info = $d_info->car;
                    $car_info->user_id = 0;
                    if(!$car_info->save()) {
                        throw new \Exception(print_r($car_info->getErrors(),true));
                    }

                    $d_info->user_id = 0;
                    if(!$d_info->save()) {
                        throw new \Exception(print_r($d_info->getErrors(),true));
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }
    public function actionDAllAssign()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $assign_users = explode(',',$request->post('assign_users'));
            $assign_count = $request->post('assign_count',0);
            $assign_user_ids = array();
            foreach($assign_users as $assign_user) {
                $user_info = UserT::findOne(['username'=>$assign_user]);
                if($user_info)
                    $assign_user_ids[] = $user_info->id;
            }
            if(count($assign_user_ids) == 0) {
                $result['msg'] = '请输入业务员工号,以英文逗号间隔';
                return $result;
            }
            if($assign_count == 0) {
                $result['msg'] = '请输入每个业务员分配记录数';
                return $result;
            }

            $data = $this->getDQuery($request);
            $query = $data['query'];
            $tran = CarT::getDb()->beginTransaction();

            try {
                $page = 0;
                $total_pages = count($assign_user_ids);
                $index = 0;
                foreach($query->each(100) as $d_info) {
                    if($index >= ($page+1) * $assign_count) $page += 1;
                    if($page == $total_pages) break;
                    if($d_info->user_id > 0) continue;

                    $d_info->user_id = $assign_user_ids[$page];
                    if(!$d_info->save()) {
                        throw new \Exception(print_r($d_info->getErrors(),true));
                    }

                    $car_info = $d_info->car;
                    $car_info->op_user3 = $d_info->user->getShowName();
                    if(!$car_info->save()) {
                        throw new \Exception(print_r($car_info->getErrors(),true));
                    }

                    //添加保单
                    $order_row = new OrderT();
                    $order_row->car_id = $d_info->id;
                    $order_row->car_no = $d_info->car->car_no;
                    $order_row->engine_no = $d_info->car->engine_no;
                    $order_row->car_frame_no = $d_info->car->car_frame_no;
                    $order_row->car_man = $d_info->car->car_man;
                    $order_row->car_man_number = $d_info->car->car_man_number;
                    $order_row->car_man_phone = $d_info->car->phone;
                    $order_row->user_id = $d_info->user_id;
                    $order_row->status_id = 1;
                    $order_row->id_man = $d_info->car->car_man;
                    $order_row->id_man_number = $d_info->car->id_number;
                    $order_row->link_man = $d_info->car->car_man;
                    $order_row->link_man_phone = $d_info->car->phone;
                    $order_row->link_man_number = $d_info->car->id_number;
                    if(!$order_row->save()) {
                        throw new \Exception(print_r($order_row->getErrors(),true));
                    }

                    //添加保单操作员
                    $order_user_info = new OrderUserT();
                    $order_user_info->user_id = $d_info->user_id;
                    $order_user_info->order_id = $order_row->id;
                    $order_user_info->type_id = 1;
                    $order_user_info->status = 0;
                    if(!$order_user_info->save()) {
                        throw new \Exception(print_r($order_user_info->getErrors(),true));
                    }

                    //添加预约
                    AppointmentT::deleteAll('car_id='.$d_info->id);
                    $row = new AppointmentT();
                    $row->car_id = $d_info->id;
                    $row->user_id = $d_info->user_id;
                    $row->pdate = date('Y-m-d');
                    $row->ptype = 0;
                    $row->ptime = '09:00';
                    $row->remark = '首次分配';
                    $row->is_first = 1;
                    $row->xdate = date('Y-m-d');
                    $row->xtime = '09:00';
                    $row->xbao = 2;
                    if(!$row->save()) {
                        throw new \Exception(print_r($row->getErrors(),true));
                    }

                    //添加历史预约
                    $h_row = new AppointmentHistoryT();
                    $h_row->car_id = $d_info->id;
                    $h_row->user_id = $d_info->user_id;
                    $h_row->pdate = date('Y-m-d');
                    $h_row->ptype = 0;
                    $h_row->ptime = '09:00';
                    $h_row->remark = '首次分配';
                    $h_row->xdate = date('Y-m-d');
                    $h_row->xtime = '09:00';
                    $h_row->xbao = 2;
                    if(!$h_row->save()) {
                        throw new \Exception(print_r($h_row->getErrors(),true));
                    }

                    $this->addLog($d_info->id,'从D库分配数据给业务员:'.$d_info->user->getShowName(),1);
                    $index++;
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }
    public function actionDReturnAll()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {

            $status = $request->post('status');
            $register_begin = $request->post('register_begin');
            $register_end = $request->post('register_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');

            $query = CarDT::find()
                ->leftJoin('car_t','`car_t`.`id`=`car_d_t`.`id`');

            if($register_begin != '') {
                $query->andWhere('car_t.register_date>="'.$register_begin.'"');
            }
            if($register_end != '') {
                $query->andWhere('car_t.register_date<="'.$register_end.'"');
            }
            if($insurer1_begin != '') {
                $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
            }
            if($insurer1_end != '') {
                $query->andWhere('car_t.insurer1_end<="'.$insurer1_end.'"');
            }
            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(1000) as $item) {
                    //删除预约
                    AppointmentT::deleteAll('car_id='.$item->id);

                    //删除保单 还没有正本生成的保单可以直接删除,正本生成后,需要退单操作
                    //删除操作人员
                    $orders = OrderT::find()
                        ->where('car_id='.$item->id.' and status_id<5')
                        ->all();
                    foreach($orders as $order_info) {
                        OrderUserT::deleteAll('order_id='.$order_info->id);
                        //删除保单
                        $order_info->delete();
                    }

                    $item->user_id = 0;
                    if(!$item->save()) {
                        throw new \Exception(print_r($item->getErrors(), true));
                    }
//            FiltrationT
                    //删除预约拨打
//                    FiltrationT::deleteAll('car_id='.$c_info->id);
                    $this->addLog($item->id,'从D库业务员手中批量回收数据',1);
                }

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

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }
    //钩选C库到无效库
    public function actionDInvalid()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = explode(',',$request->post('ids'));
            $invalid_id = $request->post('invalid_id',0);
            if($invalid_id == 0) {
                $result['msg'] = '请选择无效理由';
                return $result;
            }
            if($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 == 4) {
                        $car_info->location = 5;
                        if(!$car_info->save(false)) {
                            throw new Exception(print_r($car_info->getFirstErrors(),true));
                        }

                        $invalid_info = CarInvalidT::findOne(['id'=>$car_info->id]);
                        if(!$invalid_info) {
                            $invalid_info = new CarInvalidT(false);
                            $invalid_info->id = $car_info->id;
                            $invalid_info->user_id = $this->my->id;
                            $invalid_info->invalid_id = $invalid_id;
                            if(!$invalid_info->save(false)) {
                                throw new Exception(print_r($invalid_info->getFirstErrors(),true));
                            }
                        }

                        $d_info = CarDT::findOne(['id'=>$id]);
                        if($d_info->user_id > 0) {
                            //删除预约
                            AppointmentT::deleteAll('car_id='.$d_info->id);

                            //删除保单 还没有正本生成的保单可以直接删除,正本生成后,需要退单操作
                            //删除操作人员
                            $orders = OrderT::find()
                                ->where('car_id='.$d_info->id.' and status_id<5')
                                ->all();
                            foreach($orders as $order_info) {
                                OrderUserT::deleteAll('order_id='.$order_info->id);
                                //删除保单
                                $order_info->delete();
                            }
                        }
                        $this->addLog($car_info->id,'从C库移动到无效库',1);
                    }
                }

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

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }

    //无效库列表页
    private function getInvalidQuery($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $register_begin = $request->post('register_begin');
            $register_end = $request->post('register_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $invalid_id = $request->post('invalid_id');
            $username = $request->post('username');
            $user1 = $request->post('user1');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $register_begin = $request->get('register_begin');
            $register_end = $request->get('register_end');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $invalid_id = $request->get('invalid_id');
            $username = $request->get('username');
            $user1 = $request->get('user1');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
        }
        $params = array();
        $params['car_man'] = $car_man;
        $params['phone'] = $phone;
        $params['car_no'] = $car_no;
        $params['register_begin'] = $register_begin;
        $params['register_end'] = $register_end;
        $params['insurer1_begin'] = $insurer1_begin;
        $params['insurer1_end'] = $insurer1_end;
        $params['invalid_id'] = $invalid_id;
        $params['username'] = $username;
        $params['user1'] = $user1;
        $params['sort_key'] = $sort_key;
        $params['sort_value'] = $sort_value;

        $query = CarInvalidT::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`car_invalid_t`.`id`');

        if($car_man != '') {
            $query->andWhere('car_t.car_man="'.$car_man.'"');
        }
        if($phone != '') {
            $query->andWhere('car_t.phone="'.$phone.'"');
        }
        if($car_no != '') {
            $query->andWhere('car_t.car_no="'.$car_no.'"');
        }
        if($register_begin != '') {
            $query->andWhere('car_t.register_date>="'.$register_begin.'"');
        }
        if($register_end != '') {
            $query->andWhere('car_t.register_date<="'.$register_end.'"');
        }
        if($insurer1_begin != '') {
            $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }
        if($invalid_id > 0) {
            $query->andWhere('car_invalid_t.invalid_id='.$invalid_id);
        }
        if($user1 != '') {
            $query->andWhere('car_t.op_user1 like "'.$user1.'%"');
        }
        if($username != '') {
            $user_info = UserT::findOne(['username'=>$username]);
            if($user_info) {
                $query->andWhere('car_invalid_t.user_id='.$user_info->id);
            }
        }
        $query->orderBy($sort_key.' '.$sort_value.',car_invalid_t.id '.$sort_value);
//        echo $query->createCommand()->rawSql;
        return ['query'=>$query,'params'=>$params];
    }
    public function actionInvalidIndex()
    {
        $invalid_items = InvalidT::getTree();
        return $this->render('invalid-index',[
            'invalid_items'=>$invalid_items
        ]);
    }
    public function actionInvalidIndexJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $offset = $request->get('offset',0);
        $limit = $request->get('limit', 10);

        $data = $this->getInvalidQuery($request);
        $query = $data['query'];
//        $query->andWhere('location=1');
        $total = $query->count();

        $query->orderBy('id desc');
        $query = $query->offset($offset)->limit($limit);
        $items = $query->all();

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($items as $item) {
            $car_info = $item->car;
            if(!$car_info) continue;
            $row = $car_info->toArray();
            $row['factory_model'] = MyLib::substr_cut($car_info->factory_model, 8);
            $row['car_man'] = MyLib::substr_cut($car_info->car_man, 8);
            $row['op_user'] = $item->user?$item->user->getShowName():'';
            $row['invalid_info'] = $item->invalid->getPath();
            $row['invalid_time'] = $item->invalid->created_at;
            $data['rows'][] = $row;
        }
        return $data;
    }

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

        $data = $this->getAQuery($request);
        $query = $data['query'];
        $params = $data['params'];
        $params['page'] = $page;

        if($op==0) {
            $query = $query->andWhere('car_t.id=-1');
        }
//        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::getPageInfo($pagination);

        $invalid_items = InvalidT::getTree();

        $params['items'] = $items;
        $params['page_info'] = $page_info;
        $params['invalid_items'] = $invalid_items;
//        $params['month'] = CarT::MONTH;
//        $params['days'] = CarT::DAYS;


        return $this->render('kufang-index',$params);
    }

    public function actionAClean()
    {
        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 == 1) {
                        if($car_info->qiyong==2){
                            continue;
                        }
                        $car_info->location = 14;
                        $car_info->save();

                        $c_info = CleanT::findOne(['id'=>$car_info->id]);
                        if(!$c_info) {
                            $c_info = new CleanT();
                            $c_info->id = $car_info->id;
                            $c_info->user_id = 0;
//                            $c_info->save();
                            if($c_info->save()){
                                $fenpei= new CleanFenpei();
                                $fenpei->user_id=$this->my->id;
                                $fenpei->fenpei_num=1;
                                $fenpei->fenpei_date=date('Y-m-d');
                                $fenpei->save();

                            }

                        }
                        $this->addLog($car_info->id,'从A库分配到清洗库',1);
                    }
                }

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

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

    public function actionRightMoveB()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';
        $flag = 0;
        if($request->isPost) {
//            $unsel_lib = $request->post('unsel_lib',0);
//            $has_phone = $request->post('has_phone');

            $data = $this->getRightQuery($request);
            $query = $data;

//            if($unsel_lib == 0) {
//                $result['msg'] = '请先选择未分配选项';
//                return $result;
//            }
//            if($has_phone != 1) {
//                $result['msg'] = '请先选择有电话号码选项';
//                return $result;
//            }
            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(1000) as $item) {
                    $car_info = $item->car;
                    if($car_info->location == 15) {
                        $car_info->location = 2;
                        $car_info->save();

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

                            $this->addLog($car_info->id,'批量从清洗库分配到B库',1);
                        }
                    }
                    $item->delete();
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                throw $e;
            }
        }
        return $result;
    }
    public function actionRightB()
    {
        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 == 15) {
                        $car_info->location = 2;
                        $car_info->save();
                        RightT::deleteAll(['id'=>$car_info->id]);
                        $b_info = CarBT::findOne(['id'=>$car_info->id]);
                        if(!$b_info) {
                            $b_info = new CarBT();
                            $b_info->id = $car_info->id;
                            $b_info->user_id = 0;
                            $b_info->save();
                        }
                        $this->addLog($car_info->id,'从数据准确库分配到B库',1);
                    }
                }

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

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    public function actionBC()
    {
        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());

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

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

                    $c_info = new CarCT();
                    $c_info->id = $car_info->id;
                    $c_info->user_id = 0;
                    $c_info->save();

                    $this->addLog($car_info->id,'从B库分配到C库',1);
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

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



    public function actionCA()
    {
        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());

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

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

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

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

    /**
     ***********************************
     * E库到A库
     * 功能
     * @author liukangle
     *********************************
     */
    public function actionEA()
    {

        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) {

                    $e_info = CarET::findOne(['id'=>$id]);

                    if($e_info->user_id > 0) {
                        $result['msg'] = '被分配的不可恢复到A库';
                        return $result;
                    }

                    $car_info = CarT::findOne(['id'=>$id]);
                    $car_info->location = 1;
                    $car_info->save();

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

                    $this->addLog($car_info->id,'从E库恢复到A库',1);
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    
    public function actionED()
    {
        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) {

                    $e_info = CarET::findOne(['id'=>$id]);

                    if($e_info->user_id > 0) {
                        $result['msg'] = '被分配的不可移动到D库';
                        return $result;
                    }

                    $car_info = CarT::findOne(['id'=>$id]);
                    $car_info->location = 4;
                    $car_info->save();

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

                    $c_info = new CarDT();
                    $c_info->id = $car_info->id;
                    $c_info->user_id = 0;
                    $c_info->save();

                    $this->addLog($car_info->id,'从E库分配到D库',1);
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

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

    public function actionDC()
    {
        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());

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

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

                    $c_info = new CarCT();
                    $c_info->id = $car_info->id;
                    $c_info->user_id = 0;
                    $c_info->save();

                    $this->addLog($car_info->id,'从D库分配到C库',1);
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

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

    public function actionAE()
    {
        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 = CarET::getDb()->beginTransaction();
            try {
                //对id数组进行便利,改变其在A库中的字段位置
                foreach($ids as $id) {
                    $car_info = CarT::findOne(['id'=>$id]);
                    if($car_info->location == 1){
                        if($car_info->qiyong==2){
                            continue;
                        }
                        $car_info->location = 6;
                        $car_info->save();

                        //在E库中新加一条记录
                        $e_info = CarET::findOne(['id'=>$car_info->id]);
                        if(!$e_info){
                            $e_info = new CarET();
                            $e_info->id = $car_info->id;
                            $e_info->user_id = 0;
                            $e_info->save();
                        }
                        $this->addLog($car_info->id,'从A库分配到E库',1);
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        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 = explode(',',$request->post('ids'));

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($ids as $id) {
                    $car_info = CarT::findOne(['id'=>$id]);
                    $car_info->location = 1;
                    if(!$car_info->save()) {
                        throw new \Exception(print_r($car_info->getErrors(), true));
                    }

                    CarInvalidT::deleteAll('id='.$id);
                    AppointmentHistoryT::deleteAll('car_id='.$id);
                    $this->addLog($car_info->id,'从无效数据中恢复数据',1);
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }

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

        if($request->isPost) {
            $invalid_id = $request->post('invalid_id');

//            if($invalid_id == 0) {
//                $result['msg'] = '请先选择无效理由';
//                return $result;
//            }
            $data = $this->getInvalidQuery($request);
            $query = $data['query'];

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(1000) as $item) {
                    $car_info = $item->car;
                    $car_info->location = 1;
                    if(!$car_info->save()) {
                        throw new \Exception(print_r($car_info->getErrors(), true));
                    }
                    AppointmentHistoryT::deleteAll('car_id='.$item->id);
                    $item->delete();
                    $this->addLog($item->id,'从无效数据中恢复数据',1);
                }

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

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                $result['msg'] = $e->getMessage();
            }
        }
        return $result;
    }

    public function actionInvalid1A()
    {
        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());

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

                    CarF3T::deleteAll('id='.$id);

                    $this->addLog($car_info->id,'从F3无效数据中恢复数据',1);
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

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

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

        if($request->isPost) {
            $invalid_id = $request->post('invalid_id');

            if($invalid_id == 0) {
                $result['msg'] = '请先选择无效理由';
                return $result;
            }
            $data = $this->getInvalidQuery1($request);
            $query = $data['query'];

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(1000) as $item) {
                    $car_info = $item->car;
                    $car_info->location = 1;
                    $car_info->save();

                    $item->delete();
                    $this->addLog($item->id,'从F3无效数据中恢复数据',1);
                }

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

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                throw $e;
            }
        }
        return $result;
    }
    public function actionInvalid2A()
    {
        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());

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

                    CarF6T::deleteAll('id='.$id);

                    $this->addLog($car_info->id,'从F6无效数据中恢复数据',1);
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    public function actionInvalid3A()
    {
        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());

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($ids as $id) {
                    $car_info = CarT::findOne(['id'=>$id]);
                    $car_bt=CarBT::findOne(['id'=>$id]);
                    $invalid_id=$car_bt->iv_d;
                    if($invalid_id == 57 || $invalid_id == 58 || $invalid_id == 59 || $invalid_id ==60 || $invalid_id ==61) {
                        if($invalid_id == 61){
                            CarF2T::deleteAll('id='.$car_info->id);
                            $c_info = new CarF2T();
                            $c_info->id = $car_info->id;
                            $c_info->user_id = 0;
                            $c_info->save();
                            $car_info->location = 9;
                            $car_info->company = '其他小保险公司';

                        }elseif($invalid_id == 57 || $invalid_id == 58 || $invalid_id == 59 || $invalid_id ==60){

                            CarF1T::deleteAll('id='.$car_info->id);
                            $c_info = new CarF1T();
                            $c_info->id = $car_info->id;
                            $c_info->user_id = 0;

                            switch($invalid_id) {
                                case 57:
                                    $car_info->company = '人保';
                                    break;
                                case 58:
                                    $car_info->company = '平安';
                                    break;
                                case 59:
                                    $car_info->company = '太平洋';
                                    break;
                                case 60:
                                    $car_info->company = '国寿财';
                                    break;
                                default:
                                    $car_info->company = '';
                            }
                            $c_info->save();
                            $car_info->location = 8;
                        }

//                    $car_info->company = $ac3;
                        $car_info->save();
                        CarBT::deleteAll(['id'=>$car_info->id]);


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

                        //删除保单
                        $orders = OrderT::find()
                            ->where('car_id='.$car_info->id.' and status_id<2')
                            ->all();
                        foreach($orders as $order_info) {
                            OrderUserT::deleteAll('order_id='.$order_info->id);
                            $order_info->delete();
                        }
                        $orders = OrderT::find()
                            ->where('car_id='.$car_info->id.' and status_id>1')
                            ->all();
                        foreach($orders as $order_info) {
                            $order_user_info = OrderUserT::findOne(['order_id'=>$order_info->id,'status'=>0]);
                            if($order_user_info) {
                                $order_user_info->status = 3;
                                $order_user_info->save();
                            }
                        }
                        if($invalid_id == 61){

                            $this->addLog($car_info->id,'新保销售预约时,已在新保其他小公司,进入F2库',1);
                        }elseif($invalid_id == 57 || $invalid_id == 58 || $invalid_id == 59 || $invalid_id ==60){

                            $this->addLog($car_info->id,'新保销售预约时,已在新保四大公司,进入F1库',1);
                        }



                    } else {



//dd($invalid_id);
                        $car_info->location = 10;
                        $car_info->save();
                        CarF3T::deleteAll('id='.$car_info->id);
                        $invalid_info = new CarF3T();
                        $invalid_info->id = $car_info->id;
                        $invalid_info->invalid_id = $invalid_id;
                        $invalid_info->user_id = $this->my->id;
                        $invalid_info->save();

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

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

                        //删除保单
                        $orders = OrderT::find()
                            ->where('car_id='.$car_info->id.' and status_id<2')
                            ->all();
                        foreach($orders as $order_info) {
                            OrderUserT::deleteAll('order_id='.$order_info->id);
                            $order_info->delete();
                        }
                        $orders = OrderT::find()
                            ->where('car_id='.$car_info->id.' and status_id>1')
                            ->all();
                        foreach($orders as $order_info) {
                            $order_user_info = OrderUserT::findOne(['order_id'=>$order_info->id,'status'=>0]);
                            if($order_user_info) {
                                $order_user_info->status = 3;
                                $order_user_info->save();
                            }
                        }

                        $this->addLog($car_info->id,'无效车辆数据:'.$invalid_info->invalid->name,1);

                    }









                    $car_info->location = 1;
                    $car_info->save();

                    CarF6T::deleteAll('id='.$id);

                    $this->addLog($car_info->id,'从F6无效数据中恢复数据',1);
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

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

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

        if($request->isPost) {
            $invalid_id = $request->post('invalid_id');

            if($invalid_id == 0) {
                $result['msg'] = '请先选择无效理由';
                return $result;
            }
            $data = $this->getInvalidQuery2($request);
            $query = $data['query'];

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(1000) as $item) {
                    $car_info = $item->car;
                    $car_info->location = 1;
                    $car_info->save();

                    $item->delete();
                    $this->addLog($item->id,'从F6无效数据中恢复数据',1);
                }

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

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

    public function actionMistake()
    {
        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');
            $invalid_id = $request->post('invalid_id',0);


            $tran = CarT::getDb()->beginTransaction();
            try {

                $car_info = CarT::findOne(['id'=>$car_id]);
//                dd($car_info);

                $clean_info=CleanT::findOne(['id'=>$car_id]);

                if($invalid_id==70 || $invalid_id==71){
                    $car_info->location=1;
                    $car_info->qiyong=2;
                    $car_info->save();
                    $this->addLog($car_info->id,'进入A库永不启用',1);
                }else{
                    $car_info->location = 16;
                    CarMistakeT::deleteAll('id='.$car_info->id);
                    $invalid_info = new CarMistakeT();
                    $invalid_info->id = $car_info->id;
                    $invalid_info->invalid_id = $invalid_id;
                    $invalid_info->user_id = $this->my->id;
                    if($clean_info->repeat==1){
                        $invalid_info->judge = 1;
                    }else{
                        $invalid_info->judge = 2;
                    }
                    $invalid_info->user_id = $this->my->id;
                    $invalid_info->save();
                    $car_info->save();
                    $this->addLog($car_info->id,'不准确车辆数据:'.$invalid_info->invalid->name,1);
                }
                CarBT::deleteAll(['id'=>$car_info->id]);
                CarCT::deleteAll(['id'=>$car_info->id]);
                CarDT::deleteAll(['id'=>$car_info->id]);
                CarET::deleteAll(['id'=>$car_info->id]);
                CleanT::deleteAll(['id'=>$car_info->id]);
                $cleantj= new CleanTongji();
                $cleantj->user_id=$this->my->id;
                $cleantj->wrong_num=1;
                $cleantj->yuefen=date('Y-m-d');
                $cleantj->car_id=$car_info->id;
                $cleantj->car_no=$car_info->car_no;
                $cleantj->save();

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






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

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                throw $e;
            }
        }
        return $result;
    }
    public function actionZhongjiXuanze()
    {
        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');
            $invalid_id = $request->post('invalid_id',0);


            $tran = CarT::getDb()->beginTransaction();
            try {

                $car_info = CarT::findOne(['id'=>$car_id]);
//                dd($car_info);

                $clean_info=CleanT::findOne(['id'=>$car_id]);

                if($invalid_id==76){//准确数据

                    $car_info->location = 15;
                    $car_info->clean_right = 1;
                    $car_info->clean_num =$car_info->clean_num+1;

                    $car_info->save();

                    $c_info = RightT::findOne(['id' => $car_info->id]);
                    if (!$c_info) {
                        $c_info = new RightT();
                        $c_info->id = $car_info->id;
                        if($clean_info->repeat==1){
                            $c_info->judge = 1;
                        }else{
                            $c_info->judge = 2;
                        }
                        $c_info->user_id = 0;
                        $c_info->caozuo_id = $this->my->id;
                        $c_info->save();

                    }
                    CleanT::deleteAll(['id' => $car_info->id]);
                    $cleantj= new CleanTongji();
                    $cleantj->user_id=$this->my->id;
                    $cleantj->right_num=1;
                    $cleantj->yuefen=date('Y-m-d');
                    $cleantj->car_id=$car_info->id;
                    $cleantj->car_no=$car_info->car_no;
                    $cleantj->save();

                    $this->addLog($car_info->id, '从清洗库分配到数据准确库成功', 1);

                }elseif($invalid_id>63 && $invalid_id<69){ //预约

                    if($clean_info){
                        $clean_info->yuyue=2;
                        $clean_info->lingqu_status=2;
                        $clean_info->save();
                    }
                    $cleantj_info=CleanTongji::find()->where(['car_id'=>$car_id])->andWhere(['user_id'=>$this->my->id])->one();
                    if(!$cleantj_info){
                        $cleantj_info=new CleanTongji();
                        $cleantj_info->user_id=$this->my->id;
                        $cleantj_info->yuyue_num=1;
                        $cleantj_info->yuefen=date('Y-m-d');
                        $cleantj_info->car_id=$car_id;
                        $cleantj_info->car_no=$car_info->car_no;
                        $cleantj_info->save();
                        $this->addLog($car_info->id, '预约成功', 1);
                    }


                    $in_info=  InvalidT::findOne(['id'=>$invalid_id]);
                    $row = FiltrationT::find()
                        ->where('car_id='.$car_id)
                        ->one();
                    if(empty($row)) {
                        $row = new FiltrationT();
                        $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 = date("Y-m-d",strtotime("+1 day"));
                    $row->ptime = '09:00';
                    $row->ptype = 0;
                    $row->remark =$in_info->name ;
                    $row->business_group_id = 0;
                    $row->yuyue_num = $row->yuyue_num+1;
                    $row->is_first = 0;
                    $row->save();



                    $log_txt = '';
                    if($old_user_id > 0) {
                        $log_txt = '旧业务员是:'.$old_user_id.' ';
                    }
                    $this->addLog($car_id,$log_txt.'预约客户到'. date("Y-m-d",strtotime("+1 day")).' 09:00 预约内容为:'.$in_info->name,1);

                    if($row && $row->yuyue_num==4){ //进入无效库
                        $car_info->location = 16;
                        CarMistakeT::deleteAll('id='.$car_info->id);
                        $invalid_info = new CarMistakeT();
                        $invalid_info->id = $car_info->id;
                        $invalid_info->invalid_id = $invalid_id;
                        $invalid_info->user_id = $this->my->id;
                        if($clean_info->repeat==1){
                            $invalid_info->judge = 1;
                        }else{
                            $invalid_info->judge = 2;
                        }
                        $invalid_info->user_id = $this->my->id;
                        $invalid_info->save();
                        $car_info->save();
                        $this->addLog($car_info->id,'不准确车辆数据:'.$invalid_info->invalid->name,1);
                        CarBT::deleteAll(['id'=>$car_info->id]);
                        CarCT::deleteAll(['id'=>$car_info->id]);
                        CarDT::deleteAll(['id'=>$car_info->id]);
                        CarET::deleteAll(['id'=>$car_info->id]);
                        CleanT::deleteAll(['id'=>$car_info->id]);
                        $cleantj= new CleanTongji();
                        $cleantj->user_id=$this->my->id;
                        $cleantj->wrong_num=1;
                        $cleantj->yuefen=date('Y-m-d');
                        $cleantj->car_id=$car_info->id;
                        $cleantj->car_no=$car_info->car_no;
                        $cleantj->save();

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


                }elseif($invalid_id>68 && $invalid_id<76){ //不准确
                    $car_info->location = 16;
                    CarMistakeT::deleteAll('id='.$car_info->id);
                    $invalid_info = new CarMistakeT();
                    $invalid_info->id = $car_info->id;
                    $invalid_info->invalid_id = $invalid_id;
                    $invalid_info->user_id = $this->my->id;
                    if($clean_info->repeat==1){
                        $invalid_info->judge = 1;
                    }else{
                        $invalid_info->judge = 2;
                    }
                    $invalid_info->user_id = $this->my->id;
                    $invalid_info->save();
                    $car_info->save();
                    $this->addLog($car_info->id,'不准确车辆数据:'.$invalid_info->invalid->name,1);
                    CarBT::deleteAll(['id'=>$car_info->id]);
                    CarCT::deleteAll(['id'=>$car_info->id]);
                    CarDT::deleteAll(['id'=>$car_info->id]);
                    CarET::deleteAll(['id'=>$car_info->id]);
                    CleanT::deleteAll(['id'=>$car_info->id]);
                    $cleantj= new CleanTongji();
                    $cleantj->user_id=$this->my->id;
                    $cleantj->wrong_num=1;
                    $cleantj->yuefen=date('Y-m-d');
                    $cleantj->car_id=$car_info->id;
                    $cleantj->car_no=$car_info->car_no;
                    $cleantj->save();

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







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

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                throw $e;
            }
        }
        return $result;
    }
    private function getMistakeQuery($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $register_begin = $request->post('register_begin');
            $register_end = $request->post('register_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $invalid_id = $request->post('invalid_id');
            $username = $request->post('username');
            $user1 = $request->post('user1');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
            $judge = $request->post('judge');
        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $register_begin = $request->get('register_begin');
            $register_end = $request->get('register_end');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $invalid_id = $request->get('invalid_id');
            $username = $request->get('username');
            $user1 = $request->get('user1');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
            $judge = $request->get('judge');
        }
        $params = array();
        $params['car_man'] = $car_man;
        $params['phone'] = $phone;
        $params['car_no'] = $car_no;
        $params['register_begin'] = $register_begin;
        $params['register_end'] = $register_end;
        $params['insurer1_begin'] = $insurer1_begin;
        $params['insurer1_end'] = $insurer1_end;
        $params['invalid_id'] = $invalid_id;
        $params['username'] = $username;
        $params['user1'] = $user1;
        $params['sort_key'] = $sort_key;
        $params['sort_value'] = $sort_value;
        $params['judge'] = $judge;

        $query = CarMistakeT::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`car_mistake_t`.`id`');

        if($car_man != '') {
            $query = $query->andWhere('car_t.car_man="'.$car_man.'"');
        }
        if($phone != '') {
            $query = $query->andWhere('car_t.phone="'.$phone.'"');
        }
        if($car_no != '') {
            $query = $query->andWhere('car_t.car_no="'.$car_no.'"');
        }
        if($register_begin != '') {
            $query = $query->andWhere('car_t.register_date>="'.$register_begin.'"');
        }
        if($register_end != '') {
            $query = $query->andWhere('car_t.register_date<="'.$register_end.'"');
        }
        if($insurer1_begin != '') {
            $query = $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query = $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }
        if($invalid_id > 0) {
            $query = $query->andWhere('car_mistake_t.invalid_id='.$invalid_id);
        }
        if($judge > 0) {
            $query = $query->andWhere(['judge'=>$judge]);
        }
        if($user1 != '') {
            $query = $query->andWhere('car_t.op_user1 like "'.$user1.'%"');
        }
        if($username != '') {
            $user_info = UserT::findOne(['username'=>$username]);
            if($user_info) {
                $query = $query->andWhere('car_mistake_t.user_id='.$user_info->id);
            }
        }
        $query = $query->orderBy($sort_key.' '.$sort_value.',car_mistake_t.id '.$sort_value);
//        echo $query->createCommand()->rawSql;
        return ['query'=>$query,'params'=>$params];
    }
    public function actionMistakeIndex()
    {
        $request = Yii::$app->request;
        $page = $request->get('page',1);
        $op = $request->get('op',1);
        $data = $this->getMistakeQuery($request);
        $query = $data['query'];
        $params = $data['params'];
        $params['page'] = $page;
        if($op==0) {
            $query = $query->andWhere('car_mistake_t.id=-1');
        }
//        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::getPageInfo($pagination);

        $invalid_items = InvalidT::getTreeClean(2);

        $params['items'] = $items;
        $params['page_info'] = $page_info;
        $params['invalid_items'] = $invalid_items;

        return $this->render('mistake-index',$params);
    }
    public function actionMistakeOutput()
    {
        $request = Yii::$app->request;
        $page = $request->get('page', 1);


        $data = $this->getMistakeQuery($request);
        $query = $data['query'];
        $params = $data['params'];
        $params['page'] = $page;



//    var_dump($group_ids);

        if ($page < 1) $page = 1;
        $items = $query->all();










        $objectPHPExcel = new \PHPExcel();
        $index = 1;
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$index,'车牌号');
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$index,'车架号');
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('C'.$index,'发动机号');
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('D'.$index,'身份证号');
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('E'.$index,'姓名');
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('F'.$index,'初登日期');
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('G'.$index,'保险日期');




        foreach($query->each() as $item) {
            $index++;
            $car_info = $item->car;



            $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$index,$car_info->car_no);
            $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$index,$car_info->car_frame_no);
            $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('C'.$index,$car_info->engine_no);
            $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('D'.$index,$car_info->id_number);
            $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('E'.$index,$car_info->id_man);
            $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('F'.$index,$car_info->register_date);
            $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('G'.$index,$car_info->insurer1_date);
//            ;
        }

        header('Content-Type: application/vnd.ms-excel;charset=utf-8');
        header('Content-Disposition:attachment;filename="'.date("YmjHis").'.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objectPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        foreach($query->each() as $item) {
            $item->delete();
        }


    }
    public function actionInvalid()
    {
        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());
            $invalid_id = $request->post('invalid_id',0);

            if($invalid_id == 0) {
                $result['msg'] = '请先选择无效理由';
                return $result;
            }
            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 = 5;
                    $car_info->save();

                    CarInvalidT::deleteAll('id='.$car_info->id);
                    $invalid_info = new CarInvalidT();
                    $invalid_info->id = $car_info->id;
                    $invalid_info->invalid_id = $invalid_id;
                    $invalid_info->user_id = $this->my->id;
                    $invalid_info->save();

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

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

                    //删除操作人员
                    $orders = OrderT::find()
                        ->where('car_id='.$car_info->id.' and status_id<2')
                        ->all();
                    foreach($orders as $order_info) {
                        OrderUserT::deleteAll('order_id='.$order_info->id);
                        //删除保单
                        $order_info->delete();
                    }

                    $this->addLog($car_info->id,'无效车辆数据:'.$invalid_info->invalid->name,1);
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                throw $e;
            }
        }
        return $result;
    }
    public function actionAMoveB()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';
        $flag = 0;
        if($request->isPost) {
            $unsel_lib = $request->post('unsel_lib',0);
            $has_phone = $request->post('has_phone');
            $data = $this->getAQuery($request);
            $query = $data['query'];

            if($unsel_lib == 0) {
                $result['msg'] = '请先选择未分配选项';
                return $result;
            }
            if($has_phone != 1) {
                $result['msg'] = '请先选择有电话号码选项';
                return $result;
            }
            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(1000) as $car_info) {
                    if($car_info->location == 1) {
                        if($car_info->qiyong==2){
                            continue;
                        }
                        $car_info->location = 2;
                        $car_info->save();

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

                            $this->addLog($car_info->id,'从A库分配到B库',1);
                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                throw $e;
            }
        }
        return $result;
    }
    public function actionAMoveE()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';
        $flag = 0;
        if($request->isPost) {
            $unsel_lib = $request->post('unsel_lib',0);
            $has_phone = $request->post('has_phone');
            $data = $this->getAQuery($request);
            $query = $data['query'];

            if($unsel_lib == 0) {
                $result['msg'] = '请先选择未分配选项';
                return $result;
            }
            if($has_phone != 1) {
                $result['msg'] = '请先选择有电话号码选项';
                return $result;
            }
            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(1000) as $car_info) {
                    if($car_info->location == 1) {
                        if($car_info->qiyong==2){
                            continue;
                        }
                        $car_info->location = 6;
                        $car_info->save();

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

                            $this->addLog($car_info->id,'从A库分配到E库',1);
                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                throw $e;
            }
        }
        return $result;
    }
    public function actionAMoveClean()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';
        $flag = 0;
        if($request->isPost) {
            $unsel_lib = $request->post('unsel_lib',0);
            $has_phone = $request->post('has_phone');
            $data = $this->getAQuery($request);
            $query = $data['query'];

            if($unsel_lib == 0) {
                $result['msg'] = '请先选择未分配选项';
                return $result;
            }
            if($has_phone != 1) {
                $result['msg'] = '请先选择有电话号码选项';
                return $result;
            }
            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(1000) as $car_info) {
                    if($car_info->location == 1) {
                        if($car_info->qiyong==2){
                            continue;
                        }
                        $car_info->location =14;
                        $car_info->save();

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

                            if($b_info->save()){
                                $fenpei= new CleanFenpei();
                                $fenpei->user_id=$this->my->id;
                                $fenpei->fenpei_num=1;
                                $fenpei->fenpei_date=date('Y-m-d');
                                $fenpei->save();
                            }

                            $this->addLog($car_info->id,'从A库分配到清洗库',1);
                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

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

    private function getBQuery1($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $register_begin = $request->post('register_begin');
            $register_end = $request->post('register_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $insurer1_month = $request->post('insurer1_date');
            $insurer1_day = $request->post('insurer1_day');
            $insurer1_month1 = $request->post('insurer1_date1');
            $insurer1_day1 = $request->post('insurer1_day1');
            $username = $request->post('username');
            $status = $request->post('status');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
            $company = $request->post('company');
        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $register_begin = $request->get('register_begin');
            $register_end = $request->get('register_end');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $insurer1_month = $request->get('insurer1_date');
            $insurer1_day = $request->get('insurer1_day');
            $insurer1_month1 = $request->get('insurer1_date1');
            $insurer1_day1 = $request->get('insurer1_day1');
            $username = $request->get('username');
            $status = $request->get('status');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
            $company = $request->get('company');
        }
        $query = CarBT::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`car_b_t`.`id`')
            ->andWhere('car_b_t.back =1');

        if($car_man != '') {
            $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
        }
        if($phone != '') {
            $query = $query->andWhere('car_t.phone like "'.$phone.'"');
        }
        if($car_no != '') {
            if($car_no == '非京'){
                $query = $query->andWhere('car_no not like "京%"');
            }else{
                $query = $query->andWhere('car_no like "'.$car_no.'%"');
            }
        }
        if($register_begin != '') {
            $query = $query->andWhere('car_t.register_date>="'.$register_begin.'"');
        }
        if($register_end != '') {
            $query = $query->andWhere('car_t.register_date<="'.$register_end.'"');
        }
        if($insurer1_begin != '') {
            $query = $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query = $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }
        if($insurer1_month != '') {
            $query = $query->andWhere('month(car_t.insurer1_date)>="'.$insurer1_month.'"');
        }
        if($insurer1_day != '') {
            $query = $query->andWhere('day(car_t.insurer1_date)>="'.$insurer1_day.'"');
        }
        if($insurer1_month1 != '') {
            $query = $query->andWhere('month(car_t.insurer1_date)<="'.$insurer1_month1.'"');
        }
        if($insurer1_day1 != '') {
            $query = $query->andWhere('day(car_t.insurer1_date)<="'.$insurer1_day1.'"');
        }
        if($status == 1) {
            $query = $query->andWhere('car_b_t.user_id=0');
        }
        if($status == 2) {
            $query = $query->andWhere('car_b_t.user_id>0');
        }
        if($username != '') {
            $user_info = UserT::findOne(['username'=>$username]);
            if($user_info) {
                $query = $query->andWhere('car_b_t.user_id='.$user_info->id);
            }
        }
        if($company != '') {
            $query = $query->andWhere('company like "'.$company.'%"');
        }
        $query = $query->orderBy($sort_key.' '.$sort_value.', car_b_t.id '.$sort_value);
//        echo $query->createCommand()->rawSql;
//        exit;
        return $query;
    }
    //数据准确库
    private function getRightQuery($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $register_begin = $request->post('register_begin');
            $register_end = $request->post('register_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $insurer1_month = $request->post('insurer1_date');
            $insurer1_day = $request->post('insurer1_day');
            $insurer1_month1 = $request->post('insurer1_date1');
            $insurer1_day1 = $request->post('insurer1_day1');
            $username = $request->post('username');
            $status = $request->post('status');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
            $company = $request->post('company');
            $judge = $request->post('judge');
        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $register_begin = $request->get('register_begin');
            $register_end = $request->get('register_end');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $insurer1_month = $request->get('insurer1_date');
            $insurer1_day = $request->get('insurer1_day');
            $insurer1_month1 = $request->get('insurer1_date1');
            $insurer1_day1 = $request->get('insurer1_day1');
            $username = $request->get('username');
            $status = $request->get('status');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
            $company = $request->get('company');
            $judge = $request->get('judge');
        }
        $query = RightT::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`right_t`.`id`');


        if($car_man != '') {
            $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
        }
        if($phone != '') {
            $query = $query->andWhere('car_t.phone like "'.$phone.'"');
        }
        if($car_no != '') {
            if($car_no == '非京'){
                $query = $query->andWhere('car_no not like "京%"');
            }else{
                $query = $query->andWhere('car_no like "'.$car_no.'%"');
            }
        }
        if($register_begin != '') {
            $query = $query->andWhere('car_t.register_date>="'.$register_begin.'"');
        }
        if($register_end != '') {
            $query = $query->andWhere('car_t.register_date<="'.$register_end.'"');
        }
        if($insurer1_begin != '') {
            $query = $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query = $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }
        if($insurer1_month != '') {
            $query = $query->andWhere('month(car_t.insurer1_date)>="'.$insurer1_month.'"');
        }
        if($insurer1_day != '') {
            $query = $query->andWhere('day(car_t.insurer1_date)>="'.$insurer1_day.'"');
        }
        if($insurer1_month1 != '') {
            $query = $query->andWhere('month(car_t.insurer1_date)<="'.$insurer1_month1.'"');
        }
        if($insurer1_day1 != '') {
            $query = $query->andWhere('day(car_t.insurer1_date)<="'.$insurer1_day1.'"');
        }
        if($status == 1) {
            $query = $query->andWhere('right_t.user_id=0');
        }
        if($status == 2) {
            $query = $query->andWhere('right_t.user_id>0');
        }
        if($judge > 0) {
            $query = $query->andWhere(['judge'=>$judge]);
        }
        if($username != '') {
            $user_info = UserT::findOne(['username'=>$username]);
            if($user_info) {
                $query = $query->andWhere('car_b_t.user_id='.$user_info->id);
            }
        }
        if($company != '') {
            $query = $query->andWhere('company like "'.$company.'%"');
        }
        $query = $query->orderBy($sort_key.' '.$sort_value.', right_t.id '.$sort_value);
//        dd(32);
//        echo $query->createCommand()->rawSql;
//        exit;
        return $query;
    }
    public function actionRightIndex()
    {
        $request = Yii::$app->request;
        $page = $request->get('page',1);
        $op = $request->get('op',1);

        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $register_begin = $request->get('register_begin');
        $register_end = $request->get('register_end');
        $insurer1_begin = $request->get('insurer1_begin');
        $insurer1_end = $request->get('insurer1_end');
        $insurer1_month = $request->get('insurer1_date');
        $insurer1_day = $request->get('insurer1_day');
        $insurer1_month1 = $request->get('insurer1_date1');
        $insurer1_day1 = $request->get('insurer1_day1');
        $username = $request->get('username');
        $status = $request->get('status',1);
        $sort_key = $request->get('sort_key');
        $sort_value = $request->get('sort_value');
        $user_id = $request->get('user_id');
        $company = $request->get('company');
        $judge = $request->get('judge');

        $query = $this->getRightQuery($request);
        if($op==0) {
            $query = $query->andWhere('right_t.id=-1');
        }
//        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::getPageInfo($pagination);

        $invalid_items = InvalidT::getTree();
        $user_items = $this->my->getChildren();

        $monthes= CarT::MONTH;
        $days= CarT::DAYS;

        return $this->render('right-index',[
            'items' => $items,
            'monthes' => $monthes,
            'days' => $days,
            'page_info' => $page_info,
            'page' => $page,
            'car_man' => $car_man,
            'phone' => $phone,
            'car_no' => $car_no,
            'register_begin' => $register_begin,
            'register_end' => $register_end,
            'insurer1_begin' => $insurer1_begin,
            'insurer1_end' => $insurer1_end,
            'insurer1_month' =>$insurer1_month,
            'insurer1_day' =>$insurer1_day,
            'insurer1_month1' =>$insurer1_month1,
            'insurer1_day1' =>$insurer1_day1,
            'invalid_items' => $invalid_items,
            'user_items' => $user_items,
            'user_id' => $user_id,
            'status' => $status,
            'judge' => $judge,
            'username' => $username,
            'sort_key' => $sort_key,
            'sort_value' => $sort_value,
            'company' => $company,
        ]);
    }
    public function actionAjaxAReturn()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result['success'] = false;
        $result['msg'] = '操作失败';
        $id = $request->post('id',0);
        //没有任务
//        ->andWhere(['repeat'=>1])

        //echo $id;die;
//        dd($id);
        $tran = CarT::getDb()->beginTransaction();
        try {
            $car_info = CarT::findOne($id);
//            $car_info->location = 14;


            $c_info = CleanT::findOne(['id'=>$car_info->id]);
            if($c_info) {
                $c_info->user_id = 0;
                $c_info->save();
            }

//            FiltrationT
            //删除预约拨打
            FiltrationT::deleteAll('car_id='.$c_info->id);

            $clean_info=  CleanTongji::find()
                ->where('car_id='.$c_info->id)
                ->where('yuyue_num=1')
                ->all();
            foreach($clean_info as $cl_vl){
                $cl_vl->delete();
            }
            $this->addLog($car_info->id,'恢复一条数据成功',1);
            $result['success'] = true;
            $result['msg'] = '操作成功';

            $tran->commit();

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

        }



        return $result;
    }
    public function actionAjaxAData()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $result['success'] = false;
        $result['msg'] = '领取失败';
        //没有任务
//        ->andWhere(['repeat'=>1])
        $total_ids= CleanT::find()->where('user_id=0')->column();

//        $total_ids = OrderT::find()->andWhere('status_id=2 and hb_status=0 and lock_id=0 and (op1_id=0 or op1_id is NULL) ')->column();
        $count = count($total_ids);
        if($count<1){
            $result['msg'] = '没有任务';
            return $result;
        }
//        dd($total_ids);
        $i = rand(0,$count-1);
        $id = $total_ids[$i];
        //echo $id;die;
        $tran = CarT::getDb()->beginTransaction();
        try {
            $car_info = CarT::findOne($id);
//            $car_info->location = 14;
            if($car_info->save()){

                //取出上一条判断状态
                $clean_count= CleanT::find()
                    ->where(['user_id'=>$this->my->id])
                    ->andWhere(['lingqu_status'=>1])
                    ->count();
                $clean_count=(int)$clean_count;
                if($clean_count>0){
                    $result['msg'] = '上一条数据未处理不能领取任务';
                    return $result;

                }
                $c_info = CleanT::findOne(['id'=>$car_info->id]);
                if(!$c_info) {
                    $c_info = new CleanT();
                    $c_info->id = $car_info->id;
                    $c_info->user_id = $this->my->id;
                }else{
                    $c_info->user_id = $this->my->id;
                }
                $c_info->now_time=new Expression('NOW()');
                $c_info->save();
//            FiltrationT
                //添加到预约拨打
                $filt=FiltrationT::findOne(['car_id'=>$c_info->id]);
                if($filt){
                    $filt->user_id = $this->my->id;
                    $filt->delete_status=1;
                    $filt->save();

                }else{
                    $row = new FiltrationT();
                    $row->car_id = $c_info->id;
                    $row->user_id = $this->my->id;
                    $row->pdate = date('Y-m-d');
                    $row->ptime = '09:00';
                    $row->remark = '首次分配';
                    $row->is_first = 1;
                    $row->xdate = date('Y-m-d');
                    $row->xtime = '09:00';
                    $row->xbao = 2;
                    $row->save();
                }
//                FiltrationT::deleteAll('car_id='.$c_info->id);

                $this->addLog($car_info->id,'从A库领取一条数据成功',1);
                $result['success'] = true;
                $result['msg'] = '领取成功';
            }
            $tran->commit();

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

        }



        return $result;
    }

    public function actionAjaxADataNext()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $result['success'] = false;
        $result['msg'] = '领取失败';
        //没有任务
//        ->andWhere(['repeat'=>1])
        $total_ids= CleanT::find()->where('user_id=0')->column();

//        $total_ids = OrderT::find()->andWhere('status_id=2 and hb_status=0 and lock_id=0 and (op1_id=0 or op1_id is NULL) ')->column();
        $count = count($total_ids);
        if($count<1){
            $result['msg'] = '没有任务';
            return $result;
        }

        $i = rand(0,$count-1);
        $id = $total_ids[$i];
        //echo $id;die;
        $tran = CarT::getDb()->beginTransaction();
        try {
            $car_info = CarT::findOne($id);
//            $car_info->location = 14;
            $clean_count= CleanT::find()
                ->where(['user_id'=>$this->my->id])
                ->andWhere(['lingqu_status'=>1])
                ->count();
            $clean_count=(int)$clean_count;
            if($clean_count>0){
                $result['msg'] = '有未处理的数据不能自动进入下一条';
                return $result;

            }
            if($car_info->save()){

                $c_info = CleanT::findOne(['id'=>$car_info->id]);
                if(!$c_info) {
                    $c_info = new CleanT();
                    $c_info->id = $car_info->id;
                    $c_info->user_id = $this->my->id;
                }else{
                    $c_info->user_id = $this->my->id;
                }
                $c_info->now_time=new Expression('NOW()');
                $c_info->save();
//            FiltrationT
                //添加到预约拨打
//                FiltrationT::deleteAll('car_id='.$c_info->id);
//                $row = new FiltrationT();
//                $row->car_id = $c_info->id;
//                $row->user_id = $this->my->id;
//                $row->pdate = date('Y-m-d');
//                $row->ptime = '09:00';
//                $row->remark = '首次分配';
//                $row->is_first = 1;
//                $row->xdate = date('Y-m-d');
//                $row->xtime = '09:00';
//                $row->xbao = 2;
//                $row->save();

                $filt=FiltrationT::findOne(['car_id'=>$c_info->id]);
                if($filt){
                    $filt->user_id = $this->my->id;
                    $filt->delete_status=1;
                    $filt->save();

                }else{
                    $row = new FiltrationT();
                    $row->car_id = $c_info->id;
                    $row->user_id = $this->my->id;
                    $row->pdate = date('Y-m-d');
                    $row->ptime = '09:00';
                    $row->remark = '首次分配';
                    $row->is_first = 1;
                    $row->xdate = date('Y-m-d');
                    $row->xtime = '09:00';
                    $row->xbao = 2;
                    $row->save();
                }
                $this->addLog($car_info->id,'从A库领取一条数据成功',1);
                $result['success'] = true;
                $result['data'] = $car_info->id;
                $result['msg'] = '领取成功';
            }
            $tran->commit();

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

        }



        return $result;
    }
    public function actionCleanToRight()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $result['success'] = false;
        $result['msg'] = '领取失败';
        $request = Yii::$app->request;
        $id=$request->post('car_id');
//        dd($id);
        $tran = CarT::getDb()->beginTransaction();
        try {
            if ($request->isPost) {

                $car_info = CarT::findOne($id);
                $car_info->location = 15;
                $car_info->save();
                $clean_info=CleanT::findOne(['id'=>$id]);
                $c_info = RightT::findOne(['id' => $car_info->id]);
                if (!$c_info) {
                    $c_info = new RightT();
                    $c_info->id = $car_info->id;
                    if($clean_info->repeat==1){
                        $c_info->judge = 1;
                    }else{
                        $c_info->judge = 2;
                    }
                    $c_info->user_id = 0;
                    $c_info->caozuo_id = $this->my->id;
                    $c_info->save();

                }
                
                CleanT::deleteAll(['id' => $car_info->id]);
                $cleantj= new CleanTongji();
                $cleantj->user_id=$this->my->id;
                $cleantj->right_num=1;
                $cleantj->yuefen=date('Y-m-d');
                $cleantj->car_id=$car_info->id;
                $cleantj->car_no=$car_info->car_no;
                $cleantj->save();


                $this->addLog($car_info->id, '从清洗库分配到数据准确库成功', 1);
                $result['success'] = true;
                $result['msg'] = '分配成功';

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

        }


        return $result;
    }
    private function getcleanQuery($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $register_begin = $request->post('register_begin');
            $register_end = $request->post('register_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $insurer1_month = $request->post('insurer1_date');
            $insurer1_day = $request->post('insurer1_day');
            $insurer1_month1 = $request->post('insurer1_date1');
            $insurer1_day1 = $request->post('insurer1_day1');
            $username = $request->post('username');
            $status = $request->post('status');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
            $company = $request->post('company');
            $repeat = $request->post('repeat');
            $yuyue = $request->post('yuyue');

        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $register_begin = $request->get('register_begin');
            $register_end = $request->get('register_end');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $insurer1_month = $request->get('insurer1_date');
            $insurer1_day = $request->get('insurer1_day');
            $insurer1_month1 = $request->get('insurer1_date1');
            $insurer1_day1 = $request->get('insurer1_day1');
            $username = $request->get('username');
            $status = $request->get('status');
            $repeat = $request->get('repeat');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
            $company = $request->get('company');
            $yuyue = $request->get('yuyue');
        }
        $query = CleanT::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`clean_t`.`id`');


        if($car_man != '') {
            $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
        }
        if($phone != '') {
            $query = $query->andWhere('car_t.phone like "'.$phone.'"');
        }
        if($car_no != '') {
            if($car_no == '非京'){
                $query = $query->andWhere('car_no not like "京%"');
            }else{
                $query = $query->andWhere('car_no like "'.$car_no.'%"');
            }
        }
        if($register_begin != '') {
            $query = $query->andWhere('car_t.register_date>="'.$register_begin.'"');
        }
        if($register_end != '') {
            $query = $query->andWhere('car_t.register_date<="'.$register_end.'"');
        }
        if($insurer1_begin != '') {
            $query = $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query = $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }

//        if($insurer1_month != '') {
//            $query = $query->andWhere('month(car_t.insurer1_date)>="'.$insurer1_month.'"');
//        }
//        if($insurer1_day != '') {
//            $query = $query->andWhere('day(car_t.insurer1_date)>="'.$insurer1_day.'"');
//        }
//        if($insurer1_month1 != '') {
//            $query = $query->andWhere('month(car_t.insurer1_date)<="'.$insurer1_month1.'"');
//        }
//        if($insurer1_day1 != '') {
//            $query = $query->andWhere('day(car_t.insurer1_date)<="'.$insurer1_day1.'"');
//        }


        if($status == 1) {
            $query = $query->andWhere('clean_t.user_id=0');
        }
        if($status == 2) {
            $query = $query->andWhere('clean_t.user_id>0');
        }
        $query = $query->andWhere(['yuyue'=>1]);
//        if($yuyue>0){
//            $query = $query->andWhere(['yuyue'=>$yuyue]);
//        }
        if($repeat>0){
            $query = $query->andWhere(['repeat'=>$repeat]);
        }
//        if($username != '') {
//            $user_info = UserT::findOne(['username'=>$username]);
//            if($user_info) {
//                $query = $query->andWhere('clean_t.user_id='.$user_info->id);
//            }
//        }
        if($username == '') {
            $user_ids = $this->getChildrenUserIDs();
            $query = $query->andWhere(['in','clean_t.user_id',$user_ids]);
        }
        if($company != '') {
            $query = $query->andWhere('company like "'.$company.'%"');
        }
        $query = $query->orderBy($sort_key.' '.$sort_value.', clean_t.id '.$sort_value);
        echo $query->createCommand()->rawSql;
//        exit;
        return $query;
    }
    public function actionCleanIndex()
    {
        $request = Yii::$app->request;
        $page = $request->get('page',1);
        $op = $request->get('op',1);

        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $register_begin = $request->get('register_begin');
        $register_end = $request->get('register_end');
        $insurer1_begin = $request->get('insurer1_begin');
        $insurer1_end = $request->get('insurer1_end');
        $insurer1_month = $request->get('insurer1_date');
        $insurer1_day = $request->get('insurer1_day');
        $insurer1_month1 = $request->get('insurer1_date1');
        $insurer1_day1 = $request->get('insurer1_day1');
        $username = $request->get('username');
        $status = $request->get('status',2);
        $repeat = $request->get('repeat',1);
        $yuyue = $request->get('yuyue',1);
        $sort_key = $request->get('sort_key');
        $sort_value = $request->get('sort_value');
        $user_id = $request->get('user_id');
        $company = $request->get('company');

        $query = $this->getcleanQuery($request);
        if($op==0) {
            $query = $query->andWhere('clean_t.id=-1');
        }
//        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::getPageInfo($pagination);

        $invalid_items = InvalidT::getTree();
        $user_items = $this->my->getChildren();

        $monthes= CarT::MONTH;
        $days= CarT::DAYS;
        $carAcount=CleanT::find()->where('user_id=0')->count();

        return $this->render('clean-index',[
            'items' => $items,
            'monthes' => $monthes,
            'days' => $days,
            'page_info' => $page_info,
            'page' => $page,
            'car_man' => $car_man,
            'phone' => $phone,
            'car_no' => $car_no,
            'register_begin' => $register_begin,
            'register_end' => $register_end,
            'insurer1_begin' => $insurer1_begin,
            'insurer1_end' => $insurer1_end,
            'insurer1_month' =>$insurer1_month,
            'insurer1_day' =>$insurer1_day,
            'insurer1_month1' =>$insurer1_month1,
            'insurer1_day1' =>$insurer1_day1,
            'invalid_items' => $invalid_items,
            'user_items' => $user_items,
            'user_id' => $user_id,
            'status' => $status,
            'repeat' => $repeat,
            'yuyue' => $yuyue,
            'username' => $username,
            'sort_key' => $sort_key,
            'sort_value' => $sort_value,
            'company' => $company,
            'carAcount' => $carAcount,
        ]);
    }

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

        if($request->isPost) {
            $status = $request->post('status');
            if($status != 1) {
                $result['msg'] = '请先选择分配状态!';
                return $result;
            }
            $assign_users = explode(',',$request->post('assign_users'));
            $assign_count = $request->post('assign_count',0);
            $assign_user_ids = array();
            foreach($assign_users as $assign_user) {
                $user_info = UserT::findOne(['username'=>$assign_user,'is_delete'=>0]);
                if($user_info)
                    $assign_user_ids[] = $user_info->id;
            }
            if(count($assign_user_ids) == 0) {
                $result['msg'] = '请输入业务员工号,以英文逗号间隔';
                return $result;
            }
            if($assign_count == 0) {
                $result['msg'] = '请输入每个业务员分配记录数';
                return $result;
            }

            $query = $this->getBQuery1($request);
//            echo $query->createCommand()->rawSql;
//            echo $query->count();
//            exit;
            $tran = CarT::getDb()->beginTransaction();
            try {
                $page = 0;
                $total_pages = count($assign_user_ids);
                $index = 0;
                foreach($query->each(100) as $b_info) {
                    if($index >= ($page+1) * $assign_count) $page += 1;
                    if($page == $total_pages) break;
                    if($b_info->user_id > 0) continue;
                    $b_info->user_id = $assign_user_ids[$page];
                    $b_info->save();

                    $car_info = $b_info->car;
                    $car_info->op_user1 = $b_info->user->getShowName();
                    $car_info->save();

                    //添加保单
                    $order_row = new OrderT();
                    $order_row->car_id = $b_info->id;
                    $order_row->car_no = $b_info->car->car_no;
                    $order_row->engine_no = $b_info->car->engine_no;
                    $order_row->car_frame_no = $b_info->car->car_frame_no;
                    $order_row->car_man = $b_info->car->car_man;
                    $order_row->user_id = $b_info->user_id;
                    $order_row->status_id = 1;
                    $order_row->id_man = $b_info->car->car_man;
                    $order_row->link_man = $b_info->car->car_man;
                    $order_row->link_phone = $b_info->car->phone;
                    $order_row->return_status_id = 0;
                    $order_row->return_remark = '';
                    $order_row->save();

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

                    //添加历史预约
                    $h_row = new AppointmentHistoryT();
                    $h_row->car_id = $b_info->id;
                    $h_row->user_id = $b_info->user_id;
                    $h_row->pdate = date('Y-m-d');
                    $h_row->ptime = '09:00';
                    $h_row->remark = '首次分配';
                    $h_row->xdate = date('Y-m-d');
                    $h_row->xtime = '09:00';
                    $h_row->xbao = 2;
                    $h_row->save();

                    $this->addLog($b_info->id,'从B库的数据管理分支分配数据给业务员:'.$b_info->user->getShowName(),1);
                    $index++;
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

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

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

        if($request->isPost) {


            $status = $request->post('status');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $invalid_id = $request->post('invalid_id');

            if($status!=1){
                $result['msg'] = '请选择未分配状态';
                return $result;
            }


            $query = CarBT::find()
                ->leftJoin('car_t','`car_t`.`id`=`car_b_t`.`id`')
                ->andWhere('car_b_t.back=1');

            if($status == 1) {
                $query = $query->andWhere('car_b_t.user_id=0');
            }
            if($invalid_id >0) {
                $query = $query->andWhere('car_b_t.iv_d="'.$invalid_id.'"');
            }
            if($insurer1_begin != '') {
                $query = $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
            }
            if($insurer1_end != '') {
                $query = $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
            }




            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(1000) as $item) {
                    $car_info = $item->car;
                    $car_info->location = 5;
                    $car_info->save();

//            FiltrationT
                    //删除预约拨打
//                    FiltrationT::deleteAll('car_id='.$c_info->id);
                    CarInvalidT::deleteAll('id='.$car_info->id);
                    InvalidNew::deleteAll('car_id='.$car_info->id);


                    $invalid_info = new CarInvalidT();
                    $invalid_info->id = $car_info->id;
                    $invalid_info->invalid_id =  $item->iv_d;
                    $invalid_info->user_id = $this->my->id;
                    $invalid_info->save();


                    $this->addLog($item->id,'从B库新保数据处理批量收回到无效库',1);
                    $item->delete();
                }

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

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                throw $e;
            }
        }
        return $result;
    }
    public function actionCleanReturnA()
    {
        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) {
                    $b_info = CleanT::findOne(['id'=>$id]);
                    //删除预约
                    FiltrationT::deleteAll('car_id='.$b_info->id);
                    $car_info = $b_info->car;
                    $car_info->location = 1;
                    $car_info->save();
                    $this->addLog($b_info->id,'从清洗库回收数据到A库',1);
                    $b_info->delete();
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

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

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

        if($request->isPost) {

            $yuyue = $request->post('yuyue');
            $status = $request->post('status');
            $register_begin = $request->post('register_begin');
            $register_end = $request->post('register_end');

            if($yuyue!=1 || $status!=1){
                $result['msg'] = '请选择未预约状态或未分配状态';
                return $result;
            }


            $query = CleanT::find()
                ->leftJoin('car_t','`car_t`.`id`=`clean_t`.`id`')
                ->andWhere(['yuyue'=>$yuyue])
                ->andWhere('clean_t.user_id=0');
            if($register_begin != '') {
                $query = $query->andWhere('car_t.register_date>="'.$register_begin.'"');
            }
            if($register_end != '') {
                $query = $query->andWhere('car_t.register_date<="'.$register_end.'"');
            }




            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(1000) as $item) {
                    $car_info = $item->car;
                    $car_info->location = 1;
                    $car_info->save();
                    $item->delete();
//            FiltrationT
                    //删除预约拨打
//                    FiltrationT::deleteAll('car_id='.$c_info->id);
                    $this->addLog($item->id,'从清洗库数据未领取的任务收回到A库',1);
                }

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

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

    private function getEQuery($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $print_begin = $request->post('print_begin');
            $print_end = $request->post('print_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $status = $request->post('status');
            $username = $request->post('username');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
            $old_user = $request->post('old_user');
            $company = $request->post('company');
            $register_begin = $request->post('register_begin');
            $register_end = $request->post('register_end');
        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $print_begin = $request->get('print_begin');
            $print_end = $request->get('print_end');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $status = $request->get('status');
            $username = $request->get('username');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
            $old_user = $request->get('old_user');
            $company = $request->get('company');
            $register_begin = $request->get('register_begin');
            $register_end = $request->get('register_end');
        }
        $query = CarET::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`car_e_t`.`id`');
        if($car_man != '') {
            $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
        }
        if($phone != '') {
            $query = $query->andWhere('car_t.phone like "'.$phone.'"');
        }
        if($car_no != '') {
            $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
        }
        if($print_begin != '') {
            $query = $query->andWhere('car_t.print_date>="'.$print_begin.'"');
        }
        if($print_end != '') {
            $query = $query->andWhere('car_t.print_date<="'.$print_end.'"');
        }
        if($register_begin != '') {
            $query = $query->andWhere('register_date>="'.$register_begin.'"');
        }
        if($register_end != '') {
            $query = $query->andWhere('register_date<="'.$register_end.'"');
        }

        if($insurer1_begin != '') {
            $query = $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query = $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }
        if($status == 1) {
            $query = $query->andWhere('car_e_t.user_id=0');
        }
        if($status == 2) {
            $query = $query->andWhere('car_e_t.user_id>0');
        }
        if($username != '') {
//            $user_info = UserT::findOne(['username'=>$username]);
//            $query = $query->andWhere('op_user1 like "'.$user1.'%"');
            $user_info = UserT::find()->where('username like"'.$username.'%"')->all();
//            $user_ids='';
            $user_ids = array();
            if($user_info){
                foreach($user_info as $v){
//                    $user_ids.="'".$v->id."'".',';
//                var_dump($v->id);
                    $user_ids[] = $v->username.'('.$v->name.')';
                }
            }

//            var_dump($user_ids);
//
//            $srrs=trim($user_ids,',');
//            var_dump($srrs);
//            var_dump('car_e_t.user_id in ('.$srrs.')');
            if($user_info) {
                $query = $query->andWhere(['in','car_t.op_user1',$user_ids]);
            }
        }
//        $query = $query->andWhere(['in','car_t.op_user1',$user_ids]);
        if($old_user != '') {
            $query = $query->andWhere('car_t.old_user="'.$old_user.'"');
        }
        if($company != '') {
            $query = $query->andWhere('company="'.$company.'"');
        }
        $query = $query->orderBy($sort_key.' '.$sort_value.', car_e_t.id '.$sort_value);
        return $query;
    }

    private function getAdCleanQuery($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $register_begin = $request->post('register_begin');
            $register_end = $request->post('register_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $insurer1_month = $request->post('insurer1_date');
            $insurer1_day = $request->post('insurer1_day');
            $insurer1_month1 = $request->post('insurer1_date1');
            $insurer1_day1 = $request->post('insurer1_day1');
            $username = $request->post('username');
            $status = $request->post('status');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
            $company = $request->post('company');
            $repeat = $request->post('repeat');
            $yuyue = $request->post('yuyue');

        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $register_begin = $request->get('register_begin');
            $register_end = $request->get('register_end');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $insurer1_month = $request->get('insurer1_date');
            $insurer1_day = $request->get('insurer1_day');
            $insurer1_month1 = $request->get('insurer1_date1');
            $insurer1_day1 = $request->get('insurer1_day1');
            $username = $request->get('username');
            $status = $request->get('status');
            $repeat = $request->get('repeat');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
            $company = $request->get('company');
            $yuyue = $request->get('yuyue');
        }
        $query = CleanT::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`clean_t`.`id`');


        if($car_man != '') {
            $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
        }
        if($phone != '') {
            $query = $query->andWhere('car_t.phone like "'.$phone.'"');
        }
        if($car_no != '') {
            if($car_no == '非京'){
                $query = $query->andWhere('car_no not like "京%"');
            }else{
                $query = $query->andWhere('car_no like "'.$car_no.'%"');
            }
        }
        if($register_begin != '') {
            $query = $query->andWhere('car_t.register_date>="'.$register_begin.'"');
        }
        if($register_end != '') {
            $query = $query->andWhere('car_t.register_date<="'.$register_end.'"');
        }
        if($insurer1_begin != '') {
            $query = $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query = $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }

//        if($insurer1_month != '') {
//            $query = $query->andWhere('month(car_t.insurer1_date)>="'.$insurer1_month.'"');
//        }
//        if($insurer1_day != '') {
//            $query = $query->andWhere('day(car_t.insurer1_date)>="'.$insurer1_day.'"');
//        }
//        if($insurer1_month1 != '') {
//            $query = $query->andWhere('month(car_t.insurer1_date)<="'.$insurer1_month1.'"');
//        }
//        if($insurer1_day1 != '') {
//            $query = $query->andWhere('day(car_t.insurer1_date)<="'.$insurer1_day1.'"');
//        }





        if($repeat>0){
            $query = $query->andWhere(['repeat'=>$repeat]);
        }
//        if($username != '') {
//            $user_info = UserT::findOne(['username'=>$username]);
//            if($user_info) {
//                $query = $query->andWhere('clean_t.user_id='.$user_info->id);
//            }
//        }

        if($yuyue>0){
            $query = $query->andWhere(['yuyue'=>$yuyue]);
        }

        //采用这个
        if($status==1){ //未分配
            $query = $query->andWhere('clean_t.user_id=0');
        }
        if($status==2){
            $query = $query->andWhere('clean_t.user_id>0'); //已分配
            if($username == '') {
                $user_ids = $this->getChildrenUserIDs();
                $query = $query->andWhere(['in','clean_t.user_id',$user_ids]);
            }
        }



        //
//        $query = $query->andWhere('clean_t.user_id= 0');
        if($company != '') {
            $query = $query->andWhere('company like "'.$company.'%"');
        }
        $query = $query->orderBy($sort_key.' '.$sort_value.', clean_t.id '.$sort_value);
//        echo $query->createCommand()->rawSql;
//        exit;
        return $query;
    }
    public function actionAdCleanIndex()
    {
        $request = Yii::$app->request;
        $page = $request->get('page',1);
        $op = $request->get('op',1);

        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $register_begin = $request->get('register_begin');
        $register_end = $request->get('register_end');
        $insurer1_begin = $request->get('insurer1_begin');
        $insurer1_end = $request->get('insurer1_end');
        $insurer1_month = $request->get('insurer1_date');
        $insurer1_day = $request->get('insurer1_day');
        $insurer1_month1 = $request->get('insurer1_date1');
        $insurer1_day1 = $request->get('insurer1_day1');
        $username = $request->get('username');
        $status = $request->get('status',1);
        $repeat = $request->get('repeat',1);
        $yuyue = $request->get('yuyue',1);
//        echo $yuyue;
        $sort_key = $request->get('sort_key');
        $sort_value = $request->get('sort_value');
        $user_id = $request->get('user_id');
//        dd($user_id);
        $company = $request->get('company');

        $query = $this->getAdCleanQuery($request);
        if($op==0) {
            $query = $query->andWhere('clean_t.id=-1');
        }
//        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::getPageInfo($pagination);

        $invalid_items = InvalidT::getTree();
        $user_items = $this->my->getChildren();

        $monthes= CarT::MONTH;
        $days= CarT::DAYS;
        $carAcount=CleanT::find()->where('user_id=0')->count();
//        $cfor=CleanT::find();
//        foreach($cfor->each(1) as $c){
//
//            $c->now_time=new Expression('NOW()');
//            $c->save();
//
//        }
        $role_id=$this->my->role_id;
        return $this->render('ad-clean-index',[
            'items' => $items,
            'monthes' => $monthes,
            'days' => $days,
            'page_info' => $page_info,
            'page' => $page,
            'car_man' => $car_man,
            'phone' => $phone,
            'car_no' => $car_no,
            'register_begin' => $register_begin,
            'register_end' => $register_end,
            'insurer1_begin' => $insurer1_begin,
            'insurer1_end' => $insurer1_end,
            'insurer1_month' =>$insurer1_month,
            'insurer1_day' =>$insurer1_day,
            'insurer1_month1' =>$insurer1_month1,
            'insurer1_day1' =>$insurer1_day1,
            'invalid_items' => $invalid_items,
            'user_items' => $user_items,
            'user_id' => $user_id,
            'status' => $status,
            'repeat' => $repeat,
            'yuyue' => $yuyue,
            'username' => $username,
            'sort_key' => $sort_key,
            'sort_value' => $sort_value,
            'company' => $company,
            'role_id' => $role_id,
            'carAcount' => $carAcount,
        ]);
    }
    
    public function actionEIndex()
    {

        $request = Yii::$app->request;
        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $engine_no = $request->get('engine_no');
        $car_frame_no = $request->get('car_frame_no');
        $print_begin = $request->get('print_begin');
        $print_end = $request->get('print_end');
        $insurer1_begin = $request->get('insurer1_begin');
        $insurer1_end = $request->get('insurer1_end');
        $status = $request->get('status');
        $username = $request->get('username');
        $sort_key = $request->get('sort_key');
        $sort_value = $request->get('sort_value');
        $user_id = $request->get('user_id');
        $page = $request->get('page',1);
        $op = $request->get('op',1);
        $company = $request->get('company');
        $register_begin = $request->get('register_begin');
        $register_end = $request->get('register_end');
        $query = $this->getEQuery($request);

        if($op==0) {
            $query = $query->andWhere('car_e_t.id=-1');
        }
//        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::getPageInfo($pagination);

        $invalid_items = InvalidT::getTree();
        $user_items = $this->my->getChildren();

        return $this->render('e-index',[
            'items' => $items,
            'page_info' => $page_info,
            'page' => $page,
            'car_man' => $car_man,
            'phone' => $phone,
            'car_no' => $car_no,
            'engine_no' => $engine_no,
            'car_frame_no' => $car_frame_no,
            'print_begin' => $print_begin,
            'print_end' => $print_end,
            'register_begin' => $register_begin,
            'register_end' => $register_end,
            'insurer1_begin' => $insurer1_begin,
            'insurer1_end' => $insurer1_end,
            'invalid_items' => $invalid_items,
            'user_items' => $user_items,
            'status' => $status,
            'username' => $username,
            'sort_key' => $sort_key,
            'sort_value' => $sort_value,
            'user_id' => $user_id,
            'company' => $company
        ]);
    }


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

        if($request->isPost) {
            $status = $request->post('status');
            if($status != 1) {
                $result['msg'] = '请先选择分配状态!';
                return $result;
            }
            $assign_users = explode(',',$request->post('assign_users'));
            $assign_count = $request->post('assign_count',0);
            $assign_user_ids = array();
            foreach($assign_users as $assign_user) {
                $user_info = UserT::findOne(['username'=>$assign_user]);
                if($user_info)
                    $assign_user_ids[] = $user_info->id;
            }
            if(count($assign_user_ids) == 0) {
                $result['msg'] = '请输入业务员工号,以英文逗号间隔';
                return $result;
            }
            if($assign_count == 0) {
                $result['msg'] = '请输入每个业务员分配记录数';
                return $result;
            }

            $query = $this->getEQuery($request);

            $tran = CarT::getDb()->beginTransaction();
            try {
                $page = 0;
                $total_pages = count($assign_user_ids);
                $index = 0;
                foreach($query->each(100) as $c_info) {
                    if($index >= ($page+1) * $assign_count) $page += 1;
                    if($page == $total_pages) break;
                    if($c_info->user_id > 0) continue;
                    $c_info->user_id = $assign_user_ids[$page];
                    $c_info->save();

                    $car_info = $c_info->car;
                    $car_info->op_user2 = $c_info->user->getShowName();
                    $car_info->save();

                    //添加预约
                    AppointmentT::deleteAll('car_id='.$c_info->id);
                    $row = new AppointmentT();
                    $row->car_id = $c_info->id;
                    $row->user_id = $c_info->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 = $c_info->id;
                    $h_row->user_id = $c_info->user_id;
                    $h_row->pdate = date('Y-m-d');
                    $h_row->ptime = '09:00';
                    $h_row->remark = '首次分配';
                    $h_row->save();

                    $this->addLog($c_info->id,'从E库分配数据给业务员:'.$c_info->user->getShowName(),1);
                    $index++;
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

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

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

        if($request->isPost) {
            $status = $request->post('status');
            if($status != 1) {
                $result['msg'] = '请先选择分配状态!';
                return $result;
            }
            $query = $this->getCQuery($request);

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $c_info) {
                    $car_info = CarT::findOne(['id'=>$c_info->id]);
                    if($car_info->location == 3) {
                        $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);
                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    
    public function actionEMoveD()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $status = $request->post('status');
            if($status != 1) {
                $result['msg'] = '请先选择分配状态!';
                return $result;
            }
            $query = $this->getEQuery($request);

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $c_info) {
                    $car_info = CarT::findOne(['id'=>$c_info->id]);
                    if($car_info->location == 6) {
                        $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);
                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    public function actionFMoveD()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

//        echo 333;
//        die;

        if($request->isPost) {

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

            $query = $this->getFQuery($request);

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $c_info) {
                    $car_info = CarT::findOne(['id'=>$c_info->id]);
                    if($car_info->location == 7) {
                        $car_info->location = 4;
                        $car_info->save();

                        CarFT::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,'从F库分配到D库',1);
                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    public function actionFMoveA()
    {
        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;
            }

            $query = $this->getFQuery($request);

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $c_info) {
                    $car_info = CarT::findOne(['id'=>$c_info->id]);
                    if($car_info->location == 7) {
                        $car_info->location = 1;
                        $car_info->save();

                        CarFT::deleteAll(['id'=>$car_info->id]);
                        $this->addLog($car_info->id,'从F库分配到A库',1);
//                        $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();
//
//
//                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

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

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

//        echo 333;
//        die;

        if($request->isPost) {

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

            $query = $this->getF1Query($request);

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $c_info) {
                    $car_info = CarT::findOne(['id'=>$c_info->id]);
                    if($car_info->location == 8) {
                        $car_info->location = 2;
                        $car_info->save();

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

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

                            $this->addLog($car_info->id,'从F1库分配到B库',1);
                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    public function actionF1MoveA()
    {
        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;
            }

            $query = $this->getF1Query($request);

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $c_info) {
                    $car_info = CarT::findOne(['id'=>$c_info->id]);
                    if($car_info->location == 8) {
                        $car_info->location = 1;
                        $car_info->save();

                        CarF1T::deleteAll(['id'=>$car_info->id]);
                        $this->addLog($car_info->id,'从F1库分配到A库',1);
//                        $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();
//
//
//                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    public function actionF1B()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $status = $request->post('status');
            if($status != 1) {
                $result['msg'] = '请先选择分配状态!';
                return $result;
            }
            $query = $this->getF1Query($request);

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $c_info) {
                    $car_info = CarT::findOne(['id'=>$c_info->id]);
                    if($car_info->location == 8) {
                        $car_info->location = 2;
                        $car_info->save();

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

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

                            $this->addLog($car_info->id,'从F1库分配到B库',1);
                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

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

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

//        echo 333;
//        die;

        if($request->isPost) {

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

            $query = $this->getF2Query($request);

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $c_info) {
                    $car_info = CarT::findOne(['id'=>$c_info->id]);
                    if($car_info->location == 9) {
                        $car_info->location = 2;
                        $car_info->save();

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

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

                            $this->addLog($car_info->id,'从F2库分配到B库',1);
                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    public function actionF2MoveA()
    {
        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;
            }

            $query = $this->getF2Query($request);

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $c_info) {
                    $car_info = CarT::findOne(['id'=>$c_info->id]);
                    dd($car_info->location);
                    if($car_info->location == 8) {
                        $car_info->location = 1;
                        $car_info->save();
                        dd(32);
                        CarF2T::deleteAll(['id'=>$car_info->id]);
                        $this->addLog($car_info->id,'从F2库分配到A库',1);
//                        $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();
//
//
//                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    public function actionF2B()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $status = $request->post('status');
            if($status != 1) {
                $result['msg'] = '请先选择分配状态!';
                return $result;
            }
            $query = $this->getF2Query($request);

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $c_info) {
                    $car_info = CarT::findOne(['id'=>$c_info->id]);
                    if($car_info->location == 9) {
                        $car_info->location = 2;
                        $car_info->save();

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

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

                            $this->addLog($car_info->id,'从F2库分配到B库',1);
                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    public function actionF4MoveD()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

//        echo 333;
//        die;

        if($request->isPost) {

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

            $query = $this->getF4Query($request);

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $c_info) {
                    $car_info = CarT::findOne(['id'=>$c_info->id]);
                    if($car_info->location == 11) {
                        $car_info->location = 4;
                        $car_info->save();

                        CarF4T::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,'从F4库分配到D库',1);
                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    public function actionF4MoveA()
    {
        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;
            }

            $query = $this->getF4Query($request);

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $c_info) {
                    $car_info = CarT::findOne(['id'=>$c_info->id]);
                    if($car_info->location == 11) {
                        $car_info->location = 1;
                        $car_info->save();

                        CarF4T::deleteAll(['id'=>$car_info->id]);
                        $this->addLog($car_info->id,'从F4库分配到A库',1);
//                        $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();
//
//
//                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    public function actionF4D()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $status = $request->post('status');
            if($status != 1) {
                $result['msg'] = '请先选择分配状态!';
                return $result;
            }
            $query = $this->getF4Query($request);

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $c_info) {
                    $car_info = CarT::findOne(['id'=>$c_info->id]);
                    if($car_info->location == 11) {
                        $car_info->location = 4;
                        $car_info->save();

                        CarF4T::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,'从F4库分配到D库',1);
                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    public function actionF5MoveD()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

//        echo 333;
//        die;

        if($request->isPost) {

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

            $query = $this->getF4Query($request);

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $c_info) {
                    $car_info = CarT::findOne(['id'=>$c_info->id]);
                    if($car_info->location == 12) {
                        $car_info->location = 4;
                        $car_info->save();

                        CarF5T::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,'从F5库分配到D库',1);
                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    public function actionF5MoveA()
    {
        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;
            }

            $query = $this->getF4Query($request);

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $c_info) {
                    $car_info = CarT::findOne(['id'=>$c_info->id]);
                    if($car_info->location == 12) {
                        $car_info->location = 1;
                        $car_info->save();

                        CarF5T::deleteAll(['id'=>$car_info->id]);
                        $this->addLog($car_info->id,'从F5库分配到A库',1);
//                        $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();
//
//
//                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    public function actionF5D()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $status = $request->post('status');
            if($status != 1) {
                $result['msg'] = '请先选择分配状态!';
                return $result;
            }
            $query = $this->getF4Query($request);

            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $c_info) {
                    $car_info = CarT::findOne(['id'=>$c_info->id]);
                    if($car_info->location == 12) {
                        $car_info->location = 4;
                        $car_info->save();

                        CarF5T::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,'从F5库分配到D库',1);
                        }
                    }
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

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

    /**
     * E库分配
     * @return array
     */
    public function actionEAssign()
    {
        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',0);

            if($user_id == 0) {
                $result['msg'] = '请先选择分配给哪个业务员';
                return $result;
            }
            if(count($ids) == 0) {
                $result['msg'] = '请先钩选车辆';
                return $result;
            }

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

                    if($c_info->user_id > 0) {
                        $result['msg'] = '该车辆已经被分配';
                        return $result;
                    }
                    $c_info->user_id = $user_id;
                    $c_info->save();

                    $car_info = $c_info->car;
                    $car_info->op_user2 = $c_info->user->getShowName();
                    $car_info->save();

                    //添加预约
                    AppointmentT::deleteAll('car_id='.$c_info->id);
                    $row = new AppointmentT();
                    $row->car_id = $c_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 = $c_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();

                    $this->addLog($c_info->id,'从E库分配跟踪数据到业务员:'.$c_info->user->getShowName(),1);
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

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

    public function actionEReturn()
    {
        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) {
                    $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->save();

                    $c_info->user_id = 0;
                    $c_info->save();
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    //F库
    private function getFQuery($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $print_begin = $request->post('print_begin');
            $print_end = $request->post('print_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $insurer1_month = $request->post('insurer1_month');
            $insurer1_day = $request->post('insurer1_day');
            $insurer1_month1 = $request->post('insurer1_month1');
            $insurer1_day1 = $request->post('insurer1_day1');

            $status = $request->post('status');
            $username = $request->post('username');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
            $old_user = $request->post('old_user');
            $company = $request->post('company');
            $user1 = $request->post('user1');
        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $print_begin = $request->get('print_begin');
            $print_end = $request->get('print_end');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $insurer1_month = $request->get('insurer1_month');
            $insurer1_day = $request->get('insurer1_day');
            $insurer1_month1 = $request->get('insurer1_month1');
            $insurer1_day1 = $request->get('insurer1_day1');

            $status = $request->get('status');
            $username = $request->get('username');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
            $old_user = $request->get('old_user');
            $company = $request->get('company');
            $user1 = $request->get('user1');
        }
        $query = CarFT::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`car_f_t`.`id`');
        if($car_man != '') {
            $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
        }
        if($phone != '') {
            $query = $query->andWhere('car_t.phone like "'.$phone.'"');
        }
        if($car_no != '') {
            $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
        }
        if($print_begin != '') {
            $query = $query->andWhere('car_t.print_date>="'.$print_begin.'"');
        }
        if($print_end != '') {
            $query = $query->andWhere('car_t.print_date<="'.$print_end.'"');
        }
        if($insurer1_begin != '') {
            $query = $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query = $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }
        if($insurer1_month != '') {
            $query = $query->andWhere('month(car_t.insurer1_date)>="'.$insurer1_month.'"');
        }
        if($insurer1_day != '') {
            $query = $query->andWhere('day(car_t.insurer1_date)>="'.$insurer1_day.'"');
        }
        if($insurer1_month1 != '') {
            $query = $query->andWhere('month(car_t.insurer1_date)<="'.$insurer1_month1.'"');
        }
        if($insurer1_day1 != '') {
            $query = $query->andWhere('day(car_t.insurer1_date)<="'.$insurer1_day1.'"');
        }

        if($status == 1) {
            $query = $query->andWhere('car_f_t.user_id=0');
        }
        if($status == 2) {
            $query = $query->andWhere('car_f_t.user_id>0');
        }
        if($username != '') {
            $user_info = UserT::findOne(['username'=>$username]);
            if($user_info) {
                $query = $query->andWhere('car_f_t.user_id='.$user_info->id);
            }
        }
        if($old_user != '') {
            $query = $query->andWhere('car_t.old_user="'.$old_user.'"');
        }
        if($company != '') {
            $query = $query->andWhere('company like"'.$company.'%"');
        }
        if($user1 != '') {
            $query = $query->andWhere('op_user1 like "'.$user1.'%"');
        }
        $query = $query->orderBy($sort_key.' '.$sort_value.', car_f_t.id '.$sort_value);
        //echo $query->createCommand()->rawSql;
        return $query;
    }
    public function actionFIndex()
    {
        $request = Yii::$app->request;
        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $engine_no = $request->get('engine_no');
        $car_frame_no = $request->get('car_frame_no');
        $print_begin = $request->get('print_begin');
        $print_end = $request->get('print_end');
        $insurer1_begin = $request->get('insurer1_begin');
        $insurer1_end = $request->get('insurer1_end');
        $insurer1_month = $request->get('insurer1_month');
        $insurer1_day = $request->get('insurer1_day');
        $insurer1_month1 = $request->get('insurer1_month1');
        $insurer1_day1 = $request->get('insurer1_day1');



        $status = $request->get('status');
        $username = $request->get('username');
        $sort_key = $request->get('sort_key');
        $sort_value = $request->get('sort_value');
        $user_id = $request->get('user_id');
        $page = $request->get('page',1);
        $op = $request->get('op',1);
        $company = $request->get('company');
        $user1 = $request->get('user1');

        $query = $this->getFQuery($request);
        if($op==0) {
            $query = $query->andWhere('car_f_t.id=-1');
        }
        //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::getPageInfo($pagination);

        $invalid_items = InvalidT::getTree();
        $user_items = $this->my->getChildren();
        $monthes= CarT::MONTH;
        $days= CarT::DAYS;
        return $this->render('f-index',[
            'items' => $items,
            'monthes' => $monthes,
            'days' => $days,
            'page_info' => $page_info,
            'page' => $page,
            'car_man' => $car_man,
            'phone' => $phone,
            'car_no' => $car_no,
            'engine_no' => $engine_no,
            'car_frame_no' => $car_frame_no,
            'print_begin' => $print_begin,
            'print_end' => $print_end,
            'insurer1_begin' => $insurer1_begin,
            'insurer1_end' => $insurer1_end,
            'insurer1_month' =>$insurer1_month,
            'insurer1_day' =>$insurer1_day,
            'insurer1_month1' =>$insurer1_month1,
            'insurer1_day1' =>$insurer1_day1,
            'invalid_items' => $invalid_items,
            'user_items' => $user_items,
            'status' => $status,
            'username' => $username,
            'sort_key' => $sort_key,
            'sort_value' => $sort_value,
            'user_id' => $user_id,
            'company' => $company,
            'user1' => $user1
        ]);
    }
    private function getF1Query($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $print_begin = $request->post('print_begin');
            $print_end = $request->post('print_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $insurer1_month = $request->post('insurer1_month');
            $insurer1_day = $request->post('insurer1_day');
            $insurer1_month1 = $request->post('insurer1_month1');
            $insurer1_day1 = $request->post('insurer1_day1');

            $status = $request->post('status');
            $username = $request->post('username');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
            $old_user = $request->post('old_user');
            $company = $request->post('company');
            $user1 = $request->post('user1');
        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $print_begin = $request->get('print_begin');
            $print_end = $request->get('print_end');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $insurer1_month = $request->get('insurer1_month');
            $insurer1_day = $request->get('insurer1_day');
            $insurer1_month1 = $request->get('insurer1_month1');
            $insurer1_day1 = $request->get('insurer1_day1');

            $status = $request->get('status');
            $username = $request->get('username');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
            $old_user = $request->get('old_user');
            $company = $request->get('company');
            $user1 = $request->get('user1');
        }
        $query = CarF1T::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`car_f1_t`.`id`');
        if($car_man != '') {
            $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
        }
        if($phone != '') {
            $query = $query->andWhere('car_t.phone like "'.$phone.'"');
        }
        if($car_no != '') {
            $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
        }
        if($print_begin != '') {
            $query = $query->andWhere('car_t.print_date>="'.$print_begin.'"');
        }
        if($print_end != '') {
            $query = $query->andWhere('car_t.print_date<="'.$print_end.'"');
        }
        if($insurer1_begin != '') {
            $query = $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query = $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }
        if($insurer1_month != '') {
            $query = $query->andWhere('month(car_t.insurer1_date)>="'.$insurer1_month.'"');
        }
        if($insurer1_day != '') {
            $query = $query->andWhere('day(car_t.insurer1_date)>="'.$insurer1_day.'"');
        }
        if($insurer1_month1 != '') {
            $query = $query->andWhere('month(car_t.insurer1_date)<="'.$insurer1_month1.'"');
        }
        if($insurer1_day1 != '') {
            $query = $query->andWhere('day(car_t.insurer1_date)<="'.$insurer1_day1.'"');
        }

        if($status == 1) {
            $query = $query->andWhere('car_f1_t.user_id=0');
        }
        if($status == 2) {
            $query = $query->andWhere('car_f1_t.user_id>0');
        }
        if($username != '') {
            $user_info = UserT::findOne(['username'=>$username]);
            if($user_info) {
                $query = $query->andWhere('car_f1_t.user_id='.$user_info->id);
            }
        }
        if($old_user != '') {
            $query = $query->andWhere('car_t.old_user="'.$old_user.'"');
        }
        if($company != '') {
            $query = $query->andWhere('company like"'.$company.'%"');
        }
        if($user1 != '') {
            $query = $query->andWhere('op_user1 like "'.$user1.'%"');
        }
        $query = $query->orderBy($sort_key.' '.$sort_value.', car_f1_t.id '.$sort_value);
        //echo $query->createCommand()->rawSql;
        return $query;
    }
    public function actionF1Index()
    {
        $request = Yii::$app->request;
        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $engine_no = $request->get('engine_no');
        $car_frame_no = $request->get('car_frame_no');
        $print_begin = $request->get('print_begin');
        $print_end = $request->get('print_end');
        $insurer1_begin = $request->get('insurer1_begin');
        $insurer1_end = $request->get('insurer1_end');
        $insurer1_month = $request->get('insurer1_month');
        $insurer1_day = $request->get('insurer1_day');
        $insurer1_month1 = $request->get('insurer1_month1');
        $insurer1_day1 = $request->get('insurer1_day1');



        $status = $request->get('status');
        $username = $request->get('username');
        $sort_key = $request->get('sort_key');
        $sort_value = $request->get('sort_value');
        $user_id = $request->get('user_id');
        $page = $request->get('page',1);
        $op = $request->get('op',1);
        $company = $request->get('company');
        $user1 = $request->get('user1');

        $query = $this->getF1Query($request);
        if($op==0) {
            $query = $query->andWhere('car_f1_t.id=-1');
        }
        //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::getPageInfo($pagination);

        $invalid_items = InvalidT::getTree();
        $user_items = $this->my->getChildren();
        $monthes= CarT::MONTH;
        $days= CarT::DAYS;
        return $this->render('f1-index',[
            'items' => $items,
            'monthes' => $monthes,
            'days' => $days,
            'page_info' => $page_info,
            'page' => $page,
            'car_man' => $car_man,
            'phone' => $phone,
            'car_no' => $car_no,
            'engine_no' => $engine_no,
            'car_frame_no' => $car_frame_no,
            'print_begin' => $print_begin,
            'print_end' => $print_end,
            'insurer1_begin' => $insurer1_begin,
            'insurer1_end' => $insurer1_end,
            'insurer1_month' =>$insurer1_month,
            'insurer1_day' =>$insurer1_day,
            'insurer1_month1' =>$insurer1_month1,
            'insurer1_day1' =>$insurer1_day1,
            'invalid_items' => $invalid_items,
            'user_items' => $user_items,
            'status' => $status,
            'username' => $username,
            'sort_key' => $sort_key,
            'sort_value' => $sort_value,
            'user_id' => $user_id,
            'company' => $company,
            'user1' => $user1
        ]);
    }
    private function getF2Query($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $print_begin = $request->post('print_begin');
            $print_end = $request->post('print_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $insurer1_month = $request->post('insurer1_month');
            $insurer1_day = $request->post('insurer1_day');
            $insurer1_month1 = $request->post('insurer1_month1');
            $insurer1_day1 = $request->post('insurer1_day1');

            $status = $request->post('status');
            $username = $request->post('username');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
            $old_user = $request->post('old_user');
            $company = $request->post('company');
            $user1 = $request->post('user1');
        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $print_begin = $request->get('print_begin');
            $print_end = $request->get('print_end');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $insurer1_month = $request->get('insurer1_month');
            $insurer1_day = $request->get('insurer1_day');
            $insurer1_month1 = $request->get('insurer1_month1');
            $insurer1_day1 = $request->get('insurer1_day1');

            $status = $request->get('status');
            $username = $request->get('username');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
            $old_user = $request->get('old_user');
            $company = $request->get('company');
            $user1 = $request->get('user1');
        }
        $query = CarF2T::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`car_f2_t`.`id`');
        if($car_man != '') {
            $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
        }
        if($phone != '') {
            $query = $query->andWhere('car_t.phone like "'.$phone.'"');
        }
        if($car_no != '') {
            $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
        }
        if($print_begin != '') {
            $query = $query->andWhere('car_t.print_date>="'.$print_begin.'"');
        }
        if($print_end != '') {
            $query = $query->andWhere('car_t.print_date<="'.$print_end.'"');
        }
        if($insurer1_begin != '') {
            $query = $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query = $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }
        if($insurer1_month != '') {
            $query = $query->andWhere('month(car_t.insurer1_date)>="'.$insurer1_month.'"');
        }
        if($insurer1_day != '') {
            $query = $query->andWhere('day(car_t.insurer1_date)>="'.$insurer1_day.'"');
        }
        if($insurer1_month1 != '') {
            $query = $query->andWhere('month(car_t.insurer1_date)<="'.$insurer1_month1.'"');
        }
        if($insurer1_day1 != '') {
            $query = $query->andWhere('day(car_t.insurer1_date)<="'.$insurer1_day1.'"');
        }

        if($status == 1) {
            $query = $query->andWhere('car_f2_t.user_id=0');
        }
        if($status == 2) {
            $query = $query->andWhere('car_f2_t.user_id>0');
        }
        if($username != '') {
            $user_info = UserT::findOne(['username'=>$username]);
            if($user_info) {
                $query = $query->andWhere('car_f2_t.user_id='.$user_info->id);
            }
        }
        if($old_user != '') {
            $query = $query->andWhere('car_t.old_user="'.$old_user.'"');
        }
        if($company != '') {
            $query = $query->andWhere('company like"'.$company.'%"');
        }
        if($user1 != '') {
            $query = $query->andWhere('op_user1 like "'.$user1.'%"');
        }
        $query = $query->orderBy($sort_key.' '.$sort_value.', car_f2_t.id '.$sort_value);
        //echo $query->createCommand()->rawSql;
        return $query;
    }
    public function actionF2Index()
    {
        $request = Yii::$app->request;
        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $engine_no = $request->get('engine_no');
        $car_frame_no = $request->get('car_frame_no');
        $print_begin = $request->get('print_begin');
        $print_end = $request->get('print_end');
        $insurer1_begin = $request->get('insurer1_begin');
        $insurer1_end = $request->get('insurer1_end');
        $insurer1_month = $request->get('insurer1_month');
        $insurer1_day = $request->get('insurer1_day');
        $insurer1_month1 = $request->get('insurer1_month1');
        $insurer1_day1 = $request->get('insurer1_day1');



        $status = $request->get('status');
        $username = $request->get('username');
        $sort_key = $request->get('sort_key');
        $sort_value = $request->get('sort_value');
        $user_id = $request->get('user_id');
        $page = $request->get('page',1);
        $op = $request->get('op',1);
        $company = $request->get('company');
        $user1 = $request->get('user1');

        $query = $this->getF2Query($request);
        if($op==0) {
            $query = $query->andWhere('car_f2_t.id=-1');
        }
        //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::getPageInfo($pagination);

        $invalid_items = InvalidT::getTree();
        $user_items = $this->my->getChildren();
        $monthes= CarT::MONTH;
        $days= CarT::DAYS;
        return $this->render('f2-index',[
            'items' => $items,
            'monthes' => $monthes,
            'days' => $days,
            'page_info' => $page_info,
            'page' => $page,
            'car_man' => $car_man,
            'phone' => $phone,
            'car_no' => $car_no,
            'engine_no' => $engine_no,
            'car_frame_no' => $car_frame_no,
            'print_begin' => $print_begin,
            'print_end' => $print_end,
            'insurer1_begin' => $insurer1_begin,
            'insurer1_end' => $insurer1_end,
            'insurer1_month' =>$insurer1_month,
            'insurer1_day' =>$insurer1_day,
            'insurer1_month1' =>$insurer1_month1,
            'insurer1_day1' =>$insurer1_day1,
            'invalid_items' => $invalid_items,
            'user_items' => $user_items,
            'status' => $status,
            'username' => $username,
            'sort_key' => $sort_key,
            'sort_value' => $sort_value,
            'user_id' => $user_id,
            'company' => $company,
            'user1' => $user1
        ]);
    }
    private function getF4Query($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $print_begin = $request->post('print_begin');
            $print_end = $request->post('print_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $insurer1_month = $request->post('insurer1_month');
            $insurer1_day = $request->post('insurer1_day');
            $insurer1_month1 = $request->post('insurer1_month1');
            $insurer1_day1 = $request->post('insurer1_day1');

            $status = $request->post('status');
            $username = $request->post('username');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
            $old_user = $request->post('old_user');
            $company = $request->post('company');
            $user1 = $request->post('user1');
        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $print_begin = $request->get('print_begin');
            $print_end = $request->get('print_end');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $insurer1_month = $request->get('insurer1_month');
            $insurer1_day = $request->get('insurer1_day');
            $insurer1_month1 = $request->get('insurer1_month1');
            $insurer1_day1 = $request->get('insurer1_day1');

            $status = $request->get('status');
            $username = $request->get('username');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
            $old_user = $request->get('old_user');
            $company = $request->get('company');
            $user1 = $request->get('user1');
        }
        $query = CarF4T::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`car_f4_t`.`id`');
        if($car_man != '') {
            $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
        }
        if($phone != '') {
            $query = $query->andWhere('car_t.phone like "'.$phone.'"');
        }
        if($car_no != '') {
            $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
        }
        if($print_begin != '') {
            $query = $query->andWhere('car_t.print_date>="'.$print_begin.'"');
        }
        if($print_end != '') {
            $query = $query->andWhere('car_t.print_date<="'.$print_end.'"');
        }
        if($insurer1_begin != '') {
            $query = $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query = $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }
        if($insurer1_month != '') {
            $query = $query->andWhere('month(car_t.insurer1_date)>="'.$insurer1_month.'"');
        }
        if($insurer1_day != '') {
            $query = $query->andWhere('day(car_t.insurer1_date)>="'.$insurer1_day.'"');
        }
        if($insurer1_month1 != '') {
            $query = $query->andWhere('month(car_t.insurer1_date)<="'.$insurer1_month1.'"');
        }
        if($insurer1_day1 != '') {
            $query = $query->andWhere('day(car_t.insurer1_date)<="'.$insurer1_day1.'"');
        }

        if($status == 1) {
            $query = $query->andWhere('car_f4_t.user_id=0');
        }
        if($status == 2) {
            $query = $query->andWhere('car_f4_t.user_id>0');
        }
        if($username != '') {
            $user_info = UserT::findOne(['username'=>$username]);
            if($user_info) {
                $query = $query->andWhere('car_f4_t.user_id='.$user_info->id);
            }
        }
        if($old_user != '') {
            $query = $query->andWhere('car_t.old_user="'.$old_user.'"');
        }
        if($company != '') {
            $query = $query->andWhere('company like"'.$company.'%"');
        }
        if($user1 != '') {
            $query = $query->andWhere('op_user1 like "'.$user1.'%"');
        }
        $query = $query->orderBy($sort_key.' '.$sort_value.', car_f4_t.id '.$sort_value);
        //echo $query->createCommand()->rawSql;
        return $query;
    }
    public function actionF4Index()
    {
        $request = Yii::$app->request;
        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $engine_no = $request->get('engine_no');
        $car_frame_no = $request->get('car_frame_no');
        $print_begin = $request->get('print_begin');
        $print_end = $request->get('print_end');
        $insurer1_begin = $request->get('insurer1_begin');
        $insurer1_end = $request->get('insurer1_end');
        $insurer1_month = $request->get('insurer1_month');
        $insurer1_day = $request->get('insurer1_day');
        $insurer1_month1 = $request->get('insurer1_month1');
        $insurer1_day1 = $request->get('insurer1_day1');



        $status = $request->get('status');
        $username = $request->get('username');
        $sort_key = $request->get('sort_key');
        $sort_value = $request->get('sort_value');
        $user_id = $request->get('user_id');
        $page = $request->get('page',1);
        $op = $request->get('op',1);
        $company = $request->get('company');
        $user1 = $request->get('user1');

        $query = $this->getF4Query($request);
        if($op==0) {
            $query = $query->andWhere('car_f4_t.id=-1');
        }
        //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::getPageInfo($pagination);

        $invalid_items = InvalidT::getTree();
        $user_items = $this->my->getChildren();
        $monthes= CarT::MONTH;
        $days= CarT::DAYS;
        return $this->render('f4-index',[
            'items' => $items,
            'monthes' => $monthes,
            'days' => $days,
            'page_info' => $page_info,
            'page' => $page,
            'car_man' => $car_man,
            'phone' => $phone,
            'car_no' => $car_no,
            'engine_no' => $engine_no,
            'car_frame_no' => $car_frame_no,
            'print_begin' => $print_begin,
            'print_end' => $print_end,
            'insurer1_begin' => $insurer1_begin,
            'insurer1_end' => $insurer1_end,
            'insurer1_month' =>$insurer1_month,
            'insurer1_day' =>$insurer1_day,
            'insurer1_month1' =>$insurer1_month1,
            'insurer1_day1' =>$insurer1_day1,
            'invalid_items' => $invalid_items,
            'user_items' => $user_items,
            'status' => $status,
            'username' => $username,
            'sort_key' => $sort_key,
            'sort_value' => $sort_value,
            'user_id' => $user_id,
            'company' => $company,
            'user1' => $user1
        ]);
    }
    private function getF5Query($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $print_begin = $request->post('print_begin');
            $print_end = $request->post('print_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $insurer1_month = $request->post('insurer1_month');
            $insurer1_day = $request->post('insurer1_day');
            $insurer1_month1 = $request->post('insurer1_month1');
            $insurer1_day1 = $request->post('insurer1_day1');

            $status = $request->post('status');
            $username = $request->post('username');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
            $old_user = $request->post('old_user');
            $company = $request->post('company');
            $user1 = $request->post('user1');
        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $print_begin = $request->get('print_begin');
            $print_end = $request->get('print_end');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $insurer1_month = $request->get('insurer1_month');
            $insurer1_day = $request->get('insurer1_day');
            $insurer1_month1 = $request->get('insurer1_month1');
            $insurer1_day1 = $request->get('insurer1_day1');

            $status = $request->get('status');
            $username = $request->get('username');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
            $old_user = $request->get('old_user');
            $company = $request->get('company');
            $user1 = $request->get('user1');
        }
        $query = CarF5T::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`car_f5_t`.`id`');
        if($car_man != '') {
            $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
        }
        if($phone != '') {
            $query = $query->andWhere('car_t.phone like "'.$phone.'"');
        }
        if($car_no != '') {
            $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
        }
        if($print_begin != '') {
            $query = $query->andWhere('car_t.print_date>="'.$print_begin.'"');
        }
        if($print_end != '') {
            $query = $query->andWhere('car_t.print_date<="'.$print_end.'"');
        }
        if($insurer1_begin != '') {
            $query = $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query = $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }
        if($insurer1_month != '') {
            $query = $query->andWhere('month(car_t.insurer1_date)>="'.$insurer1_month.'"');
        }
        if($insurer1_day != '') {
            $query = $query->andWhere('day(car_t.insurer1_date)>="'.$insurer1_day.'"');
        }
        if($insurer1_month1 != '') {
            $query = $query->andWhere('month(car_t.insurer1_date)<="'.$insurer1_month1.'"');
        }
        if($insurer1_day1 != '') {
            $query = $query->andWhere('day(car_t.insurer1_date)<="'.$insurer1_day1.'"');
        }

        if($status == 1) {
            $query = $query->andWhere('car_f5_t.user_id=0');
        }
        if($status == 2) {
            $query = $query->andWhere('car_f5_t.user_id>0');
        }
        if($username != '') {
            $user_info = UserT::findOne(['username'=>$username]);
            if($user_info) {
                $query = $query->andWhere('car_f5_t.user_id='.$user_info->id);
            }
        }
        if($old_user != '') {
            $query = $query->andWhere('car_t.old_user="'.$old_user.'"');
        }
        if($company != '') {
            $query = $query->andWhere('company like"'.$company.'%"');
        }
        if($user1 != '') {
            $query = $query->andWhere('op_user1 like "'.$user1.'%"');
        }
        $query = $query->orderBy($sort_key.' '.$sort_value.', car_f5_t.id '.$sort_value);
        //echo $query->createCommand()->rawSql;
        return $query;
    }
    public function actionF5Index()
    {
        $request = Yii::$app->request;
        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $engine_no = $request->get('engine_no');
        $car_frame_no = $request->get('car_frame_no');
        $print_begin = $request->get('print_begin');
        $print_end = $request->get('print_end');
        $insurer1_begin = $request->get('insurer1_begin');
        $insurer1_end = $request->get('insurer1_end');
        $insurer1_month = $request->get('insurer1_month');
        $insurer1_day = $request->get('insurer1_day');
        $insurer1_month1 = $request->get('insurer1_month1');
        $insurer1_day1 = $request->get('insurer1_day1');



        $status = $request->get('status');
        $username = $request->get('username');
        $sort_key = $request->get('sort_key');
        $sort_value = $request->get('sort_value');
        $user_id = $request->get('user_id');
        $page = $request->get('page',1);
        $op = $request->get('op',1);
        $company = $request->get('company');
        $user1 = $request->get('user1');

        $query = $this->getF5Query($request);
        if($op==0) {
            $query = $query->andWhere('car_f5_t.id=-1');
        }
        //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::getPageInfo($pagination);

        $invalid_items = InvalidT::getTree();
        $user_items = $this->my->getChildren();
        $monthes= CarT::MONTH;
        $days= CarT::DAYS;
        return $this->render('f5-index',[
            'items' => $items,
            'monthes' => $monthes,
            'days' => $days,
            'page_info' => $page_info,
            'page' => $page,
            'car_man' => $car_man,
            'phone' => $phone,
            'car_no' => $car_no,
            'engine_no' => $engine_no,
            'car_frame_no' => $car_frame_no,
            'print_begin' => $print_begin,
            'print_end' => $print_end,
            'insurer1_begin' => $insurer1_begin,
            'insurer1_end' => $insurer1_end,
            'insurer1_month' =>$insurer1_month,
            'insurer1_day' =>$insurer1_day,
            'insurer1_month1' =>$insurer1_month1,
            'insurer1_day1' =>$insurer1_day1,
            'invalid_items' => $invalid_items,
            'user_items' => $user_items,
            'status' => $status,
            'username' => $username,
            'sort_key' => $sort_key,
            'sort_value' => $sort_value,
            'user_id' => $user_id,
            'company' => $company,
            'user1' => $user1
        ]);
    }

    private function getDQuery1($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $insurer1_month = $request->post('insurer1_month');
            $insurer1_day = $request->post('insurer1_day');
            $insurer1_month1 = $request->post('insurer1_month1');
            $insurer1_day1 = $request->post('insurer1_day1');
            $status = $request->post('status',1);
            $username = $request->post('username');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
            $old_user = $request->post('old_user');
            $user1 = $request->post('user1');
            $user2 = $request->post('user2');
            $company = $request->post('company');
        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $insurer1_month = $request->get('insurer1_month');
            $insurer1_day = $request->get('insurer1_day');
            $insurer1_month1 = $request->get('insurer1_month1');
            $insurer1_day1 = $request->get('insurer1_day1');
            $status = $request->get('status',1);
            $username = $request->get('username');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
            $old_user = $request->get('old_user');
            $user1 = $request->get('user1');
            $user2 = $request->get('user2');
            $company = $request->get('company');
        }
        $params = array();
        $params['car_man'] = $car_man;
        $params['phone'] = $phone;
        $params['car_no'] = $car_no;
        $params['insurer1_begin'] = $insurer1_begin;
        $params['insurer1_end'] = $insurer1_end;
        $params['insurer1_month'] = $insurer1_month;
        $params['insurer1_day'] = $insurer1_day;
        $params['insurer1_month1'] = $insurer1_month1;
        $params['insurer1_day1'] = $insurer1_day1;
        $params['status'] = $status;
        $params['username'] = $username;
        $params['sort_key'] = $sort_key;
        $params['sort_value'] = $sort_value;
        $params['old_user'] = $old_user;
        $params['user1'] = $user1;
        $params['user2'] = $user2;
        $params['company'] = $company;

        $query = CarDT::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`car_d_t`.`id`')
            ->andWhere('car_d_t.back =1');
        if($car_man != '') {
            $query = $query->andWhere('car_t.car_man="'.$car_man.'"');
        }
        if($phone != '') {
            $query = $query->andWhere('car_t.phone like "'.$phone.'"');
        }
        if($car_no != '') {
            $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
        }
        if($insurer1_begin != '') {
            $query = $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query = $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }
        if($insurer1_month != '') {
            $query = $query->andWhere('month(car_t.insurer1_date)>="'.$insurer1_month.'"');
        }
        if($insurer1_day != '') {
            $query = $query->andWhere('day(car_t.insurer1_date)>="'.$insurer1_day.'"');
        }
        if($insurer1_month1 != '') {
            $query = $query->andWhere('month(car_t.insurer1_date)<="'.$insurer1_month1.'"');
        }
        if($insurer1_day1 != '') {
            $query = $query->andWhere('day(car_t.insurer1_date)<="'.$insurer1_day1.'"');
        }
        if($status == 1) {
            $query = $query->andWhere('car_d_t.user_id=0');
        }
        if($status == 2) {
            $query = $query->andWhere('car_d_t.user_id>0');
        }
        if($username != '') {
            $user_info = UserT::findOne(['username'=>$username]);
            if($user_info) {
                $query = $query->andWhere('car_d_t.user_id='.$user_info->id);
            }
        }
        if($old_user != '') {
            $query = $query->andWhere('car_t.old_user like "'.$old_user.'%"');
        }
        if($user1 != '') {
            $query = $query->andWhere('car_t.op_user1 like "'.$user1.'%"');
        }
        if($user2 != '') {
            $query = $query->andWhere('car_t.op_user3 like "'.$user2.'%"');
        }
        if($company != '') {
            $query = $query->andWhere('company like "'.$company.'%"');
        }
        $query = $query->orderBy($sort_key.' '.$sort_value.',car_d_t.id '.$sort_value);
        return ['query'=>$query,'params'=>$params];
    }

    public function actionDataDReturn()
    {
        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) {
                    $d_info = CarDT::findOne(['id'=>$id]);
                    if($d_info->user_id > 0){
                        $car_info = $d_info->car;
                        $car_info->user_id = 0;
                        $car_info->save();

                        $this->addLog($d_info->id,'从业务员:'.$d_info->user->getShowName().'收回数据',1);
                        $d_info->user_id=0;
                        $d_info->save();




                        //删除预约
                        $row = AppointmentT::find()
                            ->where('user_id='.$d_info->user_id.' and car_id='.$d_info->id)
                            ->one();
                        if($row) {
                            $row->delete();
                        }

                        //删除保单
                        $order_row = OrderT::find()
                            ->where('user_id='.$d_info->user_id.' and car_id='.$d_info->id.' and status_id=1')
                            ->one();
                        if($order_row) {
                            OrderUserT::deleteAll('order_id='.$order_row->id);
                            $order_row->delete();
                        }

                    }

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

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

    private function getInvalidQuery1($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $register_begin = $request->post('register_begin');
            $register_end = $request->post('register_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $invalid_id = $request->post('invalid_id');
            $username = $request->post('username');
            $user1 = $request->post('user1');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $register_begin = $request->get('register_begin');
            $register_end = $request->get('register_end');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $invalid_id = $request->get('invalid_id');
            $username = $request->get('username');
            $user1 = $request->get('user1');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
        }
        $params = array();
        $params['car_man'] = $car_man;
        $params['phone'] = $phone;
        $params['car_no'] = $car_no;
        $params['register_begin'] = $register_begin;
        $params['register_end'] = $register_end;
        $params['insurer1_begin'] = $insurer1_begin;
        $params['insurer1_end'] = $insurer1_end;
        $params['invalid_id'] = $invalid_id;
        $params['username'] = $username;
        $params['user1'] = $user1;
        $params['sort_key'] = $sort_key;
        $params['sort_value'] = $sort_value;

        $query = CarF3T::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`car_f3_t`.`id`');

        if($car_man != '') {
            $query = $query->andWhere('car_t.car_man="'.$car_man.'"');
        }
        if($phone != '') {
            $query = $query->andWhere('car_t.phone="'.$phone.'"');
        }
        if($car_no != '') {
            $query = $query->andWhere('car_t.car_no="'.$car_no.'"');
        }
        if($register_begin != '') {
            $query = $query->andWhere('car_t.register_date>="'.$register_begin.'"');
        }
        if($register_end != '') {
            $query = $query->andWhere('car_t.register_date<="'.$register_end.'"');
        }
        if($insurer1_begin != '') {
            $query = $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query = $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }
        if($invalid_id > 0) {
            $query = $query->andWhere('car_invalid_t.invalid_id='.$invalid_id);
        }
        if($user1 != '') {
            $query = $query->andWhere('car_t.op_user1 like "'.$user1.'%"');
        }
        if($username != '') {
            $user_info = UserT::findOne(['username'=>$username]);
            if($user_info) {
                $query = $query->andWhere('car_f3_t.user_id='.$user_info->id);
            }
        }
        $query = $query->orderBy($sort_key.' '.$sort_value.',car_f3_t.id '.$sort_value);
//        echo $query->createCommand()->rawSql;
        return ['query'=>$query,'params'=>$params];
    }

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

        $data = $this->getInvalidQuery1($request);
        $query = $data['query'];
        $params = $data['params'];
        $params['page'] = $page;

        if($op==0) {
            $query = $query->andWhere('car_f3_t.id=-1');
        }

//        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::getPageInfo($pagination);

        $invalid_items = InvalidT::getTreeXinbaoNew(2);

        $params['items'] = $items;
        $params['page_info'] = $page_info;
        $params['invalid_items'] = $invalid_items;

        return $this->render('f3-index',$params);
    }
    private function getInvalidQuery2($request)
    {
        if($request->isPost) {
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $register_begin = $request->post('register_begin');
            $register_end = $request->post('register_end');
            $insurer1_begin = $request->post('insurer1_begin');
            $insurer1_end = $request->post('insurer1_end');
            $invalid_id = $request->post('invalid_id');
            $username = $request->post('username');
            $user1 = $request->post('user1');
            $sort_key = $request->post('sort_key');
            $sort_value = $request->post('sort_value');
        } else {
            $car_man = $request->get('car_man');
            $phone = $request->get('phone');
            $car_no = $request->get('car_no');
            $register_begin = $request->get('register_begin');
            $register_end = $request->get('register_end');
            $insurer1_begin = $request->get('insurer1_begin');
            $insurer1_end = $request->get('insurer1_end');
            $invalid_id = $request->get('invalid_id');
            $username = $request->get('username');
            $user1 = $request->get('user1');
            $sort_key = $request->get('sort_key');
            $sort_value = $request->get('sort_value');
        }
        $params = array();
        $params['car_man'] = $car_man;
        $params['phone'] = $phone;
        $params['car_no'] = $car_no;
        $params['register_begin'] = $register_begin;
        $params['register_end'] = $register_end;
        $params['insurer1_begin'] = $insurer1_begin;
        $params['insurer1_end'] = $insurer1_end;
        $params['invalid_id'] = $invalid_id;
        $params['username'] = $username;
        $params['user1'] = $user1;
        $params['sort_key'] = $sort_key;
        $params['sort_value'] = $sort_value;

        $query = CarF6T::find()
            ->leftJoin(CarT::tableName(),'`car_t`.`id`=`car_f6_t`.`id`');

        if($car_man != '') {
            $query = $query->andWhere('car_t.car_man="'.$car_man.'"');
        }
        if($phone != '') {
            $query = $query->andWhere('car_t.phone="'.$phone.'"');
        }
        if($car_no != '') {
            $query = $query->andWhere('car_t.car_no="'.$car_no.'"');
        }
        if($register_begin != '') {
            $query = $query->andWhere('car_t.register_date>="'.$register_begin.'"');
        }
        if($register_end != '') {
            $query = $query->andWhere('car_t.register_date<="'.$register_end.'"');
        }
        if($insurer1_begin != '') {
            $query = $query->andWhere('car_t.insurer1_date>="'.$insurer1_begin.'"');
        }
        if($insurer1_end != '') {
            $query = $query->andWhere('car_t.insurer1_date<="'.$insurer1_end.'"');
        }
        if($invalid_id > 0) {
            $query = $query->andWhere('car_invalid_t.invalid_id='.$invalid_id);
        }
        if($user1 != '') {
            $query = $query->andWhere('car_t.op_user1 like "'.$user1.'%"');
        }
        if($username != '') {
            $user_info = UserT::findOne(['username'=>$username]);
            if($user_info) {
                $query = $query->andWhere('car_f6_t.user_id='.$user_info->id);
            }
        }
        $query = $query->orderBy($sort_key.' '.$sort_value.',car_f6_t.id '.$sort_value);
//        echo $query->createCommand()->rawSql;
        return ['query'=>$query,'params'=>$params];
    }

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

        $data = $this->getInvalidQuery2($request);
        $query = $data['query'];
        $params = $data['params'];
        $params['page'] = $page;

        if($op==0) {
            $query = $query->andWhere('car_f6_t.id=-1');
        }

//        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::getPageInfo($pagination);

        $invalid_items = InvalidT::getTreeXubao(2);

        $params['items'] = $items;
        $params['page_info'] = $page_info;
        $params['invalid_items'] = $invalid_items;

        return $this->render('f6-index',$params);
    }



}