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.

280 lines
8.8 KiB

  1. <?php
  2. namespace App\Service;
  3. use App\Model\FinancialRecord;
  4. use App\Model\Store;
  5. use App\Model\UserBalance;
  6. use App\Model\Users;
  7. class FinancialRecordService implements FinancialRecordServiceInterface
  8. {
  9. public function ledgerAccounts($source_id, $money, $source_type, $money_type, $desc, $comment='')
  10. {
  11. return $this->record(
  12. FinancialRecord::ACCOUNT_LEDGER,
  13. [
  14. 'user_id' => FinancialRecord::ACCOUNT_LEDGER,
  15. 'user_type' => FinancialRecord::USER_TYPE_LEDGER,
  16. 'money' => $money,
  17. 'money_type' => $money_type,
  18. 'source_id' => $source_id,
  19. 'source_type' => $source_type,
  20. 'desc' => $desc,
  21. 'comment' => $comment,
  22. 'status' => FinancialRecord::STATUS_NORMAL,
  23. ],
  24. true
  25. );
  26. }
  27. public function record($user_id, $record, $isLedger=false)
  28. {
  29. $financialRecord = new FinancialRecord();
  30. if (!$isLedger) {
  31. $mod = bcmod((string)$user_id, '5', 0);
  32. $financialRecord->suffix($mod);
  33. }
  34. return $financialRecord->fill(
  35. [
  36. 'user_id' => $user_id,
  37. 'user_type' => $record['user_type'],
  38. 'money' => $record['money'],
  39. 'money_type' => $record['money_type'],
  40. 'source_id' => $record['source_id'],
  41. 'source_type' => $record['source_type'],
  42. 'desc' => $record['desc'],
  43. 'comment' => $record['comment'],
  44. 'status' => $record['status'],
  45. ]
  46. )->save();
  47. }
  48. /**
  49. * @inheritDoc
  50. */
  51. public function communityAwardByPlatNewUser(
  52. $user_id,
  53. $source_id,
  54. $money,
  55. $user_type=FinancialRecord::USER_TYPE_CS,
  56. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  57. $money_type=FinancialRecord::MONEY_TYPE_CS_PLAT_NEW_USER,
  58. $desc='新用户奖励',
  59. $comment='社区服务点'
  60. )
  61. {
  62. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  63. // 维护社区服务点余额
  64. $balance = UserBalance::firstOrNew([
  65. 'user_type' => UserBalance::USER_TYPE_CS,
  66. 'source_id' => $user_id
  67. ]);
  68. $balance->balance = bcadd($balance->balance, $money);
  69. $balance->save();
  70. }
  71. /**
  72. * @inheritDoc
  73. */
  74. public function communityAwardByPlatNewUserFirstOLOrder(
  75. $user_id,
  76. $source_id,
  77. $money,
  78. $user_type=FinancialRecord::USER_TYPE_CS,
  79. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  80. $money_type=FinancialRecord::MONEY_TYPE_CS_FIRST_ORDER,
  81. $desc='新用户首单奖励',
  82. $comment='社区服务点'
  83. )
  84. {
  85. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  86. // 维护社区服务点余额
  87. $balance = UserBalance::firstOrNew([
  88. 'user_type' => UserBalance::USER_TYPE_CS,
  89. 'source_id' => $user_id
  90. ]);
  91. $balance->balance = bcadd($balance->balance, $money);
  92. $balance->save();
  93. }
  94. /**
  95. * @inheritDoc
  96. */
  97. public function communitySeparateAccountsByOrderComp(
  98. $user_id,
  99. $source_id,
  100. $money,
  101. $user_type=FinancialRecord::USER_TYPE_CS,
  102. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  103. $money_type=FinancialRecord::MONEY_TYPE_CS_OL_ORDER,
  104. $desc='用户订单分成',
  105. $comment='社区服务点'
  106. )
  107. {
  108. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  109. // 维护社区服务点余额
  110. $balance = UserBalance::firstOrNew([
  111. 'user_type' => UserBalance::USER_TYPE_CS,
  112. 'source_id' => $user_id
  113. ]);
  114. $balance->balance = bcadd($balance->balance, $money);
  115. $balance->save();
  116. }
  117. /**
  118. * @inheritDoc
  119. */
  120. public function storeAwardByPlatNewUserOFLOrder(
  121. $user_id,
  122. $source_id,
  123. $money,
  124. $user_type=FinancialRecord::USER_TYPE_STORE,
  125. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  126. $money_type=FinancialRecord::MONEY_TYPE_STORE_PLAT_NEW_USER,
  127. $desc='新用户下单奖励',
  128. $comment='用户当面付商户奖励'
  129. )
  130. {
  131. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  132. // 同时维护钱包
  133. $store = Store::query()->where(['user_id' => $user_id])->first();
  134. $store->award_money = bcadd($store->award_money, $money, 2);
  135. $store->save();
  136. }
  137. /**
  138. * @inheritDoc
  139. */
  140. public function storeAwardByTodayFirstOFLOrder(
  141. $user_id,
  142. $source_id,
  143. $money,
  144. $user_type=FinancialRecord::USER_TYPE_STORE,
  145. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  146. $money_type=FinancialRecord::MONEY_TYPE_STORE_FIRST_ORDER,
  147. $desc='用户店铺首单奖励',
  148. $comment='用户当面付商户奖励'
  149. )
  150. {
  151. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  152. // 同时维护钱包
  153. $store = Store::query()->where(['user_id' => $user_id])->first();
  154. $store->award_money = bcadd($store->award_money, $money, 2);
  155. $store->save();
  156. }
  157. public function recordAll($user_id, $source_id, $money, $user_type=1, $source_type=0, $money_type=0, $desc='', $comment='') {
  158. $this->record(
  159. $user_id,
  160. [
  161. 'user_id' => $user_id,
  162. 'user_type' => $user_type,
  163. 'money' => $money,
  164. 'money_type' => $money_type,
  165. 'source_id' => $source_id,
  166. 'source_type' => $source_type,
  167. 'desc' => $desc,
  168. 'comment' => $comment,
  169. 'status' => FinancialRecord::STATUS_NORMAL,
  170. ]
  171. );
  172. $this->ledgerAccounts($source_id, $money, $source_type, $money_type, $desc, $comment);
  173. }
  174. /**
  175. * @inheritDoc
  176. */
  177. public function userByOFLOrderPaid(
  178. $user_id,
  179. $source_id,
  180. $money,
  181. $user_type=FinancialRecord::USER_TYPE_USER,
  182. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  183. $money_type=FinancialRecord::MONEY_TYPE_USER_OFL_ORDER,
  184. $desc='用户下单(线下)',
  185. $comment='用户下单'
  186. )
  187. {
  188. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  189. }
  190. /**
  191. * @inheritDoc
  192. */
  193. public function userByOLOrderPaid(
  194. $user_id,
  195. $source_id,
  196. $money,
  197. $user_type=FinancialRecord::USER_TYPE_USER,
  198. $source_type=FinancialRecord::SOURCE_TYPE_ORDER,
  199. $money_type=FinancialRecord::MONEY_TYPE_USER_OL_ORDER,
  200. $desc='用户下单(线上)',
  201. $comment='用户下单'
  202. )
  203. {
  204. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  205. }
  206. /**
  207. * @inheritDoc
  208. */
  209. public function storeByOLOrderComp(
  210. $user_id,
  211. $source_id,
  212. $money,
  213. $user_type = FinancialRecord::USER_TYPE_STORE,
  214. $source_type = FinancialRecord::SOURCE_TYPE_ORDER,
  215. $money_type = FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP,
  216. $desc = '线上外卖订单收入',
  217. $comment = '用户订单完成'
  218. )
  219. {
  220. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  221. // 同时维护钱包
  222. $store = Store::query()->where(['user_id' => $user_id])->first();
  223. $store->store_wallet = bcadd($store->store_wallet, $money, 2);
  224. $store->save();
  225. }
  226. /**
  227. * @inheritDoc
  228. */
  229. public function storeByOFLOrderComp(
  230. $user_id,
  231. $source_id,
  232. $money,
  233. $user_type = FinancialRecord::USER_TYPE_STORE,
  234. $source_type = FinancialRecord::SOURCE_TYPE_ORDER,
  235. $money_type = FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP,
  236. $desc = '线下当面付订单收入',
  237. $comment = '用户订单完成'
  238. )
  239. {
  240. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  241. }
  242. /**
  243. * @inheritDoc
  244. * 订单退款(线上)
  245. */
  246. public function userByOLOrderRefund(
  247. $user_id,
  248. $source_id,
  249. $money,
  250. $user_type = FinancialRecord::USER_TYPE_USER,
  251. $source_type = FinancialRecord::SOURCE_TYPE_ORDER,
  252. $money_type = FinancialRecord::MONEY_TYPE_USER_OL_ORDER_REFUND,
  253. $desc = '线上订单退款',
  254. $comment = '线上订单退款到微信'
  255. )
  256. {
  257. $this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
  258. }
  259. }