UploadGroup.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\model;
  13. use app\store\model\UploadFile as UploadFileModel;
  14. use app\common\model\UploadGroup as UploadGroupModel;
  15. /**
  16. * 文件库分组模型
  17. * Class UploadGroup
  18. * @package app\store\model
  19. */
  20. class UploadGroup extends UploadGroupModel
  21. {
  22. /**
  23. * 获取列表记录
  24. * @return array
  25. * @throws \think\db\exception\DataNotFoundException
  26. * @throws \think\db\exception\DbException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. */
  29. public function getList()
  30. {
  31. $list = $this->getAll();
  32. return $this->getTreeData($list);
  33. }
  34. /**
  35. * 获取所有分组
  36. * @return \think\Collection
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. private function getAll()
  42. {
  43. return $this->order(['sort', 'create_time'])->select();
  44. }
  45. /**
  46. * 获取树状列表
  47. * @param $list
  48. * @param int $parentId
  49. * @return array
  50. */
  51. private function getTreeData(&$list, int $parentId = 0)
  52. {
  53. $data = [];
  54. foreach ($list as $key => $item) {
  55. if ($item['parent_id'] == $parentId) {
  56. $children = $this->getTreeData($list, $item['group_id']);
  57. !empty($children) && $item['children'] = $children;
  58. $data[] = $item;
  59. unset($list[$key]);
  60. }
  61. }
  62. return $data;
  63. }
  64. /**
  65. * 添加新记录
  66. * @param array $data
  67. * @return false|int
  68. */
  69. public function add(array $data)
  70. {
  71. return $this->save(array_merge([
  72. 'store_id' => self::$storeId,
  73. 'sort' => 100
  74. ], $data));
  75. }
  76. /**
  77. * 编辑记录
  78. * @param array $data
  79. * @return bool
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\DbException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. */
  84. public function edit(array $data)
  85. {
  86. // 判断上级分组是否为当前子级
  87. if ($data['parent_id'] > 0) {
  88. // 获取所有上级id集
  89. $parentIds = $this->getTopGroupIds($data['parent_id']);
  90. if (in_array($this['group_id'], $parentIds)) {
  91. $this->error = '上级分组不允许设置为当前子分组';
  92. return false;
  93. }
  94. }
  95. return $this->save($data) !== false;
  96. }
  97. /**
  98. * 获取所有上级id集
  99. * @param int $groupId
  100. * @param null|array $list
  101. * @return array
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. private function getTopGroupIds(int $groupId, $list = null)
  107. {
  108. static $parentIds = [];
  109. is_null($list) && $list = $this->getAll();
  110. foreach ($list as $item) {
  111. if ($item['group_id'] == $groupId && $item['parent_id'] > 0) {
  112. $parentIds[] = $item['parent_id'];
  113. $this->getTopGroupIds($item['parent_id'], $list);
  114. }
  115. }
  116. return $parentIds;
  117. }
  118. /**
  119. * 删除商品分组
  120. * @return bool
  121. * @throws \Exception
  122. */
  123. public function remove()
  124. {
  125. // 判断是否存在下级分组
  126. if (static::detail(['parent_id' => $this['group_id']])) {
  127. $this->error = '当前分组下存在子分组,不允许删除';
  128. return false;
  129. }
  130. // 更新该分组下的所有文件
  131. UploadFileModel::updateBase(['group_id' => 0], ['group_id' => $this['group_id']]);
  132. // 删除分组记录
  133. return $this->delete();
  134. }
  135. }