<?php
/**
 * Created by PhpStorm.
 * User: liuyingjie
 * Date: 2017/11/1
 * Time: 9:25
 */

namespace frontend\controllers;
use common\models\DisplacementT;
use common\models\FixCarPart;
use common\models\FixCarPriceB;
use common\models\FixCarT;
use common\models\Message;
use common\models\MiddleFixCarT;
use Yii;
use yii\db\Exception;
use yii\web\Response;

class LeaveFactoryController extends FixCarBaseController
{
    public function actionIndex(){
        return $this->render('index',$this->getList());
    }
    public function actionInfo(){
        $request = Yii::$app->request;
        $id = $request->get('id', 0);
        $type = $request->get('type', 0);
        $gift_enable = $request->get('gift_enable', 0);

        $info = FixCarT::findOne(['id' => $id]);

        return $this->render('info', [
            'info' => $info,
            'direction_items' => $this->getDirection(),
            'range_items' =>  $this->getRange(),
            'log_items' => $this->getCarLog($info->car_id),
            'driver_items' => $this->getDrivers(),
            'brand_items' => $this->getBrands(),
            'series_items' => $this->getSeries($info->brand_id),
            'displacement_items' => $this->getDisplacement($info->series_id),
            'type' => $type,
            'years' => $this->getYears($info->displacement_id),
            'gift_enable' => $gift_enable,
        ]);
    }
    public function actionUpdate(){
        Yii::$app->response->format = Response::FORMAT_JSON;

        $request = Yii::$app->request;
        $follow_car_items = $request->post('follow_car_items');
        $status = $request->post('status');
        $car_id = $request->post('id');

        if($car_id < 1) return Message::getMessage();

        $tran = FixCarT::getDb()->beginTransaction();
        try{
            $model = FixCarT::findOne($car_id);
            $model->getModelScenario();
            $model->attributes = $request->post();
            $model->out_factory_date = date('Y-m-d H:i:s');
            //如果有随车物品，则拼接为字符串
            if (!empty($follow_car_items)) $model->follow_car_items = implode($follow_car_items, ',');
            if(!$model->validate()) throw new Exception(array_values($model->firstErrors)[0]);
            if(!$model->save()) throw new Exception('操作失败!');

            $middle_models = MiddleFixCarT::find()->where('fix_id='.$car_id)->all();
            foreach($middle_models as $middle_model){
                $middle_model->status = $status;
                if(!$middle_model->save()) throw new Exception('操作失败!');
            }

            $tran->commit();
            $success = true;
            $message = '操作成功';
        }catch(Exception $e){
            $tran->rollBack();
            $success = false;
            $message = $e->getMessage();
        }finally{
            unset($request,$follow_car_items,$status,$car_id,$tran,$model,$middle_models);
        }
        return Message::getMessage($success,$message);
    }
    public function actionTest(){
        $displacements = DisplacementT::find()->all();
        foreach($displacements as $displacement){
            $car_price_b = FixCarPriceB::find()->where('displacement_id='.$displacement->id)->all();
            if(!$car_price_b) continue;
            foreach($car_price_b as $p){
                if(FixCarPart::findOne([
                    'series_id'=>$displacement->series_id,
                    'displacement_id' => $displacement->id,
                    'year_id'=>$p->id,
                ])) continue;
                $data[] = [
                    'series_id'=>$displacement->series_id,
                    'displacement_id' => $displacement->id,
                    'year_id'=>$p->id,
                ];
            }
        }
        Yii::$app->getDb()->createCommand()->batchInsert(FixCarPart::tableName(),['series_id','displacement_id','year_id'],$data)->execute();
    }
}