Employee.php 4.6 KB

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