AccountService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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\Services\BaseService;
  15. /**
  16. * 账户明细管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class AccountService
  20. * @package App\Services\Common
  21. */
  22. class AccountService extends BaseService
  23. {
  24. public static $instance = null;
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * AccountService constructor.
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new AccountLogModel();
  34. }
  35. /**
  36. * 静态入口
  37. * @return static|null
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = (new static());
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * @param $params
  48. * @param int $pageSize
  49. * @return array
  50. */
  51. public function getDataList($params, $pageSize = 15)
  52. {
  53. $status = $params['status'] ?? '';
  54. $type = $params['type'] ?? '';
  55. $keyword = $params['keyword'] ?? '';
  56. $orderNo = $params['order_no'] ?? '';
  57. $userId = $params['user_id'] ?? '';
  58. $date = $params['date'] ?? [];
  59. $query = AccountLogModel::from('account_logs as a')
  60. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  61. ->where('a.mark', 1);
  62. // 状态筛选
  63. if ($status !== '' && $status != 0) {
  64. $query->where('a.status', $status);
  65. }
  66. // 类型筛选
  67. if ($type !== '' && $type != 0) {
  68. $query->where('a.type', $type);
  69. }
  70. // 用户ID筛选
  71. if ($userId) {
  72. $query->where('a.user_id', $userId);
  73. }
  74. // 订单号搜索
  75. if (!empty($orderNo)) {
  76. $query->where('a.source_order_no', 'like', "%{$orderNo}%");
  77. }
  78. // 关键词搜索(用户昵称/手机号)
  79. if (!empty($keyword)) {
  80. $query->where(function ($q) use ($keyword) {
  81. $q->where('b.nickname', 'like', "%{$keyword}%")
  82. ->orWhere('b.mobile', 'like', "%{$keyword}%");
  83. });
  84. }
  85. // 日期筛选
  86. if (!empty($date) && is_array($date)) {
  87. $start = $date[0] ?? '';
  88. $end = $date[1] ?? '';
  89. if ($start) {
  90. $query->where('a.create_time', '>=', strtotime($start));
  91. }
  92. if ($end && $start < $end) {
  93. $query->where('a.create_time', '<=', strtotime($end));
  94. }
  95. }
  96. // 统计数据
  97. $model = clone $query;
  98. $counts = [
  99. 'count' => $model->count('a.id'),
  100. 'total' => $model->sum('a.money')
  101. ];
  102. // 分页查询
  103. $list = $query->select(['a.*', 'b.mobile', 'b.nickname'])
  104. ->orderBy('a.create_time', 'desc')
  105. ->orderBy('a.id', 'desc')
  106. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  107. $list = $list ? $list->toArray() : [];
  108. // 格式化数据
  109. if ($list && isset($list['data'])) {
  110. $accountTypes = [
  111. 1 => '商城消费',
  112. 2 => '充值缴费',
  113. 3 => '商城退款',
  114. 4 => '佣金提现',
  115. 5 => '提现驳回',
  116. 6 => '平台入款',
  117. 7 => '商家佣金',
  118. 8 => '代理收益',
  119. 9 => '推广收益'
  120. ];
  121. foreach ($list['data'] as &$item) {
  122. $item['create_time_text'] = datetime($item['create_time']);
  123. $item['update_time_text'] = datetime($item['update_time']);
  124. $type = isset($item['type']) ? intval($item['type']) : 0;
  125. $item['type_text'] = $accountTypes[$type] ?? '未知';
  126. $item['money'] = number_format($item['money'], 2, '.', '');
  127. $item['before_money'] = number_format($item['before_money'], 2, '.', '');
  128. $item['status_text'] = ['', '已完成', '待处理', '失败/取消'][$item['status']] ?? '未知';
  129. }
  130. }
  131. return [
  132. 'msg' => '操作成功',
  133. 'code' => 0,
  134. 'pageSize' => $pageSize,
  135. 'total' => isset($list['total']) ? $list['total'] : 0,
  136. 'counts' => $counts,
  137. 'data' => isset($list['data']) ? $list['data'] : [],
  138. 'count' => isset($list['total']) ? $list['total'] : 0
  139. ];
  140. }
  141. /**
  142. * 查询
  143. * @param $params
  144. * @return \Illuminate\Database\Eloquent\Builder
  145. */
  146. public function getQuery($params)
  147. {
  148. $where = ['a.mark' => 1];
  149. $status = isset($params['status']) ? $params['status'] : 0;
  150. $type = isset($params['type']) ? $params['type'] : 0;
  151. if ($status > 0) {
  152. $where['a.status'] = $status;
  153. }
  154. if ($type > 0) {
  155. $where['a.type'] = $type;
  156. }
  157. return $this->model->with(['member'])->from("account_logs as a")
  158. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  159. ->where($where)
  160. ->where(function ($query) use ($params) {
  161. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  162. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  163. if ($userId) {
  164. $query->where('a.user_id', $userId);
  165. }
  166. if ($keyword) {
  167. $query->where(function ($query) use ($keyword) {
  168. $query->where('b.nickname', 'like', "%{$keyword}%")
  169. ->orWhere('b.mobile', 'like', "%{$keyword}%");
  170. });
  171. }
  172. $orderNo = isset($params['order_no']) ? trim($params['order_no']) : '';
  173. if ($orderNo) {
  174. $query->where(function ($query) use ($orderNo) {
  175. $query->where('a.source_order_no', 'like', "%{$orderNo}%");
  176. });
  177. }
  178. })
  179. ->where(function ($query) use ($params) {
  180. // 日期
  181. $date = isset($params['date']) ? $params['date'] : [];
  182. $start = isset($date[0]) ? $date[0] : '';
  183. $end = isset($date[1]) ? $date[1] : '';
  184. $end = $start >= $end ? '' : $end;
  185. if ($start) {
  186. $query->where('a.create_time', '>=', strtotime($start));
  187. }
  188. if ($end) {
  189. $query->where('a.create_time', '<=', strtotime($end));
  190. }
  191. });
  192. }
  193. /**
  194. * 获取列表
  195. */
  196. public function getList()
  197. {
  198. $params = request()->all();
  199. $page = $params['page'] ?? 1;
  200. $limit = $params['limit'] ?? 20;
  201. $status = $params['status'] ?? '';
  202. $type = $params['type'] ?? '';
  203. $keyword = $params['keyword'] ?? '';
  204. $orderNo = $params['order_no'] ?? '';
  205. $userId = $params['user_id'] ?? '';
  206. $date = $params['date'] ?? [];
  207. $query = AccountLogModel::from('account_logs as a')
  208. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  209. ->where('a.mark', 1);
  210. // 状态筛选
  211. if ($status !== '' && $status != 0) {
  212. $query->where('a.status', $status);
  213. }
  214. // 类型筛选
  215. if ($type !== '' && $type != 0) {
  216. $query->where('a.type', $type);
  217. }
  218. // 用户ID筛选
  219. if ($userId) {
  220. $query->where('a.user_id', $userId);
  221. }
  222. // 订单号搜索
  223. if (!empty($orderNo)) {
  224. $query->where('a.source_order_no', 'like', "%{$orderNo}%");
  225. }
  226. // 关键词搜索(用户昵称/手机号)
  227. if (!empty($keyword)) {
  228. $query->where(function ($q) use ($keyword) {
  229. $q->where('b.nickname', 'like', "%{$keyword}%")
  230. ->orWhere('b.mobile', 'like', "%{$keyword}%");
  231. });
  232. }
  233. // 日期筛选
  234. if (!empty($date) && is_array($date)) {
  235. $start = $date[0] ?? '';
  236. $end = $date[1] ?? '';
  237. if ($start) {
  238. $query->where('a.create_time', '>=', strtotime($start));
  239. }
  240. if ($end && $start < $end) {
  241. $query->where('a.create_time', '<=', strtotime($end));
  242. }
  243. }
  244. // 统计数据
  245. $counts = [
  246. 'count' => $query->count('a.id'),
  247. 'total' => $query->sum('a.money')
  248. ];
  249. $total = $counts['count'];
  250. $list = $query->select(['a.*', 'b.mobile', 'b.nickname'])
  251. ->orderBy('a.create_time', 'desc')
  252. ->orderBy('a.id', 'desc')
  253. ->offset(($page - 1) * $limit)
  254. ->limit($limit)
  255. ->get()
  256. ->toArray();
  257. // 格式化数据
  258. $accountTypes = config('payment.accountTypes', [
  259. 1 => '商城消费',
  260. 2 => '充值缴费',
  261. 3 => '商城退款',
  262. 4 => '佣金提现',
  263. 5 => '提现驳回',
  264. 6 => '平台入款'
  265. ]);
  266. foreach ($list as &$item) {
  267. $item['create_time_text'] = date('Y-m-d H:i:s', (int)$item['create_time']);
  268. $item['type_text'] = $item['remark'] ?: ($accountTypes[$item['type']] ?? '收支明细');
  269. $item['money'] = number_format($item['money'], 2, '.', '');
  270. $item['before_money'] = number_format($item['before_money'], 2, '.', '');
  271. $item['status_text'] = ['', '已完成', '待处理', '失败/取消'][$item['status']] ?? '未知';
  272. }
  273. return [
  274. 'code' => 0,
  275. 'msg' => '获取成功',
  276. 'data' => $list,
  277. 'count' => $total,
  278. 'counts' => $counts
  279. ];
  280. }
  281. /**
  282. * 获取详情
  283. */
  284. public function getInfo($id = null)
  285. {
  286. if ($id === null) {
  287. $id = request()->input('id');
  288. }
  289. $info = AccountLogModel::from('account_logs as a')
  290. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  291. ->where('a.id', $id)
  292. ->where('a.mark', 1)
  293. ->select(['a.*', 'b.mobile', 'b.nickname'])
  294. ->first();
  295. if (!$info) {
  296. return ['code' => 1, 'msg' => '记录不存在'];
  297. }
  298. $info = $info->toArray();
  299. $info['create_time_text'] = datetime($info['create_time']);
  300. $info['update_time_text'] = datetime($info['update_time']);
  301. $accountTypes = [
  302. 1 => '商城消费',
  303. 2 => '充值缴费',
  304. 3 => '商城退款',
  305. 4 => '佣金提现',
  306. 5 => '提现驳回',
  307. 6 => '平台入款',
  308. 7 => '商家佣金',
  309. 8 => '代理收益',
  310. 9 => '推广收益'
  311. ];
  312. $type = isset($info['type']) ? intval($info['type']) : 0;
  313. $info['type_text'] = $accountTypes[$type] ?? '未知';
  314. $info['status_text'] = ['', '已完成', '待处理', '失败/取消'][$info['status']] ?? '未知';
  315. $info['money'] = number_format($info['money'], 2, '.', '');
  316. $info['before_money'] = number_format($info['before_money'], 2, '.', '');
  317. return [
  318. 'code' => 0,
  319. 'msg' => '获取成功',
  320. 'data' => $info
  321. ];
  322. }
  323. /**
  324. * 添加账户明细
  325. */
  326. public function add()
  327. {
  328. $params = request()->all();
  329. $data = [
  330. 'user_id' => (int)($params['user_id'] ?? 0),
  331. 'source_order_no' => $params['source_order_no'] ?? '',
  332. 'type' => (int)($params['type'] ?? 1),
  333. 'money' => (float)($params['money'] ?? 0),
  334. 'before_money' => (float)($params['before_money'] ?? 0),
  335. 'remark' => $params['remark'] ?? '',
  336. 'status' => (int)($params['status'] ?? 1),
  337. 'create_time' => time(),
  338. 'update_time' => time(),
  339. 'mark' => 1
  340. ];
  341. $result = AccountLogModel::insert($data);
  342. if ($result) {
  343. return ['code' => 0, 'msg' => '添加成功'];
  344. }
  345. return ['code' => 1, 'msg' => '添加失败'];
  346. }
  347. /**
  348. * 编辑账户明细
  349. */
  350. public function edit()
  351. {
  352. $params = request()->all();
  353. $id = $params['id'] ?? 0;
  354. $log = AccountLogModel::where('id', $id)
  355. ->where('mark', 1)
  356. ->first();
  357. if (!$log) {
  358. return ['code' => 1, 'msg' => '记录不存在'];
  359. }
  360. if (isset($params['user_id'])) {
  361. $log->user_id = (int)$params['user_id'];
  362. }
  363. if (isset($params['source_order_no'])) {
  364. $log->source_order_no = $params['source_order_no'];
  365. }
  366. if (isset($params['type'])) {
  367. $log->type = (int)$params['type'];
  368. }
  369. if (isset($params['money'])) {
  370. $log->money = (float)$params['money'];
  371. }
  372. if (isset($params['before_money'])) {
  373. $log->before_money = (float)$params['before_money'];
  374. }
  375. if (isset($params['remark'])) {
  376. $log->remark = $params['remark'];
  377. }
  378. if (isset($params['status'])) {
  379. $log->status = (int)$params['status'];
  380. }
  381. $log->update_time = time();
  382. $log->save();
  383. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => '修改账户明细', 'content' => json_encode($params, 256), 'module' => 'admin']);
  384. ActionLogModel::record();
  385. return ['code' => 0, 'msg' => '修改成功'];
  386. }
  387. /**
  388. * 设置状态
  389. */
  390. public function status()
  391. {
  392. $params = request()->all();
  393. $id = $params['id'] ?? 0;
  394. $status = $params['status'] ?? 1;
  395. $log = AccountLogModel::where('id', $id)
  396. ->where('mark', 1)
  397. ->first();
  398. if (!$log) {
  399. return ['code' => 1, 'msg' => '记录不存在'];
  400. }
  401. $log->status = $status;
  402. $log->update_time = time();
  403. $log->save();
  404. return ['code' => 0, 'msg' => '设置成功'];
  405. }
  406. /**
  407. * 删除
  408. * @return array
  409. */
  410. public function delete()
  411. {
  412. $id = request()->input('id');
  413. if (is_array($id)) {
  414. // 批量删除
  415. $count = AccountLogModel::whereIn('id', $id)
  416. ->where('mark', 1)
  417. ->update(['mark' => 0, 'update_time' => time()]);
  418. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "批量删除账户明细", 'content' => json_encode($id, 256), 'module' => 'admin']);
  419. ActionLogModel::record();
  420. return ['code' => 0, 'msg' => "成功删除{$count}条记录"];
  421. } else {
  422. // 单个删除
  423. $log = AccountLogModel::where('id', $id)
  424. ->where('mark', 1)
  425. ->first();
  426. if (!$log) {
  427. return ['code' => 1, 'msg' => '记录不存在'];
  428. }
  429. $log->mark = 0;
  430. $log->update_time = time();
  431. $log->save();
  432. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除账户明细", 'content' => json_encode(['id' => $id], 256), 'module' => 'admin']);
  433. ActionLogModel::record();
  434. return ['code' => 0, 'msg' => '删除成功'];
  435. }
  436. }
  437. }