RoleDao.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\system\menu;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\system\auth\Role;
  15. /**
  16. * Class RoleDao
  17. * @package app\common\dao\system\menu
  18. * @author xaboy
  19. * @day 2020-04-18
  20. */
  21. class RoleDao extends BaseDao
  22. {
  23. /**
  24. * @return BaseModel
  25. * @author xaboy
  26. * @day 2020-03-30
  27. */
  28. protected function getModel(): string
  29. {
  30. return Role::class;
  31. }
  32. /**
  33. * @param $merId
  34. * @param array $where
  35. * @return BaseModel|Role
  36. * @author xaboy
  37. * @day 2020-04-18
  38. */
  39. public function search($merId, array $where = [])
  40. {
  41. $roleModel = Role::getInstance();
  42. if (isset($where['role_name'])) {
  43. $roleModel = $roleModel->whereLike('role_name', '%' . $where['role_name'] . '%');
  44. }
  45. if (isset($where['status'])) {
  46. $roleModel = $roleModel->where('status', intval($where['status']));
  47. }
  48. if (isset($where['role_ids']) && $where['role_ids'] !== '') {
  49. $roleModel = $roleModel->whereIn('role_id', $where['role_ids']);
  50. }
  51. return $roleModel->where('mer_id', $merId);
  52. }
  53. /**
  54. * @param int $merId
  55. * @return array
  56. * @author xaboy
  57. * @day 2020-04-18
  58. */
  59. public function getAllOptions(int $merId)
  60. {
  61. return Role::getDB()->where('status', 1)->where('mer_id', $merId)->column('role_name', 'role_id');
  62. }
  63. /**
  64. * @param $merId
  65. * @param array $ids
  66. * @return array
  67. * @author xaboy
  68. * @day 2020-04-18
  69. */
  70. public function idsByRules($merId, array $ids)
  71. {
  72. $rules = Role::getDB()->where('status', 1)->where('mer_id', $merId)->whereIn($this->getPk(), $ids)->column('rules');
  73. $data = [];
  74. foreach ($rules as $rule) {
  75. $data = array_merge(explode(',', $rule), $data);
  76. }
  77. return array_unique($data);
  78. }
  79. /**
  80. * @param int $merId
  81. * @param int $id
  82. * @return bool
  83. * @author xaboy
  84. * @day 2020-04-18
  85. */
  86. public function merExists(int $merId, int $id)
  87. {
  88. return Role::getDB()->where($this->getPk(), $id)->where('mer_id', $merId)->count() > 0;
  89. }
  90. }