You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
simple-yewu/frontend/controllers/FixCarProjectController.php

130 lines
5.0 KiB

<?php
/**
* Created by PhpStorm.
* User: liuyingjie
* Date: 2017/8/30
* Time: 11:20
*/
namespace frontend\controllers;
use common\libs\MyLib;
use common\models\FixCarBeautify;
use common\models\FixCarJidian;
use common\models\FixCarMetal;
use common\models\FixCarPrice;
use common\models\FixCarProject;
use common\models\FixMetalLevel;
use common\models\Message;
use Yii;
use common\models\FixCarCategory;
use common\models\FixCarGroup;
use yii\data\Pagination;
use yii\db\Exception;
use yii\helpers\ArrayHelper;
use yii\web\Response;
class FixCarProjectController extends BaseController
{
public function actionIndex()
{
$request = Yii::$app->request;
$session = Yii::$app->session;
$render['page'] = $page = $request->get('page', 0);
$render['name'] = $name = $request->get('name', '');
$render['car_price'] = $car_price = $request->get('car_price', 0);
$render['type'] = $type = $request->get('type', 0);
$render['groups'] = FixCarGroup::find()->all();
$query = FixCarProject::find();
if('' !== $name) $query = $query->andWhere(['like','name',$name]);
if($type > 0) $query = $query->andWhere('type='.$type);
$sql = $query->createCommand()->rawSql;
$session->remove('fix-car-project-index');
$session->set('fix-car-project-index', $sql);
$total = $query->count();
$pagination = new Pagination(['totalCount' => $total, 'pageSize' => 20]);
$pagination->setPage($page - 1);
$query = $query->offset($pagination->offset)->limit($pagination->limit);
$render['lists'] = $project = $query->all();
$render['page_info'] = MyLib::getPageInfo($pagination);
return $this->render('index', $render);
}
public function actionInfo()
{
$request = Yii::$app->request;
//保存、编辑
if ($request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
try{
$id = intval($request->post('id',0));
$type = intval($request->post('type',0));
$price = $request->post('price',[]);
$fix_car_project = $id > 0 ? FixCarProject::findOne($id) : new FixCarProject();
$fix_car_project->scenario = 'default';
$fix_car_project->attributes = $request->post();
if(!$fix_car_project->validate()){
$errors = array_values($fix_car_project->errors);
return Message::getMessage(false,$errors[0]);
}
$fix_car_project->save();
$project_id = $id >0 ? $id :Yii::$app->db->getLastInsertID();
//机电、喷漆
if(!in_array($type,[12,7])){
unset($data);
if($id > 0) FixCarJidian::deleteAll(['project_id'=>$id]);
foreach($price as $key=>$val){
$data[] = [
'id' => $key.$project_id,
'price'=> $val,
'project_id'=>$project_id,
'car_price_id'=>$key
];
}
if(!Yii::$app->db->createCommand()->batchInsert(FixCarJidian::tableName(),['id','price','project_id','car_price_id'],$data)->execute()) throw new Exception('操作失败a!');
//钣金
}elseif(7 === $type){
unset($data);
if($id > 0) FixCarMetal::deleteAll(['project_id'=>$id]);
foreach($price as $key=>$val){
$data[] = [
'id' => $key.$project_id,
'price'=> $val,
'project_id'=>$project_id,
'level_id'=>$key
];
}
if(!Yii::$app->db->createCommand()->batchInsert(FixCarMetal::tableName(),['id','price','project_id','level_id'],$data)->execute()) throw new Exception('操作失败b!');
//美容
}elseif(12 === $type){
$beautify = $id > 0 ? FixCarBeautify::findOne($id) : new FixCarBeautify();
$beautify->price = $price;
$beautify->project_id = $project_id;
if(!$beautify->save()) throw new Exception('操作失败c!');
}
return Message::getMessage(true,'操作成功!');
}catch(Exception $e){
return Message::getMessage(false,$e->getMessage());
}
}
//分组
$render['groups'] = FixCarGroup::find()->all();
$render['levels'] = FixMetalLevel::find()->all();
$render['car_prices'] = array_chunk(FixCarPrice::find()->asArray()->all(),10);
//详情
$id = $request->get('id', 0);
if ($id > 0) {
$render['info'] = $info = FixCarProject::findOne($id);
}
return $this->render('info', $render);
}
}