MassagerService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. <?php
  2. namespace app\api\service;
  3. use app\admin\model\dynamic\Dynamic;
  4. use app\admin\model\dynamic\Like;
  5. use app\api\model\massager\Collect;
  6. use app\api\model\massager\Comment;
  7. use app\api\model\massager\Massager;
  8. use app\api\model\massager\Work;
  9. use app\api\model\massager\WorkPeriod;
  10. use app\api\model\order\Order;
  11. use app\api\model\service\Service;
  12. class MassagerService extends BaseService
  13. {
  14. private $massagerModel;
  15. private $colletModel;
  16. private $commentModel;
  17. private $orderModel;
  18. private $workModel;
  19. public function __construct()
  20. {
  21. $this->massagerModel = new Massager();
  22. $this->colletModel = new Collect();
  23. $this->commentModel = new Comment();
  24. $this->orderModel = new Order();
  25. $this->workModel = new Work();
  26. }
  27. /**
  28. * 合并助教数据代码
  29. * @param $paginate
  30. * @param $user_id
  31. * @return array
  32. */
  33. private function mergeMassager($paginate, $user_id)
  34. {
  35. $items = $paginate->items();
  36. $collet_relation = [];
  37. if ($user_id) {
  38. $collet_relation = $this->colletModel->fetchByUserIdAndMassagerIds($user_id, array_map(function ($data) {
  39. return $data['id'];
  40. }, $items));
  41. $collet_relation = array_reduce($collet_relation, function ($p, $cur) {
  42. $p[$cur['massager_id']] = 1;
  43. return $p;
  44. }, []);
  45. }
  46. $services = (new \app\admin\model\Service())->where([
  47. "duration_minute" => 120,
  48. "type" => \E_SERVICE_TYPE::App,
  49. ])->select();
  50. $services = array_reduce($services, function ($p, $cur) {
  51. $p[$cur["level"]] = $cur["real_price"];
  52. return $p;
  53. }, []);
  54. foreach ($items as &$item) {
  55. $recently_work_date = $this->workDateRange(array_map(function ($data) {
  56. return [strtotime($data["service_start_date"]), strtotime($data["service_end_date"])];
  57. }, $item["nearly_two_days_orders"]), true);
  58. $item['recently_work_date'] = $recently_work_date;
  59. $item['is_can_collet'] = !isset($collet_relation[$item['id']]);
  60. $item["price"] = $services[$item['level']] ?? 260;
  61. unset($item['nearly_two_days_orders']);
  62. }
  63. return [
  64. array_map(function ($data) {
  65. return Massager::fmtMassager($data);
  66. }, $items),
  67. $paginate->total()
  68. ];
  69. }
  70. /**
  71. * 判断是否能够选择
  72. * @param $in_time
  73. * @param $ranges
  74. * @return bool
  75. */
  76. private function checkCanOption($in_time, $ranges)
  77. {
  78. foreach ($ranges as $range) {
  79. if ($in_time >= $range[0] && $in_time <= $range[1]) {
  80. return false;
  81. }
  82. }
  83. return true;
  84. }
  85. /**
  86. * 获取可助教工作的时间段
  87. * @param array $order_date_range
  88. * @param bool $recently
  89. * @return array|false|string|null
  90. */
  91. private function workDateRange(array $order_date_range, $recently = true)
  92. {
  93. $now_time = time();
  94. $range = [];
  95. $max_time = strtotime(date("Y-m-d 23:59:59"));
  96. $acc_time = $now_time;
  97. $recently_work_date = null;
  98. while (true) {
  99. if ($acc_time === $now_time) {
  100. $y_m_d = date("Y-m-d", $acc_time);
  101. $now_minute = date("i", $acc_time);
  102. if ($now_minute < 30) {
  103. $range["$y_m_d"] = ["date" => $y_m_d, "range" => [["value" => date("H:30", $acc_time), "can_option" => false]]];
  104. }
  105. }
  106. $acc_time += (60 * 60);
  107. $y_m_d = date("Y-m-d", $acc_time);
  108. $y_m_d_h = date("Y-m-d H:00:00", $acc_time);
  109. $v_1 = [
  110. "value" => date("H:00", strtotime($y_m_d_h)),
  111. "can_option" => !(($acc_time - (60 * 60)) === $now_time) && $this->checkCanOption(strtotime($y_m_d_h), $order_date_range)
  112. ];
  113. $v_2 = [
  114. "value" => date("H:30", $acc_time),
  115. "can_option" => $this->checkCanOption($acc_time + (30 * 60), $order_date_range)
  116. ];
  117. if ($recently) {
  118. if ($v_1["can_option"]) {
  119. $recently_work_date = "$y_m_d_h";
  120. break;
  121. }
  122. if ($v_2["can_option"]) {
  123. $recently_work_date = date("Y-m-d H:30:00", $acc_time);
  124. break;
  125. }
  126. }
  127. if (isset($range["$y_m_d"])) {
  128. array_push($range["$y_m_d"]["range"], $v_1, $v_2);
  129. } else {
  130. $range["$y_m_d"] = [
  131. "date" => $y_m_d,
  132. "range" => [$v_1, $v_2]
  133. ];
  134. }
  135. if ($acc_time >= $max_time) break;
  136. }
  137. if ($recently) {
  138. return $recently_work_date;
  139. }
  140. $format_range = [];
  141. $keys = array_keys($range);
  142. for ($i = 0; $i <= count($keys) - 1; $i++) {
  143. $key = $keys[$i];
  144. $format_range[$i] = $range[$key];
  145. }
  146. return $format_range;
  147. }
  148. /**
  149. * 获取App助教
  150. * @param null $user_id
  151. * @param array $params
  152. * @param $gender
  153. * @return array
  154. */
  155. public function fetchAppMassager($user_id, array $params, $gender)
  156. {
  157. $paginate = $this->massagerModel->fetchAppMassager(
  158. $params['city_code'],
  159. $params['lng_lat'],
  160. isset($params['search_text']) && mb_strlen($params['search_text']) > 0 ? $params['search_text'] : null,
  161. isset($params['distance']) && is_numeric($params["distance"]) ? $params['distance'] : 300,
  162. isset($params['service_status']) && is_numeric($params['service_status']) ? $params['service_status'] : null,
  163. $gender,
  164. isset($params['free_travel']) && is_numeric($params['free_travel']) ? $params['free_travel'] : null,
  165. isset($params['hot']) && is_numeric($params['hot']) ? $params['hot'] : null,
  166. isset($params['service_id']) && is_numeric($params['service_id']) ? $params['service_id'] : null,
  167. $params['sort'] ?? null,
  168. $params['page'] ?? 1,
  169. $params['size'] ?? 10,
  170. $user_id,
  171. isset($params['level']) && is_numeric($params['level']) ? $params['level'] : null
  172. );
  173. return $this->mergeMassager($paginate, $user_id);
  174. }
  175. /**
  176. * 获取球房助教
  177. * @param array $params
  178. * @param $gender
  179. * @param null $user_id
  180. * @return array
  181. */
  182. public function fetchStoreMassager(array $params, $gender, $user_id = null)
  183. {
  184. $paginate = $this->massagerModel->fetchStoreMassager(
  185. $params['store_id'],
  186. $params['service_id'] ?? null,
  187. $params['search_text'] ?? null,
  188. $params['hot'] ?? null,
  189. $gender,
  190. $params['page'] ?? 1,
  191. $params['size'] ?? 10
  192. );
  193. return $this->mergeMassager($paginate, $user_id);
  194. }
  195. /**
  196. * 获取助教上班的的时间
  197. * @param $id
  198. * @return |null
  199. */
  200. public function getWorkDateRange($id)
  201. {
  202. $massager = $this->massagerModel->getMassager($id, ["store", "nearlyTwoDaysOrders"]);
  203. if (!$massager)
  204. return $this->ok([]);
  205. $order_date_range = array_map(function ($data) {
  206. return [strtotime($data["service_start_date"]), strtotime($data["service_end_date"])];
  207. }, $massager["nearlyTwoDaysOrders"]);
  208. $no_work_ranges = (new WorkPeriod())->where("massager_id", $id)->where("end_time", ">", time())->select();
  209. $ranges_ = array_map(function ($data) {
  210. return [$data["start_time"], $data["end_time"]];
  211. }, $no_work_ranges);
  212. $range = $this->workDateRange(array_merge($order_date_range, $ranges_), false);
  213. return $this->ok([
  214. "id" => $massager->id,
  215. "range" => $range
  216. ]);
  217. }
  218. /**
  219. * 获取单个助教详情
  220. * @param $massager_id
  221. * @param null $user_id
  222. * @return |null
  223. */
  224. public function get($massager_id, $user_id = null, $is_add_clock = null)
  225. {
  226. $massager = $this->massagerModel->fetchHiddenAndNormal($massager_id);
  227. if (null === $massager)
  228. return null;
  229. $massager['is_can_collet'] = true;
  230. if ($user_id) {
  231. $relation = $this->colletModel->getByUserIdAndMassagerId($user_id, $massager_id);
  232. if ($relation)
  233. $massager['is_can_collet'] = false;
  234. }
  235. $serviceModel = new Service();
  236. $query = $serviceModel
  237. ->where([
  238. "level" => $massager["level"],
  239. "type" => \E_SERVICE_TYPE::App,
  240. "p_code" => parse_area($massager['city_code'])['p_code']
  241. ]);
  242. if ($is_add_clock != 1) {
  243. $query->where("is_add_clock", 0);
  244. }
  245. $items = collection($query
  246. ->order('sort', 'desc')
  247. ->order('real_price', 'ASC')->select())->toArray();
  248. $membership_discount_rate = config("site.membership_discount_rate") / 100;
  249. $available_services = [];
  250. foreach ($items as &$item) {
  251. $item["membership_discount_price"] = fixed2Float($item["real_price"] * $membership_discount_rate);
  252. $available_services[] = $item;
  253. }
  254. $massager["services"] = $available_services;
  255. return Massager::fmtMassager($massager);
  256. }
  257. /**
  258. * @param $massager_id
  259. * @param $page
  260. * @param $size
  261. * @return array
  262. * @throws \think\exception\DbException
  263. */
  264. public function fetchMassagerComment($massager_id, $page, $size)
  265. {
  266. $paginate = $this->commentModel->fetchByMassagerId($massager_id, $page, $size);
  267. $items = $paginate->items();
  268. return [
  269. array_map(function ($data) {
  270. $nickname = "匿名用户";
  271. $avatar = null;
  272. if ($data['user'] && 0 == $data['is_anonymity']) {
  273. $nickname = $data['user']['nickname'];
  274. $avatar = $data['user']['avatar'];
  275. }
  276. $data['user_nickname'] = $nickname;
  277. $data['user_avatar'] = $avatar;
  278. unset($data['user']);
  279. return $data;
  280. }, $items),
  281. $paginate->total()
  282. ];
  283. }
  284. public function getMassagerTentativeNowLevelConfiguration($m_id, $city_code, $intraday = null)
  285. {
  286. $configuration = [
  287. [0, 50],
  288. [300, 60],
  289. [500, 65],
  290. [700, 70]
  291. ];
  292. $level_index = 1;
  293. $profit_rate = 50;
  294. $next_configuration = 288;
  295. $sumPerformanceByNowMonth = $this->orderModel->sumPerformanceByIntraday($m_id, $city_code, $intraday);
  296. if ($sumPerformanceByNowMonth > $configuration[0][0]) {
  297. for ($index = 0; $index < count($configuration); $index++) {
  298. $item = $configuration[$index];
  299. if ($sumPerformanceByNowMonth > $item[0]) {
  300. $level_index += 1;
  301. $profit_rate = $item[1];
  302. $next_configuration = isset($configuration[$index + 1]) ? $configuration[$index + 1][0] : null;
  303. }
  304. }
  305. $level_index -= 1;
  306. }
  307. return [
  308. "level" => $level_index,
  309. "profit_rate" => $profit_rate,
  310. "next_configuration" => $next_configuration,
  311. "sum_performance_by_now_month" => $sumPerformanceByNowMonth,
  312. ];
  313. }
  314. public function getMassagerNowLevelConfiguration($m_id, $city_code, $y = null, $m = null)
  315. {
  316. $configuration = array_map(function ($data) {
  317. $performance = explode("|", $data["月业绩/元"]);
  318. $duration = explode("|", $data["在岗时长/小时"]);
  319. $reorder = explode("|", $data["加钟比例(%)"]);
  320. $praise = explode("|", $data["好评率(%)"]);
  321. $chargeback = explode("|", $data["退单率(%)"]);
  322. return [
  323. [
  324. "condition" => isset($performance[0]) && is_numeric($performance[0]) ? (float)$performance[0] : 6500,
  325. "rate" => isset($performance[1]) && is_numeric($performance[1]) ? (float)$performance[1] : 55
  326. ],
  327. [
  328. "condition" => isset($duration[0]) && is_numeric($duration[0]) ? (float)$duration[0] : 100,
  329. "rate" => isset($duration[1]) && is_numeric($duration[1]) ? (float)$duration[1] : 1
  330. ],
  331. [
  332. "condition" => isset($reorder[0]) && is_numeric($reorder[0]) ? (float)$reorder[0] : 30,
  333. "rate" => isset($reorder[1]) && is_numeric($reorder[1]) ? (float)$reorder[1] : 1
  334. ],
  335. [
  336. "condition" => isset($praise[0]) && is_numeric($praise[0]) ? (float)$praise[0] : 30,
  337. "rate" => isset($praise[1]) && is_numeric($praise[1]) ? (float)$praise[1] : 1
  338. ],
  339. [
  340. "condition" => isset($chargeback[0]) && is_numeric($chargeback[0]) ? (float)$chargeback[0] : 30,
  341. "rate" => isset($chargeback[1]) && is_numeric($chargeback[1]) ? (float)$chargeback[1] : -1
  342. ]
  343. ];
  344. }, [
  345. config("site.massager_level_1"),
  346. config("site.massager_level_2"),
  347. config("site.massager_level_3"),
  348. config("site.massager_level_4"),
  349. config("site.massager_level_5"),
  350. ]);
  351. $sumPerformanceByNowMonth = $this->orderModel->sumPerformanceByNowMonth($m_id, $city_code, $y, $m);
  352. $sumDurationByNowMonth = $this->workModel->sumByNowMonth($m_id, $y, $m);
  353. $reorderRateByNowMonth = $this->orderModel->reorderRateByNowMonth($m_id, $city_code, $y, $m);
  354. $praiseRateByNowMonth = $this->orderModel->countByPraiseRate($m_id, $city_code, $y, $m);
  355. $chargebackRateByNowMonth = $this->orderModel->chargebackRate($m_id, $city_code, $y, $m);
  356. $profit_rate = config("site.massager_basics_deposit_rate") ?? 50;
  357. $level_index = 0;
  358. $next_configuration = null;
  359. for ($i = 0; $i < count($configuration); $i++) {
  360. $now_configuration = $configuration[$i];
  361. if ($sumPerformanceByNowMonth >= $now_configuration[0]["condition"]) {
  362. $level_index += 1;
  363. $next_configuration = isset($configuration[$i + 1]) ? $configuration[$i + 1] : null;
  364. $profit_rate = $now_configuration[0]["rate"];
  365. $profit_rate += ($sumDurationByNowMonth >= $now_configuration[1]["condition"] ? $now_configuration[1]["rate"] : 0);
  366. $profit_rate += ($reorderRateByNowMonth >= $now_configuration[2]["condition"] ? $now_configuration[2]["rate"] : 0);
  367. $profit_rate += ($praiseRateByNowMonth >= $now_configuration[3]["condition"] ? $now_configuration[3]["rate"] : 0);
  368. $profit_rate += ($chargebackRateByNowMonth >= $now_configuration[4]["condition"] ? $now_configuration[4]["rate"] : 0);
  369. } else {
  370. break;
  371. }
  372. }
  373. return [
  374. "level" => $level_index,
  375. "profit_rate" => $profit_rate,
  376. "next_configuration" => $next_configuration,
  377. "sum_performance_by_now_month" => $sumPerformanceByNowMonth,
  378. ];
  379. }
  380. public function getProfitRate($m_id, $city_code, $y = null, $m = null, $intraday = null): float
  381. {
  382. $massager = $this->massagerModel->findById($m_id);
  383. if (!$massager)
  384. return 0;
  385. if ($massager["fixed_profit_rate"] > 0)
  386. return $massager["fixed_profit_rate"];
  387. // DOTO: 临时替换
  388. $level_info = $this->getMassagerTentativeNowLevelConfiguration($m_id, $city_code, $intraday);
  389. // $level_info = $this->getMassagerNowLevelConfiguration($m_id, $city_code, $y, $m);
  390. return $level_info["profit_rate"];
  391. }
  392. public function interiorScoreLevel($score)
  393. {
  394. $config = config("site.massager_review_level");
  395. $keys = array_keys($config);
  396. $level_desc = "不及格";
  397. foreach ($keys as $key) {
  398. if ($score >= (float)$key) {
  399. $level_desc = $config[$key];
  400. } else {
  401. break;
  402. }
  403. }
  404. return $level_desc;
  405. }
  406. public function updateInteriorScore($m_id, $year, $month)
  407. {
  408. $config = config("site.massager_review_score");
  409. $fmt_config = [
  410. "duration" => explode("|", $config["时长/天"] ?? "6|2"),
  411. "praise" => explode("|", $config["评论/好评"] ?? "1|2"),
  412. "negative" => explode("|", $config["评论/差评"] ?? "1|-10"),
  413. "performance" => explode("|", $config["业绩/天"] ?? "500|2"),
  414. "reorder" => explode("|", $config["项目加钟"] ?? "1|2")
  415. ];
  416. $byDuration = $this->interiorScoreByDuration($m_id, $year, $month, $fmt_config["duration"]);
  417. $byPraise = $this->interiorScoreByPraise($m_id, $year, $month, $fmt_config["praise"]);
  418. $byNegative = $this->interiorScoreByNegative($m_id, $year, $month, $fmt_config["negative"]);
  419. $byPerformance = $this->interiorScoreByPerformance($m_id, $year, $month, $fmt_config["performance"]);
  420. $byReorder = $this->interiorScoreByReorder($m_id, $year, $month, $fmt_config["reorder"]);
  421. $total_score = fixed2Float(
  422. $byDuration["score"]
  423. + $byPraise["score"]
  424. + $byNegative["score"]
  425. + $byPerformance["score"]
  426. + $byReorder["score"]
  427. );
  428. if (date("Y") == $year && date("m") == $month) {
  429. $this->massagerModel->update([
  430. "interior_score" => $total_score
  431. ], ["id" => $m_id]);
  432. }
  433. return [
  434. "total_interior_score" => $total_score,
  435. "level_desc" => $this->interiorScoreLevel($total_score),
  436. "items" => [
  437. "byDuration" => $byDuration,
  438. "byPraise" => $byPraise,
  439. "byNegative" => $byNegative,
  440. "byPerformance" => $byPerformance,
  441. "byReorder" => $byReorder
  442. ]
  443. ];
  444. }
  445. public function interiorScoreByDuration($m_id, $year, $month, $config)
  446. {
  447. $last_day = date("t", strtotime("$year-$month-01 00:00:00"));
  448. $starttime = strtotime("$year-$month-01 00:00:00");
  449. $endtime = strtotime("$year-$month-$last_day 23:59:59");
  450. $works = (new Work())
  451. ->where("massager_id", $m_id)
  452. ->where("clock_in_time", ">=", $starttime)
  453. ->where("clock_off_time", "<=", $endtime)
  454. ->select();
  455. $clocks_times = [];
  456. foreach ($works as $work) {
  457. $times = splittingDate($work["clock_in_time"], $work["clock_off_time"]);
  458. $clocks_times = array_merge($clocks_times, $times);
  459. }
  460. $clocks_times = array_merge(array_unique($clocks_times));
  461. $every_days = [];
  462. $DAY_TIME = 24 * 60 * 60;
  463. $start_time_copy = $starttime;
  464. while ($start_time_copy <= $endtime) {
  465. array_push($every_days, date("Y-m-d", $start_time_copy));
  466. $start_time_copy += $DAY_TIME;
  467. }
  468. $score = 0;
  469. $intersections = [];
  470. foreach ($every_days as $now_day) {
  471. $now_day_times = splittingDate(strtotime("$now_day 00:00:00"), strtotime("$now_day 23:59:59"));
  472. $intersection = array_intersect($now_day_times, $clocks_times);
  473. $meet = count($intersection) >= (fixed2Float((($config[0] ?? 6) * 60) / 10) + 1);
  474. if ($meet) {
  475. $intersections[] = [
  476. "date" => $now_day,
  477. "count" => count($intersection),
  478. ];
  479. $score += $config[1] ?? 2;
  480. }
  481. }
  482. return [
  483. "score" => fixed2Float($score),
  484. "details" => $intersections
  485. ];
  486. }
  487. public function interiorScoreByPraise($m_id, $year, $month, $config)
  488. {
  489. $negative = $this->commentModel->fetchPraiseCommentByMassager($m_id, $year, $month);
  490. $count = count($negative);
  491. $item_score = $config[1] / $config[0];
  492. return ["score" => fixed2Float($item_score * $count), "details" => $negative];
  493. }
  494. public function interiorScoreByNegative($m_id, $year, $month, $config)
  495. {
  496. $negative = $this->commentModel->fetchNegativeCommentByMassager($m_id, $year, $month);
  497. $count = count($negative);
  498. $item_score = $config[1] / $config[0];
  499. return ["score" => fixed2Float($item_score * $count), "details" => $negative];
  500. }
  501. public function interiorScoreByPerformance($m_id, $year, $month, $config)
  502. {
  503. $all_orders = $this->orderModel->fetchByMassager($m_id, $year, $month);
  504. $fmt = [];
  505. foreach ($all_orders as $order) {
  506. $key = date("Y-m-d", $order["pay_time"]);
  507. if (isset($fmt[$key])) {
  508. $fmt[$key] += $order["total_real_amount"];
  509. } else {
  510. $fmt[$key] = $order["total_real_amount"];
  511. }
  512. }
  513. $score = 0;
  514. $details = [];
  515. foreach ($fmt as $key => $value) {
  516. if ($value >= $config[0]) {
  517. $details[] = [
  518. "date" => $key,
  519. "count" => $value,
  520. ];
  521. $score += $config[1];
  522. }
  523. }
  524. return ["score" => fixed2Float($score), "details" => $details];
  525. }
  526. public function interiorScoreByReorder($m_id, $year, $month, $config)
  527. {
  528. $all_orders = $this->orderModel->fetchReorderByMassager($m_id, $year, $month);
  529. $count = count($all_orders);
  530. $item_score = $config[1] / $config[0];
  531. return ["score" => fixed2Float($item_score * $count), "details" => $all_orders];
  532. }
  533. public function fetchSelfDynamics($m_id, $page = 1, $size = 10)
  534. {
  535. $paginate = (new Dynamic())
  536. ->where("massager_id", $m_id)
  537. ->with(["comments", "likes"])
  538. ->page($page)
  539. ->paginate($size);
  540. return [
  541. $paginate->items(),
  542. $paginate->total()
  543. ];
  544. }
  545. public function fetchFansNumberAndPraiseNumber($m_id)
  546. {
  547. return [
  548. "fansNumber" => Collect::where("massager_id", $m_id)->count(),
  549. "praiseNumber" => Like::where("massager_id", $m_id)->count()
  550. ];
  551. }
  552. }