AgencyActionService.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. <?php
  2. namespace app\api\service;
  3. use app\admin\model\Admin;
  4. use app\api\controller\AgencyBaseApi;
  5. use app\api\model\Area;
  6. use app\api\model\deposit\Record;
  7. use app\api\model\massager\Closing;
  8. use app\api\model\massager\Comment;
  9. use app\api\model\massager\Massager;
  10. use app\api\model\massager\Visa;
  11. use app\api\model\order\Order;
  12. use app\api\model\profit\Bill;
  13. use app\api\model\Store;
  14. use app\api\model\system\Message;
  15. use redis\RedLock;
  16. use think\Db;
  17. use think\Exception;
  18. class AgencyActionService extends BaseService
  19. {
  20. private $model;
  21. private $storeModel;
  22. private $areaModel;
  23. private $orderModel;
  24. private $massagerModel;
  25. private $visaModel;
  26. private $massagerCommentModel;
  27. private $massagerClosingModel;
  28. public function __construct()
  29. {
  30. $this->model = new Admin();
  31. $this->storeModel = new Store();
  32. $this->areaModel = new Area();
  33. $this->orderModel = new Order();
  34. $this->massagerModel = new Massager();
  35. $this->visaModel = new Visa();
  36. $this->massagerCommentModel = new Comment();
  37. $this->massagerClosingModel = new Closing();
  38. }
  39. private function fetchPermissionIds($admin_id)
  40. {
  41. $permission = $this->fetchPermission($admin_id);
  42. return [
  43. "allowable_area_codes" => array_map(function ($data) {
  44. return $data["area_code"];
  45. }, $permission["allowable_areas"]),
  46. "allowable_store_ids" => array_map(function ($data) {
  47. return $data["id"];
  48. }, $permission["allowable_stores"])
  49. ];
  50. }
  51. private function checkAuth($params)
  52. {
  53. $permission = $this->fetchPermissionIds($params["admin_id"]);
  54. if (is_null($params["city_code"]) && is_null($params["store_id"]))
  55. return true;
  56. return $params["city_code"] > 0 ? in_array($params["city_code"], $permission["allowable_area_codes"]) : in_array($params["store_id"], $permission["allowable_store_ids"]);
  57. }
  58. public function login($account, $password)
  59. {
  60. $admin = $this->model
  61. ->where("mobile|username", $account)
  62. ->where("type", "in", [\E_IDENTITY_TYPE::Agency, \E_IDENTITY_TYPE::Store])
  63. ->find();
  64. if (null === $admin)
  65. return $this->fail("账号错误!");
  66. if (md5(md5($password) . $admin["salt"]) !== $admin["password"])
  67. return $this->fail("密码错误!");
  68. if (\E_BASE_STATUS::Normal !== $admin["status"])
  69. return $this->fail([
  70. \E_BASE_STATUS::Hidden => "账号被封!",
  71. \E_BASE_STATUS::Checking => "账号正在审核!",
  72. ][$admin["status"]]);
  73. $token = $this->refreshAdminToken($admin["id"]);
  74. $admin["token"] = $token;
  75. unset($admin["password"]);
  76. unset($admin["session_token"]);
  77. unset($admin["salt"]);
  78. return $this->ok($admin);
  79. }
  80. public function loginByMobile($mobile, $sms_code)
  81. {
  82. $check = \app\common\library\Sms::check($mobile, $sms_code, "agency_login");
  83. if (!$check)
  84. return $this->fail("短信验证码不正确!");
  85. $agency = $this->model->findByMobile($mobile);
  86. if (null === $agency)
  87. return $this->fail("账号不存在!");
  88. if (\E_BASE_STATUS::Normal !== $agency->status)
  89. return $this->fail("账号异常!");
  90. $token = $this->refreshAdminToken($agency["id"]);
  91. $agency["token"] = $token;
  92. unset($agency["password"]);
  93. unset($agency["session_token"]);
  94. unset($agency["salt"]);
  95. return $this->ok($agency);
  96. }
  97. public function wxAppLogin($openid, $union_id)
  98. {
  99. $admin = $this->model->findByUnionId($union_id);
  100. if (!$admin) {
  101. return $this->fail("请先用手机号码登录并绑定微信!");
  102. }
  103. if (\E_BASE_STATUS::Normal !== $admin["status"])
  104. return $this->fail("账号异常!");
  105. $this->model->update([
  106. "app_openid" => $openid,
  107. ], ["id" => $admin['id']]);
  108. $token = $this->refreshAdminToken($admin["id"]);
  109. $admin["token"] = $token;
  110. unset($admin["password"]);
  111. unset($admin["session_token"]);
  112. unset($admin["salt"]);
  113. return $this->ok($admin);
  114. }
  115. public function bindAppWx($u_id, $openid, $union_id)
  116. {
  117. $admin = $this->model->findByUnionId($union_id);
  118. if (!$admin) {
  119. return $this->fail("请先用手机号码登录并绑定微信!");
  120. }
  121. $this->model->update([
  122. "app_openid" => $openid,
  123. "union_id" => $union_id,
  124. ], ["id" => $u_id]);
  125. return $this->ok(true);
  126. }
  127. public function fetchPermission($admin_id)
  128. {
  129. $permission = [
  130. "allowable_areas" => [],
  131. "allowable_stores" => [],
  132. ];
  133. $admin = $this->model
  134. ->where("id", $admin_id)
  135. ->where("type", "in", [\E_IDENTITY_TYPE::Agency, \E_IDENTITY_TYPE::Store])
  136. ->find();
  137. if (null === $admin)
  138. return $permission;
  139. if (\E_IDENTITY_TYPE::Agency === $admin["type"]) {
  140. $permission["allowable_areas"] = $this->areaModel
  141. ->where("area_code", "in", explode(",", $admin["city_codes"]))
  142. ->where("use", 1)
  143. ->field("id,area_code,name")
  144. ->select();
  145. $permission["allowable_stores"] = $this->storeModel
  146. ->where("city_code", "in", explode(",", $admin["city_codes"]))
  147. ->where("status", \E_BASE_STATUS::Normal)
  148. ->field("id,name")
  149. ->select();
  150. }
  151. if (\E_IDENTITY_TYPE::Store === $admin["type"]) {
  152. $permission["allowable_stores"] = $this->storeModel
  153. ->where("city_code", "in", explode(",", $admin["city_codes"]))
  154. ->where("status", \E_BASE_STATUS::Normal)
  155. ->select();
  156. }
  157. return $permission;
  158. }
  159. public function updateBankInfo($admin, $opening_bank_name, $bank_real_name, $bank_no)
  160. {
  161. $this->model->update([
  162. "opening_bank_name" => $opening_bank_name,
  163. "bank_real_name" => $bank_real_name,
  164. "bank_no" => $bank_no
  165. ], ["id" => $admin["id"]]);
  166. return $this->ok();
  167. }
  168. /**
  169. * @param $admin
  170. * @param null $city_code
  171. * @param null $store_id
  172. * @return \SResult
  173. * @throws \think\Exception
  174. * @throws \think\db\exception\DataNotFoundException
  175. * @throws \think\db\exception\ModelNotFoundException
  176. * @throws \think\exception\DbException
  177. */
  178. public function fetchTodayPerformance($admin, $city_code = null, $store_id = null)
  179. {
  180. $allow = $this->checkAuth(["admin_id" => $admin["id"], "city_code" => $city_code, "store_id" => $store_id]);
  181. if (!$allow)
  182. return $this->fail("暂无权限");
  183. // 当日销售金额 当日分润金额 当日订单量 当日新增助教 当日新增评论
  184. $order_total_service_amount_query = $this->orderModel
  185. ->where("TO_DAYS(FROM_UNIXTIME(createtime)) = TO_DAYS(NOW())")
  186. ->where("status", "in", [
  187. \E_ORDER_STATUS::Proceed,
  188. \E_ORDER_STATUS::Purchase,
  189. \E_ORDER_STATUS::WaitFeedback,
  190. \E_ORDER_STATUS::Finish
  191. ]);
  192. $order_count_query = Order::where("TO_DAYS(FROM_UNIXTIME(createtime)) = TO_DAYS(NOW())")
  193. ->where("status", "in", [
  194. \E_ORDER_STATUS::Proceed,
  195. \E_ORDER_STATUS::Purchase,
  196. \E_ORDER_STATUS::WaitFeedback,
  197. \E_ORDER_STATUS::Finish
  198. ]);
  199. $massger_count_query = $this->massagerModel
  200. ->where("TO_DAYS(FROM_UNIXTIME(createtime)) = TO_DAYS(NOW())")
  201. ->where("status", "NOT IN", [\E_MASSAGER_STATUS::Close, \E_MASSAGER_STATUS::Hidden]);
  202. $today_profit_amounts_query = (new Bill())->field("id,`change`")->where("TO_DAYS(FROM_UNIXTIME(createtime)) = TO_DAYS(NOW())")
  203. ->where("identity_type", $admin["type"])
  204. ->where("target_id", $admin["id"])
  205. ->where("change_type", "profit");
  206. if (!$city_code && !$store_id) {
  207. $permission = $this->fetchPermissionIds($admin["id"]);
  208. if (\E_IDENTITY_TYPE::Agency === $admin["type"]) {
  209. $order_total_service_amount_query->where("city_code", "in", $permission["allowable_area_codes"]);
  210. $order_count_query->where("city_code", "in", $permission["allowable_area_codes"]);
  211. $massger_count_query->where("city_code", "in", $permission["allowable_area_codes"]);
  212. $today_profit_amounts_query->where("city_code", "in", $permission["allowable_area_codes"]);
  213. } else {
  214. $order_total_service_amount_query->where("store_id", "in", $permission["allowable_store_ids"]);
  215. $order_count_query->where("store_id", "in", $permission["allowable_store_ids"]);
  216. $massger_count_query->where("store_id", "in", $permission["allowable_store_ids"]);
  217. $today_profit_amounts_query->where("target_id", "in", $permission["allowable_store_ids"]);
  218. }
  219. } else {
  220. if ($city_code) {
  221. $order_total_service_amount_query->where("city_code", $city_code);
  222. $order_count_query->where("city_code", $city_code);
  223. $massger_count_query->where("city_code", $city_code);
  224. $today_profit_amounts_query->where("city_code", $city_code);
  225. } else {
  226. $order_total_service_amount_query->where("store_id", $store_id);
  227. $order_count_query->where("store_id", $store_id);
  228. $massger_count_query->where("store_id", $store_id);
  229. $today_profit_amounts_query->where("target_id", $store_id);
  230. }
  231. }
  232. $today_profit_amounts = $today_profit_amounts_query->select();
  233. return $this->ok([
  234. "today_order_total_amount" => $order_total_service_amount_query->sum("total_service_amount"),
  235. "today_order_count" => $order_count_query->count(),
  236. "today_profit_amount" => array_reduce($today_profit_amounts, function ($p, $cur) {
  237. $p += $cur["change"];
  238. return $p;
  239. }, 0),
  240. "today_massger_count" => $massger_count_query->count()
  241. ]);
  242. }
  243. public function fetchOrders($admin, $city_code = null, $store_id = null, $page = 1, $size = 10)
  244. {
  245. $query = $this->orderModel
  246. ->where("order.status", "in", [
  247. \E_ORDER_STATUS::Proceed,
  248. \E_ORDER_STATUS::Purchase,
  249. \E_ORDER_STATUS::WaitFeedback,
  250. \E_ORDER_STATUS::Finish
  251. ]);
  252. if (!$city_code && !$store_id) {
  253. $permission = $this->fetchPermissionIds($admin["id"]);
  254. \E_IDENTITY_TYPE::Agency === $admin["type"] ? $query->where("order.city_code", "in", $permission["allowable_area_codes"]) : $query->where("order.store_id", "in", $permission["allowable_store_ids"]);
  255. } else {
  256. $city_code ? $query->where("order.city_code", $city_code) : $query->where("order.store_id", $store_id);
  257. }
  258. return $query
  259. ->with(["massager", "services"])
  260. ->order("updatetime", "desc")
  261. ->page($page)
  262. ->paginate($size);
  263. }
  264. public function fetchBill($admin, $city_code = null, $store_id = null, $page = 1, $size = 10)
  265. {
  266. $query = (new Bill())->where("identity_type", $admin["type"])
  267. ->where("target_id", $admin["id"])
  268. ->where("change_type", "profit");
  269. if (!$city_code && !$store_id) {
  270. $permission = $this->fetchPermissionIds($admin["id"]);
  271. \E_IDENTITY_TYPE::Agency === $admin["type"] ? $query->where("city_code", "in", $permission["allowable_area_codes"]) : $query->where("target_id", "in", $permission["allowable_store_ids"]);
  272. } else {
  273. $city_code ? $query->where("city_code", $city_code) : $query->where("target_id", $store_id);
  274. }
  275. return $query->order("createtime", "desc")
  276. ->page($page)
  277. ->paginate($size);
  278. }
  279. public function fetchMassger($admin, $city_code = null, $store_id = null, $page = 1, $size = 10)
  280. {
  281. $query = $this->massagerModel;
  282. if (!$city_code && !$store_id) {
  283. $permission = $this->fetchPermissionIds($admin["id"]);
  284. \E_IDENTITY_TYPE::Agency === $admin["type"] ? $query->where("city_code", "in", $permission["allowable_area_codes"]) : $query->where("store_id", "in", $permission["allowable_store_ids"]);
  285. } else {
  286. $city_code ? $query->where("city_code", $city_code) : $query->where("store_id", $store_id);
  287. }
  288. return $query->order("createtime", "desc")
  289. ->page($page)
  290. ->paginate($size);
  291. }
  292. /**
  293. * 待办事项
  294. * @param $admin
  295. */
  296. public function backlog($admin)
  297. {
  298. $permission = $this->fetchPermissionIds($admin['id']);
  299. if (count($permission["allowable_area_codes"]) > 0) {
  300. return [
  301. "comment_check_count" => $this->massagerCommentModel
  302. ->where("city_code", "in", $permission["allowable_area_codes"])
  303. ->where(["negative" => 1, "allegedly" => 1, "allegedly_status" => \E_BASE_STATUS::Default])
  304. ->count(),
  305. "massager_check_count" => $this->massagerModel
  306. ->where("city_code", "in", $permission["allowable_area_codes"])
  307. ->where("status", \E_BASE_STATUS::Checking)
  308. ->count(),
  309. "visa_check_count" => $this->visaModel
  310. ->where("old_area_code", "in", $permission["allowable_area_codes"])
  311. ->where("status", \E_BASE_STATUS::Checking)
  312. ->count(),
  313. "closing_check_count" => $this->massagerClosingModel
  314. ->where("city_code", "in", $permission["allowable_area_codes"])
  315. ->where("status", \E_BASE_STATUS::Checking)
  316. ->count(),
  317. ];
  318. } else {
  319. return [
  320. "comment_check_count" => $this->massagerCommentModel
  321. ->where("store_id", "in", $permission["allowable_store_ids"])
  322. ->where(["negative" => 1, "allegedly" => 1, "allegedly_status" => \E_BASE_STATUS::Default])
  323. ->count(),
  324. "massager_check_count" => 0,
  325. "visa_check_count" => 0,
  326. ];
  327. }
  328. }
  329. public function fetchCheckComment($admin)
  330. {
  331. $permission = $this->fetchPermissionIds($admin['id']);
  332. return $this->massagerCommentModel
  333. ->where("comment.city_code", "in", $permission["allowable_area_codes"])
  334. ->where(["comment.negative" => 1, "comment.allegedly" => 1, "comment.allegedly_status" => \E_BASE_STATUS::Default])
  335. ->with(["user", "massager"])
  336. ->order("comment.createtime", "desc")
  337. ->select();
  338. }
  339. public function commentCheck($admin, $id, $check)
  340. {
  341. $comment = $this->massagerCommentModel->where([
  342. "id" => $id,
  343. "allegedly" => 1,
  344. "allegedly_status" => \E_BASE_STATUS::Default,
  345. ])->find();
  346. if (!$comment)
  347. return $this->fail("申诉记录不存在!");
  348. $is_pass = $check === "pass";
  349. $this->massagerCommentModel->update([
  350. "updatetime" => time(),
  351. "negative" => (int)$is_pass,
  352. "allegedly_status" => $check
  353. ], ["id" => $id]);
  354. $total_count = $this->massagerCommentModel->where([
  355. 'massager_id' => $comment["massager_id"],
  356. "status" => \E_BASE_STATUS::Normal
  357. ])->count();
  358. $gte_3_count = $this->massagerCommentModel
  359. ->where([
  360. 'massager_id' => $comment["massager_id"],
  361. "negative" => 1,
  362. "status" => \E_BASE_STATUS::Normal
  363. ])
  364. ->count();
  365. $praise_rate = 100;
  366. if ($total_count > 0 && $gte_3_count > 0)
  367. $praise_rate = fixed2Float((($gte_3_count / $total_count)) * 100);
  368. $this->massagerModel->update([
  369. "updatetime" => time(),
  370. "praise_rate" => $praise_rate,
  371. ], ["id" => $id]);
  372. Message::sendSystemMessage(
  373. \E_IDENTITY_TYPE::Massager,
  374. ["to_massager_id" => $id],
  375. "助教审核",
  376. "您的异地签证申请已被管理员" . ($is_pass ? "通过!" : "拒绝!")
  377. );
  378. return $this->ok($is_pass ? "审核通过!" : "审核驳回!");
  379. }
  380. public function fetchCheckMassager($admin)
  381. {
  382. $permission = $this->fetchPermissionIds($admin['id']);
  383. return $this->massagerModel
  384. ->where("city_code", "in", $permission["allowable_area_codes"])
  385. ->where("status", \E_BASE_STATUS::Checking)
  386. ->select();
  387. }
  388. public function massagerCheck($admin, $id, $check)
  389. {
  390. if (!$id || !$check)
  391. return $this->fail("参数错误!");
  392. $massager = $this->massagerModel->where([
  393. "id" => $id,
  394. "status" => \E_BASE_STATUS::Checking,
  395. ])->find();
  396. if (!$massager)
  397. return $this->fail("申请记录不存在!");
  398. $is_pass = $check === "pass";
  399. $this->massagerModel->update([
  400. "updatetime" => time(),
  401. "status" => $is_pass ? \E_MASSAGER_STATUS::Normal : \E_MASSAGER_STATUS::Hidden
  402. ], ["id" => $id]);
  403. Message::sendSystemMessage(
  404. \E_IDENTITY_TYPE::Massager,
  405. ["to_massager_id" => $id],
  406. "助教审核",
  407. "您的异地签证申请已被管理员" . ($is_pass ? "通过!" : "拒绝!")
  408. );
  409. return $this->ok("审核成功!");
  410. }
  411. public function fetchCheckVisa($admin)
  412. {
  413. $permission = $this->fetchPermissionIds($admin['id']);
  414. return $this->visaModel
  415. ->where("old_area_code", "in", $permission["allowable_area_codes"])
  416. ->where("status", \E_BASE_STATUS::Checking)
  417. ->order("createtime", "desc")
  418. ->select();
  419. }
  420. public function visaCheck($admin, $id = null, $check = null)
  421. {
  422. if (!$id || !$check)
  423. return $this->fail("参数错误!");
  424. $record = $this->visaModel->where([
  425. "id" => $id,
  426. "status" => \E_MASSAGER_STATUS::Default,
  427. ])->find();
  428. if (!$record)
  429. return $this->fail("申请记录不存在!");
  430. $new_area = (new \app\admin\model\Area())->where([
  431. "area_code" => $record["new_area_code"],
  432. "use" => 1,
  433. "level" => 2
  434. ])->find();
  435. if (!$new_area)
  436. return $this->fail("申请迁入的地址不存在!");
  437. $is_pass = $check === "pass";
  438. $this->model->update([
  439. "updatetime" => time(),
  440. "status" => $is_pass ? "pass" : "reject"
  441. ], ["id" => $id]);
  442. if ($is_pass) {
  443. $this->massagerModel->update([
  444. "updatetime" => time(),
  445. "city_code" => $new_area["area_code"],
  446. "lng" => $record["lng"],
  447. "lat" => $record["lat"],
  448. ],
  449. ["id" => $record["massager_id"]]
  450. );
  451. }
  452. Message::sendSystemMessage(
  453. \E_IDENTITY_TYPE::Massager,
  454. ["to_massager_id" => $record["massager_id"]],
  455. "异地签证提醒",
  456. "您的异地签证申请已被管理员" . ($is_pass ? "通过!" : "拒绝!")
  457. );
  458. return $this->ok("审核成功!");
  459. }
  460. public function fetchCheckClosing($admin)
  461. {
  462. $permission = $this->fetchPermissionIds($admin['id']);
  463. return $this->massagerClosingModel
  464. ->where("city_code", "in", $permission["allowable_area_codes"])
  465. ->where("status", \E_BASE_STATUS::Checking)
  466. ->select();
  467. }
  468. public function closingCheck($admin, $id = null, $check = null)
  469. {
  470. if (!$id || !$check)
  471. return $this->fail("参数错误!");
  472. $record = $this->massagerClosingModel->where([
  473. "id" => $id,
  474. "status" => \E_MASSAGER_STATUS::Checking,
  475. ])->find();
  476. if (!$record)
  477. return $this->fail("申请记录不存在!");
  478. $massager = (new \app\api\model\massager\Massager())->findById($record["massager_id"]);
  479. if (!$massager)
  480. return $this->fail("助教不存在!");
  481. $is_pass = $check === "pass";
  482. $redLock = RedLock::of();
  483. $massagerLock = $redLock->lock(\app\api\model\massager\Wallet::MWKey($massager["id"]));
  484. if (!is_array($massagerLock))
  485. return $this->fail("请稍后再试!");
  486. $agency = (new Admin())->findAgency($record["city_code"]);
  487. $agencyLock = false;
  488. $pLock = false;
  489. if ($agency) {
  490. $agencyLock = $redLock->lock(Admin::AgencyKey($agency->id));
  491. if (!is_array($agencyLock))
  492. return $this->fail("请稍后再试!");
  493. } else {
  494. $pLock = $redLock->lock(Admin::PlatformKey());
  495. if (!is_array($pLock))
  496. return $this->fail("请稍后再试!");
  497. }
  498. $locks = [$massagerLock, $agencyLock, $pLock];
  499. $mWallet = (new \app\api\model\massager\Wallet())->getWallet($massager["id"]);
  500. Db::startTrans();
  501. try {
  502. if ($is_pass) {
  503. $s_result = (new MassagerActionService())->fetchDiffAmountDetailsByYm($massager["id"], $record["city_code"], $record["year"], $record["month"]);
  504. if (0 === $s_result->code())
  505. return $this->fail("获取数据异常");
  506. $details = $s_result->data();
  507. $m_profit_amount = $mWallet["profit_amount"];
  508. $p_bills = [];
  509. $m_bills = [];
  510. if ($agency) { // 代理商存在 支付的钱由代理商支付
  511. if ($agency["profit_amount"] < $details["diff_total_amount"])
  512. return $this->fail("代理商钱包余额不足!");
  513. $agency_profit_amount = $agency["profit_amount"];
  514. foreach ($details["month_orders"] as $order) {
  515. array_push($p_bills, [
  516. "identity_type" => \E_IDENTITY_TYPE::Agency,
  517. "target_id" => $agency["id"],
  518. "target_name" => $agency["nickname"],
  519. "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::ClosingExpend,
  520. "order_no" => $order["no"],
  521. "total_amount" => $order["total_real_amount"] - $order["trip_amount"],
  522. "rate" => $order["bill"]["diff_rate"],
  523. "change" => -$order["bill"]["diff_amount"],
  524. "before" => $agency_profit_amount,
  525. "after" => fixed2Float($agency_profit_amount - $order["bill"]["diff_amount"]),
  526. "createtime" => time(),
  527. "city_code" => $order["city_code"]
  528. ], [
  529. "identity_type" => \E_IDENTITY_TYPE::Massager,
  530. "target_id" => $massager["id"],
  531. "target_name" => $massager["name"],
  532. "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::ClosingIncome,
  533. "order_no" => $order["no"],
  534. "total_amount" => $order["total_real_amount"] - $order["trip_amount"],
  535. "rate" => $order["bill"]["diff_rate"],
  536. "change" => $order["bill"]["diff_amount"],
  537. "before" => $m_profit_amount,
  538. "after" => fixed2Float($m_profit_amount + $order["bill"]["diff_amount"]),
  539. "createtime" => time(),
  540. "city_code" => $order["city_code"]
  541. ]);
  542. array_push($m_bills, [
  543. "massager_id" => $massager["id"],
  544. "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money,
  545. "change_type" => \E_M_BILL_CHANGE_TYPE::ClosingIncome,
  546. "change" => $order["bill"]["diff_amount"],
  547. "before" => $m_profit_amount,
  548. "after" => fixed2Float($m_profit_amount + $order["bill"]["diff_amount"]),
  549. "reason" => "业绩结算",
  550. "relation_no" => $order["no"],
  551. "createtime" => time()
  552. ]);
  553. $agency_profit_amount -= $order["bill"]["diff_amount"];
  554. $m_profit_amount += $order["bill"]["diff_amount"];
  555. }
  556. (new Admin())->where("id", $agency["id"])->setDec("profit_amount", $details["diff_total_amount"]);
  557. } else { // 不存在则平台发放
  558. $platform = (new Admin())->where("id", 1)->find();
  559. if (!$platform)
  560. return $this->fail("平台账号异常!");
  561. $platform_profit_amount = $platform["profit_amount"];
  562. if ($platform_profit_amount < $details["diff_total_amount"])
  563. return $this->fail("平台钱包余额不足!");
  564. foreach ($details["month_orders"] as $order) {
  565. array_push($p_bills, [
  566. "identity_type" => \E_IDENTITY_TYPE::Platform,
  567. "target_id" => 1,
  568. "target_name" => "平台",
  569. "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::ClosingExpend,
  570. "order_no" => $order["no"],
  571. "total_amount" => $order["total_real_amount"] - $order["trip_amount"],
  572. "rate" => $order["bill"]["diff_rate"],
  573. "change" => -$order["bill"]["diff_amount"],
  574. "before" => $platform_profit_amount,
  575. "after" => fixed2Float($platform_profit_amount - $order["bill"]["diff_amount"]),
  576. "createtime" => time(),
  577. "city_code" => $order["city_code"]
  578. ], [
  579. "identity_type" => \E_IDENTITY_TYPE::Massager,
  580. "target_id" => $massager["id"],
  581. "target_name" => $massager["name"],
  582. "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::ClosingIncome,
  583. "order_no" => $order["no"],
  584. "total_amount" => $order["total_real_amount"] - $order["trip_amount"],
  585. "rate" => $order["bill"]["diff_rate"],
  586. "change" => $order["bill"]["diff_amount"],
  587. "before" => $m_profit_amount,
  588. "after" => fixed2Float($m_profit_amount + $order["bill"]["diff_amount"]),
  589. "createtime" => time(),
  590. "city_code" => $order["city_code"]
  591. ]);
  592. array_push($m_bills, [
  593. "massager_id" => $massager["id"],
  594. "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money,
  595. "change_type" => \E_M_BILL_CHANGE_TYPE::ClosingIncome,
  596. "change" => $order["bill"]["diff_amount"],
  597. "before" => $m_profit_amount,
  598. "after" => fixed2Float($m_profit_amount + $order["bill"]["diff_amount"]),
  599. "reason" => "业绩结算",
  600. "relation_no" => $order["no"],
  601. "createtime" => time()
  602. ]);
  603. $platform_profit_amount -= $order["bill"]["diff_amount"];
  604. $m_profit_amount += $order["bill"]["diff_amount"];
  605. }
  606. (new Admin())->where("id", 1)->setDec("profit_amount", $details["diff_total_amount"]);
  607. }
  608. (new \app\api\model\massager\Wallet())->where("id", $mWallet["id"])->setInc("profit_amount", $details["diff_total_amount"]);
  609. $this->massagerClosingModel->update([
  610. "status" => "allow",
  611. "closing_rate" => $details["current_rate"],
  612. "closing_amount" => $details["diff_total_amount"],
  613. "updatetime" => time()
  614. ], ["id" => $record["id"]]);
  615. (new \app\api\model\profit\Bill())->saveAll($p_bills);
  616. (new \app\api\model\massager\Bill())->saveAll($m_bills);
  617. } else {
  618. // 拒绝
  619. $this->massagerClosingModel->update([
  620. "status" => "reject"
  621. ], ["id" => $record["id"]]);
  622. }
  623. \app\api\model\system\Message::sendSystemMessage(
  624. \E_IDENTITY_TYPE::Massager,
  625. ["to_massager_id" => $record["massager_id"]],
  626. "业绩结算",
  627. "您的业绩结算申请已被管理员" . ($is_pass ? "通过!" : "拒绝!")
  628. );
  629. Db::commit();
  630. } catch (Exception $e) {
  631. Db::rollback();
  632. return $this->fail($e->getMessage());
  633. } finally {
  634. foreach ($locks as $lock) {
  635. if (is_array($lock))
  636. $redLock->unlock($lock);
  637. }
  638. }
  639. return $this->ok(null, "审核成功!");
  640. }
  641. public function fetchStore($admin, $page = 1, $size = 10)
  642. {
  643. $permission = $this->fetchPermissionIds($admin['id']);
  644. $paginate = $this->storeModel
  645. ->where("status", \E_BASE_STATUS::Normal)
  646. ->where("id", "in", $permission["allowable_store_ids"])
  647. ->page($page)
  648. ->paginate($size);
  649. return [
  650. $paginate->items(),
  651. $paginate->total()
  652. ];
  653. }
  654. public function fetchSystemMessage($m_id, $page, $size)
  655. {
  656. $messageModel = new Message();
  657. $paginate = $messageModel->fetchAgencySystemMessage($m_id, $page, $size);
  658. $messageModel->update(["is_read" => 1], [
  659. "to_massager_id" => $m_id,
  660. "is_read" => 0,
  661. "identity_type" => \E_IDENTITY_TYPE::Massager
  662. ]);
  663. return [
  664. $paginate->items(),
  665. $paginate->total()
  666. ];
  667. }
  668. public function deposit($admin_id, $platform, $amount)
  669. {
  670. $admin = $this->model->where("id", $admin_id)->find();
  671. if ($admin["status"] != \E_BASE_STATUS::Normal)
  672. return $this->fail("状态异常无法发起提现!");
  673. if (0 == $admin["allow_deposit"])
  674. return $this->fail("请联系管理员开放提现权限!");
  675. if (mb_strlen($admin["opening_bank_name"] ?? '') == 0
  676. || mb_strlen($admin["bank_real_name"] ?? '') == 0
  677. || mb_strlen($admin["bank_no"] ?? '') == 0
  678. )
  679. return $this->fail("未绑定银行卡信息,无法发起提现");
  680. $to_day = (int)date("d");
  681. $c = config("site.date_of_deposit");
  682. $date_of_deposit = explode("|", $c);
  683. if (false === $date_of_deposit)
  684. return $this->fail("无法提现,管理员设置提现日期错误");
  685. $allow_deposit = false;
  686. foreach ($date_of_deposit as $item) {
  687. if ((int)$item === $to_day) {
  688. $allow_deposit = true;
  689. break;
  690. }
  691. }
  692. if (false === $allow_deposit)
  693. return $this->fail("提现日期为: ${$c}日,其他时间无法发起提现!");
  694. if (!$admin || !$admin["applet_openid"])
  695. return $this->fail("未绑定微信,无法发起提现!");
  696. if (1 !== $admin["allow_deposit"])
  697. return $this->fail("暂无提现权限!");
  698. $where = [
  699. "apply_status" => \E_BASE_STATUS::Default,
  700. "deposit_status" => \E_BASE_STATUS::Default
  701. ];
  702. $city_code = null;
  703. if (\E_IDENTITY_TYPE::Agency === $admin["type"]) {
  704. if ($amount > $admin["profit_amount"]) {
  705. return $this->fail("数额不足,无法发起提现");
  706. }
  707. $agency_deposit_rate = config("site.agency_deposit_rate") ?? 50;
  708. $allow_deposit_amount = $admin["profit_amount"] * ($agency_deposit_rate / 100);
  709. if ($amount > $allow_deposit_amount) {
  710. return $this->fail("您最高可提现{$allow_deposit_amount}!可提现比例为 ${$agency_deposit_rate}%");
  711. }
  712. array_merge($where, [
  713. "identity_type" => \E_IDENTITY_TYPE::Agency,
  714. "agency_id" => $admin_id
  715. ]);
  716. $city_code = explode(",", $admin["city_codes"])[0];
  717. } else {
  718. $store = $this->storeModel->findById($admin["store_id"]);
  719. if (!$store)
  720. return $this->fail("球房信息不存在!");
  721. if ($store["profit_amount"] < $amount) {
  722. return $this->fail("数额不足,无法发起提现");
  723. }
  724. array_merge($where, [
  725. "identity_type" => \E_IDENTITY_TYPE::Store,
  726. "store_id" => $admin["store_id"],
  727. ]);
  728. $city_code = $store["city_code"];
  729. }
  730. $record = (new Record())->where($where)->find();
  731. if ($record) {
  732. return $this->fail("您上一笔提现未通过审核,等待上一笔提现后再次发起");
  733. }
  734. (new Record())->save([
  735. "no" => "TX" . (\E_IDENTITY_TYPE::Agency === $admin["type"] ? "A" : "S") . time() . rand(10000, 99999),
  736. "platform" => $platform,
  737. "identity_type" => $admin["type"],
  738. "store_id" => \E_IDENTITY_TYPE::Agency === $admin["type"] ? null : $admin["store_id"],
  739. "agency_id" => \E_IDENTITY_TYPE::Agency === $admin["type"] ? $admin["id"] : null,
  740. "massager_id" => null,
  741. "city_code" => $city_code,
  742. "deposit_amount" => $amount,
  743. "service_charge_rate" => config("site.service_charge_rate"),
  744. "apply_status" => \E_BASE_STATUS::Default,
  745. "deposit_status" => \E_BASE_STATUS::Default,
  746. "operation_id" => $admin["id"],
  747. "opening_bank_name" => $admin["opening_bank_name"],
  748. "bank_real_name" => $admin["bank_real_name"],
  749. "bank_no" => $admin["bank_no"],
  750. "createtime" => time(),
  751. "updatetime" => time()
  752. ]);
  753. return $this->ok(true, "申请提现成功,请耐心等待管理员审核!");
  754. }
  755. }