EmployeeService.php 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Depart;
  5. use App\Model\DepartIndex;
  6. use App\Model\Employee;
  7. use App\Model\EmployeeDepartPermission;
  8. use App\Model\EmployeeFile;
  9. use App\Model\EmployeeManagerDepart;
  10. use App\Model\EmployeeMenuPermission;
  11. use App\Model\EmployeeRole;
  12. use App\Model\EmployeeTeamPermission;
  13. use App\Model\ProductCategory;
  14. use App\Model\ProductInventory;
  15. use App\Model\ProductInventorySet;
  16. use App\Model\Role;
  17. use App\Model\RoleMenu;
  18. use App\Model\RoleMenuButton;
  19. use App\Model\Storehouse;
  20. use App\Model\SysMenu;
  21. use App\Model\SysMenuButton;
  22. use App\Model\Team;
  23. use App\Model\WxEmployeeOfficial;
  24. use App\Service\Weixin\WeixinService;
  25. use Illuminate\Support\Facades\DB;
  26. use Illuminate\Support\Facades\Hash;
  27. use Mockery\Exception;
  28. /**
  29. * 人员相关
  30. * @package App\Models
  31. */
  32. class EmployeeService extends Service
  33. {
  34. public function employeeEditImg($data, $user){
  35. $time = time();
  36. try {
  37. DB::beginTransaction();
  38. $old = EmployeeFile::where('del_time',0)
  39. ->where('employee_id',$user['id'])
  40. ->select('file')
  41. ->get()->toArray();
  42. $old = array_column($old,'file');
  43. EmployeeFile::where('del_time',0)
  44. ->where('employee_id',$user['id'])
  45. ->update(['del_time' => $time]);
  46. $new = [];
  47. $insert = [];
  48. if(! empty($data['img_url'])){
  49. $insert[] = [
  50. 'employee_id' => $user['id'],
  51. 'file' => $data['img_url'],
  52. 'crt_time' => $time,
  53. ];
  54. EmployeeFile::insert($insert);
  55. $new[] = $data['img_url'];
  56. }
  57. DB::commit();
  58. }catch (\Throwable $exception){
  59. DB::rollBack();
  60. return [false, $exception->getMessage()];
  61. }
  62. return [true, ['file' => ['new' => $new, 'old' => $old]]];
  63. }
  64. public function getEmployeeImg($user){
  65. $file = EmployeeFile::where('del_time',0)
  66. ->where('employee_id',$user['id'])
  67. ->select('file')
  68. ->get()->toArray();
  69. $file = array_column($file,'file');
  70. $file = $file[0] ?? "";
  71. $img_str = "";
  72. $timpstamp = 86400;
  73. if(! empty($file)){
  74. $fileUploadService = new FileUploadService();
  75. $img_str = $fileUploadService->getFileShow($file, $timpstamp);
  76. }
  77. return [true, ['img_url' => $img_str]];
  78. }
  79. public function employeeEditOther($data,$user){
  80. list($status,$msg) = $this->employeeOtherRule($data,$user);
  81. if(!$status) return [$status,$msg];
  82. try {
  83. DB::beginTransaction();
  84. $model = new Employee();
  85. $model = $model->where('id',$user['id'])->first();
  86. $model->password = Hash::make($data['new_password']);
  87. $model->save();
  88. DB::commit();
  89. }catch (\Exception $exception){
  90. DB::rollBack();
  91. return [false, $exception->getMessage()];
  92. }
  93. return [true,''];
  94. }
  95. public function employeeOtherRule($data,$user){
  96. if(! isset($data['old_password'])) return [false,'请输入原密码'];
  97. if($data['old_password'] == "") return [false,'原密码不能为空'];
  98. if(! isset($data['new_password'])) return [false,'请输入新密码'];
  99. if($data['new_password'] == "") return [false,'新密码不能为空'];
  100. if(! isset($data['re_password'])) return [false,'请输入确认密码'];
  101. if($data['re_password'] == "") return [false,'确认密码不能为空'];
  102. if(! Hash::check($data['old_password'], $user['password'])) return [false,'原密码错误'];
  103. if($data['new_password'] == $data['old_password']) return [false,'原密码与新密码一致'];
  104. if($data['new_password'] !== $data['re_password']) return [false,'新密码与确认密码不一致'];
  105. return [true,''];
  106. }
  107. /**
  108. * 用户编辑
  109. * @param $data
  110. * @param $user
  111. * @return array
  112. */
  113. public function employeeEdit($data,$user){
  114. list($status,$msg) = $this->employeeRule($data,false);
  115. if(!$status) return [$status,$msg];
  116. try {
  117. DB::beginTransaction();
  118. $model = new Employee();
  119. $model = $model->where('id',$data['id'])->first();
  120. $model->number = $data['number'];
  121. $model->emp_name = $data['emp_name'];
  122. $model->mobile = $data['mobile'] ?? '';
  123. $model->leave_time = $data['leave_time'] ?? '';
  124. $model->entry_time = $data['entry_time'] ?? '';
  125. $model->state = empty($data['leave_time']) ? Employee::USE : Employee::NOT_USE;
  126. $model->is_admin = $data['is_admin'];
  127. $model->account = $data['number'];
  128. if($model->is_admin == 1){
  129. if($data['password'] !== '******'){
  130. $model->password = Hash::make($data['password']);
  131. }
  132. }
  133. $model->save();
  134. EmployeeDepartPermission::where('employee_id',$data['id'])->delete();
  135. if(isset($data['depart'])){
  136. $insert = [];
  137. foreach ($data['depart'] as $value){
  138. $insert[] = [
  139. 'employee_id' => $model->id,
  140. 'depart_id' => $value,
  141. ];
  142. }
  143. EmployeeDepartPermission::insert($insert);
  144. }
  145. EmployeeRole::where('employee_id',$data['id'])->update([
  146. 'del_time' => time()
  147. ]);
  148. if(isset($data['role'])){
  149. $insert = [];
  150. foreach ($data['role'] as $value){
  151. $insert[] = [
  152. 'employee_id' => $model->id,
  153. 'role_id' => $value,
  154. 'crt_time' => time(),
  155. 'upd_time' => time(),
  156. ];
  157. }
  158. EmployeeRole::insert($insert);
  159. }
  160. DB::commit();
  161. }catch (\Exception $exception){
  162. DB::rollBack();
  163. return [false, $exception->getMessage()];
  164. }
  165. return [true,''];
  166. }
  167. /**
  168. * 用户新增
  169. * @param $data
  170. * @param $user
  171. * @return array
  172. */
  173. public function employeeAdd($data,$user){
  174. list($status,$msg) = $this->employeeRule($data);
  175. if(!$status) return [$status,$msg];
  176. try{
  177. DB::beginTransaction();
  178. $model = new Employee();
  179. $model->number = $data['number'];
  180. $model->emp_name = $data['emp_name'];
  181. $model->mobile = $data['mobile'] ?? '';
  182. $model->leave_time = $data['leave_time'] ?? '';
  183. $model->entry_time = $data['entry_time'] ?? '';
  184. $model->state = empty($data['leave_time']) ? Employee::USE : Employee::NOT_USE;
  185. $model->crt_id = $user['id'];
  186. $model->is_admin = $data['is_admin'];
  187. $model->account = $data['number'];
  188. if($model->is_admin == 1){
  189. if($data['password'] !== '********'){
  190. $model->password = Hash::make($data['password']);
  191. }
  192. }
  193. $model->save();
  194. if(isset($data['depart'])){
  195. $insert = [];
  196. foreach ($data['depart'] as $value){
  197. $insert[] = [
  198. 'employee_id' => $model->id,
  199. 'depart_id' => $value,
  200. ];
  201. }
  202. EmployeeDepartPermission::insert($insert);
  203. }
  204. if(isset($data['role'])){
  205. $insert = [];
  206. foreach ($data['role'] as $value){
  207. $insert[] = [
  208. 'employee_id' => $model->id,
  209. 'role_id' => $value,
  210. 'crt_time' => time(),
  211. 'upd_time' => time(),
  212. ];
  213. }
  214. EmployeeRole::insert($insert);
  215. }
  216. DB::commit();
  217. }catch (Exception $e){
  218. DB::rollBack();
  219. return [false, $e->getMessage()];
  220. }
  221. return [true,''];
  222. }
  223. /**
  224. * 用户删除
  225. * @param $data
  226. * @return array
  227. */
  228. public function employeeDel($data){
  229. if($this->isEmpty($data,'id')) return [false,'请选择删除的数据!'];
  230. Employee::whereIn('id',$data['id'])->update([
  231. 'del_time'=>time()
  232. ]);
  233. EmployeeRole::where('del_time',0)->whereIn('employee_id',$data['id'])->update([
  234. 'del_time'=>time()
  235. ]);
  236. EmployeeDepartPermission::whereIn('employee_id',$data['id'])->delete();
  237. return [true,'删除成功'];
  238. }
  239. /**
  240. * 用户列表
  241. * @param $data
  242. * @param $user
  243. * @return array
  244. */
  245. public function employeeList($data,$user){
  246. $model = Employee::where('del_time',0)
  247. ->select('number','mobile','emp_name','id','entry_time','leave_time','is_admin','state')
  248. ->orderBy('id','desc');
  249. if(! empty($data['depart'])) {
  250. $depart = Depart::where('del_time',0)
  251. ->select('id','parent_id')
  252. ->get()->toArray();
  253. $result = array_merge($this->getAllDescendants($depart,$data['depart']),[$data['depart']]);
  254. $employee_id = DB::table('employee_depart_permission')
  255. ->whereIn("depart_id", $result)
  256. ->select("employee_id")
  257. ->get()->toArray();
  258. $employee_id = array_column($employee_id,'employee_id');
  259. $model->whereIn("id", $employee_id);
  260. }else{
  261. if(! $user['is_all_depart'] && empty($data['is_top_qx'])){
  262. $employee_id = $this->getEmployee($user);
  263. $model->whereIn('id',$employee_id);
  264. }
  265. }
  266. if(! empty($data['number'])) $model->where('number', 'LIKE', '%'.$data['number'].'%');
  267. if(! empty($data['emp_name'])) $model->where('emp_name', 'LIKE', '%'.$data['emp_name'].'%');
  268. if(! empty($data['state'])) $model->where('state',$data['state']);
  269. if(! empty($data['mobile'])) $model->where('mobile', 'LIKE', '%'.$data['mobile'].'%');
  270. if(! isset($data['all_emp'])) $model->where('id','<>',Employee::SPECIAL_ADMIN);
  271. if(! empty($data['role'])) {
  272. $emp = EmployeeRole::where('role_id',$data['role'])
  273. ->where('del_time',0)
  274. ->select('employee_id')->get()->toArray();
  275. $model->whereIn('id',array_column($emp,'employee_id'));
  276. }
  277. if($user['id'] != Employee::SPECIAL_ADMIN) $model->where('is_manager',0);
  278. $list = $this->limit($model,'',$data);
  279. //组织数据
  280. $list = $this->organizationEmployeeData($list);
  281. return [true , $list];
  282. }
  283. /**
  284. * 用户数据组装
  285. * @param $data
  286. * @return array
  287. */
  288. public function organizationEmployeeData($data) {
  289. if (empty($data['data'])) return $data;
  290. $res = DB::table('employee_role as a')
  291. ->leftJoin('role as b','a.role_id','=','b.id')
  292. ->where('a.del_time',0)
  293. ->where('b.del_time',0)
  294. ->whereIn("a.employee_id",array_column($data['data'],'id'))
  295. ->select('a.employee_id','b.title','b.id')
  296. ->get()->toArray();
  297. $role = $role2 = [];
  298. foreach ($res as $value){
  299. if(isset($role[$value->employee_id])){
  300. $role[$value->employee_id] .= ',' . $value->title;
  301. }else{
  302. $role[$value->employee_id] = $value->title;
  303. }
  304. $role2[$value->employee_id][] = $value->id;
  305. }
  306. $map = $this->getTopDepartSon();
  307. $res = DB::table('employee_depart_permission as a')
  308. ->select('a.employee_id','b.title','b.id')
  309. ->join('depart as b','a.depart_id','=','b.id')
  310. ->whereIn("a.employee_id",array_column($data['data'],'id'))
  311. ->orderBy('b.id')
  312. ->get()->toArray();
  313. $depart_title = $depart_id = $man_top_depart = [];
  314. foreach ($res as $value){
  315. if(isset($depart_title[$value->employee_id])){
  316. $depart_title[$value->employee_id] .= ',' . $value->title;
  317. }else{
  318. $depart_title[$value->employee_id] = $value->title;
  319. }
  320. $depart_id[$value->employee_id][] = $value->id;
  321. $tmp = $map[$value->id] ?? [];
  322. if(empty($tmp)) continue;
  323. $key = $tmp['id'] . $tmp['title'];
  324. if(! isset($man_top_depart[$value->employee_id][$key])) {
  325. $man_top_depart[$value->employee_id][$key] = $map[$value->id];
  326. }
  327. }
  328. foreach ($man_top_depart as $key => $value){
  329. $man_top_depart[$key] = array_values($value);
  330. }
  331. $wx = WxEmployeeOfficial::where('appid',WeixinService::APPID)
  332. ->where('employee_id','>',0)
  333. ->select('employee_id')
  334. ->get()->toArray();
  335. $wx = array_column($wx,'employee_id');
  336. foreach ($data['data'] as $key => $value){
  337. $data['data'][$key]['role'] = $role2[$value['id']] ?? [];
  338. $data['data'][$key]['role_name'] = $role[$value['id']] ?? '';
  339. $data['data'][$key]['depart'] = $depart_id[$value['id']] ?? [];
  340. $data['data'][$key]['depart_title'] = $depart_title[$value['id']] ?? '';
  341. $is_wx = "未绑定微信公众号";
  342. if(in_array($value['id'], $wx)) $is_wx = "已绑定微信公众号";
  343. $data['data'][$key]['is_wx'] = $is_wx;
  344. $data['data'][$key]['my_top'] = $man_top_depart[$value['id']] ?? [];
  345. }
  346. return $data;
  347. }
  348. //门店下所有子集
  349. public function getTopDepartSon(){
  350. $departList = Depart::where('del_time',0)
  351. ->select('id','parent_id','title')
  352. ->get()->toArray();
  353. $map = [];
  354. foreach ($departList as $value){
  355. if($value['parent_id'] == 0){
  356. $result = array_merge($this->getAllDescendants($departList,$value['id']),[$value['id']]);
  357. foreach ($result as $val){
  358. $map[$val] = [
  359. 'id' => $value['id'],
  360. 'title' => $value['title'],
  361. ];
  362. }
  363. }
  364. }
  365. return $map;
  366. }
  367. //获取当前顶级部门下人员id
  368. public function getEmployee($user){
  369. $top_depart_id = $user['depart_top'][0] ?? [];
  370. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  371. $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();
  372. // 查找所有子级id
  373. $childIds = $this->findChildIds($top_depart_id, $list);
  374. $childIds[] = $top_depart_id;
  375. $employee_id = EmployeeDepartPermission::whereIn('depart_id',$childIds)
  376. ->select("employee_id")
  377. ->get()->toArray();
  378. return array_unique(array_column($employee_id,'employee_id'));
  379. }
  380. /**
  381. * 用户参数规则
  382. * @param $data
  383. * @param $is_add
  384. * @return array
  385. */
  386. public function employeeRule($data,$is_add = true){
  387. if($this->isEmpty($data,'number')) return [false,'工号不存在!'];
  388. if($this->isEmpty($data,'emp_name')) return [false,'姓名不存在!'];
  389. if(empty($data['depart'])) return [false,'部门不能为空'];
  390. $mobile = $data['mobile'] ?? "";
  391. $number = $data['number'] ?? "";
  392. if(! $is_add){
  393. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  394. $bool = Employee::where('del_time',0)
  395. ->where('id','<>',$data['id'])
  396. ->where(function ($query) use ($mobile, $number){
  397. $query->where('number', $number);
  398. $query->when(! empty($mobile), function ($query) use ($mobile) {
  399. return $query->orWhere('mobile', $mobile);
  400. });
  401. })->exists();
  402. }else{
  403. $bool = Employee::where('del_time',0)
  404. ->where(function ($query) use ($mobile, $number){
  405. $query->where('number', $number);
  406. $query->when(! empty($mobile), function ($query) use ($mobile) {
  407. return $query->orWhere('mobile', $mobile);
  408. });
  409. })->exists();
  410. }
  411. if($bool) return [false,'工号或手机号码已存在!'];
  412. return [true,''];
  413. }
  414. /**
  415. * 角色编辑
  416. * @param $data
  417. * @return array
  418. */
  419. public function roleEdit($data,$user){
  420. list($status,$msg) = $this->roleRule($data,$user, false);
  421. if(!$status) return [$status,$msg];
  422. $model = new Role();
  423. $model = $model->where('id',$data['id'])->first();
  424. $model->title = $data['title'];
  425. $model->save();
  426. return [true,''];
  427. }
  428. /**
  429. * 角色新增
  430. * @param $data
  431. * @param $user
  432. * @return array
  433. */
  434. public function roleAdd($data,$user){
  435. list($status,$msg) = $this->roleRule($data,$user);
  436. if(!$status) return [$status,$msg];
  437. $model = new Role();
  438. $model->title = $data['title'] ;
  439. $model->depart_id = $data['depart_id'] ?? 0;
  440. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  441. $model->save();
  442. return [true,''];
  443. }
  444. /**
  445. * 角色删除
  446. * @param $data
  447. * @return array
  448. */
  449. public function roleDel($data){
  450. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  451. $bool = EmployeeRole::where('del_time',0)
  452. ->whereIn('role_id',$data['id'])
  453. ->exists();
  454. if($bool) return [false,'角色已绑定人员!'];
  455. Role::where('id',$data['id'])->update([
  456. 'del_time' => time()
  457. ]);
  458. RoleMenu::where('del_time',0)->where('role_id',$data['id'])->update([
  459. 'del_time' => time()
  460. ]);
  461. RoleMenuButton::where('del_time',0)->where('role_id',$data['id'])->update([
  462. 'del_time' => time()
  463. ]);
  464. return [true, ''];
  465. }
  466. /**
  467. * 角色列表
  468. * @param $data
  469. * @return array
  470. */
  471. public function roleList($data,$user){
  472. $model = Role::TopClear($user,$data);
  473. $model = $model->where('del_time',0)
  474. ->select('title','crt_time','id','upd_time')
  475. ->orderBy('id','desc');
  476. if(! empty($data['title'])) $model->where('title', 'LIKE', '%' . $data['title'] . '%');
  477. $list = $this->limit($model,'',$data);
  478. return [true, $list];
  479. }
  480. /**
  481. * 角色参数规则
  482. * @param $data
  483. * @param $is_check
  484. * @return array
  485. */
  486. public function roleRule(&$data,$user, $is_check = true){
  487. if($this->isEmpty($data,'title')) return [false,'名称不能为空!'];
  488. //所属部门 以及 顶级部门
  489. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  490. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  491. if($is_check){
  492. $bool = Role::where('title',$data['title'])
  493. ->where('top_depart_id',$data['top_depart_id'])
  494. ->where('del_time',0)
  495. ->exists();
  496. if($bool) return [false,'角色名称已存在!'];
  497. }else{
  498. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  499. $top_depart_id = Role::where('id',$data['id'])->value('top_depart_id');
  500. $bool = Role::where('title',$data['title'])
  501. ->where('top_depart_id',$top_depart_id)
  502. ->where('id','<>',$data['id'])
  503. ->where('del_time',0)
  504. ->exists();
  505. if($bool) return [false,'角色名称已存在!'];
  506. }
  507. return [true, ''];
  508. }
  509. /**
  510. * 角色菜单更新
  511. * @param $data
  512. * @return array
  513. */
  514. public function roleMenu($data){
  515. if(empty($data['role_id'])) return [false,'角色不能为空!'];
  516. if(empty($data['menu'])) return [false,'菜单数据不能为空!'];
  517. DB::beginTransaction();
  518. try {
  519. RoleMenu::where('del_time',0)->where('role_id',$data['role_id'])->update(['del_time' => time()]);
  520. RoleMenuButton::where('del_time',0)->where('role_id',$data['role_id'])->update(['del_time' => time()]);
  521. $insert = $insert2 = [];
  522. foreach ($data['menu'] as $t){
  523. $insert[] = [
  524. 'role_id' => $data['role_id'],
  525. 'menu_id' => $t['menu_id'],
  526. 'type' => $t['type'],
  527. 'crt_time' => time()
  528. ];
  529. if(! empty($t['button'])){
  530. foreach ($t['button'] as $b){
  531. $insert2[] = [
  532. 'role_id' => $data['role_id'],
  533. 'menu_id' => $t['menu_id'],
  534. 'button_id' => $b,
  535. 'crt_time' => time()
  536. ];
  537. }
  538. RoleMenuButton::insert($insert2);
  539. }
  540. }
  541. RoleMenu::insert($insert);
  542. DB::commit();
  543. }catch (\Throwable $exception){
  544. DB::rollBack();
  545. return [false,$exception->getMessage()];
  546. }
  547. return [true, ''];
  548. }
  549. /**
  550. * 角色详情
  551. * @param $data
  552. * @return array
  553. */
  554. public function roleDetail($data){
  555. if(empty($data['role_id'])) return [false,'请选择角色'];
  556. $role = Role::where('id',$data['role_id'])
  557. ->where('del_time',0)
  558. ->select('id','title')
  559. ->first();
  560. if(empty($role)) return [false,'角色不存在或已被删除'];
  561. $role = $role->toArray();
  562. $menu = RoleMenu::where('role_id',$data['role_id'])
  563. ->where('del_time',0)
  564. ->select('menu_id','type')
  565. ->get()->toArray();
  566. $button = $this->fillRoleButton([$data['role_id']]);
  567. foreach ($menu as $key => $value){
  568. $menu[$key]['button'] = $button[$value['menu_id']] ?? [];
  569. }
  570. $role['menu'] = $menu;
  571. return [true, $role];
  572. }
  573. public function departSetIndex($data,$user){
  574. if(empty($data['data'])) return [false, '指标数据不能为空'];
  575. $time = time();
  576. $insert = [];
  577. foreach ($data['data'] as $value){
  578. if(empty($value['top_depart_id'])) return [false, '请选择门店'];
  579. if(! isset($value['param_one'])) return [false, '请填写销售额'];
  580. if(floatval($value['param_one']) == 0.0) return [false,'销售额不能为空'];
  581. $insert[] = [
  582. 'top_depart_id' => $value['top_depart_id'],
  583. 'param_one' => $value['param_one'],
  584. 'crt_time' => $time,
  585. ];
  586. }
  587. if(empty($insert)) return [true, ''];
  588. //删除指标
  589. DepartIndex::where('del_time',0)
  590. ->update(['del_time' => $time]);
  591. //写入指标
  592. DepartIndex::insert($insert);
  593. return [true, ''];
  594. }
  595. /**
  596. * 部门编辑
  597. * @param $data
  598. * @return array
  599. */
  600. public function departEdit($data, $user){
  601. list($status,$msg) = $this->departRule($data,$user,false);
  602. if(!$status) return [$status,$msg];
  603. $update = $msg['data'][0];
  604. $model = new Depart();
  605. $model->where('id',$data['id'])->update($update);
  606. Storehouse::where('del_time',0)
  607. ->where('top_depart_id',$data['id'])
  608. ->update(['title' => $update['title']]);
  609. return [true, ''];
  610. }
  611. /**
  612. * 部门新增
  613. * @param $data
  614. * @param $user
  615. * @return array
  616. */
  617. public function departAdd($data,$user){
  618. list($status,$msg) = $this->departRule($data,$user);
  619. if(!$status) return [$status,$msg];
  620. try {
  621. DB::beginTransaction();
  622. $time = time();
  623. foreach ($msg['data'] as $value){
  624. $model = new Depart();
  625. $model->parent_id = $value['parent_id'];
  626. $model->title = $value['title'];
  627. $model->code = $value['code'];
  628. $model->is_main = $value['is_main'];
  629. $model->basic_type_id = $value['basic_type_id'] ?? 0;
  630. $model->rate = $value['rate'] ?? 0;
  631. $model->notify_id = $value['notify_id'] ?? 0;
  632. $model->area = $value['area'] ?? 0;
  633. $model->province = $value['province'] ?? "";
  634. $model->save();
  635. $depart_id = $model->id;
  636. if(empty($depart_id)) {
  637. DB::rollBack();
  638. return [false,'部门新建失败'];
  639. }
  640. if(empty($value['parent_id'])){
  641. $m = new Storehouse();
  642. $m->title = $value['title'];
  643. $m->depart_id = $depart_id;
  644. $m->top_depart_id = $depart_id;
  645. $m->crt_id = $user['id'];
  646. $m->save();
  647. if(empty($m->id)) {
  648. DB::rollBack();
  649. return [false,'仓库生成失败'];
  650. }
  651. $employee = new Employee();
  652. $number = "admin" . $value['code'];
  653. $employee->number = $number;
  654. $employee->emp_name = $value['title'] . "管理员账号";
  655. $employee->entry_time = date('Y-m-d');
  656. $employee->state = 1;
  657. $employee->crt_id = $user['id'];
  658. $employee->is_admin = 1;
  659. $employee->account = $number;
  660. $employee->password = Hash::make("password");
  661. $employee->is_manager = 1;
  662. $employee->save();
  663. if(empty($employee->id)) {
  664. DB::rollBack();
  665. return [false,'管理员账号生成失败'];
  666. }
  667. $depart = new EmployeeDepartPermission();
  668. $depart->employee_id = $employee->id;
  669. $depart->depart_id = $depart_id;
  670. $depart->save();
  671. if(empty($depart->id)) {
  672. DB::rollBack();
  673. return [false,'管理员账号部门关联生成失败'];
  674. }
  675. //公司系统设置
  676. ProductInventorySet::insert(['top_depart_id' => $depart_id, 'param_one' => 1, 'crt_time' => $time]);
  677. //基础设置 产品分类 默认
  678. $this->createBasicType($depart_id);
  679. // if(! empty($value['is_main'])){
  680. // //总供应商 所有分社可见
  681. // Supplier::insert([
  682. // 'title' => $value['title'],
  683. // 'crt_id' => $user['id'],
  684. // 'crt_time' => $time,
  685. // 'depart_id' => $depart_id,
  686. // 'top_depart_id' => $depart_id,
  687. // 'is_main' => Supplier::is_main,
  688. // ]);
  689. // }
  690. }
  691. }
  692. DB::commit();
  693. }catch (\Exception $exception){
  694. DB::rollBack();
  695. return [false,$exception->getMessage()];
  696. }
  697. return [true,'保存成功!'];
  698. }
  699. private function createBasicType($depart_id = 0){
  700. $insert = [];
  701. $time = time();
  702. foreach (BasicType::$type as $key => $value){
  703. $insert[] = [
  704. 'title' => '默认',
  705. 'type' => $key,
  706. 'depart_id' => $depart_id,
  707. 'top_depart_id' => $depart_id,
  708. 'crt_time' => $time
  709. ];
  710. }
  711. BasicType::insert($insert);
  712. ProductCategory::insert([
  713. 'title' => '默认',
  714. 'parent_id' => 0,
  715. 'crt_time' => $time,
  716. 'depart_id' => $depart_id,
  717. 'top_depart_id' => $depart_id,
  718. ]);
  719. }
  720. /**
  721. * 部门删除
  722. * @param $data
  723. * @return array
  724. */
  725. public function departDel($data){
  726. list($status,$msg) = $this->checkDepartDel($data);
  727. if(! $status) return [false, $msg];
  728. Depart::whereIn('id',$data['id'])->update([
  729. 'del_time'=>time()
  730. ]);
  731. return [true,'删除成功'];
  732. }
  733. /**
  734. * 判断部门是否可以删除
  735. * @param $data
  736. * @return array
  737. */
  738. public function checkDepartDel($data){
  739. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  740. $bool = Depart::whereIn('parent_id',$data['id'])->where('del_time',0)->exists();
  741. if($bool) return [false,'部门下有子部门!'];
  742. if($this->checkDepartHasPerson($data['id'])) return [false,'部门下有人员档案!'];
  743. $bool = ProductInventory::whereIn('top_depart_id',$data['id'])->where('del_time',0)->exists();
  744. if($bool) return [false,'部门下的仓库有产品信息!'];
  745. return [true, ''];
  746. }
  747. /**
  748. * 部门列表
  749. * @param $data
  750. * @param $user
  751. * @return array
  752. */
  753. public function departList($data,$user){
  754. $model = Depart::where('del_time',0)
  755. ->select('title','id','code','parent_id','is_main','basic_type_id','rate','notify_id','channel_id','area','province')
  756. ->orderby('id', 'asc');
  757. if(! empty($data['get_top']) && $data['get_top'] == 1){
  758. //指派销售
  759. $depart_id = $this->getDepartIdListOfMySales($user);
  760. $model->whereIn('id', $depart_id);
  761. }else{
  762. if(! $user['is_all_depart'] && empty($data['is_top_qx'])){
  763. $depart_id = $this->getDepartIdList($user);
  764. $model->whereIn('id',$depart_id);
  765. }
  766. }
  767. if(isset($data['parent_id'])) $model->where('parent_id', $data['parent_id']);
  768. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  769. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  770. $list = $model->get()->toArray();
  771. $list = $this->fillDepartList($list, $user);
  772. $list_tree = $list;
  773. if(! empty($list_tree)) {
  774. $list_tree = $this->makeTree(0,$list_tree);
  775. $list_tree = $this->set_sort_circle($list_tree);
  776. }
  777. return [200,['data' => $list,'tree' => $list_tree]];
  778. }
  779. public function fillDepartList($list,$user){
  780. if(empty($list)) return $list;
  781. $basic = BasicType::where('del_time',0)
  782. ->whereIn('id', array_unique(array_column($list,'basic_type_id')))
  783. ->pluck('title','id')->toArray();
  784. $emp_id = array_unique(array_merge_recursive(array_column($list,'notify_id'),array_column($list,'channel_id')));
  785. $emp_map = Employee::whereIn('id',$emp_id)
  786. ->pluck('emp_name','id')
  787. ->toArray();
  788. //省
  789. $address_map = config('address');
  790. $address_map = array_column($address_map,'label','value');
  791. foreach ($list as $key => $value){
  792. $list[$key]['basic_type_title'] = $basic[$value['basic_type_id']] ?? '';
  793. $list[$key]['is_show_basic_type'] = $user['is_all_depart'];
  794. $list[$key]['notify_name'] = $emp_map[$value['notify_id']] ?? '';
  795. $list[$key]['channel_name'] = $emp_map[$value['channel_id']] ?? '';
  796. $list[$key]['area_name'] = Depart::$area[$value['area']] ?? '';
  797. $list[$key]['province_name'] = $address_map[$value['province']] ?? '';
  798. }
  799. return $list;
  800. }
  801. //获取可见的部门范围
  802. public function getDepartIdList($user){
  803. $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();
  804. $result = [];
  805. foreach ($user['depart_range'] as $v){
  806. // 查找所有父级id
  807. $parentIds = $this->findParentIds($v, $list);
  808. // 查找所有子级id
  809. $childIds = $this->findChildIds($v, $list);
  810. // 合并父级和子级id
  811. $tmp = array_merge($parentIds, $childIds, [$v]);
  812. $result = array_merge($result,$tmp);
  813. }
  814. return array_unique($result);
  815. }
  816. //获取指派销售时的部门
  817. public function getDepartIdListOfMySales($user){
  818. $top = $user['depart_top'][0] ?? [];
  819. $top = $top['depart_id'] ?? 0;
  820. $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();
  821. // 查找所有子级id
  822. $childIds = $this->findChildIds($top, $list);
  823. $result = array_merge($childIds, [$top]);
  824. return $result;
  825. }
  826. /**
  827. * 部门参数规则
  828. * @param $data
  829. * @param $is_check
  830. * @return array
  831. */
  832. public function departRule($data,$user, $is_check = true){
  833. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  834. $code = array_column($data['data'],'code');
  835. $title = array_column($data['data'],'title');
  836. $code = array_map(function($val) {
  837. return $val !== null ? $val : 0;
  838. }, $code);
  839. $title = array_map(function($val) {
  840. return $val !== null ? $val : 0;
  841. }, $title);
  842. $code_count = array_count_values($code);
  843. $title_count = array_count_values($title);
  844. foreach ($code as $value){
  845. if(empty($value)) return [false,'编码不能为空!'];
  846. if($code_count[$value] > 1) return [false,'编码不能重复'];
  847. }
  848. foreach ($title as $value){
  849. if(empty($value)) return [false,'名称不能为空!'];
  850. if($title_count[$value] > 1) return [false,'名称不能重复'];
  851. }
  852. $count = 0;
  853. foreach ($data['data'] as $value){
  854. if(empty($value['parent_id']) && ! empty($value['is_main'])) $count ++;
  855. }
  856. if($count > 1) return [false,'总社已存在!'];
  857. if($count == 1){
  858. $id = $data['id'] ?? 0;
  859. $bool = Depart::where('del_time',0)
  860. ->where('parent_id',0)
  861. ->where('is_main',1)
  862. ->when(! empty($id), function ($query) use ($id) {
  863. return $query->where('id', '<>',$id);
  864. })
  865. ->exists();
  866. if($bool) return [false,'总社已存在!'];
  867. }
  868. //省
  869. $address_map = config('address');
  870. $address_map = array_column($address_map,'label','value');
  871. foreach ($data['data'] as $key => $value){
  872. if(! empty($value['area'])){
  873. $area = array_keys(Depart::$area);
  874. if(! in_array($value['area'], $area)) return [false, '门店所属大区不存在'];
  875. }
  876. if(! empty($value['province']) && ! isset($address_map[$value['province']])) return [false, '门店所属省份不存在'];
  877. if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0;
  878. $data['data'][$key]['upd_time'] = time();
  879. //Depart::whereRaw("(binary code = '{$value['code']}' OR title = '{$value['title']}')")
  880. if($is_check){
  881. if(empty($user['is_all_depart']) && empty($value['parent_id'])) return [false,'上级部门必须选择'];
  882. $data['data'][$key]['crt_time'] = time();
  883. $bool = Depart::whereRaw("binary code = '{$value['code']}'")
  884. ->where('del_time',0)
  885. ->exists();
  886. }else{
  887. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  888. if($data['id'] == $user['head']['id'] && empty($data['is_main'])) return [false,'总社不支持变更!'];
  889. $bool = Depart::whereRaw("binary code = '{$value['code']}'")
  890. ->where('id','<>',$data['id'])
  891. ->where('del_time',0)
  892. ->exists();
  893. }
  894. if($bool) return [false,'编码不能重复'];
  895. }
  896. return [true, $data];
  897. }
  898. /**
  899. * 检测部门下是否存在人员
  900. * @param $depart_id
  901. * @return false
  902. */
  903. public function checkDepartHasPerson($depart_id = []){
  904. if(empty($depart_id)) return false;
  905. $bool = EmployeeDepartPermission::from('employee_depart_permission as a')
  906. ->leftJoin('employee as b','b.id','a.employee_id')
  907. ->where('b.del_time',0)
  908. ->whereIn('a.depart_id',$depart_id)
  909. ->exists();
  910. return $bool;
  911. }
  912. /**
  913. * 班组编辑
  914. * @param $data
  915. * @return array
  916. */
  917. public function teamEdit($data){
  918. list($status,$msg) = $this->teamRule($data,false);
  919. if(!$status) return [$status,$msg];
  920. $model = new Team();
  921. $model = $model->where('id',$data['id'])->first();
  922. $model->title = $data['title'];
  923. $model->code = $data['code'];
  924. $model->save();
  925. return [true,'保存成功!'];
  926. }
  927. /**
  928. * 班组新增
  929. * @param $data
  930. * @param $user
  931. * @return array
  932. */
  933. public function teamAdd($data,$user){
  934. list($status,$msg) = $this->teamRule($data);
  935. if(!$status) return [$status,$msg];
  936. $model = new Team();
  937. $model->title = $data['title'] ;
  938. $model->code = $data['code'];
  939. $model->save();
  940. return [true,'保存成功!'];
  941. }
  942. /**
  943. * 班组删除
  944. * @param $data
  945. * @return array
  946. */
  947. public function teamDel($data){
  948. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  949. Team::where('id',$data['id'])->update([
  950. 'del_time'=>time()
  951. ]);
  952. return [true,'删除成功'];
  953. }
  954. /**
  955. * 班组列表
  956. * @param $data
  957. * @return array
  958. */
  959. public function teamList($data){
  960. $list = Team::where('del_time',0)
  961. ->select('title','id','crt_time','upd_time','code')
  962. ->orderBy('id','desc');
  963. $list = $this->limit($list,'',$data);
  964. return [200,$list];
  965. }
  966. /**
  967. * 班组参数规则
  968. * @param $data
  969. * @param $is_add
  970. * @return array
  971. */
  972. public function teamRule($data,$is_add = true){
  973. if($this->isEmpty($data,'title')) return [false,'名称不存在!'];
  974. if($this->isEmpty($data,'code')) return [false,'编码不存在'];
  975. $model = Team::where('title',$data['title'])
  976. ->where('code',$data['code'])
  977. ->where('del_time',0);
  978. if(! $is_add){
  979. if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
  980. $model->where('id','<>',$data['id']);
  981. }
  982. $bool = $model->exists();
  983. if($bool) return [false,'名称和编码已存在!'];
  984. return [true,''];
  985. }
  986. /**
  987. * 班组详情
  988. * @param $data
  989. * @return array
  990. */
  991. public function teamDetail($data){
  992. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  993. $result = EmployeeTeamPermission::from('employee_team_permission as a')
  994. ->leftJoin('employee as b','b.id','a.employee_id')
  995. ->where('team_id',$data['id'])
  996. ->select('b.id','b.emp_name','b.number as code')
  997. ->get()->toArray();
  998. return [true,$result];
  999. }
  1000. /**
  1001. * 人员权限
  1002. * @param $data
  1003. * @return array
  1004. */
  1005. public function employeeRole($data){
  1006. $role_ids = [];
  1007. $employee_ids = [];
  1008. foreach ($data as $v){
  1009. if(isset($v['role_id'])){
  1010. if(!in_array($v['role_id'],$role_ids)){
  1011. $role_ids[] = $v['role_id'];
  1012. }
  1013. }
  1014. if(isset($v['employee_id'])){
  1015. if(!in_array($v['employee_id'],$employee_ids)){
  1016. $employee_ids[] = $v['employee_id'];
  1017. }
  1018. }
  1019. }
  1020. EmployeeMenuPermission::wherein('role_id',$role_ids)->delete();
  1021. EmployeeMenuPermission::wherein('employee_id',$employee_ids)->delete();
  1022. EmployeeMenuPermission::insert($data);
  1023. return [200,'保存成功!'];
  1024. }
  1025. /**
  1026. * 人员部门关系更新
  1027. * @param $data
  1028. * @return array
  1029. */
  1030. public function employeeDepart($data){
  1031. if($this->isEmpty($data,'insert')) return [false,'数据不能为空!'];
  1032. DB::beginTransaction();
  1033. try {
  1034. if($data['type'] == 1){
  1035. EmployeeDepartPermission::whereIn('depart_id',$data['insert']['depart_id'])->delete();
  1036. }else{
  1037. EmployeeDepartPermission::whereIn('employee_id',$data['insert']['employee_id'])->delete();
  1038. }
  1039. $insert = [];
  1040. foreach ($data['insert']['depart_id'] as $t){
  1041. foreach ($data['insert']['employee_id'] as $e){
  1042. $insert[] = [
  1043. 'depart_id' => $t,
  1044. 'employee_id' => $e
  1045. ];
  1046. }
  1047. }
  1048. EmployeeDepartPermission::insert($insert);
  1049. DB::commit();
  1050. }catch (\Throwable $exception){
  1051. DB::rollBack();
  1052. return [false,$exception->getMessage()];
  1053. }
  1054. return [true,'保存成功!'];
  1055. }
  1056. /**
  1057. * 人员班组关系更新
  1058. * @param $data
  1059. * @return array
  1060. */
  1061. public function employeeTeam($data){
  1062. if($this->isEmpty($data,'insert')) return [false,'数据不能为空!'];
  1063. DB::beginTransaction();
  1064. try {
  1065. if($data['type'] == 1){
  1066. EmployeeTeamPermission::whereIn('team_id',$data['insert']['team_id'])->delete();
  1067. }else{
  1068. EmployeeTeamPermission::whereIn('employee_id',$data['insert']['employee_id'])->delete();
  1069. }
  1070. $insert = [];
  1071. foreach ($data['insert']['team_id'] as $t){
  1072. foreach ($data['insert']['employee_id'] as $e){
  1073. $insert[] = [
  1074. 'team_id' => $t,
  1075. 'employee_id' => $e
  1076. ];
  1077. }
  1078. }
  1079. EmployeeTeamPermission::insert($insert);
  1080. DB::commit();
  1081. }catch (\Throwable $exception){
  1082. DB::rollBack();
  1083. return [false,$exception->getMessage()];
  1084. }
  1085. return [true,'保存成功!'];
  1086. }
  1087. /**
  1088. * 登陆参数规则
  1089. * @param $data
  1090. * @return array
  1091. */
  1092. public function loginRule($data){
  1093. if($this->isEmpty($data,'account')) return [false,'账号不能为空!'];
  1094. if($this->isEmpty($data,'password')) return [false,'密码不存在!'];
  1095. $account = $data['account'];
  1096. $res = Employee::where('del_time',0)
  1097. ->where(function ($query)use($account) {
  1098. $query->where('account', $account)
  1099. ->orWhere('mobile', $account);
  1100. })
  1101. ->get()->toArray();
  1102. if(empty($res)) return [false,'账号不存在或已被删除!'];
  1103. if(count($res) > 1) return [false,'该手机号检测出多个账户,请联系后台!'];
  1104. $res = reset($res);
  1105. if(! Hash::check($data['password'], $res['password'])) return [false,'密码错误!'];
  1106. if($res['is_admin'] != Employee::IS_ADMIN) return [false,'该账号不能登录!'];
  1107. if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
  1108. //门店的信息
  1109. $login_message = EmployeeService::getLoginMessage($res['id']);
  1110. return [true, ['id'=>$res['id'],'name'=>$res['emp_name'],'all_top'=> $login_message]];
  1111. }
  1112. /**
  1113. * 检查人员信息
  1114. * @param $userId
  1115. * @return array
  1116. */
  1117. public static function checkUser($userId){
  1118. $res = Employee::where('id', $userId)
  1119. ->where('del_time',0)
  1120. ->where('is_admin',Employee::IS_ADMIN)
  1121. ->where('state',Employee::USE)->get()->first();
  1122. if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
  1123. return [true, $res];
  1124. }
  1125. /**
  1126. * 获取登录账号的角色
  1127. * @param $employee_id
  1128. * @return array
  1129. */
  1130. public static function getPersonRole($employee_id){
  1131. if(empty($employee_id) || $employee_id == Employee::SPECIAL_ADMIN) return [];
  1132. $role = EmployeeRole::where('del_time',0)
  1133. ->where('employee_id',$employee_id)
  1134. ->select('role_id')
  1135. ->get()->toArray();
  1136. //组织
  1137. $role_id = array_unique(array_column($role,'role_id'));
  1138. asort($role_id);
  1139. $role_id = array_values($role_id);
  1140. return $role_id;
  1141. }
  1142. public static function getPersonRoleQx($role_id = []){
  1143. if(empty($role_id)) return [];
  1144. $role = RoleMenu::where('del_time',0)
  1145. ->whereIn('role_id',$role_id)
  1146. ->select('menu_id','type')
  1147. ->get()->toArray();
  1148. $sysmenu = SysMenu::where('del_time',0)
  1149. ->where('is_authority','>',0)
  1150. ->select('id')
  1151. ->get()->toArray();
  1152. $sysmenu = array_column($sysmenu,'id');
  1153. $return = [];
  1154. foreach ($role as $value){
  1155. if(! in_array($value['menu_id'],$sysmenu)) continue;
  1156. if(isset($return[$value['menu_id']])){
  1157. if($return[$value['menu_id']] < $value['type']) $return[$value['menu_id']] = $value['type'];
  1158. }else{
  1159. $return[$value['menu_id']] = $value['type'];
  1160. }
  1161. }
  1162. return $return;
  1163. }
  1164. public static function getSpecialButton($role_id,$user){
  1165. $return = [];
  1166. $special_button = config('specialButton');
  1167. if($user == Employee::SPECIAL_ADMIN) {
  1168. foreach ($special_button as $value){
  1169. $return[] = $value['id'];
  1170. }
  1171. return $return;
  1172. }
  1173. $role_button = RoleMenuButton::where('del_time',0)
  1174. ->where('button_id','<',0)
  1175. ->whereIn('role_id',$role_id)
  1176. ->select('menu_id','button_id')
  1177. ->get()->toArray();
  1178. foreach ($role_button as $value){
  1179. $return[] = $value['button_id'];
  1180. }
  1181. return $return;
  1182. }
  1183. //通过角色获取菜单
  1184. public function getMenuByRoleInList($user){
  1185. $role_id = $user['role'] ?? [];
  1186. $menu = SysMenu::where('del_time',0)->select('id')->get()->toArray();
  1187. if($user['id'] == Employee::SPECIAL_ADMIN) return array_column($menu,'id');
  1188. //没绑定角色
  1189. if(empty($role_id)) return [];
  1190. $role_menu = RoleMenu::whereIn('role_id',$role_id)
  1191. ->where('del_time',0)
  1192. ->select('menu_id')
  1193. ->get()->toArray();
  1194. return array_column($role_menu,'menu_id');
  1195. }
  1196. //通过角色获取菜单以及按钮
  1197. public function getMenuByRole($user){
  1198. $role_id = $user['role'] ?? [];
  1199. $menu = SysMenu::where('del_time',0)->select('id','uri')->get()->toArray();
  1200. $button = SysMenuButton::where('del_time',0)->select('id','title','sort','func','menu_id')->get()->toArray();
  1201. $button_map = [];
  1202. foreach ($button as $value){
  1203. $button_map[$value['menu_id']][] = $value;
  1204. }
  1205. $object = [];
  1206. //超级管理员
  1207. if($user['id'] == Employee::SPECIAL_ADMIN){
  1208. foreach ($menu as $value){
  1209. $object[] = [
  1210. 'id' => $value['id'],
  1211. // 'type' => 0,//所有权限
  1212. 'uri' => $value['uri'],
  1213. 'button' => $button_map[$value['id']] ?? [],
  1214. ];
  1215. }
  1216. }else{
  1217. //没绑定角色
  1218. if(empty($role_id)) return [];
  1219. $search = RoleMenu::whereIn('role_id',$role_id)
  1220. ->where('del_time',0)
  1221. ->select('menu_id','type')
  1222. ->get()->toArray();
  1223. $menu_map = array_column($menu,'uri','id');
  1224. //该角色下 菜单里所有按钮
  1225. $button_menu = $this->fillRoleButton($role_id);
  1226. $button_t = array_column($button,null,'id');
  1227. foreach ($search as $value){
  1228. $bt = $button_menu[$value['menu_id']] ?? [];
  1229. $new = [];
  1230. foreach ($bt as $b){
  1231. if(! empty($button_t[$b])) $new[] = $button_t[$b];
  1232. }
  1233. $object[] = [
  1234. 'id' => $value['menu_id'],
  1235. 'uri' => $menu_map[$value['menu_id']] ?? '',
  1236. // 'type' => $value['type'],
  1237. 'button' => $new,
  1238. ];
  1239. }
  1240. }
  1241. return $object;
  1242. }
  1243. /**
  1244. * 人员直接绑定部门
  1245. * @param $data
  1246. * @param $user
  1247. * @return array
  1248. */
  1249. public function employeeManagerDepart($data,$user){
  1250. if($user['id'] != Employee::SPECIAL_ADMIN) return [false,'非ADMIN账号不能操作'];
  1251. if($this->isEmpty($data,'employee_id')) return [false,'请选择操作人员'];
  1252. if($this->isEmpty($data,'depart_id')) return [false,'请选择部门'];
  1253. EmployeeManagerDepart::where('employee_id',$data['employee_id'])->update([
  1254. 'del_time' => time()
  1255. ]);
  1256. $insert = [];
  1257. foreach ($data['depart_id'] as $value){
  1258. $insert[] = [
  1259. 'employee_id' => $data['employee_id'],
  1260. 'depart_id' => $value,
  1261. 'crt_time' => time(),
  1262. 'upd_time' => time(),
  1263. ];
  1264. }
  1265. EmployeeManagerDepart::insert($insert);
  1266. return [true,''];
  1267. }
  1268. /**
  1269. * 填充角色下的按钮
  1270. * @param $role_id
  1271. * @return array
  1272. */
  1273. public function fillRoleButton($role_id){
  1274. $button = RoleMenuButton::whereIn('role_id',$role_id)
  1275. ->where('del_time',0)
  1276. ->select('menu_id','button_id')
  1277. ->get()->toArray();
  1278. $button_map = [];
  1279. foreach ($button as $value){
  1280. if(! isset($button_map[$value['menu_id']])){
  1281. $button_map[$value['menu_id']][] = $value['button_id'];
  1282. }else{
  1283. if(! in_array($value['button_id'], $button_map[$value['menu_id']])) $button_map[$value['menu_id']][] = $value['button_id'];
  1284. }
  1285. }
  1286. return $button_map;
  1287. }
  1288. /**
  1289. * 获取登录账号的部门
  1290. * @param $employee_id
  1291. * @return array|string[]
  1292. */
  1293. public static function getLoginDepart($employee_id, $top_depart_id = 0){
  1294. if(empty($employee_id)) return [];
  1295. //自己绑定的部门 启用的部门
  1296. $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
  1297. ->join('depart as b','b.id','a.depart_id')
  1298. ->where('a.employee_id',$employee_id)
  1299. ->where('b.is_use',Depart::IS_UES)
  1300. ->select('a.depart_id','b.is_main','b.parent_id','b.basic_type_id','b.title')
  1301. ->orderBy('b.parent_id','asc')
  1302. ->orderBy('b.is_main','desc')
  1303. ->orderBy('a.depart_id','asc')
  1304. ->get()->toArray();
  1305. $top = $map = $rule = $head = [];
  1306. $res_data = [];//获取当前所在部门(也可能是门店 为了 depart_id)
  1307. $my_depart = [];//当前门店下 我勾选的门店以及部门信息
  1308. $is_all_depart = 0;
  1309. if(! empty($depart)){
  1310. //库存校验
  1311. $set_map = ProductInventorySet::where('del_time',0)->pluck('param_one','top_depart_id')->toArray();
  1312. //所有部门
  1313. $list = Depart::where('del_time',0)->get()->toArray();
  1314. $depart_map = array_column($list,null,'id');
  1315. foreach ($depart as $key => $value){
  1316. if($value['parent_id'] == 0){
  1317. $is_stock = $set_map[$value['depart_id']] ?? 1;
  1318. $depart[$key]['is_stock'] = $is_stock;
  1319. $top[$value['depart_id']] = [
  1320. 'depart_id' => $value['depart_id'],
  1321. 'is_main' => $value['is_main'],
  1322. 'basic_type_id' => $value['basic_type_id'],
  1323. 'title' => $value['title'],
  1324. 'is_stock' => $is_stock,
  1325. ];
  1326. $map[$value['depart_id']] = $value['depart_id'];
  1327. if($value['depart_id'] == $top_depart_id) {
  1328. $my_depart[] = $depart[$key];
  1329. if(empty($res_data)){
  1330. $res_data = $depart[$key];
  1331. }else{
  1332. if($value['depart_id'] >= $res_data['depart_id']) $res_data = $depart[$key];
  1333. }
  1334. }
  1335. }else{
  1336. $t = self::getTopParentId($value['depart_id'],$list);
  1337. if($t && isset($depart_map[$t])) {
  1338. $is_stock = $set_map[$t] ?? 1;
  1339. $depart[$key]['is_stock'] = $is_stock;
  1340. $t_tmp = $depart_map[$t] ?? [];
  1341. if( ! isset($top[$t_tmp['id']])){
  1342. $top[$t_tmp['id']] = [
  1343. 'depart_id' => $t_tmp['id'],
  1344. 'is_main' => $t_tmp['is_main'],
  1345. 'basic_type_id' => $t_tmp['basic_type_id'],
  1346. 'title' => $t_tmp['title'],
  1347. 'is_stock' => $set_map[$t] ?? 1,
  1348. ];
  1349. }
  1350. $map[$value['depart_id']] = $t;
  1351. }
  1352. if($t == $top_depart_id){
  1353. $my_depart[] = $depart[$key];
  1354. if(empty($res_data)){
  1355. $res_data = $depart[$key];
  1356. }else{
  1357. if($value['depart_id'] >= $res_data['depart_id']) $res_data = $depart[$key];
  1358. }
  1359. }
  1360. }
  1361. }
  1362. //-------------重组当前所在门店
  1363. $top = array_values($top);
  1364. usort($top, function($a, $b) {
  1365. return $b['is_main'] - $a['is_main'];
  1366. });
  1367. if(! empty($top_depart_id)){
  1368. $targetArray = null;
  1369. foreach ($top as $key => $value) {
  1370. if ($value['depart_id'] == $top_depart_id) {
  1371. $targetArray = $value;
  1372. unset($top[$key]); // 从原数组中移除目标数组
  1373. break; // 找到后跳出循环
  1374. }
  1375. }
  1376. if ($targetArray) array_unshift($top, $targetArray);
  1377. }
  1378. //-------------重组当前所在门店
  1379. //-------------拥有的数据权限(门店、部门)
  1380. $my_top = $top[0] ?? [];
  1381. foreach ($my_depart as $value){
  1382. if(in_array($value['depart_id'],$rule)) continue;
  1383. if(! $value['parent_id']){ //顶级
  1384. if(! empty($value['is_main']) && ! $is_all_depart) $is_all_depart = 1;
  1385. if($value['is_main']) {//是总公司
  1386. //所有部门都有
  1387. $rule = array_column($list,'id');
  1388. }else{//不是总公司
  1389. //自己以及子部门
  1390. $depart_id = array_merge(self::getAllIds($list,$map[$value['depart_id']]),[$map[$value['depart_id']]]);
  1391. $rule = array_merge_recursive($rule,$depart_id);
  1392. }
  1393. }else{//非顶级
  1394. if(! empty($my_top['is_main']) && $value['is_main'] && ! $is_all_depart) $is_all_depart = 1;
  1395. if($value['is_main']) {//是总社
  1396. $top_tmp = $map[$value['depart_id']];
  1397. if(! empty($depart_map[$top_tmp]['is_main'])){
  1398. //顶级公司是总公司 所有部门都有
  1399. $rule = array_column($list,'id');
  1400. }else{
  1401. //顶级公司是分公司 分公司所有部门
  1402. $depart_id = array_merge(self::getAllIds($list,$top_tmp),[$top_tmp]);
  1403. $rule = array_merge_recursive($rule,$depart_id);
  1404. }
  1405. }else{//不是总社
  1406. $rule = array_merge($rule,[$value['depart_id']]);
  1407. }
  1408. }
  1409. }
  1410. //-------------拥有的数据权限(门店、部门)
  1411. //总店
  1412. foreach ($list as $value){
  1413. if(empty($value['parent_id']) && ! empty($value['is_main'])) $head = $value;
  1414. }
  1415. }
  1416. $rule = array_unique($rule);
  1417. return [$depart,$top,$map,$rule,$is_all_depart,$head,$res_data];
  1418. }
  1419. //获取用户的所属门店信息
  1420. public static function getLoginMessage($employee_id){
  1421. if(empty($employee_id)) return [];
  1422. //自己绑定的部门 启用的部门
  1423. $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
  1424. ->join('depart as b','b.id','a.depart_id')
  1425. ->where('a.employee_id',$employee_id)
  1426. ->where('b.is_use',Depart::IS_UES)
  1427. ->select('a.depart_id','b.is_main','b.parent_id','b.basic_type_id','b.title')
  1428. ->orderBy('b.parent_id','asc')
  1429. ->orderBy('b.is_main','desc')
  1430. ->orderBy('a.depart_id','asc')
  1431. ->get()->toArray();
  1432. $top = [];
  1433. if(! empty($depart)){
  1434. //所有部门
  1435. $list = Depart::where('del_time',0)->get()->toArray();
  1436. $depart_map = array_column($list,null,'id');
  1437. foreach ($depart as $value){
  1438. if($value['parent_id'] == 0){//顶级
  1439. if(! isset($top[$value['depart_id']])){
  1440. $top[$value['depart_id']] = [
  1441. 'depart_id' => $value['depart_id'],
  1442. 'is_main' => $value['is_main'],
  1443. 'title' => $value['title'],
  1444. ];
  1445. }
  1446. }else{
  1447. //门店
  1448. $t = self::getTopParentId($value['depart_id'],$list);
  1449. if($t && isset($depart_map[$t])) {
  1450. $t_tmp = $depart_map[$t] ?? [];
  1451. $is_all_depart = 0;
  1452. if(! empty($t_tmp['is_main']) && $value['is_main']) $is_all_depart = 1;
  1453. if(! isset($top[$t_tmp['id']])){
  1454. $top[$t_tmp['id']] = [
  1455. 'depart_id' => $t_tmp['id'],
  1456. 'is_main' => $is_all_depart,
  1457. 'title' => $t_tmp['title'],
  1458. ];
  1459. }else{
  1460. if(! empty($is_all_depart)) $top[$t_tmp['id']]['is_main'] = $is_all_depart;
  1461. }
  1462. }
  1463. }
  1464. }
  1465. }
  1466. $top = array_values($top);
  1467. usort($top, function($a, $b) {
  1468. return $b['is_main'] - $a['is_main'];
  1469. });
  1470. return $top;
  1471. }
  1472. /**
  1473. * 获取顶级id
  1474. * @param $id
  1475. * @param $data
  1476. * @return int
  1477. */
  1478. public static function getTopParentId($id, $data) {
  1479. foreach ($data as $item) {
  1480. if ($item['id'] == $id) {
  1481. if ($item['parent_id'] == 0) {
  1482. // 找到最顶级的id
  1483. return $item['id'];
  1484. } else {
  1485. // 继续递归查找父级
  1486. return self::getTopParentId($item['parent_id'], $data);
  1487. }
  1488. }
  1489. }
  1490. // 如果没有找到匹配的id,则返回null或者其他你希望的默认值
  1491. return 0;
  1492. }
  1493. /**
  1494. * 递归获取所有id
  1495. * @param $data
  1496. * @param $id
  1497. * @return array
  1498. */
  1499. public static function getAllIds($data, $id) {
  1500. $result = array(); // 存储结果的数组
  1501. foreach ($data as $node) {
  1502. if ($node['parent_id'] == $id) { // 如果当前节点的父 ID 等于指定 ID,则将该节点添加到结果中
  1503. $result[] = $node['id'];
  1504. // 递归查询该节点的所有子孙节点,并将结果合并到结果数组中
  1505. $result = array_merge($result, self::getAllIds($data, $node['id']));
  1506. }
  1507. }
  1508. return $result;
  1509. }
  1510. public static function checkWxUser($userId){
  1511. $res = Employee::where('id', $userId)
  1512. ->where('del_time',0)
  1513. ->where('state',Employee::USE)->get()->first();
  1514. if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
  1515. return [true, $res];
  1516. }
  1517. public function getTopMessage($data,$user){
  1518. $top = $user['depart_top'] ?? [];
  1519. return [true, ['top' => $top]];
  1520. }
  1521. public function searchByEmpName($emp_name){
  1522. $emp = Employee::where('del_time',0)
  1523. ->where('emp_name', 'LIKE', '%'.$emp_name.'%')
  1524. ->select('id')
  1525. ->get()->toArray();
  1526. return array_column($emp,'id');
  1527. }
  1528. }