<?php
/**
 * Created by PhpStorm.
 * User: liuyingjie
 * Date: 2017/8/31
 * Time: 17:11
 */

namespace frontend\controllers;

use common\models\FixCarItemT;
use common\models\FixCarProject;
use common\models\FixCarReport;
use common\models\FixCarT;
use common\models\FixItemRecord;
use common\models\Message;
use common\models\MiddleFixCarT;
use common\models\StoreRoom;
use Yii;
use yii\db\Exception;
use yii\helpers\ArrayHelper;
use yii\web\NotFoundHttpException;
use yii\web\Response;

class FixCarItemController extends BaseController
{
    public function behaviors()
    {
        return [
            [
                'class' => 'yii\filters\ContentNegotiator',
                'only' => ['add','batch-add','change-level','delete','pay-type-change','get-item','batch-get-item','confirm-fix','batch-confirm-fix','return','item','change-item-report','ajax-get-item'],
                'formats' => [
                    'application/json' => Response::FORMAT_JSON,
                ]
            ],
            'verbs' => [
                'class' => \yii\filters\VerbFilter::className(),
                'actions' => [
                    'ajax-get-item'     =>  ['post'],
                    'index'             =>  ['get'],
                    'add'               =>  ['post'],
                    'batch-add'         =>  ['post'],
                    'change-level'      =>  ['post'],
                    'delete'            =>  ['post'],
                    'pay-type-change'   =>  ['post'],
                    'items-list'        =>  ['post'],
                    'item-list-info'    =>  ['post'],
                    'get-item'          =>  ['post'],
                    'batch-get-item'    =>  ['post'],
                    'confirm-fix'       =>  ['post'],
                    'batch-confirm-fix' =>  ['post'],
                    'return'            =>  ['post'],
                    'item'              =>  ['post'],
                    'change-item-report'=>  ['post'],
                ],
            ],
        ];
    }
    public function actionIndex()
    {
        $request = Yii::$app->request;
        $fix_id = $request->get('fix_id', 0);
        $t = $request->get('t', 0);

        $view = '';
        $query = FixCarItemT::find()->where('fix_id=' . $fix_id);
        switch ($t) {
            case 1:
                $view = 'a-index';
                $query = $query->andWhere('type=1 OR (type =4 AND mark_wait_list=1)');
                break;
            case 2:
                $view = 'b-index';
                $query = $query->andWhere('(type=2 AND fix_group IN (7,8)) OR type=5 OR type = 6 OR (type=4 AND mark_wait_list=2)');
                break;
            case 3:
                $view = 'c-index';
                $query = $query->andWhere('type=3');
                break;
        }

        $render['items'] = $query->all();
        return $this->render($view, $render);
    }

