BalanceLogService.php 21 KB

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