UserWechatUserDao.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 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. * 设置搜索条件
  97. * @param $where array 条件数组
  98. * @param array|null $field 需要查询的字段
  99. * @return \crmeb\basic\BaseModel
  100. */
  101. public function searchWhere($where, ?array $field = [])
  102. {
  103. $model = $this->getModel();
  104. $userAlias = $this->alias . '.';
  105. $wechatUserAlias = $this->join_alis . '.';
  106. // --- 时间筛选模块 ---
  107. // 筛选类型:visitno(未访问), visit(访问时间), add_time(注册时间)
  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. // last_time 小于开始时间 或 大于结束时间(即不在该时间段内访问)
  115. $model = $model->where($userAlias . "last_time < " . strtotime($startTime) . " OR " . $userAlias . "last_time > " . $endTime);
  116. }
  117. }
  118. // 筛选指定时间内访问过的用户
  119. if ($where['user_time_type'] == 'visit' && $where['user_time'] != '') {
  120. list($startTime, $endTime) = explode('-', $where['user_time']);
  121. if ($startTime && $endTime) {
  122. $model = $model->where($userAlias . 'last_time', '>', strtotime($startTime));
  123. $model = $model->where($userAlias . 'last_time', '<', strtotime($endTime) + 24 * 3600);
  124. }
  125. }
  126. // 筛选指定时间内注册的用户
  127. if ($where['user_time_type'] == 'add_time' && $where['user_time'] != '') {
  128. list($startTime, $endTime) = explode('-', $where['user_time']);
  129. if ($startTime && $endTime) {
  130. $model = $model->where($userAlias . 'add_time', '>', strtotime($startTime));
  131. $model = $model->where($userAlias . 'add_time', '<', strtotime($endTime) + 24 * 3600);
  132. }
  133. }
  134. }
  135. // --- 购买次数筛选 (单一数值) ---
  136. // pay_count: -1(0次), 其他数值(大于该数值)
  137. if (isset($where['pay_count']) && $where['pay_count'] != '') {
  138. if ($where['pay_count'] == '-1') {
  139. $model = $model->where($userAlias . 'pay_count', 0);
  140. } else {
  141. $model = $model->where($userAlias . 'pay_count', '>', $where['pay_count']);
  142. }
  143. }
  144. // --- 购买次数筛选 (区间) ---
  145. // pay_count_num: [min, max]
  146. if (isset($where['pay_count_num']) && count($where['pay_count_num']) == 2) {
  147. if ($where['pay_count_num'][0] != '' && $where['pay_count_num'][1] != '') {
  148. // 区间查询
  149. $model = $model->whereBetween($userAlias . 'pay_count', $where['pay_count_num']);
  150. } elseif ($where['pay_count_num'][0] != '' && $where['pay_count_num'][1] == '') {
  151. // 大于最小值
  152. $model = $model->where($userAlias . 'pay_count', '>', $where['pay_count_num'][0]);
  153. } elseif ($where['pay_count_num'][0] == '' && $where['pay_count_num'][1] != '') {
  154. // 小于最大值
  155. $model = $model->where($userAlias . 'pay_count', '<', $where['pay_count_num'][1]);
  156. }
  157. }
  158. // --- 消费金额筛选 (区间) ---
  159. // pay_count_money: [min, max] 统计 store_order 表中实际支付金额
  160. if (isset($where['pay_count_money']) && count($where['pay_count_money']) == 2) {
  161. $min = $where['pay_count_money'][0];
  162. $max = $where['pay_count_money'][1];
  163. if ($min !== '' || $max !== '') {
  164. $model = $model->where(function ($query) use ($userAlias, $min, $max) {
  165. // 子查询:在 store_order 表中查找满足条件的记录
  166. $query->whereExists(function ($q) use ($userAlias, $min, $max) {
  167. $q->name('store_order')
  168. ->whereColumn('uid', $userAlias . 'uid')
  169. ->where('paid', 1) // 已支付
  170. ->where('refund_status', 0); // 未退款
  171. // 根据区间条件筛选 pay_price
  172. if ($min !== '' && $max !== '') {
  173. $q->whereBetween('pay_price', [$min, $max]);
  174. } elseif ($min !== '' && $max === '') {
  175. $q->where('pay_price', '>', $min);
  176. } elseif ($min === '' && $max !== '') {
  177. $q->where('pay_price', '<', $max);
  178. }
  179. });
  180. // 特殊处理:如果最小值为0或空,需要包含没有订单记录的用户(即消费金额为0的用户)
  181. if ($min === '' || $min == 0) {
  182. $query->whereOr(function ($q) use ($userAlias) {
  183. $q->whereNotExists(function ($sub) use ($userAlias) {
  184. $sub->name('store_order')
  185. ->whereColumn('uid', $userAlias . 'uid')
  186. ->where('paid', 1)
  187. ->where('refund_status', 0);
  188. });
  189. });
  190. }
  191. });
  192. }
  193. }
  194. // --- 充值次数筛选 (区间) ---
  195. // recharge_count: [min, max] 统计 user_recharge 表中的记录数
  196. if (isset($where['recharge_count']) && count($where['recharge_count']) == 2) {
  197. $min = $where['recharge_count'][0];
  198. $max = $where['recharge_count'][1];
  199. if ($min !== '' || $max !== '') {
  200. $model = $model->where(function ($query) use ($userAlias, $min, $max) {
  201. // 子查询:通过分组统计充值记录数
  202. $query->whereExists(function ($q) use ($userAlias, $min, $max) {
  203. $q->name('user_recharge')
  204. ->whereColumn('uid', $userAlias . 'uid')
  205. ->field('uid')
  206. ->group('uid')
  207. ->having('COUNT(*) BETWEEN ' . (int)$min . ' AND ' . (int)$max);
  208. });
  209. // 特殊处理:包含无充值记录的用户
  210. if ($min === '' || $min == 0) {
  211. $query->whereOr(function ($q) use ($userAlias) {
  212. $q->whereNotExists(function ($sub) use ($userAlias) {
  213. $sub->name('user_recharge')
  214. ->whereColumn('uid', $userAlias . 'uid');
  215. });
  216. });
  217. }
  218. });
  219. }
  220. }
  221. // --- 余额筛选 (区间) ---
  222. // balance: [min, max]
  223. if (isset($where['balance']) && count($where['balance']) == 2) {
  224. if ($where['balance'][0] != '' && $where['balance'][1] != '') {
  225. $model = $model->whereBetween($userAlias . 'now_money', $where['balance']);
  226. } elseif ($where['balance'][0] != '' && $where['balance'][1] == '') {
  227. $model = $model->where($userAlias . 'now_money', '>', $where['balance'][0]);
  228. } elseif ($where['balance'][0] == '' && $where['balance'][1] != '') {
  229. $model = $model->where($userAlias . 'now_money', '<', $where['balance'][1]);
  230. }
  231. }
  232. // --- 积分筛选 (区间) ---
  233. // integral: [min, max]
  234. if (isset($where['integral']) && count($where['integral']) == 2) {
  235. if ($where['integral'][0] != '' && $where['integral'][1] != '') {
  236. $model = $model->whereBetween($userAlias . 'integral', $where['integral']);
  237. } elseif ($where['integral'][0] != '' && $where['integral'][1] == '') {
  238. $model = $model->where($userAlias . 'integral', '>', $where['integral'][0]);
  239. } elseif ($where['integral'][0] == '' && $where['integral'][1] != '') {
  240. $model = $model->where($userAlias . 'integral', '<', $where['integral'][1]);
  241. }
  242. }
  243. // --- 基础属性筛选 ---
  244. // 用户等级
  245. if (isset($where['level']) && $where['level']) {
  246. $model = $model->where($userAlias . 'level', $where['level']);
  247. }
  248. // 用户分组
  249. if (isset($where['group_id']) && $where['group_id']) {
  250. $model = $model->where($userAlias . 'group_id', $where['group_id']);
  251. }
  252. // 用户状态
  253. if (isset($where['status']) && $where['status'] != '') {
  254. $model = $model->where($userAlias . 'status', $where['status']);
  255. }
  256. // 是否为推广员
  257. if (isset($where['is_promoter']) && $where['is_promoter'] != '') {
  258. $model = $model->where($userAlias . 'is_promoter', $where['is_promoter']);
  259. }
  260. // --- 标签筛选 ---
  261. // label_id: 单个ID或ID数组/逗号分隔字符串
  262. if (isset($where['label_id']) && $where['label_id']) {
  263. $model = $model->whereIn($userAlias . 'uid', function ($query) use ($where) {
  264. if (is_array($where['label_id'])) {
  265. $label_ids = array_map('intval', $where['label_id']);
  266. $query->name('user_label_relation')->whereIn('label_id', $label_ids)->field('uid')->select();
  267. } else {
  268. if (strpos($where['label_id'], ',') !== false) {
  269. $label_ids = array_map('intval', explode(',', $where['label_id']));
  270. $query->name('user_label_relation')->whereIn('label_id', $label_ids)->field('uid')->select();
  271. } else {
  272. $query->name('user_label_relation')->where('label_id', (int)$where['label_id'])->field('uid')->select();
  273. }
  274. }
  275. });
  276. }
  277. // --- 会员状态筛选 ---
  278. // isMember: 0(非会员), 1(会员)
  279. if (isset($where['isMember']) && $where['isMember'] != '') {
  280. if ($where['isMember'] == 0) {
  281. $model = $model->where($userAlias . 'is_money_level', 0);
  282. } else {
  283. $model = $model->where($userAlias . 'is_money_level', '>', 0);
  284. }
  285. }
  286. // --- 关键字搜索 ---
  287. // field_key: 指定搜索字段 (nickname, phone, uid)
  288. // nickname: 搜索关键词
  289. $fieldKey = $where['field_key'] ?? '';
  290. $nickname = $where['nickname'] ?? '';
  291. if ($fieldKey && $nickname && in_array($fieldKey, $this->withField)) {
  292. switch ($fieldKey) {
  293. case "nickname":
  294. case "phone":
  295. $model = $model->where($userAlias . trim($fieldKey), 'like', "%" . trim($nickname) . "%");
  296. break;
  297. case "uid":
  298. $model = $model->where($userAlias . trim($fieldKey), trim($nickname));
  299. break;
  300. }
  301. } else if (!$fieldKey && $nickname) {
  302. // 未指定字段时,模糊搜索昵称、UID或手机号
  303. $model = $model->where($userAlias . 'nickname|' . $userAlias . 'uid|' . $userAlias . 'phone', 'LIKE', "%$where[nickname]%");
  304. }
  305. // --- 地域筛选 ---
  306. // country: domestic(国内), abroad(国外)
  307. if (isset($where['country']) && $where['country']) {
  308. if ($where['country'] == 'domestic') {
  309. $model = $model->where($wechatUserAlias . 'country', 'in', ['中国', 'China']);
  310. } else if ($where['country'] == 'abroad') {
  311. $model = $model->where($wechatUserAlias . 'country', 'not in', ['中国', '']);
  312. }
  313. }
  314. // --- 客户端类型筛选 ---
  315. // user_type: app, wechat, routine 等
  316. if (isset($where['user_type']) && $where['user_type']) {
  317. if ($where['user_type'] == 'app') {
  318. $model = $model->whereIn($userAlias . 'user_type', ['app', 'apple']);
  319. } else {
  320. $model = $model->where($userAlias . 'user_type', $where['user_type']);
  321. }
  322. }
  323. // --- 性别筛选 ---
  324. // sex: 1(男), 2(女), 0(未知)
  325. if (isset($where['sex']) && $where['sex'] !== '' && in_array($where['sex'], [0, 1, 2])) {
  326. $model = $model->where($wechatUserAlias . 'sex', $where['sex']);
  327. }
  328. // --- 省份筛选 ---
  329. if (isset($where['province']) && $where['province']) {
  330. $model = $model->where($wechatUserAlias . 'province', $where['province']);
  331. }
  332. // --- 城市筛选 ---
  333. if (isset($where['city']) && $where['city']) {
  334. $model = $model->where($wechatUserAlias . 'city', $where['city']);
  335. }
  336. // --- 通用时间筛选 ---
  337. // 使用模型搜索器 time
  338. if (isset($where['time'])) {
  339. $model->withSearch(['time'], ['time' => $where['time'], 'timeKey' => 'u.add_time']);
  340. }
  341. // --- 删除状态筛选 ---
  342. if (isset($where['is_del'])) {
  343. $model->where($userAlias . 'is_del', $where['is_del']);
  344. }
  345. // --- 指定ID筛选 ---
  346. if (isset($where['ids']) && count($where['ids'])) {
  347. $model->whereIn($userAlias . 'uid', $where['ids']);
  348. }
  349. // --- 代理等级筛选 ---
  350. if (isset($where['agent_level']) && $where['agent_level'] != '') {
  351. $model->where($userAlias . 'agent_level', $where['agent_level']);
  352. }
  353. return $field ? $model->field($field) : $model;
  354. }
  355. /**
  356. * 获取用户性别
  357. * @param $time
  358. * @param $userType
  359. * @return mixed
  360. */
  361. public function getSex($time, $userType)
  362. {
  363. return $this->getModel()->when($userType != '', function ($query) use ($userType) {
  364. $query->where($this->join_alis . '.user_type', $userType);
  365. })->where(function ($query) use ($time) {
  366. if ($time[0] == $time[1]) {
  367. $query->whereDay($this->join_alis . '.add_time', $time[0]);
  368. } else {
  369. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  370. $query->whereTime($this->join_alis . '.add_time', 'between', $time);
  371. }
  372. })->field('count(' . $this->alias . '.uid) as value,' . $this->join_alis . '.sex as name')
  373. ->group($this->join_alis . '.sex')->select()->toArray();
  374. }
  375. }