Employee.php 4.8 KB

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