AccountService.php 14 KB

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