<?php

namespace frontend\controllers;

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

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

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

        $total = $query->count();

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

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

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

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

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

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


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

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

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

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

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

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

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

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

            $tran = AppointmentT::getDb()->beginTransaction();
            try {
                $car_user_info = null;
                $car_info = CarT::findOne(['id'=>$car_id]);
                switch($car_info->location) {
                    case 2:
                        $car_user_info = CarBT::findOne(['id'=>$car_id]);
                        break;
                    case 3:
                        $car_user_info = CarCT::findOne(['id'=>$car_id]);
                        break;
                    case 4:
                        $car_user_info = CarDT::findOne(['id'=>$car_id]);
                        break;
                    case 5:
                        $car_user_info = CarInvalidT::findOne(['id'=>$car_id]);
                        break;
                    case 6:
                        $car_user_info = CarET::findOne(['id'=>$car_id]);
                        break;
                    case 14:
                        $car_user_info = CleanT::findOne(['id'=>$car_id]);
                        break;
                    default:
                        $name = '';
                }
                if(!$car_user_info) {
                    $result['msg'] = '该车辆未分配';
                    return $result;
                }
                if($car_user_info->user_id != $this->my->id) {
                    $result['msg'] = '该车辆未分配给'.$this->my->username.', 无法预约';
                    return $result;
                }
                $clean_info=CleanT::findOne(['id'=>$car_id]);
                if($clean_info){
                    $clean_info->yuyue=2;
                    $clean_info->lingqu_status=2;
                    $clean_info->save();
                }
                $cleantj_info=CleanTongji::find()->where(['car_id'=>$car_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);
                }



                $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 = $pdate;
                $row->ptime = $ptime;
                $row->ptype = $ptype;
                $row->remark = $remark;
                $row->business_group_id = $business_group_id;
                $row->is_first = 0;
                $row->save();



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

                $tran->commit();

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

            $result['success'] = true;
            $result['msg'] = '保存成功';
        }
        return $result;
    }

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

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

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

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

        return $result;
    }

    //首拨
    public function actionFirst()
    {
        $session = Yii::$app->session;
        $request = Yii::$app->request;
        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $page = $request->get('page',1);
        $username = $request->get('username');
        $sort_key = $request->get('sort_key','filtration_t.pdate');
        $sort_value = $request->get('sort_value','ASC');
        $user_id = $request->get('user_id');
        $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');






        $query = FiltrationT::find()
            ->leftJoin('car_t','`car_t`.`id`=`filtration_t`.`car_id`')
            ->andWhere(['!=','`filtration_t`.`remark`','首次分配'])
        ->andWhere('delete_status=1');

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

        if($car_man != '') {
            $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
        }
        if($phone != '') {
            $query = $query->andWhere('car_t.phone like "'.$phone.'"');
        }
        if($car_no != '') {
            $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
        }
        if($username != '') {
            $user_query = UserT::find()
                ->where('username="'.$username.'"')
                ->all();
//            foreach($user_query as $user_info) {
//                $user_ids[] = $user_info->id;
//            }
//            if(count($user_ids) > 0)
//                $query = $query->andWhere(['in','filtration_t.user_id',$user_ids]);
        }
        if($sort_key != '') {
            $query = $query->orderBy($sort_key.' '.$sort_value.',filtration_t.id '.$sort_value);
        }
        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.insurer2_date>="'.$insurer1_begin.'"');
//        }
//        if($insurer1_end != '') {
//            $query = $query->andWhere('car_t.insurer2_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.'"');
        }
        $sql = $query->createCommand()->rawSql;
