groupbuy_class.model.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * 团购地区模型
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class groupbuy_classModel{
  11. const TABLE_NAME = 'groupbuy_class';
  12. const PK = 'class_id';
  13. /**
  14. * 构造检索条件
  15. *
  16. * @param array $condition 检索条件
  17. * @return string
  18. */
  19. private function getCondition($condition){
  20. $condition_str = '';
  21. if (!empty($condition['class_id'])){
  22. $condition_str .= "and class_id = '".$condition['class_id']."'";
  23. }
  24. if (!empty($condition['in_class_id'])){
  25. $condition_str .= "and class_id in (".$condition['in_class_id'].")";
  26. }
  27. if (isset($condition['class_parent_id'])){
  28. $condition_str .= "and class_parent_id = '".$condition['class_parent_id']."'";
  29. }
  30. if (!empty($condition['deep'])){
  31. $condition_str .= "and deep <= '".$condition['deep']."'";
  32. }
  33. return $condition_str;
  34. }
  35. /**
  36. * 读取列表
  37. *
  38. */
  39. public function getList($condition = '',$page = ''){
  40. $param = array() ;
  41. $param['table'] = self::TABLE_NAME ;
  42. $param['where'] = $this->getCondition($condition);
  43. $param['order'] = isset($condition['order']) ? $condition['order']: ' '.self::PK.' desc ';
  44. return Db::select($param,$page);
  45. }
  46. /**
  47. * 读取列表
  48. *
  49. */
  50. public function getTreeList($condition='',$page='',$max_deep=1){
  51. $class_list = $this->getList($condition,$page);
  52. $tree_list = array();
  53. if(is_array($class_list)) {
  54. $tree_list = $this->_getTreeList($class_list,0,0,$max_deep);
  55. }
  56. return $tree_list;
  57. }
  58. //按照顺序显示树形结构
  59. private function _getTreeList($list,$parent_id,$deep=0,$max_deep) {
  60. $result = array();
  61. foreach($list as $node) {
  62. if($node['class_parent_id'] == $parent_id) {
  63. if($deep <= $max_deep) {
  64. $temp = $this->_getTreeList($list,$node['class_id'],$deep+1,$max_deep);
  65. if(!empty($temp)) {
  66. $node['have_child'] = 1;
  67. }
  68. else {
  69. $node['have_child'] = 0;
  70. }
  71. //标记是否为叶子节点
  72. if($deep == $max_deep) {
  73. $node['node'] = 1;
  74. }
  75. else {
  76. $node['node'] = 0;
  77. }
  78. $node['deep'] = $deep;
  79. $result[] = $node;
  80. if(!empty($temp)) {
  81. $result = array_merge($result,$temp);
  82. }
  83. unset($temp);
  84. }
  85. }
  86. }
  87. return $result;
  88. }
  89. /**
  90. * 根据编号获取所有下级编号的数组
  91. *
  92. * @param array
  93. * @return array 数组类型的返回结果
  94. */
  95. public function getAllClassId($class_id_array) {
  96. $all_class_id_array = array();
  97. $class_list = $this->getList();
  98. foreach($class_id_array as $class_id) {
  99. $all_class_id_array[] = $class_id;
  100. foreach($class_list as $class) {
  101. if($class['class_parent_id'] == $class_id) {
  102. $all_class_id_array[] = $class['class_id'];
  103. }
  104. }
  105. }
  106. return $all_class_id_array;
  107. }
  108. /**
  109. * 根据编号获取单个内容
  110. *
  111. * @param int $groupbuy_area_id 地区ID
  112. * @return array 数组类型的返回结果
  113. */
  114. public function getOne($id){
  115. if (intval($id) > 0){
  116. $param = array();
  117. $param['table'] = self::TABLE_NAME;
  118. $param['field'] = self::PK;
  119. $param['value'] = intval($id);
  120. $result = Db::getRow($param);
  121. return $result;
  122. }else {
  123. return false;
  124. }
  125. }
  126. /*
  127. * 判断是否存在
  128. * @param array $condition
  129. * @param obj $page //分页对象
  130. * @return array
  131. */
  132. public function isExist($condition='') {
  133. $param = array() ;
  134. $param['table'] = self::TABLE_NAME ;
  135. $param['where'] = $this->getCondition($condition);
  136. $list = Db::select($param);
  137. if(empty($list)) {
  138. return false;
  139. }
  140. else {
  141. return true;
  142. }
  143. }
  144. /*
  145. * 增加
  146. * @param array $param
  147. * @return bool
  148. */
  149. public function save($param){
  150. return Db::insert(self::TABLE_NAME,$param) ;
  151. }
  152. /*
  153. * 更新
  154. * @param array $update_array
  155. * @param array $where_array
  156. * @return bool
  157. */
  158. public function update($update_array, $where_array){
  159. $where = $this->getCondition($where_array) ;
  160. return Db::update(self::TABLE_NAME,$update_array,$where) ;
  161. }
  162. /*
  163. * 删除
  164. * @param array $param
  165. * @return bool
  166. */
  167. public function drop($param){
  168. $where = $this->getCondition($param) ;
  169. return Db::delete(self::TABLE_NAME, $where) ;
  170. }
  171. }