OrganizationRepository.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Repositories;
  3. use App\Exceptions\ResponseException;
  4. use App\Models\Category;
  5. use App\Models\Organization;
  6. use App\Models\MembersLog;
  7. use App\Validators\Rules\MobileRule;
  8. use Illuminate\Container\Container as Application;
  9. use Illuminate\Support\Facades\Auth;
  10. use Illuminate\Support\Facades\Hash;
  11. use Illuminate\Support\Facades\Log;
  12. use Illuminate\Support\Facades\Schema;
  13. use Prettus\Repository\Eloquent\BaseRepository;
  14. use Prettus\Repository\Criteria\RequestCriteria;
  15. use Illuminate\Support\Facades\DB;
  16. class OrganizationRepository extends BaseRepository
  17. {
  18. /**
  19. * Specify Model class name
  20. *
  21. * @return string
  22. */
  23. public function __construct(Application $app)
  24. {
  25. parent::__construct($app);
  26. }
  27. public function model()
  28. {
  29. return Organization::class;
  30. }
  31. public function save($data, $id)
  32. {
  33. return $this->update($data, $id);
  34. }
  35. /**
  36. * Boot up the repository, pushing criteria
  37. */
  38. public function boot()
  39. {
  40. $this->pushCriteria(app(RequestCriteria::class));
  41. }
  42. public function getOrganizationByAccount($account)
  43. {
  44. if (validator_check($account, 'email')) {
  45. return $this->model->where('email', $account)->first();
  46. }
  47. if (validator_check($account, new MobileRule())) {
  48. return $this->model->where('mobile', $account)->first();
  49. }
  50. return $this->model->where('username', $account)->orWhere('organization_code', $account)->first();
  51. }
  52. public function updateLoginStatus($company)
  53. {
  54. $company->last_login_ip=ip2long(request()->ip);
  55. $company->last_login_time=time();
  56. $company->refresh_time=time();
  57. $company->save();
  58. }
  59. public function getOrgInfoByID($id)
  60. {
  61. $orgInfo = $this->model->find($id, ['*']);
  62. if (!$orgInfo) {
  63. throw new ResponseException('参数错误', '400');
  64. }
  65. $landline = $orgInfo->landline_tel?explode('-', $orgInfo->landline_tel):array();
  66. if (is_array($landline)) {
  67. $orgInfo->landline_first = isset($landline[0]) ? ($landline[0] == 0 ? '' : $landline[0]) : "";
  68. $orgInfo->landline_next = isset($landline[1]) ? ($landline[1] == 0 ? '' : $landline[1]) : "";
  69. $orgInfo->landline_last = isset($landline[2]) ? ($landline[2] == 0 ? '' : $landline[2]) : "";
  70. }
  71. return $orgInfo;
  72. }
  73. public function getOrganizationColumn($id, $column = ['*'])
  74. {
  75. return $this->model->where('id', $id)->select($column)->first()->toArray();
  76. }
  77. public function changeEmail($id, $email)
  78. {
  79. $this->model->where('id', $id)->update(['email'=>$email, 'email_audit'=>1]);
  80. }
  81. public function checkUniaue($key, $value, $id = 0)
  82. {
  83. if (!Schema::hasColumn($this->model->getTable(), $key)) {
  84. return true;
  85. }
  86. if ($this->model->withTrashed()->where($key, $value)
  87. ->when($id>0, function ($query) use ($id) {
  88. return $query->where('id', '<>', $id);
  89. })->first()) {
  90. return false;
  91. }
  92. return true;
  93. }
  94. }