Files
liche/admin/controllers/auto/Cars.php
T

316 lines
12 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: xuxb
* Date: 2021/8/6
* Time: 11:15
*/
class Cars extends HD_Controller{
protected $log_dir;
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');
$this->load->model('auto/auto_cars_model');
$this->log_dir = "auto_" . get_class($this);
}
public function index(){
return $this->lists();
}
public function lists(){
$params = $this->input->get();
$where = array();
if($params['brand_id']){
$where['brand_id'] = $params['brand_id'];
} else{
$params['brand_id'] = '';
}
if($params['s_id']){
$where['s_id'] = $params['s_id'];
} else{
$params['s_id'] = '';
}
if(strlen($params['status']) > 0){
$where['status'] = $params['status'];
} else {
$params['status'] = '';
}
$page = $params['page'];
$page = !$page ? 1 : $page;
$size = $params['size'];
$size = !$size ? 20 : $size;
//获取品牌map
$where_brand = array('status > -1' => null);
$map_brand = $this->auto_brand_model->map('id', 'name', $where_brand, 'id desc', 0 , 0, 'id, name');
//状态
$statusAry = array('0' => '关闭', '1' => '开启');
$total = $this->auto_cars_model->count($where);
$lists = array();
if($total){
$orderby = 'id desc';
$select = '*';
$rows = $this->auto_cars_model->select($where, $orderby, $page, $size, $select);
if($rows){
$s_ids = array();
$attr_ids = array();
foreach($rows as $v){
!in_array($v['s_id'], $s_ids) && $s_ids[] = $v['s_id'];
$ids = explode('_', $v['attrs']);
$attr_ids = array_merge($attr_ids, $ids);
}
$attr_ids = array_unique($attr_ids);
//获取车系列表
$map_sery = array();
if($s_ids){
$str_ids = implode(',', $s_ids);
$where_sery = array("id in ({$str_ids})" => null);
$map_sery = $this->auto_series_model->map('id', 'name', $where_sery, 'id desc', 0, 0, 'id,name');
}
$map_attr = array();
if($attr_ids){
$str_ids = implode(',', $attr_ids);
$where_attr = array("id in ({$str_ids})" => null);
$map_attr = $this->auto_attr_model->map('id', '*', $where_attr, 'id desc', 0 , 0, 'id, title, type');
}
//属性按'车型-车身颜色-内饰颜色'排序
foreach($rows as $v){
$attr_ids = explode('_', $v['attrs']);
$arr = array();
foreach($attr_ids as $attr_id){
$attr = $map_attr[$attr_id];
$arr[$attr['type']] =$attr['title'];
}
$attr_title = "{$arr[1]}-{$arr[0]}-内饰{$arr[2]}";
$title = "{$map_brand[$v['brand_id']]} {$map_sery[$v['s_id']]} {$attr_title}";
$lists[] = array(
'id' => $v['id'],
'title' => $title,
'status' => $v['status'],
'price_car' => $v['price_car'] > 0 ? $v['price_car'] : '0.00',
'price_insure' => $v['price_insure'] > 0 ? $v['price_insure'] : '0.00',
'price_fine' => $v['price_fine'] > 0 ? $v['price_fine'] : '0.00',
'price_finance' => $v['price_finance'] > 0 ? $v['price_finance'] : '0.00',
'first_pay' => $v['first_pay'] > 0 ? $v['first_pay'] : '0.00',
'month_pay' => $v['month_pay'] > 0 ? $v['month_pay'] : '0.00',
'price_coplus' => $v['price_coplus'] > 0 ? $v['price_coplus'] : '0.00',
'brokerage_1' => $v['brokerage_1'] > 0 ? $v['brokerage_1'] : '0.00',
'brokerage_2' => $v['brokerage_2'] > 0 ? $v['brokerage_2'] : '0.00',
);
}
}
}
$this->data['params'] = $params;
$this->data['lists'] = $lists;
$this->data['statusAry'] = $statusAry;
$this->data['brandAry'] = $map_brand;
$this->data['pager'] = array('count' => ceil($total / $size), 'curr' => $page, 'totle' => $total);
$this->data['_title'] = '车型库管理';
$this->show_view('auto/cars/lists',true);
}
public function get(){
$id = $this->input->get('id');
//获取品牌map
$where_brand = array('status > -1' => null);
$map_brand = $this->auto_brand_model->map('id', 'name', $where_brand, 'id desc', 0 , 0, 'id, name');
//状态
$statusAry = array('0' => '关闭', '1' => '开启');
if($id){
$row = $this->auto_cars_model->get(array('id' => $id));
//获取属性列表
$attr_ids = explode('_', $row['attrs']);
$str_ids = implode(',', $attr_ids);
$where_attr = array("id in ({$str_ids})" => null);
$map_attr = $this->auto_attr_model->map('type', 'title', $where_attr, 'id desc', 0 , 0, 'type, title');
$attr = "{$map_attr[1]}-{$map_attr[0]}-{$map_attr[2]}(内饰)";
//车系
$row_sery = $this->auto_series_model->get(array('id' => $row['s_id']));
$info = array(
'id' => $row['id'],
'brand_name' => $map_brand[$row['brand_id']],
'sery_name' => $row_sery['name'],
'attr' => $attr,
'price_car' => $row['price_car'] > 0 ? $row['price_car'] : '',
'price_insure' => $row['price_insure'] > 0 ? $row['price_insure'] : '',
'price_fine' => $row['price_fine'] > 0 ? $row['price_fine'] : '',
'price_finance' => $row['price_finance'] > 0 ? $row['price_finance'] : '',
'first_pay' => $row['first_pay'] > 0 ? $row['first_pay'] : '',
'month_pay' => $row['month_pay'] > 0 ? $row['month_pay'] : '0.00',
'price_coplus' => $row['price_coplus'] > 0 ? $row['price_coplus'] : '',
'brokerage_1' => $row['brokerage_1'] > 0 ? $row['brokerage_1'] : '',
'brokerage_2' => $row['brokerage_2'] > 0 ? $row['brokerage_2'] : '',
'status' => $row['status'],
);
$title = '编辑车型库';
$view = 'auto/cars/get';
} else {
//新增车型库
$info = array('brand_id' => '', 's_id' => '');
$title = '新增车型库';
$view = 'auto/cars/add';
}
$this->data['info'] = $info;
$this->data['statusAry'] = $statusAry;
$this->data['brandAry'] = $map_brand;
$this->data['_title'] = $title;
$this->show_view($view);
}
public function add(){
$info = $this->input->post('info');
$brand_id = $info['brand_id'];
$s_id = $info['s_id'];
$where = array('s_id' => $s_id);
$orderby = 'type asc, id asc';
$select = 'id,type';
$map = $this->auto_attr_model->map('type', '', $where, $orderby, 0, 0, $select);
$count = count($map);
if(!$count){
return $this->show_json(SYS_CODE_FAIL, '该车系暂无属性!');
}
$attrs = array_column($map[0], 'id');//属性组合
for($i=1; $i<$count; $i++){
$arr1 = $attrs;
$arr2 = $map[$i];
$attrs = array();
foreach($arr1 as $k1 => $v1){
foreach($arr2 as $k2 => $v2){
$attrs[] = "{$v1}_{$v2['id']}";
}
}
}
//车型库现有数据
$where = array('s_id' => $s_id);
$map_cars = $this->auto_cars_model->map('attrs', '*', $where);
$adds = array();
foreach($attrs as $attr){
if($map_cars[$attr]){
$map_cars[$attr]['ok'] = 1;//保留
} else {
$adds[] = array(
'brand_id' => $brand_id,
's_id' => $s_id,
'attrs' => $attr,
'status' => 1,
'c_time' => time()
);
}
}
//获取需要删除的车型库
$del_ids = array();
foreach($map_cars as $v){
if(!$v['ok']){
$del_ids[] = $v['id'];
}
}
//删除旧库
if($del_ids){
$str_ids = implode(',', $del_ids);
$where = array("id in ({$str_ids})" => null);
$ret = $this->auto_cars_model->delete($where);
if(!$ret){
debug_log("[error]#" . $this->auto_cars_model->db->last_query(), __FUNCTION__, $this->log_dir);
return $this->show_json(SYS_CODE_FAIL, '更新失败!');
}
}
//新增库
if($adds){
$ret = $this->auto_cars_model->add_batch($adds);
if(!$ret){
debug_log("[error]#" . $this->auto_cars_model->db->last_query(), __FUNCTION__, $this->log_dir);
return $this->show_json(SYS_CODE_FAIL, '更新失败!');
}
}
return $this->show_json(SYS_CODE_SUCCESS, '更新成功!');
}
public function edit(){
$info = $this->input->post('info');
$upd = array(
'price_car' => floatval($info['price_car']),
'price_insure' => floatval($info['price_insure']),
'price_fine' => floatval($info['price_fine']),
'price_finance' => floatval($info['price_finance']),
'first_pay' => floatval($info['first_pay']),
'month_pay' => floatval($info['month_pay']),
'price_coplus' => floatval($info['price_coplus']),
'brokerage_1' => floatval($info['brokerage_1']),
'brokerage_2' => floatval($info['brokerage_2']),
);
$where = array('id' => $info['id']);
$ret = $this->auto_cars_model->update($upd, $where);
if(!$ret){
debug_log("[error]#" . $this->auto_cars_model->db->last_query(), __FUNCTION__, $this->log_dir);
return $this->show_json(SYS_CODE_FAIL, '更新失败!');
}
return $this->show_json(SYS_CODE_SUCCESS, '更新成功!');
}
function edit_status(){
$id = $this->input->post('id');
$field = $this->input->post('field');
$value = $this->input->post('value');
$status = $this->input->post('status');
if('status' == $field){
$status = $value;
}
$upd = array('status' => $status);
if(is_numeric($id)){
$where = array('id' => $id);
} else {
$where = array("id in ({$id})" => null);
}
$ret = $this->auto_cars_model->update($upd, $where);
if(!$ret){
debug_log("[error]# " . $this->auto_cars_model->db->last_query(), __FUNCTION__, $this->log_dir);
return $this->show_json(SYS_CODE_FAIL, '保存失败');
}
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
}
public function del(){
// TODO: Implement del() method.
}
public function batch(){
// TODO: Implement batch() method.
}
public function export(){
// TODO: Implement export() method.
}
}