    /**
     *------------------------------------------------------------
     *  单个添加
     * @param
     * @return  json
     * @author liuyingjie
     *------------------------------------------------------------
     */
    public function actionAdd()
    {
        $result = [];
        $result['success'] = false;
        $result['msg'] = '操作失败!';

        $request = Yii::$app->request;
        $id = $request->post('id');
        $fix_id = $request->post('fix_id');
        $t = $request->post('t');
        $wait = $request->post('wait', 0);
        $pay_type = $request->post('pay_type', 0);
        $report_id = $request->post('report_id',0);
        $report_id = $report_id ? $report_id : null;
        $group_id = $request->post('gid', 0);
        $price = $request->post('price',0);

        $tran = FixCarItemT::getDb()->beginTransaction();
        try {
            switch ($group_id) {
                case 11:
                    //待定
                    if ($wait > 0) {
                        $t = 4;
                        $wait = 6;
                        //非待定
                    } else {
                        $t = 6;
                    }
                    break;
                case 12:
                    //待定
                    if ($wait > 0) {
                        $t = 4;
                        $wait = 5;
                        //非待定
                    } else {
                        $t = 5;
                    }
                    break;
            }
            if (!$request->isAjax || $id <= 0 || $fix_id <= 0 || $t <= 0) return $result;

            $status = FixCarT::find()->select('status,brand_id,series_id,displacement_id,car_year')->where('id='.$fix_id)->one();

            if($status->status < 6) { //正常清单添加
                $item = ($res = FixCarItemT::findOne(['report_id' => $report_id, 'fk_id' => $id, 'fix_id' => $fix_id, 'status' => 0, 'fix_status' => 0, 'metal_plate_install' => 0, 'fix_user' => 0, 'lock_id' => 0, 'fix_group' => $group_id])) ? $res : new FixCarItemT();
            }else{ //已进入车间后，再次前台派工清单添加
                $item = ($res = FixCarItemT::findOne(['report_id'=>$report_id,'fk_id'=>$id,'fix_id'=>$fix_id,'fix_group'=>$group_id])) ? $res : new FixCarItemT();
            }
            $item->count = $item->isNewRecord ? 1 : ($fix_id == $item->fix_id && $id == $item->fk_id ? $item->count += 1 : $item->count);
            $item->fix_id = $fix_id;
            $item->fk_id = $id;
            $item->pay_type = $pay_type;
            $item->type = $t;
            $item->mark_wait_list = $wait > 0 ? $wait : 0;
            $item->report_id = $report_id;
            $item->fix_group = $group_id;
            $item->price = $price;

            //已进入车间 且 该项目不存在
            if(6 === $status->status && $item->isNewRecord){
                switch($group_id){
                    case 7:
                        $type = 1;
                        $middle_status = 6;
                        break;
                    case 8:
                        $type = 1;
                        $middle_status= 7;
                        break;
                    case 12:
                        $middle_status = 12;
                        $type = 1;
                        break;
                    case 10:
                    case 11:
                        $middle_status = 11;
                        $type = 2;
                        break;
                }

                $user = FixCarItemT::findOne(['report_id'=>$report_id,'fix_id'=>$fix_id,'fix_group'=>$group_id]);
                if(count($user) > 0){
                    $item->fix_user = $user->fix_user;
                    $item->lock_id = $user->lock_id;
                }

                $middle = MiddleFixCarT::findOne(['fix_id'=>$fix_id,'type'=>$type]);
                $middle->status = $middle_status;
                $middle->save();
            }
            if (!$item->save()) throw new Exception('a');

            //出库清单
            if(in_array($item->type,[1,3])){
                $part_name = StoreRoom::findOne($item->fk_id)->category->name;

                $item_record = ($item_record_res = FixItemRecord::findOne(['item_id'=>$item->id])) ? $item_record_res : new FixItemRecord();
                $item_record->name = $part_name;
                if(1 === $item->type) $item_record->group_id = $item->fix_group;
                $item_record->fix_id = $item->fix_id;
                $item_record->brand_id = $status->brand_id;
                $item_record->series_id = $status->series_id;
                $item_record->displacement_id = $status->displacement_id;
                $item_record->car_year = $status->car_year;
                $item_record->number = $item->count;
                $item_record->item_id = $item->id;
                $item_record->price = $item->price;

                if(!$item_record->save()) throw new Exception('c');
            }

            //当为喷漆时，自动添加底子
            if (8 == $group_id) {
                $bottom = ($res = FixCarItemT::findOne(['report_id' => $report_id, 'fk_id' => $item->fk_id, 'fix_id' => $item->fix_id, 'fix_group' => 13, 'status' => 0, 'fix_status' => 0, 'metal_plate_install' => 0, 'fix_user' => 0, 'lock_id' => 0])) ? $res : new FixCarItemT();

                $bottom->count = $item->count;
                $bottom->fix_id = $item->fix_id;
                $bottom->fk_id = $item->fk_id;
                $bottom->pay_type = $item->pay_type;
                $bottom->type = $item->type;
                $bottom->mark_wait_list = $item->mark_wait_list;
                $bottom->report_id = $item->report_id;
                $bottom->fix_group = 13;
                $bottom->price = 0;
                $bottom->fix_user =  $item->fix_user ? $item->fix_user : 0;
                $bottom->lock_id =  $item->lock_id ? $item->fix_user :0;

                if (!$bottom->save()) throw new Exception('b');
            }

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

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

        return $result;
    }

    /**
     *------------------------------------------------------------
     *  批量添加
     * @param
     * @return  json
     * @author liuyingjie
     *------------------------------------------------------------
     */
    public function actionBatchAdd()
    {
        $result = [];
        $result['success'] = false;
        $result['msg'] = '操作失败!';

        $request = Yii::$app->request;
        if (!$request->isAjax) return $result;

        $id = $request->post('ids', []);
        $prices = $request->post('price',[]);
        $fix_id = $request->post('fix_id');
        $t = $request->post('t');
        $pay_type = $request->post('pay_type', 0);
        $report_id = $request->post('report_id', null);
        $report_id = !$report_id ? null : $report_id;
        $group_id = $request->post('gid');
        switch ($group_id) {
            case 11:
                $t = 6;
                break;
            case 12:
                $t = 5;
                break;
        }
        if (0 == count($id)) return $result;
        foreach ($id as $key => $val) {
            if (FixCarItemT::updateAllCounters(['count' => 1], ['report_id' => $report_id, 'fix_id' => $fix_id, 'fk_id' => $val, 'fix_group' => $group_id, 'status' => 0, 'fix_status' => 0, 'metal_plate_install' => 0, 'fix_user' => 0, 'lock_id' => 0])) {
                continue;
            }
            $insert[] = [
                'fix_id' => $fix_id,
                'fk_id' => $val,
                'count' => 1,
                'pay_type' => $pay_type,
                'type' => $t,
                'report_id' => $report_id,
                'fix_user' => 0,
                'fix_remark' => '',
                'status' => 0,
                'insurance_price' => 0,
                'fix_group' => $group_id,
                'price'=>$prices[$key]
            ];
        }

        //如果是喷漆，则批量添加或更新底子
        if (8 == $group_id) {
            foreach ($id as $key => $val) {
                if (FixCarItemT::updateAllCounters(['count' => 1], ['report_id' => $report_id, 'fix_id' => $fix_id, 'fk_id' => $val, 'fix_group' => 13, 'status' => 0, 'fix_status' => 0, 'metal_plate_install' => 0, 'fix_user' => 0, 'lock_id' => 0])) {
                    continue;
                }
                $insert[] = [
                    'fix_id' => $fix_id,
                    'fk_id' => $val,
                    'count' => 1,
                    'pay_type' => $pay_type,
                    'type' => $t,
                    'report_id' => $report_id,
                    'fix_user' => 0,
                    'fix_remark' => '',
                    'status' => 0,
                    'insurance_price' => 0,
                    'fix_group' => 13
                ];
            }
        }
        if (isset($insert)) {
            Yii::$app->db->createCommand()->batchInsert(FixCarItemT::tableName(), ['fix_id', 'fk_id', 'count', 'pay_type', 'type', 'report_id', 'fix_user', 'fix_remark', 'status', 'insurance_price', 'fix_group'], $insert)->execute();
        }
        $result['success'] = true;
        $result['msg'] = '操作成功!';
        return $result;
    }

    /**
     *------------------------------------------------------------
     *  修改损伤程度
     * @param
     * @return view
     * @author liuyingjie
     *------------------------------------------------------------
     */
    public function actionChangeLevel()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $result = [];
        $result['success'] = true;
        $result['msg'] = '操作失败!';

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

        if (!$request->isAjax || $id <= 0) return $result;

        $level = $request->post('level');
        $category_id = $request->post('category_id');

        try {
            $project = FixCarProject::findOne(['car_price' => $level, 'category_id' => $category_id]);
            if (!$project) throw new Exception('');

            $item = FixCarItemT::findOne($id);
            $item->fk_id = $project->id;
            if (!$item->save()) throw new Exception('');

            $result['success'] = true;
            $result['msg'] = '操作成功!';
        } catch (Exception $e) {
            $result['msg'] = $e->getMessage();
        }
        return $result;
    }