//        echo $query->createCommand()->rawSql;
        $total = $query->count();

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

        $current_index = $pagination->offset;
        $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('first',[
            'items' => $items,
            'page_info' => $page_info,
            'car_man' => $car_man,
            'phone' => $phone,
            'car_no' => $car_no,
            'page' => $page,
            'sort_key' => $sort_key,
            'sort_value' => $sort_value,
            'type' => 1,
            'invalid_items' => $invalid_items,
            'username' => $username,
            'user_items' => $user_items,
            'user_id' => $user_id,
            '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,
        ]);
    }

    public function actionInfo()
    {
        $request = Yii::$app->request;
        $id = $request->get('id',0);
        $type = $request->get('type',0);
        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $car_no = $request->get('car_no');
        $page = $request->get('page',1);
        $sort_key = $request->get('sort_key','appointment_t.pdate');
        $sort_value = $request->get('sort_value','ASC');
        $next_index = $request->get('next_index',0);
        $back_params = $request->get('back_params');
        $my_path = $this->my->group?$this->my->group->getPath():'';

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

        $success_items = InvalidT::getTree(1);
//        $failure_items = InvalidT::getTreeXinbaoNew(2);
        $failure_items = InvalidT::getTreeClean(2);
        //下一辆
        $next_id = 0;
        if($type == 1) {
            $query = AppointmentT::find()
                ->leftJoin('car_t','`car_t`.`id`=`appointment_t`.`car_id`')
                ->where('appointment_t.is_first=1 and car_t.is_track=0')
                ->andWhere('appointment_t.pdate<="'.date('Y-m-d').'"');

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

            if($car_man != '') {
                $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
            }
            if($phone != '') {
                $query = $query->andWhere('car_t.phone="'.$phone.'"');
            }
            if($car_no != '') {
                $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
            }
            if($sort_key != '') {
                $query = $query->orderBy($sort_key.' '.$sort_value);
            }
            $next_item = $query->offset($next_index)->one();
            if($next_item) {
                $next_id = $next_item->car_id;
            }
            $next_index++;
        }
        if($type == 2) {
            $query = AppointmentT::find()
                ->leftJoin('car_t','`car_t`.`id`=`appointment_t`.`car_id`')
                ->where('appointment_t.is_first=0 and car_t.is_track=0')
                ->andWhere('appointment_t.pdate<="'.date('Y-m-d').'"');

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

            if($car_man != '') {
                $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
            }
            if($phone != '') {
                $query = $query->andWhere('car_t.phone="'.$phone.'"');
            }
            if($car_no != '') {
                $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
            }
            if($sort_key != '') {
                $query = $query->orderBy($sort_key.' '.$sort_value);
            }
            $next_item = $query->offset($next_index)->one();
            if($next_item) {
                $next_id = $next_item->car_id;
            }
            $next_index++;
        }
        if($type == 3) {
            $query = AppointmentT::find()
                ->leftJoin('car_t','`car_t`.`id`=`appointment_t`.`car_id`')
                ->where('appointment_t.is_first=0 and car_t.is_track=0');

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

            if($car_man != '') {
                $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
            }
            if($phone != '') {
                $query = $query->andWhere('car_t.phone="'.$phone.'"');
            }
            if($car_no != '') {
                $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
            }
            if($sort_key != '') {
                $query = $query->orderBy($sort_key.' '.$sort_value);
            }
            $next_item = $query->offset($next_index)->one();
            if($next_item) {
                $next_id = $next_item->car_id;
            }
            $next_index++;
        }



        //获取礼品数据


        $list = MeetT::getAllData(1);



        return $this->render('info',[
            'type' => $type,
            'car_info' => $car_info,



            'list' => $list,

            'success_items' => $success_items,
            'failure_items' => $failure_items,
            'next_id' => $next_id,
            'next_index' => $next_index,
            'car_man' => $car_man,
            'car_no' => $car_no,
            'phone' => $phone,
            'page' => $page,
            'sort_key' => $sort_key,
            'sort_value' => $sort_value,
            'back_params' => $back_params,
//            'gift_type_items' => $gift_type_items,


            'business_group_id' => $this->my->business_group_id,

        ]);
    }

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

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

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

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

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

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

        $page_info = MyLib::getPageInfo($pagination);

        $list = MeetT::getAllData(1);

        return $this->render('today',[
            'items' => $items,
            'page_info' => $page_info,
            'car_man' => $car_man,
            'phone' => $phone,
            'car_no' => $car_no,
            'ptype' => $ptype,
            'list'  => $list,
            'page' => $page,
            'sort_key' => $sort_key,
            'sort_value' => $sort_value,
            'type' => 2
        ]);
    }

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

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

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

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

        $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();
        
        $list = MeetT::getAllData(1);

        return $this->render('all',[
            'items' => $items,
            'page_info' => $page_info,
            'car_man' => $car_man,
            'phone' => $phone,
            'ptype' => $ptype,
            'list'  => $list,
            'car_no' => $car_no,
            'page' => $page,
            'sort_key' => $sort_key,
            'sort_value' => $sort_value,
            'type' => 3,
            'invalid_items' => $invalid_items,
            'username' => $username,
            'user_items' => $user_items,
            'user_id' => $user_id
        ]);
    }

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

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

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

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

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

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

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

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

