EmployeeService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Depart;
  4. use App\Model\Employee;
  5. use App\Model\EmployeeDepartPermission;
  6. use App\Model\EmployeeManagerDepart;
  7. use App\Model\EmployeeMenuPermission;
  8. use App\Model\EmployeeRole;
  9. use App\Model\EmployeeTeamPermission;
  10. use App\Model\Role;
  11. use App\Model\SysMenu;
  12. use App\Model\Team;
  13. use Illuminate\Support\Facades\Cache;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Hash;
  16. /**
  17. * 人员相关
  18. * @package App\Models
  19. */
  20. class EmployeeService extends Service
  21. {
  22. public function employeeEdit($data,$user){
  23. list($status,$msg) = $this->employeeRule($data,false);
  24. if(!$status) return [$status,$msg];
  25. $model = new Employee();
  26. $model = $model->where('id',$data['id'])->first();
  27. $model->number = $data['number'] ;
  28. $model->mobile = $data['mobile'];
  29. $model->emp_name = $data['emp_name'];
  30. $model->is_admin = $data['is_admin'];
  31. if($model->is_admin == 1){
  32. $model->account = $data['account'];
  33. if($data['password'] !== '********'){
  34. $model->password = Hash::make($data['password']);
  35. }
  36. }
  37. $model->save();
  38. return [true,'保存成功!'];
  39. }
  40. public function employeeAdd($data,$user){
  41. list($status,$msg) = $this->employeeRule($data);
  42. if(!$status) return [$status,$msg];
  43. $model = new Employee();
  44. $model->number = $data['number'] ;
  45. $model->mobile = $data['mobile'];
  46. $model->emp_name = $data['emp_name'];
  47. $model->state = 1;
  48. $model->crt_id = $user['id'];
  49. $model->is_admin = $data['is_admin'];
  50. if($model->is_admin == 1){
  51. $model->account = $data['account'];
  52. $model->password = Hash::make($data['password']);
  53. }
  54. $model->save();
  55. return [true,'保存成功!'];
  56. }
  57. public function employeeDel($data){
  58. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  59. Employee::where('id',$data['id'])->update([
  60. 'del_time'=>time()
  61. ]);
  62. return [true,'删除成功'];
  63. }
  64. public function employeeList($data){
  65. $list = Employee::where('del_time',0)->select('emp_name','mobile','crt_time','account','is_admin','id')->orderBy('id','desc');
  66. $list = $this->limit($list,'',$data);
  67. return [200,$list];
  68. }
  69. public function employeeRule($data,$is_add = true){
  70. if($this->isEmpty($data,'number')) return [false,'工号不存在!'];
  71. if($this->isEmpty($data,'mobile')) return [false,'手机号不存在!'];
  72. if($this->isEmpty($data,'emp_name')) return [false,'姓名不存在!'];
  73. if(! $is_add){
  74. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  75. $bool = Employee::where('number',$data['number'])
  76. ->where('id','<>',$data['id'])
  77. ->where('del_time',0)->exists();
  78. $bool_account = Employee::where('account',$data['account'])
  79. ->where('id','<>',$data['id'])
  80. ->where('del_time',0)->exists();
  81. }else{
  82. $bool = Employee::where('number',$data['number'])
  83. ->where('del_time',0)->exists();
  84. $bool_account = Employee::where('account',$data['account'])
  85. ->where('del_time',0)->exists();
  86. }
  87. if($bool) return [false,'工号已存在!'];
  88. if($bool_account) return [false,'账号已存在!'];
  89. return [true,''];
  90. }
  91. public function roleEdit($data){
  92. list($status,$msg) = $this->roleRule($data);
  93. if(!$status) return [$status,$msg];
  94. $first = Role::where('title',$data['title'])->where('id','<>',$data['id'])->where('del_time',0)->first();
  95. if(!empty($first))return [false,'名称已存在!'];
  96. $model = new Role();
  97. $model = $model->where('id',$data['id'])->first();
  98. $model->title = $data['title'];
  99. $model->save();
  100. return [true,'保存成功!'];
  101. }
  102. public function roleAdd($data,$user){
  103. list($status,$msg) = $this->roleRule($data);
  104. if(!$status) return [$status,$msg];
  105. $first = Role::where('title',$data['title'])->where('del_time',0)->first();
  106. if(!empty($first))return [false,'名称已存在!'];
  107. $model = new Role();
  108. $model->title = $data['title'] ;
  109. $model->save();
  110. return [true,'保存成功!'];
  111. }
  112. public function roleDel($data){
  113. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  114. Role::where('id',$data['id'])->update([
  115. 'del_time'=>time()
  116. ]);
  117. return [true,'删除成功'];
  118. }
  119. public function roleList($data){
  120. $list = Role::where('del_time',0)->select('title','title','crt_time','id','upd_time')->orderBy('id','desc');
  121. $list = $this->limit($list,'',$data);
  122. return [200,$list];
  123. }
  124. public function roleRule($data){
  125. if($this->isEmpty($data,'title')) return [false,'名称不存在!'];
  126. return [true,''];
  127. }
  128. public function departEdit($data){
  129. list($status,$msg) = $this->departRule($data);
  130. if(!$status) return [$status,$msg];
  131. $first = Depart::where('title',$data['title'])->where('id','<>',$data['id'])->where('del_time',0)->first();
  132. if(!empty($first))return [false,'名称已存在!'];
  133. $model = new Depart();
  134. $model = $model->where('id',$data['id'])->first();
  135. $model->title = $data['title'];
  136. $model->code = $data['code']??'';
  137. $model->save();
  138. return [true,'保存成功!'];
  139. }
  140. public function departAdd($data,$user){
  141. list($status,$msg) = $this->departRule($data);
  142. if(!$status) return [$status,$msg];
  143. $first = Depart::where('title',$data['title'])->where('id','<>',$data['id'])->where('del_time',0)->first();
  144. if(!empty($first))return [false,'名称已存在!'];
  145. $model = new Depart();
  146. $model->title = $data['title'] ;
  147. $model->code = $data['code'] ?? '' ;
  148. $model->save();
  149. return [true,'保存成功!'];
  150. }
  151. public function departDel($data){
  152. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  153. Depart::where('id',$data['id'])->update([
  154. 'del_time'=>time()
  155. ]);
  156. return [true,'删除成功'];
  157. }
  158. public function departList($data){
  159. $list = Depart::where('del_time',0)->select('title','crt_time','id','upd_time','code')->orderBy('id','desc');
  160. $list = $this->limit($list,'',$data);
  161. return [200,$list];
  162. }
  163. public function departRule($data){
  164. if($this->isEmpty($data,'title')) return [false,'名称不存在!'];
  165. return [true,''];
  166. }
  167. public function employeeRole($data){
  168. $role_ids = [];
  169. $employee_ids = [];
  170. foreach ($data as $v){
  171. if(isset($v['role_id'])){
  172. if(!in_array($v['role_id'],$role_ids)){
  173. $role_ids[] = $v['role_id'];
  174. }
  175. }
  176. if(isset($v['employee_id'])){
  177. if(!in_array($v['employee_id'],$employee_ids)){
  178. $employee_ids[] = $v['employee_id'];
  179. }
  180. }
  181. }
  182. EmployeeMenuPermission::wherein('role_id',$role_ids)->delete();
  183. EmployeeMenuPermission::wherein('employee_id',$employee_ids)->delete();
  184. EmployeeMenuPermission::insert($data);
  185. return [200,'保存成功!'];
  186. }
  187. public function employeeDepart($data){
  188. if($this->isEmpty($data,'insert')) return [false,'数据不能为空!'];
  189. DB::beginTransaction();
  190. try {
  191. if($data['type'] == 1){
  192. EmployeeDepartPermission::whereIn('depart_id',$data['insert']['depart_id'])->delete();
  193. }else{
  194. EmployeeDepartPermission::whereIn('employee_id',$data['insert']['employee_id'])->delete();
  195. }
  196. $insert = [];
  197. foreach ($data['insert']['depart_id'] as $t){
  198. foreach ($data['insert']['employee_id'] as $e){
  199. $insert[] = [
  200. 'depart_id' => $t,
  201. 'employee_id' => $e
  202. ];
  203. }
  204. }
  205. EmployeeDepartPermission::insert($insert);
  206. DB::commit();
  207. }catch (\Throwable $exception){
  208. DB::rollBack();
  209. return [false,$exception->getMessage()];
  210. }
  211. return [true,'保存成功!'];
  212. }
  213. public function loginRule($data){
  214. if($this->isEmpty($data,'account')) return [false,'账号不能为空!'];
  215. if($this->isEmpty($data,'password')) return [false,'密码不能为空!'];
  216. $res = Employee::where('del_time',0)
  217. ->where('account', $data['account'])
  218. ->get()->toArray();
  219. if(empty($res)) return [false,'账号不存在或已被删除!'];
  220. $res = reset($res);
  221. if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
  222. if($res['is_admin'] != Employee::IS_ADMIN) return [false,'该账号不能登录!'];
  223. //密码校验
  224. if(! Hash::check($data['password'], $res['password'])) {
  225. $msg = LoginService::errorSetLogin($data['account']);
  226. return [false,$msg];
  227. }
  228. return [true, ['id'=>$res['id'], 'name'=>$res['emp_name'], 'account' => $res['account']]];
  229. }
  230. public static function checkUser($userId){
  231. $res = Employee::where('id', $userId)
  232. ->where('del_time',0)
  233. ->where('is_admin',Employee::IS_ADMIN)
  234. ->where('state',Employee::USE)
  235. ->get()->first();
  236. if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
  237. return [true, $res];
  238. }
  239. //获取登录账号的角色
  240. public static function getPersonRole($employee_id){
  241. if(empty($employee_id)) return [];
  242. $role = EmployeeRole::where('del_time',0)
  243. ->where('employee_id',$employee_id)
  244. ->select('role_id')
  245. ->get()->toArray();
  246. //组织
  247. $role_id = array_column($role,'role_id');
  248. asort($role_id);
  249. $role_id = array_values($role_id);
  250. return $role_id;
  251. }
  252. //获取登录账号的权限部门
  253. public static function getPersonDepart($employee_id){
  254. if(empty($employee_id)) return [];
  255. //admin账号
  256. if($employee_id == Employee::SPECIAL_ADMIN) return [Depart::RULE_DEPART];
  257. //操作人员直接绑定部门
  258. $employee_manager_depart = EmployeeManagerDepart::where('del_time',0)
  259. ->where('employee_id',$employee_id)
  260. ->select('depart_id')
  261. ->get()->toArray();
  262. //操作人员绑定角色
  263. $employee_role = EmployeeRole::from('employee_role as a')
  264. ->leftJoin('role_depart as b','b.role_id','a.role_id')
  265. ->select('b.depart_id','b.role_id')
  266. ->where('a.del_time',0)
  267. ->where('b.del_time',0)
  268. ->where('a.employee_id',$employee_id)
  269. ->get()->toArray();
  270. return array_filter(array_merge_recursive(array_column($employee_manager_depart,'depart_id'),array_column($employee_role,'depart_id')));
  271. }
  272. //人员直接绑定部门
  273. public function employeeManagerDepart($data,$user){
  274. if($user['id'] != Employee::SPECIAL_ADMIN) return [false,'非ADMIN账号不能操作'];
  275. if($this->isEmpty($data,'employee_id')) return [false,'请选择操作人员'];
  276. if($this->isEmpty($data,'depart_id')) return [false,'请选择部门'];
  277. EmployeeManagerDepart::where('employee_id',$data['employee_id'])->update([
  278. 'del_time' => time()
  279. ]);
  280. $insert = [];
  281. foreach ($data['depart_id'] as $value){
  282. $insert[] = [
  283. 'employee_id' => $data['employee_id'],
  284. 'depart_id' => $value,
  285. 'crt_time' => time(),
  286. 'upd_time' => time(),
  287. ];
  288. }
  289. EmployeeManagerDepart::insert($insert);
  290. return [true,''];
  291. }
  292. }