rabin 6 years ago
parent
commit
941c5c2e98
3 changed files with 138 additions and 9 deletions
  1. 7 2
      database/info.php
  2. 33 0
      lib/Manage.php
  3. 98 7
      src/Api.php

+ 7 - 2
database/info.php

@@ -21,7 +21,7 @@ $type = array
 	6 => '多图片上传',
 	7 => '地区选择器',
 	//8 => '多级分类',
-	//9 => '区间输入框',
+	9 => '区间输入框',
 
 	10 => '单选框',
 	11 => '多选框',
@@ -162,7 +162,7 @@ return array
 		'type_option'      => array
         (
             'type'      => 'varchar-800',
-            'name'      => '属性可选项-多个值用换行隔开,如果是数字输入框,这里定义的是搜索时的选项,比如单价如果需要搜索,可以设置为"100万以下,{v}<=100",半角逗号之后的为表达式,{v}为当前值。',
+            'name'      => '属性可选项-多个值用换行隔开,如果是数字或区间输入框,这里定义的是搜索时的选项,比如单价如果需要搜索,可以设置为"100万以下,{v}<=100",半角逗号之后的为表达式,{v}为当前值,如果是区间输入框,则{s}为区间最小值,{e}为区间最大值,如"100万以下,{e}<=100"。',
             'default'   => '',
             'desc'      => '属性可选项',
             'match'     => 'is_string',
@@ -307,6 +307,7 @@ return array
 			'option' => array
 			(
 				'ids' => array('yes-id', 'in'),
+				'list_reorder' => array('yes', '>='),
 				'state' => 1,
 			),
 			'type' => 'all',
@@ -320,6 +321,7 @@ return array
 			'option' => array
 			(
 				'ids' => array('yes-id', 'in'),
+				'search_reorder' => array('yes', '>='),
 				'state' => 1,
 			),
 			'type' => 'all',
@@ -333,6 +335,7 @@ return array
 			'option' => array
 			(
 				'ids' => array('yes-id', 'in'),
+				'view_reorder' => array('yes', '>='),
 				'state' => 1,
 			),
 			'type' => 'all',
@@ -340,6 +343,7 @@ return array
 			'col' => '*|id',
 		),
 
+		/*
 		'getSearch' => array
 		(
 			# 匹配的正则或函数 选填项
@@ -353,5 +357,6 @@ return array
 			'order' => array('search_reorder' => 'desc', 'id' => 'desc'),
 			'col' => '*',
 		),
+		*/
 	)
 );

+ 33 - 0
lib/Manage.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace Attr\Lib;
+
+use Dever;
+
+class Manage
+{
+    /**
+     * 更新属性信息
+     *
+     * @return mixed
+     */
+    public function update($id, $name, $data)
+    {
+        foreach ($data as $k => $v) {
+            if (isset($data[$k . '_s']) && isset($data[$k . '_e'])) {
+                $temp = explode(',', $v);
+                if (isset($temp[0])) {
+                    $update[$k . '_s'] = $temp[0];
+                }
+                if (isset($temp[1])) {
+                    $update[$k . '_e'] = $temp[1];
+                }
+            }
+        }
+
+        if (isset($update)) {
+        	$update['where_id'] = $id;
+        	Dever::db($name)->update($update);
+        }
+    }
+}

+ 98 - 7
src/Api.php