//            FiltrationT
                    //删除预约拨打
                    //伪删除
//                    FiltrationT::deleteAll('car_id='.$c_info->id);
                   $fi_info= FiltrationT::find()->where('car_id='.$c_info->id)->all();
                    foreach($fi_info as $f){
                        $f->remark='首次分配';
                        $f->delete_status=2;
                        $f->save();
                    }




//              $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 actionAssignOther()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

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

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

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

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

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

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

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

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

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

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

                    $old_user_info = '';

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

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

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

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

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

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

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

    }


//    public function actionInvalidAllA()
//    {
//        Yii::$app->response->format = Response::FORMAT_JSON;
//        $request = Yii::$app->request;
//        $result = array();
//        $result['success'] = false;
//        $result['msg'] = '操作失败';
//
//        if($request->isPost) {
//
//            $car_man = $request->post('car_man');
//            $phone = $request->post('phone');
//            $car_no = $request->post('car_no');
//            $page = $request->post('page',1);
//            $username = $request->post('username');
//            $sort_key = $request->post('sort_key','filtration_t.pdate');
//            $sort_value = $request->post('sort_value','ASC');
//            $user_id = $request->post('user_id');
//
//            $query = FiltrationT::find()
//                ->leftJoin('car_t','`car_t`.`id`=`filtration_t`.`car_id`')
//                ->andWhere(['!=','`filtration_t`.`remark`','首次分配']);
//
//            if($username == '') {
//                $user_ids = $this->getChildrenUserIDs();
//                $query = $query->andWhere(['in','filtration_t.user_id',$user_ids]);
//            }
//
//            if($car_man != '') {
//                $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
//            }
//            if($phone != '') {
//                $query = $query->andWhere('car_t.phone like "'.$phone.'"');
//            }
//            if($car_no != '') {
//                $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
//            }
//            if($username != '') {
//                $user_query = UserT::find()
//                    ->where('username="'.$username.'"')
//                    ->all();
//                foreach($user_query as $user_info) {
//                    $user_ids[] = $user_info->id;
//                }
//                if(count($user_ids) > 0)
//                    $query = $query->andWhere(['in','filtration_t.user_id',$user_ids]);
//            }
//            if($sort_key != '') {
//                $query = $query->orderBy($sort_key.' '.$sort_value.',filtration_t.id '.$sort_value);
//            }
//
//
//            $tran = CarT::getDb()->beginTransaction();
//            try {
//                foreach($query->each(1000) as $item) {
//                    $car_info = $item->car;
//                    //伪删除
//                    $item->remark='首次分配';
//                    $item->delete_status=2;
//                    $item->save();
//
//                    $c_info = CleanT::findOne(['id'=>$car_info->id]);
//
//                    if($c_info) {
//                        $c_info->user_id = 0;
//                        $c_info->yuyue = 1;
//                        $c_info->lingqu_status=1;
//                        $c_info->save();
//                    }
//
////            FiltrationT
//                    //删除预约拨打
////                    FiltrationT::deleteAll('car_id='.$c_info->id);
//                    $this->addLog($item->id,'从预约列表数据中恢复清洗库数据',1);
//                }
//
//                $result['success'] = true;
//                $result['msg'] = '操作成功';
//
//                $tran->commit();
//            } catch (\Exception $e) {
//                $tran->rollBack();
//                throw $e;
//            }
//        }
//        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) {
//
//            $car_man = $request->post('car_man');
//            $phone = $request->post('phone');
//            $car_no = $request->post('car_no');
//            $page = $request->post('page',1);
//            $username = $request->post('username');
//            $sort_key = $request->post('sort_key','filtration_t.pdate');
//            $sort_value = $request->post('sort_value','ASC');
//            $user_id = $request->post('user_id');
//            $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');
//
//            $query = FiltrationT::find()
//                ->leftJoin('car_t','`car_t`.`id`=`filtration_t`.`car_id`')
//                ->andWhere(['!=','`filtration_t`.`remark`','首次分配']);
//
//            if($username == '') {
//                $user_ids = $this->getChildrenUserIDs();
//                $query = $query->andWhere(['in','filtration_t.user_id',$user_ids]);
//            }
//
//            if($car_man != '') {
//                $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
//            }
//            if($phone != '') {
//                $query = $query->andWhere('car_t.phone like "'.$phone.'"');
//            }
//            if($car_no != '') {
//                $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
//            }
//            if($username != '') {
//                $user_query = UserT::find()
//                    ->where('username="'.$username.'"')
//                    ->all();
//                foreach($user_query as $user_info) {
//                    $user_ids[] = $user_info->id;
//                }
//                if(count($user_ids) > 0)
//                    $query = $query->andWhere(['in','filtration_t.user_id',$user_ids]);
//            }
//            if($sort_key != '') {
//                $query = $query->orderBy($sort_key.' '.$sort_value.',filtration_t.id '.$sort_value);
//            }
//            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.insurer2_date>="'.$insurer1_begin.'"');
////        }
////        if($insurer1_end != '') {
////            $query = $query->andWhere('car_t.insurer2_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.'"');
//            }
//
//            $tran = CarT::getDb()->beginTransaction();
//            try {
//                foreach($query->each(1000) as $item) {
//                    $car_info = $item->car;
//                    //伪删除
//                    $item->remark='首次分配';
//                    $item->delete_status=2;
//                    $item->save();
//
//                    $c_info = CleanT::findOne(['id'=>$car_info->id]);
//
//                    if($c_info) {
//                        $c_info->user_id = 0;
//                        $c_info->yuyue = 1;
//                        $c_info->lingqu_status=1;
//                        $c_info->save();
//                    }
//
////            FiltrationT
//                    //删除预约拨打
////                    FiltrationT::deleteAll('car_id='.$c_info->id);
//                    $this->addLog($item->id,'从预约列表数据中恢复清洗库数据',1);
//                }
//
//                $result['success'] = true;
//                $result['msg'] = '操作成功';
//
//                $tran->commit();
//            } catch (\Exception $e) {
//                $tran->rollBack();
//                throw $e;
//            }
//        }
//        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) {

            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $car_no = $request->post('car_no');
            $page = $request->post('page',1);
            $username = $request->post('username');
            $sort_key = $request->post('sort_key','filtration_t.pdate');
            $sort_value = $request->post('sort_value','ASC');
            $user_id = $request->post('user_id');
            $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');

            $query = FiltrationT::find()
                ->leftJoin('car_t','`car_t`.`id`=`filtration_t`.`car_id`')
                ->andWhere(['!=','`filtration_t`.`remark`','首次分配']);

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

            if($car_man != '') {
                $query = $query->andWhere('car_t.car_man like "'.$car_man.'"');
            }
            if($phone != '') {
                $query = $query->andWhere('car_t.phone like "'.$phone.'"');
            }
            if($car_no != '') {
                $query = $query->andWhere('car_t.car_no like "'.$car_no.'"');
            }
            if($username != '') {
                $user_query = UserT::find()
                    ->where('username="'.$username.'"')
                    ->all();
//                foreach($user_query as $user_info) {
//                    $user_ids[] = $user_info->id;
//                }
//                if(count($user_ids) > 0)
//                    $query = $query->andWhere(['in','filtration_t.user_id',$user_ids]);
            }
            if($sort_key != '') {
                $query = $query->orderBy($sort_key.' '.$sort_value.',filtration_t.id '.$sort_value);
            }
            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.insurer2_date>="'.$insurer1_begin.'"');
//        }
//        if($insurer1_end != '') {
//            $query = $query->andWhere('car_t.insurer2_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.'"');
            }

           
            $tran = CarT::getDb()->beginTransaction();
            try {
                foreach($query->each(100) as $item) {
                    $car_info = $item->car;
                    //伪删除
                    $item->remark='首次分配';
                    $item->delete_status=2;
                    $item->save();

                    $c_info = CleanT::findOne(['id'=>$car_info->id]);

                    if($c_info) {
                        $c_info->user_id = 0;
                        $c_info->yuyue = 1;
                        $c_info->lingqu_status=1;
                        $c_info->save();
                    }

//            FiltrationT
                    //删除预约拨打
//                    FiltrationT::deleteAll('car_id='.$c_info->id);
                    $this->addLog($item->id,'从预约列表数据中恢复清洗库数据',1);
                }

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

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



}