    /**
     *------------------------------------------------------------
     *  清单删除
     * @param int id fix_car_item_t 表主键
     * @return json
     * @author liuyingjie
     *------------------------------------------------------------
     */
    public function actionDelete()
    {
        $request = Yii::$app->request;
        $id = intval($request->post('id'));
        $gid = intval($request->post('gid'));

        if (!$request->isAjax || $id <= 0) return Message::getMessage();

        try {
            $fix_car_item =  FixCarItemT::findOne($id);
            //判断该维修信息是否已绑定维修工
            if($fix_car_item->fix_user > 0) throw new Exception('该条信息已被'.$fix_car_item->user->getShowName().'锁定!');

            //如果为喷漆，则同时删除喷漆、跟底子
            if(8 === $gid){
                $f = FixCarItemT::find()->where('fk_id='.$fix_car_item->fk_id.' AND fix_group=13')->one();
                if(!$f->delete()) throw new Exception('删除失败!');
            }
            if (!$fix_car_item->delete()) throw new Exception('删除失败!');
            return Message::getMessage(true,'操作成功!');
        } catch (Exception $e) {
            $result['msg'] = $e->getMessage();
            return Message::getMessage(false,$e->getMessage());
        }
    }

    public function actionPayTypeChange()
    {
        $result = [];
        $result['success'] = false;
        $result['msg'] = '操作失败!';

        $request = Yii::$app->request;
        $id = $request->post('id');
        $pay_type = $request->post('pay_type');

        if (!$request->isAjax || !$id || !$pay_type) return $result;

        try {
            $item = FixCarItemT::findOne($id);
            $item->pay_type = $pay_type;
            if (!$item->save()) throw new Exception('操作失败!');
            $result['success'] = true;
            $result['msg'] = '操作成功!';
        } catch (Exception $e) {
            $result['msg'] = $e->getMessage();
        }
        return $result;
    }

