Files
liche/admin/controllers/auto/Brand.php
T
2023-01-30 15:45:38 +08:00

333 lines
11 KiB
PHP

<?php
/**
* Created by Vim
* User: lcc
* Date: 2020/08/04
* Time: 10:19
*/
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_brand_biz_model');
$this->load->model('biz/biz_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_brand_model->count($where);
$rows = $this->auto_brand_model->select($where, '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'],
't_biz' => $this->auto_brand_biz_model->count(['brand_id'=>$val['id']]),
'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/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);
return $this->show_json(SYS_CODE_SUCCESS, '操作成功');
}
public function batch(){
}
public function export(){
}
/**
* 获取可选品牌列表
* @return bool
*/
function json_lists(){
$keyword = trim($this->input->post('keyword'));
$status = $this->input->post('status');
$page = $this->input->post('page');
$size = $this->input->post('size');
$title = trim($this->input->post('title'));
$un_ids = $this->input->post('un_ids');
$where = array();
$keyword && $where['keyword'] = $keyword;
$title && $where["name like '%{$title}%'"] = null;
$un_ids && $where["id not in({$un_ids})"] = null;
if(strlen($status) > 0){
$where['status'] = $status;
} else {
$where['status > -1'] = null;
}
$total = $this->auto_brand_model->count($where);
$lists = array();
if($total){
$orderby = 'id desc';
$select = 'id, name';
$rows = $this->auto_brand_model->select($where, $orderby, $page, $size, $select);
foreach($rows as $v){
$lists[] = array(
'id' => $v['id'],
'name' => $v['name'],
);
}
}
$this->data = array('total' => $total, 'list' => $lists);
return $this->show_json(SYS_CODE_SUCCESS);
}
//授权门店
public function get_biz(){
$id = $this->input->get('id');
$info = $this->auto_brand_model->get(array('id' => $id));
if (!$info) {
return $this->show_json(SYS_CODE_FAIL, '数据不存在!');
}
$cate_lists = $this->biz_model->type_ary();
$params = [
'id' => $id,
'cate_id' => 1
];
$this->data['params'] = $params;
$this->data['cate_lists'] = $cate_lists;
$this->data['info'] = $info;
$this->data['_title'] = '授权品牌';
return $this->show_view('auto/brand/edit_biz',true);
}
//授权门店
public function edit_biz(){
$brand_id = $this->input->post('id');
$type = intval($this->input->post('type'));
$brand_biz = $this->input->post('brand_biz');
$info = $this->auto_brand_model->get(array('id' => $brand_id));
if (!$info) {
return $this->show_json(SYS_CODE_FAIL, '参数错误!');
}
$biz_ids = '';
$brand_biz && $biz_ids = implode(',',array_unique(array_keys($brand_biz)));
if($brand_biz && $biz_ids){
$where = [
"biz_id not in ({$biz_ids})" => null,
'brand_id' => $brand_id
];
$type && $where['type'] = $type;
if($this->auto_brand_biz_model->count($where)){
$this->auto_brand_biz_model->delete($where);
}
$add_datas = [];
foreach ($brand_biz as $key => $val) {
$where = [
'biz_id' => $key,
'brand_id' => $brand_id
];
$type && $where['type'] = $type;
if(!$this->auto_brand_biz_model->count($where)){
if(!$type){
$biz_row = $this->biz_model->get(['id'=>$key],'type');
$type = $biz_row['type'];
}
$add_datas[] = [
'biz_id' => $key,
'type' => $type,
'brand_id' => $brand_id,
'c_time' => time()
];
}
}
$add_datas && $this->auto_brand_biz_model->add_batch($add_datas);
}else{
$where = [
'brand_id' => $brand_id,
];
$type && $where['type'] = $type;
if($this->auto_brand_biz_model->count($where)){
$this->auto_brand_biz_model->delete($where);
}
}
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
}
public function ajax_biz(){
$type = intval($this->input->get('type'));
$brand_id = intval($this->input->get('id'));
if($type){
$where = [
'status' => 1,
'type' => $type,
"id not in (select biz_id from lc_auto_brand_biz where brand_id={$brand_id} and type={$type})" => null
];
}else{
$where = [
'status' => 1,
"id not in (select biz_id from lc_auto_brand_biz where brand_id={$brand_id})" => null
];
}
$biz_lists = $this->biz_model->map('id','biz_name',$where,'',0,0,'id,biz_name');
if($type){
$where = [
'status' => 1,
'type' => $type,
"id in (select biz_id from lc_auto_brand_biz where brand_id={$brand_id} and type={$type})" => null
];
}else{
$where = [
'status' => 1,
"id in (select biz_id from lc_auto_brand_biz where brand_id={$brand_id})" => null
];
}
$brand_biz = $this->biz_model->map('id','biz_name',$where,'',0,0,'id,biz_name');
$this->data['biz_lists'] = $biz_lists;
$this->data['brand_biz'] = $brand_biz;
return $this->show_json(SYS_CODE_SUCCESS);
}
//旧门店授权品牌脚本
public function change(){
$page = $this->input->get('page');
$size = $this->input->get('size');
!$page && $page = 1;
!$size && $size = 20;
$where = [
'status' => 1
];
$rows = $this->biz_model->select($where,'id asc',$page,$size,'id,type,jsondata');
if($rows){
foreach ($rows as $key=>$val) {
$jsondata = json_decode($val['jsondata'],true);
$add_datas = [];
if($jsondata['auto_brands'] && is_array($jsondata['auto_brands'])){
foreach ($jsondata['auto_brands'] as $v) {
$where = [
'biz_id'=>$val['id'],
'type'=>$val['type'],
'brand_id'=>$v
];
if(!$this->auto_brand_biz_model->count($where)){
$add_datas[] = [
'biz_id' => $val['id'],
'type' => $val['type'],
'brand_id' => $v,
'c_time' => time()
];
}
}
$add_datas && $this->auto_brand_biz_model->add_batch($add_datas);
echo "门店id:{$val['id']},添加数据:".json_encode($add_datas,JSON_UNESCAPED_UNICODE).'<br>';
}else{
echo "门店id:{$val['id']},无授权品牌<br>";
}
}
}else{
echo '执行完毕';
}
}
}