Employee.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 Employee extends Model
  13. {
  14. use HasFactory;
  15. // 与模型关联的表名
  16. protected $table = 'personnel_employee';
  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 addEmployee_content($data)
  29. {
  30. $insert_data = [
  31. 'name' => $data['name'],
  32. 'mobile' => $data['mobile'],
  33. 'password' => md5($data['password']),
  34. 'role_id' => $data['role_id'],
  35. 'city_ids' => isset($data['city_ids'])? $data['city_ids'] : '',
  36. 'department_id' => $data['department_id'],
  37. 'open_notice' => $data['open_notice'],
  38. 'insert_time' => time(),
  39. ];
  40. $Employee_id = $this->insertGetId($insert_data);
  41. if($Employee_id){
  42. $this->where('id', $Employee_id)->update(['employee_code' => $this->employee_idToCode($Employee_id)]);
  43. }
  44. return $Employee_id;
  45. }
  46. /**
  47. * id转编码
  48. * @author 唐远望
  49. * @version 1.0
  50. * @date 2025-12-04
  51. * @param int $id 编码
  52. *
  53. */
  54. public function employee_idToCode($id)
  55. {
  56. return 'RH' . str_pad($id, 9, '0', STR_PAD_LEFT);
  57. }
  58. /**
  59. * 编码转id
  60. * @author 唐远望
  61. * @version 1.0
  62. * @date 2025-12-04
  63. * @param string $code 编码
  64. *
  65. */
  66. public function employee_codeToId($code)
  67. {
  68. return intval(str_ireplace('RH', '', $code));
  69. }
  70. /**
  71. * 写入数据
  72. * @author 唐远望
  73. * @version 1.0
  74. * @date 2025-12-04
  75. * @param $data
  76. * @return bool
  77. */
  78. public function addEmployee($data)
  79. {
  80. DB::beginTransaction();
  81. try {
  82. $this->addEmployee_content($data);
  83. DB::commit();
  84. return true;
  85. // 成功处理...
  86. } catch (\Exception $e) {
  87. DB::rollBack();
  88. // 错误处理...
  89. return false;
  90. }
  91. }
  92. /**
  93. * 编辑内容
  94. * @author 唐远望
  95. * @version 1.0
  96. * @date 2025-12-04
  97. * @param $data
  98. * @return bool
  99. */
  100. public function editEmployee_content($where, $data)
  101. {
  102. $Employee = $this->where($where)->first();
  103. if (!$Employee) {
  104. return false;
  105. }
  106. $Employee->name = $data['name'];
  107. $Employee->mobile = $data['mobile'];
  108. if(isset($data['password']) && $data['password']!='') $Employee->password = md5($data['password']);
  109. $Employee->role_id = $data['role_id'];
  110. $Employee->city_ids = isset($data['city_ids'])? $data['city_ids'] : '';
  111. $Employee->department_id = $data['department_id'];
  112. $Employee->open_notice = $data['open_notice'];
  113. $Employee->update_time = time();
  114. $Employee->save();
  115. return true;
  116. }
  117. /**
  118. * 更新数据
  119. * @author 唐远望
  120. * @version 1.0
  121. * @date 2025-12-04
  122. * @param $data
  123. * @return bool
  124. */
  125. public function updateEmployee($where, $data)
  126. {
  127. DB::beginTransaction();
  128. try {
  129. $this->editEmployee_content($where, $data);
  130. DB::commit();
  131. return true;
  132. // 成功处理...
  133. } catch (\Exception $e) {
  134. DB::rollBack();
  135. // 错误处理...
  136. return false;
  137. }
  138. }
  139. /**
  140. * 修改状态
  141. * @author 唐远望
  142. * @version 1.0
  143. * @date 2025-12-04
  144. * @param $id
  145. * @param $status
  146. * @return bool
  147. */
  148. public function changeStatus($where, $status)
  149. {
  150. $Employee = $this->where($where)->first();
  151. if (!$Employee) {
  152. return false;
  153. }
  154. $Employee->status = $status;
  155. $Employee->update_time = time();
  156. $Employee->save();
  157. return true;
  158. }
  159. /**
  160. * 删除数据
  161. * @author 唐远望
  162. * @version 1.0
  163. * @date 2025-12-04
  164. * @param $id
  165. * @return bool
  166. */
  167. public function deleteEmployee($where)
  168. {
  169. $Employee = $this->where($where)->first();
  170. if (!$Employee) {
  171. return false;
  172. }
  173. $Employee->delete();
  174. return true;
  175. }
  176. }