    /**
     *------------------------------------------------------------
     *  维修清单池
     * @param   int type 1、钣金；2、底子工；3、喷漆工；
     * @return  view
     * @author liuyingjie
     *------------------------------------------------------------
     */
    public function actionItemsList()
    {
        $request = Yii::$app->request;

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

        $result = [];
        $result['success'] = false;
        $result['msg'] = '操作失败!';

        switch ($type) {
            case self::METAL_PLATE:
                $redirect = '/business-order/metal-plate-list';
                break;
            case self::BOTTOM_WORK: //底子工
                $redirect = '/business-order/bottom-work-list';
                break;
            case self::PAINT_WORK: //喷漆工
                $redirect = '/business-order/paint-work-list';
                break;
            case self::CAR_WASH:
                $redirect = '/business-order/car-wash-list';
                break;
            case self::ELECTROMECHANICAL:
                $redirect = '/business-order/electromechanical-list';
                break;
        }
        if (!$request->isGet || '' == $type) return $this->redirect($redirect);

//        $query = FixCarT::find()->joinWith('items');
        $query = MiddleFixCarT::find()->joinWith('items');
        switch ($type) {
            case self::METAL_PLATE:
                //钣金工
                //lock_id 等于 0 并且 fix_group 等于 7(钣金项目) 并且 type 等于 2(维修清单) 并且 fix_car_t.status = 6(钣金)
                $query = $query->andWhere('middle_fix_car_t.type=1')
                    ->andWhere('fix_car_item_t.lock_id = 0 AND fix_car_item_t.fix_group = 7 AND fix_car_item_t.type = 2 AND middle_fix_car_t.status = 6 AND fix_car_item_t.fix_status=0 AND fix_car_item_t.status=0');
                $view = 'items-list';
                break;
            case self::BOTTOM_WORK: //底子工
                $query = $query->andWhere('middle_fix_car_t.type=1')
                    ->andWhere('fix_car_item_t.lock_id = 0 AND fix_car_item_t.fix_group = 13 AND fix_car_item_t.type = 2 AND middle_fix_car_t.status = 7 AND fix_car_item_t.fix_status=0 AND fix_car_item_t.status=0');
                $view = 'items-list';
                break;
            case self::PAINT_WORK:  //喷漆工
                $query = $query->andWhere('middle_fix_car_t.type=1')
                    ->andWhere('fix_car_item_t.lock_id = 0 AND fix_car_item_t.fix_group = 14 AND fix_car_item_t.type = 2 AND middle_fix_car_t.status = 8 AND fix_car_item_t.fix_status=0 AND fix_car_item_t.status=0');
                $view = 'items-list';
                break;
            case self::CAR_WASH:
                $query = $query->andWhere('fix_car_item_t.lock_id = 0 AND fix_car_item_t.fix_group = 14 AND fix_car_item_t.type = 2 AND fix_car_t.status = 8 AND fix_car_item_t.fix_status=0 AND fix_car_item_t.status=0');
                $view = 'items-list';
                break;
            case self::ELECTROMECHANICAL:
                $query = $query->andWhere('middle_fix_car_t.type=2')
                    ->andWhere('fix_car_item_t.lock_id = 0 AND fix_car_item_t.fix_group BETWEEN 10 AND 11 AND middle_fix_car_t.status = 11 AND fix_car_item_t.fix_status=0 AND fix_car_item_t.status=0');
                $view = 'items-list';
                break;
        }

        //辅料清单
        $items = $query->all();
        if (count($items) == 0) return $this->redirect($redirect);

        return $this->render($view, ['items' => $items, 'type_name' => $type, 'redirect' => $redirect]);
    }

