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.

426 lines
18 KiB

6 years ago
6 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. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  68. // 线上订单完成(用户点击确认收货完成/管理后台点击完成/配送员点击完成/自动收货等),进行相关分账
  69. // 订单
  70. $orderMain = OrderMain::query()
  71. ->where(['global_order_id' => $global_order_id])
  72. ->whereIn('state', [OrderMain::ORDER_STATE_COMPLETE,OrderMain::ORDER_STATE_EVALUATED,OrderMain::ORDER_STATE_UNREFUND])
  73. ->first();
  74. if (empty($orderMain)) {
  75. return false;
  76. }
  77. $currentTime = time();
  78. Db::beginTransaction();
  79. try {
  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. $this->financialRecordService->mmAwardByNewStore($mmInfo->admin_user_id, $global_order_id, $MmMpAwardConfig['mm_new_store'], '发展新商户【'.$store->name.'】'); // 市场经理新商户奖励
  182. $this->financialRecordService->mpAwardByNewStore($mpInfo->admin_user_id, $global_order_id, $MmMpAwardConfig['mp_new_store'], '市场经理「'.$mmInfo->name.'」发展新商户【'.$store->name.'】'); // 服务商新商户奖励
  183. }
  184. }
  185. // 线上订单服务商分账
  186. if ($orderMain->type == OrderMain::ORDER_TYPE_ONLINE) {
  187. $money = bcmul($orderMain->money, bcdiv($MmMpAwardConfig['separate_rate'], 100, 6), 2);
  188. $this->financialRecordService->mpSeparateAccountByOLOrderComp($mpInfo->admin_user_id, $global_order_id, $money);
  189. }
  190. // =======服务商、市场经理奖励分账 / End=======
  191. Db::commit();
  192. return true;
  193. } catch (\Exception $e) {
  194. $this->log->event(LogLabel::SEPARATE_ACCOUNTS_LOG, ['exception' => $e->getMessage(), 'order_main' => json_encode($orderMain)]);
  195. Db::rollBack();
  196. return false;
  197. }
  198. }
  199. /**
  200. * @inheritDoc
  201. */
  202. public function orderOfflinePaid($global_order_id)
  203. {
  204. // 线下订单支付完成
  205. // 订单
  206. $orderMain = OrderMain::query()
  207. ->where(['global_order_id' => $global_order_id])
  208. ->first();
  209. if (empty($orderMain)) {
  210. return false;
  211. }
  212. // 查询子订单,当面付目前实际上只有一个子订单
  213. $order = Order::query()->select(['id', 'money', 'user_id', 'store_id', 'pay_time'])
  214. ->where(['order_main_id' => $orderMain->id])
  215. ->first();
  216. if (empty($order)) {
  217. return false;
  218. }
  219. $currentTime = time();
  220. Db::beginTransaction();
  221. try {
  222. // =======用户支付流水 / Start=======
  223. $this->financialRecordService->userByOFLOrderPaid($orderMain->user_id, $global_order_id, $orderMain->money);
  224. // =======用户支付流水 / End=======
  225. // =======线下订单支付完成商户分账 / Start=======
  226. // 前提:用户线上下单并且支付完成
  227. // 奖励规则A:用户是平台新用户,奖励商户2元
  228. // 奖励规则B:用户是非新用户,但是是商户当日首单,奖励商户0.05元
  229. // =======线下订单支付完成商户分账 / Start=======
  230. // 旧商户订单流水基础数据 TODO 直接移除或后续考虑移除
  231. $storeAccountBase = [
  232. 'user_id' => $order->user_id,
  233. 'order_id' => $order->id,
  234. 'store_id' => $order->store_id,
  235. 'type' => 1,
  236. 'time' => date('Y-m-d H:i:s', $currentTime),
  237. 'add_time' => $currentTime,
  238. ];
  239. // 旧商户订单流水 TODO 直接移除或后续考虑移除
  240. $storeAccount = [
  241. 'money' => $order->money,
  242. 'note' => '当面付订单收入',
  243. 'category' => 2,
  244. ];
  245. StoreAccount::query()->insert(array_merge($storeAccountBase, $storeAccount));
  246. // 商户
  247. $store = Store::find($order->store_id);
  248. // 新商户订单流水
  249. $this->financialRecordService->storeByOFLOrderComp($store->user_id, $global_order_id, $order->money);
  250. $needAward = false;
  251. $awardAmount = 0;
  252. // 新用户商户奖励
  253. if ($this->userService->isPlatformNewUser($orderMain->user_id, $orderMain->id)) {
  254. $awardAmount = SystemConfig::query()->where(['type' => 1, 'menu_name' => 'award_new_user'])->value('value');
  255. // 旧商户流水 TODO 直接移除或后续考虑移除
  256. $storeAccount = [
  257. 'money' => $awardAmount,
  258. 'note' => '新用户下单成功,平台奖励',
  259. 'category' => 3,
  260. ];
  261. // 新商户流水
  262. $this->financialRecordService->storeAwardByPlatNewUserOFLOrder($store->user_id, $global_order_id, $awardAmount);
  263. $needAward = true;
  264. }
  265. else {
  266. // 商户当日首单奖励
  267. if (
  268. $this->userService->isStoreFirstOrderToday(
  269. $order->user_id,
  270. $order->store_id,
  271. $order->id,
  272. FinancialRecord::OFL_FIRST_AWARD_LIMIT_AMOUNT
  273. )
  274. && $order->money >= FinancialRecord::OFL_FIRST_AWARD_LIMIT_AMOUNT
  275. ) {
  276. $awardAmount = SystemConfig::query()->where(['type' => 1, 'menu_name' => 'award_each_order'])->value('value');
  277. // 旧商户流水 TODO 直接移除或后续考虑移除
  278. $storeAccount = [
  279. 'money' => $awardAmount,
  280. 'note' => '用户下单成功,平台奖励',
  281. 'category' => 4,
  282. ];
  283. // 新商户流水
  284. $this->financialRecordService->storeAwardByTodayFirstOFLOrder($store->user_id, $global_order_id, $awardAmount);
  285. $needAward = true;
  286. }
  287. }
  288. if ($needAward && $awardAmount) {
  289. // 旧商户流水 TODO 直接移除或后续考虑移除
  290. StoreAccount::query()->insert(array_merge($storeAccountBase, $storeAccount));
  291. // 发模板消息
  292. $openid = Users::query()->where(['id' => $store['user_id']])->value('openid');
  293. $res = $this->miniprogramService->sendTemMsgForAward($storeAccount['money'], $storeAccount['note'], $openid, $storeAccountBase['time']);
  294. }
  295. // =======线下订单支付完成商户分账 / End=======
  296. // =======服务商、市场经理奖励分账 / Start=======
  297. // 前提A:新用户下单并且订单完成(线上、线下都行)
  298. // 奖励规则A:用户是平台新用户,奖励市场经理 1 元,服务商 0.5 元,如果是线上订单,服务商有6%的订单分成
  299. // 前提B:新商户旗下产生 10 个新用户
  300. // 奖励规则B:奖励市场经理 25 元,服务商 10 元(仅仅奖励一次)
  301. // 前提C:用户线上下单并且订单完成
  302. // 奖励规则C:奖励服务商账单 6% 分成
  303. // 判断是新商户:入驻绑定的时候把关系存在SSDB
  304. // =======服务商、市场经理奖励分账 / Start=======
  305. $MmMpAwardConfig = [
  306. 'mm_new_user' => 1,
  307. 'mm_new_store' => 25,
  308. 'mp_new_user' => 0.5,
  309. 'mp_new_store' => 10,
  310. 'separate_rate' => 6,
  311. 'limit_new_user_number' => 10,
  312. ];
  313. // 当前订单(子)对应商户是否有市场经理绑定关系
  314. $store = Store::query()->find($order['store_id']); // 商户
  315. $mmInfo = MmInfo::query()->where(['user_id' => $store->mm_user_id])->first(); // 市场经理
  316. $market = Market::query()->find($mmInfo->market_id); // 市场
  317. $mpInfo = MpInfo::query()->find($market->mp_id); // 服务商
  318. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  319. $ssdbName = 'mm_'.$mmInfo->id.'_award_'.$store->id;
  320. // TODO 暂时在这里初始化
  321. if (!$ssdb->exec('hexists', $ssdbName, 'is_awarded')) {
  322. $ssdb->exec('hset', $ssdbName, 'is_awarded', 0);
  323. }
  324. if (!$ssdb->exec('hexists', $ssdbName, 'new_user_number')) {
  325. $ssdb->exec('hset', $ssdbName, 'new_user_number', 0);
  326. }
  327. // 平台新用户
  328. if ($this->userService->isPlatformNewUser($orderMain->user_id, $orderMain->id)) {
  329. $ssdb->exec('hincr', $ssdbName, 'new_user_number', 1);
  330. $this->financialRecordService->mmAwardByPlatNewUser($mmInfo->admin_user_id, $global_order_id, $MmMpAwardConfig['mm_new_user'], '发展新用户'); // 市场经理新用户奖励
  331. $this->financialRecordService->mpAwardByPlatNewUser($mpInfo->admin_user_id, $global_order_id, $MmMpAwardConfig['mp_new_user'], '市场经理「'.$mmInfo->name.'」发展新用户'); // 服务商新用户奖励
  332. }
  333. $record = $ssdb->exec('hgetall', $ssdbName);
  334. // 判断是否已经奖励过新拓展商户的奖励,没有的话判断新用户个数是否达到要求,进行奖励
  335. if (
  336. !empty($record)
  337. &&$record['is_awarded']==0
  338. &&$record['new_user_number']>=$MmMpAwardConfig['limit_new_user_number']
  339. ) { // 存在记录且未发放奖励,同时新用户数已经超过10
  340. $this->financialRecordService->mmAwardByNewStore($mmInfo->admin_user_id, $global_order_id, $MmMpAwardConfig['mm_new_store'], '发展新商户【'.$store->name.'】'); // 市场经理新商户奖励
  341. $this->financialRecordService->mpAwardByNewStore($mpInfo->admin_user_id, $global_order_id, $MmMpAwardConfig['mp_new_store'], '市场经理「'.$mmInfo->name.'」发展新新商户【'.$store->name.'】'); // 服务商新商户奖励
  342. }
  343. // =======服务商、市场经理奖励分账 / End=======
  344. Db::commit();
  345. return true;
  346. } catch (\Exception $e) {
  347. $this->log->event(LogLabel::SEPARATE_ACCOUNTS_LOG, ['exception' => $e->getMessage(), 'order_main' => json_encode($orderMain)]);
  348. Db::rollBack();
  349. return false;
  350. }
  351. }
  352. }