Files
liche/admin/controllers/auto/Series.php
T
2021-08-05 15:28:19 +08:00

146 lines
4.8 KiB
PHP

<?php
/**
* Created by Vim
* User: lcc
* Date: 2020/08/05
* Time: 10:19
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Series extends HD_Controller{
public function __construct(){
parent::__construct();
$this->load->model('auto/auto_brand_model');
$this->load->model('auto/auto_series_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;
$count = $this->auto_series_model->count($where);
$rows = $this->auto_series_model->select($where, 'id desc', $page, $size);
$status_arr = $this->auto_series_model->get_status();
$list = [];
if($rows){
$brand_arr = array_column($rows,'brand_id');
$brand_rows = $this->auto_brand_model->get_map_by_ids($brand_arr,'id,name');
foreach($rows as $key=>$val){
$brand_name = $brand_rows[$val['brand_id']] ? $brand_rows[$val['brand_id']][0]['name'] : '';
$list[] = [
'id' => $val['id'],
'brand_name' => $brand_name,
'name' => $val['name'],
'status' => $val['status'],
'status_name' => $status_arr[$val['status']],
'c_time' => date('Y-m-d H:i:s',$val['c_time'])
];
}
}
$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/series/lists', true);
}
public function get(){
$id = $this->input->get('id');
$info = [];
if ($id) {
$info = $this->auto_series_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/series/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_series_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_series_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_series_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_series_model->update(array('status' => $stauts), $where);
return $this->show_json(SYS_CODE_SUCCESS, '操作成功');
}
public function batch(){
}
public function export(){
}
}