UserAddressDao.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\dao\user;
  12. use app\dao\BaseDao;
  13. use app\model\user\User;use app\model\user\UserAddress;
  14. /**
  15. * 用户收获地址
  16. * Class UserAddressDao
  17. * @package app\dao\user
  18. */
  19. class UserAddressDao extends BaseDao
  20. {
  21. /**
  22. * 设置模型
  23. * @return string
  24. */
  25. protected function setModel(): string
  26. {
  27. return UserAddress::class;
  28. }
  29. protected function JoinModel() : string
  30. {
  31. return User::class;
  32. }
  33. public function getJoinModel(string $alias = 'a', string $join_alias = 'u', $join = 'left')
  34. {
  35. $this->alias = $alias;
  36. $this->joinAlis = $join_alias;
  37. /** @var User $user */
  38. $user = app()->make($this->joinModel());
  39. $table = $user->getName();
  40. return parent::getModel()->alias($alias)->join($table . ' ' . $join_alias, $alias . '.uid = ' . $join_alias . '.uid', $join);
  41. }
  42. /**
  43. * 获取列表
  44. * @param array $where
  45. * @param string $field
  46. * @param int $page
  47. * @param int $limit
  48. * @return array
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public function getList(array $where, string $field = '*', int $page = 0, int $limit = 0): array
  54. {
  55. return $this->search($where)->field($field)->page($page, $limit)->order('is_default DESC')->select()->toArray();
  56. }
  57. /**
  58. * 地域全部用户
  59. * @param $time
  60. * @param $userType
  61. * @return mixed
  62. */
  63. public function getRegionAll($time, $userType)
  64. {
  65. return $this->getJoinModel()->when($userType != '', function ($query) use ($userType) {
  66. $query->where($this->joinAlis . '.user_type', $userType);
  67. })->where(function ($query) use ($time) {
  68. $query->whereTime($this->joinAlis . '.add_time', '<', strtotime($time[1]) + 86400)->whereOr($this->joinAlis . '.add_time', NULL);
  69. })->field('count(distinct(' . $this->alias . '.uid)) as allNum,' . $this->alias . '.province')
  70. ->group($this->alias . '.province')->select()->toArray();
  71. }
  72. /**
  73. * 地域新增用户
  74. * @param $time
  75. * @param $userType
  76. * @return mixed
  77. */
  78. public function getRegionNew($time, $userType)
  79. {
  80. return $this->getJoinModel()->when($userType != '', function ($query) use ($userType) {
  81. $query->where($this->joinAlis . '.user_type', $userType);
  82. })->where(function ($query) use ($time) {
  83. if ($time[0] == $time[1]) {
  84. $query->whereDay($this->joinAlis . '.add_time', $time[0]);
  85. } else {
  86. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  87. $query->whereTime($this->joinAlis . '.add_time', 'between', $time);
  88. }
  89. })->field('count(distinct(' . $this->alias . '.uid)) as newNum,' . $this->alias . '.province')
  90. ->group($this->alias . '.province')->select()->toArray();
  91. }
  92. }