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.

295 lines
9.3 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. $user_type=FinancialRecord::USER_TYPE_CS,
  61. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  62. $money_type=FinancialRecord::MONEY_TYPE_CS_PLAT_NEW_USER,
  63. $desc='新用户奖励',
  64. $comment='社区服务点'
  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' => UserBalance::USER_TYPE_CS,
  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. $user_type=FinancialRecord::USER_TYPE_CS,
  86. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  87. $money_type=FinancialRecord::MONEY_TYPE_CS_FIRST_ORDER,
  88. $desc='新用户首单奖励',
  89. $comment='社区服务点'
  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' => UserBalance::USER_TYPE_CS,
  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. $user_type=FinancialRecord::USER_TYPE_CS,
  111. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  112. $money_type=FinancialRecord::MONEY_TYPE_CS_OL_ORDER,
  113. $desc='用户订单分成',
  114. $comment='社区服务点'
  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' => UserBalance::USER_TYPE_CS,
  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. $user_type=FinancialRecord::USER_TYPE_STORE,
  136. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  137. $money_type=FinancialRecord::MONEY_TYPE_STORE_PLAT_NEW_USER,
  138. $desc='新用户下单奖励',
  139. $comment='用户当面付商户奖励'
  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. /**
  149. * @inheritDoc
  150. */
  151. public function storeAwardByTodayFirstOFLOrder(
  152. $user_id,
  153. $source_id,
  154. $money,
  155. $user_type=FinancialRecord::USER_TYPE_STORE,
  156. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  157. $money_type=FinancialRecord::MONEY_TYPE_STORE_FIRST_ORDER,
  158. $desc='用户店铺首单奖励',
  159. $comment='用户当面付商户奖励'
  160. )
  161. {
  162. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  163. // 同时维护钱包
  164. $store = Store::query()->where(['user_id' => $user_id])->first();
  165. $store->award_money = bcadd($store->award_money, $money, 2);
  166. $store->save();
  167. }
  168. public function recordAll($user_id, $source_id, $money, $user_type=1, $source_type=0, $money_type=0, $desc='', $comment='') {
  169. $this->record(
  170. $user_id,
  171. [
  172. 'user_id' => $user_id,
  173. 'user_type' => $user_type,
  174. 'money' => $money,
  175. 'money_type' => $money_type,
  176. 'source_id' => $source_id,
  177. 'source_type' => $source_type,
  178. 'desc' => $desc,
  179. 'comment' => $comment,
  180. 'status' => FinancialRecord::STATUS_NORMAL,
  181. ]
  182. );
  183. $this->ledgerAccounts($source_id, $money, $source_type, $money_type, $desc, $comment);
  184. }
  185. /**
  186. * @inheritDoc
  187. */
  188. public function userByOFLOrderPaid(
  189. $user_id,
  190. $source_id,
  191. $money,
  192. $user_type=FinancialRecord::USER_TYPE_USER,
  193. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  194. $money_type=FinancialRecord::MONEY_TYPE_USER_OFL_ORDER,
  195. $desc='用户下单(线下)',
  196. $comment='用户下单'
  197. )
  198. {
  199. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  200. }
  201. /**
  202. * @inheritDoc
  203. */
  204. public function userByOLOrderPaid(
  205. $user_id,
  206. $source_id,
  207. $money,
  208. $user_type=FinancialRecord::USER_TYPE_USER,
  209. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  210. $money_type=FinancialRecord::MONEY_TYPE_USER_OL_ORDER,
  211. $desc='用户下单(线上)',
  212. $comment='用户下单'
  213. )
  214. {
  215. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  216. }
  217. /**
  218. * @inheritDoc
  219. */
  220. public function storeByOLOrderComp(
  221. $user_id,
  222. $source_id,
  223. $money,
  224. $user_type = FinancialRecord::USER_TYPE_STORE,
  225. $source_type = FinancialRecord::SOURCE_TYPE_ORDER,
  226. $money_type = FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP,
  227. $desc = '线上外卖订单收入',
  228. $comment = '用户订单完成'
  229. )
  230. {
  231. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  232. // 同时维护钱包
  233. $store = Store::query()->where(['user_id' => $user_id])->first();
  234. $store->store_wallet = bcadd($store->store_wallet, $money, 2);
  235. $store->save();
  236. }
  237. /**
  238. * @inheritDoc
  239. */
  240. public function storeByOFLOrderComp(
  241. $user_id,
  242. $source_id,
  243. $money,
  244. $user_type = FinancialRecord::USER_TYPE_STORE,
  245. $source_type = FinancialRecord::SOURCE_TYPE_ORDER,
  246. $money_type = FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP,
  247. $desc = '线下当面付订单收入',
  248. $comment = '用户订单完成'
  249. )
  250. {
  251. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  252. }
  253. /**
  254. * @inheritDoc
  255. * 订单退款(线上)
  256. */
  257. public function userByOLOrderRefund(
  258. $user_id,
  259. $source_id,
  260. $money,
  261. $user_type = FinancialRecord::USER_TYPE_USER,
  262. $source_type = FinancialRecord::SOURCE_TYPE_ORDER,
  263. $money_type = FinancialRecord::MONEY_TYPE_USER_OL_ORDER_REFUND,
  264. $desc = '线上订单退款',
  265. $comment = '线上订单退款到微信'
  266. )
  267. {
  268. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  269. }
  270. }