    /**
     *------------------------------------------------------------
     *  清单详情
     * @param void
     * @return void
     * @author liuyingjie
     *------------------------------------------------------------
     */
    public function actionItemListInfo()
    {
        $request = Yii::$app->request;

        $fix_id = $request->get('fix_id', 0);
        $type_name = $request->get('type_name', '');
        $redirect = $request->referrer;

        $result = [];
        $result['success'] = false;
        $result['msg'] = '操作失败!';

        $filter = [self::METAL_PLATE, self::BOTTOM_WORK, self::PAINT_WORK, self::METAL_PLATE_INSTALL, self::CHASING_WORK, self::ELECTROMECHANICAL, self::FACIAL, self::CAR_WASH];
        if (!$request->isGet || $fix_id <= 0 || !in_array($type_name, $filter)) return $this->redirect($redirect);

        $query = FixCarItemT::find();
        switch ($type_name) {
            case self::METAL_PLATE:
                //钣金工
                //lock_id 等于 0 并且 fix_group 等于 7(钣金项目) 并且 type 等于 2(维修清单) 并且 fix_car_t.status = 6(钣金)
                $query = $query->andWhere('lock_id=0 AND fix_group=7 AND type=2 AND fix_id=' . $fix_id);
                $view = 'item-list-info';
                break;
            case self::BOTTOM_WORK: //底子工
                $query = $query->andWhere('lock_id=0 AND fix_group=13 AND type=2 AND fix_id=' . $fix_id);
                $view = 'item-list-info';
                break;
            case self::PAINT_WORK:
                $query = $query->andWhere('lock_id=0 AND fix_group=14 AND type=2 AND fix_id=' . $fix_id);
                $view = 'item-list-info';
                break;
            case self::ELECTROMECHANICAL:
                $query = $query->andWhere('lock_id=0 AND fix_group BETWEEN 10 and 11 AND fix_id=' . $fix_id);
                $view = 'electromechanical-list-info';
                break;
        }

        $lists = $query->all();
        return $this->render($view, ['lists' => $lists, 'redirect' => $redirect]);
    }

    /**
     *------------------------------------------------------------
     *  领取清单操作
     * @param void
     * @return  json
     * @author liuyingjie
     *------------------------------------------------------------
     */
    public function actionGetItem()
    {
        $request = Yii::$app->request;

        $item_id = $request->post('item_id', 0);

        $result = [];
        $result['success'] = false;
        $result['msg'] = '操作失败!';

        if (!$request->isAjax || $item_id <= 0) return $result;

        try {
            $item = FixCarItemT::findOne($item_id);
            $item->fix_user = $item->lock_id = $this->my->id;
            $item->get_time = date('Y-m-d H:i:s');
            if (!$item->save()) throw new Exception('更新失败!');

            $result['success'] = true;
            $result['msg'] = '操作成功!';
        } catch (Exception $e) {
            $result['msg'] = $e->getMessage();
        }
        return $result;
    }

    public function actionBatchGetItem()
    {
        $request = Yii::$app->request;

        $item_id = $request->post('item_id', []);

        $result = [];
        $result['success'] = false;
        $result['msg'] = '操作失败!';

        if (!$request->isAjax || empty($item_id)) return $result;

        $item_id = array_shift($item_id);
        foreach ($item_id as $id) {
            $item = FixCarItemT::findOne($id);
            $item->fix_user = $item->lock_id = $this->my->id;
            $item->save();
        }
    }

