1
0

VrFloder.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\model;
  4. use think\facade\Db;
  5. use think\Model;
  6. /**
  7. * @mixin \think\Model
  8. *
  9. */
  10. class VrFloder extends Model
  11. {
  12. /**
  13. * 数据添加前置操作
  14. */
  15. public static function onBeforeWrite($floder)
  16. {
  17. // 1 如果是修改,且没有根, 则只是修改信息,并未修改结构位置
  18. if (!empty($floder->id) && empty($floder->pid)) return true;
  19. // 2 如果是修改,且文件夹位置变更, 则将后续位置信息减2
  20. if (!empty($floder->id) && !empty($floder->pid)) {
  21. if ($floder->fld_type == 1) {
  22. $conditionLeft = [['lft', '>', $floder->rgt], ['fld_type', '=', 1], ['create_user', '=', $floder->create_user]];
  23. $conditionRight = [['rgt', '>', $floder->rgt], ['fld_type', '=', 1], ['create_user', '=', $floder->create_user]];
  24. } else {
  25. $conditionLeft = [['lft', '>', $floder->rgt], ['fld_type', '=', 2], ['root_id', '=', $floder->root_id]];
  26. $conditionRight = [['rgt', '>', $floder->rgt], ['fld_type', '=', 2], ['root_id', '=', $floder->root_id]];
  27. }
  28. VrFloder::where($conditionLeft)->update(['lft' => Db::raw('lft-2')]);
  29. VrFloder::where($conditionRight)->update(['rgt' => Db::raw('rgt-2')]);
  30. }
  31. // 3 如果是在根下添加文件夹
  32. if (empty($floder->id) && empty($floder->pid)) {
  33. if ($floder->fld_type == 1) { // 个人
  34. $parent = VrFloder::where(['fld_type' => 1, 'create_user' => $floder->create_user, 'lft' => 1, 'depth' => 0])->find();
  35. } else { // 团队根
  36. $parent = VrFloder::where(['fld_type' => 2, 'root_id' => $floder->root_id, 'lft' => 1, 'depth' => 0])->find();
  37. }
  38. if (empty($parent)) {
  39. $parentId = VrFloder::insertGetId([
  40. 'fld_name' => '全部',
  41. 'createtime' => time(),
  42. 'vr_num' => 0,
  43. 'fld_type' => $floder->fld_type,
  44. 'create_user' => $floder->create_user,
  45. 'lft' => 1,
  46. 'rgt' => 2,
  47. 'depth' => 0,
  48. 'root_id' => $floder->root_id ?? null
  49. ]);
  50. $parent = VrFloder::find($parentId);
  51. }
  52. }
  53. // 4 添加子文件夹
  54. if (empty($floder->id) && !empty($floder->pid)) $parent = VrFloder::find($floder->pid);
  55. $conditionLeft = [['lft', '>', $parent->rgt], ['fld_type', '=', $parent->fld_type], ['create_user', '=', $floder->create_user]];
  56. $conditionRight = [['rgt', '>=', $parent->rgt], ['fld_type', '=', $parent->fld_type], ['create_user', '=', $floder->create_user]];
  57. VrFloder::where($conditionLeft)->update(['lft' => Db::raw('lft+2')]);
  58. VrFloder::where($conditionRight)->update(['rgt' => Db::raw('rgt+2')]);
  59. $floder->lft = $parent->rgt;
  60. $floder->rgt = $parent->rgt + 1;
  61. $floder->depth = $parent->depth + 1;
  62. return true;
  63. }
  64. public function group()
  65. {
  66. return $this->hasmany(VrGroup::class, 'floder_id', 'id');
  67. }
  68. /**
  69. * 处理
  70. */
  71. public function getCreatetimeAttr($value){
  72. if (empty($value)) return $value;
  73. return date('Y-m-d H:i:s',(int)$value);
  74. }
  75. }