UserWechatUserDao.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\user;
  13. use think\model;
  14. use app\dao\BaseDao;
  15. use app\model\user\User;
  16. use app\model\wechat\WechatUser;
  17. /**
  18. *
  19. * Class UserWechatUserDao
  20. * @package app\dao\user
  21. */
  22. class UserWechatUserDao extends BaseDao
  23. {
  24. /**
  25. * @var string
  26. */
  27. protected $alias = '';
  28. /**
  29. * @var string
  30. */
  31. protected $join_alis = '';
  32. /**
  33. * 精确搜索白名单
  34. * @var string[]
  35. */
  36. protected $withField = ['uid', 'nickname', 'user_type', 'phone'];
  37. /**
  38. * 设置模型
  39. * @return string
  40. */
  41. protected function setModel(): string
  42. {
  43. return User::class;
  44. }
  45. public function joinModel(): string
  46. {
  47. return WechatUser::class;
  48. }
  49. /**
  50. * 关联模型
  51. * @param string $alias
  52. * @param string $join_alias
  53. * @return \crmeb\basic\BaseModel
  54. */
  55. public function getModel(string $alias = 'u', string $join_alias = 'w', $join = 'left')
  56. {
  57. $this->alias = $alias;
  58. $this->join_alis = $join_alias;
  59. /** @var WechatUser $wechcatUser */
  60. $wechcatUser = app()->make($this->joinModel());
  61. $table = $wechcatUser->getName();
  62. return parent::getModel()->alias($alias)->join($table . ' ' . $join_alias, $alias . '.uid = ' . $join_alias . '.uid', $join);
  63. }
  64. public function getList(array $where, $field = '*', int $page, int $limit)
  65. {
  66. return $this->getModel()->where($where)->field($field)->page($page, $limit)->select()->toArray();
  67. }
  68. /**
  69. * 获取总数
  70. * @param array $where
  71. * @return int
  72. */
  73. public function getCount(array $where): int
  74. {
  75. return $this->getModel()->where($where)->count();
  76. }
  77. /**
  78. * 组合条件模型条数
  79. * @param Model $model
  80. * @return int
  81. */
  82. public function getCountByWhere(array $where): int
  83. {
  84. return $this->searchWhere($where)->group($this->alias . '.uid')->count();
  85. }
  86. /**
  87. * 组合条件模型查询列表
  88. * @param Model $model
  89. * @return array
  90. */
  91. public function getListByModel(array $where, string $field = '', string $order = '', int $page, int $limit): array
  92. {
  93. return $this->searchWhere($where)->field($field)->page($page, $limit)->group($this->alias . '.uid')->order(($order ? $order . ' ,' : '') . $this->alias . '.uid desc')->select()->toArray();
  94. }
  95. /**
  96. * @param $where
  97. * @param array|null $field
  98. * @param int $page
  99. * @param int $limit
  100. * @return \crmeb\basic\BaseModel
  101. */
  102. public function searchWhere($where, ?array $field = [])
  103. {
  104. $model = $this->getModel();
  105. $userAlias = $this->alias . '.';
  106. $wechatUserAlias = $this->join_alis . '.';
  107. // 用户访问时间
  108. if (isset($where['user_time_type']) && isset($where['user_time'])) {
  109. //最后一次访问时间
  110. if ($where['user_time_type'] == 'visitno' && $where['user_time'] != '') {
  111. list($startTime, $endTime) = explode('-', $where['user_time']);
  112. if ($startTime && $endTime) {
  113. $endTime = strtotime($endTime) + 24 * 3600;
  114. $model = $model->where($userAlias . "last_time < " . strtotime($startTime) . " OR " . $userAlias . "last_time > " . $endTime);
  115. }
  116. }
  117. //访问时间
  118. if ($where['user_time_type'] == 'visit' && $where['user_time'] != '') {
  119. list($startTime, $endTime) = explode('-', $where['user_time']);
  120. if ($startTime && $endTime) {
  121. $model = $model->where($userAlias . 'last_time', '>', strtotime($startTime));
  122. $model = $model->where($userAlias . 'last_time', '<', strtotime($endTime) + 24 * 3600);
  123. }
  124. }
  125. //添加时间
  126. if ($where['user_time_type'] == 'add_time' && $where['user_time'] != '') {
  127. list($startTime, $endTime) = explode('-', $where['user_time']);
  128. if ($startTime && $endTime) {
  129. $model = $model->where($userAlias . 'add_time', '>', strtotime($startTime));
  130. $model = $model->where($userAlias . 'add_time', '<', strtotime($endTime) + 24 * 3600);
  131. }
  132. }
  133. }
  134. //购买次数
  135. if (isset($where['pay_count']) && $where['pay_count'] != '') {
  136. if ($where['pay_count'] == '-1') {
  137. $model = $model->where($userAlias . 'pay_count', 0);
  138. } else {
  139. $model = $model->where($userAlias . 'pay_count', '>', $where['pay_count']);
  140. }
  141. }
  142. //用户等级
  143. if (isset($where['level']) && $where['level']) {
  144. $model = $model->where($userAlias . 'level', $where['level']);
  145. }
  146. //用户分组
  147. if (isset($where['group_id']) && $where['group_id']) {
  148. $model = $model->where($userAlias . 'group_id', $where['group_id']);
  149. }
  150. //用户状态
  151. if (isset($where['status']) && $where['status'] != '') {
  152. $model = $model->where($userAlias . 'status', $where['status']);
  153. }
  154. //用户是否为推广员
  155. if (isset($where['is_promoter']) && $where['is_promoter'] != '') {
  156. $model = $model->where($userAlias . 'is_promoter', $where['is_promoter']);
  157. }
  158. //用户标签
  159. if (isset($where['label_id']) && $where['label_id']) {
  160. $model = $model->whereIn($userAlias . 'uid', function ($query) use ($where) {
  161. if (is_array($where['label_id'])) {
  162. $query->name('user_label_relation')->whereIn('label_id', $where['label_id'])->field('uid')->select();
  163. } else {
  164. if (strpos($where['label_id'], ',') !== false) {
  165. $query->name('user_label_relation')->whereIn('label_id', explode(',', $where['label_id']))->field('uid')->select();
  166. } else {
  167. $query->name('user_label_relation')->where('label_id', $where['label_id'])->field('uid')->select();
  168. }
  169. }
  170. });
  171. }
  172. //是否付费会员
  173. if (isset($where['isMember']) && $where['isMember'] != '') {
  174. if ($where['isMember'] == 0) {
  175. $model = $model->where($userAlias . 'is_money_level', 0);
  176. } else {
  177. $model = $model->where($userAlias . 'is_money_level', '>', 0);
  178. }
  179. }
  180. //用户昵称,uid,手机号搜索
  181. $fieldKey = $where['field_key'] ?? '';
  182. $nickname = $where['nickname'] ?? '';
  183. if ($fieldKey && $nickname && in_array($fieldKey, $this->withField)) {
  184. switch ($fieldKey) {
  185. case "nickname":
  186. case "phone":
  187. $model = $model->where($userAlias . trim($fieldKey), 'like', "%" . trim($nickname) . "%");
  188. break;
  189. case "uid":
  190. $model = $model->where($userAlias . trim($fieldKey), trim($nickname));
  191. break;
  192. }
  193. } else if (!$fieldKey && $nickname) {
  194. $model = $model->where($userAlias . 'nickname|' . $userAlias . 'uid|' . $userAlias . 'phone', 'LIKE', "%$where[nickname]%");
  195. }
  196. //所在城市
  197. if (isset($where['country']) && $where['country']) {
  198. if ($where['country'] == 'domestic') {
  199. $model = $model->where($wechatUserAlias . 'country', 'in', ['中国', 'China']);
  200. } else if ($where['country'] == 'abroad') {
  201. $model = $model->where($wechatUserAlias . 'country', 'not in', ['中国', '']);
  202. }
  203. }
  204. //用户类型
  205. if (isset($where['user_type']) && $where['user_type']) {
  206. if ($where['user_type'] == 'app') {
  207. $model = $model->whereIn($userAlias . 'user_type', ['app', 'apple']);
  208. } else {
  209. $model = $model->where($userAlias . 'user_type', $where['user_type']);
  210. }
  211. }
  212. //用户性别
  213. if (isset($where['sex']) && $where['sex'] !== '' && in_array($where['sex'], [0, 1, 2])) {
  214. $model = $model->where($wechatUserAlias . 'sex', $where['sex']);
  215. }
  216. //所在省份
  217. if (isset($where['province']) && $where['province']) {
  218. $model = $model->where($wechatUserAlias . 'province', $where['province']);
  219. }
  220. //所在城市
  221. if (isset($where['city']) && $where['city']) {
  222. $model = $model->where($wechatUserAlias . 'city', $where['city']);
  223. }
  224. if (isset($where['time'])) {
  225. $model->withSearch(['time'], ['time' => $where['time'], 'timeKey' => 'u.add_time']);
  226. }
  227. if (isset($where['is_del'])) {
  228. $model->where($userAlias . 'is_del', $where['is_del']);
  229. }
  230. if (isset($where['ids']) && count($where['ids'])) {
  231. $model->whereIn($userAlias . 'uid', $where['ids']);
  232. }
  233. return $field ? $model->field($field) : $model;
  234. }
  235. /**
  236. * 获取用户性别
  237. * @param $time
  238. * @param $userType
  239. * @return mixed
  240. */
  241. public function getSex($time, $userType)
  242. {
  243. return $this->getModel()->when($userType != '', function ($query) use ($userType) {
  244. $query->where($this->join_alis . '.user_type', $userType);
  245. })->where(function ($query) use ($time) {
  246. if ($time[0] == $time[1]) {
  247. $query->whereDay($this->join_alis . '.add_time', $time[0]);
  248. } else {
  249. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  250. $query->whereTime($this->join_alis . '.add_time', 'between', $time);
  251. }
  252. })->field('count(' . $this->alias . '.uid) as value,' . $this->join_alis . '.sex as name')
  253. ->group($this->join_alis . '.sex')->select()->toArray();
  254. }
  255. }