@@ -77,6 +77,25 @@ class Api
 	{
 		if ($attr) {
 			foreach ($attr as $k => $v) {
+				if ($v['type'] == 9) {
+
+					$k = 'attr_' . $v['id'] . '_s';
+					$config['struct'][$k]['name'] = $v['name'];
+					$config['struct'][$k]['default'] = '';
+					$config['struct'][$k]['desc'] = $v['name'];
+					$config['struct'][$k]['update'] = 'hidden';
+					$config['struct'][$k]['type'] = 'varchar-11';
+					$config['struct'][$k]['match'] = 'is_numeric';
+
+					$k = 'attr_' . $v['id'] . '_e';
+					$config['struct'][$k]['name'] = $v['name'];
+					$config['struct'][$k]['default'] = '';
+					$config['struct'][$k]['desc'] = $v['name'];
+					$config['struct'][$k]['update'] = 'hidden';
+					$config['struct'][$k]['type'] = 'varchar-11';
+					$config['struct'][$k]['match'] = 'is_numeric';
+				}
+
 				$k = 'attr_' . $v['id'];
 				$config['struct'][$k] = array();
 
@@ -154,7 +173,11 @@ class Api
 					default:
 						$config['struct'][$k]['update'] = 'text';
 						$config['struct'][$k]['search'] = 'fulltext';
-						if ($v['data_type'] == 1 || $v['type'] == 1) {
+						if ($v['type'] == 9) {
+							$config['struct'][$k]['name'] .= '-这是一个区间数值,后台填写请用半角逗号,隔开';
+							$config['struct'][$k]['type'] = 'varchar-100';
+							$config['struct'][$k]['match'] = 'is_string';
+						} elseif ($v['data_type'] == 1 || $v['type'] == 1) {
 							$config['struct'][$k]['type'] = 'varchar-11';
 							$config['struct'][$k]['match'] = 'is_numeric';
 						} else {
@@ -195,6 +218,9 @@ class Api
 				$method = 'getListByIds';
 			} elseif ($order == 'search') {
 				$method = 'getSearchByIds';
+			} elseif ($order == 'search_reorder') {
+				$method = 'getSearchByIds';
+				$where['search_reorder'] = 1;
 			} elseif ($order == 'view') {
 				$method = 'getViewByIds';
 			}
@@ -286,13 +312,15 @@ class Api
 		}
 	}
 
-	public function getAttrByCate($category, $order = 'list_reorder')
+	public function getAttrByCate($category, $order = 'list_reorder', $total = false)
 	{
 		$data = array();
+		//print_r(Dever::db('category/attr')->all());
 		if ($category && Dever::project('category')) {
 			$array = explode(',', $category);
 			$cate = array();
-			$total = count($array)-1;
+			$total = $total ? $total : count($array);
+			$total -= 1;
 			foreach ($array as $k => $v) {
 				$cate[$k] = $v;
 				if ($k < $total) {
@@ -301,7 +329,7 @@ class Api
 				$where['category'] = $cate;
 				$info = Dever::db('category/attr')->one($where);
 				if ($info) {
-					$data += Dever::load('attr/api')->getList($info['attr'], $info['attr_input']);
+					$data += Dever::load('attr/api')->getList($info['attr'], $info['attr_input'], $order);
 				}
 			}
 			$this->attrSort($data, $order);
@@ -315,20 +343,20 @@ class Api
 	 *
 	 * @return mixed
 	 */
-	public function getSearch($ids, $cate = true, $city = false, $search_value = false)
+	public function getSearch($ids, $cate = true, $city = false, $search_value = false, $total = 3)
 	{
 		$value = array();
 		$search_value = Dever::preInput('attr_', $search_value);
 
 		if ($cate && $ids && Dever::project('category')) {
-			$data = $this->getAttrByCate($ids, 'search_reorder');
+			$data = $this->getAttrByCate($ids, 'search_reorder', $total);
 			if (!$data) {
 				return array('search' => array(), 'value' => $value);
 			}
 		} else {
 			$where['ids'] = $ids;
 			$where['search_reorder'] = 1;
-			$data = Dever::db('attr/info')->getSearch($where);
+			$data = Dever::db('attr/info')->getSearchByIds($where);
 		}
 
 		if ($data) {
@@ -366,4 +394,67 @@ class Api
 
 		return array('search' => $data, 'value' => $value);
 	}
+
+	/**
+	 * 获取搜索sql
+	 *
+	 * @return mixed
+	 */
+	public function getSql($attr, $where, $sql = array(), $tname = 't_1')
+	{
+		if (!$attr) {
+			return $sql;
+		}
+		foreach ($attr as $k => $v) {
+            $key = 'attr_' . $v['id'];
+            if (isset($where[$key]) && $where[$key]) {
+                //$where['option'][$key] = array('yes', '=');
+                if ($v['type'] == 7) {
+                    # 地区有点复杂,暂时先这样,后续优化一下
+                    $county = Dever::db('area/county')->one($where[$key]);
+                    if ($county) {
+                        $city = Dever::db('area/city')->one($county['city_id']);
+                        if ($city) {
+                            $province = Dever::db('area/province')->one($city['province_id']);
+                            if ($province) {
+                                $value = $province['id'] . ',' . $city['id'] . ',' . $county['id'];
+                                //$where[$key] = $value;
+                                $sql[] = ' '.$tname.'.'.$key.' = "'.$value.'"';
+                            }
+                        }
+                    }
+                } elseif ($v['type'] == 1 && $v['type_option']) {
+                    $temp = explode("\n", $v['type_option']);
+                    if (isset($temp[$where[$key]-1])) {
+                        $temp = explode(',', $temp[$where[$key]-1]);
+                        $match = $temp[1];
+
+                        //$where['option'][$key] = array('yes', '<=');
+                        //$where[$key] = 100;
+
+                        $match = str_replace('{v}', $tname . '.' . $key, $match);
+                        $sql[] = $match;
+                    }
+                } elseif ($v['type'] == 9 && $v['type_option']) {
+                    $temp = explode("\n", $v['type_option']);
+                    if (isset($temp[$where[$key]-1])) {
+                        $temp = explode(',', $temp[$where[$key]-1]);
+                        $match = $temp[1];
+
+                        //$where['option'][$key] = array('yes', '<=');
+                        //$where[$key] = 100;
+
+                        $match = str_replace('{s}', $tname . '.' . $key . '_s', $match);
+                        $match = str_replace('{e}', $tname . '.' . $key . '_e', $match);
+                        $match = str_replace('{v}', $tname . '.' . $key . '_e', $match);
+                        $sql[] = $match;
+                    }
+                } else {
+                    $sql[] = ' '.$tname.'.'.$key.' = "'.$where[$key].'"';
+                }
+            }
+        }
+
+        return $sql;
+	}
 }