diff --git a/admin/controllers/biz/store/Store.php b/admin/controllers/biz/store/Store.php index bb0461b6..c1fa8129 100755 --- a/admin/controllers/biz/store/Store.php +++ b/admin/controllers/biz/store/Store.php @@ -35,8 +35,9 @@ class Store extends HD_Controller $page = $this->input->get('page'); $status = $this->input->get('status'); - $this->data['province_id'] = $province_id = $this->input->get('province_id', true); + $this->data['province_id'] = $province_id = 350;//默认福建城市 $this->data['city_id'] = $city_id = intval($this->input->get('city_id', true)); + $this->data['county_id'] = $county_id = intval($this->input->get('county_id', true)); $this->data['bizname'] = $bizname = $this->input->get('bizname'); $this->data['brand_id'] = $brand_id = intval($this->input->get('brand_id')); $this->data['content'] = $content = $this->input->get('content'); @@ -44,7 +45,19 @@ class Store extends HD_Controller $status_arr = array('del' => -1, 'off' => 0, 'on' => 1); $this->data['provinces'] = $this->area_model->select(array(), '', 0, 0, 'distinct(province_id), province_name'); if ($province_id) { - $this->data['citys'] = $this->area_model->select(array('province_id' => $province_id), '', 0, 0, 'distinct(city_id), city_name'); + //获取系统配置的城市 + $map_city = $this->city_ary(); + $rows_city = array(); + foreach($map_city as $k => $v){ + $rows_city[] = array( + 'city_id' => $k, + 'city_name' => $v + ); + } + $this->data['citys'] = $rows_city; + } + if($city_id){ + $this->data['countys'] = $this->area_model->select(array('city_id' => $city_id)); } if (isset($status_arr[$status])) { $this->data['status'] = $status; @@ -59,6 +72,7 @@ class Store extends HD_Controller $brand_id && $where['brand_id'] = $brand_id; $province_id && $where['province_id'] = $province_id; $city_id && $where['city_id'] = $city_id; + $county_id && $where['county_id'] = $county_id; $biz_lists = $this->biz_model->select($where, 'id desc', $page, $pagesize); $count = $this->biz_model->count(); @@ -133,6 +147,18 @@ class Store extends HD_Controller } } + $this->data['biz'] = $biz; + } else { + $map_city = $this->city_ary(); + $rows_city = array(); + foreach($map_city as $k => $v){ + $rows_city[] = array( + 'city_id' => $k, + 'city_name' => $v + ); + } + $this->data['citys'] = $rows_city; + $biz = array('province_id' => '350');//默认福建城市 $this->data['biz'] = $biz; } diff --git a/admin/controllers/sys/city/City.php b/admin/controllers/sys/city/City.php index 8e601dc1..0209d70f 100755 --- a/admin/controllers/sys/city/City.php +++ b/admin/controllers/sys/city/City.php @@ -16,6 +16,7 @@ class City extends HD_Controller{ parent::__construct(); $this->load->model("area_model"); $this->load->model("sys/sys_city_model"); + $this->load->model('sys/sys_area_model'); } public function index() { @@ -23,26 +24,72 @@ class City extends HD_Controller{ } public function lists() { - $this->data['bizname'] = $bizname = $this->input->get('bizname'); - if ($bizname){ - $where['name like \'%'.$bizname.'%\''] = null; + $params = $this->input->get(); + $where = array(); + if ($params['keyword']){ + $where["name like '%{$params['keyword']}%'"] = null; } - $page = $this->input->get('page'); + $page = $params['page']; $page = !$page ? 1 : $page; - $size = $this->input->get('size'); + $size = $params['size']; $size = !$size ? 20 : $size; - $lists = $this->sys_city_model->select($where,'id desc',$page,$size); + $count = $this->sys_city_model->count($where); + $lists = array(); + if($count){ + $rows = $this->sys_city_model->select($where,'id desc',$page,$size); + $map_checked = array(); + $city_checked = array(); + if($rows){ + $city_ids = array_column($rows, 'city_id'); + $str_ids = implode(',', $city_ids); + //获取城市下所有的区域 + $where = array("city_id in ({$str_ids})" => null); + $orderby = 'city_id asc, county_id asc'; + $select = 'city_id, county_id, county_name'; + $rows_county = $this->area_model->select($where, $orderby, 0, 0, $select); + //获取已经选择的区域 + $orderby = 'city_id asc, county_id asc'; + $select = 'county_id, status'; + $map_county = $this->sys_area_model->map('county_id', 'status', $where, $orderby, 0, 0, $select); + if($rows_county){ + foreach($rows_county as $k => $v){ + $city_id = $v['city_id']; + $county_id = $v['county_id']; + $checked = 1==$map_county[$county_id] ? 1 : 0; + $map_checked[$city_id][] = array( + 'county_id' => $county_id, + 'county_name' => $v['county_name'], + 'checked' => $checked, + ); + 1 == $checked && $city_checked[$city_id] = $checked; + } + } + } + foreach($rows as $k => $v){ + $city_id = $v['city_id']; + $lists[] = array( + 'id' => $v['id'], + 'city_id' => $city_id, + 'firstchar' => $v['firstchar'], + 'name' => $v['name'], + 'checked' => $city_checked[$city_id] ? 1 : 0, + 'countys' => $map_checked[$city_id] ? $map_checked[$city_id] : array(), + ); + } + } + + $this->data['params'] = $params; + $this->data['lists'] = $lists; $this->data['pager'] = array('count'=>ceil($count/$size),'curr'=>$page,'totle'=>$count); $this->data['_title'] = '城市管理'; - $this->data['city_lists'] = $lists; - $this->data['pager']['total'] = $count; - $this->data['status_arr'] = array(1=>'开启',0=>'关闭'); $this->show_view('sys/city/lists',true); } public function get() { + $info = array('province_id' => '350', 'city_id' => '', 'name' => '', 'firstchar' => '',);//默认添加福建城市 + $this->data['info'] = $info; $this->data['_title'] = '新增城市'; $this->show_view('sys/city/edit'); } @@ -73,6 +120,13 @@ class City extends HD_Controller{ if(!$firstchar || empty($firstchar)){ return $this->show_json(SYS_CODE_FAIL, '首字母不能为空'); } + + //判断是否重复 + $row = $this->sys_city_model->get(array('city_id' => $city_id)); + if($row){ + return $this->show_json(SYS_CODE_FAIL, '该城市已经添加'); + } + $data = array( 'city_id' => $city_id, 'name' => $name, @@ -85,15 +139,77 @@ class City extends HD_Controller{ { } + + /** + * 保存选中的区域 + * @return bool + */ + function edit_areas(){ + $lists = $this->input->post('lists'); + $lists = json_decode($lists, true); + $add_ids = array();//需要新增选中的区域ID + $del_ids = array();//需要删除区域的城市ID + foreach($lists as $k => $v){ + $upd = array('status' => $v['checked'] ? 1 : 0); + $where = array('id' => $v['id']); + //更新城市的状态 + $ret = $this->sys_city_model->update($upd, $where); + if($ret){ + $del_ids[] = $v['city_id']; + foreach($v['countys'] as $k_county => $v_county){ + if($v_county['checked']){ + $add_ids[] = $v_county['county_id']; + } + } + } + } + if($del_ids){ + //删除旧的选中区域 + $str_ids = implode(',', $del_ids); + $where = array("city_id in ({$str_ids})" => null); + $ret = $this->sys_area_model->delete($where); + } + if($add_ids){ + //新增选中的区域 + $adds = array(); + $str_ids = implode(',', $add_ids); + $where = array("county_id in ({$str_ids})" => null); + $orderby = 'county_id asc'; + $select = 'province_id, province_name, city_id, city_name, county_id, county_name'; + $rows = $this->area_model->select($where, $orderby, 0, 0, $select); + foreach($rows as $v){ + $adds[] = array( + 'province_id' => $v['province_id'], + 'province_name' => $v['province_name'], + 'city_id' => $v['city_id'], + 'city_name' => $v['city_name'], + 'county_id' => $v['county_id'], + 'county_name' => $v['county_name'], + 'status' => 1, + ); + } + if($adds){ + $ret = $this->sys_area_model->add_batch($adds); + } + } + + return $this->show_json(SYS_CODE_SUCCESS,'保存成功'); + } + public function del() { $id = $this->input->post('id'); - $stauts = $this->input->post('status'); if($id) { - $this->sys_city_model->update(array('status' => $stauts), array('id' => $id)); + $where = array('id' => $id); + $row = $this->sys_city_model->get($where); + $ret = $this->sys_city_model->delete($where); + if($ret){ + $where = array('city_id' => $row['city_id']); + $this->sys_area_model->delete($where); + } } - return $this->show_json(SYS_CODE_SUCCESS,'保存成功'); + return $this->show_json(SYS_CODE_SUCCESS,'操作成功'); } public function batch() { diff --git a/admin/core/HD_Controller.php b/admin/core/HD_Controller.php index ff2b945e..4b4c998e 100755 --- a/admin/core/HD_Controller.php +++ b/admin/core/HD_Controller.php @@ -104,4 +104,25 @@ abstract class HD_Controller extends CI_Controller return false; } + /** + * 获取可以管理的城市 + * @param null $def + * @return mixed + */ + protected function city_ary($def = null){ + $this->load->model("sys/sys_city_model", 'city_model'); + + if(!is_null($def)){ + return ''; + } + + $where = array('status' => 1); + $orderby = "id desc"; + $select = "city_id, name"; + + $map_city = $this->city_model->map('city_id', 'name', $where, $orderby, 0, 0, $select); + + return $map_city; + } + } \ No newline at end of file diff --git a/admin/views/biz/store/edit.php b/admin/views/biz/store/edit.php index 6c20e526..7d592cdb 100755 --- a/admin/views/biz/store/edit.php +++ b/admin/views/biz/store/edit.php @@ -123,7 +123,7 @@ min-height: 300px; } - +
@@ -161,7 +161,7 @@
-
+ +
+ +
diff --git a/admin/views/sys/city/edit.php b/admin/views/sys/city/edit.php index f7ab1c4a..5e7a7f7a 100755 --- a/admin/views/sys/city/edit.php +++ b/admin/views/sys/city/edit.php @@ -1,13 +1,107 @@ - +
-
+
+ +
-
+
-
+
- \ No newline at end of file + + \ No newline at end of file diff --git a/admin/views/sys/city/lists.php b/admin/views/sys/city/lists.php index 43683af6..f4e23297 100755 --- a/admin/views/sys/city/lists.php +++ b/admin/views/sys/city/lists.php @@ -1,56 +1,136 @@ -
-
-