UserService.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2020-01-29 16:54
  7. */
  8. namespace common\services;
  9. use backend\models\search\UserSearch;
  10. use common\models\User;
  11. class UserService extends Service implements UserServiceInterface
  12. {
  13. public function getSearchModel(array $options = [])
  14. {
  15. return new UserSearch();
  16. }
  17. public function getModel($id, array $options = [])
  18. {
  19. $model = User::findOne($id);
  20. if( isset($options['scenario']) && !empty($options['scenario']) ){
  21. if($model !== null) {
  22. $model->setScenario($options['scenario']);
  23. }
  24. }
  25. return $model;
  26. }
  27. public function newModel(array $options = [])
  28. {
  29. $model = new User();
  30. $model->loadDefaultValues();
  31. isset($options['scenario']) && $model->setScenario($options['scenario']);
  32. return $model;
  33. }
  34. public function create(array $postData, array $options = [])
  35. {
  36. $model = $this->newModel($options);
  37. if( $model->load($postData) ){
  38. $model->generateAuthKey();
  39. $model->setPassword($model->password);
  40. if( $model->save() ) {
  41. return true;
  42. }
  43. }
  44. return $model;
  45. }
  46. public function getUserCountByPeriod($startAt=null, $endAt=null)
  47. {
  48. $model = User::find();
  49. if( $startAt != null && $endAt != null ){
  50. $model->andWhere(["between", "created_at", $startAt, $endAt]);
  51. }else if ($startAt != null){
  52. $model->andwhere([">", "created_at", $startAt]);
  53. } else if($endAt != null){
  54. $model->andWhere(["<", "created_at", $endAt]);
  55. }
  56. return $model->count('id');
  57. }
  58. }