Fleaarea.php 6.0 KB

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