Area.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 数据层模型
  10. */
  11. class Area extends BaseModel
  12. {
  13. /**
  14. * 获取地址列表
  15. * @access public
  16. * @author csdeshang
  17. * @param array $condition 条件
  18. * @param obj $fields 字段
  19. * @param array $group 分组
  20. * @param int $limit 数量限制
  21. * @param str $order 排序
  22. * @return array
  23. */
  24. public function getAreaList($condition = array(), $fields = '*', $group = '', $limit = 0, $order = '')
  25. {
  26. return Db::name('area')->where($condition)->field($fields)->limit($limit)->order($order)->group($group)->select()->toArray();
  27. }
  28. /**
  29. * 从缓存获取分类 通过分类id
  30. * @access public
  31. * @author csdeshang
  32. * @param int $id 分类id
  33. * @return array
  34. */
  35. public function getAreaInfoById($id)
  36. {
  37. $data = $this->getCache();
  38. return $data['name'][$id];
  39. }
  40. /**
  41. * 获取地址详情
  42. * @access public
  43. * @author csdeshang
  44. * @param int $condition 条件
  45. * @param array $field 字段
  46. * @return array
  47. */
  48. public function getAreaInfo($condition = array(), $field = '*')
  49. {
  50. return Db::name('area')->where($condition)->field($field)->find();
  51. }
  52. /**
  53. * 获取一级地址(省级)名称数组
  54. * @access public
  55. * @author csdeshang
  56. * @return array 键为id 值为名称字符串
  57. */
  58. public function getTopLevelAreas()
  59. {
  60. $data = $this->getCache();
  61. $arr = array();
  62. foreach ($data['children'][0] as $i) {
  63. $arr[$i] = $data['name'][$i];
  64. }
  65. return $arr;
  66. }
  67. /**
  68. * 获取获取市级id对应省级id的数组
  69. * @access public
  70. * @author csdeshang
  71. * @return array 键为市级id 值为省级id
  72. */
  73. public function getCityProvince()
  74. {
  75. $data = $this->getCache();
  76. $arr = array();
  77. foreach ($data['parent'] as $k => $v) {
  78. if ($v && isset($data['parent'][$v]) && $data['parent'][$v] == 0) {
  79. $arr[$k] = $v;
  80. }
  81. }
  82. return $arr;
  83. }
  84. /**
  85. * 获取地区缓存
  86. * @access public
  87. * @author csdeshang
  88. * @return array
  89. */
  90. public function getAreas()
  91. {
  92. return $this->getCache();
  93. }
  94. /**
  95. * 获取全部地区名称数组
  96. * @access public
  97. * @author csdeshang
  98. * @return array 键为id 值为名称字符串
  99. */
  100. public function getAreaNames()
  101. {
  102. $data = $this->getCache();
  103. return $data['name'];
  104. }
  105. /**
  106. * 获取用于前端js使用的全部地址数组
  107. * @access public
  108. * @author csdeshang
  109. * @param str $src 缓存
  110. * @return array
  111. */
  112. public function getAreaArrayForJson($src = 'cache')
  113. {
  114. if ($src == 'cache') {
  115. $data = $this->getCache();
  116. } else {
  117. $data = $this->_getAllArea();
  118. }
  119. $arr = array();
  120. foreach ($data['children'] as $k => $v) {
  121. foreach ($v as $vv) {
  122. $arr[$k][] = array($vv, $data['name'][$vv]);
  123. }
  124. }
  125. return $arr;
  126. }
  127. /**
  128. * 更新编辑信息
  129. * @access public
  130. * @author csdeshang
  131. * @param array $data 参数内容
  132. * @param array $condition 条件
  133. * @return bool
  134. */
  135. public function editArea($data = array(), $condition = array())
  136. {
  137. // 删除缓存
  138. $this->dropCache();
  139. return Db::name('area')->where($condition)->update($data);
  140. }
  141. /**
  142. * 新增地区
  143. * @access public
  144. * @author csdeshang
  145. * @param array $data 参数内容
  146. * @return boolean
  147. */
  148. public function addArea($data)
  149. {
  150. // 删除缓存
  151. $this->dropCache();
  152. return Db::name('area')->insertGetId($data);
  153. }
  154. /**
  155. * 删除地区
  156. * @access public
  157. * @author csdeshang
  158. * @param array $area_ids 地区id
  159. * @return boolean
  160. */
  161. public function delAreaByAreaIds($area_ids)
  162. {
  163. $area_arr = explode(',', $area_ids);
  164. if (empty($area_arr)) {
  165. return false;
  166. }
  167. $condition = array();
  168. $condition[] = array('area_parent_id', 'in', $area_arr);
  169. $child_list = $this->getAreaList($condition);
  170. if (!empty($child_list)) {
  171. foreach ($child_list as $v) {
  172. $area_child_arr[] = $v['area_id'];
  173. }
  174. $area_child_ids = implode(',', $area_child_arr);
  175. $this->delAreaByAreaIds($area_child_ids);
  176. }
  177. // 删除地区
  178. $condition = array();
  179. $condition[] = array('area_id', 'in', $area_arr);
  180. $this->delArea($condition);
  181. }
  182. /**
  183. * 删除地区
  184. * @access public
  185. * @author csdeshang
  186. * @param unknown $condition 条件
  187. * @return boolean
  188. */
  189. public function delArea($condition)
  190. {
  191. // 删除缓存
  192. $this->dropCache();
  193. return Db::name('area')->where($condition)->delete();
  194. }
  195. /**
  196. * 删除缓存数据
  197. * @access public
  198. * @author csdeshang
  199. */
  200. public function dropCache()
  201. {
  202. $this->cachedData = null;
  203. dkcache('area');
  204. }
  205. /**
  206. * 获取地区数组 格式如下
  207. * array(
  208. * 'name' => array(
  209. * '地区id' => '地区名称',
  210. * // ..
  211. * ),
  212. * 'parent' => array(
  213. * '子地区id' => '父地区id',
  214. * // ..
  215. * ),
  216. * 'children' => array(
  217. * '父地区id' => array(
  218. * '子地区id 1',
  219. * '子地区id 2',
  220. * // ..
  221. * ),
  222. * // ..
  223. * ),
  224. * 'region' => array(array(
  225. * '华北区' => array(
  226. * '省级id 1',
  227. * '省级id 2',
  228. * // ..
  229. * ),
  230. * // ..
  231. * ),
  232. * )
  233. *
  234. * @return array
  235. */
  236. protected function getCache()
  237. {
  238. // 对象属性中有数据则返回
  239. if ($this->cachedData !== null)
  240. return $this->cachedData;
  241. // 缓存中有数据则返回
  242. if ($data = rkcache('area')) {
  243. $this->cachedData = $data;
  244. return $data;
  245. }
  246. // 查库
  247. $data = $this->_getAllArea();
  248. wkcache('area', $data);
  249. $this->cachedData = $data;
  250. return $data;
  251. }
  252. protected $cachedData;
  253. /**
  254. * 获取所有地区
  255. * @access public
  256. * @author csdeshang
  257. * @return array
  258. */
  259. private function _getAllArea()
  260. {
  261. $data = array();
  262. $area_all_array = Db::name('area')->select()->toArray();
  263. foreach ((array)$area_all_array as $a) {
  264. $data['name'][$a['area_id']] = $a['area_name'];
  265. $data['parent'][$a['area_id']] = $a['area_parent_id'];
  266. $data['children'][$a['area_parent_id']][] = $a['area_id'];
  267. if ($a['area_deep'] == 1 && $a['area_region'])
  268. $data['region'][$a['area_region']][] = $a['area_id'];
  269. }
  270. wkcache('area', $data);
  271. $this->cachedData = $data;
  272. return $data;
  273. }
  274. /**
  275. * 从缓存获取分类 通过上级分类id
  276. * @access public
  277. * @author csdeshang
  278. * @param int $pid 上级分类id 若传0则返回1级分类
  279. * @return array
  280. */
  281. public function getAreaListByParentId($pid)
  282. {
  283. $data = $this->getCache();
  284. $ret = array();
  285. foreach ((array)$data['children'][$pid] as $i) {
  286. if ($data['data'][$i]) {
  287. $ret[] = $data['data'][$i];
  288. }
  289. }
  290. return $ret;
  291. }
  292. /**
  293. * 递归取得本地区及所有上级地区名称
  294. * @access public
  295. * @author csdeshang
  296. * @param int $area_id 地区id
  297. * @param str $area_name 地区名称
  298. * @return array
  299. */
  300. public function getTopAreaName($area_id, $area_name = '')
  301. {
  302. $info_parent = $this->getAreaInfo(array('area_id' => $area_id), 'area_name,area_parent_id');
  303. if ($info_parent) {
  304. return $this->getTopAreaName($info_parent['area_parent_id'], $info_parent['area_name']) . ' ' . $info_parent['area_name'];
  305. }
  306. }
  307. /**
  308. * 递归取得本地区所有孩子ID
  309. * @access public
  310. * @author csdeshang
  311. * @param $area_id 地区id
  312. * @return array
  313. */
  314. public function getChildrenIDs($area_id)
  315. {
  316. $result = array();
  317. $list = $this->getAreaList(array('area_parent_id' => $area_id), 'area_id');
  318. if ($list) {
  319. foreach ($list as $v) {
  320. $result[] = $v['area_id'];
  321. $result = array_merge($result, $this->getChildrenIDs($v['area_id']));
  322. }
  323. }
  324. return $result;
  325. }
  326. /**
  327. * 从缓存获取所有子节点 通过id
  328. * @access public
  329. * @author csdeshang
  330. * @param int $pid 上级地区id 若传0则返回1级分类
  331. * @return array
  332. */
  333. public function getChildsByPid($pid)
  334. {
  335. $data = $this->getCache();
  336. if (!isset($data['children'][$pid])) {
  337. return false;
  338. }
  339. $ret = array();
  340. foreach ((array)$data['children'][$pid] as $i) {
  341. $ret[] = $i;
  342. $ret_temp = $this->getChildsByPid($i);
  343. if ($ret_temp != false) {
  344. foreach ($ret_temp as $v) {
  345. $ret[] = $v;
  346. }
  347. }
  348. }
  349. return $ret;
  350. }
  351. /**
  352. * 获取子节点名称通过id
  353. * @access public
  354. * @author csdeshang
  355. * @param int $pid id
  356. * @return array
  357. */
  358. public function GetChildName($pid)
  359. {
  360. return Db::name('area')->field('area_id,area_name')->where(array('area_parent_id' => $pid))->select()->toArray();
  361. }
  362. /**
  363. * 获取列表 根据id
  364. * @access public
  365. * @author csdeshang
  366. * @param type $parent_id id
  367. * @return array
  368. */
  369. public function getAreaChild($parent_id = -1)
  370. {
  371. $map = array();
  372. if ($parent_id >= 0) {
  373. $map['area_parent_id'] = $parent_id;
  374. }
  375. return Db::name('area')->where($map)->order('area_sort')->select()->toArray();
  376. }
  377. }