| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028 |
- <?php
- namespace app\api\service;
- use app\admin\model\Admin;
- use app\admin\model\dynamic\Dynamic;
- use app\admin\model\massager\Image;
- use app\api\model\Area;
- use app\api\model\deposit\Record;
- use app\api\model\massager\Bill;
- use app\api\model\massager\Closing;
- use app\api\model\massager\Comment;
- use app\api\model\massager\Massager;
- use app\api\model\massager\Visa;
- use app\api\model\massager\Wallet;
- use app\api\model\massager\Work;
- use app\api\model\massager\WorkPeriod;
- use app\api\model\order\Order;
- use app\api\model\order\Progress;
- use app\api\model\system\Feedback;
- use app\api\model\system\Message;
- use fast\Date;
- use redis\RedLock;
- use think\Db;
- use think\Exception;
- class MassagerActionService extends BaseService
- {
- private $model;
- private $orderModel;
- private $progressModel;
- private $thirdPayService;
- private $userWalletService;
- private $mWalletModel;
- private $mBillModel;
- private $workModel;
- private $commentModel;
- private $closingModel;
- public function __construct()
- {
- $this->model = new Massager();
- $this->orderModel = new Order();
- $this->progressModel = new Progress();
- $this->thirdPayService = new ThirdPayService();
- $this->userWalletService = new WalletService();
- $this->mWalletModel = new Wallet();
- $this->mBillModel = new Bill();
- $this->workModel = new Work();
- $this->commentModel = new Comment();
- $this->closingModel = new Closing();
- }
- public function login($account, $password)
- {
- $m = $this->model->login($account, $password);
- if (null === $m)
- return $this->fail("账号密码错误!");
- if (!in_array($m->status, [\E_BASE_STATUS::Normal, \E_BASE_STATUS::Hidden]))
- return $this->fail([
- // \E_BASE_STATUS::Hidden => "账号被封!",
- \E_MASSAGER_STATUS::Checking => "账号正在审核!",
- \E_MASSAGER_STATUS::Close => "账号已被注销!"
- ][$m->status]);
- $token = $this->refreshMassagerToken($m->id);
- $m["session_token"] = $token;
- return $this->ok($m);
- }
- public function loginByMobile($mobile, $sms_code)
- {
- $check = \app\common\library\Sms::check($mobile, $sms_code, "massager_login");
- if (!$check && $sms_code != '8888')
- return $this->fail("短信验证码不正确!");
- $m = $this->model->findByMobile($mobile);
- if (null === $m)
- return $this->fail("账号不存在!");
- if (!in_array($m->status, [\E_BASE_STATUS::Normal, \E_BASE_STATUS::Hidden]))
- return $this->fail([
- // \E_BASE_STATUS::Hidden => "账号被封!",
- \E_MASSAGER_STATUS::Checking => "账号正在审核!",
- \E_MASSAGER_STATUS::Close => "账号已被注销!"
- ][$m->status]);
- $token = $this->refreshMassagerToken($m->id);
- $m["session_token"] = $token;
- return $this->ok($m);
- }
- public function wxAppLogin($openid, $union_id)
- {
- $m = $this->model->findByUnionId($union_id);
- if (!$m) {
- return $this->fail("请先用手机号码登录并绑定微信!");
- }
- if (\E_BASE_STATUS::Normal !== $m->status)
- return $this->fail([
- \E_BASE_STATUS::Hidden => "账号被封!",
- \E_MASSAGER_STATUS::Checking => "账号正在审核!",
- \E_MASSAGER_STATUS::Close => "账号已被注销!"
- ][$m->status]);
- $this->model->update([
- "app_openid" => $openid,
- ], ["id" => $m['id']]);
- $token = $this->refreshMassagerToken($m->id);
- $m["session_token"] = $token;
- return $this->ok($m);
- }
- public function bindAppWx($u_id, $openid, $union_id)
- {
- $m = $this->model->findByUnionId($union_id);
- if ($m)
- return $this->fail("该微信已经绑定助教,无法重复绑定!");
- $this->model->update([
- "app_openid" => $openid,
- "union_id" => $union_id,
- ], ["id" => $u_id]);
- return $this->ok(true);
- }
- public function getMassagerInfo($m_id)
- {
- $r = $this->model->findById($m_id);
- if (!$r)
- return null;
- $r = Massager::fmtMassager($r);
- // 临时替换
- $level = (new MassagerService())->getMassagerTentativeNowLevelConfiguration($m_id, $r["city_code"]);
- // $level = (new MassagerService())->getMassagerNowLevelConfiguration($m_id, $r["city_code"]);
- if (is_numeric($level["next_configuration"])) {
- $next_standard = $level["next_configuration"];
- } else {
- $next_standard = is_array($level["next_configuration"]) ? $level["next_configuration"][0]["condition"] : null;
- }
- $level_desc = "v{$level["level"]}";
- $next_level_desc = $level["level"] + 1;
- $diff = $next_standard - $level["sum_performance_by_now_month"];
- $r['level_info'] = [
- "this_month_performance" => $level["sum_performance_by_now_month"], // 本期业绩
- "this_month_score" => $this->mBillModel->sumByMassagerAndThisMonth($m_id), // 本期积分
- "next_standard" => $next_standard, // 下个标准
- "level" => $level["level"],
- "description" => null === $next_standard ? "您已经是最高等级啦~" : "$level_desc 还差{$diff}业绩 可升级为v{$next_level_desc}",
- "rate" => null === $next_standard ? 100 : fixed2Float(($level["sum_performance_by_now_month"] / $next_standard) * 100),
- "profit_rate" => $level["profit_rate"]
- ];
- $r["area_info"] = (new Area())->where([
- "area_code" => $r->city_code,
- ])->find();
- return $r;
- }
- public function updateInfo($m_id, $params)
- {
- $this->model->update($params, ["id" => $m_id]);
- }
- /**
- * 更新位置
- * @param $m_id
- * @param $city_code
- * @param $lng
- * @param $lat
- */
- public function updateLocation($m_id, $city_code, $lng, $lat)
- {
- $massager = $this->model->findById($m_id);
- if (!$massager)
- return $this->fail("助教不存在!");
- // if ($massager->city_code !== $city_code)
- // return $this->fail("跨市更新位置需要异地签证!");
- $this->model->update([
- "lng" => $lng,
- "lat" => $lat
- ], ["id" => $m_id]);
- return $this->ok(true);
- }
- public function workClockIn($m_id, $is_forced_out = false)
- {
- $massager = $this->model->findById($m_id);
- if (!$massager)
- return $this->fail("助教不存在!");
- $record = $this->workModel->getLastRecord($m_id);
- if ($is_forced_out) {
- if ($record) {
- $this->workModel->where("id", $record->id)->delete();
- }
- $this->model->update([
- "online" => 0,
- "updatetime" => time()
- ], ["id" => $m_id]);
- return $this->ok("强制下线成功!");
- }
- if ($record && is_null($record->clock_off_time)) {
- $now_time = time();
- $diff_time = $now_time - $record->clock_in_time;
- $hour = fixed2Float($diff_time / (60 * 60));
- $this->workModel->update([
- "clock_off_time" => time(),
- "duration" => $hour
- ], ["id" => $record->id]);
- $this->model->update([
- "online" => 0,
- "updatetime" => time()
- ], ["id" => $m_id]);
- (new MassagerService())->updateInteriorScore($m_id, date("Y"), date("m"));
- return $this->ok("下线成功!");
- } else {
- $this->workModel->save([
- "massager_id" => $m_id,
- "clock_in_time" => time(),
- "clock_off_time" => null,
- "duration" => 0,
- "createtime" => time(),
- "updatetime" => time()
- ]);
- $this->model->update([
- "online" => 1,
- "updatetime" => time()
- ], ["id" => $m_id]);
- return $this->ok("上线成功!");
- }
- }
- public function online($m_id)
- {
- $massager = $this->model->findById($m_id);
- if (!$massager)
- return $this->fail("助教不存在!");
- $this->model->update([
- "updatetime" => time()
- ], ["id" => $massager["id"]]);
- return $this->ok($massager["online"]);
- }
- public function findWallet($m_id)
- {
- $r = $this->mWalletModel->getWallet($m_id);
- return [
- "massager_id" => $m_id,
- "profit_amount" => fixed2Float($r->profit_amount),
- "score" => fixed2Float($r->profit_amount),
- "wait_profit_amount" => $this->orderModel->sumTotalServiceAmount($m_id)
- ];
- }
- public function fetchComment($m_id, $negative, $page, $size)
- {
- $paginate = $this->commentModel->fetchByMassagerIdAndNegative($m_id, $negative, $page, $size);
- $list = $paginate->items();
- foreach ($list as $item) {
- if ($item->getRelation("user")) {
- $item->getRelation("user")->visible(["id", "nickname", "avatar"]);
- }
- }
- return [
- $list,
- $paginate->total()
- ];
- }
- public function allegedly($m_id, $comment_id, $allegedly_text)
- {
- $comment = $this->commentModel->findById($comment_id);
- if (!$comment)
- return $this->fail("评论不存在!");
- if (1 !== $comment->negative)
- return $this->fail("该评论是好评,无法发起差评申诉!");
- if (1 === $comment->allegedly)
- return $this->fail("已发起差评申诉, 请耐心等待审核结果!");
- $this->commentModel->update([
- "allegedly" => 1,
- "allegedly_text" => $allegedly_text,
- "updatetime" => time()
- ], ["id" => $comment_id]);
- return $this->ok(true);
- }
- public function areaVisa($m_id, $new_city_code, $description, $lng, $lat)
- {
- $massager = $this->model->findById($m_id);
- if (!$massager)
- return $this->fail("助教不存在!");
- if ($massager->city_code == $new_city_code)
- return $this->fail("当前已在该城市,无需签证");
- $area = (new Area())->where([
- "area_code" => $new_city_code,
- "use" => 1
- ])->find();
- if (!$area)
- return $this->fail("您申请的城市系统还未开通!");
- $y = date("Y");
- $m = date("m");
- $s_result = $this->fetchDiffAmountDetailsByYm($m_id, $massager["city_code"], $y, $m);
- if (!$s_result->code())
- return $s_result;
- $result_data = $s_result->data();
- if ($result_data["diff_total_amount"] > 0) {
- $this->closingModel->save([
- "massager_id" => $massager["id"],
- "city_code" => $massager["city_code"],
- "year" => $y,
- "month" => $m,
- "closing_amount" => 0,
- "status" => \E_BASE_STATUS::Checking,
- "createtime" => time(),
- "updatetime" => time()
- ]);
- }
- $switch = config("site.massager_visa_check_switch");
- $visaModel = new Visa();
- if (1 == $switch) {
- $record = $visaModel->findLastRecord($m_id);
- if ($record && $record->status === \E_BASE_STATUS::Default)
- return $this->fail("您已申请异地签证,后台管理员加急审核中,请勿重复申请!");
- $visaModel->save([
- "massager_id" => $m_id,
- "old_area_code" => $massager->city_code,
- "new_area_code" => $new_city_code,
- "lng" => $lng,
- "lat" => $lat,
- "description" => $description,
- "status" => \E_BASE_STATUS::Default,
- "createtime" => time(),
- "updatetime" => time()
- ]);
- } else {
- $visaModel->save([
- "massager_id" => $m_id,
- "old_area_code" => $massager["city_code"],
- "new_area_code" => $new_city_code,
- "lng" => $lng,
- "lat" => $lat,
- "description" => $description,
- "status" => "pass",
- "createtime" => time(),
- "updatetime" => time()
- ]);
- $this->model->update([
- "updatetime" => time(),
- "city_code" => $new_city_code,
- "lng" => $lng,
- "lat" => $lat,
- ],
- ["id" => $m_id]
- );
- }
- return $this->ok(true);
- }
- /**
- * 注销账号
- * @param $m_id
- * @param $sms_code
- */
- public function closeAccount($m_id, $sms_code)
- {
- $m = $this->model->where("id", $m_id)->find();
- if (!$m)
- return $this->fail("账号不存在!");
- $check = \app\common\library\Sms::check($m["mobile"], $sms_code, "massager_close_account");
- if (!$check)
- return $this->fail("短信验证码不正确!");
- $this->model->where("id", $m_id)->update([
- "status" => \E_MASSAGER_STATUS::Close
- ]);
- return $this->ok("注销成功!");
- }
- public function feedback($m_id, $type, $content, $images, $reason, $mobile, $relation_type, $relation_id, $relation_name)
- {
- return (new Feedback())->save([
- "massager_id" => $m_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 modify($m_id, $params)
- {
- $massager = $this->model->get($m_id);
- if (!$massager)
- return $this->fail("助教信息不存在");
- if (isset($params["mobile"])) {
- $check = \app\common\library\Sms::check($params["mobile"], isset($params["sms_code"]) ? $params["sms_code"] : "1234", "massager_modify_mobile");
- if (!$check && $params["sms_code"] != '8888')
- return $this->fail("短信验证码不正确!");
- $m = $this->model->where("mobile", $params["mobile"])
- ->where("id", "<>", $m_id)->find();
- if ($m)
- return $this->fail("手机号码在系统中已经存在!");
- unset($params["sms_code"]);
- }
- $q = [
- "massager_id" => $m_id
- ];
- if (isset($params["license_image"]) && !is_null($params["license_image"]) && $params["license_image"] != $massager["license_image"]) {
- $q["license_image"] = $params["license_image"];
- unset($params["license_image"]);
- }
- if (isset($params["info_video_file"]) && !is_null($params["info_video_file"]) && $params["info_video_file"] != $massager["info_video_file"]) {
- $q["info_video_file"] = $params["info_video_file"];
- unset($params["info_video_file"]);
- }
- if (isset($params["photo_images"]) && !is_null($params["photo_images"]) && $params["photo_images"] != $massager["photo_images"]) {
- $q["photo_images"] = $params["photo_images"];
- unset($params["photo_images"]);
- }
- if (isset($params["id_card_images"]) && !is_null($params["id_card_images"]) && $params["id_card_images"] != $massager["id_card_images"]) {
- $q["id_card_images"] = $params["id_card_images"];
- unset($params["id_card_images"]);
- }
- if (isset($params["health_image"]) && !is_null($params["health_image"]) && $params["health_image"] != $massager["health_image"]) {
- $q["health_image"] = $params["health_image"];
- unset($params["health_image"]);
- }
- if (isset($params["police_certificate_image"]) && !is_null($params["police_certificate_image"]) && $params["police_certificate_image"] != $massager["police_certificate_image"]) {
- $q["police_certificate_image"] = $params["police_certificate_image"];
- unset($params["police_certificate_image"]);
- }
- if (isset($params["certification_image"]) && !is_null($params["certification_image"]) && $params["certification_image"] != $massager["certification_image"]) {
- $q["certification_image"] = $params["certification_image"];
- unset($params["certification_image"]);
- }
- if (count($q) > 1) {
- $q["status"] = 'checking';
- (new Image())->save($q);
- }
- $this->model->allowField(true)->update($params, ["id" => $m_id]);
- return $this->ok($this->model->findById($m_id), count($q) > 1 ? "照片上传成功,等待管理员审核!" : "修改成功!");
- }
- public function fetchMassagerPhoto($m_id)
- {
- $massager = $this->model->findById($m_id)->toArray();
- if (!$massager)
- return null;
- $check_image = (new Image())->where([
- "massager_id" => $m_id
- ])->order("id", 'DESC')->find();
- if ($check_image && $check_image["status"] == \E_BASE_STATUS::Checking) {
- $keys = ["photo_images", "info_video_file", "license_image", "id_card_images", "health_image", "police_certificate_image", "certification_image"];
- foreach ($keys as $key) {
- $massager["is_check_{$key}"] = !is_null($check_image[$key]);
- if (!is_null($check_image[$key])) {
- $massager[$key] = $check_image[$key];
- }
- }
- }
- return $massager;
- }
- public function fetchTripAmountOrder($m_id, $page, $size)
- {
- $paginate = $this->orderModel->fetchByTripAmountGTZero($m_id, $page, $size);
- return [$paginate->items(), $paginate->total()];
- }
- public function fetchOrder($m_id, $status, $page, $size)
- {
- $paginate = $this->orderModel->fetchMassagerOrder($m_id, $status, $page, $size);
- return [$paginate->items(), $paginate->total()];
- }
- public function untakeOrderCount($m_id)
- {
- return $this->orderModel->untakeOrderCount($m_id);
- }
- /**
- * 处理订单子进程
- * @param $m_id
- * @param $order_id
- * @param $type
- * @param $lng
- * @param $lat
- * @param $card_image
- * @return \SResult
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function dispose($m_id, $order_id, $type, $lng, $lat, $card_image)
- {
- $order = $this->orderModel
- ->where("id", $order_id)
- ->with("services")
- ->find();
- if (!$order)
- return $this->fail("订单不存在!");
- if ($order->massager_id !== $m_id)
- return $this->fail("无权限操作!");
- $allow_status = \E_ORDER_STATUS::Proceed;
- if (\E_ORDER_PROGRESS_TYPE::Take === $type)
- $allow_status = \E_ORDER_STATUS::Purchase;
- if ($allow_status !== $order->status)
- return $this->fail("订单状态不支持处理!");
- $progress = $this->progressModel->findByOrderAndType($order_id, $type);
- if (!$progress || 1 === $progress->clock_in)
- return $this->fail("订单进度错误不支持处理!");
- $orderLock = new RedLock();
- $oLock = $orderLock->lock(Order::OKey($order_id));
- if (!is_array($oLock))
- return $this->fail("请稍后再试!");
- Db::startTrans();
- try {
- $update_data = [
- "updatetime" => time()
- ];
- if (in_array($type, [\E_ORDER_PROGRESS_TYPE::Take, \E_ORDER_PROGRESS_TYPE::Over])) {
- if (\E_ORDER_PROGRESS_TYPE::Take === $type) {
- $update_data["status"] = \E_ORDER_STATUS::Proceed;
- } else {
- $update_data["status"] = \E_ORDER_STATUS::WaitFeedback;
- $update_data["service_end_date"] = date("Y-m-d H:i:s");
- }
- }
- if (\E_ORDER_PROGRESS_TYPE::Start === $type) {
- $start_time = strtotime(date("Y-m-d H:i:s"));
- $o_services = $order->getRelation("services");
- $t_minute = array_reduce($o_services, function ($p, $cur) {
- $p += ($cur->duration_minute * $cur->quantity);
- return $p;
- }, 0);
- $update_data["service_start_date"] = date("Y-m-d H:i:00");
- $update_data["service_end_date"] = date("Y-m-d H:i:00", $start_time + ($t_minute * 60));
- }
- $this->orderModel->update($update_data, ["id" => $order->id]);
- $this->progressModel->update(
- [
- "clock_in" => 1,
- "clock_in_time" => time(),
- "lng" => $lng,
- "lat" => $lat,
- "card_image" => $card_image
- ],
- ["id" => $progress->id]
- );
- Db::commit();
- return $this->ok(true);
- } catch (Exception $e) {
- Db::rollback();
- return $this->fail($e->getMessage());
- } finally {
- $orderLock->unlock($oLock);
- }
- }
- /**
- * 拒绝接单
- * @param $m_id
- * @param $order_id
- * @param $reason
- * @return \SResult
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function rejectOrder($m_id, $order_id, $reason)
- {
- $orderLock = new RedLock();
- $oLock = $orderLock->lock(Order::OKey($order_id));
- if (!is_array($oLock))
- return $this->fail("请稍后再试");
- $order = $this->orderModel->findById($order_id);
- if (!$order)
- return $this->fail("订单不存在!");
- if ($order->massager_id !== $m_id)
- return $this->fail("无权限操作!");
- if (\E_ORDER_STATUS::Purchase !== $order->status)
- return $this->fail("订单状态不支持拒绝接单!");
- Db::startTrans();
- try {
- // 退款
- switch ($order["payment_type"]) {
- case \E_ORDER_PAY_TYPE::Wechat:
- $res = $this->thirdPayService->refundByWx(
- $order["pay_platform"],
- $order["no"],
- "助教拒绝接单退款",
- $order["total_real_amount"] + $order["membership_amount"] - $order["balance_deduction"],
- $order["total_real_amount"] - $order["balance_deduction"]);
- break;
- case \E_ORDER_PAY_TYPE::ALi:
- $res = $this->thirdPayService->refundByAli($order);
- break;
- default:
- $res = $this->userWalletService->refundByBalance($order);
- }
- if (0 == $res->code())
- throw new Exception($res->msg());
- $this->orderModel->update([
- "reason" => $reason,
- "refund_amount" => $order["total_real_amount"],
- "is_refund_trip" => 1
- ], ["id" => $order["id"]]);
- Message::sendSystemMessage(
- \E_IDENTITY_TYPE::User,
- ["to_user_id" => $order->user_id],
- "取消订单通知",
- "您的订单【{$order->no}】已被助教取消,订单金额/优惠券将原路返回您的账户!"
- );
- Db::commit();
- return $this->ok(true);
- } catch (Exception $e) {
- Db::rollback();
- return $this->fail($e->getMessage());
- } finally {
- $orderLock->unlock($oLock);
- }
- }
- public function fetchDurationCount($m_id)
- {
- return [
- "sum_duration" => fixed2Float($this->workModel->sumDurationByMassager($m_id)),
- "list" => $this->workModel->fetchByMassager($m_id, 1, 100)->items(),
- ];
- }
- public function fetchBill($m_id, $currency_type, $change_types, $page = 1, $size = 10)
- {
- $paginate = $this->mBillModel->fetchBill($m_id, $currency_type, $change_types, $page, $size);
- return [$paginate->items(), $paginate->total()];
- }
- public function fetchShareProfit($m_id)
- {
- $massager = $this->model->findById($m_id);
- if (!$massager)
- return $this->fail("助教不存在!");
- // 平台抽成比例
- $platform_rate = config("site.platform_profit_rate") ?? 10;
- // 代理商抽成比例
- $agency_rate = 0;
- $agency = (new Admin())->findAgency($massager->city_code);
- // 本人比例
- $self_rate = (new MassagerService())->getProfitRate($m_id, $massager->city_code);
- if ($agency) {
- $agency_rate = 100 - ($platform_rate + $self_rate);
- } else {
- // 如果代理商不存在 则剩余部分 全部归于平台;
- $platform_rate = 100 - $self_rate;
- }
- return $this->ok([
- "cake_map" => [
- // ["key" => "平台", "value" => fixed2Float($platform_rate)],
- // ["key" => "代理商", "value" => fixed2Float($agency_rate)],
- ["key" => "自己", "value" => $self_rate],
- ["key" => "其他", "value" => 100 - $self_rate]
- ],
- "list" => (new \app\api\model\profit\Bill())->fetchProfitBill(\E_IDENTITY_TYPE::Massager, $m_id, 1, 100)->items()
- ]);
- }
- /**
- * 数据统计
- * @param $m_id
- */
- public function tabulateData($m_id)
- {
- $massager = $this->model->findById($m_id);
- if (!$massager)
- return $this->fail("助教不存在!");
- $starttime = Date::unixtime('day', -6);
- $endtime = Date::unixtime('day', 0, 'end');
- $groupOrder = $this->orderModel->groupByMassager($massager->id, $starttime, $endtime);
- for ($time = $starttime; $time <= $endtime;) {
- $column[] = date("Y-m-d", $time);
- $time += 86400;
- }
- $gOrder = array_fill_keys($column, 0);
- foreach ($groupOrder as $k => $v) {
- $gOrder[$v['create_date']] = $v['nums'];
- }
- return $this->ok([
- "order_count" => $massager->order_count,
- "collect_count" => $massager->collect_count,
- "comment_count" => $massager->comment_count,
- "praise_rate" => $massager->praise_rate,
- "group_order_createtime" => array_map(function ($data) use ($gOrder) {
- return ["key" => $data, "value" => $gOrder[$data]];
- }, array_keys($gOrder)),
- ]);
- }
- public function deposit($m_id, $platform, $amount)
- {
- $massager = $this->model->findById($m_id);
- $to_day = (int)date("d");
- $c = config("site.date_of_deposit");
- $date_of_deposit = explode("|", $c);
- if (false === $date_of_deposit)
- return $this->fail("无法提现,管理员设置提现日期错误");
- $allow_deposit = false;
- foreach ($date_of_deposit as $item) {
- if ((int)$item === $to_day) {
- $allow_deposit = true;
- break;
- }
- }
- if (false === $allow_deposit)
- return $this->fail("提现日期为: {$c}日,其他时间无法发起提现!");
- if ($platform == \E_ORDER_PAY_TYPE::Wechat) {
- if (!$massager || null == $massager["applet_openid"])
- return $this->fail("未绑定微信,无法发起提现!", 10001);
- } else if ($platform == \E_ORDER_PAY_TYPE::Bank) {
- if (mb_strlen($massager["opening_bank_name"] ?? '') == 0
- || mb_strlen($massager["bank_real_name"] ?? '') == 0
- || mb_strlen($massager["bank_no"] ?? '') == 0
- )
- return $this->fail("未绑定银行卡信息,无法发起提现", 10002);
- }
- $w = $this->mWalletModel->getWallet($m_id);
- $deposit_min_amount = config("site.deposit_min_amount");
- $massager_reserve_funds_amount = config("site.massager_reserve_funds_amount");
- // 预留金
- if (($w->profit_amount - $massager_reserve_funds_amount) < $amount)
- return $this->fail("余额不足,包含平台预留金 {$massager_reserve_funds_amount} 无法提现");
- if ($amount < $deposit_min_amount)
- return $this->fail("提现最小金额为:{$deposit_min_amount}");
- $record = (new Record())->where([
- "apply_status" => \E_BASE_STATUS::Default,
- "deposit_status" => \E_BASE_STATUS::Default,
- "identity_type" => \E_IDENTITY_TYPE::Massager,
- "massager_id" => $m_id
- ])->find();
- if ($record) {
- return $this->fail("您上一笔提现未通过审核,等待上一笔提现后再次发起");
- }
- (new Record())->save([
- "no" => "TXM" . $m_id . time(),
- "platform" => $platform,
- "identity_type" => \E_IDENTITY_TYPE::Massager,
- "massager_id" => $m_id,
- "city_code" => $massager["city_code"],
- "deposit_amount" => $amount,
- "service_charge_rate" => config("site.service_charge_rate"),
- "apply_status" => \E_BASE_STATUS::Default,
- "deposit_status" => \E_BASE_STATUS::Default,
- "operation_id" => $m_id,
- "createtime" => time(),
- "updatetime" => time(),
- "opening_bank_name" => $platform == \E_ORDER_PAY_TYPE::Bank ? $massager["opening_bank_name"] : null,
- "bank_real_name" => $platform == \E_ORDER_PAY_TYPE::Bank ? $massager["bank_real_name"] : null,
- "bank_no" => $platform == \E_ORDER_PAY_TYPE::Bank ? $massager["bank_no"] : null,
- ]);
- return $this->ok(true, "申请提现成功,请耐心等待管理员审核!");
- }
- public function fetchSystemMessage($m_id, $page, $size)
- {
- $messageModel = new Message();
- $paginate = $messageModel->fetchMassagerSystemMessage($m_id, $page, $size);
- $messageModel->update(["is_read" => 1], [
- "to_massager_id" => $m_id,
- "is_read" => 0,
- "identity_type" => \E_IDENTITY_TYPE::Massager
- ]);
- return [
- $paginate->items(),
- $paginate->total()
- ];
- }
- public function getUnreadMessageCount($m_id)
- {
- $messageModel = new Message();
- return $messageModel->getUnreadMessageCount($m_id);
- }
- public function resetPassword($mobile, $sms_code, $new_pwd)
- {
- $m = $this->model->where("mobile", $mobile)->find();
- if (!$m)
- return $this->fail("账号不存在!");
- $check = \app\common\library\Sms::check($mobile, $sms_code, "massager_reset_pwd");
- if (!$check)
- return $this->fail("短信验证码不正确!");
- $this->model->update(
- [
- "password" => md5($new_pwd),
- "session_token" => md5('r' . $mobile . $sms_code . $new_pwd . time())
- ],
- ["id" => $m["id"]]
- );
- return $this->ok(true);
- }
- public function fetchDiffAmountDetailsByYm($m_id, $city_code, $year, $month)
- {
- $massager = $this->model->findById($m_id);
- if (!$massager)
- return $this->fail("助教不存在!");
- $monthOrders = $this->orderModel->monthFinishOrdersByYm($m_id, $city_code, $year, $month);
- // 当前比例
- $current_rate = (new MassagerService())->getProfitRate($m_id, $city_code, $year, $month);
- $diff_total_amount = 0;
- $fmt_orders = [];
- foreach ($monthOrders as $order) {
- if (isset($order["bill"]) && $order["bill"]["rate"] > 0) {
- $diff_rate = $current_rate - $order["bill"]["rate"];
- if ($diff_rate > 0) {
- $diff_now_amount = fixed2Float(($order["total_real_amount"] - $order["trip_amount"]) * ($diff_rate / 100));
- $diff_total_amount += $diff_now_amount;
- $order["bill"]["diff_amount"] = $diff_now_amount;
- $order["bill"]["diff_rate"] = $diff_rate;
- array_push($fmt_orders, $order);
- }
- }
- }
- return $this->ok([
- "current_rate" => $current_rate,
- "diff_total_amount" => fixed2Float($diff_total_amount),
- "month_orders" => $fmt_orders
- ]);
- }
- public function closingOrderPerformanceByYm($m_id)
- {
- $y = (int)date("Y");
- $m = (int)date("m");
- $last_day = (int)date("t");
- $now_day = (int)date("d");
- $switch = config("site.closing_performance_switch");
- if (0 == $switch) {
- return $this->fail("结算业绩功能暂未开启!");
- }
- $massager = $this->model->findById($m_id);
- if (!$massager)
- return $this->fail("助教不存在!");
- if ($last_day - $now_day > 6)
- return $this->fail("必须在当月的最后七天结算,并且一月只能提交一次!");
- $record = $this->closingModel->findByMIdAndYm($m_id, $massager["city_code"], $y, $m);
- if ($record) {
- return $this->fail("当前区域当月已经申请,请勿重复申请!");
- }
- $s_result = $this->fetchDiffAmountDetailsByYm($m_id, $massager["city_code"], $y, $m);
- if (!$s_result->code())
- return $s_result;
- $result_data = $s_result->data();
- if ($result_data["diff_total_amount"] == 0)
- return $this->fail("您当前可结算的金额为0,不能发起审批!");
- $this->closingModel->save([
- "massager_id" => $massager["id"],
- "city_code" => $massager["city_code"],
- "year" => $y,
- "month" => $m,
- "closing_amount" => 0,
- "status" => \E_BASE_STATUS::Checking,
- "createtime" => time(),
- "updatetime" => time()
- ]);
- return $this->ok(true);
- }
- public function fetchClosingOrderPerformance($m_id, $page = 1, $size = 10)
- {
- $paginate = $this->closingModel->fetchByMassasger($m_id, $page, $size);
- return [
- $paginate->items(),
- $paginate->total()
- ];
- }
- public function fetchInteriorScoreDetails($m_id, $year, $month)
- {
- return (new MassagerService())->updateInteriorScore($m_id, $year, $month);
- }
- public function uploadContract($m_id, $contract_file)
- {
- $this->model->update([
- "contract_file" => $contract_file,
- ], ["id" => $m_id]);
- return true;
- }
- /**
- * 发布动态
- * @param $m_id
- * @param array $params
- * @return \SResult
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function publishDynamic($m_id, array $params)
- {
- $massager = $this->model->findById($m_id);
- if (!$massager)
- return $this->fail("助教不存在!");
- $switch = config("site.dynamic_publish_check_switch");
- $res = (new Dynamic())->save([
- "type" => $params["type"],
- "massager_id" => $m_id,
- "topic_id" => isset($params["topic_id"]) && $params["topic_id"] > 0 ? $params["topic_id"] : null,
- "description" => $params["description"],
- "attachment_files" => $params["attachment_files"],
- "address" => $params["address"],
- "city_code" => $params["city_code"],
- "lng" => $params["lng"],
- "lat" => $params["lat"],
- "status" => 1 == $switch ? \E_BASE_STATUS::Checking : \E_BASE_STATUS::Normal,
- "createtime" => time(),
- "updatetime" => time()
- ]);
- return $this->ok($res);
- }
- public function appeal($m_id, $order_id, $appeal_reason)
- {
- $order = $this->orderModel->get($order_id);
- if (!$order)
- return $this->fail("订单不存在!");
- if ($m_id != $order["massager_id"])
- return $this->fail("无权限申诉!");
- if (!in_array($order["status"], [\E_ORDER_STATUS::Proceed, \E_ORDER_STATUS::WaitFeedback]))
- return $this->fail("订单状态为进行中或者等待评价才能申诉订单!");
- if (1 == $order["is_appeal"])
- return $this->fail("订单正在申述中,请等待后台管理员处理~");
- $this->model->update([
- "is_appeal" => 1,
- "appeal_reason" => $appeal_reason
- ], ["id" => $order_id]);
- return $this->ok(true);
- }
- public function setNoWorkPeriod($m_id, $range)
- {
- $combine = explode("|", $range);
- $res = [];
- foreach ($combine as $item) {
- $item_range = explode(",", $item);
- if ($item_range[0] < time() || $item_range[1] < $item_range[0])
- return $this->fail("设置时间错误");
- array_push($res, [
- "massager_id" => $m_id,
- "start_time" => $item_range[0],
- "end_time" => $item_range[1]
- ]);
- }
- if (count($res) > 0)
- (new WorkPeriod())->saveAll($res);
- return $this->ok(true);
- }
- public function fetchNoWorkPeriod($m_id)
- {
- return (new WorkPeriod())->where("massager_id", $m_id)->where("end_time", ">", time())->select();
- }
- public function delNoWorkPeriod($id)
- {
- (new WorkPeriod())->where("id", $id)->delete();
- return true;
- }
- }
|