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); } } }