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/KeepInGoodController.php

137 lines
4.9 KiB

5 years ago
<?php
/**
* Created by PhpStorm.
* User: liuyingjie
* Date: 2017/8/18
* Time: 9:50
*/
namespace frontend\controllers;
use common\libs\MyLib;
use common\models\CarKeepInGoodBsdc;
use common\models\CarKeepInGoodItems;
use common\models\DisplacementT;
use common\models\SeriesT;
use Yii;
use common\models\BrandT;
use yii\data\Pagination;
use yii\db\Exception;
use yii\helpers\ArrayHelper;
use yii\web\Response;
class KeepInGoodController extends BaseController
{
public function actionIndex()
{
$request = Yii::$app->request;
$session = Yii::$app->session;
$page = $request->get('page');
$query = CarKeepInGoodBsdc::find();
$sql = $query->createCommand()->rawSql;
$total = $query->count();
$session->remove('keep-in-good-index');
$session->set('keep-in-good-index',$sql);
$pagination = new Pagination(['totalCount'=>$total,'pageSize'=>20]);
$pagination->setPage($page-1);
$query = $query->offset($pagination->offset)->limit($pagination->limit);
$bsdcs = $query->all();
$page_info = MyLib::getPageInfo($pagination);
return $this->render('index',[
'bsdcs'=>$bsdcs,
'page_info'=>$page_info,
'page'=>$page
]);
}
public function actionInfo()
{
$request = Yii::$app->request;
//添加
if ($request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
$id = $request->post('id',0);
$result = [];
$result['success'] = false;
$result['msg'] = '操作失败!';
$tran = Yii::$app->db->beginTransaction();
try {
//品牌、车系、排量、年份
$bsdc = $id <= 0 ? new CarKeepInGoodBsdc : CarKeepInGoodBsdc::findOne($id);
$bsdc->scenario = 'create';
$bsdc->attributes = $request->post();
if (!$bsdc->validate()) {
$error = array_values(ArrayHelper::toArray($bsdc->errors));
$result['msg'] = $error[0];
return $result;
}
if (!$bsdc->save()) throw new Exception('失败!');
$level = $request->post('level');
$use = $request->post('use');
$price1 = $request->post('price1');
$hour1 = $request->post('hour1');
$price2 = $request->post('price2');
$hour2 = $request->post('hour2');
$price3 = $request->post('price3');
$hour3 = $request->post('hour3');
$air_price = $request->post('air_price');
$price4 = $request->post('price4');
$hour4 = $request->post('hour4');
$price5 = $request->post('price5');
if($id <= 0) {
$bsdc_id = Yii::$app->db->getLastInsertID();
// if($id > 0 ) CarKeepInGoodItems::deleteAll(['bsdc_id'=>$bsdc_id]);
$create[] = ['bsdc_id' => $bsdc_id, 'level' => $level, 'use' => $use, 'price' => $price1, 'hour' => $hour1, 'air_price' => null, 'type' => 1]; //机油
$create[] = ['bsdc_id' => $bsdc_id, 'level' => null, 'use' => null, 'price' => $price2, 'hour' => $hour2, 'air_price' => null, 'type' => 2]; //机油滤芯
$create[] = ['bsdc_id' => $bsdc_id, 'level' => null, 'use' => null, 'price' => $price3, 'hour' => $hour3, 'air_price' => $air_price, 'type' => 3]; //空气滤芯
$create[] = ['bsdc_id' => $bsdc_id, 'level' => null, 'use' => null, 'price' => $price4, 'hour' => $hour4, 'air_price' => null, 'type' => 4]; //空调滤芯
$create[] = ['bsdc_id' => $bsdc_id, 'level' => null, 'use' => null, 'price' => $price5, 'hour' => null, 'air_price' => null, 'type' => 5]; //汽油滤芯
if (!Yii::$app->db->createCommand()->batchInsert(CarKeepInGoodItems::tableName(), ['bsdc_id', 'level', 'use', 'price', 'hour', 'air_price', 'type'], $create)->execute()) throw new Exception('失败2');
}
$result['success'] = true;
$result['msg'] = '操作成功!';
$tran->commit();
} catch (Exception $e) {
$result['msg'] = $e->getMessage();
}
return $result;
}
$id = $request->get('id');
$info = $series = $displacements = null;
if($in = CarKeepInGoodBsdc::findOne($id)) {
$info = $in;
$series = SeriesT::findAll(['brand_id'=>$info->brand_id]);
$displacements = DisplacementT::findAll(['series_id'=>$info->series_id]);
}
$brands = null;
if ($b = BrandT::find()->all()) $brands = $b;
return $this->render('info', [
'brands' => $brands,
'series' => $series,
'displacements'=>$displacements,
'info'=>$info
]);
}
}