Department.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Models\Manager\Personnel;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\DB;
  6. /**
  7. * 部门模型
  8. * @author 唐远望
  9. * @version 1.0
  10. * @date 2025-12-04
  11. */
  12. class Department extends Model
  13. {
  14. use HasFactory;
  15. // 与模型关联的表名
  16. protected $table = 'personnel_department';
  17. // 是否主动维护时间戳
  18. public $timestamps = false;
  19. // 定义时间戳字段名
  20. // const CREATED_AT = 'insert_time';
  21. // const UPDATED_AT = 'update_time';
  22. /**
  23. * 添加
  24. * @author 唐远望
  25. * @version 1.0
  26. * @date 2025-12-04
  27. */
  28. public function addDepartment_content($data)
  29. {
  30. $insert_data = [
  31. 'name' => $data['name'],
  32. 'insert_time' => time(),
  33. ];
  34. $Department_id = $this->insertGetId($insert_data);
  35. return $Department_id;
  36. }
  37. /**
  38. * 写入数据
  39. * @author 唐远望
  40. * @version 1.0
  41. * @date 2025-12-04
  42. * @param $data
  43. * @return bool
  44. */
  45. public function addDepartment($data)
  46. {
  47. DB::beginTransaction();
  48. try {
  49. $this->addDepartment_content($data);
  50. DB::commit();
  51. return true;
  52. // 成功处理...
  53. } catch (\Exception $e) {
  54. DB::rollBack();
  55. // 错误处理...
  56. return false;
  57. }
  58. }
  59. /**
  60. * 编辑内容
  61. * @author 唐远望
  62. * @version 1.0
  63. * @date 2025-12-04
  64. * @param $data
  65. * @return bool
  66. */
  67. public function editDepartment_content($Department, $data)
  68. {
  69. $Department->name = $data['name'];
  70. $Department->update_time = time();
  71. $Department->save();
  72. return true;
  73. }
  74. /**
  75. * 更新数据
  76. * @author 唐远望
  77. * @version 1.0
  78. * @date 2025-12-04
  79. * @param $data
  80. * @return bool
  81. */
  82. public function updateDepartment($where, $data)
  83. {
  84. DB::beginTransaction();
  85. try {
  86. $this->editDepartment_content($where, $data);
  87. DB::commit();
  88. return true;
  89. // 成功处理...
  90. } catch (\Exception $e) {
  91. DB::rollBack();
  92. // 错误处理...
  93. return false;
  94. }
  95. }
  96. /**
  97. * 修改状态
  98. * @author 唐远望
  99. * @version 1.0
  100. * @date 2025-12-04
  101. * @param $id
  102. * @param $status
  103. * @return bool
  104. */
  105. public function changeStatus($where, $status)
  106. {
  107. $Department = $this->where($where)->first();
  108. if (!$Department) {
  109. return false;
  110. }
  111. $Department->status = $status;
  112. $Department->update_time = time();
  113. $Department->save();
  114. return true;
  115. }
  116. /**
  117. * 删除数据
  118. * @author 唐远望
  119. * @version 1.0
  120. * @date 2025-12-04
  121. * @param $id
  122. * @return bool
  123. */
  124. public function deleteDepartment($where)
  125. {
  126. $Department = $this->where($where)->first();
  127. if (!$Department) {
  128. return false;
  129. }
  130. $Department->delete();
  131. return true;
  132. }
  133. }