You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

424 lines
20 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Commons\Log;
  4. use App\Constants\v3\LogLabel;
  5. use App\Constants\v3\OrderState;
  6. use App\Constants\v3\OrderType;
  7. use App\Model\v3\FinancialRecord;
  8. use App\Model\v3\Market;
  9. use App\Model\v3\MmInfo;
  10. use App\Model\v3\MpInfo;
  11. use App\Model\v3\Order;
  12. use App\Model\v3\OrderMain;
  13. use App\Model\v3\ServiceReward;
  14. use App\Model\v3\Store;
  15. use App\Model\v3\UserRelationBind;
  16. use App\Model\v3\User;
  17. use App\Service\v3\Interfaces\FinancialRecordServiceInterface;
  18. use App\Service\v3\Interfaces\MiniprogramServiceInterface;
  19. use App\Service\v3\Interfaces\OrderOnlineServiceInterface;
  20. use App\Service\v3\Interfaces\SeparateAccountsServiceInterface;
  21. use App\Service\v3\Interfaces\SmsSendServiceInterface;
  22. use App\Service\v3\Interfaces\UserServiceInterface;
  23. use App\TaskWorker\SSDBTask;
  24. use Hyperf\DbConnection\Db;
  25. use Hyperf\Di\Annotation\Inject;
  26. use Hyperf\Utils\ApplicationContext;
  27. class SeparateAccountsService implements SeparateAccountsServiceInterface
  28. {
  29. /**
  30. * @Inject
  31. * @var Log
  32. */
  33. protected $log;
  34. /**
  35. * @Inject
  36. * @var UserServiceInterface
  37. */
  38. protected $userService;
  39. /**
  40. * @Inject
  41. * @var FinancialRecordServiceInterface
  42. */
  43. protected $financialRecordService;
  44. /**
  45. * @Inject
  46. * @var MiniprogramServiceInterface
  47. */
  48. protected $miniprogramService;
  49. /**
  50. * @Inject
  51. * @var OrderOnlineServiceInterface
  52. */
  53. protected $orderOnlineService;
  54. /**
  55. * @Inject
  56. * @var SmsSendServiceInterface
  57. */
  58. protected $smsAliSendService;
  59. /**
  60. * @inheritDoc
  61. */
  62. public function orderOnlinePaid($globalOrderId)
  63. {
  64. try {
  65. // 线上订单支付完成
  66. // 订单
  67. $orderMain = OrderMain::query()->where(['global_order_id' => $globalOrderId])->first();
  68. if (empty($orderMain)) {
  69. return false;
  70. }
  71. // =======用户支付流水 / Start=======
  72. $this->financialRecordService->userByOLOrderPaid($orderMain->user_id, $orderMain->global_order_id, $orderMain->money);
  73. // =======用户支付流水 / End=======
  74. return true;
  75. } catch (\Exception $e) {
  76. $this->log->event(LogLabel::SEPARATE_ACCOUNTS_LOG, ['exception' => $e->getMessage()]);
  77. return false;
  78. }
  79. }
  80. /**
  81. * @inheritDoc
  82. */
  83. public function orderOnlineCompleted($globalOrderId, $userId)
  84. {
  85. // 线上订单完成(用户点击确认收货完成/管理后台点击完成/配送员点击完成/自动收货等),进行相关分账
  86. // 订单
  87. $orderMain = $this->orderOnlineService->check($globalOrderId, $userId,OrderState::FINISH);
  88. $currentTime = time();
  89. Db::beginTransaction();
  90. try {
  91. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  92. // =======商户订单收入流水 / Start=======
  93. // 查询子订单
  94. $orders = Order::query()
  95. ->where(['order_main_id' => $orderMain->global_order_id])
  96. ->get()->toArray();
  97. // 新商户流水
  98. foreach ($orders as $key => &$order) {
  99. $store = Store::query()->find($order['store_id']);
  100. $this->financialRecordService->storeByOLOrderComp($store->user_id, $orderMain->global_order_id ,$order['money']);
  101. }
  102. // =======商户订单收入流水 / End=======
  103. // =======社区服务点分账 / Start=======
  104. // 前提:用户线上下单并且订单完成
  105. // 奖励规则A:用户是平台新用户,奖励社区服务点平台新用户奖励x元+平台新用户首单奖励y元+订单商品金额z%的分成
  106. // 奖励规则B:用户是非新用户,奖励社区服务点订单实际支付金额z%的分成
  107. // =======社区服务点分账 / Start=======
  108. // 当前用户的社区服务点绑定关系
  109. $communityBind = UserRelationBind::query()
  110. ->where(['bind_type' => UserRelationBind::BIND_TYPE_COMMUNITY, 'user_id' => $orderMain->user_id])
  111. ->first();
  112. if ($communityBind) {
  113. // 奖励/分账金额
  114. $award = ServiceReward::query()->where(['type' => ServiceReward::TYPE_COMMUNITY])->first();
  115. if (empty($award)) {
  116. Db::rollBack();
  117. return false;
  118. }
  119. $award = $award->set_reward;
  120. // 平台新用户
  121. if ($this->userService->isPlatformNewUser($orderMain->user_id, $orderMain->global_order_id)) {
  122. $this->financialRecordService->communityAwardByPlatNewUser(
  123. $communityBind->source_id,
  124. $orderMain->global_order_id,
  125. $award['new_user_reward']
  126. );
  127. // 发送短信
  128. co(function () use ($orderMain, $award) {
  129. $this->smsAliSendService->doCommunityFinancial($orderMain->user_id, $award['new_user_reward']);
  130. });
  131. $this->financialRecordService->communityAwardByPlatNewUserFirstOLOrder(
  132. $communityBind->source_id,
  133. $orderMain->global_order_id,
  134. $award['first_reward']
  135. );
  136. // 发送短信
  137. co(function () use ($orderMain, $award) {
  138. $this->smsAliSendService->doCommunityFinancial($orderMain->user_id, $award['first_reward']);
  139. });
  140. }
  141. // 账单分成
  142. $money = bcmul($orderMain->money, bcdiv($award['flow_reward'], 100, 6), 2);
  143. $this->financialRecordService->communitySeparateAccountsByOrderComp($communityBind->source_id, $orderMain->global_order_id, $money);
  144. // 发送短信
  145. co(function () use ($orderMain, $money) {
  146. $this->smsAliSendService->doCommunityFinancial($orderMain->user_id, $money);
  147. });
  148. }
  149. // =======社区服务点分账 / End=======
  150. // =======服务商、市场经理奖励分账 / Start=======
  151. // 前提A:新用户下单并且订单完成(线上、线下都行)
  152. // 奖励规则A:用户是平台新用户,奖励市场经理 1 元,服务商 0.5 元,如果是线上订单,服务商有6%的订单分成
  153. // 前提B:新商户旗下产生 10 个新用户
  154. // 奖励规则B:奖励市场经理 25 元,服务商 10 元(仅仅奖励一次)
  155. // 前提C:用户线上下单并且订单完成
  156. // 奖励规则C:奖励服务商账单 6% 分成
  157. // 判断是新商户:入驻绑定的时候把关系存在SSDB
  158. // =======服务商、市场经理奖励分账 / Start=======
  159. $MmMpAwardConfig = [
  160. 'mm_new_user' => 1,
  161. 'mm_new_store' => 25,
  162. 'mp_new_user' => 0.5,
  163. 'mp_new_store' => 10,
  164. 'separate_rate' => 6,
  165. 'limit_new_user_number' => 10,
  166. ];
  167. foreach ($orders as $key => &$order) {
  168. // 当前订单(子)对应商户是否有市场经理绑定关系
  169. $store = Store::query()->find($order['store_id']); // 商户
  170. // $mmInfo = MmInfo::query()->where(['user_id' => $store->mm_user_id])->first(); // 市场经理
  171. // if (empty($mmInfo)) continue;
  172. // $market = Market::query()->find($mmInfo->market_id); // 市场
  173. $market = Market::query()->find($orderMain->market_id); // 市场
  174. if (empty($market)) continue;
  175. $mpInfo = MpInfo::query()->find($market->mp_id); // 服务商
  176. if (empty($mpInfo)) continue;
  177. // $ssdbName = 'mm_'.$mmInfo->id.'_award_'.$store->id;
  178. $ssdbName = 'mm_'.$mpInfo->id.'_award_'.$store->id;
  179. // TODO 暂时在这里初始化
  180. if (!$ssdb->exec('hexists', $ssdbName, 'is_awarded')) {
  181. $ssdb->exec('hset', $ssdbName, 'is_awarded', 0);
  182. }
  183. if (!$ssdb->exec('hexists', $ssdbName, 'new_user_number')) {
  184. $ssdb->exec('hset', $ssdbName, 'new_user_number', 0);
  185. }
  186. // 平台新用户
  187. if ($this->userService->isPlatformNewUser($orderMain->user_id, $orderMain->id)) {
  188. $ssdb->exec('hincr', $ssdbName, 'new_user_number', 1);
  189. // $this->financialRecordService->mmAwardByPlatNewUser($mmInfo->admin_user_id, $globalOrderId, $MmMpAwardConfig['mm_new_user'], '发展新用户'); // 市场经理新用户奖励
  190. // $this->financialRecordService->mpAwardByPlatNewUser($mpInfo->admin_user_id, $globalOrderId, $MmMpAwardConfig['mp_new_user'], '市场经理「'.$mmInfo->name.'」发展新用户'); // 服务商新用户奖励
  191. $this->financialRecordService->mpAwardByPlatNewUser($mpInfo->admin_user_id, $globalOrderId, $MmMpAwardConfig['mp_new_user'], '发展新用户'); // 服务商新用户奖励
  192. }
  193. $record = $ssdb->exec('hgetall', $ssdbName);
  194. // 判断是否已经奖励过新拓展商户的奖励,没有的话判断新用户个数是否达到要求,进行奖励
  195. if (
  196. !empty($record)
  197. &&$record['is_awarded']==0
  198. &&$record['new_user_number']>=$MmMpAwardConfig['limit_new_user_number']
  199. ) { // 存在记录且未发放奖励,同时新用户数已经超过10
  200. $ssdb->exec('hset', $ssdbName, 'is_awarded', 1);
  201. // $this->financialRecordService->mmAwardByNewStore($mmInfo->admin_user_id, $globalOrderId, $MmMpAwardConfig['mm_new_store'], '发展新商户【'.$store->name.'】'); // 市场经理新商户奖励
  202. // $this->financialRecordService->mpAwardByNewStore($mpInfo->admin_user_id, $globalOrderId, $MmMpAwardConfig['mp_new_store'], '市场经理「'.$mmInfo->name.'」发展新商户【'.$store->name.'】'); // 服务商新商户奖励
  203. $this->financialRecordService->mpAwardByNewStore($mpInfo->admin_user_id, $globalOrderId, $MmMpAwardConfig['mp_new_store'], '发展新商户【'.$store->name.'】'); // 服务商新商户奖励
  204. }
  205. // 线上订单服务商分账
  206. if ($orderMain->type == OrderType::ONLINE) {
  207. $rate = bcdiv($order['money'], $orderMain->total_money, 6);
  208. $preMoney = bcmul($orderMain->money, $rate, 6);
  209. $money = bcmul($preMoney, bcdiv($MmMpAwardConfig['separate_rate'], 100, 6), 2);
  210. $this->financialRecordService->mpSeparateAccountByOLOrderComp($mpInfo->admin_user_id, $globalOrderId, $money);
  211. }
  212. }
  213. // =======服务商、市场经理奖励分账 / End=======
  214. Db::commit();
  215. return true;
  216. } catch (\Exception $e) {
  217. $this->log->event(LogLabel::SEPARATE_ACCOUNTS_LOG, ['exception' => $e->getMessage(), 'order_main' => json_encode($orderMain)]);
  218. Db::rollBack();
  219. return false;
  220. }
  221. }
  222. /**
  223. * @inheritDoc
  224. */
  225. public function orderOfflinePaid($globalOrderId)
  226. {
  227. // 线下订单支付完成
  228. // 订单
  229. $orderMain = OrderMain::query()->where(['global_order_id' => $globalOrderId])->first();
  230. if (empty($orderMain)) {
  231. return false;
  232. }
  233. // 查询子订单,当面付目前实际上只有一个子订单
  234. $order = Order::query()
  235. ->where(['order_main_id' => $orderMain->global_order_id])
  236. ->first();
  237. if (empty($order)) {
  238. return false;
  239. }
  240. $currentTime = time();
  241. Db::beginTransaction();
  242. try {
  243. // =======用户支付流水 / Start=======
  244. $this->financialRecordService->userByOFLOrderPaid($orderMain->user_id, $orderMain->global_order_id, $orderMain->money);
  245. // =======用户支付流水 / End=======
  246. // =======线下订单支付完成商户分账 / Start=======
  247. // 前提:用户线上下单并且支付完成
  248. // 奖励规则A:用户是平台新用户,奖励商户2元
  249. // 奖励规则B:用户是非新用户,但是是商户当日首单,奖励商户0.05元
  250. // =======线下订单支付完成商户分账 / Start=======
  251. $message = [];
  252. // 商户
  253. $store = Store::find($order->store_id);
  254. // 新商户订单流水
  255. $this->financialRecordService->storeByOFLOrderComp($store->user_id, $orderMain->global_order_id, $order->money);
  256. $needAward = false;
  257. $awardAmount = 0;
  258. // 新用户商户奖励
  259. if ($this->userService->isPlatformNewUser($orderMain->user_id, $orderMain->global_order_id)) {
  260. $awardAmount = 2;
  261. // 旧商户流水 TODO 直接移除或后续考虑移除
  262. $message = [
  263. 'money' => $awardAmount,
  264. 'note' => '新用户下单成功,平台奖励',
  265. ];
  266. // 新商户流水
  267. $this->financialRecordService->storeAwardByPlatNewUserOFLOrder($store->user_id, $orderMain->global_order_id, $awardAmount);
  268. $needAward = true;
  269. } else {
  270. // 商户当日首单奖励
  271. if (
  272. $this->userService->isStoreFirstOrderToday(
  273. $order->user_id,
  274. $order->store_id,
  275. $order->id,
  276. FinancialRecord::OFL_FIRST_AWARD_LIMIT_AMOUNT
  277. )
  278. && $order->money >= FinancialRecord::OFL_FIRST_AWARD_LIMIT_AMOUNT
  279. ) {
  280. $awardAmount = 0.05;
  281. // 旧商户流水 TODO 直接移除或后续考虑移除
  282. $message = [
  283. 'money' => $awardAmount,
  284. 'note' => '用户下单成功,平台奖励',
  285. ];
  286. // 新商户流水
  287. $this->financialRecordService->storeAwardByTodayFirstOFLOrder($store->user_id, $orderMain->global_order_id, $awardAmount);
  288. $needAward = true;
  289. }
  290. }
  291. if ($needAward && $awardAmount) {
  292. // 发模板消息
  293. $openid = User::query()->where(['id' => $store['user_id']])->value('openid');
  294. $res = $this->miniprogramService->sendTemMsgForAward($message['money'], $message['note'], $openid, date('Y-m-d H:i:s', $currentTime));
  295. }
  296. // =======线下订单支付完成商户分账 / End=======
  297. // =======线下订单支付完成商户分账 / End=======
  298. // =======服务商、市场经理奖励分账 / Start=======
  299. // 前提A:新用户下单并且订单完成(线上、线下都行)
  300. // 奖励规则A:用户是平台新用户,奖励市场经理 1 元,服务商 0.5 元,如果是线上订单,服务商有6%的订单分成
  301. // 前提B:新商户旗下产生 10 个新用户
  302. // 奖励规则B:奖励市场经理 25 元,服务商 10 元(仅仅奖励一次)
  303. // 前提C:用户线上下单并且订单完成
  304. // 奖励规则C:奖励服务商账单 6% 分成
  305. // 判断是新商户:入驻绑定的时候把关系存在SSDB
  306. // =======服务商、市场经理奖励分账 / Start=======
  307. $MmMpAwardConfig = [
  308. 'mm_new_user' => 1,
  309. 'mm_new_store' => 25,
  310. 'mp_new_user' => 0.5,
  311. 'mp_new_store' => 10,
  312. 'separate_rate' => 6,
  313. 'limit_new_user_number' => 10,
  314. ];
  315. // 当前订单(子)对应商户是否有市场经理绑定关系
  316. $store = Store::query()->find($order['store_id']); // 商户
  317. // $mmInfo = MmInfo::query()->where(['user_id' => $store->mm_user_id])->first(); // 市场经理
  318. $market = Market::query()->find($orderMain->market_id); // 市场
  319. $mpInfo = MpInfo::query()->find($market->mp_id); // 服务商
  320. if (!empty($mpInfo)) {
  321. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  322. // $ssdbName = 'mm_'.$mmInfo->id.'_award_'.$store->id;
  323. $ssdbName = 'mm_'.$mpInfo->id.'_award_'.$store->id;
  324. // TODO 暂时在这里初始化
  325. if (!$ssdb->exec('hexists', $ssdbName, 'is_awarded')) {
  326. $ssdb->exec('hset', $ssdbName, 'is_awarded', 0);
  327. }
  328. if (!$ssdb->exec('hexists', $ssdbName, 'new_user_number')) {
  329. $ssdb->exec('hset', $ssdbName, 'new_user_number', 0);
  330. }
  331. // 平台新用户
  332. if ($this->userService->isPlatformNewUser($orderMain->user_id, $orderMain->id)) {
  333. $ssdb->exec('hincr', $ssdbName, 'new_user_number', 1);
  334. // $this->financialRecordService->mmAwardByPlatNewUser($mmInfo->admin_user_id, $global_order_id, $MmMpAwardConfig['mm_new_user'], '发展新用户'); // 市场经理新用户奖励
  335. // $this->financialRecordService->mpAwardByPlatNewUser($mpInfo->admin_user_id, $global_order_id, $MmMpAwardConfig['mp_new_user'], '市场经理「'.$mmInfo->name.'」发展新用户'); // 服务商新用户奖励
  336. $this->financialRecordService->mpAwardByPlatNewUser($mpInfo->admin_user_id, $globalOrderId, $MmMpAwardConfig['mp_new_user'], '发展新用户'); // 服务商新用户奖励
  337. }
  338. $record = $ssdb->exec('hgetall', $ssdbName);
  339. // 判断是否已经奖励过新拓展商户的奖励,没有的话判断新用户个数是否达到要求,进行奖励
  340. if (
  341. !empty($record)
  342. &&$record['is_awarded']==0
  343. &&$record['new_user_number']>=$MmMpAwardConfig['limit_new_user_number']
  344. ) { // 存在记录且未发放奖励,同时新用户数已经超过10
  345. $ssdb->exec('hset', $ssdbName, 'is_awarded', 1);
  346. // $this->financialRecordService->mmAwardByNewStore($mmInfo->admin_user_id, $global_order_id, $MmMpAwardConfig['mm_new_store'], '发展新商户【'.$store->name.'】'); // 市场经理新商户奖励
  347. // $this->financialRecordService->mpAwardByNewStore($mpInfo->admin_user_id, $global_order_id, $MmMpAwardConfig['mp_new_store'], '市场经理「'.$mmInfo->name.'」发展新新商户【'.$store->name.'】'); // 服务商新商户奖励
  348. $this->financialRecordService->mpAwardByNewStore($mpInfo->admin_user_id, $globalOrderId, $MmMpAwardConfig['mp_new_store'], '发展新商户【'.$store->name.'】'); // 服务商新商户奖励
  349. }
  350. }
  351. // =======服务商、市场经理奖励分账 / End=======
  352. Db::commit();
  353. return true;
  354. } catch (\Exception $e) {
  355. $this->log->event(LogLabel::SEPARATE_ACCOUNTS_LOG, ['exception' => $e->getMessage(), 'order_main' => json_encode($orderMain)]);
  356. Db::rollBack();
  357. return false;
  358. }
  359. }
  360. }