UserStoreUserDao.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. declare (strict_types=1);
  12. namespace app\dao\store;
  13. use think\model;
  14. use app\dao\BaseDao;
  15. use app\model\user\User;
  16. use app\model\store\StoreUser;
  17. /**
  18. * Class UserStoreUserDao
  19. * @package app\dao\store
  20. */
  21. class UserStoreUserDao extends BaseDao
  22. {
  23. /**
  24. * @var string
  25. */
  26. protected $alias = '';
  27. /**
  28. * @var string
  29. */
  30. protected $join_alis = '';
  31. /**
  32. * 精确搜索白名单
  33. * @var string[]
  34. */
  35. protected $withField = ['uid', 'nickname', 'user_type', 'phone'];
  36. /**
  37. * 设置模型
  38. * @return string
  39. */
  40. protected function setModel(): string
  41. {
  42. return User::class;
  43. }
  44. public function joinModel(): string
  45. {
  46. return StoreUser::class;
  47. }
  48. /**
  49. * 关联模型
  50. * @param string $alias
  51. * @param string $join_alias
  52. * @param string $join
  53. * @return \crmeb\basic\BaseModel
  54. */
  55. public function getModel(string $alias = 'u', string $join_alias = 's', $join = '')
  56. {
  57. $this->alias = $alias;
  58. $this->join_alis = $join_alias;
  59. /** @var StoreUser $storeUser */
  60. $storeUser = app()->make($this->joinModel());
  61. $table = $storeUser->getName();
  62. return parent::getModel()->withTrashed()->alias($alias)->join($table . ' ' . $join_alias, $alias . '.uid = ' . $join_alias . '.uid', $join);
  63. }
  64. /**
  65. * 获取列表
  66. * @param array $where
  67. * @param string $field
  68. * @param int $page
  69. * @param int $limit
  70. * @return array
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\DbException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. */
  75. public function getList(array $where, $field = '*', int $page = 0, int $limit = 0)
  76. {
  77. return $this->getModel()->where($where)->field($field)->when($page && $limit, function ($query) use ($page, $limit) {
  78. $query->page($page, $limit);
  79. })->select()->toArray();
  80. }
  81. /**
  82. * 获取总数
  83. * @param array $where
  84. * @return int
  85. */
  86. public function getCount(array $where): int
  87. {
  88. return $this->getModel()->where($where)->count();
  89. }
  90. /**
  91. * 组合条件模型条数
  92. * @param Model $model
  93. * @return int
  94. */
  95. public function getCountByWhere(array $where): int
  96. {
  97. return $this->searchWhere($where)->group($this->alias . '.uid')->count();
  98. }
  99. /**
  100. * 组合条件模型查询列表
  101. * @param array $where
  102. * @param string $field
  103. * @param string $order
  104. * @param int $page
  105. * @param int $limit
  106. * @return array
  107. */
  108. public function getListByModel(array $where, string $field = '', string $order = '', int $page = 0, int $limit = 0): array
  109. {
  110. return $this->searchWhere($where)->field($field)->when($page && $limit, function ($query) use ($page, $limit) {
  111. $query->page($page, $limit);
  112. })->group($this->alias . '.uid')->order(($order ? $order . ' ,' : '') . $this->alias . '.uid desc')->select()->toArray();
  113. }
  114. /**
  115. * @param $where
  116. * @param array|null $field
  117. * @param int $page
  118. * @param int $limit
  119. * @return \crmeb\basic\BaseModel
  120. */
  121. public function searchWhere($where, ?array $field = [])
  122. {
  123. $model = $this->getModel();
  124. $userAlias = $this->alias . '.';
  125. $storeUserAlias = $this->join_alis . '.';
  126. if (isset($where['is_filter_del']) && $where['is_filter_del'] == 1) {
  127. $model = $model->where($userAlias . 'delete_time', null);
  128. }
  129. //门店
  130. if (isset($where['store_id']) && $where['store_id'] !== '') {
  131. $model = $model->where($storeUserAlias . 'store_id', $where['store_id']);
  132. }
  133. // 用户访问时间
  134. if (isset($where['user_time_type']) && isset($where['user_time'])) {
  135. //最后一次访问时间
  136. if ($where['user_time_type'] == 'visitno' && $where['user_time'] != '') {
  137. [$startTime, $endTime] = explode('-', $where['user_time']);
  138. if ($startTime && $endTime) {
  139. $endTime = strtotime($endTime) + 24 * 3600;
  140. $model = $model->where($userAlias . "last_time < " . strtotime($startTime) . " OR " . $userAlias . "last_time > " . $endTime);
  141. }
  142. }
  143. //访问时间
  144. if ($where['user_time_type'] == 'visit' && $where['user_time'] != '') {
  145. [$startTime, $endTime] = explode('-', $where['user_time']);
  146. if ($startTime && $endTime) {
  147. $model = $model->where($userAlias . 'last_time', '>', strtotime($startTime));
  148. $model = $model->where($userAlias . 'last_time', '<', strtotime($endTime) + 24 * 3600);
  149. }
  150. }
  151. //添加时间
  152. if ($where['user_time_type'] == 'add_time' && $where['user_time'] != '') {
  153. [$startTime, $endTime] = explode('-', $where['user_time']);
  154. if ($startTime && $endTime) {
  155. $model = $model->where($userAlias . 'add_time', '>', strtotime($startTime));
  156. $model = $model->where($userAlias . 'add_time', '<', strtotime($endTime) + 24 * 3600);
  157. }
  158. }
  159. }
  160. //购买次数
  161. if (isset($where['pay_count']) && $where['pay_count'] != '') {
  162. if ($where['pay_count'] == '-1') {
  163. $model = $model->where($userAlias . 'pay_count', 0);
  164. } else {
  165. $model = $model->where($userAlias . 'pay_count', '>', $where['pay_count']);
  166. }
  167. }
  168. //用户等级
  169. if (isset($where['level']) && $where['level']) {
  170. $model = $model->where($userAlias . 'level', $where['level']);
  171. }
  172. //用户分组
  173. if (isset($where['group_id']) && $where['group_id']) {
  174. $model = $model->where($userAlias . 'group_id', $where['group_id']);
  175. }
  176. //用户状态
  177. if (isset($where['status']) && $where['status'] != '') {
  178. $model = $model->where($userAlias . 'status', $where['status']);
  179. }
  180. //用户是否为推广员
  181. if (isset($where['is_promoter']) && $where['is_promoter'] != '') {
  182. $model = $model->where($userAlias . 'is_promoter', $where['is_promoter']);
  183. }
  184. //用户标签
  185. if (isset($where['label_id']) && $where['label_id']) {
  186. $model = $model->whereIn($userAlias . 'uid', function ($query) use ($where) {
  187. if (is_array($where['label_id'])) {
  188. $query->name('user_label_relation')->whereIn('label_id', $where['label_id'])->field('uid')->select();
  189. } else {
  190. $query->name('user_label_relation')->where('label_id', $where['label_id'])->field('uid')->select();
  191. }
  192. });
  193. }
  194. //是否付费会员
  195. if (isset($where['isMember']) && $where['isMember'] != '') {
  196. if ($where['isMember'] == 0) {
  197. $model = $model->where($userAlias . 'is_money_level', 0);
  198. } else {
  199. $model = $model->where($userAlias . 'is_money_level', '>', 0);
  200. }
  201. }
  202. //用户昵称,uid,手机号搜索
  203. $fieldKey = $where['field_key'] ?? '';
  204. $nickname = $where['nickname'] ?? '';
  205. if ($fieldKey && $nickname && in_array($fieldKey, $this->withField)) {
  206. switch ($fieldKey) {
  207. case "nickname":
  208. $model = $model->where($userAlias . trim($fieldKey), 'like', "%" . trim($nickname) . "%");
  209. break;
  210. case "phone":
  211. $model = $model->where($userAlias . trim($fieldKey), 'like', "%" . trim($nickname));
  212. break;
  213. case "uid":
  214. $model = $model->where($userAlias . trim($fieldKey), trim($nickname));
  215. break;
  216. }
  217. } else if (!$fieldKey && $nickname) {
  218. $model = $model->where($userAlias . 'nickname|' . $userAlias . 'uid|' . $userAlias . 'phone', 'LIKE', "%$where[nickname]%");
  219. }
  220. //用户类型
  221. if (isset($where['user_type']) && $where['user_type']) {
  222. $model = $model->where($userAlias . 'user_type', $where['user_type']);
  223. }
  224. if (isset($where['time'])) {
  225. $model->withSearch(['time'], ['time' => $where['time'], 'timeKey' => 'u.add_time']);
  226. }
  227. return $field ? $model->field($field) : $model;
  228. }
  229. }