AccountService.php 15 KB

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