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.

457 lines
14 KiB

  1. <?php
  2. namespace App\Service;
  3. use App\Model\FinancialRecord;
  4. use App\Model\Store;
  5. use App\Model\UserBalance;
  6. use Hyperf\Di\Annotation\Inject;
  7. class FinancialRecordService implements FinancialRecordServiceInterface
  8. {
  9. /**
  10. * @Inject
  11. * @var SmsServiceInterface
  12. */
  13. protected $smsAliService;
  14. public function ledgerAccounts($source_id, $money, $source_type, $money_type, $desc, $comment='')
  15. {
  16. return $this->record(
  17. FinancialRecord::ACCOUNT_LEDGER,
  18. [
  19. 'user_id' => FinancialRecord::ACCOUNT_LEDGER,
  20. 'user_type' => FinancialRecord::USER_TYPE_LEDGER,
  21. 'money' => $money,
  22. 'money_type' => $money_type,
  23. 'source_id' => $source_id,
  24. 'source_type' => $source_type,
  25. 'desc' => $desc,
  26. 'comment' => $comment,
  27. 'status' => FinancialRecord::STATUS_NORMAL,
  28. ],
  29. true
  30. );
  31. }
  32. public function record($user_id, $record, $isLedger=false)
  33. {
  34. $financialRecord = new FinancialRecord();
  35. if (!$isLedger) {
  36. $mod = bcmod((string)$user_id, '5', 0);
  37. $financialRecord->suffix($mod);
  38. }
  39. return $financialRecord->fill(
  40. [
  41. 'user_id' => $user_id,
  42. 'user_type' => $record['user_type'],
  43. 'money' => $record['money'],
  44. 'money_type' => $record['money_type'],
  45. 'source_id' => $record['source_id'],
  46. 'source_type' => $record['source_type'],
  47. 'desc' => $record['desc'],
  48. 'comment' => $record['comment'],
  49. 'status' => $record['status'],
  50. ]
  51. )->save();
  52. }
  53. /**
  54. * @inheritDoc
  55. */
  56. public function communityAwardByPlatNewUser(
  57. $user_id,
  58. $source_id,
  59. $money,
  60. $desc='新用户奖励',
  61. $comment='社区服务点',
  62. $user_type=FinancialRecord::USER_TYPE_CS,
  63. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  64. $money_type=FinancialRecord::MONEY_TYPE_CS_PLAT_NEW_USER
  65. )
  66. {
  67. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  68. // 维护社区服务点余额
  69. $balance = UserBalance::firstOrNew([
  70. 'user_type' => $user_type,
  71. 'source_id' => $user_id
  72. ]);
  73. $balance->balance = bcadd($balance->balance, $money, 2);
  74. $balance->save();
  75. // 发送短信
  76. $this->smsAliService->sendForCommunityFinancial($user_id, $money);
  77. }
  78. /**
  79. * @inheritDoc
  80. */
  81. public function communityAwardByPlatNewUserFirstOLOrder(
  82. $user_id,
  83. $source_id,
  84. $money,
  85. $desc='新用户首单奖励',
  86. $comment='社区服务点',
  87. $user_type=FinancialRecord::USER_TYPE_CS,
  88. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  89. $money_type=FinancialRecord::MONEY_TYPE_CS_FIRST_ORDER
  90. )
  91. {
  92. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  93. // 维护社区服务点余额
  94. $balance = UserBalance::firstOrNew([
  95. 'user_type' => $user_type,
  96. 'source_id' => $user_id
  97. ]);
  98. $balance->balance = bcadd($balance->balance, $money, 2);
  99. $balance->save();
  100. // 发送短信
  101. $this->smsAliService->sendForCommunityFinancial($user_id, $money);
  102. }
  103. /**
  104. * @inheritDoc
  105. */
  106. public function communitySeparateAccountsByOrderComp(
  107. $user_id,
  108. $source_id,
  109. $money,
  110. $desc='用户订单分成',
  111. $comment='社区服务点',
  112. $user_type=FinancialRecord::USER_TYPE_CS,
  113. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  114. $money_type=FinancialRecord::MONEY_TYPE_CS_OL_ORDER
  115. )
  116. {
  117. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  118. // 维护社区服务点余额
  119. $balance = UserBalance::firstOrNew([
  120. 'user_type' => $user_type,
  121. 'source_id' => $user_id
  122. ]);
  123. $balance->balance = bcadd($balance->balance, $money,2);
  124. $balance->save();
  125. // 发送短信
  126. $this->smsAliService->sendForCommunityFinancial($user_id, $money);
  127. }
  128. /**
  129. * @inheritDoc
  130. */
  131. public function storeAwardByPlatNewUserOFLOrder(
  132. $user_id,
  133. $source_id,
  134. $money,
  135. $desc='新用户下单奖励',
  136. $comment='用户当面付商户奖励',
  137. $user_type=FinancialRecord::USER_TYPE_STORE,
  138. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  139. $money_type=FinancialRecord::MONEY_TYPE_STORE_PLAT_NEW_USER
  140. )
  141. {
  142. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  143. // 同时维护钱包
  144. $store = Store::query()->where(['user_id' => $user_id])->first();
  145. $store->award_money = bcadd($store->award_money, $money, 2);
  146. $store->save();
  147. // 维护余额
  148. $balance = UserBalance::firstOrNew([
  149. 'user_type' => $user_type,
  150. 'source_id' => $store->id
  151. ]);
  152. $balance->balance = bcadd($balance->balance, $money, 2);
  153. $balance->save();
  154. }
  155. /**
  156. * @inheritDoc
  157. */
  158. public function storeAwardByTodayFirstOFLOrder(
  159. $user_id,
  160. $source_id,
  161. $money,
  162. $desc='用户店铺首单奖励',
  163. $comment='用户当面付商户奖励',
  164. $user_type=FinancialRecord::USER_TYPE_STORE,
  165. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  166. $money_type=FinancialRecord::MONEY_TYPE_STORE_FIRST_ORDER
  167. )
  168. {
  169. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  170. // 同时维护钱包
  171. $store = Store::query()->where(['user_id' => $user_id])->first();
  172. $store->award_money = bcadd($store->award_money, $money, 2);
  173. $store->save();
  174. // 维护余额
  175. $balance = UserBalance::firstOrNew([
  176. 'user_type' => $user_type,
  177. 'source_id' => $store->id
  178. ]);
  179. $balance->balance = bcadd($balance->balance, $money, 2);
  180. $balance->save();
  181. }
  182. public function recordAll($user_id, $source_id, $money, $user_type=1, $source_type=0, $money_type=0, $desc='', $comment='') {
  183. $this->record(
  184. $user_id,
  185. [
  186. 'user_id' => $user_id,
  187. 'user_type' => $user_type,
  188. 'money' => $money,
  189. 'money_type' => $money_type,
  190. 'source_id' => $source_id,
  191. 'source_type' => $source_type,
  192. 'desc' => $desc,
  193. 'comment' => $comment,
  194. 'status' => FinancialRecord::STATUS_NORMAL,
  195. ]
  196. );
  197. $this->ledgerAccounts($source_id, $money, $source_type, $money_type, $desc, $comment);
  198. }
  199. /**
  200. * @inheritDoc
  201. */
  202. public function userByOFLOrderPaid(
  203. $user_id,
  204. $source_id,
  205. $money,
  206. $desc='用户下单(线下)',
  207. $comment='用户下单',
  208. $user_type=FinancialRecord::USER_TYPE_USER,
  209. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  210. $money_type=FinancialRecord::MONEY_TYPE_USER_OFL_ORDER
  211. )
  212. {
  213. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  214. }
  215. /**
  216. * @inheritDoc
  217. */
  218. public function userByOLOrderPaid(
  219. $user_id,
  220. $source_id,
  221. $money,
  222. $desc='用户下单(线上)',
  223. $comment='用户下单',
  224. $user_type=FinancialRecord::USER_TYPE_USER,
  225. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  226. $money_type=FinancialRecord::MONEY_TYPE_USER_OL_ORDER
  227. )
  228. {
  229. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  230. }
  231. /**
  232. * @inheritDoc
  233. */
  234. public function storeByOLOrderComp(
  235. $user_id,
  236. $source_id,
  237. $money,
  238. $desc = '线上外卖订单收入',
  239. $comment = '用户订单完成',
  240. $user_type = FinancialRecord::USER_TYPE_STORE,
  241. $source_type = FinancialRecord::SOURCE_TYPE_ORDER,
  242. $money_type = FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP
  243. )
  244. {
  245. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  246. // 同时维护钱包
  247. $store = Store::query()->where(['user_id' => $user_id])->first();
  248. $store->store_wallet = bcadd($store->store_wallet, $money, 2);
  249. $store->save();
  250. // 维护余额
  251. $balance = UserBalance::firstOrNew([
  252. 'user_type' => $user_type,
  253. 'source_id' => $store->id
  254. ]);
  255. $balance->balance = bcadd($balance->balance, $money, 2);
  256. $balance->save();
  257. }
  258. /**
  259. * @inheritDoc
  260. */
  261. public function storeByOFLOrderComp(
  262. $user_id,
  263. $source_id,
  264. $money,
  265. $desc = '线下当面付订单收入',
  266. $comment = '用户订单完成',
  267. $user_type = FinancialRecord::USER_TYPE_STORE,
  268. $source_type = FinancialRecord::SOURCE_TYPE_ORDER,
  269. $money_type = FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP
  270. )
  271. {
  272. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  273. // 同时维护钱包
  274. $store = Store::query()->where(['user_id' => $user_id])->first();
  275. $store->store_wallet = bcadd($store->store_wallet, $money, 2);
  276. $store->save();
  277. // 维护余额
  278. $balance = UserBalance::firstOrNew([
  279. 'user_type' => $user_type,
  280. 'source_id' => $store->id
  281. ]);
  282. $balance->balance = bcadd($balance->balance, $money, 2);
  283. $balance->save();
  284. }
  285. /**
  286. * @inheritDoc
  287. * 订单退款(线上)
  288. */
  289. public function userByOLOrderRefund(
  290. $user_id,
  291. $source_id,
  292. $money,
  293. $desc = '线上订单退款',
  294. $comment = '线上订单退款到微信',
  295. $user_type = FinancialRecord::USER_TYPE_USER,
  296. $source_type = FinancialRecord::SOURCE_TYPE_ORDER,
  297. $money_type = FinancialRecord::MONEY_TYPE_USER_OL_ORDER_REFUND
  298. )
  299. {
  300. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  301. }
  302. /**
  303. * @inheritDoc
  304. */
  305. public function mmAwardByPlatNewUser(
  306. $user_id,
  307. $source_id,
  308. $money,
  309. $desc = '发展新用户',
  310. $comment = '市场经理发展新用户奖励',
  311. $user_type = FinancialRecord::USER_TYPE_MM,
  312. $source_type = FinancialRecord::SOURCE_TYPE_ORDER,
  313. $money_type = FinancialRecord::MONEY_TYPE_MM_PLAT_NEW_USER
  314. )
  315. {
  316. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  317. // 维护余额
  318. $balance = UserBalance::firstOrNew([
  319. 'user_type' => $user_type,
  320. 'source_id' => $user_id
  321. ]);
  322. $balance->balance = bcadd($balance->balance, $money, 2);
  323. $balance->save();
  324. }
  325. /**
  326. * @inheritDoc
  327. */
  328. public function mmAwardByNewStore(
  329. $user_id,
  330. $source_id,
  331. $money,
  332. $desc = '发展新商户',
  333. $comment = '市场经理发展新商户奖励',
  334. $user_type = FinancialRecord::USER_TYPE_MM,
  335. $source_type = FinancialRecord::SOURCE_TYPE_ORDER,
  336. $money_type = FinancialRecord::MONEY_TYPE_MM_PLAT_NEW_STORE
  337. )
  338. {
  339. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  340. // 维护余额
  341. $balance = UserBalance::firstOrNew([
  342. 'user_type' => $user_type,
  343. 'source_id' => $user_id
  344. ]);
  345. $balance->balance = bcadd($balance->balance, $money, 2);
  346. $balance->save();
  347. }
  348. /**
  349. * @inheritDoc
  350. */
  351. public function mpAwardByPlatNewUser(
  352. $user_id,
  353. $source_id,
  354. $money,
  355. $desc = '服务商发展新用户',
  356. $comment = '服务商发展新用户奖励',
  357. $user_type = FinancialRecord::USER_TYPE_MP,
  358. $source_type = FinancialRecord::SOURCE_TYPE_ORDER,
  359. $money_type = FinancialRecord::MONEY_TYPE_MP_PLAT_NEW_USER
  360. )
  361. {
  362. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  363. // 维护余额
  364. $balance = UserBalance::firstOrNew([
  365. 'user_type' => $user_type,
  366. 'source_id' => $user_id
  367. ]);
  368. $balance->balance = bcadd($balance->balance, $money, 2);
  369. $balance->save();
  370. }
  371. /**
  372. * @inheritDoc
  373. */
  374. public function mpAwardByNewStore(
  375. $user_id,
  376. $source_id,
  377. $money,
  378. $desc = '服务商发展新商户',
  379. $comment = '服务商发展新商户奖励',
  380. $user_type = FinancialRecord::USER_TYPE_MP,
  381. $source_type = FinancialRecord::SOURCE_TYPE_ORDER,
  382. $money_type = FinancialRecord::MONEY_TYPE_MP_PLAT_NEW_STORE
  383. )
  384. {
  385. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  386. // 维护余额
  387. $balance = UserBalance::firstOrNew([
  388. 'user_type' => $user_type,
  389. 'source_id' => $user_id
  390. ]);
  391. $balance->balance = bcadd($balance->balance, $money, 2);
  392. $balance->save();
  393. }
  394. /**
  395. * @inheritDoc
  396. */
  397. public function mpSeparateAccountByOLOrderComp(
  398. $user_id,
  399. $source_id,
  400. $money,
  401. $desc = '服务商线上订单分账',
  402. $comment = '服务商用户线上订单完成后分账',
  403. $user_type = FinancialRecord::USER_TYPE_MP,
  404. $source_type = FinancialRecord::SOURCE_TYPE_ORDER,
  405. $money_type = FinancialRecord::MONEY_TYPE_MP_OL_ORDER
  406. )
  407. {
  408. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  409. // 维护余额
  410. $balance = UserBalance::firstOrNew([
  411. 'user_type' => $user_type,
  412. 'source_id' => $user_id
  413. ]);
  414. $balance->balance = bcadd($balance->balance, $money, 2);
  415. $balance->save();
  416. }
  417. }