Area.php 11 KB

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