249 lines
6.9 KiB
PHP
Executable File
249 lines
6.9 KiB
PHP
Executable File
<?php
|
||
|
||
namespace api\controllers\gm\v1;
|
||
|
||
use Yii;
|
||
use yii\data\Pagination;
|
||
use addons\models\AcCarBrand;
|
||
|
||
class CarbrandController extends Common
|
||
{
|
||
public function actionIndex()
|
||
{
|
||
$apis = [
|
||
'list'=>'品牌列表',
|
||
'detail'=>'详情',
|
||
'add'=>'添加',
|
||
'edit'=>'编辑',
|
||
'delete'=>'删除',
|
||
'show'=>'显示',
|
||
'hide'=>'隐藏',
|
||
'ajax'=>'ajax操作'
|
||
];
|
||
$this->result('您正使用CMTS-GM系统车辆品牌管理接口!', $apis, 200);
|
||
}
|
||
|
||
//车品牌列表
|
||
public function actionList()
|
||
{
|
||
$s = $this->search();
|
||
$res = $s['res'];
|
||
if (!$res) {
|
||
$this->result('没有查询到相应的数据!', [], 0);
|
||
}
|
||
$status = Yii::$app->params['CommonStatus'];
|
||
|
||
$return = [];
|
||
$return['code'] = 200;
|
||
$return['msg'] = '车辆品牌查询成功!';
|
||
$return['data']= [
|
||
'total'=>$s['query']->count(),
|
||
'brands' => $res,
|
||
'page'=>$this->page
|
||
];
|
||
$this->result($return['msg'], $return['data'], $return['code']);
|
||
}
|
||
|
||
//品牌详情
|
||
public function actionDetail()
|
||
{
|
||
$id = (int)Yii::$app->request->get('id');
|
||
if(!$id) $this->result('请求错误,未携带ID参数');
|
||
$model = new AcCarBrand();
|
||
$res = $model->findOne($id)->toArray();
|
||
if(!$res) $this->result('未查询到相应数据');
|
||
$this->result('品牌信息查询完成', $res, 200);
|
||
}
|
||
|
||
//新增车品牌
|
||
public function actionAdd()
|
||
{
|
||
$post = $this->postdata;
|
||
$data = [];
|
||
//格式化数据 为空的项则不修改
|
||
$data['title'] = trim($post['title']);
|
||
if(!$data['title']) $this->result('请传入品牌名称');
|
||
$data['status_code'] = 1; //状态代码:1可用
|
||
$data['create_at'] = time();
|
||
$data['update_at'] = $data['create_at'];
|
||
|
||
//数据预检查
|
||
$res = $this->preSave($data,'add');
|
||
if(!$res) $this->result('数据预检查未通过,保存失败', $data, 100);
|
||
|
||
//保存资料
|
||
$model = new AcCarBrand();
|
||
foreach ($data as $key=>$val) {
|
||
$model->$key = $val;
|
||
}
|
||
$res = $model->save();
|
||
$msg = '数据保存失败!';
|
||
if(!$res) $this->result($msg, [], 100);
|
||
$msg = '数据保存成功!';
|
||
$return = [];
|
||
$return['id']= $model->attributes['id']; //获取插入后id;
|
||
$this->result($msg,$return, 200);
|
||
}
|
||
|
||
//更新车品牌
|
||
public function actionEdit()
|
||
{
|
||
$detail = $this->preUpdate();
|
||
$post = $this->postdata;
|
||
|
||
$data = [];
|
||
//格式化数据
|
||
$data['title'] = trim($post['title']);
|
||
if(!$data['title']) $this->result('请传入品牌名称');
|
||
//目标键=》POST键
|
||
$cols = ['status_code' => 'status_code', 'deleted' => 'deleted'];
|
||
foreach ($cols as $col => $key) {
|
||
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
|
||
}
|
||
$data['update_at'] = time();
|
||
//数据预检查
|
||
$res = $this->preSave($data,'edit');
|
||
if(!$res) $this->result('数据预检查未通过,保存失败', $data, 100);
|
||
//保存资料
|
||
foreach ($data as $key=>$val) {
|
||
$detail->$key = $val;
|
||
}
|
||
$res = $detail->save();
|
||
$msg = '品牌数据更新失败!';
|
||
if(!$res) $this->result($msg, [], 100);
|
||
$msg = '品牌数据更新成功!';
|
||
$return = [];
|
||
$return['data']= $detail;
|
||
$this->result($msg,$return, 200);
|
||
}
|
||
|
||
//删除
|
||
public function actionDelete()
|
||
{
|
||
$detail = $this->preUpdate();
|
||
$data = [];
|
||
$data['update_at'] = time();
|
||
$data['deleted'] = $detail->deleted + 1;
|
||
|
||
//保存资料
|
||
foreach ($data as $key=>$val) {
|
||
$detail->$key = $val;
|
||
}
|
||
$res = $detail->save();
|
||
$msg = '品牌数据删除失败!';
|
||
if(!$res) $this->result($msg, [], 100);
|
||
|
||
$msg = '品牌数据删除成功!';
|
||
$this->result($msg,[], 200);
|
||
}
|
||
|
||
public function actionAjax()
|
||
{
|
||
$detail = $this->preUpdate();
|
||
$get = Yii::$app->request->get();
|
||
$msg = '';
|
||
$errorCode = 0;
|
||
$detail->update_at = time();
|
||
|
||
switch ($get['do']) {
|
||
case 'hide':
|
||
$detail->status_code = 0;
|
||
$res = $detail->save();
|
||
if ($res) {
|
||
$msg = '品牌隐藏标记成功';
|
||
$errorCode = 200;
|
||
} else {
|
||
$msg = '品牌隐藏标记失败';
|
||
}
|
||
break;
|
||
case 'show':
|
||
$detail->status_code = 1;
|
||
$res = $detail->save();
|
||
if ($res) {
|
||
$msg = '品牌显示标记成功';
|
||
$errorCode = 200;
|
||
} else {
|
||
$msg = '品牌显示标记失败';
|
||
}
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
$this->result($msg,[], $errorCode);
|
||
}
|
||
|
||
private function search(){
|
||
$pid = $this->pid;
|
||
$return = [];
|
||
$model = new AcCarBrand();
|
||
$where = $where2 = [];
|
||
$where2[] = 'and';
|
||
// $where['pid'] = $pid;
|
||
$post = $this->postdata;
|
||
$search = $post['search'] ?? [];
|
||
$search['title'] = isset($search['title']) ? trim(htmlspecialchars_decode($search['title'])) : '';
|
||
if (!empty($search['title'])) {
|
||
$where2[] = ['LIKE', 'title', $search['title']];
|
||
}
|
||
$where['deleted'] = 0;
|
||
$data = $model->find()->where($where);
|
||
if ($search['title']) {
|
||
$data = $data->andwhere($where2);
|
||
}
|
||
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
|
||
$res = $data->offset($pages->offset)->limit($pages->limit)->all();
|
||
$brands = [];
|
||
foreach ($res as $s) {
|
||
$s = $s->toArray();
|
||
$brands[$s['id']] = $s;
|
||
}
|
||
return ['res'=>$brands,'query'=>$data];
|
||
}
|
||
|
||
//数据更新前的预检查,返回对应关联数据
|
||
private function preUpdate()
|
||
{
|
||
$id = (int)Yii::$app->request->get('id');
|
||
if(!$id) $this->result('请求错误,未携带ID参数');
|
||
$post = $this->postdata;
|
||
if($post['id'] != $id) $this->result('传参与请求数据不匹配',[],403);
|
||
$model = new AcCarBrand();
|
||
$res = $model->findOne($id);
|
||
if(!$res) $this->result('未查询到相应数据',[],404);
|
||
return $res;
|
||
}
|
||
|
||
/*
|
||
* 数据保存前的预检查(查重、参数校验等)
|
||
* @data,要保存的数据[]
|
||
* @op,操作类型(add,edit……)
|
||
* 校验机制:
|
||
* 编辑数据时,必须用get方式传入参数id,并与post进来的数据id进行比对,只有一致时才能继续;
|
||
* 查重:禁止录入重复数据
|
||
* 返回:校验结果,true,false
|
||
* */
|
||
private function preSave($data,$op)
|
||
{
|
||
$title = trim($data['title']);
|
||
$model = new AcCarBrand();
|
||
switch ($op) {
|
||
case 'add':
|
||
$hasOne = $model->find()->where(['title'=>$title])->one();
|
||
if ($hasOne) {
|
||
$this->result('品牌名称重复,请检查重录');
|
||
}
|
||
break;
|
||
case 'edit':
|
||
$id = (int)$data['id'];
|
||
$origin = $model->findOne($id);
|
||
if(!$origin) $this->result('指定id('.$id.')的数据不存在,请检查');
|
||
$hasOne = $model->find()->select('id')->where(['title'=>$title,'id'<>$id])->indexBy('id')->one();
|
||
if ($hasOne) {
|
||
$this->result('品牌名称重复,请检查重录');
|
||
}
|
||
break;
|
||
}
|
||
return true;
|
||
}
|
||
}
|