BalanceLogService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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\Services\BaseService;
  17. use App\Services\PaymentService;
  18. use App\Services\RedisService;
  19. use Illuminate\Support\Facades\DB;
  20. /**
  21. * 余额管理-服务类
  22. * @author laravel开发员
  23. * @since 2020/11/11
  24. * @package App\Services\Common
  25. */
  26. class BalanceLogService extends BaseService
  27. {
  28. public static $instance = null;
  29. /**
  30. * 构造函数
  31. * @author laravel开发员
  32. * @since 2020/11/11
  33. * AccountService constructor.
  34. */
  35. public function __construct()
  36. {
  37. $this->model = new BalanceLogModel();
  38. }
  39. /**
  40. * 静态入口
  41. * @return static|null
  42. */
  43. public static function make()
  44. {
  45. if (!self::$instance) {
  46. self::$instance = (new static());
  47. }
  48. return self::$instance;
  49. }
  50. /**
  51. * @param $params
  52. * @param int $pageSize
  53. * @return array
  54. */
  55. public function getDataList($params, $pageSize = 15)
  56. {
  57. $query = $this->getQuery($params);
  58. $model = clone $query;
  59. $counts = [
  60. 'count' => $model->count('a.id'),
  61. 'total' => $model->sum('a.money'),
  62. ];
  63. $list = $query->select(['a.*'])
  64. ->orderBy('a.create_time', 'desc')
  65. ->orderBy('a.id', 'desc')
  66. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  67. $list = $list ? $list->toArray() : [];
  68. if ($list) {
  69. foreach ($list['data'] as &$item) {
  70. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  71. $item['pay_img'] = $item['pay_img'] ? get_image_url($item['pay_img']) : '';
  72. $item['member'] = isset($item['member']) ? $item['member'] : [];
  73. }
  74. }
  75. return [
  76. 'pageSize' => $pageSize,
  77. 'total' => isset($list['total']) ? $list['total'] : 0,
  78. 'counts' => $counts,
  79. 'list' => isset($list['data']) ? $list['data'] : []
  80. ];
  81. }
  82. /**
  83. * 查询
  84. * @param $params
  85. * @return \Illuminate\Database\Eloquent\Builder
  86. */
  87. public function getQuery($params)
  88. {
  89. $where = ['a.mark' => 1];
  90. $type = isset($params['type']) ? $params['type'] : 0;
  91. if ($type > 0) {
  92. $where['a.type'] = $type;
  93. }
  94. $accountType = isset($params['account_type']) ? $params['account_type'] : 0;
  95. if ($accountType > 0) {
  96. $where['a.account_type'] = $accountType;
  97. }
  98. return $this->model->with(['member'])->from("balance_logs as a")
  99. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  100. ->where($where)
  101. ->where(function ($query) use ($params) {
  102. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  103. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  104. if ($userId) {
  105. $query->where('a.user_id', $userId);
  106. }
  107. if ($keyword) {
  108. $query->where(function ($query) use ($keyword) {
  109. $query->where('b.nickname', 'like', "%{$keyword}%")
  110. ->orWhere('b.mobile', 'like', "%{$keyword}%");
  111. });
  112. }
  113. $orderNo = isset($params['order_no']) ? trim($params['order_no']) : '';
  114. if ($orderNo) {
  115. $query->where(function ($query) use ($orderNo) {
  116. $query->where('a.order_no', 'like', "%{$orderNo}%");
  117. });
  118. }
  119. $status = isset($params['status']) ? $params['status'] : 0;
  120. if ($status < 0) {
  121. $query->whereIn('a.status', [1, 3]);
  122. } else if ($status > 0) {
  123. $query->where('a.status', $status);
  124. }
  125. })
  126. ->where(function ($query) use ($params) {
  127. // 日期
  128. $date = isset($params['date']) ? $params['date'] : [];
  129. $start = isset($date[0]) ? $date[0] : '';
  130. $end = isset($date[1]) ? $date[1] : '';
  131. $end = $start >= $end ? '' : $end;
  132. if ($start) {
  133. $query->where('a.create_time', '>=', strtotime($start));
  134. }
  135. if ($end) {
  136. $query->where('a.create_time', '<=', strtotime($end));
  137. }
  138. });
  139. }
  140. /**
  141. * 添加或编辑
  142. * @return array
  143. * @since 2020/11/11
  144. * @author laravel开发员
  145. */
  146. public function edit()
  147. {
  148. $data = request()->all();
  149. return parent::edit($data); // TODO: Change the autogenerated stub
  150. }
  151. /**
  152. * 审核
  153. * @param $adminId
  154. * @param $params
  155. * @return array|false
  156. */
  157. public function confirm($adminId, $params)
  158. {
  159. $id = isset($params['id']) ? intval($params['id']) : 0;
  160. $status = isset($params['status']) ? intval($params['status']) : 0;
  161. $payStatus = isset($params['pay_status']) ? intval($params['pay_status']) : 0;
  162. $confirmRemark = isset($params['confirm_remark']) ? trim($params['confirm_remark']) : '';
  163. $payImg = isset($params['pay_img']) ? trim($params['pay_img']) : '';
  164. $payImg = $payImg ? get_image_path($payImg) : '';
  165. $info = $this->model->with(['member'])->where(['id' => $id, 'mark' => 1])->first();
  166. $userInfo = isset($info['member']) ? $info['member'] : [];
  167. $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
  168. $realname = isset($userInfo['realname']) ? $userInfo['realname'] : '';
  169. $orderStatus = isset($info['status']) ? $info['status'] : 0;
  170. $accountType = isset($info['account_type']) ? $info['account_type'] : 0;
  171. $payType = isset($info['pay_type']) ? $info['pay_type'] : 10;
  172. $orderUserId = isset($info['user_id']) ? $info['user_id'] : 0;
  173. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  174. $money = isset($info['money']) ? $info['money'] : 0;
  175. $actualMoney = isset($info['actual_money']) ? $info['actual_money'] : 0;
  176. $fields = [1 => 'balance', 2 => 'property', 4 => 'ls_score'];
  177. $field = isset($fields[$accountType]) ? $fields[$accountType] : 'balance';
  178. $balance = isset($userInfo[$field]) ? $userInfo[$field] : 0;
  179. if (empty($info) || empty($userInfo) || $orderUserId <= 0 || $money <= 0) {
  180. $this->error = '提现信息不存在或参数错误';
  181. return false;
  182. }
  183. if ($orderStatus != 1) {
  184. $this->error = '提现订单状态不可操作';
  185. return false;
  186. }
  187. if (!in_array($status, [2, 3])) {
  188. $this->error = '审核状态错误';
  189. return false;
  190. }
  191. if ($status == 3 && empty($confirmRemark)) {
  192. $this->error = '请填写审核驳回备注';
  193. return false;
  194. }
  195. DB::beginTransaction();
  196. $status = $payStatus == 20 && $status == 2 && $payType!=10 ? 4 : $status;
  197. $updateData = ['status' => $status, 'pay_status' => $payStatus, 'pay_img' => $payImg, 'confirm_admin_id' => $adminId, 'update_time' => time(), 'confirm_remark' => $confirmRemark];
  198. if (!$this->model->where(['id' => $id])->update($updateData)) {
  199. DB::rollBack();
  200. $this->error = '提现审核失败';
  201. return false;
  202. }
  203. // 如果驳回
  204. if ($status == 3) {
  205. $updateData = ["{$field}" => DB::raw("{$field} + {$money}"), 'update_time' => time()];
  206. if (!MemberModel::where(['id' => $orderUserId])->update($updateData)) {
  207. DB::rollBack();
  208. $this->error = '提现审核处理失败';
  209. return false;
  210. }
  211. $log = [
  212. 'user_id' => $orderUserId,
  213. 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
  214. 'user_type' => isset($info['user_type']) ? $info['user_type'] : 1,
  215. 'type' => 5,
  216. 'money' => $money,
  217. 'after_money' => moneyFormat($balance + $money, 2),
  218. 'date' => date('Y-m-d'),
  219. 'create_time' => time(),
  220. 'remark' => '提现驳回',
  221. 'status' => 1,
  222. 'mark' => 1,
  223. ];
  224. if (!AccountLogModel::insertGetId($log)) {
  225. DB::rollBack();
  226. $this->error = '提现审核处理失败';
  227. return false;
  228. }
  229. } else if ($status == 2) {
  230. // 微信打款
  231. $result = [];
  232. if ($payStatus == 20) {
  233. $order = [
  234. 'order_no' => $orderNo,
  235. 'pay_money' => $actualMoney,
  236. 'account' => $openid,
  237. 'real_name' => $realname,
  238. 'body' => '余额提现',
  239. ];
  240. if (!$result = PaymentService::make()->transfer($order)) {
  241. DB::rollBack();
  242. $this->error = PaymentService::make()->getError();
  243. return false;
  244. }
  245. $batchId = isset($result['batch_id']) ? $result['batch_id'] : '';
  246. $mchId = isset($result['mch_id']) ? $result['mch_id'] : '';
  247. $packageInfo = isset($result['package_info']) ? $result['package_info'] : '';
  248. $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()];
  249. if (!$this->model->where(['id' => $id])->update($updateData)) {
  250. DB::rollBack();
  251. $this->error = '提现打款处理失败';
  252. return false;
  253. }
  254. }
  255. }
  256. DB::commit();
  257. $this->error = '提现审核成功';
  258. return ['id' => $id, 'money' => $money, 'status' => $status];
  259. }
  260. /**
  261. * 打款
  262. * @param $adminId
  263. * @param $params
  264. * @return array|false
  265. */
  266. public function payment($adminId, $params)
  267. {
  268. $id = isset($params['id']) ? intval($params['id']) : 0;
  269. $payStatus = isset($params['pay_status']) ? intval($params['pay_status']) : 0;
  270. $payImg = isset($params['pay_img']) ? trim($params['pay_img']) : '';
  271. $payImg = $payImg ? get_image_path($payImg) : '';
  272. $info = $this->model->with(['member'])->where(['id' => $id, 'mark' => 1])->first();
  273. $userInfo = isset($info['member']) ? $info['member'] : [];
  274. $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
  275. $realname = isset($userInfo['realname']) ? $userInfo['realname'] : '';
  276. $orderStatus = isset($info['status']) ? $info['status'] : 0;
  277. $accountType = isset($info['account_type']) ? $info['account_type'] : 0;
  278. $orderUserId = isset($info['user_id']) ? $info['user_id'] : 0;
  279. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  280. $money = isset($info['money']) ? $info['money'] : 0;
  281. $actualMoney = isset($info['actual_money']) ? $info['actual_money'] : 0;
  282. $fields = [1 => 'balance', 2 => 'property', 4 => 'ls_score'];
  283. $field = isset($fields[$accountType]) ? $fields[$accountType] : 'balance';
  284. if (empty($info) || empty($userInfo) || $orderUserId <= 0 || $money <= 0) {
  285. $this->error = '提现信息不存在或参数错误';
  286. return false;
  287. }
  288. if ($orderStatus != 2) {
  289. $this->error = '提现订单状态不可操作';
  290. return false;
  291. }
  292. if ($payStatus != 20) {
  293. $this->error = '请选择已打款';
  294. return false;
  295. }
  296. DB::beginTransaction();
  297. $updateData = ['pay_status' => $payStatus, 'pay_img' => $payImg, 'update_time' => time()];
  298. if ($payStatus == 20) {
  299. $updateData['status'] = 4;
  300. }
  301. if (!$this->model->where(['id' => $id])->update($updateData)) {
  302. DB::rollBack();
  303. $this->error = '提现打款处理失败';
  304. return false;
  305. }
  306. // 线上打款逻辑
  307. $order = [
  308. 'order_no' => $orderNo,
  309. 'pay_money' => $actualMoney,
  310. 'account' => $openid,
  311. 'real_name' => $realname,
  312. 'body' => '余额提现',
  313. ];
  314. if (!$result = PaymentService::make()->transfer($order)) {
  315. DB::rollBack();
  316. $this->error = PaymentService::make()->getError();
  317. return false;
  318. }
  319. $batchId = isset($result['batch_id']) ? $result['batch_id'] : '';
  320. $mchId = isset($result['mch_id']) ? $result['mch_id'] : '';
  321. $packageInfo = isset($result['package_info']) ? $result['package_info'] : '';
  322. $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()];
  323. if (!$this->model->where(['id' => $id])->update($updateData)) {
  324. DB::rollBack();
  325. $this->error = '提现打款处理失败';
  326. return false;
  327. }
  328. DB::commit();
  329. $this->error = '在线打款处理成功';
  330. return ['id' => $id, 'money' => $money, 'status' => $payStatus];
  331. }
  332. /**
  333. * 删除
  334. * @return array
  335. */
  336. public function delete()
  337. {
  338. // 设置日志标题
  339. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除余额明细", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  340. ActionLogModel::record();
  341. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  342. return parent::delete(); // TODO: Change the autogenerated stub
  343. }
  344. /*\提现统计
  345. * @param int $type
  346. * @return array|mixed
  347. */
  348. public function getTotal($type = 1)
  349. {
  350. $cacheKey = "caches:balances:total_{$type}";
  351. $data = RedisService::get($cacheKey);
  352. if ($data) {
  353. return $data;
  354. }
  355. $data = $this->model->where(['mark' => 1])
  356. ->where(function ($query) use ($type) {
  357. if ($type == 1) {
  358. $query->where(['type' => 2])->whereIn('status', [2, 4]);
  359. } else {
  360. $query->where(['type' => 2]);
  361. }
  362. })->sum('money');
  363. if ($data) {
  364. RedisService::set($cacheKey, $data, rand(300, 600));
  365. }
  366. return $data;
  367. }
  368. }