Department.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Models\Api\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. 'company_id'=> $data['company_id'],
  33. 'insert_time' => time(),
  34. ];
  35. $Department_id = $this->insertGetId($insert_data);
  36. return $Department_id;
  37. }
  38. /**
  39. * 写入数据
  40. * @author 唐远望
  41. * @version 1.0
  42. * @date 2025-12-04
  43. * @param $data
  44. * @return bool
  45. */
  46. public function addDepartment($data)
  47. {
  48. DB::beginTransaction();
  49. try {
  50. $this->addDepartment_content($data);
  51. DB::commit();
  52. return true;
  53. // 成功处理...
  54. } catch (\Exception $e) {
  55. DB::rollBack();
  56. // 错误处理...
  57. return false;
  58. }
  59. }
  60. /**
  61. * 编辑内容
  62. * @author 唐远望
  63. * @version 1.0
  64. * @date 2025-12-04
  65. * @param $data
  66. * @return bool
  67. */
  68. public function editDepartment_content($Department, $data)
  69. {
  70. $Department->name = $data['name'];
  71. $Department->update_time = time();
  72. $Department->save();
  73. return true;
  74. }
  75. /**
  76. * 更新数据
  77. * @author 唐远望
  78. * @version 1.0
  79. * @date 2025-12-04
  80. * @param $data
  81. * @return bool
  82. */
  83. public function updateDepartment($where, $data)
  84. {
  85. DB::beginTransaction();
  86. try {
  87. $this->editDepartment_content($where, $data);
  88. DB::commit();
  89. return true;
  90. // 成功处理...
  91. } catch (\Exception $e) {
  92. DB::rollBack();
  93. // 错误处理...
  94. return false;
  95. }
  96. }
  97. /**
  98. * 修改状态
  99. * @author 唐远望
  100. * @version 1.0
  101. * @date 2025-12-04
  102. * @param $id
  103. * @param $status
  104. * @return bool
  105. */
  106. public function changeStatus($where, $status)
  107. {
  108. $Department = $this->where($where)->first();
  109. if (!$Department) {
  110. return false;
  111. }
  112. $Department->status = $status;
  113. $Department->update_time = time();
  114. $Department->save();
  115. return true;
  116. }
  117. /**
  118. * 删除数据
  119. * @author 唐远望
  120. * @version 1.0
  121. * @date 2025-12-04
  122. * @param $id
  123. * @return bool
  124. */
  125. public function deleteDepartment($where)
  126. {
  127. $Department = $this->where($where)->first();
  128. if (!$Department) {
  129. return false;
  130. }
  131. $Department->delete();
  132. return true;
  133. }
  134. }