UserWechatUserDao.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. use think\facade\Log;
  18. /**
  19. *
  20. * Class UserWechatUserDao
  21. * @package app\dao\user
  22. */
  23. class UserWechatUserDao extends BaseDao
  24. {
  25. /**
  26. * @var string
  27. */
  28. protected $alias = '';
  29. /**
  30. * @var string
  31. */
  32. protected $join_alis = '';
  33. /**
  34. * 精确搜索白名单
  35. * @var string[]
  36. */
  37. protected $withField = ['uid', 'nickname', 'user_type', 'phone'];
  38. /**
  39. * 设置模型
  40. * @return string
  41. */
  42. protected function setModel(): string
  43. {
  44. return User::class;
  45. }
  46. public function joinModel(): string
  47. {
  48. return WechatUser::class;
  49. }
  50. /**
  51. * 关联模型
  52. * @param string $alias
  53. * @param string $join_alias
  54. * @return \crmeb\basic\BaseModel
  55. */
  56. public function getModel(string $alias = 'u', string $join_alias = 'w', $join = 'left')
  57. {
  58. $this->alias = $alias;
  59. $this->join_alis = $join_alias;
  60. /** @var WechatUser $wechcatUser */
  61. $wechcatUser = app()->make($this->joinModel());
  62. $table = $wechcatUser->getName();
  63. return parent::getModel()->alias($alias)->join($table . ' ' . $join_alias, $alias . '.uid = ' . $join_alias . '.uid', $join);
  64. }
  65. public function getList(array $where, $field = '*', int $page, int $limit)
  66. {
  67. return $this->getModel()->where($where)->field($field)->page($page, $limit)->select()->toArray();
  68. }
  69. /**
  70. * 获取总数
  71. * @param array $where
  72. * @return int
  73. */
  74. public function getCount(array $where): int
  75. {
  76. return $this->getModel()->where($where)->count();
  77. }
  78. /**
  79. * 组合条件模型条数
  80. * @param Model $model
  81. * @return int
  82. */
  83. public function getCountByWhere(array $where): int
  84. {
  85. return $this->searchWhere($where)->group($this->alias . '.uid')->count();
  86. }
  87. /**
  88. * 组合条件模型查询列表
  89. * @param Model $model
  90. * @return array
  91. */
  92. public function getListByModel(array $where, string $field = '', string $order = '', int $page, int $limit): array
  93. {
  94. return $this->searchWhere($where)->field($field)->page($page, $limit)->group($this->alias . '.uid')->order(($order ? $order . ' ,' : '') . $this->alias . '.uid desc')->select()->toArray();
  95. }
  96. /**
  97. * @param $where
  98. * @param array|null $field
  99. * @param int $page
  100. * @param int $limit
  101. * @return \crmeb\basic\BaseModel
  102. */
  103. public function searchWhere($where, ?array $field = [])
  104. {
  105. $model = $this->getModel();
  106. $userAlias = $this->alias . '.';
  107. $wechatUserAlias = $this->join_alis . '.';
  108. // 用户访问时间
  109. if (isset($where['user_time_type']) && isset($where['user_time'])) {
  110. //最后一次访问时间
  111. if ($where['user_time_type'] == 'visitno' && $where['user_time'] != '') {
  112. list($startTime, $endTime) = explode('-', $where['user_time']);
  113. if ($startTime && $endTime) {
  114. $endTime = strtotime($endTime) + 24 * 3600;
  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. if (isset($where['pay_count']) && $where['pay_count'] != '') {
  137. if ($where['pay_count'] == '-1') {
  138. $model = $model->where($userAlias . 'pay_count', 0);
  139. } else {
  140. $model = $model->where($userAlias . 'pay_count', '>', $where['pay_count']);
  141. }
  142. }
  143. if (isset($where['pay_count_num']) && count($where['pay_count_num']) == 2) {
  144. if ($where['pay_count_num'][0] != '' && $where['pay_count_num'][1] != '') {
  145. $model = $model->whereBetween($userAlias . 'pay_count', $where['pay_count_num']);
  146. } elseif ($where['pay_count_num'][0] != '' && $where['pay_count_num'][1] == '') {
  147. $model = $model->where($userAlias . 'pay_count', '>', $where['pay_count_num'][0]);
  148. } elseif ($where['pay_count_num'][0] == '' && $where['pay_count_num'][1] != '') {
  149. $model = $model->where($userAlias . 'pay_count', '<', $where['pay_count_num'][1]);
  150. }
  151. }
  152. //储值余额
  153. if (isset($where['balance']) && count($where['balance']) == 2) {
  154. if ($where['balance'][0] != '' && $where['balance'][1] != '') {
  155. $model = $model->whereBetween($userAlias . 'now_money', $where['balance']);
  156. } elseif ($where['balance'][0] != '' && $where['balance'][1] == '') {
  157. $model = $model->where($userAlias . 'now_money', '>', $where['balance'][0]);
  158. } elseif ($where['balance'][0] == '' && $where['balance'][1] != '') {
  159. $model = $model->where($userAlias . 'now_money', '<', $where['balance'][1]);
  160. }
  161. }
  162. //积分剩余
  163. if (isset($where['integral']) && count($where['integral']) == 2) {
  164. if ($where['integral'][0] != '' && $where['integral'][1] != '') {
  165. $model = $model->whereBetween($userAlias . 'integral', $where['integral']);
  166. } elseif ($where['integral'][0] != '' && $where['integral'][1] == '') {
  167. $model = $model->where($userAlias . 'integral', '>', $where['integral'][0]);
  168. } elseif ($where['integral'][0] == '' && $where['integral'][1] != '') {
  169. $model = $model->where($userAlias . 'integral', '<', $where['integral'][1]);
  170. }
  171. }
  172. //用户等级
  173. if (isset($where['level']) && $where['level']) {
  174. $model = $model->where($userAlias . 'level', $where['level']);
  175. }
  176. //用户分组
  177. if (isset($where['group_id']) && $where['group_id']) {
  178. $model = $model->where($userAlias . 'group_id', $where['group_id']);
  179. }
  180. //用户状态
  181. if (isset($where['status']) && $where['status'] != '') {
  182. $model = $model->where($userAlias . 'status', $where['status']);
  183. }
  184. //用户是否为推广员
  185. if (isset($where['is_promoter']) && $where['is_promoter'] != '') {
  186. $model = $model->where($userAlias . 'is_promoter', $where['is_promoter']);
  187. }
  188. //用户标签
  189. if (isset($where['label_id']) && $where['label_id']) {
  190. $model = $model->whereIn($userAlias . 'uid', function ($query) use ($where) {
  191. if (is_array($where['label_id'])) {
  192. $query->name('user_label_relation')->whereIn('label_id', $where['label_id'])->field('uid')->select();
  193. } else {
  194. if (strpos($where['label_id'], ',') !== false) {
  195. $query->name('user_label_relation')->whereIn('label_id', explode(',', $where['label_id']))->field('uid')->select();
  196. } else {
  197. $query->name('user_label_relation')->where('label_id', (int)$where['label_id'])->field('uid')->select();
  198. }
  199. }
  200. });
  201. }
  202. //是否付费会员
  203. if (isset($where['isMember']) && $where['isMember'] != '') {
  204. if ($where['isMember'] == 0) {
  205. $model = $model->where($userAlias . 'is_money_level', 0);
  206. } else {
  207. $model = $model->where($userAlias . 'is_money_level', '>', 0);
  208. }
  209. }
  210. //用户昵称,uid,手机号搜索
  211. $fieldKey = $where['field_key'] ?? '';
  212. $nickname = $where['nickname'] ?? '';
  213. if ($fieldKey && $nickname && in_array($fieldKey, $this->withField)) {
  214. switch ($fieldKey) {
  215. case "nickname":
  216. case "phone":
  217. $model = $model->where($userAlias . trim($fieldKey), 'like', "%" . trim($nickname) . "%");
  218. break;
  219. case "uid":
  220. $model = $model->where($userAlias . trim($fieldKey), trim($nickname));
  221. break;
  222. }
  223. } else if (!$fieldKey && $nickname) {
  224. $model = $model->where($userAlias . 'nickname|' . $userAlias . 'uid|' . $userAlias . 'phone', 'LIKE', "%$where[nickname]%");
  225. }
  226. //所在城市
  227. if (isset($where['country']) && $where['country']) {
  228. if ($where['country'] == 'domestic') {
  229. $model = $model->where($wechatUserAlias . 'country', 'in', ['中国', 'China']);
  230. } else if ($where['country'] == 'abroad') {
  231. $model = $model->where($wechatUserAlias . 'country', 'not in', ['中国', '']);
  232. }
  233. }
  234. //用户类型
  235. if (isset($where['user_type']) && $where['user_type']) {
  236. if ($where['user_type'] == 'app') {
  237. $model = $model->whereIn($userAlias . 'user_type', ['app', 'apple']);
  238. } else {
  239. $model = $model->where($userAlias . 'user_type', $where['user_type']);
  240. }
  241. }
  242. //用户性别
  243. if (isset($where['sex']) && $where['sex'] !== '' && in_array($where['sex'], [0, 1, 2])) {
  244. $model = $model->where($wechatUserAlias . 'sex', $where['sex']);
  245. }
  246. //所在省份
  247. if (isset($where['province']) && $where['province']) {
  248. $model = $model->where($wechatUserAlias . 'province', $where['province']);
  249. }
  250. //所在城市
  251. if (isset($where['city']) && $where['city']) {
  252. $model = $model->where($wechatUserAlias . 'city', $where['city']);
  253. }
  254. if (isset($where['time'])) {
  255. $model->withSearch(['time'], ['time' => $where['time'], 'timeKey' => 'u.add_time']);
  256. }
  257. if (isset($where['is_del'])) {
  258. $model->where($userAlias . 'is_del', $where['is_del']);
  259. }
  260. if (isset($where['ids']) && count($where['ids'])) {
  261. $model->whereIn($userAlias . 'uid', $where['ids']);
  262. }
  263. if (isset($where['agent_level']) && $where['agent_level'] != '') {
  264. $model->where($userAlias . 'agent_level', $where['agent_level']);
  265. }
  266. return $field ? $model->field($field) : $model;
  267. }
  268. /**
  269. * 获取用户性别
  270. * @param $time
  271. * @param $userType
  272. * @return mixed
  273. */
  274. public function getSex($time, $userType)
  275. {
  276. return $this->getModel()->when($userType != '', function ($query) use ($userType) {
  277. $query->where($this->join_alis . '.user_type', $userType);
  278. })->where(function ($query) use ($time) {
  279. if ($time[0] == $time[1]) {
  280. $query->whereDay($this->join_alis . '.add_time', $time[0]);
  281. } else {
  282. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  283. $query->whereTime($this->join_alis . '.add_time', 'between', $time);
  284. }
  285. })->field('count(' . $this->alias . '.uid) as value,' . $this->join_alis . '.sex as name')
  286. ->group($this->join_alis . '.sex')->select()->toArray();
  287. }
  288. /**
  289. * @param $unionId
  290. * @return array|Model|null
  291. * @throws \think\db\exception\DataNotFoundException
  292. * @throws \think\db\exception\DbException
  293. * @throws \think\db\exception\ModelNotFoundException
  294. */
  295. public function getUserByUnionId($unionId)
  296. {
  297. $info = $this->getModel()->where('w.unionid', $unionId)->find();
  298. return $info;
  299. }
  300. public function getUserByUid($uid){
  301. $info = $this->getModel()->where('w.uid', $uid)->find();
  302. return $info;
  303. }
  304. }