| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- <?php
- namespace app\api\service;
- use app\admin\model\Agency;
- use app\admin\model\system\GrantVoucher;
- use app\api\model\massager\Collect;
- use app\api\model\massager\Comment;
- use app\api\model\massager\Massager;
- use app\api\model\User;
- use app\api\model\user\Area;
- use app\api\model\system\Feedback;
- use app\api\model\user\Wallet;
- use app\api\model\Voucher;
- use redis\RedLock;
- use think\Db;
- use think\Exception;
- class UserActionService extends BaseService
- {
- private $model;
- private $massagerModel;
- private $colletModel;
- private $commentModel;
- private $userAreaModel;
- public function __construct()
- {
- $this->model = new User();
- $this->massagerModel = new Massager();
- $this->colletModel = new Collect();
- $this->commentModel = new Comment();
- $this->userAreaModel = new Area();
- }
- public function modify($user_id, $params)
- {
- $this->model->allowField(true)->update(
- $params,
- ["id" => $user_id]
- );
- return $this->model->get($user_id);
- }
- public function cancel($user_id) {
- $this->model->where("id", $user_id)->delete();
- $this->ok();
- }
- /**
- * 收藏助教
- * @param $user_id
- * @param $massager_id
- * @return \SResult
- */
- public function colletMassager($user_id, $massager_id): \SResult
- {
- $relation = $this->colletModel->getByUserIdAndMassagerId($user_id, $massager_id);
- if ($relation)
- return $this->fail('您已经收藏!');
- Db::startTrans();
- try {
- $this->colletModel->save([
- 'user_id' => $user_id,
- 'massager_id' => $massager_id,
- 'createtime' => time(),
- 'updatetime' => time()
- ]);
- $this->massagerModel->where('id', $massager_id)->setInc("collect_count");
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- return $this->fail($e->getMessage());
- } finally {
- return $this->ok(true);
- }
- }
- /**
- * 取消助教
- * @param $user_id
- * @param $massager_id
- * @return \SResult
- */
- public function cancelColletMassager($user_id, $massager_id): \SResult
- {
- $relation = $this->colletModel->getByUserIdAndMassagerId($user_id, $massager_id);
- if (null === $relation)
- return $this->ok(true);
- Db::startTrans();
- try {
- $relation->delete();
- $this->massagerModel->where('id', $massager_id)->setDec("collect_count");
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- return $this->fail($e->getMessage());
- } finally {
- return $this->ok(true);
- }
- }
- /**
- * 评论助教
- * @param array $params
- * @return \SResult
- */
- public function commentMassager(array $params): \SResult
- {
- $total_count = $this->commentModel->where([
- "massager_id" => $params["massager_id"],
- "status" => \E_BASE_STATUS::Normal
- ])->count();
- $total_count += 1;
- $gte_3_count = $this->commentModel->where([
- 'massager_id' => $params['massager_id'],
- "status" => \E_BASE_STATUS::Normal
- ])->where("star", ">=", 3)->count();
- if ($params['star'] >= 3)
- $gte_3_count += 1;
- $praise_rate = 100;
- if ($total_count > 0 && $gte_3_count > 0) {
- $praise_rate = fixed2Float((($gte_3_count / $total_count)) * 100);
- }
- Db::startTrans();
- try {
- $this->commentModel->save([
- "user_id" => $params['user_id'],
- "massager_id" => $params["massager_id"],
- "is_anonymity" => $params["is_anonymity"],
- "tags" => $params["tags"],
- "star" => $params["star"],
- "content" => $params["content"],
- "status" => \E_BASE_STATUS::Normal,
- "createtime" => time(),
- "updatetime" => time()
- ]);
- $this->massagerModel
- ->where('id', $params["massager_id"])
- ->inc("comment_count")
- ->update(['praise_rate' => $praise_rate]);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- return $this->fail($e->getMessage());
- } finally {
- return $this->ok(true);
- }
- }
- /**
- * 添加地址
- * @param $user_id
- * @param $params
- * @return Area
- */
- public function addArea($user_id, $params)
- {
- $params['user_id'] = $user_id;
- return $this->userAreaModel->allowField(true)->create($params);
- }
- /**
- * 删除地址
- * @param $user_id
- * @param $area_id
- */
- public function delArea($user_id, $area_id)
- {
- return $this->userAreaModel->where([
- "id" => $area_id,
- "user_id" => $user_id
- ])->delete();
- }
- /**
- * 获取默认地址
- * @param $user_id
- * @return array|bool|false|\PDOStatement|string|\think\Model|null
- */
- public function getEnableArea($user_id)
- {
- return $this->userAreaModel->getEnableArea($user_id);
- }
- /**
- * 获取地址列表
- * @param $user_id
- * @return bool|false|\PDOStatement|string|\think\Collection
- */
- public function fetchArea($user_id)
- {
- return $this->userAreaModel->fetchArea($user_id);
- }
- /**
- * @param $user_id
- * @param $area_id
- * @return \SResult
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function changeAreaEnable($user_id, $area_id)
- {
- $area = $this->userAreaModel->where([
- "id" => $area_id,
- "user_id" => $user_id
- ])->find();
- if (!$area)
- return $this->fail("地址不存在");
- $this->userAreaModel->where("user_id", $user_id)->update(["enable" => 0]);
- $area->enable = 1;
- $area->update(["enable" => 1], [
- "id" => $area_id,
- "user_id" => $user_id
- ]);
- return $this->ok($area);
- }
- public function applyBeMassager($user_id, $params)
- {
- $m = $this->massagerModel->findByMobile($params["mobile"]);
- if ($m)
- return $this->fail("该手机号码已经申请等待审核!");
- $params["status"] = \E_MASSAGER_STATUS::Checking;
- $params["type"] = \E_SERVICE_TYPE::App;
- $this->massagerModel->allowField(true)->save($params);
- return $this->ok(true);
- }
- public function applyBeAgency($params)
- {
- $model = new Agency();
- $applyRecord = $model->where([
- "mobile" => $params["mobile"]
- ])->where("status", "in", [\E_BASE_STATUS::Default, "allow"])->find();
- if ($applyRecord)
- return $this->fail("该手机号码已经申请等待审核!");
- $model->save([
- "name" => $params["name"],
- "mobile" => $params["mobile"],
- "id_card" => isset($params["id_card"]) ? $params["id_card"] : null,
- "description" => isset($params["description"]) ? $params["description"] : null,
- "budget" => isset($params["budget"]) ? $params["budget"] : null,
- "license_images" => isset($params["license_images"]) ? $params["license_images"] : null,
- "area_ids" => isset($params["area_ids"]) ? $params["area_ids"] : null,
- "status" => \E_BASE_STATUS::Default,
- "createtime" => time(),
- "updatetime" => time()
- ]);
- return $this->ok(true);
- }
- public function feedback($user_id, $type, $content, $images, $reason, $mobile, $relation_type, $relation_id, $relation_name)
- {
- return (new Feedback())->save([
- "user_id" => $user_id,
- "type" => $type,
- "reason" => $reason,
- "mobile" => $mobile,
- "content" => $content,
- "images" => $images,
- "relation_type" => $relation_type,
- "relation_id" => $relation_id,
- "relation_name" => $relation_name,
- "status" => \E_BASE_STATUS::Default,
- "createtime" => time(),
- "updatetime" => time()
- ]);
- }
- public function fetchGrantVoucher($user_id)
- {
- $model = new GrantVoucher();
- $last = $model->where([
- "to_user_id" => $user_id,
- ])->order("id", "desc")->find();
- if (!$last || $last["draw"] == 1)
- return null;
- $last["vouchers"] = (new Voucher())->where("id", "in", explode(",", $last["voucher_ids"]))->order("takeAmount", "ASC")->select();
- $model->update(["is_read" => 1], ["id" => $last["id"]]);
- return $last;
- }
- public function grantVoucher($user_id, $gVoucherId)
- {
- $walletLock = new RedLock();
- $wLock = $walletLock->lock(Wallet::UWKey($user_id));
- if (!is_array($wLock))
- return $this->fail("请稍后再试");
- $model = new GrantVoucher();
- $last = $model->where([
- "id" => $gVoucherId,
- ])->find();
- if (!$last || $last["draw"] == 1)
- return $this->fail("数据异常!");
- $take_effect_time = Voucher::getTakeEffectTime($last["take_effect"]);
- $vouchers = (new Voucher())->where("id", "in", explode(",", $last["voucher_ids"]))->order("takeAmount", "ASC")->select();
- Db::startTrans();
- try {
- $uVoucher = [];
- foreach ($vouchers as $voucher) {
- array_push($uVoucher, [
- "user_id" => $user_id,
- "voucher_id" => $voucher["id"],
- "takeAmount" => $voucher["takeAmount"],
- "fullAmount" => $voucher["fullAmount"],
- "status" => \E_BASE_STATUS::Normal,
- "take_effect_time" => $take_effect_time,
- "expiretime" => $take_effect_time + (24 * 60 * 60) * $voucher["indate_day"],
- "createtime" => time(),
- "updatetime" => time()
- ]);
- }
- if (count($uVoucher) > 0) {
- (new \app\api\model\user\Voucher())->insertAll($uVoucher);
- }
- (new GrantVoucher())->update(["is_read" => 1, "draw" => 1], ["id" => $last["id"]]);
- Db::commit();
- return $this->ok(true);
- } catch (Exception $e) {
- Db::rollback();
- return $this->fail($e->getMessage());
- } finally {
- $walletLock->unlock($wLock);
- }
- }
- }
|