Fleaarea.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 数据层模型
  10. */
  11. class Fleaarea extends BaseModel
  12. {
  13. /**
  14. * 读取系统设置列表
  15. * @access public
  16. * @author csdeshang
  17. * @param array $condition 检索条件
  18. * @param string $order 排序条件
  19. * @return array 数组格式的返回结果
  20. */
  21. public function getFleaareaList($condition, $field = '*', $order = 'fleaarea_sort desc', $limit = 0)
  22. {
  23. $result = Db::name('fleaarea')->field($field)->limit($limit)->order($order)->where($condition)->select()->toArray();
  24. return $result;
  25. }
  26. /**
  27. * 新增
  28. * @access public
  29. * @author csdeshang
  30. * @param array $data 参数内容
  31. * @return bool 布尔类型的返回结果
  32. */
  33. public function addFleaarea($data)
  34. {
  35. $result = Db::name('fleaarea')->insertGetId($data);
  36. return $result;
  37. }
  38. /**
  39. * 取单个地区的内容
  40. * @access public
  41. * @author csdeshang
  42. * @param array $condition 检索条件
  43. * @return array 数组类型的返回结果
  44. */
  45. public function getOneFleaarea($condition)
  46. {
  47. if (!empty($condition)) {
  48. $result = Db::name('fleaarea')->where($condition)->find();
  49. return $result;
  50. } else {
  51. return false;
  52. }
  53. }
  54. /**
  55. * 更新信息
  56. * @access public
  57. * @author csdeshang
  58. * @param array $data 更新数据
  59. * @return bool 布尔类型的返回结果
  60. */
  61. public function editFleaarea($condition, $data)
  62. {
  63. $result = Db::name('fleaarea')->where($condition)->update($data);
  64. return $result;
  65. }
  66. /**
  67. * 删除地区
  68. * @access public
  69. * @author csdeshang
  70. * @param int/array $id 删除ID
  71. * @param int $deep 删除深度,默认为1
  72. * @return array $rs_row 返回数组形式的查询结果
  73. */
  74. public function delFleaarea($id, $deep = 1)
  75. {
  76. if (!empty($id)) {
  77. if (!is_array($id)) {
  78. $id = array($id);
  79. }
  80. /**
  81. * 取得地区缓存内容
  82. */
  83. $child_deep = $deep + 1;
  84. for ($i = $child_deep; $i <= 4; $i++) {
  85. $cache_file = ROOT_PATH . "extend" . DIRECTORY_SEPARATOR . "area" . DIRECTORY_SEPARATOR . "area_" . $deep . ".php";
  86. if (file_exists($cache_file)) {
  87. require_once($cache_file);
  88. $tmp = 'cache_data_' . $i;
  89. $$tmp = $area_array;
  90. unset($tmp, $area_array);
  91. }
  92. }
  93. foreach ($id as $k => $v) {
  94. if (intval($v) > 0) {
  95. $del_tmp[] = "fleaarea_id = '" . $v . "'";
  96. /**
  97. * 判断子类中是否还有内容
  98. */
  99. if ($child_deep <= 4) {
  100. $del_parent_id = array($v);
  101. for ($i = $child_deep; $i <= 4; $i++) {
  102. $tmp = 'cache_data_' . $i;
  103. if (isset($$tmp) && is_array($$tmp) && !empty($del_parent_id)) {
  104. foreach ($del_parent_id as $k_parent => $v_parent) {
  105. foreach ($$tmp as $k_2 => $v_2) {
  106. if ($v_2['fleaarea_parent_id'] == $v_parent) {
  107. $del_tmp[] = "fleaarea_id = '" . $v_2['fleaarea_id'] . "'";
  108. $next_parent_id[] = $v_2['fleaarea_id'];
  109. }
  110. }
  111. }
  112. /**
  113. * 再下一级的父ID
  114. */
  115. $del_parent_id = $next_parent_id;
  116. }
  117. }
  118. }
  119. }
  120. }
  121. $where = implode(' or ', $del_tmp);
  122. $result = Db::name('fleaarea')->where($where)->delete();
  123. return $result;
  124. } else {
  125. return false;
  126. }
  127. }
  128. /**
  129. * 地址展示
  130. * @access public
  131. * @author csdeshang
  132. * @return type
  133. */
  134. public function fleaarea_show()
  135. {
  136. $area_one_level = array();
  137. $area_two_level = array();
  138. $condition = array();
  139. $condition[] = array('fleaarea_parent_id', '=', 1);
  140. $area_list = $this->getFleaareaList($condition, 'fleaarea_id,fleaarea_name,fleaarea_parent_id', 'fleaarea_parent_id asc,fleaarea_sort asc,fleaarea_id asc');
  141. if (is_array($area_list) && !empty($area_list)) {
  142. foreach ($area_list as $val) {
  143. if ($val['fleaarea_parent_id'] == 0) {
  144. $fleaarea_id = $val['fleaarea_id'];
  145. $area_one_level[] = $val;
  146. $area_two_level[$fleaarea_id]['id'] = $fleaarea_id;
  147. } else {
  148. $fleaarea_parent_id = $val['fleaarea_parent_id'];
  149. if (isset($area_two_level[$fleaarea_parent_id])) {
  150. $area_two_level[$fleaarea_parent_id]['children'][] = $val;
  151. $area_children = $area_two_level[$fleaarea_parent_id]['children'];
  152. $area_two_level[$fleaarea_parent_id]['content'] = json_encode($area_children);
  153. }
  154. }
  155. }
  156. }
  157. return (array('area_one_level' => $area_one_level, 'area_two_level' => $area_two_level));
  158. }
  159. }