BalanceLogService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 (!MemberModel::where(['id' => $orderUserId])->update($updateData)) {
  209. DB::rollBack();
  210. $this->error = '提现审核处理失败';
  211. return false;
  212. }
  213. $log = [
  214. 'user_id' => $orderUserId,
  215. 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
  216. 'account_type' => $accountType,
  217. 'user_type' => isset($info['user_type']) ? $info['user_type'] : 1,
  218. 'type' => 5,
  219. 'money' => $money,
  220. 'after_money' => moneyFormat($balance + $money, 2),
  221. 'date' => date('Y-m-d'),
  222. 'create_time' => time(),
  223. 'remark' => '提现驳回',
  224. 'status' => 1,
  225. 'mark' => 1,
  226. ];
  227. if (!AccountLogModel::insertGetId($log)) {
  228. DB::rollBack();
  229. $this->error = '提现审核处理失败';
  230. return false;
  231. }
  232. } else if ($status == 2) {
  233. // 微信打款
  234. $result = [];
  235. if ($payStatus == 20) {
  236. $order = [
  237. 'order_no' => $orderNo,
  238. 'pay_money' => $actualMoney,
  239. 'account' => $openid,
  240. 'real_name' => $realname,
  241. 'body' => ['账户提现', '余额提现', '数字资产提现', '报单积分提现', '绿色积分提现'][$accountType],
  242. ];
  243. if (!$result = PaymentService::make()->transfer($order)) {
  244. DB::rollBack();
  245. $this->error = PaymentService::make()->getError();
  246. return false;
  247. }
  248. $batchId = isset($result['batch_id']) ? $result['batch_id'] : '';
  249. $mchId = isset($result['mch_id']) ? $result['mch_id'] : '';
  250. $packageInfo = isset($result['package_info']) ? $result['package_info'] : '';
  251. $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()];
  252. if (!$this->model->where(['id' => $id])->update($updateData)) {
  253. DB::rollBack();
  254. $this->error = '提现打款处理失败';
  255. return false;
  256. }
  257. // 数字资产底池和平台运营账户入账
  258. $poolMoney = isset($info['pool_money']) ? $info['pool_money'] : 0;
  259. $ptMoney = isset($info['pt_money']) ? $info['pt_money'] : 0;
  260. if ($accountType == 2) {
  261. $poolInfo = PtAccountModel::where(['mark' => 1])->first();
  262. $poolTotal = isset($poolInfo['pool_total']) ? $poolInfo['pool_total'] : 0;
  263. $poolBalance = isset($poolInfo['balance']) ? $poolInfo['balance'] : 0;
  264. $poolInfo->today_pool += $poolMoney;
  265. $poolInfo->pool_total += $poolMoney;
  266. $poolInfo->balance += $ptMoney;
  267. $poolInfo->update_time = time();
  268. if (!$poolInfo->save()) {
  269. DB::rollBack();
  270. $this->error = '提现审核处理失败';
  271. return false;
  272. }
  273. $log = [
  274. 'user_id' => 0,
  275. 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
  276. 'account_type' => 5,
  277. 'user_type' => 0,
  278. 'type' => 10,
  279. 'money' => $poolMoney,
  280. 'after_money' => moneyFormat($poolTotal + $poolMoney, 2),
  281. 'date' => date('Y-m-d'),
  282. 'create_time' => time(),
  283. 'remark' => '用户资产提现回流平台底池',
  284. 'status' => 1,
  285. 'mark' => 1,
  286. ];
  287. if (!AccountLogModel::insertGetId($log)) {
  288. DB::rollBack();
  289. $this->error = '提现审核处理失败';
  290. return false;
  291. }
  292. $log = [
  293. 'user_id' => 0,
  294. 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
  295. 'account_type' => 5,
  296. 'user_type' => 0,
  297. 'type' => 11,
  298. 'money' => $ptMoney,
  299. 'after_money' => moneyFormat($poolBalance + $ptMoney, 2),
  300. 'date' => date('Y-m-d'),
  301. 'create_time' => time(),
  302. 'remark' => '用户资产提现回流运营账户',
  303. 'status' => 1,
  304. 'mark' => 1,
  305. ];
  306. if (!AccountLogModel::insertGetId($log)) {
  307. DB::rollBack();
  308. $this->error = '提现审核处理失败';
  309. return false;
  310. }
  311. }
  312. }
  313. }
  314. DB::commit();
  315. $this->error = '提现审核成功';
  316. return ['id' => $id, 'money' => $money, 'status' => $status];
  317. }
  318. /**
  319. * 打款
  320. * @param $adminId
  321. * @param $params
  322. * @return array|false
  323. */
  324. public function payment($adminId, $params)
  325. {
  326. $id = isset($params['id']) ? intval($params['id']) : 0;
  327. $payStatus = isset($params['pay_status']) ? intval($params['pay_status']) : 0;
  328. $payImg = isset($params['pay_img']) ? trim($params['pay_img']) : '';
  329. $payImg = $payImg ? get_image_path($payImg) : '';
  330. $info = $this->model->with(['member'])->where(['id' => $id, 'mark' => 1])->first();
  331. $userInfo = isset($info['member']) ? $info['member'] : [];
  332. $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
  333. $realname = isset($userInfo['realname']) ? $userInfo['realname'] : '';
  334. $orderStatus = isset($info['status']) ? $info['status'] : 0;
  335. $accountType = isset($info['account_type']) ? $info['account_type'] : 0;
  336. $orderUserId = isset($info['user_id']) ? $info['user_id'] : 0;
  337. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  338. $money = isset($info['money']) ? $info['money'] : 0;
  339. $actualMoney = isset($info['actual_money']) ? $info['actual_money'] : 0;
  340. $fields = [1 => 'balance', 2 => 'property', 4 => 'ls_score'];
  341. $field = isset($fields[$accountType]) ? $fields[$accountType] : 'balance';
  342. $balance = isset($userInfo[$field]) ? $userInfo[$field] : 0;
  343. if (empty($info) || empty($userInfo) || $orderUserId <= 0 || $money <= 0) {
  344. $this->error = '提现信息不存在或参数错误';
  345. return false;
  346. }
  347. if ($orderStatus != 2) {
  348. $this->error = '提现订单状态不可操作';
  349. return false;
  350. }
  351. if ($payStatus != 20) {
  352. $this->error = '请选择已打款';
  353. return false;
  354. }
  355. DB::beginTransaction();
  356. $updateData = ['pay_status' => $payStatus, 'pay_img' => $payImg, 'update_time' => time()];
  357. if ($payStatus == 20) {
  358. $updateData['status'] = 4;
  359. }
  360. if (!$this->model->where(['id' => $id])->update($updateData)) {
  361. DB::rollBack();
  362. $this->error = '提现打款处理失败';
  363. return false;
  364. }
  365. // 线上打款逻辑
  366. $order = [
  367. 'order_no' => $orderNo,
  368. 'pay_money' => $actualMoney,
  369. 'account' => $openid,
  370. 'real_name' => $realname,
  371. 'body' => ['账户提现', '余额提现', '数字资产提现', '报单积分提现', '绿色积分提现'][$accountType],
  372. ];
  373. if (!$result = PaymentService::make()->transfer($order)) {
  374. DB::rollBack();
  375. $this->error = PaymentService::make()->getError();
  376. return false;
  377. }
  378. $batchId = isset($result['batch_id']) ? $result['batch_id'] : '';
  379. $mchId = isset($result['mch_id']) ? $result['mch_id'] : '';
  380. $packageInfo = isset($result['package_info']) ? $result['package_info'] : '';
  381. $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()];
  382. if (!$this->model->where(['id' => $id])->update($updateData)) {
  383. DB::rollBack();
  384. $this->error = '提现打款处理失败';
  385. return false;
  386. }
  387. // 数字资产底池和平台运营账户入账
  388. $poolMoney = isset($info['pool_money']) ? $info['pool_money'] : 0;
  389. $ptMoney = isset($info['pt_money']) ? $info['pt_money'] : 0;
  390. if ($accountType == 2) {
  391. $poolInfo = PtAccountModel::where(['mark' => 1])->first();
  392. $poolTotal = isset($poolInfo['pool_total']) ? $poolInfo['pool_total'] : 0;
  393. $poolBalance = isset($poolInfo['balance']) ? $poolInfo['balance'] : 0;
  394. $poolInfo->today_pool += $poolMoney;
  395. $poolInfo->pool_total += $poolMoney;
  396. $poolInfo->balance += $ptMoney;
  397. $poolInfo->update_time = time();
  398. if (!$poolInfo->save()) {
  399. DB::rollBack();
  400. $this->error = '提现审核处理失败';
  401. return false;
  402. }
  403. $log = [
  404. 'user_id' => 0,
  405. 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
  406. 'account_type' => 5,
  407. 'user_type' => 0,
  408. 'type' => 10,
  409. 'money' => $poolMoney,
  410. 'after_money' => moneyFormat($poolTotal + $poolMoney, 2),
  411. 'date' => date('Y-m-d'),
  412. 'create_time' => time(),
  413. 'remark' => '用户资产提现回流平台底池',
  414. 'status' => 1,
  415. 'mark' => 1,
  416. ];
  417. if (!AccountLogModel::insertGetId($log)) {
  418. DB::rollBack();
  419. $this->error = '提现审核处理失败';
  420. return false;
  421. }
  422. $log = [
  423. 'user_id' => 0,
  424. 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
  425. 'account_type' => 5,
  426. 'user_type' => 0,
  427. 'type' => 11,
  428. 'money' => $ptMoney,
  429. 'after_money' => moneyFormat($poolBalance + $ptMoney, 2),
  430. 'date' => date('Y-m-d'),
  431. 'create_time' => time(),
  432. 'remark' => '用户资产提现回流运营账户',
  433. 'status' => 1,
  434. 'mark' => 1,
  435. ];
  436. if (!AccountLogModel::insertGetId($log)) {
  437. DB::rollBack();
  438. $this->error = '提现审核处理失败';
  439. return false;
  440. }
  441. }
  442. DB::commit();
  443. $this->error = '在线打款处理成功';
  444. return ['id' => $id, 'money' => $money, 'status' => $payStatus];
  445. }
  446. /**
  447. * 删除
  448. * @return array
  449. */
  450. public function delete()
  451. {
  452. // 设置日志标题
  453. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除余额明细", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  454. ActionLogModel::record();
  455. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  456. return parent::delete(); // TODO: Change the autogenerated stub
  457. }
  458. /*\提现统计
  459. * @param int $type
  460. * @return array|mixed
  461. */
  462. public function getTotal($type = 1)
  463. {
  464. $cacheKey = "caches:balances:total_{$type}";
  465. $data = RedisService::get($cacheKey);
  466. if ($data) {
  467. return $data;
  468. }
  469. $data = $this->model->where(['mark' => 1])
  470. ->where(function ($query) use ($type) {
  471. if ($type == 1) {
  472. $query->where(['type' => 2])->whereIn('status', [2, 4]);
  473. } else {
  474. $query->where(['type' => 2]);
  475. }
  476. })->sum('money');
  477. if ($data) {
  478. RedisService::set($cacheKey, $data, rand(300, 600));
  479. }
  480. return $data;
  481. }
  482. }