Files
liche/admin/controllers/auto/Cars.php
T
2022-03-24 23:40:06 +08:00

381 lines
14 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($params['v_id']){
$where['v_id'] = $params['v_id'];
} else{
$params['V_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_unique(array_column($rows,'s_id'));
$attr_ids = array_unique(array_column($rows,'v_id'));
//获取车系列表
$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', 'title', $where_attr, 'id desc', 0 , 0, 'id, title, type');
}
//属性按'车型-车身颜色-内饰颜色'排序
foreach($rows as $v){
$title = "{$map_brand[$v['brand_id']]} {$map_sery[$v['s_id']]} {$map_attr[$v['v_id']]}";
$lists[] = array(
'id' => $v['id'],
'title' => $title,
'status' => $v['status'],
'price_car' => $v['price_car'] > 0 ? $v['price_car'] : '0.00',
'price_floor' => $v['price_floor'] > 0 ? $v['price_floor'] : '0.00',
'price_book' => $v['price_book'] > 0 ? $v['price_book'] : '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_fine_floor' => $v['price_fine_floor'] > 0 ? $v['price_fine_floor'] : '0.00',
'price_finance' => $v['price_finance'] > 0 ? $v['price_finance'] : '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 = array();
$row['v_id'] && $attr_ids[] = $row['v_id'];
$row['cor_id'] && $attr_ids[] = $row['cor_id'];
$row['incor_id'] && $attr_ids[] = $row['incor_id'];
$str_ids = implode(',', $attr_ids);
$where_attr = array("id in ({$str_ids})" => null);
$map_attr = $this->auto_attr_model->map('id', 'title', $where_attr, 'id desc', 0 , 0, 'id, title');
$attr = "{$map_attr[$row['v_id']]}-{$map_attr[$row['cor_id']]}-{$map_attr[$row['incor_id']]}(内饰)";
//车系
$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_floor' => $row['price_floor'] > 0 ? $row['price_floor'] : '',
'price_book' => $row['price_book'] > 0 ? $row['price_book'] : '',
'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'] : '',
'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,'type'=>1);
$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();//属性组合 0车身颜色,1车型,2内饰颜色
foreach($map as $i => $arr){
$arr1 = $attrs;
$attrs = array();
foreach($arr as $v){
if($arr1){
foreach($arr1 as $v1){
$v1[$i] = $v['id'];
$attrs[] = $v1;
}
} else {
$attrs[] = array($i => $v['id']);
}
}
}
//车型库现有数据
$where = array('s_id' => $s_id);
$rows_car = $this->auto_cars_model->select($where, 'id desc', 0, 0, 'id, v_id, cor_id, incor_id');
$map_cars = array();
foreach($rows_car as $v){
$k = "{$v['v_id']}";
$map_cars[$k] = $v;
}
$adds = array();
foreach($attrs as $attr){
$v_id = $attr[1] ? $attr[1] : 0;//1-车型
$k = "{$v_id}";
if($map_cars[$k]){
$map_cars[$k]['ok'] = 1;//保留
} else {
$adds[] = array(
'brand_id' => $brand_id,
's_id' => $s_id,
'v_id' => $v_id,
'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_floor' => floatval($info['price_floor']),
'price_book' => floatval($info['price_book']),
'price_insure' => floatval($info['price_insure']),
'price_fine' => floatval($info['price_fine']),
'price_fine_floor' => floatval($info['price_fine_floor']),
'price_finance' => floatval($info['price_finance']),
'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.
}
/**
* 获取车型库信息
* @return bool
*/
function json_get(){
$brand_id = $this->input->get_post('brand_id');
$s_id = $this->input->get_post('s_id');
$v_id = $this->input->get_post('v_id');
$cor_id = $this->input->get_post('cor_id');
$incor_id = $this->input->get_post('incor_id');
$where = array(
'status' => 1,
'brand_id' => $brand_id,
's_id' => $s_id,
'v_id' => $v_id,
);
$row = $this->auto_cars_model->get($where);
// $data = array(
// 'id' => $row['id'],
// 'price_car' => $row['price_car']
// );
$this->data = $row;
return $this->show_json(SYS_CODE_SUCCESS);
}
/**
* 旧数据attrs转存新字段
*/
function json_transfer(){
$where = array('v_id' => 0, 'cor_id' => 0, 'incor_id' => 0);
$total = $this->auto_cars_model->count($where);
$rows = $this->auto_cars_model->select($where, 'id asc', 1, 100, 'id, attrs');
$count = count($rows);
$done = 0;
foreach($rows as $v){
$attr_ids = str_replace('_', ',', $v['attrs']);
if($attr_ids){
$where = array("id in({$attr_ids})" => null);
$map_attr = $this->auto_attr_model->map('type', 'id', $where, 'id desc', 0, 0, 'id, type');
$upd = array();
$map_attr[0] && $upd['cor_id'] = $map_attr[0];
$map_attr[1] && $upd['v_id'] = $map_attr[1];
$map_attr[2] && $upd['incor_id'] = $map_attr[2];
if($upd){
$ret = $this->auto_cars_model->update($upd, array('id' => $v['id']));
$ret && $done++;
}
}
}
exit("total:{$total}; count:{$count}; done:{$done};");
}
}