    /**
     *------------------------------------------------------------
     *  确认维修
     * @param
     *  int item_id fix_car_item_t 表主键ID
     *  int type 1,维修清单确认;2,辅料清单使用;3;钣金安装;4,总检确认
     * @return  json
     * @author liuyingjie
     *------------------------------------------------------------
     */
    public function actionConfirmFix()
    {
        $request = Yii::$app->request;

        $item_id = $request->post('item_id', 0);
        $type = $request->post('type', 0);

        if (!$request->isAjax || $item_id <= 0 || $type <= 0) return Message::getMessage();

        try {
            $item = FixCarItemT::findOne($item_id);
            //只有更换清单跟辅料清单可以领取
            if(in_array($item->type,[1,3]) && '0000-00-00 00:00:00' === $item->get_time){
                $result['msg'] = '未领取，不可确认!';
                return $result;
            }

            $item->fix_status = 1;
            if (3 == $type) {
                $item->metal_plate_install = 1;
            }
            if (4 == $type) {
                $item->fix_status = 2;
            }
            $item->confirm_at = date('Y-m-d H:i:s');

            //辅料数量减1
            if (2 == $type) {
                if (0 == $item->count) {
                    $result['msg'] = '数量不足!';
                    return $result;
                }
                $item->count -= 1;
                $result['content'] = $item->count;
            }

            if (!$item->save()) throw new Exception('操作失败!');

            return Message::getMessage(true,'操作成功!');

        } catch (Exception $e) {
            return Message::getMessage(false,$e->getMessage());
        }
    }

    public function actionBatchConfirmFix()
    {
        $request = Yii::$app->request;

        $item_ids = $request->post('item_id', '');
        $type = $request->post('type', 0);
        $result = [];
        $result['success'] = false;
        $result['msg'] = '操作失败!';

        if (!$request->isAjax || empty($item_ids) || $type <= 0) return $result;
        try {
            foreach ($item_ids as $k => $item_id) {
                $item = FixCarItemT::findOne($item_id);
                $item->fix_status = 1;
                if (3 == $type) {
                    $item->metal_plate_install = 1;
                }
                if (4 == $type) {
                    $item->fix_status = 2;
                }
                $item->confirm_at = date('Y-m-d H:i:s');

                //辅料数量减1
                if (2 == $type) {
                    if (0 == $item->count) {
                        $result['msg'] = '数量不足!';
                        return $result;
                    }
                    $item->count -= 1;
                    $result['content'][$k] = $item->count;
                }

                if (!$item->save()) throw new Exception('操作失败!');
            }

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

        } catch (Exception $e) {
            $result['success'] = false;
            $result['msg'] = '操作失败!';
        }
        return $result;
    }

