| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- <?php
- namespace app\api\service;
- use app\api\model\massager\Collect;
- use app\api\model\massager\Comment;
- use app\api\model\massager\Massager;
- use app\api\model\system\Message;
- use app\api\model\order\Order;
- use app\api\model\User;
- use app\api\model\user\Bill;
- use app\api\model\user\Voucher;
- use app\api\model\user\Wallet;
- use app\common\library\Auth;
- use app\common\model\Sms;
- use fast\Random;
- use GatewayWorker\Lib\Db;
- class UserService extends BaseService
- {
- private $model;
- private $userBillModel;
- private $walletModel;
- public function __construct()
- {
- $this->model = new User();
- $this->userBillModel = new Bill();
- $this->walletModel = new Wallet();
- }
- function findUser($user_id)
- {
- $user = $this->model->findById($user_id);
- if (!$user)
- return $this->fail("用户不存在!");
- return $this->ok(User::fmtUser($user));
- }
- function findWallet($user_id)
- {
- $w = $this->walletModel->getUserWallet($user_id);
- return [
- "user_id" => $w->user_id,
- "score" => $w->score,
- "money" => (float)$w->money,
- "give_money" => (float)$w->give_money,
- "total_money" => fixed2Float($w->money + $w->give_money),
- ];
- }
- function summary($user_id)
- {
- $wallet = $this->findWallet($user_id);
- return [
- "msg_count" => (new Message())->where(["to_user_id" => $user_id, "is_read" => 0])->count(),
- "collect_count" => (new Collect())->where(["user_id" => $user_id])->count(),
- "score" => $wallet["score"],
- "total_money" => $wallet["total_money"],
- "voucher_count" => (new Voucher())->fetchVoucherCount($user_id),
- "order_count" => (new Order())->where("user_id", $user_id)->count()
- ];
- }
- public function fetchCollectMassager($user_id, $page, $size)
- {
- $paginate = (new Collect())->fetchCollectMassager($user_id, $page, $size);
- $items = array_map(function ($data) {
- return $data["massager"];
- }, $paginate->items());
- foreach ($items as &$item) {
- $item['recently_work_date'] = null;
- $item['is_can_collet'] = false;
- unset($item['nearly_two_days_orders']);
- }
- return [
- array_map(function ($data) {
- return Massager::fmtMassager($data);
- }, $items),
- $paginate->total()
- ];
- }
- public function fetchMassagerComment($user_id, $page, $size)
- {
- $paginate = (new Comment())->fetchByUserId($user_id, $page, $size);
- return [
- $paginate->items(),
- $paginate->total()
- ];
- }
- public function fetchSystemMessage($user_id, $page, $size)
- {
- $messageModel = new Message();
- $paginate = $messageModel->fetchUserSystemMessage($user_id, $page, $size);
- $messageModel->update(["is_read" => 1], [
- "to_user_id" => $user_id,
- "is_read" => 0,
- "identity_type" => \E_IDENTITY_TYPE::User
- ]);
- return [
- $paginate->items(),
- $paginate->total()
- ];
- }
- public function findByUnionId($union_id)
- {
- return $this->model->findByUnionId($union_id);
- }
- public function bindAppWx($u_id, $params)
- {
- $user = $this->findByUnionId($params["unionId"]);
- if ($user)
- return $this->fail("该微信已经绑定用户,请解绑后再进行绑定!");
- $this->model->update([
- "app_openid" => $params["openId"],
- "union_id" => $params["unionId"],
- "nickname" => $params["nickName"],
- "avatar" => $params["avatarUrl"],
- ], ["id" => $u_id]);
- return $this->ok(true);
- }
- public function unbindWx($u_id)
- {
- $this->model->update([
- "app_openid" => null,
- "web_openid" => null,
- "union_id" => null,
- ], ["id" => $u_id]);
- return $this->ok(true);
- }
- public function bindMobile($u_id, $mobile, $sms_code)
- {
- $check = \app\common\library\Sms::check($mobile, $sms_code, "bind_mobile");
- if (!$check)
- return $this->fail("短信验证码不正确!");
- $user = $this->model->where("mobile", $mobile)->find();
- if ($user)
- return $this->fail("该手机号码已经绑定用户,无法继续绑定!");
- $this->model->update([
- "mobile" => $mobile
- ], ["id" => $u_id]);
- return $this->ok(true);
- }
- public function resetPwd($u_id, $sms_code, $new_password)
- {
- $user = $this->model->get($u_id);
- if (!$user || mb_strlen($user["mobile"]) != 11)
- return $this->fail("请先绑定手机再修改登录密码!");
- $check = \app\common\library\Sms::check($user["mobile"], $sms_code, "reset_pwd");
- if (!$check)
- return $this->fail("短信验证码不正确!");
- $this->model->update([
- "password" => $new_password
- ], ["id" => $u_id]);
- return $this->ok(true);
- }
- // 更新用户分组
- public static function updateGrouping($user_id)
- {
- $grouping_config = [
- [
- "grouping" => "D",
- "rule" => ["已消费总金额" => 0, "累计下单次数" => 0]
- ],
- [
- "grouping" => "C",
- "rule" => config("site.user_level_C")
- ],
- [
- "grouping" => "B",
- "rule" => config("site.user_level_B")
- ],
- [
- "grouping" => "A",
- "rule" => config("site.user_level_A")
- ],
- ];
- $total_consume_amount = Order::sumByUser($user_id);
- $now_place_order_count = Order::countByUser($user_id);
- $grouping = "D";
- foreach ($grouping_config as $item) {
- if ($item["rule"]["已消费总金额"] >= $total_consume_amount || $item["rule"]["累计下单次数"] >= $now_place_order_count)
- break;
- $grouping = $item["grouping"];
- }
- User::update([
- "grouping" => $grouping
- ], ["id" => $user_id]);
- return $grouping;
- }
- public function fetchInviteCount($user_id)
- {
- $change_type = \E_USER_BILL_CHANGE_TYPE::ProfitIncrease[0];
- $total_profit = \db()->query("select sum(`change`) as total_profit_amount from ma_user_bill where user_id = {$user_id} AND change_type = {$change_type}");
- $users = $this->model->where([
- "parent_id" => $user_id,
- "is_use_award" => 0,
- ])->field("id")->select();
- $user_ids = array_map(function ($data) {
- return $data['id'];
- }, $users);
- $two_users = $this->model->where("parent_id", "in", $user_ids)->where("is_use_award", 0)->field("id")->select();
- $two_user_ids = array_map(function ($data) {
- return $data['id'];
- }, $two_users);
- $user_ids_str = join(",", array_merge($user_ids, $two_user_ids));
- $order_status_str = join(",", ["'" . \E_ORDER_STATUS::Purchase . "'", "'" . \E_ORDER_STATUS::Proceed . "'", "'" . \E_ORDER_STATUS::WaitFeedback . "'"]);
- $total_wait_profit = 0;
- if (mb_strlen($user_ids_str) > 0) {
- $orders = \db()->query("
- SELECT
- o.user_id,
- o.total_real_amount,
- o.trip_amount
- FROM
- ma_user u
- JOIN ma_order o ON u.id = o.user_id
- WHERE
- u.id IN ({$user_ids_str})
- AND o.`status` IN ({$order_status_str})
- ORDER BY
- o.pay_time DESC
- ");
- foreach ($orders as $order) {
- $total_real_amount = ($order["total_real_amount"] - $order["trip_amount"]);
- if (in_array($order["user_id"], $user_ids)) {
- $item_wait_profit = fixed2Float($total_real_amount * (config("site.user_direct_invite_rate") / 100));
- } else {
- $item_wait_profit = fixed2Float($total_real_amount * (config("site.user_two_invite_rate") / 100));
- }
- $total_wait_profit += $item_wait_profit;
- }
- }
- return [
- "total_number" => $this->model->where("parent_id", $user_id)->count(),
- "total_profit_amount" => $total_profit[0]["total_profit_amount"] ?? 0,
- "total_wait_profit" => $total_wait_profit,
- "users" => $this->model->where("parent_id", $user_id)->field("id,nickname,avatar")->limit(4)->select()
- ];
- }
- public function fetchInviteDetails($user_id, $page = 1, $size = 10)
- {
- $paginate = $this->model->where("parent_id", $user_id)->field("id,nickname,avatar,is_use_award")->page($page)->paginate($size);
- return [
- $paginate->items(),
- $paginate->total()
- ];
- }
- public function fetchRanking()
- {
- $change_type = \E_USER_BILL_CHANGE_TYPE::ProfitIncrease[0];
- $rows = \db()->query("select user_id,sum(`change`) as total from ma_user_bill where change_type = {$change_type} group by user_id order by total DESC limit 20");
- $ids = array_map(function ($data) {
- return $data["user_id"];
- }, $rows);
- $users = $this->model
- ->where("id", "in", $ids)
- ->field("id,nickname,avatar")
- ->select();
- $fmt_users = [];
- foreach ($users as $user) {
- $fmt_users[$user["id"]] = $user;
- }
- return array_map(function ($data) use ($fmt_users) {
- $now = isset($fmt_users[$data["user_id"]]) ? $fmt_users[$data["user_id"]] : null;
- return [
- "user_id" => $data["user_id"],
- "nickname" => $now ? $now["nickname"] : null,
- "avatar" => $now ? $now["avatar"] : null,
- "total_profit_amount" => $data["total"]
- ];
- }, $rows);
- }
- }
|