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