Files
liche/admin/controllers/auto/Attr.php
T
2022-03-24 22:29:48 +08:00

209 lines
7.0 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 Attr 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_attr_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["title like '%{$params['title']}%'"] = null;
if($params['s_id']){
$where['s_id'] = $params['s_id'];
}if($params['brand_id']){
$where_sery = array('status > -1' => null, 'brand_id' => $params['brand_id']);
$rows_sery = $this->auto_series_model->select($where_sery, 'id desc', 0, 0, 'id');
if($rows_sery){
$sery_ids = array_column($rows_sery, 'id');
$str_ids = implode(',', $sery_ids);
$where["s_id in ({$str_ids})"] = null;
} else {
$where['s_id'] = -1;
}
}
!$params['s_id'] && $params['s_id'] = '';
!$params['brand_id'] && $params['brand_id'] = '';
$count = $this->auto_attr_model->count($where);
$rows = $this->auto_attr_model->select($where, 'id desc', $page, $size);
$type_arr = $this->auto_attr_model->get_type();
$status_arr = $this->auto_brand_model->get_status();
//获取品牌map
$where_brand = array('status > -1' => null);
$map_brand = $this->auto_brand_model->map('id', 'name', $where_brand, 'id desc', 0 , 0, 'id, name');
$list = [];
if($rows){
$series_arr = array_column($rows,'s_id');
$series_rows = $this->auto_series_model->get_map_by_ids($series_arr,'id,name');
foreach($rows as $key=>$val){
$list[] = [
'id' => $val['id'],
'title' => $val['title'],
's_name' => $series_rows[$val['s_id']] ? $series_rows[$val['s_id']][0]['name'] : '',
'status_name' => $status_arr[$val['status']],
'type_cn' => $type_arr[$val['type']],
'c_time' => date('Y-m-d H:i:s',$val['c_time'])
];
}
}
$this->data['lists'] = $list;
$this->data['params'] = $params;
$this->data['brandAry'] = $map_brand;
$this->data['pager'] = array('count' => ceil($count / $size), 'curr' => $page, 'totle' => $count);
$this->data['_title'] = '属性管理';
$this->show_view('auto/attr/lists', true);
}
public function get(){
$id = $this->input->get('id');
$info = [
'title' => ''
];
if ($id) {
$info = $this->auto_attr_model->get(array('id' => $id));
if (!$info || empty($info)) {
return $this->show_json(SYS_CODE_FAIL, '数据不存在!');
}
$info['jsondata'] = json_decode($info['jsondata'],true);
$info['jsondata']['img'] && $info['jsondata']['s_img'] = build_qiniu_image_url($info['jsondata']['img']);
}
$brand_id= '';
if($info['s_id']){
$row_sery = $this->auto_series_model->get(array('id' => $info['s_id']));
$brand_id = $row_sery['brand_id'];
}
$type_arr = $this->auto_attr_model->get_type();
!$info['type'] && $info['type'] = 0;
$info['brand_id'] = $brand_id;
!$info['s_id'] && $info['s_id'] = '';
!$info['jsondata']['img'] && $info['jsondata']['img'] = '';
$this->data['type_arr'] = $type_arr;
$this->data['info'] = $info;
$this->data['_title'] = $id ? '编辑' : '新增';
return $this->show_view('auto/attr/edit',true);
}
public function add(){
if (!$this->if_ajax) {
return $this->show_json(SYS_CODE_FAIL, '提交出错!');
}
$post = $this->input->post();
if (!$post['title']) {
return $this->show_json(SYS_CODE_FAIL, '标题不能为空');
}
$add_data = [
'title' => $post['title'],
's_id' => $post['s_id'],
'type' => $post['type'],
'c_time' => time()
];
$post['jsondata'] && $add_data['jsondata'] = json_encode($post['jsondata'],JSON_UNESCAPED_UNICODE);
$result = $this->auto_attr_model->add($add_data);
if (!$result) {
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, '提交出错!');
}
$post = $this->input->post();
$row = $this->auto_attr_model->get(['id'=>$post['id']]);
if(!$row){
return $this->show_json(SYS_CODE_FAIL, '数据不存在');
}
if (!$post['title']) {
return $this->show_json(SYS_CODE_FAIL, '标题不能为空');
}
$update = [
'title' => $post['title'],
's_id' => $post['s_id'],
'type' => $post['type'],
];
$post['jsondata'] && $update['jsondata'] = json_encode($post['jsondata'],JSON_UNESCAPED_UNICODE);
$result = $this->auto_attr_model->update($update,['id'=>$row['id']]);
if (!$result) {
return $this->show_json(SYS_CODE_FAIL, '保存失败');
}
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
}
public function del(){
}
public function batch(){
}
public function export(){
}
/**
* 属性选项列表
* @return bool
*/
function json_lists(){
$s_id = $this->input->get_post('s_id');
$status = $this->input->get_post('status');
$type = $this->input->get_post('type');
$page = $this->input->get_post('page');
$size = $this->input->get_post('size');
$where = array();
$s_id && $where['s_id'] = $s_id;
strlen($type) > 0 && $where['type'] = $type;
if(strlen($status) > 0){
$where['status'] = $status;
} else {
$where['status>-1'] = null;
}
$total = $this->auto_attr_model->count($where);
$lists = array();
if($total){
$orderby = 'id desc';
$select = 'id, title';
$rows = $this->auto_attr_model->select($where, $orderby, $page, $size, $select);
foreach($rows as $v){
$lists[] = array(
'id' => $v['id'],
'title' => $v['title'],
);
}
}
$this->data = array('total' => $total, 'list' => $lists);
return $this->show_json(SYS_CODE_SUCCESS);
}
}