AccountService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. $type = isset($item['type']) ? intval($item['type']) : 0;
  124. $item['type_text'] = $accountTypes[$type] ?? '未知';
  125. $item['money'] = number_format($item['money'], 2, '.', '');
  126. $item['before_money'] = number_format($item['before_money'], 2, '.', '');
  127. $item['status_text'] = ['', '已完成', '待处理', '失败/取消'][$item['status']] ?? '未知';
  128. }
  129. }
  130. return [
  131. 'msg' => '操作成功',
  132. 'code' => 0,
  133. 'pageSize' => $pageSize,
  134. 'total' => isset($list['total']) ? $list['total'] : 0,
  135. 'counts' => $counts,
  136. 'data' => isset($list['data']) ? $list['data'] : [],
  137. 'count' => isset($list['total']) ? $list['total'] : 0
  138. ];
  139. }
  140. /**
  141. * 查询
  142. * @param $params
  143. * @return \Illuminate\Database\Eloquent\Builder
  144. */
  145. public function getQuery($params)
  146. {
  147. $where = ['a.mark' => 1];
  148. $status = isset($params['status']) ? $params['status'] : 0;
  149. $type = isset($params['type']) ? $params['type'] : 0;
  150. if ($status > 0) {
  151. $where['a.status'] = $status;
  152. }
  153. if ($type > 0) {
  154. $where['a.type'] = $type;
  155. }
  156. return $this->model->with(['member'])->from("account_logs as a")
  157. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  158. ->where($where)
  159. ->where(function ($query) use ($params) {
  160. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  161. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  162. if ($userId) {
  163. $query->where('a.user_id', $userId);
  164. }
  165. if ($keyword) {
  166. $query->where(function ($query) use ($keyword) {
  167. $query->where('b.nickname', 'like', "%{$keyword}%")
  168. ->orWhere('b.mobile', 'like', "%{$keyword}%");
  169. });
  170. }
  171. $orderNo = isset($params['order_no']) ? trim($params['order_no']) : '';
  172. if ($orderNo) {
  173. $query->where(function ($query) use ($orderNo) {
  174. $query->where('a.source_order_no', 'like', "%{$orderNo}%");
  175. });
  176. }
  177. })
  178. ->where(function ($query) use ($params) {
  179. // 日期
  180. $date = isset($params['date']) ? $params['date'] : [];
  181. $start = isset($date[0]) ? $date[0] : '';
  182. $end = isset($date[1]) ? $date[1] : '';
  183. $end = $start >= $end ? '' : $end;
  184. if ($start) {
  185. $query->where('a.create_time', '>=', strtotime($start));
  186. }
  187. if ($end) {
  188. $query->where('a.create_time', '<=', strtotime($end));
  189. }
  190. });
  191. }
  192. /**
  193. * 获取列表
  194. */
  195. public function getList()
  196. {
  197. $params = request()->all();
  198. $page = $params['page'] ?? 1;
  199. $limit = $params['limit'] ?? 20;
  200. $status = $params['status'] ?? '';
  201. $type = $params['type'] ?? '';
  202. $keyword = $params['keyword'] ?? '';
  203. $orderNo = $params['order_no'] ?? '';
  204. $userId = $params['user_id'] ?? '';
  205. $date = $params['date'] ?? [];
  206. $query = AccountLogModel::from('account_logs as a')
  207. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  208. ->where('a.mark', 1);
  209. // 状态筛选
  210. if ($status !== '' && $status != 0) {
  211. $query->where('a.status', $status);
  212. }
  213. // 类型筛选
  214. if ($type !== '' && $type != 0) {
  215. $query->where('a.type', $type);
  216. }
  217. // 用户ID筛选
  218. if ($userId) {
  219. $query->where('a.user_id', $userId);
  220. }
  221. // 订单号搜索
  222. if (!empty($orderNo)) {
  223. $query->where('a.source_order_no', 'like', "%{$orderNo}%");
  224. }
  225. // 关键词搜索(用户昵称/手机号)
  226. if (!empty($keyword)) {
  227. $query->where(function ($q) use ($keyword) {
  228. $q->where('b.nickname', 'like', "%{$keyword}%")
  229. ->orWhere('b.mobile', 'like', "%{$keyword}%");
  230. });
  231. }
  232. // 日期筛选
  233. if (!empty($date) && is_array($date)) {
  234. $start = $date[0] ?? '';
  235. $end = $date[1] ?? '';
  236. if ($start) {
  237. $query->where('a.create_time', '>=', strtotime($start));
  238. }
  239. if ($end && $start < $end) {
  240. $query->where('a.create_time', '<=', strtotime($end));
  241. }
  242. }
  243. // 统计数据
  244. $counts = [
  245. 'count' => $query->count('a.id'),
  246. 'total' => $query->sum('a.money')
  247. ];
  248. $total = $counts['count'];
  249. $list = $query->select(['a.*', 'b.mobile', 'b.nickname'])
  250. ->orderBy('a.create_time', 'desc')
  251. ->orderBy('a.id', 'desc')
  252. ->offset(($page - 1) * $limit)
  253. ->limit($limit)
  254. ->get()
  255. ->toArray();
  256. // 格式化数据
  257. $accountTypes = config('payment.accountTypes', [
  258. 1 => '商城消费',
  259. 2 => '充值缴费',
  260. 3 => '商城退款',
  261. 4 => '佣金提现',
  262. 5 => '提现驳回',
  263. 6 => '平台入款'
  264. ]);
  265. foreach ($list as &$item) {
  266. $item['create_time_text'] = date('Y-m-d H:i:s', (int)$item['create_time']);
  267. $item['type_text'] = $item['remark'] ?: ($accountTypes[$item['type']] ?? '收支明细');
  268. $item['money'] = number_format($item['money'], 2, '.', '');
  269. $item['before_money'] = number_format($item['before_money'], 2, '.', '');
  270. $item['status_text'] = ['', '已完成', '待处理', '失败/取消'][$item['status']] ?? '未知';
  271. }
  272. return [
  273. 'code' => 0,
  274. 'msg' => '获取成功',
  275. 'data' => $list,
  276. 'count' => $total,
  277. 'counts' => $counts
  278. ];
  279. }
  280. /**
  281. * 获取详情
  282. */
  283. public function getInfo($id = null)
  284. {
  285. if ($id === null) {
  286. $id = request()->input('id');
  287. }
  288. $info = AccountLogModel::from('account_logs as a')
  289. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  290. ->where('a.id', $id)
  291. ->where('a.mark', 1)
  292. ->select(['a.*', 'b.mobile', 'b.nickname'])
  293. ->first();
  294. if (!$info) {
  295. return ['code' => 1, 'msg' => '记录不存在'];
  296. }
  297. $info = $info->toArray();
  298. $info['create_time_text'] = datetime($info['create_time']);
  299. $info['update_time_text'] = datetime($info['update_time']);
  300. $accountTypes = [
  301. 1 => '商城消费',
  302. 2 => '充值缴费',
  303. 3 => '商城退款',
  304. 4 => '佣金提现',
  305. 5 => '提现驳回',
  306. 6 => '平台入款',
  307. 7 => '商家佣金',
  308. 8 => '代理收益',
  309. 9 => '推广收益'
  310. ];
  311. $type = isset($info['type']) ? intval($info['type']) : 0;
  312. $info['type_text'] = $accountTypes[$type] ?? '未知';
  313. $info['status_text'] = ['', '已完成', '待处理', '失败/取消'][$info['status']] ?? '未知';
  314. $info['money'] = number_format($info['money'], 2, '.', '');
  315. $info['before_money'] = number_format($info['before_money'], 2, '.', '');
  316. return [
  317. 'code' => 0,
  318. 'msg' => '获取成功',
  319. 'data' => $info
  320. ];
  321. }
  322. /**
  323. * 添加账户明细
  324. */
  325. public function add()
  326. {
  327. $params = request()->all();
  328. $data = [
  329. 'user_id' => (int)($params['user_id'] ?? 0),
  330. 'source_order_no' => $params['source_order_no'] ?? '',
  331. 'type' => (int)($params['type'] ?? 1),
  332. 'money' => (float)($params['money'] ?? 0),
  333. 'before_money' => (float)($params['before_money'] ?? 0),
  334. 'remark' => $params['remark'] ?? '',
  335. 'status' => (int)($params['status'] ?? 1),
  336. 'create_time' => time(),
  337. 'update_time' => time(),
  338. 'mark' => 1
  339. ];
  340. $result = AccountLogModel::insert($data);
  341. if ($result) {
  342. return ['code' => 0, 'msg' => '添加成功'];
  343. }
  344. return ['code' => 1, 'msg' => '添加失败'];
  345. }
  346. /**
  347. * 编辑账户明细
  348. */
  349. public function edit()
  350. {
  351. $params = request()->all();
  352. $id = $params['id'] ?? 0;
  353. $log = AccountLogModel::where('id', $id)
  354. ->where('mark', 1)
  355. ->first();
  356. if (!$log) {
  357. return ['code' => 1, 'msg' => '记录不存在'];
  358. }
  359. if (isset($params['user_id'])) {
  360. $log->user_id = (int)$params['user_id'];
  361. }
  362. if (isset($params['source_order_no'])) {
  363. $log->source_order_no = $params['source_order_no'];
  364. }
  365. if (isset($params['type'])) {
  366. $log->type = (int)$params['type'];
  367. }
  368. if (isset($params['money'])) {
  369. $log->money = (float)$params['money'];
  370. }
  371. if (isset($params['before_money'])) {
  372. $log->before_money = (float)$params['before_money'];
  373. }
  374. if (isset($params['remark'])) {
  375. $log->remark = $params['remark'];
  376. }
  377. if (isset($params['status'])) {
  378. $log->status = (int)$params['status'];
  379. }
  380. $log->update_time = time();
  381. $log->save();
  382. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => '修改账户明细', 'content' => json_encode($params, 256), 'module' => 'admin']);
  383. ActionLogModel::record();
  384. return ['code' => 0, 'msg' => '修改成功'];
  385. }
  386. /**
  387. * 设置状态
  388. */
  389. public function status()
  390. {
  391. $params = request()->all();
  392. $id = $params['id'] ?? 0;
  393. $status = $params['status'] ?? 1;
  394. $log = AccountLogModel::where('id', $id)
  395. ->where('mark', 1)
  396. ->first();
  397. if (!$log) {
  398. return ['code' => 1, 'msg' => '记录不存在'];
  399. }
  400. $log->status = $status;
  401. $log->update_time = time();
  402. $log->save();
  403. return ['code' => 0, 'msg' => '设置成功'];
  404. }
  405. /**
  406. * 删除
  407. * @return array
  408. */
  409. public function delete()
  410. {
  411. $id = request()->input('id');
  412. if (is_array($id)) {
  413. // 批量删除
  414. $count = AccountLogModel::whereIn('id', $id)
  415. ->where('mark', 1)
  416. ->update(['mark' => 0, 'update_time' => time()]);
  417. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "批量删除账户明细", 'content' => json_encode($id, 256), 'module' => 'admin']);
  418. ActionLogModel::record();
  419. return ['code' => 0, 'msg' => "成功删除{$count}条记录"];
  420. } else {
  421. // 单个删除
  422. $log = AccountLogModel::where('id', $id)
  423. ->where('mark', 1)
  424. ->first();
  425. if (!$log) {
  426. return ['code' => 1, 'msg' => '记录不存在'];
  427. }
  428. $log->mark = 0;
  429. $log->update_time = time();
  430. $log->save();
  431. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除账户明细", 'content' => json_encode(['id' => $id], 256), 'module' => 'admin']);
  432. ActionLogModel::record();
  433. return ['code' => 0, 'msg' => '删除成功'];
  434. }
  435. }
  436. }