Department.php 3.1 KB

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