    /**
     *------------------------------------------------------------
     *  退回
     * @param   int id (fix_car_item_t.id)
     * @param   int gid (fix_car_item_t.fix_group)
     * @return  json
     * @author liuyingjie
     *------------------------------------------------------------
     */
    public function actionReturn(){
        $request = Yii::$app->request;

        $item_id = $request->post('id', 0);
        $gid = $request->post('gid', 0);

        $result = [];
        $result['success'] = false;
        $result['msg'] = '操作失败!';

        if (!$request->isAjax || $item_id <= 0 || $gid <= 0) return $result;

        $tran = FixCarItemT::getDb()->beginTransaction();
        try {
            $item = FixCarItemT::findOne($item_id);
            $item->fix_status = 3;

            switch($gid){
                case 7:
                    $type = 1;
                    $middle_status = 6;
                    $item->metal_plate_install = 0;
                    break;
                case 8:
                    $type = 1;
                    $middle_status =8;
                    break;
                case 12:
                    $middle_status = 13;
                    $type = 1;
                    break;
                case 13:
                    $type = 1;
                    $middle_status = 7;
                    break;
                case 10:
                case 11:
                    $type = 2;
                    $middle_status = 11;
                    break;
            }
            if (!$item->save()) throw new Exception('操作失败!');

            $middle = MiddleFixCarT::findOne(['fix_id'=>$item->fix_id,'status'=>14,'type'=>$type]);
            $middle->status = $middle_status;
            if(!$middle->save()) throw new Exception('');

            $fix_car_t = FixCarT::findOne($item->fix_id);
            $fix_car_t->status = 6;
            if(!$fix_car_t->save()) throw new Exception('');

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

            $tran->commit();
        } catch (Exception $e) {
            $result['success'] = false;
            $result['msg'] = '操作失败!';
            $tran->rollBack();
        }
        return $result;
    }
    /**
     *------------------------------------------------------------
     *  待维修订单数
     * @param   int type
     *      1, 钣金 ; 2,底子 ; 3,喷漆 ; 4,机电;
     * @return  json
     * @author liuyingjie
     *------------------------------------------------------------
     */
    public function actionItem()
    {
        $request = Yii::$app->request;
        $type = $request->post('type');

        $result = [];
        $result['success'] = false;
        $result['msg'] = '操作失败!';

        if (!$request->isAjax || $type < 1) return $result;

        $query = MiddleFixCarT::find()->joinWith('items', 'LEFT')->joinWith('fix');
        switch ($type) {
            case 1:
                //钣金工
                $query = $query->where('fix_car_item_t.lock_id = 0 AND fix_car_item_t.fix_user = 0 AND fix_car_item_t.fix_group = 7 AND fix_car_item_t.type = 2  AND fix_car_item_t.fix_status=0 AND middle_fix_car_t.status=6 AND middle_fix_car_t.type=1');
                break;
            case 2: //底子工
                $query = $query->where('fix_car_item_t.lock_id = 0 AND fix_car_item_t.fix_user = 0 AND fix_car_item_t.fix_group = 13 AND fix_car_item_t.type = 2  AND fix_car_item_t.fix_status=0 AND middle_fix_car_t.status=7 AND middle_fix_car_t.type=1');
                break;
            case 3:  //喷漆工
                $query = $query->where('fix_car_item_t.lock_id = 0 AND fix_car_item_t.fix_user = 0 AND fix_car_item_t.fix_group = 8 AND fix_car_item_t.type = 2  AND fix_car_item_t.fix_status=0  AND middle_fix_car_t.status=8 AND middle_fix_car_t.type=1');
                break;
            case 4:  //机电
                $query = $query->where('fix_car_item_t.lock_id = 0 AND fix_car_item_t.fix_user = 0 AND fix_car_item_t.fix_group BETWEEN 10 AND 11 AND fix_car_item_t.fix_status=0 AND  middle_fix_car_t.status=11 AND middle_fix_car_t.type=2');
                break;
            case 5: //美容
                $query = $query->where('fix_car_item_t.lock_id = 0 AND fix_car_item_t.fix_user = 0 AND fix_car_item_t.fix_group=12 AND fix_car_item_t.fix_status=0  AND middle_fix_car_t.type=1')->joinWith('fix')->andWhere('fix_car_t.status_1=1 AND fix_car_t.status_2=1');
                break;
        }

        $items = $query->select('fix_car_item_t.id,fix_car_item_t.fix_id')->orderBy('fix_car_t.finish_date ASC')->all();

        $fix_id = [];
        foreach ($items as $item) {
            $fix_id[$item->fix_id][] = $item->id;
        }
        $result['content'] = '待维修订单数:' . count($fix_id) . '&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" class="act_btn" value="领取" onclick="batchGetItem()">';
        $result['data'] = $fix_id;
        return $result;
    }

    public function actionChangeItemReport(){
        $request = Yii::$app->request;

        $id = $request->post('id',0);
        $report_id = $request->post('report_id');

        if(!$request->isAjax || $id < 1 || $report_id < 1) return Message::getMessage();

        $model = FixCarItemT::findOne($id);
        $model->report_id = $report_id;
        $model->save();

        return Message::getMessage(true,'操作成功!');
    }
    //通知库房出库
    public function actionAjaxGetItem(){
        $request = Yii::$app->request;

        $id = $request->post('id',0);
        if($id < 1) return Message::getMessage();

        $tran = FixCarItemT::getDb()->beginTransaction();
        try{
            $model = FixCarItemT::findOne($id);
            $model->is_get = 1;
            if(!$model->save()) throw new Exception('a');

            $mode2 = FixItemRecord::findOne(['item_id'=>$id]);
            $mode2->status = 1;
            $mode2->get_user = $this->my->id;
            if(!$mode2->save()) throw new Exception('b');

            $tran->commit();
            return Message::getMessage(true,'操作成功!');
        }catch(Exception $e){
            $tran->rollBack();
            return Message::getMessage();
        }
    }
}
