ClassifyMethod.php 7.6 KB

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