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.

428 lines
19 KiB

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