UserLogic.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\openapi\logic\user;
  20. use app\adminapi\logic\distribution\DistributionLogic;
  21. use app\common\{logic\BaseLogic,
  22. model\User,
  23. model\Order,
  24. enum\PayEnum,
  25. model\UserLevel,
  26. model\UserLabel,
  27. enum\CouponEnum,
  28. model\CouponList,
  29. enum\AccountLogEnum,
  30. model\UserLabelIndex,
  31. logic\AccountLogLogic,
  32. enum\UserTerminalEnum};
  33. use think\facade\Db;
  34. use think\Model;
  35. /**
  36. * 用户逻辑层
  37. * Class UserLogic
  38. * @package app\adminapi\logic\user
  39. */
  40. class UserLogic extends BaseLogic
  41. {
  42. /**
  43. * @notes 用户概况页面
  44. * @return array
  45. * @author cjhao
  46. * @date 2021/8/17 14:58
  47. */
  48. public function index():array
  49. {
  50. $today = strtotime(date('Y-m-d'));
  51. //用户数
  52. $userCount = User::count();
  53. //今日新增用户数
  54. $userNewCount = User::where('create_time','>=',$today)->count();
  55. //成交用户数
  56. $repetitionCount = Order::where(['pay_status'=>PayEnum::ISPAID])->count();
  57. //复购用户数
  58. $purchaseCount = Order::where(['pay_status'=>PayEnum::ISPAID])
  59. ->group('user_id')
  60. ->having('count(user_id) >= 2')
  61. ->count();
  62. $dayList = day_time(14,true);
  63. $dayList = array_reverse($dayList);
  64. $echarts_data = [];
  65. //图表数据
  66. foreach ($dayList as $dayKey => $dayVal){
  67. $newUserCount = User::whereTime('create_time','between',[$dayVal,$dayVal+86399])->count();
  68. $echarts_data[] = [
  69. 'day' => date('m-d',$dayVal),
  70. 'user_new_count' => $newUserCount,
  71. ];
  72. }
  73. $data = [
  74. 'user_count' => $userCount,
  75. 'user_new_count' => $userNewCount,
  76. 'repetition_count' => $repetitionCount,
  77. 'purchase_count' => $purchaseCount,
  78. 'echarts_data' => $echarts_data,
  79. ];
  80. return $data;
  81. }
  82. /**
  83. * @notes 用户搜索条件列表
  84. * @return array
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. * @author cjhao
  89. * @date 2021/8/10 16:58
  90. */
  91. public function otherLists(): array
  92. {
  93. $userLevel = UserLevel::order('rank', 'asc')->field('id,name')->select();
  94. $userLabel = UserLabel::field('id,name')->select();
  95. $sourceList = UserTerminalEnum::getTermInalDesc();
  96. return [
  97. 'user_level_list' => $userLevel,
  98. 'user_label_list' => $userLabel,
  99. 'source_list' => $sourceList,
  100. ];
  101. }
  102. /**
  103. * @notes 设置用户标签
  104. * @param array $param
  105. * @return bool
  106. * @throws \Exception
  107. * @author cjhao
  108. * @date 2021/8/17 11:43
  109. */
  110. public function setLabel(array $param): bool
  111. {
  112. $userIds = $param['user_ids'] ?? [];
  113. $labelIds = $param['label_ids'] ?? [];
  114. $labelIds = UserLabel::where(['id' => $labelIds])->column('id');
  115. //当前用户已绑定的标签
  116. $userLabelIndexList = UserLabelIndex::where(['user_id' => $userIds])
  117. ->group('user_id')
  118. ->column('group_concat(label_id Separator \',\') as user_label_id', 'user_id');
  119. $addData = [];
  120. foreach ($userIds as $userId) {
  121. $userLabelIndex = $userLabelIndexList[$userId]['user_label_id'] ?? '';
  122. $userLabelIds = explode(',', $userLabelIndex);
  123. foreach ($labelIds as $labelId) {
  124. //该用户已有该标签,跳过
  125. if (in_array($labelId, $userLabelIds)) {
  126. continue;
  127. }
  128. $addData[] = [
  129. 'user_id' => $userId,
  130. 'label_id' => $labelId,
  131. ];
  132. }
  133. }
  134. //写入数据
  135. if ($addData) {
  136. (new UserLabelIndex)->saveAll($addData);
  137. }
  138. return true;
  139. }
  140. /**
  141. * @notes 用户详情
  142. * @param int $userId
  143. * @return mixed
  144. * @throws \think\db\exception\DataNotFoundException
  145. * @throws \think\db\exception\DbException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. * @author cjhao
  148. * @date 2021/8/18 15:52
  149. */
  150. public function detail($mobile)
  151. {
  152. $user = User::field('id,nickname,avatar,sex,mobile,birthday,level,create_time,user_money,user_earnings,user_money+user_earnings as total_user_money,user_integral,disable,user_delete')
  153. ->with('user_level')
  154. ->where(['mobile'=>$mobile])->find()->toArray();
  155. if($user){
  156. $user['user_level_name'] = $user['name'];
  157. unset($user['name']);
  158. unset($user['rank']);
  159. unset($user['discount']);
  160. }
  161. return $user;
  162. }
  163. /**
  164. * @notes 更新用户信息
  165. * @param array $params
  166. * @return bool
  167. * @author cjhao
  168. * @date 2021/8/18 17:21
  169. */
  170. public function setUserInfo(array $params):bool
  171. {
  172. User::where(['id'=>$params['user_id']])->update([$params['field']=>$params['value']]);
  173. return true;
  174. }
  175. /**
  176. * @notes 设置用户标签
  177. * @param array $params
  178. * @return bool
  179. * @throws \Exception
  180. * @author cjhao
  181. * @date 2021/8/19 11:31
  182. */
  183. public function setUserLabel(array $params):bool {
  184. //先删除用户标签,在重新设置用户标签
  185. UserLabelIndex::where(['user_id'=>$params['user_id']])->delete();
  186. $addData = [];
  187. foreach ($params['label_ids'] as $labelId){
  188. $addData[] = [
  189. 'user_id' => $params['user_id'],
  190. 'label_id' => $labelId,
  191. ];
  192. }
  193. if($addData){
  194. (new UserLabelIndex)->saveAll($addData);
  195. }
  196. return true;
  197. }
  198. /**
  199. * @notes 调整用户余额
  200. * @param array $params
  201. * @return string
  202. * @author cjhao
  203. * @date 2021/9/10 18:15
  204. */
  205. public function adjustUserWallet(array $params)
  206. {
  207. Db::startTrans();
  208. try {
  209. $user = User::find($params['user_id']);
  210. switch ($params['type']){
  211. case 1:
  212. //增加
  213. if(1 == $params['action']){
  214. //调整可用余额
  215. $user->user_money = $user->user_money + $params['num'];
  216. $user->save();
  217. //记录日志
  218. AccountLogLogic::add($user->id, AccountLogEnum::BNW_INC_ADMIN, AccountLogEnum::INC, $params['num'], '', $params['remark'] ?? '');
  219. }else{
  220. $user->user_money = $user->user_money - $params['num'];
  221. $user->save();
  222. //记录日志
  223. AccountLogLogic::add($user->id, AccountLogEnum::BNW_DEC_ADMIN,AccountLogEnum::DEC, $params['num'], '', $params['remark'] ?? '');
  224. }
  225. break;
  226. case 2:
  227. //增加
  228. if(1 == $params['action']){
  229. //调整可用余额
  230. $user->user_earnings = $user->user_earnings + $params['num'];
  231. $user->save();
  232. //记录日志
  233. AccountLogLogic::add($user->id, AccountLogEnum::BW_INC_ADMIN, AccountLogEnum::INC, $params['num'], '', $params['remark'] ?? '');
  234. }else{
  235. $user->user_earnings = $user->user_earnings - $params['num'];
  236. $user->save();
  237. //记录日志
  238. AccountLogLogic::add($user->id, AccountLogEnum::BW_DEC_ADMIN,AccountLogEnum::DEC, $params['num'], '', $params['remark'] ?? '');
  239. }
  240. break;
  241. case 3:
  242. //增加
  243. if(1 == $params['action']){
  244. //调整可用余额
  245. $user->user_integral = $user->user_integral + $params['num'];
  246. $user->save();
  247. //记录日志
  248. AccountLogLogic::add($user->id, AccountLogEnum::INTEGRAL_INC_ADMIN, AccountLogEnum::INC, $params['num'], '', $params['remark'] ?? '');
  249. }else{
  250. $user->user_integral = $user->user_integral - $params['num'];
  251. $user->save();
  252. //记录日志
  253. AccountLogLogic::add($user->id, AccountLogEnum::INTEGRAL_DEC_ADMIN,AccountLogEnum::DEC, $params['num'], '', $params['remark'] ?? '');
  254. }
  255. break;
  256. }
  257. Db::commit();
  258. return true;
  259. } catch (\Exception $e) {
  260. Db::rollback();
  261. return $e->getMessage();
  262. }
  263. }
  264. /**
  265. * @notes 用户信息
  266. * @author Tab
  267. * @date 2021/9/13 19:33
  268. */
  269. public static function info($params)
  270. {
  271. $info = User::findOrEmpty($params['user_id'])->toArray();
  272. if (empty($info)) {
  273. $info = '';
  274. } else {
  275. $info = $info['sn'] . '(' . $info['nickname'] . ')';
  276. }
  277. $count = User::where('inviter_id', $params['user_id'])->count();
  278. return [
  279. 'name' => $info,
  280. 'count' => $count
  281. ];
  282. }
  283. /**
  284. * @notes 上级分销商调整信息
  285. * @param $params
  286. * @return array
  287. * @author Tab
  288. * @date 2021/9/14 10:50
  289. */
  290. public static function adjustFirstLeaderInfo($params)
  291. {
  292. $userField = [
  293. 'id',
  294. 'sn',
  295. 'nickname',
  296. 'first_leader'
  297. ];
  298. $user = User::field($userField)->findOrEmpty($params['user_id'])->toArray();
  299. if(empty($user['first_leader'])) {
  300. $firstLeader = '系统';
  301. } else {
  302. $firstLeaderField = [
  303. 'id',
  304. 'sn',
  305. 'nickname',
  306. ];
  307. $firstLeader = User::field($firstLeaderField)->findOrEmpty($user['first_leader'])->toArray();
  308. }
  309. return [
  310. 'user' => $user,
  311. 'first_leader' => $firstLeader
  312. ];
  313. }
  314. /**
  315. * @notes
  316. * @param $params
  317. * @return bool
  318. * @author Tab
  319. * @date 2021/9/14 11:42
  320. */
  321. public static function adjustFirstLeader($params)
  322. {
  323. Db::startTrans();
  324. try {
  325. switch($params['type']) {
  326. // 指定推荐人
  327. case 'assign':
  328. $formatData = self::assignFirstLeader($params);
  329. break;
  330. // 设置推荐人为系统,即清空上级
  331. case 'system':
  332. $formatData = self::clearFirstLeader($params);
  333. break;
  334. }
  335. $user = User::findOrEmpty($params['user_id']);
  336. // 旧关系链
  337. if (!empty($user->ancestor_relation)) {
  338. $old_ancestor_relation = $user->id . ',' .$user->ancestor_relation;
  339. } else {
  340. $old_ancestor_relation = $user->id;
  341. }
  342. // 更新当前用户的分销关系
  343. User::where(['id' => $params['user_id']])->update($formatData);
  344. //更新当前用户下级的分销关系
  345. $data = [
  346. 'second_leader' => $formatData['first_leader'],
  347. 'third_leader' => $formatData['second_leader'],
  348. 'update_time' => time(),
  349. ];
  350. User::where(['first_leader' => $params['user_id']])->update($data);
  351. //更新当前用户下下级的分销关系
  352. $data = [
  353. 'third_leader' => $formatData['first_leader'],
  354. 'update_time' => time()
  355. ];
  356. User::where(['second_leader' => $params['user_id']])->update($data);
  357. //更新当前用户所有后代的关系链
  358. $posterityArr = User::field('id,ancestor_relation')
  359. ->whereFindInSet('ancestor_relation', $params['user_id'])
  360. ->select()
  361. ->toArray();
  362. $updateData = [];
  363. $replace_ancestor_relation = $params['user_id'] . ','. $formatData['ancestor_relation'];
  364. foreach($posterityArr as $item) {
  365. $replace = substr($item['ancestor_relation'], strpos($item['ancestor_relation'], $params['user_id']));
  366. $updateData[] = [
  367. 'id' => $item['id'],
  368. 'ancestor_relation' => trim(str_replace($old_ancestor_relation, $replace_ancestor_relation, $item['ancestor_relation']), ',')
  369. ];
  370. }
  371. // 批量更新
  372. (new User())->saveAll($updateData);
  373. Db::commit();
  374. return true;
  375. } catch(\Exception $e) {
  376. Db::rollback();
  377. self::setError($e->getMessage());
  378. return false;
  379. }
  380. }
  381. /**
  382. * @notes 指定上级分销商
  383. * @param $params
  384. * @return array
  385. * @throws \think\Exception
  386. * @author Tab
  387. * @date 2021/9/14 11:44
  388. */
  389. public static function assignFirstLeader($params)
  390. {
  391. if (empty($params['first_id'])) {
  392. throw new \think\Exception('请选择上级分销商');
  393. }
  394. if ($params['first_id'] == $params['user_id']) {
  395. throw new \think\Exception('上级分销商不可以选择自己');
  396. }
  397. $firstLeader = User::field(['id', 'first_leader', 'second_leader', 'third_leader', 'ancestor_relation'])
  398. ->where('id', $params['first_id'])
  399. ->findOrEmpty()
  400. ->toArray();
  401. if(empty($firstLeader)) {
  402. throw new \think\Exception('分销商不存在');
  403. }
  404. $ancestorRelation =explode(',', $firstLeader['ancestor_relation']);
  405. if(!empty($ancestorRelation) && in_array($params['user_id'], $ancestorRelation)) {
  406. throw new \think\Exception('不允许填写自己任一下级的邀请码');
  407. }
  408. // 上级
  409. $first_leader_id = $firstLeader['id'];
  410. // 上上级
  411. $second_leader_id = $firstLeader['first_leader'];
  412. // 上上上级
  413. $third_leader_id = $firstLeader['second_leader'];
  414. // 拼接关系链
  415. $firstLeader['ancestor_relation'] = $firstLeader['ancestor_relation'] ?: ''; // 清空null值及0
  416. $my_ancestor_relation = $first_leader_id. ',' . $firstLeader['ancestor_relation'];
  417. // 去除两端逗号
  418. $my_ancestor_relation = trim($my_ancestor_relation, ',');
  419. $data = [
  420. 'first_leader' => $first_leader_id,
  421. 'second_leader' => $second_leader_id,
  422. 'third_leader' => $third_leader_id,
  423. 'ancestor_relation' => $my_ancestor_relation,
  424. 'admin_update_leader' => 1,
  425. ];
  426. return $data;
  427. }
  428. /**
  429. * @notes 清空上级
  430. * @param $params
  431. * @return array
  432. * @author Tab
  433. * @date 2021/9/14 11:46
  434. */
  435. public static function clearFirstLeader($params)
  436. {
  437. $data = [
  438. 'first_leader' => 0,
  439. 'second_leader' => 0,
  440. 'third_leader' => 0,
  441. 'ancestor_relation' => '',
  442. 'admin_update_leader' => 1,
  443. ];
  444. return $data;
  445. }
  446. }