BalanceLogService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\AccountLogModel;
  13. use App\Models\ActionLogModel;
  14. use App\Models\BalanceLogModel;
  15. use App\Models\MemberModel;
  16. use App\Models\MessageModel;
  17. use App\Models\PtAccountModel;
  18. use App\Services\BaseService;
  19. use App\Services\PaymentService;
  20. use App\Services\RedisService;
  21. use Illuminate\Support\Facades\DB;
  22. /**
  23. * 余额管理-服务类
  24. * @author laravel开发员
  25. * @since 2020/11/11
  26. * @package App\Services\Common
  27. */
  28. class BalanceLogService extends BaseService
  29. {
  30. public static $instance = null;
  31. /**
  32. * 构造函数
  33. * @author laravel开发员
  34. * @since 2020/11/11
  35. * AccountService constructor.
  36. */
  37. public function __construct()
  38. {
  39. $this->model = new BalanceLogModel();
  40. }
  41. /**
  42. * 静态入口
  43. * @return static|null
  44. */
  45. public static function make()
  46. {
  47. if (!self::$instance) {
  48. self::$instance = (new static());
  49. }
  50. return self::$instance;
  51. }
  52. /**
  53. * @param $params
  54. * @param int $pageSize
  55. * @return array
  56. */
  57. public function getDataList($params, $pageSize = 15)
  58. {
  59. $query = $this->getQuery($params);
  60. $model = clone $query;
  61. $counts = [
  62. 'count' => $model->count('a.id'),
  63. 'total' => $model->sum('a.money'),
  64. ];
  65. $list = $query->select(['a.*'])
  66. ->orderBy('a.create_time', 'desc')
  67. ->orderBy('a.id', 'desc')
  68. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  69. $list = $list ? $list->toArray() : [];
  70. if ($list) {
  71. foreach ($list['data'] as &$item) {
  72. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  73. $item['pay_img'] = $item['pay_img'] ? get_image_url($item['pay_img']) : '';
  74. $item['member'] = isset($item['member']) ? $item['member'] : [];
  75. }
  76. }
  77. return [
  78. 'pageSize' => $pageSize,
  79. 'total' => isset($list['total']) ? $list['total'] : 0,
  80. 'counts' => $counts,
  81. 'list' => isset($list['data']) ? $list['data'] : []
  82. ];
  83. }
  84. /**
  85. * 查询
  86. * @param $params
  87. * @return \Illuminate\Database\Eloquent\Builder
  88. */
  89. public function getQuery($params)
  90. {
  91. $where = ['a.mark' => 1];
  92. $type = isset($params['type']) ? $params['type'] : 0;
  93. if ($type > 0) {
  94. $where['a.type'] = $type;
  95. }
  96. $accountType = isset($params['account_type']) ? $params['account_type'] : 0;
  97. if ($accountType > 0) {
  98. $where['a.account_type'] = $accountType;
  99. }
  100. return $this->model->with(['member'])->from("balance_logs as a")
  101. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  102. ->where($where)
  103. ->where(function ($query) use ($params) {
  104. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  105. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  106. if ($userId) {
  107. $query->where('a.user_id', $userId);
  108. }
  109. if ($keyword) {
  110. $query->where(function ($query) use ($keyword) {
  111. $query->where('b.nickname', 'like', "%{$keyword}%")
  112. ->orWhere('b.mobile', 'like', "%{$keyword}%");
  113. });
  114. }
  115. $orderNo = isset($params['order_no']) ? trim($params['order_no']) : '';
  116. if ($orderNo) {
  117. $query->where(function ($query) use ($orderNo) {
  118. $query->where('a.order_no', 'like', "%{$orderNo}%");
  119. });
  120. }
  121. $status = isset($params['status']) ? $params['status'] : 0;
  122. if ($status < 0) {
  123. $query->whereIn('a.status', [1, 3]);
  124. } else if ($status > 0) {
  125. $query->where('a.status', $status);
  126. }
  127. })
  128. ->where(function ($query) use ($params) {
  129. // 日期
  130. $date = isset($params['date']) ? $params['date'] : [];
  131. $start = isset($date[0]) ? $date[0] : '';
  132. $end = isset($date[1]) ? $date[1] : '';
  133. $end = $start >= $end ? '' : $end;
  134. if ($start) {
  135. $query->where('a.create_time', '>=', strtotime($start));
  136. }
  137. if ($end) {
  138. $query->where('a.create_time', '<=', strtotime($end));
  139. }
  140. });
  141. }
  142. /**
  143. * 添加或编辑
  144. * @return array
  145. * @since 2020/11/11
  146. * @author laravel开发员
  147. */
  148. public function edit()
  149. {
  150. $data = request()->all();
  151. return parent::edit($data); // TODO: Change the autogenerated stub
  152. }
  153. /**
  154. * 审核
  155. * @param $adminId
  156. * @param $params
  157. * @return array|false
  158. */
  159. public function confirm($adminId, $params)
  160. {
  161. $id = isset($params['id']) ? intval($params['id']) : 0;
  162. $status = isset($params['status']) ? intval($params['status']) : 0;
  163. $payStatus = isset($params['pay_status']) ? intval($params['pay_status']) : 0;
  164. $confirmRemark = isset($params['confirm_remark']) ? trim($params['confirm_remark']) : '';
  165. $payImg = isset($params['pay_img']) ? trim($params['pay_img']) : '';
  166. $payImg = $payImg ? get_image_path($payImg) : '';
  167. $info = $this->model->with(['member'])->where(['id' => $id, 'mark' => 1])->first();
  168. $userInfo = isset($info['member']) ? $info['member'] : [];
  169. $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
  170. $realname = isset($userInfo['realname']) ? $userInfo['realname'] : '';
  171. $orderStatus = isset($info['status']) ? $info['status'] : 0;
  172. $accountType = isset($info['account_type']) ? $info['account_type'] : 0;
  173. $payType = isset($info['pay_type']) ? $info['pay_type'] : 10;
  174. $orderUserId = isset($info['user_id']) ? $info['user_id'] : 0;
  175. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  176. $money = isset($info['money']) ? $info['money'] : 0;
  177. $actualMoney = isset($info['actual_money']) ? $info['actual_money'] : 0;
  178. $fields = [1 => 'balance', 2 => 'property', 4 => 'ls_score'];
  179. $field = isset($fields[$accountType]) ? $fields[$accountType] : 'balance';
  180. $balance = isset($userInfo[$field]) ? $userInfo[$field] : 0;
  181. if (empty($info) || empty($userInfo) || $orderUserId <= 0 || $money <= 0) {
  182. $this->error = '提现信息不存在或参数错误';
  183. return false;
  184. }
  185. if ($orderStatus != 1) {
  186. $this->error = '提现订单状态不可操作';
  187. return false;
  188. }
  189. if (!in_array($status, [2, 3])) {
  190. $this->error = '审核状态错误';
  191. return false;
  192. }
  193. if ($status == 3 && empty($confirmRemark)) {
  194. $this->error = '请填写审核驳回备注';
  195. return false;
  196. }
  197. DB::beginTransaction();
  198. $status = $payStatus == 20 && $status == 2 && $payType!=10 ? 4 : $status;
  199. $updateData = ['status' => $status, 'pay_status' => $payStatus, 'pay_img' => $payImg, 'confirm_admin_id' => $adminId, 'update_time' => time(), 'confirm_remark' => $confirmRemark];
  200. if (!$this->model->where(['id' => $id])->update($updateData)) {
  201. DB::rollBack();
  202. $this->error = '提现审核失败';
  203. return false;
  204. }
  205. // 如果驳回
  206. if ($status == 3) {
  207. $updateData = ["{$field}" => DB::raw("{$field} + {$money}"), 'update_time' => time()];
  208. if($accountType == 1){
  209. $updateData['withdraw_bonus'] = DB::raw("withdraw_bonus - {$money}");
  210. }
  211. if (!MemberModel::where(['id' => $orderUserId])->update($updateData)) {
  212. DB::rollBack();
  213. $this->error = '提现审核处理失败';
  214. return false;
  215. }
  216. $log = [
  217. 'user_id' => $orderUserId,
  218. 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
  219. 'account_type' => $accountType,
  220. 'user_type' => isset($info['user_type']) ? $info['user_type'] : 1,
  221. 'type' => 5,
  222. 'money' => $money,
  223. 'after_money' => moneyFormat($balance + $money, 2),
  224. 'date' => date('Y-m-d'),
  225. 'create_time' => time(),
  226. 'remark' => '提现驳回',
  227. 'status' => 1,
  228. 'mark' => 1,
  229. ];
  230. if (!AccountLogModel::insertGetId($log)) {
  231. DB::rollBack();
  232. $this->error = '提现审核处理失败';
  233. return false;
  234. }
  235. } else if ($status == 2) {
  236. // 微信打款
  237. $result = [];
  238. if ($payStatus == 20) {
  239. $order = [
  240. 'order_no' => $orderNo,
  241. 'pay_money' => $actualMoney,
  242. 'account' => $openid,
  243. 'real_name' => $realname,
  244. 'body' => ['账户提现', '余额提现', '数字资产提现', '报单积分提现', '绿色积分提现'][$accountType],
  245. ];
  246. if (!$result = PaymentService::make()->transfer($order)) {
  247. DB::rollBack();
  248. $this->error = PaymentService::make()->getError();
  249. return false;
  250. }
  251. $batchId = isset($result['batch_id']) ? $result['batch_id'] : '';
  252. $mchId = isset($result['mch_id']) ? $result['mch_id'] : '';
  253. $packageInfo = isset($result['package_info']) ? $result['package_info'] : '';
  254. $updateData = ['pay_status' => 20, 'pay_at' => date('Y-m-d H:i:s'), 'package_info' => $packageInfo, 'mch_id' => $mchId, 'batch_id' => $batchId, 'receive_status' => 2, 'update_time' => time()];
  255. if (!$this->model->where(['id' => $id])->update($updateData)) {
  256. DB::rollBack();
  257. $this->error = '提现打款处理失败';
  258. return false;
  259. }
  260. // 数字资产底池和平台运营账户入账
  261. $poolMoney = isset($info['pool_money']) ? $info['pool_money'] : 0;
  262. $ptMoney = isset($info['pt_money']) ? $info['pt_money'] : 0;
  263. if ($accountType == 2) {
  264. $poolInfo = PtAccountModel::where(['mark' => 1])->first();
  265. $poolTotal = isset($poolInfo['pool_total']) ? $poolInfo['pool_total'] : 0;
  266. $poolBalance = isset($poolInfo['balance']) ? $poolInfo['balance'] : 0;
  267. $poolInfo->today_pool += $poolMoney;
  268. $poolInfo->pool_total += $poolMoney;
  269. $poolInfo->balance += $ptMoney;
  270. $poolInfo->update_time = time();
  271. if (!$poolInfo->save()) {
  272. DB::rollBack();
  273. $this->error = '提现审核处理失败';
  274. return false;
  275. }
  276. $log = [
  277. 'user_id' => 0,
  278. 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
  279. 'account_type' => 5,
  280. 'user_type' => 0,
  281. 'type' => 10,
  282. 'money' => $poolMoney,
  283. 'after_money' => moneyFormat($poolTotal + $poolMoney, 2),
  284. 'date' => date('Y-m-d'),
  285. 'create_time' => time(),
  286. 'remark' => '用户资产提现回流平台底池',
  287. 'status' => 1,
  288. 'mark' => 1,
  289. ];
  290. if (!AccountLogModel::insertGetId($log)) {
  291. DB::rollBack();
  292. $this->error = '提现审核处理失败';
  293. return false;
  294. }
  295. $log = [
  296. 'user_id' => 0,
  297. 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
  298. 'account_type' => 5,
  299. 'user_type' => 0,
  300. 'type' => 11,
  301. 'money' => $ptMoney,
  302. 'after_money' => moneyFormat($poolBalance + $ptMoney, 2),
  303. 'date' => date('Y-m-d'),
  304. 'create_time' => time(),
  305. 'remark' => '用户资产提现回流运营账户',
  306. 'status' => 1,
  307. 'mark' => 1,
  308. ];
  309. if (!AccountLogModel::insertGetId($log)) {
  310. DB::rollBack();
  311. $this->error = '提现审核处理失败';
  312. return false;
  313. }
  314. }
  315. }
  316. }
  317. DB::commit();
  318. $this->error = '提现审核成功';
  319. return ['id' => $id, 'money' => $money, 'status' => $status];
  320. }
  321. /**
  322. * 打款
  323. * @param $adminId
  324. * @param $params
  325. * @return array|false
  326. */
  327. public function payment($adminId, $params)
  328. {
  329. $id = isset($params['id']) ? intval($params['id']) : 0;
  330. $payStatus = isset($params['pay_status']) ? intval($params['pay_status']) : 0;
  331. $payImg = isset($params['pay_img']) ? trim($params['pay_img']) : '';
  332. $payImg = $payImg ? get_image_path($payImg) : '';
  333. $info = $this->model->with(['member'])->where(['id' => $id, 'mark' => 1])->first();
  334. $userInfo = isset($info['member']) ? $info['member'] : [];
  335. $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
  336. $realname = isset($userInfo['realname']) ? $userInfo['realname'] : '';
  337. $orderStatus = isset($info['status']) ? $info['status'] : 0;
  338. $accountType = isset($info['account_type']) ? $info['account_type'] : 0;
  339. $orderUserId = isset($info['user_id']) ? $info['user_id'] : 0;
  340. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  341. $money = isset($info['money']) ? $info['money'] : 0;
  342. $actualMoney = isset($info['actual_money']) ? $info['actual_money'] : 0;
  343. $fields = [1 => 'balance', 2 => 'property', 4 => 'ls_score'];
  344. $field = isset($fields[$accountType]) ? $fields[$accountType] : 'balance';
  345. $balance = isset($userInfo[$field]) ? $userInfo[$field] : 0;
  346. if (empty($info) || empty($userInfo) || $orderUserId <= 0 || $money <= 0) {
  347. $this->error = '提现信息不存在或参数错误';
  348. return false;
  349. }
  350. if ($orderStatus != 2) {
  351. $this->error = '提现订单状态不可操作';
  352. return false;
  353. }
  354. if ($payStatus != 20) {
  355. $this->error = '请选择已打款';
  356. return false;
  357. }
  358. DB::beginTransaction();
  359. $updateData = ['pay_status' => $payStatus, 'pay_img' => $payImg, 'update_time' => time()];
  360. if ($payStatus == 20) {
  361. $updateData['status'] = 4;
  362. }
  363. if (!$this->model->where(['id' => $id])->update($updateData)) {
  364. DB::rollBack();
  365. $this->error = '提现打款处理失败';
  366. return false;
  367. }
  368. // 线上打款逻辑
  369. $order = [
  370. 'order_no' => $orderNo,
  371. 'pay_money' => $actualMoney,
  372. 'account' => $openid,
  373. 'real_name' => $realname,
  374. 'body' => ['账户提现', '余额提现', '数字资产提现', '报单积分提现', '绿色积分提现'][$accountType],
  375. ];
  376. if (!$result = PaymentService::make()->transfer($order)) {
  377. DB::rollBack();
  378. $this->error = PaymentService::make()->getError();
  379. return false;
  380. }
  381. $batchId = isset($result['batch_id']) ? $result['batch_id'] : '';
  382. $mchId = isset($result['mch_id']) ? $result['mch_id'] : '';
  383. $packageInfo = isset($result['package_info']) ? $result['package_info'] : '';
  384. $updateData = ['pay_status' => 20, 'pay_at' => date('Y-m-d H:i:s'), 'package_info' => $packageInfo, 'mch_id' => $mchId, 'batch_id' => $batchId, 'receive_status' => 2, 'update_time' => time()];
  385. if (!$this->model->where(['id' => $id])->update($updateData)) {
  386. DB::rollBack();
  387. $this->error = '提现打款处理失败';
  388. return false;
  389. }
  390. // 数字资产底池和平台运营账户入账
  391. $poolMoney = isset($info['pool_money']) ? $info['pool_money'] : 0;
  392. $ptMoney = isset($info['pt_money']) ? $info['pt_money'] : 0;
  393. if ($accountType == 2) {
  394. $poolInfo = PtAccountModel::where(['mark' => 1])->first();
  395. $poolTotal = isset($poolInfo['pool_total']) ? $poolInfo['pool_total'] : 0;
  396. $poolBalance = isset($poolInfo['balance']) ? $poolInfo['balance'] : 0;
  397. $poolInfo->today_pool += $poolMoney;
  398. $poolInfo->pool_total += $poolMoney;
  399. $poolInfo->balance += $ptMoney;
  400. $poolInfo->update_time = time();
  401. if (!$poolInfo->save()) {
  402. DB::rollBack();
  403. $this->error = '提现审核处理失败';
  404. return false;
  405. }
  406. $log = [
  407. 'user_id' => 0,
  408. 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
  409. 'account_type' => 5,
  410. 'user_type' => 0,
  411. 'type' => 10,
  412. 'money' => $poolMoney,
  413. 'after_money' => moneyFormat($poolTotal + $poolMoney, 2),
  414. 'date' => date('Y-m-d'),
  415. 'create_time' => time(),
  416. 'remark' => '用户资产提现回流平台底池',
  417. 'status' => 1,
  418. 'mark' => 1,
  419. ];
  420. if (!AccountLogModel::insertGetId($log)) {
  421. DB::rollBack();
  422. $this->error = '提现审核处理失败';
  423. return false;
  424. }
  425. $log = [
  426. 'user_id' => 0,
  427. 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
  428. 'account_type' => 5,
  429. 'user_type' => 0,
  430. 'type' => 11,
  431. 'money' => $ptMoney,
  432. 'after_money' => moneyFormat($poolBalance + $ptMoney, 2),
  433. 'date' => date('Y-m-d'),
  434. 'create_time' => time(),
  435. 'remark' => '用户资产提现回流运营账户',
  436. 'status' => 1,
  437. 'mark' => 1,
  438. ];
  439. if (!AccountLogModel::insertGetId($log)) {
  440. DB::rollBack();
  441. $this->error = '提现审核处理失败';
  442. return false;
  443. }
  444. }
  445. DB::commit();
  446. $this->error = '在线打款处理成功';
  447. return ['id' => $id, 'money' => $money, 'status' => $payStatus];
  448. }
  449. /**
  450. * 删除
  451. * @return array
  452. */
  453. public function delete()
  454. {
  455. // 设置日志标题
  456. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除余额明细", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  457. ActionLogModel::record();
  458. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  459. return parent::delete(); // TODO: Change the autogenerated stub
  460. }
  461. /*\提现统计
  462. * @param int $type
  463. * @return array|mixed
  464. */
  465. public function getTotal($type = 1)
  466. {
  467. $cacheKey = "caches:balances:total_{$type}";
  468. $data = RedisService::get($cacheKey);
  469. if ($data) {
  470. return $data;
  471. }
  472. $data = $this->model->where(['mark' => 1])
  473. ->where(function ($query) use ($type) {
  474. if ($type == 1) {
  475. $query->where(['type' => 2])->whereIn('status', [2, 4]);
  476. } else {
  477. $query->where(['type' => 2]);
  478. }
  479. })->sum('money');
  480. if ($data) {
  481. RedisService::set($cacheKey, $data, rand(300, 600));
  482. }
  483. return $data;
  484. }
  485. }