BalanceLogsService.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace App\Services\Common;
  3. use App\Models\BalanceLogModel;
  4. use App\Models\MemberModel;
  5. use App\Services\BaseService;
  6. use Illuminate\Support\Facades\DB;
  7. /**
  8. * 统一余额日志服务(充值/提现)
  9. * 支持会员、代理、商户的充值和提现记录
  10. */
  11. class BalanceLogsService extends BaseService
  12. {
  13. /**
  14. * 获取列表
  15. */
  16. public function getList()
  17. {
  18. // 获取参数
  19. $params = request()->all();
  20. $page = $params['page'] ?? 1;
  21. $limit = $params['limit'] ?? 20;
  22. $keyword = $params['keyword'] ?? '';
  23. $status = $params['status'] ?? '';
  24. $type = $params['type'] ?? ''; // 1-充值,2-提现
  25. $accountType = $params['account_type'] ?? ''; // 1-会员,2-代理,3-商户
  26. $startTime = $params['start_time'] ?? '';
  27. $endTime = $params['end_time'] ?? '';
  28. $query = BalanceLogModel::where('mark', 1);
  29. // 类型筛选
  30. if ($type !== '') {
  31. $query->where('type', $type);
  32. }
  33. // 账户类型筛选
  34. if ($accountType !== '') {
  35. $query->where('account_type', $accountType);
  36. }
  37. // 关键词搜索(订单号、姓名、手机号)
  38. if (!empty($keyword)) {
  39. $query->where(function($q) use ($keyword) {
  40. $q->where('order_no', 'like', "%{$keyword}%")
  41. ->orWhere('realname', 'like', "%{$keyword}%")
  42. ->orWhereHas('user', function($sq) use ($keyword) {
  43. $sq->where('realname', 'like', "%{$keyword}%")
  44. ->orWhere('nickname', 'like', "%{$keyword}%")
  45. ->orWhere('mobile', 'like', "%{$keyword}%");
  46. });
  47. });
  48. }
  49. // 状态筛选
  50. if ($status !== '') {
  51. $query->where('status', $status);
  52. }
  53. // 时间范围
  54. if (!empty($startTime)) {
  55. $query->where('create_time', '>=', strtotime($startTime));
  56. }
  57. if (!empty($endTime)) {
  58. $query->where('create_time', '<=', strtotime($endTime . ' 23:59:59'));
  59. }
  60. $total = $query->count();
  61. $list = $query->with(['user:id,realname,nickname,mobile,user_type'])
  62. ->orderBy('create_time', 'desc')
  63. ->offset(($page - 1) * $limit)
  64. ->limit($limit)
  65. ->get()
  66. ->toArray();
  67. // 格式化数据
  68. foreach ($list as &$item) {
  69. $item['money'] = number_format($item['money'], 2, '.', '');
  70. $item['actual_money'] = number_format($item['actual_money'], 2, '.', '');
  71. $item['create_time_text'] = date('Y-m-d H:i:s', $item['create_time']);
  72. $item['update_time_text'] = date('Y-m-d H:i:s', $item['update_time']);
  73. $item['status_text'] = $this->getStatusText($item['status']);
  74. $item['pay_type_text'] = $this->getPayTypeText($item['pay_type']);
  75. $item['account_type_text'] = $this->getAccountTypeText($item['account_type']);
  76. $item['type_text'] = $item['type'] == 1 ? '充值' : '提现';
  77. // 根据账户类型添加额外信息
  78. if (isset($item['user'])) {
  79. $item['user_realname'] = $item['user']['realname'] ?? '';
  80. $item['user_nickname'] = $item['user']['nickname'] ?? '';
  81. $item['user_mobile'] = $item['user']['mobile'] ?? '';
  82. $item['user_type'] = $item['user']['user_type'] ?? 0;
  83. }
  84. }
  85. return [
  86. 'code' => 0,
  87. 'msg' => '获取成功',
  88. 'data' => $list,
  89. 'count' => $total
  90. ];
  91. }
  92. /**
  93. * 获取详情
  94. */
  95. public function getInfo($id = null)
  96. {
  97. // 如果没有传入ID,从请求中获取
  98. if ($id === null) {
  99. $id = request()->input('id');
  100. }
  101. $info = BalanceLogModel::where('id', $id)
  102. ->where('mark', 1)
  103. ->with(['user:id,realname,nickname,mobile,user_type'])
  104. ->first();
  105. if (!$info) {
  106. return ['code' => 1, 'msg' => '记录不存在'];
  107. }
  108. $info = $info->toArray();
  109. $info['money'] = number_format($info['money'], 2, '.', '');
  110. $info['actual_money'] = number_format($info['actual_money'], 2, '.', '');
  111. $info['create_time_text'] = date('Y-m-d H:i:s', $info['create_time']);
  112. $info['update_time_text'] = date('Y-m-d H:i:s', $info['update_time']);
  113. $info['status_text'] = $this->getStatusText($info['status']);
  114. $info['pay_type_text'] = $this->getPayTypeText($info['pay_type']);
  115. $info['account_type_text'] = $this->getAccountTypeText($info['account_type']);
  116. $info['type_text'] = $info['type'] == 1 ? '充值' : '提现';
  117. // 根据账户类型添加额外信息
  118. if (isset($info['user'])) {
  119. $info['user_realname'] = $info['user']['realname'] ?? '';
  120. $info['user_nickname'] = $info['user']['nickname'] ?? '';
  121. $info['user_mobile'] = $info['user']['mobile'] ?? '';
  122. $info['user_type'] = $info['user']['user_type'] ?? 0;
  123. }
  124. return [
  125. 'code' => 0,
  126. 'msg' => '获取成功',
  127. 'data' => $info
  128. ];
  129. }
  130. /**
  131. * 审核(仅提现)
  132. */
  133. public function audit($id = null, $status = null, $remark = '', $payImg = '')
  134. {
  135. // 如果没有传入参数,从请求中获取
  136. if ($id === null) {
  137. $params = request()->all();
  138. $id = $params['id'] ?? null;
  139. $status = $params['status'] ?? null;
  140. $remark = $params['remark'] ?? '';
  141. $payImg = $params['pay_img'] ?? '';
  142. }
  143. DB::beginTransaction();
  144. try {
  145. $record = BalanceLogModel::where('id', $id)
  146. ->where('mark', 1)
  147. ->where('type', 2) // 必须是提现
  148. ->lockForUpdate()
  149. ->first();
  150. if (!$record) {
  151. throw new \Exception('提现记录不存在');
  152. }
  153. if ($record->status != 1) {
  154. throw new \Exception('该记录已处理');
  155. }
  156. // 更新状态
  157. $record->status = $status;
  158. $record->confirm_remark = $remark;
  159. $record->update_time = time();
  160. // 如果是通过,更新支付状态和打款凭证
  161. if ($status == 2) {
  162. $record->pay_status = 20; // 20-已支付
  163. $record->pay_at = date('Y-m-d H:i:s');
  164. if (!empty($payImg)) {
  165. $record->pay_img = $payImg;
  166. }
  167. }
  168. $record->save();
  169. // 如果是驳回,退回金额
  170. if ($status == 3) {
  171. $this->refundBalance($record);
  172. }
  173. DB::commit();
  174. return ['code' => 0, 'msg' => '操作成功'];
  175. } catch (\Exception $e) {
  176. DB::rollBack();
  177. return ['code' => 1, 'msg' => $e->getMessage()];
  178. }
  179. }
  180. /**
  181. * 退回余额
  182. */
  183. private function refundBalance($record)
  184. {
  185. // 所有用户(会员、代理、商户)都在 member 表中
  186. $user = MemberModel::find($record->user_id);
  187. if ($user) {
  188. // 退回余额
  189. $user->balance = bcadd($user->balance, $record->money, 2);
  190. $user->save();
  191. }
  192. }
  193. /**
  194. * 删除记录
  195. */
  196. public function delete()
  197. {
  198. // 获取参数
  199. $id = request()->input('id');
  200. $record = BalanceLogModel::where('id', $id)
  201. ->where('mark', 1)
  202. ->first();
  203. if (!$record) {
  204. return ['code' => 1, 'msg' => '记录不存在'];
  205. }
  206. if ($record->status == 1) {
  207. return ['code' => 1, 'msg' => '待审核的记录不能删除'];
  208. }
  209. $record->mark = 0;
  210. $record->save();
  211. return ['code' => 0, 'msg' => '删除成功'];
  212. }
  213. /**
  214. * 批量删除
  215. */
  216. public function deleteAll($ids = null)
  217. {
  218. // 如果没有传入参数,从请求中获取
  219. if ($ids === null) {
  220. $ids = request()->input('ids', []);
  221. }
  222. $count = BalanceLogModel::whereIn('id', $ids)
  223. ->where('mark', 1)
  224. ->where('status', '!=', 1)
  225. ->update(['mark' => 0]);
  226. return [
  227. 'code' => 0,
  228. 'msg' => "成功删除{$count}条记录"
  229. ];
  230. }
  231. /**
  232. * 获取状态文本
  233. */
  234. private function getStatusText($status)
  235. {
  236. $statusMap = [
  237. -1 => '已取消',
  238. 1 => '待审核',
  239. 2 => '已审核/到账',
  240. 3 => '审核失败'
  241. ];
  242. return $statusMap[$status] ?? '未知';
  243. }
  244. /**
  245. * 获取支付方式文本
  246. */
  247. private function getPayTypeText($payType)
  248. {
  249. $payTypeMap = [
  250. 10 => '微信',
  251. 20 => '支付宝',
  252. 50 => '银行卡'
  253. ];
  254. return $payTypeMap[$payType] ?? '未知';
  255. }
  256. /**
  257. * 获取账户类型文本
  258. */
  259. private function getAccountTypeText($accountType)
  260. {
  261. $accountTypeMap = [
  262. 1 => '会员',
  263. 2 => '代理',
  264. 3 => '商户'
  265. ];
  266. return $accountTypeMap[$accountType] ?? '未知';
  267. }
  268. /**
  269. * 获取代理等级文本
  270. */
  271. private function getAgentLevelText($level)
  272. {
  273. $levelMap = [
  274. 1 => '一级代理',
  275. 2 => '二级代理',
  276. 3 => '三级代理'
  277. ];
  278. return $levelMap[$level] ?? '未知';
  279. }
  280. }