BalanceLogService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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\Api;
  12. use App\Models\AccountLogModel;
  13. use App\Models\ActionLogModel;
  14. use App\Models\BalanceLogModel;
  15. use App\Models\GoodsModel;
  16. use App\Models\MemberBankModel;
  17. use App\Models\MemberModel;
  18. use App\Services\BaseService;
  19. use App\Services\ConfigService;
  20. use App\Services\RedisService;
  21. use Illuminate\Support\Facades\DB;
  22. /**
  23. * 余额管理-服务类
  24. * @author laravel开发员
  25. * @since 2020/11/11
  26. */
  27. class BalanceLogService extends BaseService
  28. {
  29. public static $instance = null;
  30. /**
  31. * 构造函数
  32. * @author laravel开发员
  33. * @since 2020/11/11
  34. * AccountService constructor.
  35. */
  36. public function __construct()
  37. {
  38. $this->model = new BalanceLogModel();
  39. }
  40. /**
  41. * 静态入口
  42. * @return static|null
  43. */
  44. public static function make()
  45. {
  46. if (!self::$instance) {
  47. self::$instance = (new static());
  48. }
  49. return self::$instance;
  50. }
  51. /**
  52. * @param $params
  53. * @param int $pageSize
  54. * @return array
  55. */
  56. public function getDataList($params, $pageSize = 15)
  57. {
  58. $query = $this->getQuery($params);
  59. $list = $query->select(['a.*'])
  60. ->orderBy('a.status', 'asc')
  61. ->orderBy('a.create_time', 'desc')
  62. ->orderBy('a.id', 'desc')
  63. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  64. $list = $list ? $list->toArray() : [];
  65. if ($list) {
  66. foreach ($list['data'] as &$item) {
  67. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  68. $item['time_text'] = $item['create_time'] ? datetime($item['create_time'], 'Y年m月d日') : '';
  69. }
  70. }
  71. return [
  72. 'pageSize' => $pageSize,
  73. 'total' => isset($list['total']) ? $list['total'] : 0,
  74. 'list' => isset($list['data']) ? $list['data'] : []
  75. ];
  76. }
  77. public function getQuery($params)
  78. {
  79. $where = ['a.mark' => 1];
  80. $status = isset($params['status']) ? $params['status'] : 0;
  81. $type = isset($params['type']) ? $params['type'] : 0;
  82. if ($status > 0) {
  83. $where['a.status'] = $status;
  84. }
  85. if ($type > 0) {
  86. $where['a.type'] = $type;
  87. }
  88. return $this->model->with(['member'])->from("balance_logs as a")
  89. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  90. ->where($where)
  91. ->where(function ($query) use ($params) {
  92. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  93. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  94. if ($userId) {
  95. $query->where('a.user_id', $userId);
  96. }
  97. if ($keyword) {
  98. $query->where(function ($query) use ($keyword) {
  99. $query->where('b.nickname', 'like', "%{$keyword}%")
  100. ->orWhere('b.mobile', 'like', "%{$keyword}%")
  101. ->orWhere('b.realname', 'like', "%{$keyword}%");
  102. });
  103. }
  104. $orderNo = isset($params['order_no']) ? trim($params['order_no']) : '';
  105. if ($orderNo) {
  106. $query->where(function ($query) use ($orderNo) {
  107. $query->where('a.order_no', 'like', "%{$orderNo}%");
  108. });
  109. }
  110. $account = isset($params['account']) ? trim($params['account']) : '';
  111. if ($account) {
  112. $query->where(function ($query) use ($account) {
  113. $query->where('a.account', 'like', "%{$account}%");
  114. });
  115. }
  116. })
  117. ->where(function ($query) use ($params) {
  118. // 日期
  119. $date = isset($params['date']) ? $params['date'] : [];
  120. $start = isset($date[0]) ? $date[0] : '';
  121. $end = isset($date[1]) ? $date[1] : '';
  122. $end = $start >= $end ? '' : $end;
  123. if ($start) {
  124. $query->where('a.create_time', '>=', strtotime($start));
  125. }
  126. if ($end) {
  127. $query->where('a.create_time', '<=', strtotime($end));
  128. }
  129. });
  130. }
  131. /**
  132. * 收入提现
  133. * @param $userId
  134. * @param $params
  135. * @return array|false
  136. */
  137. public function withdraw($userId, $params)
  138. {
  139. // 参数验证
  140. $payType = isset($params['pay_type']) && $params['pay_type'] ? intval($params['pay_type']) : 10;
  141. $accountType = isset($params['type']) && $params['type'] ? intval($params['type']) : 1;
  142. $money = isset($params['money']) ? floatval($params['money']) : 0;
  143. $accountId = isset($params['account_id']) ? intval($params['account_id']) : 0;
  144. if ($money <= 0) {
  145. $this->error = '请输入提现金额';
  146. return false;
  147. }
  148. if ($payType != 10 && $accountId <= 0) {
  149. $this->error = '请选择收款账户';
  150. return false;
  151. }
  152. $openWithdraw = ConfigService::make()->getConfigByCode("withdraw_{$accountType}_open", 1);
  153. if (!$openWithdraw) {
  154. $this->error = 2304;
  155. return false;
  156. }
  157. $withdrawMin = ConfigService::make()->getConfigByCode("withdraw_min_{$accountType}", 0.1);
  158. if ($withdrawMin > 0 && $money < $withdrawMin) {
  159. $this->error = lang($accountType==2?2309:2305, ['money' => $withdrawMin]);
  160. return false;
  161. }
  162. // 锁
  163. $cacheLockKey = "caches:members:withdraw:{$userId}";
  164. if (RedisService::get($cacheLockKey)) {
  165. $this->error = 1034;
  166. return false;
  167. }
  168. if($accountId){
  169. $accountInfo = MemberBankModel::where(['id' => $accountId, 'user_id' => $userId, 'mark' => 1])->first();
  170. $realname = isset($accountInfo['realname']) ? $accountInfo['realname'] : '';
  171. $account = isset($accountInfo['account']) ? $accountInfo['account'] : '';
  172. $accountName = isset($accountInfo['account_name']) ? $accountInfo['account_name'] : '';
  173. $accountRemark = isset($accountInfo['account_remark']) ? $accountInfo['account_remark'] : '';
  174. if (empty($accountInfo) || empty($realname) || empty($accountName) || empty($account)) {
  175. $this->error = '抱歉,当前收款账户错误请更换后重试';
  176. return false;
  177. }
  178. }else{
  179. $accountName = '微信支付';
  180. $account = '微信零钱';
  181. $accountRemark = '';
  182. }
  183. // 判断用户账号状态
  184. $fields = [1=>'balance',2=>'property',4=>'ls_score'];
  185. $field = isset($fields[$accountType])? $fields[$accountType]:'balance';
  186. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(10, 20));
  187. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  188. ->select(['id', 'balance','property','buy_type','withdraw_bonus','bonus_status','bd_score','ls_score', 'status'])
  189. ->first();
  190. $realname = isset($userInfo['realname']) ? $userInfo['realname'] : '';
  191. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  192. $bonusStatus = isset($userInfo['bonus_status']) ? $userInfo['bonus_status'] : 0;
  193. $withdrawBonus = isset($userInfo['withdraw_bonus']) ? $userInfo['withdraw_bonus'] : 0;
  194. $buyType = isset($userInfo['buy_type']) ? $userInfo['buy_type'] : 1;
  195. $balance = isset($userInfo[$field]) ? $userInfo[$field] : 0;
  196. if (empty($userInfo) || $status != 1) {
  197. $this->error = 2016;
  198. RedisService::clear($cacheLockKey);
  199. return false;
  200. }
  201. if($bonusStatus!=1){
  202. $buyType = max(1, $buyType-1);
  203. }
  204. if($accountType==1){
  205. // 提现额度
  206. $mealPrice = GoodsModel::where(['type'=>2,'mark'=>1])
  207. ->where('meal_type','>', $buyType)
  208. ->orderBy('id','asc')
  209. ->value('price');
  210. $withdrawQuota = intval($mealPrice/0.27 * 0.73/100)*100;
  211. if(($money+$withdrawBonus)>$withdrawQuota){
  212. $quota = moneyFormat($withdrawQuota-$withdrawBonus,2);
  213. $this->error = "抱歉您的账号剩余可提现额度为:{$quota}元";
  214. RedisService::clear($cacheLockKey);
  215. return false;
  216. }
  217. // 直推提现额度
  218. $directLimit = ConfigService::make()->getConfigByCode('withdraw_direct_limit', 0);
  219. $directLimit = $directLimit>0?$directLimit:0;
  220. if($directLimit>0){
  221. $childrenCount = MemberModel::where(['mark'=> 1])
  222. ->where('parent_id', $userId)
  223. ->count('id');
  224. if($childrenCount<=0){
  225. $this->error = "抱歉您的账号可提现额度不足,请先邀请好友";
  226. RedisService::clear($cacheLockKey);
  227. return false;
  228. }
  229. $quota = ConfigService::make()->getConfigByCode("business_jd_bonus_{$buyType}",0);
  230. $quota = $quota>0?$quota:0;
  231. if(($quota>0 && ($money+$withdrawBonus)>=$quota) && $childrenCount==1){
  232. $this->error = "抱歉您的账号可提现额度【{$quota}元】已用完";
  233. RedisService::clear($cacheLockKey);
  234. return false;
  235. }
  236. }
  237. }
  238. if ($money > $balance) {
  239. $this->error = '该账户可提现余额不足';
  240. RedisService::clear($cacheLockKey);
  241. return false;
  242. }
  243. // 计算实际到账金额
  244. $poolMoney = 0;
  245. $ptMoney = 0;
  246. $ptRate = 0;
  247. $poolRate = 0;
  248. $total = $money;
  249. $actualMoney = $money;
  250. if($accountType==2){
  251. // 提现金额
  252. $price = PriceService::make()->getTodayPrice(1);
  253. if($price<=0){
  254. $this->error = '请等候今日资产价格刷新后重试~';
  255. RedisService::clear($cacheLockKey);
  256. return false;
  257. }
  258. // 结算比例
  259. $rate = ConfigService::make()->getConfigByCode('withdraw_2_rate',0);
  260. $rate = $rate>0 && $rate<=100? $rate : 100;
  261. $total = moneyFormat($total * $price,2);
  262. $actualMoney = moneyFormat($total * $rate/100, 2);
  263. // 底池比例
  264. $poolRate = ConfigService::make()->getConfigByCode('withdraw_2_back_rate',0);
  265. $poolRate = $poolRate>0 && $poolRate<50? $poolRate : 0;
  266. $poolMoney = round($total * $poolRate/100, 2);
  267. // 运营账户比例
  268. $ptRate = ConfigService::make()->getConfigByCode('withdraw_2_pt_rate',0);
  269. $ptRate = $ptRate>0 && $ptRate<=50? $ptRate : 0;
  270. $ptMoney = round($total * $ptRate/100, 2);
  271. }
  272. // 手续费
  273. $feeRate = ConfigService::make()->getConfigByCode("withdraw_{$accountType}_fee",0);
  274. $feeRate = $feeRate>0 && $feeRate<=50? $feeRate : 0;
  275. $fee = round($actualMoney * $feeRate/100, 2);
  276. $actualMoney = moneyFormat($actualMoney - $fee, 2);
  277. $accountTypeName = ['账户','收益余额','数字资产','报单积分','绿色积分'][$accountType];
  278. $accountTypeName = $accountTypeName?$accountTypeName:'账户';
  279. // 提现处理
  280. DB::beginTransaction();
  281. $updateData = ["{$field}" => DB::raw("{$field} - {$money}"), 'update_time' => time()];
  282. if($accountType==1){
  283. $updateData['withdraw_bonus'] = DB::raw("withdraw_bonus + {$money}");
  284. }
  285. // 会员账户
  286. $userType = 1;
  287. if (!MemberModel::where(['id' => $userId])->update($updateData)) {
  288. DB::rollBack();
  289. $this->error = '提现处理失败';
  290. RedisService::clear($cacheLockKey);
  291. return false;
  292. }
  293. $orderNo = get_order_num('JW');
  294. $order = [
  295. 'user_id' => $userId,
  296. 'order_no' => $orderNo,
  297. 'money' => $money,
  298. 'total' => $total,
  299. 'after_money' => moneyFormat(max(0, $balance - $money), 2),
  300. 'actual_money' => $actualMoney,
  301. 'fee' => $fee,
  302. 'pool_money' => $poolMoney,
  303. 'pool_rate' => $poolRate,
  304. 'pt_money' => $ptMoney,
  305. 'pt_rate' => $ptRate,
  306. 'user_type' => $userType,
  307. 'type' => 2,
  308. 'account_type' => $accountType,
  309. 'pay_type' => $payType,
  310. 'realname' => $realname,
  311. 'account_name' => $accountName,
  312. 'account' => $account,
  313. 'account_remark' => $accountRemark,
  314. 'date' => date('Y-m-d'),
  315. 'create_time' => time(),
  316. 'status' => 1,
  317. 'mark' => 1
  318. ];
  319. if (!$orderId = $this->model::insertGetId($order)) {
  320. DB::rollBack();
  321. $this->error = '提现处理失败';
  322. RedisService::clear($cacheLockKey);
  323. return false;
  324. }
  325. $log = [
  326. 'user_id' => $userId,
  327. 'source_order_no' => $orderNo,
  328. 'user_type' => $userType,
  329. 'account_type' => $accountType,
  330. 'type' => 4,
  331. 'money' => -$money,
  332. 'after_money' => moneyFormat(max(0, $balance - $money), 2),
  333. 'date' => date('Y-m-d'),
  334. 'create_time' => time(),
  335. 'remark' => "提现到{$account}",
  336. 'status' => 1,
  337. 'mark' => 1,
  338. ];
  339. if (!$accountId = AccountLogModel::insertGetId($log)) {
  340. DB::rollBack();
  341. $this->error = '提现处理失败';
  342. RedisService::clear($cacheLockKey);
  343. return false;
  344. }
  345. DB::commit();
  346. // 操作日志
  347. ActionLogModel::setRecord($userId, ['type' => 2, 'title' => "{$accountTypeName}提现", 'content' => "姓名:{$realname},账号:{$accountName}/{$account}/{$accountRemark},提现{$money}元,单号:{$orderNo}", 'module' => 'balanceLog']);
  348. ActionLogModel::record();
  349. RedisService::clear($cacheLockKey);
  350. $this->error = '提现申请成功,请耐心等候审核~';
  351. return ['id' => $orderId, 'aid' => $accountId, 'money' => $money];
  352. }
  353. /**
  354. * 积分转账
  355. * @param $userId
  356. * @param $params
  357. * @return array|false
  358. */
  359. public function transfer($userId, $params)
  360. {
  361. // 参数验证
  362. $money = isset($params['money']) ? floatval($params['money']) : 0;
  363. $accountType = isset($params['type']) && $params['type']? intval($params['type']) : 3;
  364. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  365. $remark = isset($params['remark']) ? trim($params['remark']) : '';
  366. if ($money <= 0) {
  367. $this->error = '请输入转账数量';
  368. return false;
  369. }
  370. if (empty($mobile)) {
  371. $this->error = '请输入对方手机账号';
  372. return false;
  373. }
  374. $openTransfer = ConfigService::make()->getConfigByCode("transfer_{$accountType}_open", 1);
  375. if (!$openTransfer) {
  376. $this->error = '转账功能未开放';
  377. return false;
  378. }
  379. // 锁
  380. $cacheLockKey = "caches:members:transfer:{$userId}_{$accountType}";
  381. if (RedisService::get($cacheLockKey)) {
  382. $this->error = 1034;
  383. return false;
  384. }
  385. // 判断用户账号状态
  386. $fields = [2=>'property',3=>'bd_score',4=>'ls_score'];
  387. $field = isset($fields[$accountType])? $fields[$accountType]:'bd_score';
  388. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(10, 20));
  389. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  390. ->select(['id', 'balance','mobile','nickname','property','bd_score','ls_score', 'status'])
  391. ->first();
  392. $realname = isset($userInfo['realname']) ? $userInfo['realname'] : '';
  393. $nickname = isset($userInfo['nickname']) ? $userInfo['nickname'] : '';
  394. $userMobile = isset($userInfo['mobile']) &&$userInfo['mobile']? $userInfo['mobile'] : '';
  395. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  396. $balance = isset($userInfo[$field]) ? $userInfo[$field] : 0;
  397. if (empty($userInfo) || $status != 1) {
  398. $this->error = 2016;
  399. RedisService::clear($cacheLockKey);
  400. return false;
  401. }
  402. if ($money > $balance) {
  403. $this->error = '该账户积分余额不足';
  404. RedisService::clear($cacheLockKey);
  405. return false;
  406. }
  407. $total = $money;
  408. $actualMoney = $money;
  409. $accountTypeName = ['账户','收益余额','数字资产','报单积分','绿色积分'][$accountType];
  410. $accountTypeName = $accountTypeName?$accountTypeName:'账户';
  411. // 对方账户验证
  412. $transferAccount = MemberModel::where(['mobile' => $mobile, 'mark' => 1])
  413. ->select(['id', 'balance','property','bd_score','ls_score', 'status'])
  414. ->first();
  415. $transferUserId = isset($transferAccount['id']) ? $transferAccount['id'] : 0;
  416. $status = isset($transferAccount['status']) ? $transferAccount['status'] : 0;
  417. $transferBalance = isset($transferAccount[$field]) ? $transferAccount[$field] : 0;
  418. if (empty($transferAccount) || $status != 1 || $transferUserId<=0) {
  419. $this->error = '对方账户不存在或已冻结';
  420. RedisService::clear($cacheLockKey);
  421. return false;
  422. }
  423. // 转账处理
  424. DB::beginTransaction();
  425. $updateData = ["{$field}" => DB::raw("{$field} - {$money}"), 'update_time' => time()];
  426. // 扣除会员账户
  427. if (!MemberModel::where(['id' => $userId])->update($updateData)) {
  428. DB::rollBack();
  429. $this->error = '转账处理失败';
  430. RedisService::clear($cacheLockKey);
  431. return false;
  432. }
  433. $updateData = ["{$field}" => DB::raw("{$field} + {$money}"), 'update_time' => time()];
  434. if (!MemberModel::where(['mobile' => $mobile])->update($updateData)) {
  435. DB::rollBack();
  436. $this->error = '转账处理失败';
  437. RedisService::clear($cacheLockKey);
  438. return false;
  439. }
  440. $orderNo = get_order_num('TS');
  441. $order = [
  442. 'user_id' => $userId,
  443. 'order_no' => $orderNo,
  444. 'money' => $money,
  445. 'total' => $total,
  446. 'after_money' => moneyFormat(max(0, $balance - $money), 2),
  447. 'actual_money' => $actualMoney,
  448. 'user_type' => 1,
  449. 'type' => 3,
  450. 'account_type' => $accountType,
  451. 'pay_type' => 30,
  452. 'realname' => '',
  453. 'account_name' => $accountTypeName,
  454. 'account' => $mobile,
  455. 'remark' => $remark,
  456. 'date' => date('Y-m-d'),
  457. 'create_time' => time(),
  458. 'status' => 4,
  459. 'mark' => 1
  460. ];
  461. if (!$orderId = $this->model::insertGetId($order)) {
  462. DB::rollBack();
  463. $this->error = '提现处理失败';
  464. RedisService::clear($cacheLockKey);
  465. return false;
  466. }
  467. $log = [
  468. 'user_id' => $userId,
  469. 'source_order_no' => $orderNo,
  470. 'user_type' => 1,
  471. 'account_type' => $accountType,
  472. 'type' => 9,
  473. 'money' => -$money,
  474. 'after_money' => moneyFormat(max(0, $balance - $money), 2),
  475. 'date' => date('Y-m-d'),
  476. 'create_time' => time(),
  477. 'remark' => "转账到{$mobile}",
  478. 'status' => 1,
  479. 'mark' => 1,
  480. ];
  481. if (!$accountId = AccountLogModel::insertGetId($log)) {
  482. DB::rollBack();
  483. $this->error = '转账处理失败';
  484. RedisService::clear($cacheLockKey);
  485. return false;
  486. }
  487. $userMobileText = $userMobile?format_mobile($userMobile):$nickname;
  488. $log = [
  489. 'user_id' => $transferUserId,
  490. 'source_order_no' => $orderNo,
  491. 'user_type' => 1,
  492. 'account_type' => $accountType,
  493. 'type' => 9,
  494. 'money' => $money,
  495. 'after_money' => moneyFormat(max(0, $transferBalance + $money), 2),
  496. 'date' => date('Y-m-d'),
  497. 'create_time' => time(),
  498. 'remark' => "收到{$userMobileText}用户的{$accountTypeName}转账",
  499. 'status' => 1,
  500. 'mark' => 1,
  501. ];
  502. if (!AccountLogModel::insertGetId($log)) {
  503. DB::rollBack();
  504. $this->error = '转账处理失败';
  505. RedisService::clear($cacheLockKey);
  506. return false;
  507. }
  508. DB::commit();
  509. // 操作日志
  510. ActionLogModel::setRecord($userId, ['type' => 2, 'title' => "{$accountTypeName}积分转账", 'content' => "账户{$userMobileText}转至{$mobile},数量{$money},单号:{$orderNo}", 'module' => 'balanceLog']);
  511. ActionLogModel::record();
  512. RedisService::clear($cacheLockKey);
  513. $this->error = '转账成功~';
  514. return ['id' => $orderId, 'aid' => $accountId, 'money' => $money];
  515. }
  516. }