UseScopeBaseModel.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. }
  153. //部门和顶级部门(公司)过滤 取别名a
  154. public function scopeAClear($query, $user, $search)
  155. {
  156. //是否所有部门
  157. $is_all_depart = $user['is_all_depart'] ?? 0;
  158. //权限范围内的部门
  159. $depart_range = $user['depart_range'] ?? [];
  160. //我可见的
  161. $is_see = $search['is_see'] ?? 0;
  162. //可见范围方法
  163. $model = $query->getModel(); // 获取模型的实例
  164. $range_function = $model::range_function ?? ""; // 访问静态属性
  165. $is_function_range = $this->hasMethod(new RangeService(),$range_function);
  166. //顶级部门
  167. $search_depart_id = $search['top_depart_id'] ?? 0; //顶级公司
  168. if(empty($search_depart_id)){
  169. //默认进来 自身顶级公司
  170. $top_depart_id = $user['depart_top'][0] ?? [];
  171. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  172. }else{
  173. //查询 顶级公司
  174. $top_depart_id = $search_depart_id;
  175. }
  176. $id = [];
  177. //可见范围 以及单据里面填写人员
  178. if($is_function_range) $id = RangeService::$range_function($user,$search);
  179. //个人部门所有
  180. $auth_type = $this->getQx($search,$user);
  181. if($is_all_depart){
  182. //所有权限
  183. if(empty($search_depart_id)){
  184. if(! $is_see){
  185. if(! $auth_type){
  186. //全部
  187. $query->whereIn('a.depart_id', $depart_range);
  188. }else{
  189. if($auth_type == 1) { //我创建的 且加上可见
  190. $query->where('a.crt_id',$user['id'])
  191. ->orWhereIn('a.id', $id);
  192. }elseif ($auth_type == 2 || $auth_type == 3){
  193. //自己权限范围内的部门 或 所有
  194. $query->whereIn('a.depart_id', $depart_range);
  195. }
  196. }
  197. }else{
  198. //可见
  199. $query->whereIn('a.id', $id);
  200. }
  201. }else{
  202. if(! $is_see){
  203. if(! $auth_type){
  204. //查看指定公司
  205. $query->where('a.top_depart_id', $top_depart_id);
  206. }else{
  207. if($auth_type == 1) { //指定公司下 且 我创建的 且加上可见
  208. $query->where('a.top_depart_id', $top_depart_id)
  209. ->where('a.crt_id',$user['id'])
  210. ->orWhereIn('a.id', $id);
  211. }elseif ($auth_type == 2 || $auth_type == 3){
  212. // (指定公司下的 且 自己权限范围内的部门 或 所有) 且加上可见
  213. $query->where('a.top_depart_id', $top_depart_id)
  214. ->whereIn('a.depart_id', $depart_range)
  215. ->orWhereIn('a.id', $id);
  216. }
  217. }
  218. }else{
  219. //查看指定公司 且 我可见
  220. $query->whereIn('a.id', $id);
  221. }
  222. }
  223. }else{
  224. //非所有权限
  225. if(! $is_see){
  226. if(! $auth_type){
  227. //指定公司下全部 且加上可见
  228. $query->where('a.top_depart_id', $top_depart_id)
  229. ->whereIn('a.depart_id', $depart_range)
  230. ->orWhereIn('a.id', $id);
  231. }else{
  232. if($auth_type == 1) {
  233. //指定公司下 且 我创建的 且加上可见
  234. $query->where('a.top_depart_id', $top_depart_id)
  235. ->where('a.crt_id',$user['id'])
  236. ->orWhereIn('a.id', $id);
  237. }elseif ($auth_type == 2) {
  238. //指定公司下 且 自己权限范围内的部门 且加上可见
  239. $query->where('a.top_depart_id', $top_depart_id)
  240. ->whereIn('a.depart_id', $depart_range)
  241. ->orWhereIn('a.id', $id);
  242. }elseif ($auth_type == 3) {
  243. // 指定公司下所有 且加上可见
  244. $query->where('a.top_depart_id', $top_depart_id)
  245. ->orWhereIn('a.id', $id);
  246. }
  247. }
  248. }else{
  249. //某个分社可见
  250. $query->whereIn('a.id', $id);
  251. }
  252. }
  253. }
  254. //部门和顶级部门(公司)过滤 Old
  255. public function scopeClear1($query, $user, $search)
  256. {
  257. //是否所有部门
  258. $is_all_depart = $user['is_all_depart'] ?? 0;
  259. //权限范围内的部门
  260. $depart_range = $user['depart_range'] ?? [];
  261. //我可见的
  262. $is_see = $search['is_see'] ?? 0;
  263. //可见范围方法
  264. $model = $query->getModel(); // 获取模型的实例
  265. $range_function = $model::range_function ?? ""; // 访问静态属性
  266. $is_function_range = $this->hasMethod(new RangeService(),$range_function);
  267. //顶级部门
  268. $search_depart_id = $search['top_depart_id'] ?? 0; //顶级公司
  269. if(empty($search_depart_id)){
  270. //默认进来 自身顶级公司
  271. $top_depart_id = $user['depart_top'][0] ?? [];
  272. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  273. }else{
  274. //查询 顶级公司
  275. $top_depart_id = $search_depart_id;
  276. }
  277. $id = [];
  278. //可见范围 以及单据里面填写人员
  279. if($is_function_range) $id = RangeService::$range_function($user,$search);
  280. if($is_all_depart){
  281. //所有部门
  282. if(empty($search_depart_id)){
  283. if(! $is_see){
  284. //全部
  285. $query->whereIn('depart_id', $depart_range);
  286. }else{
  287. //可见
  288. $query->whereIn('id', $id);
  289. }
  290. }else{
  291. if(! $is_see){
  292. //查看某个分社
  293. $query->where('top_depart_id', $top_depart_id);
  294. }else{
  295. //查看某个分社可见
  296. $query->whereIn('id', $id);
  297. }
  298. }
  299. }else{
  300. //某个分社
  301. if(! $is_see){
  302. //某个分社全部
  303. $query->where('top_depart_id', $top_depart_id)
  304. ->whereIn('depart_id', $depart_range)
  305. ->orWhereIn('id', $id);
  306. }else{
  307. //某个分社可见
  308. $query->whereIn('id', $id);
  309. }
  310. }
  311. }
  312. //顶级部门过滤 取别名a
  313. public function scopeATopClear($query, $user, $search)
  314. {
  315. //是否所有部门
  316. $is_all_depart = $user['is_all_depart'] ?? 0;
  317. //权限范围内的部门
  318. $depart_range = $user['depart_range'] ?? [];
  319. //顶级部门
  320. $search_depart_id = $search['top_depart_id'] ?? 0;
  321. if(empty($search_depart_id)){
  322. //默认进来 自身顶级公司
  323. $top_depart_id = $user['depart_top'][0] ?? [];
  324. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  325. }else{
  326. //查询 顶级公司
  327. $top_depart_id = $search_depart_id;
  328. }
  329. if($is_all_depart){
  330. //所有部门
  331. if(empty($search_depart_id)){
  332. //全部
  333. $query->whereIn('a.top_depart_id', $depart_range);
  334. }else{
  335. //查看某个分社
  336. $query->where('a.top_depart_id', $top_depart_id);
  337. }
  338. }else{
  339. //某个分社全部
  340. $query->where('a.top_depart_id', $top_depart_id);
  341. }
  342. return $query;
  343. }
  344. //产品不可见 部门和顶级部门(公司)过滤
  345. public function scopeProductClear($query, $user, $search)
  346. {
  347. //是否所有部门
  348. $is_all_depart = $user['is_all_depart'] ?? 0;
  349. //权限范围内的部门
  350. $depart_range = $user['depart_range'] ?? [];
  351. //总社id
  352. $top_depart_id = $user['head'] ?? [];
  353. $top_depart = $top_depart_id['id'] ?? 0;
  354. //可见范围方法
  355. $model = $query->getModel(); // 获取模型的实例
  356. $range_function = $model::range_function ?? ""; // 访问静态属性
  357. $is_function_range = $this->hasMethod(new RangeService(),$range_function);
  358. //顶级部门
  359. $search_depart_id = $search['top_depart_id'] ?? 0; //顶级公司
  360. if(empty($search_depart_id)){
  361. //默认进来 自身顶级公司
  362. $top_depart_id = $user['depart_top'][0] ?? [];
  363. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  364. }else{
  365. //查询 顶级公司
  366. $top_depart_id = $search_depart_id;
  367. }
  368. $id = [];
  369. //产品 不可见范围
  370. if($is_function_range) $id = RangeService::$range_function($user,$search);
  371. if($is_all_depart){
  372. //所有部门
  373. if(empty($search_depart_id)){
  374. //全部
  375. $query->whereIn('depart_id', $depart_range);
  376. }else{
  377. //查看某个分社
  378. $query->where('top_depart_id', $top_depart_id);
  379. }
  380. }else{
  381. //某个分社全部 去掉不可见数
  382. $query->where('top_depart_id', $top_depart_id)
  383. ->whereIn('depart_id', $depart_range)
  384. ->orWhere('top_depart_id',$top_depart)
  385. ->whereNotIn('id', $id);
  386. }
  387. }
  388. public function getQx($data, $user){
  389. if(empty($data['menu_id'])) return 0;
  390. if($user['id'] == Employee::SPECIAL_ADMIN) return 0;
  391. if(! empty($user['role_authority'][$data['menu_id']])) {
  392. //指定菜单 显示对应权限
  393. return $user['role_authority'][$data['menu_id']];
  394. }else{
  395. return 0;
  396. }
  397. }
  398. function hasMethod($class, $methodName)
  399. {
  400. $reflection = new \ReflectionClass($class);
  401. return $reflection->hasMethod($methodName);
  402. }
  403. }