UserWechatUserDao.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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\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()->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. $wechatUserAlias = $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['user_time_type']) && isset($where['user_time'])) {
  131. //最后一次访问时间
  132. if ($where['user_time_type'] == 'visitno' && $where['user_time'] != '') {
  133. [$startTime, $endTime] = explode('-', $where['user_time']);
  134. if ($startTime && $endTime) {
  135. $endTime = strtotime($endTime) + 24 * 3600;
  136. $model = $model->where($userAlias . "last_time < " . strtotime($startTime) . " OR " . $userAlias . "last_time > " . $endTime);
  137. }
  138. }
  139. //访问时间
  140. if ($where['user_time_type'] == 'visit' && $where['user_time'] != '') {
  141. [$startTime, $endTime] = explode('-', $where['user_time']);
  142. if ($startTime && $endTime) {
  143. $model = $model->where($userAlias . 'last_time', '>', strtotime($startTime));
  144. $model = $model->where($userAlias . 'last_time', '<', strtotime($endTime) + 24 * 3600);
  145. }
  146. }
  147. //添加时间
  148. if ($where['user_time_type'] == 'add_time' && $where['user_time'] != '') {
  149. [$startTime, $endTime] = explode('-', $where['user_time']);
  150. if ($startTime && $endTime) {
  151. $model = $model->where($userAlias . 'add_time', '>', strtotime($startTime));
  152. $model = $model->where($userAlias . 'add_time', '<', strtotime($endTime) + 24 * 3600);
  153. }
  154. }
  155. }
  156. //购买次数
  157. if (isset($where['pay_count']) && $where['pay_count'] != '') {
  158. if ($where['pay_count'] == '-1') {
  159. $model = $model->where($userAlias . 'pay_count', 0);
  160. } else {
  161. $model = $model->where($userAlias . 'pay_count', '>', $where['pay_count']);
  162. }
  163. }
  164. //用户等级
  165. if (isset($where['level']) && $where['level']) {
  166. $model = $model->where($userAlias . 'level', $where['level']);
  167. }
  168. //用户分组
  169. if (isset($where['group_id']) && $where['group_id']) {
  170. $model = $model->where($userAlias . 'group_id', $where['group_id']);
  171. }
  172. //用户状态
  173. if (isset($where['status']) && $where['status'] != '') {
  174. $model = $model->where($userAlias . 'status', $where['status']);
  175. }
  176. //用户是否为推广员
  177. if (isset($where['is_promoter']) && $where['is_promoter'] != '') {
  178. $model = $model->where($userAlias . 'is_promoter', $where['is_promoter']);
  179. }
  180. //用户标签
  181. if (isset($where['label_id']) && $where['label_id']) {
  182. $model = $model->whereIn($userAlias . 'uid', function ($query) use ($where) {
  183. if (is_array($where['label_id'])) {
  184. $query->name('user_label_relation')->whereIn('label_id', $where['label_id'])->field('uid')->select();
  185. } else {
  186. if (strpos($where['label_id'], ',') !== false) {
  187. $query->name('user_label_relation')->whereIn('label_id', explode(',', $where['label_id']))->field('uid')->select();
  188. } else {
  189. $query->name('user_label_relation')->where('label_id', $where['label_id'])->field('uid')->select();
  190. }
  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. case "phone":
  209. $model = $model->where($userAlias . trim($fieldKey), 'like', "%" . trim($nickname) . "%");
  210. break;
  211. case "uid":
  212. $model = $model->where($userAlias . trim($fieldKey), trim($nickname));
  213. break;
  214. }
  215. } else if ((!$fieldKey || $fieldKey == 'all') && $nickname) {
  216. $model = $model->where($userAlias . 'nickname|' . $userAlias . 'uid|' . $userAlias . 'phone', 'LIKE', "%$where[nickname]%");
  217. }
  218. //用户类型
  219. if (isset($where['user_type']) && $where['user_type']) {
  220. $model = $model->where($userAlias . 'user_type', $where['user_type']);
  221. }
  222. //用户性别
  223. if (isset($where['sex']) && $where['sex'] !== '' && in_array($where['sex'], [0, 1, 2])) {
  224. $model = $model->where(function ($query) use ($wechatUserAlias, $userAlias, $where) {
  225. $query->where($userAlias . 'sex', $where['sex']);
  226. });
  227. }
  228. //所在国家 所在省份 所在城市
  229. if ((isset($where['country']) && $where['country']) || (isset($where['province']) && $where['province']) || (isset($where['city']) && $where['city'])) {
  230. $model = $model->where(function ($query) use ($wechatUserAlias, $userAlias, $where) {
  231. $query->when(isset($where['country']) && $where['country'], function ($g) use ($wechatUserAlias, $userAlias, $where) {
  232. if ($where['country'] == 'domestic') {
  233. $g->where($wechatUserAlias . 'country', 'in', ['中国', 'China']);
  234. } else if ($where['country'] == 'abroad') {
  235. $g->whereOr($userAlias . 'addres', '');
  236. }
  237. })->when(isset($where['province']) && $where['province'], function ($q) use ($wechatUserAlias, $userAlias, $where) {
  238. $q->whereOr($wechatUserAlias . 'province', $where['province'])->whereOr($userAlias . 'provincials', 'Like', '%' . $where['province'] . '%')->whereOr($userAlias . 'addres', 'Like', '%' . $where['province'] . '%');
  239. })->when(isset($where['city']) && $where['city'], function ($c) use ($wechatUserAlias, $userAlias, $where) {
  240. $c->whereOr($wechatUserAlias . 'city', $where['city'])->whereOr($userAlias . 'provincials', 'Like', '%' . $where['city'] . '%')->whereOr($userAlias . 'addres', 'Like', '%' . $where['city'] . '%');
  241. });
  242. });
  243. }
  244. if (isset($where['time'])) {
  245. $model->withSearch(['time'], ['time' => $where['time'], 'timeKey' => 'u.add_time']);
  246. }
  247. return $field ? $model->field($field) : $model;
  248. }
  249. /**
  250. * 地域全部用户
  251. * @param $time
  252. * @param $userType
  253. * @return mixed
  254. */
  255. public function getRegionAll($time, $userType)
  256. {
  257. return $this->getModel()->when($userType != '', function ($query) use ($userType) {
  258. $query->where($this->alias . '.user_type', $userType);
  259. })->where(function ($query) use ($time) {
  260. $query->whereTime($this->alias . '.add_time', '<', strtotime($time[1]) + 86400)->whereOr($this->alias . '.add_time', NULL);
  261. })->field('count(' . $this->alias . '.uid) as allNum,' . $this->join_alis . '.province')
  262. ->group($this->join_alis . '.province')->select()->toArray();
  263. }
  264. /**
  265. * 地域新增用户
  266. * @param $time
  267. * @param $userType
  268. * @return mixed
  269. */
  270. public function getRegionNew($time, $userType)
  271. {
  272. return $this->getModel()->when($userType != '', function ($query) use ($userType) {
  273. $query->where($this->alias . '.user_type', $userType);
  274. })->where(function ($query) use ($time) {
  275. if ($time[0] == $time[1]) {
  276. $query->whereDay($this->alias . '.add_time', $time[0]);
  277. } else {
  278. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  279. $query->whereTime($this->alias . '.add_time', 'between', $time);
  280. }
  281. })->field('count(' . $this->alias . '.uid) as newNum,' . $this->join_alis . '.province')
  282. ->group($this->join_alis . '.province')->select()->toArray();
  283. }
  284. /**
  285. * 获取用户性别
  286. * @param $time
  287. * @param $userType
  288. * @return mixed
  289. */
  290. public function getSex($time, $userType)
  291. {
  292. return $this->getModel()->when($userType != '', function ($query) use ($userType) {
  293. $query->where($this->join_alis . '.user_type', $userType);
  294. })->where(function ($query) use ($time) {
  295. if ($time[0] == $time[1]) {
  296. $query->whereDay($this->alias . '.add_time', $time[0]);
  297. } else {
  298. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  299. $query->whereTime($this->alias . '.add_time', 'between', $time);
  300. }
  301. })->field($this->alias . '.uid,' . $this->alias . '.sex as u_name,' . $this->join_alis . '.sex as w_name')
  302. ->select()->toArray();
  303. }
  304. }