UseScopeBaseModel.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. namespace App\Model;
  3. use App\Scopes\TopDepartmentScope;
  4. use App\Service\RangeService;
  5. use Illuminate\Database\Eloquent\Model;
  6. class UseScopeBaseModel extends Model
  7. {
  8. //可见范围
  9. const range_function = '';
  10. public function __construct(array $attributes = [])
  11. {
  12. parent::__construct($attributes);
  13. }
  14. //顶级部门过滤
  15. public function scopeTopClear($query, $user, $search)
  16. {
  17. //是否所有部门
  18. $is_all_depart = $user['is_all_depart'] ?? 0;
  19. //权限范围内的部门
  20. $depart_range = $user['depart_range'] ?? [];
  21. //顶级部门
  22. $search_depart_id = $search['top_depart_id'] ?? 0;
  23. if(empty($search_depart_id)){
  24. //默认进来 自身顶级公司
  25. $top_depart_id = $user['depart_top'][0] ?? [];
  26. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  27. }else{
  28. //查询 顶级公司
  29. $top_depart_id = $search_depart_id;
  30. }
  31. if($is_all_depart){
  32. //所有部门
  33. if(empty($search_depart_id)){
  34. //全部
  35. $query->whereIn('top_depart_id', $depart_range);
  36. }else{
  37. //查看某个分社
  38. $query->where('top_depart_id', $top_depart_id);
  39. }
  40. }else{
  41. //某个分社全部
  42. $query->where('top_depart_id', $top_depart_id);
  43. }
  44. //获取当前门店下
  45. if(! empty($search['get_my_top_depart_data'])){
  46. $depart = ! empty($user['depart_top'][0]) ? $user['depart_top'][0]: [];
  47. $depart_id = $depart['depart_id'] ?? 0;
  48. $query->where('top_depart_id', $depart_id);
  49. }
  50. return $query;
  51. }
  52. //部门和顶级部门(公司)过滤
  53. public function scopeClear($query, $user, $search)
  54. {
  55. //是否所有部门
  56. $is_all_depart = $user['is_all_depart'] ?? 0;
  57. //权限范围内的部门
  58. $depart_range = $user['depart_range'] ?? [];
  59. //我可见的
  60. $is_see = $search['is_see'] ?? 0;
  61. //可见范围方法
  62. $model = $query->getModel(); // 获取模型的实例
  63. $range_function = $model::range_function ?? ""; // 访问静态属性
  64. $is_function_range = $this->hasMethod(new RangeService(),$range_function);
  65. //顶级部门
  66. $search_depart_id = $search['top_depart_id'] ?? 0; //顶级公司
  67. if(empty($search_depart_id)){
  68. //默认进来 自身顶级公司
  69. $top_depart_id = $user['depart_top'][0] ?? [];
  70. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  71. }else{
  72. //查询 顶级公司
  73. $top_depart_id = $search_depart_id;
  74. }
  75. $id = [];
  76. //可见范围 以及单据里面填写人员
  77. if($is_function_range) $id = RangeService::$range_function($user,$search);
  78. //个人部门所有
  79. $auth_type = $this->getQx($search,$user);
  80. if($is_all_depart){
  81. //所有权限
  82. if(empty($search_depart_id)){
  83. if(! $is_see){
  84. if(! $auth_type){
  85. //全部
  86. $query->whereIn('depart_id', $depart_range);
  87. }else{
  88. if($auth_type == 1) { //我创建的 且加上可见
  89. $query->where('crt_id',$user['id'])
  90. ->orWhereIn('id', $id);
  91. }elseif ($auth_type == 2 || $auth_type == 3){
  92. //自己权限范围内的部门 或 所有
  93. $query->whereIn('depart_id', $depart_range);
  94. }
  95. }
  96. }else{
  97. //可见
  98. $query->whereIn('id', $id);
  99. }
  100. }else{
  101. if(! $is_see){
  102. if(! $auth_type){
  103. //查看指定公司
  104. $query->where('top_depart_id', $top_depart_id);
  105. }else{
  106. if($auth_type == 1) { //指定公司下 且 我创建的 且加上可见
  107. $query->where('top_depart_id', $top_depart_id)
  108. ->where('crt_id',$user['id'])
  109. ->orWhereIn('id', $id);
  110. }elseif ($auth_type == 2 || $auth_type == 3){
  111. // (指定公司下的 且 自己权限范围内的部门 或 所有) 且加上可见
  112. $query->where('top_depart_id', $top_depart_id)
  113. ->whereIn('depart_id', $depart_range)
  114. ->orWhereIn('id', $id);
  115. }
  116. }
  117. }else{
  118. //查看指定公司 且 我可见
  119. $query->whereIn('id', $id);
  120. }
  121. }
  122. }else{
  123. //非所有权限
  124. if(! $is_see){
  125. if(! $auth_type){
  126. //指定公司下全部 且加上可见
  127. $query->where('top_depart_id', $top_depart_id)
  128. ->whereIn('depart_id', $depart_range)
  129. ->orWhereIn('id', $id);
  130. }else{
  131. if($auth_type == 1) {
  132. //指定公司下 且 我创建的 且加上可见
  133. $query->where('top_depart_id', $top_depart_id)
  134. ->where('crt_id',$user['id'])
  135. ->orWhereIn('id', $id);
  136. }elseif ($auth_type == 2) {
  137. //指定公司下 且 自己权限范围内的部门 且加上可见
  138. $query->where('top_depart_id', $top_depart_id)
  139. ->whereIn('depart_id', $depart_range)
  140. ->orWhereIn('id', $id);
  141. }elseif ($auth_type == 3) {
  142. // 指定公司下所有 且加上可见
  143. $query->where('top_depart_id', $top_depart_id)
  144. ->orWhereIn('id', $id);
  145. }
  146. }
  147. }else{
  148. //某个分社可见
  149. $query->whereIn('id', $id);
  150. }
  151. }
  152. if(! empty($search['get_my_top_depart_data'])){
  153. $depart = ! empty($user['depart_top'][0]) ? $user['depart_top'][0]: [];
  154. $depart_id = $depart['depart_id'] ?? 0;
  155. $query->where('top_depart_id', $depart_id);
  156. }
  157. }
  158. //部门和顶级部门(公司)过滤 取别名a
  159. public function scopeAClear($query, $user, $search)
  160. {
  161. //是否所有部门
  162. $is_all_depart = $user['is_all_depart'] ?? 0;
  163. //权限范围内的部门
  164. $depart_range = $user['depart_range'] ?? [];
  165. //我可见的
  166. $is_see = $search['is_see'] ?? 0;
  167. //可见范围方法
  168. $model = $query->getModel(); // 获取模型的实例
  169. $range_function = $model::range_function ?? ""; // 访问静态属性
  170. $is_function_range = $this->hasMethod(new RangeService(),$range_function);
  171. //顶级部门
  172. $search_depart_id = $search['top_depart_id'] ?? 0; //顶级公司
  173. if(empty($search_depart_id)){
  174. //默认进来 自身顶级公司
  175. $top_depart_id = $user['depart_top'][0] ?? [];
  176. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  177. }else{
  178. //查询 顶级公司
  179. $top_depart_id = $search_depart_id;
  180. }
  181. $id = [];
  182. //可见范围 以及单据里面填写人员
  183. if($is_function_range) $id = RangeService::$range_function($user,$search);
  184. //个人部门所有
  185. $auth_type = $this->getQx($search,$user);
  186. if($is_all_depart){
  187. //所有权限
  188. if(empty($search_depart_id)){
  189. if(! $is_see){
  190. if(! $auth_type){
  191. //全部
  192. $query->whereIn('a.depart_id', $depart_range);
  193. }else{
  194. if($auth_type == 1) { //我创建的 且加上可见
  195. $query->where('a.crt_id',$user['id'])
  196. ->orWhereIn('a.id', $id);
  197. }elseif ($auth_type == 2 || $auth_type == 3){
  198. //自己权限范围内的部门 或 所有
  199. $query->whereIn('a.depart_id', $depart_range);
  200. }
  201. }
  202. }else{
  203. //可见
  204. $query->whereIn('a.id', $id);
  205. }
  206. }else{
  207. if(! $is_see){
  208. if(! $auth_type){
  209. //查看指定公司
  210. $query->where('a.top_depart_id', $top_depart_id);
  211. }else{
  212. if($auth_type == 1) { //指定公司下 且 我创建的 且加上可见
  213. $query->where('a.top_depart_id', $top_depart_id)
  214. ->where('a.crt_id',$user['id'])
  215. ->orWhereIn('a.id', $id);
  216. }elseif ($auth_type == 2 || $auth_type == 3){
  217. // (指定公司下的 且 自己权限范围内的部门 或 所有) 且加上可见
  218. $query->where('a.top_depart_id', $top_depart_id)
  219. ->whereIn('a.depart_id', $depart_range)
  220. ->orWhereIn('a.id', $id);
  221. }
  222. }
  223. }else{
  224. //查看指定公司 且 我可见
  225. $query->whereIn('a.id', $id);
  226. }
  227. }
  228. }else{
  229. //非所有权限
  230. if(! $is_see){
  231. if(! $auth_type){
  232. //指定公司下全部 且加上可见
  233. $query->where('a.top_depart_id', $top_depart_id)
  234. ->whereIn('a.depart_id', $depart_range)
  235. ->orWhereIn('a.id', $id);
  236. }else{
  237. if($auth_type == 1) {
  238. //指定公司下 且 我创建的 且加上可见
  239. $query->where('a.top_depart_id', $top_depart_id)
  240. ->where('a.crt_id',$user['id'])
  241. ->orWhereIn('a.id', $id);
  242. }elseif ($auth_type == 2) {
  243. //指定公司下 且 自己权限范围内的部门 且加上可见
  244. $query->where('a.top_depart_id', $top_depart_id)
  245. ->whereIn('a.depart_id', $depart_range)
  246. ->orWhereIn('a.id', $id);
  247. }elseif ($auth_type == 3) {
  248. // 指定公司下所有 且加上可见
  249. $query->where('a.top_depart_id', $top_depart_id)
  250. ->orWhereIn('a.id', $id);
  251. }
  252. }
  253. }else{
  254. //某个分社可见
  255. $query->whereIn('a.id', $id);
  256. }
  257. }
  258. }
  259. //部门和顶级部门(公司)过滤 Old
  260. public function scopeClear1($query, $user, $search)
  261. {
  262. //是否所有部门
  263. $is_all_depart = $user['is_all_depart'] ?? 0;
  264. //权限范围内的部门
  265. $depart_range = $user['depart_range'] ?? [];
  266. //我可见的
  267. $is_see = $search['is_see'] ?? 0;
  268. //可见范围方法
  269. $model = $query->getModel(); // 获取模型的实例
  270. $range_function = $model::range_function ?? ""; // 访问静态属性
  271. $is_function_range = $this->hasMethod(new RangeService(),$range_function);
  272. //顶级部门
  273. $search_depart_id = $search['top_depart_id'] ?? 0; //顶级公司
  274. if(empty($search_depart_id)){
  275. //默认进来 自身顶级公司
  276. $top_depart_id = $user['depart_top'][0] ?? [];
  277. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  278. }else{
  279. //查询 顶级公司
  280. $top_depart_id = $search_depart_id;
  281. }
  282. $id = [];
  283. //可见范围 以及单据里面填写人员
  284. if($is_function_range) $id = RangeService::$range_function($user,$search);
  285. if($is_all_depart){
  286. //所有部门
  287. if(empty($search_depart_id)){
  288. if(! $is_see){
  289. //全部
  290. $query->whereIn('depart_id', $depart_range);
  291. }else{
  292. //可见
  293. $query->whereIn('id', $id);
  294. }
  295. }else{
  296. if(! $is_see){
  297. //查看某个分社
  298. $query->where('top_depart_id', $top_depart_id);
  299. }else{
  300. //查看某个分社可见
  301. $query->whereIn('id', $id);
  302. }
  303. }
  304. }else{
  305. //某个分社
  306. if(! $is_see){
  307. //某个分社全部
  308. $query->where('top_depart_id', $top_depart_id)
  309. ->whereIn('depart_id', $depart_range)
  310. ->orWhereIn('id', $id);
  311. }else{
  312. //某个分社可见
  313. $query->whereIn('id', $id);
  314. }
  315. }
  316. }
  317. //顶级部门过滤 取别名a
  318. public function scopeATopClear($query, $user, $search)
  319. {
  320. //是否所有部门
  321. $is_all_depart = $user['is_all_depart'] ?? 0;
  322. //权限范围内的部门
  323. $depart_range = $user['depart_range'] ?? [];
  324. //顶级部门
  325. $search_depart_id = $search['top_depart_id'] ?? 0;
  326. if(empty($search_depart_id)){
  327. //默认进来 自身顶级公司
  328. $top_depart_id = $user['depart_top'][0] ?? [];
  329. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  330. }else{
  331. //查询 顶级公司
  332. $top_depart_id = $search_depart_id;
  333. }
  334. if($is_all_depart){
  335. //所有部门
  336. if(empty($search_depart_id)){
  337. //全部
  338. $query->whereIn('a.top_depart_id', $depart_range);
  339. }else{
  340. //查看某个分社
  341. $query->where('a.top_depart_id', $top_depart_id);
  342. }
  343. }else{
  344. //某个分社全部
  345. $query->where('a.top_depart_id', $top_depart_id);
  346. }
  347. return $query;
  348. }
  349. //产品不可见 部门和顶级部门(公司)过滤
  350. public function scopeProductClear($query, $user, $search)
  351. {
  352. //是否所有部门
  353. $is_all_depart = $user['is_all_depart'] ?? 0;
  354. //权限范围内的部门
  355. $depart_range = $user['depart_range'] ?? [];
  356. //总社id
  357. $top_depart_id = $user['head'] ?? [];
  358. $top_depart = $top_depart_id['id'] ?? 0;
  359. //可见范围方法
  360. $model = $query->getModel(); // 获取模型的实例
  361. $range_function = $model::range_function ?? ""; // 访问静态属性
  362. $is_function_range = $this->hasMethod(new RangeService(),$range_function);
  363. //顶级部门
  364. $search_depart_id = $search['top_depart_id'] ?? 0; //顶级公司
  365. if(empty($search_depart_id)){
  366. //默认进来 自身顶级公司
  367. $top_depart_id = $user['depart_top'][0] ?? [];
  368. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  369. }else{
  370. //查询 顶级公司
  371. $top_depart_id = $search_depart_id;
  372. }
  373. $id = [];
  374. //产品 不可见范围
  375. if($is_function_range) $id = RangeService::$range_function($user,$search);
  376. if($is_all_depart){
  377. //所有部门
  378. if(empty($search_depart_id)){
  379. //全部
  380. $query->whereIn('depart_id', $depart_range);
  381. }else{
  382. //查看某个分社
  383. $query->where('top_depart_id', $top_depart_id);
  384. }
  385. }else{
  386. //某个分社全部 去掉不可见数
  387. $query->where('top_depart_id', $top_depart_id)
  388. ->whereIn('depart_id', $depart_range)
  389. ->orWhere('top_depart_id',$top_depart)
  390. ->whereNotIn('id', $id);
  391. }
  392. }
  393. public function getQx($data, $user){
  394. if(empty($data['menu_id'])) return 0;
  395. if($user['id'] == Employee::SPECIAL_ADMIN) return 0;
  396. if(! empty($user['role_authority'][$data['menu_id']])) {
  397. //指定菜单 显示对应权限
  398. return $user['role_authority'][$data['menu_id']];
  399. }else{
  400. return 0;
  401. }
  402. }
  403. function hasMethod($class, $methodName)
  404. {
  405. $reflection = new \ReflectionClass($class);
  406. return $reflection->hasMethod($methodName);
  407. }
  408. }