Files
spacestation/admin/controllers/auto/Brand.php
T
2024-05-26 16:00:30 +08:00

162 lines
5.3 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Brand extends HD_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('auto/auto_brand_model');
$this->load->model('auto/auto_series_model');
$this->load->model('auto/auto_cars_model');
}
public function index()
{
$this->lists();
}
public function lists()
{
$params = $this->input->get();
$page = $this->input->get('page');
!$page && $page = 1;
$size = 20;
$where["status > -1"] = null;
$params['title'] && $where["name like '%{$params['title']}%'"] = null;
if (!strlen($params['status'])) {
$params['status'] = $where['status'] = 1;
} else {
if ($params['status'] != 'all') {
$where['status'] = $params['status'];
}
}
$count = $this->auto_brand_model->count($where);
$rows = $this->auto_brand_model->select($where, 'status desc, id desc', $page, $size);
$status_arr = $this->auto_brand_model->get_status();
$list = [];
if ($rows) {
foreach ($rows as $key => $val) {
$list[] = [
'id' => $val['id'],
'name' => $val['name'],
'status' => $val['status'],
'status_name' => $status_arr[$val['status']],
'c_time' => date('Y-m-d H:i:s', $val['c_time'])
];
}
}
$status_arr_list = $status_arr;
unset($status_arr_list[-1]);
$this->data['status_arr'] = $status_arr_list;
$this->data['lists'] = $list;
$this->data['params'] = $params;
$this->data['pager'] = array('count' => ceil($count / $size), 'curr' => $page, 'totle' => $count);
$this->data['_title'] = '品牌列表';
$this->show_view('auto/brand/lists', true);
}
public function get()
{
$id = $this->input->get('id');
$info = [];
if ($id) {
$info = $this->auto_brand_model->get(array('id' => $id));
if (!$info || empty($info)) {
return $this->show_json(SYS_CODE_FAIL, '数据不存在!');
}
}
$this->data['info'] = $info;
$this->data['_title'] = $id ? '编辑品牌' : '新增品牌';
return $this->show_view('auto/brand/edit');
}
public function add()
{
if (!$this->if_ajax) {
return $this->show_json(SYS_CODE_FAIL, '提交出错!');
}
$id = $this->input->post('id');
$name = $this->input->post('name');
$img = $this->input->post('img');
if (!$name || empty($name)) {
return $this->show_json(SYS_CODE_FAIL, '品牌名称不能为空');
}
$old = $this->auto_brand_model->get(['name' => $name, 'status>' => -1]);
if ($old) {
return $this->show_json(SYS_CODE_FAIL, '品牌已经存在');
}
$add_data = array(
'name' => $name,
'logo' => $img,
'c_time' => time()
);
$brand_id = $this->auto_brand_model->add($add_data);
if (!$brand_id) {
return $this->show_json(SYS_CODE_FAIL, '添加失败');
}
return $this->show_json(SYS_CODE_SUCCESS, '添加成功');
}
public function edit()
{
if (!$this->if_ajax) {
return $this->show_json(SYS_CODE_FAIL, '提交出错!');
}
$id = $this->input->post('id');
$name = $this->input->post('name');
$img = $this->input->post('img');
$row = $this->auto_brand_model->get(['id' => $id]);
if (!$row) {
return $this->show_json(SYS_CODE_FAIL, '数据不存在!');
}
if (!$name) {
return $this->show_json(SYS_CODE_FAIL, '品牌名称不能为空');
}
//防止品牌名称重复
$where = array('name' => $name, "status<>-1" => null);
$id && $where['id <>'] = $id;
$old = $this->auto_brand_model->get($where);
if ($old) {
return $this->show_json(SYS_CODE_FAIL, '品牌已经存在');
}
$up_data = array(
'name' => $name,
);
$img && $up_data['logo'] = $img;
$this->auto_brand_model->update($up_data, array('id' => $id));
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
}
public function del()
{
$id = $this->input->post('id');
if (!$id) {
$this->show_json(SYS_CODE_FAIL, '参数错误');
}
$stauts = $this->input->post('status');
$where = array('id' => $id);
$this->auto_brand_model->update(array('status' => $stauts), $where);
if ($stauts == -1) {
# 车型库
$this->auto_cars_model->update(array('status' => $stauts), array('brand_id' => $id));
# 车系
$this->auto_series_model->update(array('status' => $stauts), array('brand_id' => $id));
}
return $this->show_json(SYS_CODE_SUCCESS, '操作成功');
}
public function batch()
{
}
public function export()
{
}
}