ClassifyMethod.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: qiuzijian
  5. * Date: 2021-04-17
  6. * Time: 17:16
  7. */
  8. namespace Modules\Admin\Traits;
  9. use Illuminate\Support\Arr;
  10. use Illuminate\Support\Facades\Cache;
  11. trait ClassifyMethod
  12. {
  13. /**
  14. * 模型对象
  15. * @var
  16. */
  17. protected $classify;
  18. /**
  19. * 缓存名
  20. * @var string
  21. */
  22. public $cacheTitle = 'Classify';
  23. /**
  24. * 排序方式
  25. * @var string
  26. */
  27. public $sort = 'desc';
  28. /**
  29. * 删除
  30. * @param $classifyInfo
  31. * @return mixed
  32. */
  33. public function add($classifyInfo)
  34. {
  35. return $this->addOrEdit($classifyInfo);
  36. }
  37. /**
  38. * 添加
  39. * @param $classifyInfo
  40. * @return bool
  41. */
  42. public function edit($classifyInfo)
  43. {
  44. if (!isset($classifyInfo['id'])) {
  45. return false;
  46. }
  47. return $this->addOrEdit($classifyInfo);
  48. }
  49. /**
  50. * 递归删除
  51. * @param $classifyId
  52. * @return bool
  53. */
  54. public function del($classifyId)
  55. {
  56. if (empty($classifyId)) {
  57. return false;
  58. }
  59. $classify = $this->classify->find($classifyId);
  60. // $subClassifys = $this->classify->where('parent_id', $classifyId)->get()->toArray();
  61. // if (count($subClassifys) > 0) { // 如果有子菜单将子菜单删除
  62. // foreach ($subClassifys as $subClassify) {
  63. // self::del($subClassify['id']);
  64. // }
  65. // }
  66. if ($classify) {
  67. $this->classify->where('degree', 'like', $classify->degree . '|%')->delete();
  68. $classify->delete();
  69. }
  70. $this->sortClassifyAndddCache(1);
  71. $this->sortClassifyAndddCache();
  72. return true;
  73. }
  74. /**
  75. * 获取单个信息
  76. * @param $classifyId
  77. * @return false
  78. */
  79. public function getOneInfo($classifyId)
  80. {
  81. if (!$classifyId) {
  82. return false;
  83. }
  84. return $this->classify->find($classifyId);
  85. }
  86. /**
  87. * 获取所有
  88. * @return mixed
  89. */
  90. public function getAll($show = '')
  91. {
  92. if (!empty($show)) {
  93. return $this->classify->where('show', $show)->all();
  94. }
  95. return $this->classify->all();
  96. }
  97. /**
  98. * 获取层级列表
  99. * @param string $show
  100. * @param string $category
  101. * @return array|mixed|string
  102. */
  103. public function getTierList($show = '', $degree = '')
  104. {
  105. // 判断并获取缓存
  106. if (!empty($show)) {
  107. if (Cache::has($this->cacheTitle . 'ShowList')) {
  108. $classifyList = Cache::get($this->cacheTitle . 'ShowList');
  109. } else {
  110. $classifyList = $this->sortClassifyAndddCache(1);
  111. }
  112. } else {
  113. if (Cache::has($this->cacheTitle . 'List')) {
  114. $classifyList = Cache::get($this->cacheTitle . 'List');
  115. } else {
  116. $classifyList = $this->sortClassifyAndddCache();
  117. }
  118. }
  119. $classifyList = empty($classifyList) ? [] : $classifyList;
  120. if (empty($degree)) {
  121. return $classifyList;
  122. }
  123. return Arr::get($classifyList, $degree);
  124. }
  125. /**
  126. * 递归岗位数据
  127. * @param $classify
  128. * @param int $pid
  129. * @return array|string
  130. */
  131. public function sortClassify($classify, $pid = '0')
  132. {
  133. $arr = [];
  134. if (empty($classify)) {
  135. return '';
  136. }
  137. $num = 0;
  138. foreach ($classify as $key => $value) {
  139. if ($value['parent_id'] == $pid) {
  140. $arr[$num] = $value;
  141. $arr[$num]['child'] = self::sortClassify($classify, $value['id']);
  142. if (count($arr[$num]['child']) == 0) {
  143. unset($arr[$num]['child']);
  144. }
  145. $num++;
  146. }
  147. }
  148. return $arr;
  149. }
  150. /**
  151. * 排序子岗位并缓存
  152. * ---------------------------------
  153. * array_column($array,$name)
  154. * $array => 要查询的数组
  155. * $name => 在数组中要返回的字段值
  156. *----------------------------------
  157. * array_multisort($array1,$rule,$array2)
  158. * $array1 => 规定数组(规定以这个数组来排序)
  159. * $rule => 规定排列顺序。
  160. * 可能的值:
  161. * SORT_ASC - 默认。按升序排列 (A-Z)。
  162. * SORT_DESC - 按降序排列 (Z-A)。
  163. * $array2 => 要规定的数组。(要排序的数组)
  164. */
  165. public function sortClassifyAndddCache($show = '')
  166. {
  167. $classify = $this->classify;
  168. if (!empty($show)) {
  169. $classify = $classify->where('show', $show);
  170. }
  171. $classify = $classify->orderBy('sort', $this->sort)->get()->toArray();
  172. if ($classify) {
  173. $classifyList = $this->sortClassify($classify);
  174. foreach ($classifyList as $key => $value) {
  175. if (isset($value['child'])) {
  176. $sort = array_column($value['child'], 'sort');
  177. if ($this->sort == 'desc') {
  178. array_multisort($sort, SORT_ASC, $value['child']);
  179. } else {
  180. array_multisort($sort, SORT_DESC, $value['child']);
  181. }
  182. }
  183. }
  184. if (!empty($show)) {
  185. Cache::forever($this->cacheTitle . 'ShowList', $classifyList);
  186. } else {
  187. Cache::forever($this->cacheTitle . 'List', $classifyList);
  188. }
  189. return $classifyList;
  190. }
  191. Cache::forget($this->cacheTitle . 'List');
  192. Cache::forget($this->cacheTitle . 'ShowList');
  193. return '';
  194. }
  195. protected function addOrEdit($classifyInfo)
  196. {
  197. // 判断层级
  198. $classifyInfo['parent_id'] = $classifyInfo['parent_id'] ?? 0;
  199. if ($classifyInfo['parent_id'] == 0) {
  200. $classifyInfo['tier'] = 1;
  201. $degree = '';
  202. } else {
  203. $classifyParent = $this->classify->find($classifyInfo['parent_id']);
  204. $classifyInfo['tier'] = $classifyParent->tier + 1;
  205. $degree = $classifyParent->degree . '|';
  206. }
  207. // 将标题转化成拼音作为标记
  208. if (empty($classifyInfo['slug'])) {
  209. $classifyInfo['slug'] = implode(array_map(function ($v) {
  210. return ucfirst($v);
  211. }, pinyin($classifyInfo['title'])));
  212. }
  213. if (isset($classifyInfo['id'])) {
  214. $curClassifyInfo = $this->classify->find($classifyInfo['id']);
  215. }
  216. $classify = $this->classify->updateOrCreate(['id' => $classifyInfo['id'] ?? ''], $classifyInfo);
  217. $classify->degree = $degree . $classify->id;
  218. $classify->save();
  219. // 更新子集
  220. if (isset($classifyInfo['id']) && $curClassifyInfo['degree'] != $classify['degree']) { // 如果改变父级,联动改变其下属所有子集
  221. $diffTier = $curClassifyInfo['tier'] - $classifyInfo['tier'];
  222. $childClassifys = $this->classify->where('degree', 'like', $curClassifyInfo['degree'] . '|%')->get();
  223. foreach ($childClassifys as $childClassify) {
  224. $degree = str_replace($curClassifyInfo['degree'] . '|', $classify['degree'] . '|', $childClassify['degree']);
  225. $_classify = $this->classify->where('id', $childClassify['id'])->first();
  226. $_classify->degree = $degree;
  227. $_classify->tier = $_classify->tier - $diffTier;
  228. $_classify->save();
  229. }
  230. }
  231. $this->sortClassifyAndddCache(1);
  232. $this->sortClassifyAndddCache();
  233. return $classify;
  234. }
  235. }