Roles.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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-05
  11. */
  12. class Roles extends Model
  13. {
  14. use HasFactory;
  15. // 与模型关联的表名
  16. protected $table = 'personnel_roles';
  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-05
  27. */
  28. public function addRoles_content($data)
  29. {
  30. $insert_data = [
  31. 'name' => $data['name'],
  32. 'insert_time' => time(),
  33. ];
  34. $Roles_id = $this->insertGetId($insert_data);
  35. return $Roles_id;
  36. }
  37. /**
  38. * 写入数据
  39. * @author 唐远望
  40. * @version 1.0
  41. * @date 2025-12-05
  42. * @param $data
  43. * @return bool
  44. */
  45. public function addRoles($data)
  46. {
  47. DB::beginTransaction();
  48. try {
  49. $this->addRoles_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-05
  64. * @param $data
  65. * @return bool
  66. */
  67. public function editRoles_content($where, $data)
  68. {
  69. $Roles = $this->where($where)->first();
  70. if (!$Roles) {
  71. return false;
  72. }
  73. $Roles->name = $data['name'];
  74. $Roles->update_time = time();
  75. $Roles->save();
  76. return true;
  77. }
  78. /**
  79. * 更新数据
  80. * @author 唐远望
  81. * @version 1.0
  82. * @date 2025-12-05
  83. * @param $data
  84. * @return bool
  85. */
  86. public function updateRoles($where, $data)
  87. {
  88. DB::beginTransaction();
  89. try {
  90. $this->editRoles_content($where, $data);
  91. DB::commit();
  92. return true;
  93. // 成功处理...
  94. } catch (\Exception $e) {
  95. DB::rollBack();
  96. // 错误处理...
  97. return false;
  98. }
  99. }
  100. /**
  101. * 修改状态
  102. * @author 唐远望
  103. * @version 1.0
  104. * @date 2025-12-05
  105. * @param $id
  106. * @param $status
  107. * @return bool
  108. */
  109. public function changeStatus($where, $status)
  110. {
  111. $Roles = $this->where($where)->first();
  112. if (!$Roles) {
  113. return false;
  114. }
  115. $Roles->status = $status;
  116. $Roles->update_time = time();
  117. $Roles->save();
  118. return true;
  119. }
  120. /**
  121. * 删除数据
  122. * @author 唐远望
  123. * @version 1.0
  124. * @date 2025-12-05
  125. * @param $id
  126. * @return bool
  127. */
  128. public function deleteRoles($where)
  129. {
  130. $Roles = $this->where($where)->first();
  131. if (!$Roles) {
  132. return false;
  133. }
  134. $Roles->delete();
  135. return true;
  136. }
  137. }