AccountService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. use Illuminate\Support\Facades\DB;
  16. /**
  17. * 账户明细管理-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * Class AccountService
  21. * @package App\Services\Common
  22. */
  23. class AccountService extends BaseService
  24. {
  25. public static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * AccountService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new AccountLogModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * @param $params
  49. * @param int $pageSize
  50. * @return array
  51. */
  52. public function getDataList($params, $pageSize = 15)
  53. {
  54. $status = $params['status'] ?? '';
  55. $type = $params['type'] ?? '';
  56. $keyword = $params['keyword'] ?? '';
  57. $orderNo = $params['order_no'] ?? '';
  58. $userId = $params['user_id'] ?? '';
  59. $storeId = $params['store_id'] ?? '';
  60. $date = $params['date'] ?? [];
  61. $query = AccountLogModel::from('account_logs as a')
  62. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  63. ->where('a.mark', 1);
  64. // 状态筛选
  65. if ($status !== '' && $status != 0) {
  66. $query->where('a.status', $status);
  67. }
  68. // 类型筛选
  69. if ($type !== '' && $type != 0) {
  70. $query->where('a.type', $type);
  71. }
  72. // 用户ID筛选
  73. if ($userId) {
  74. $query->where('a.user_id', $userId);
  75. }
  76. // 企业筛选
  77. if ($storeId) {
  78. $query->where('a.store_id', $storeId);
  79. }
  80. // 订单号搜索
  81. if (!empty($orderNo)) {
  82. $query->where('a.source_order_no', 'like', "%{$orderNo}%");
  83. }
  84. // 关键词搜索(用户昵称/手机号)
  85. if (!empty($keyword)) {
  86. $query->where(function ($q) use ($keyword) {
  87. $q->where('b.nickname', 'like', "%{$keyword}%")
  88. ->orWhere('b.mobile', 'like', "%{$keyword}%");
  89. });
  90. }
  91. // 日期筛选
  92. if (!empty($date) && is_array($date)) {
  93. $start = $date[0] ?? '';
  94. $end = $date[1] ?? '';
  95. if ($start) {
  96. $query->where('a.create_time', '>=', strtotime($start));
  97. }
  98. if ($end && $start < $end) {
  99. $query->where('a.create_time', '<=', strtotime($end));
  100. }
  101. }
  102. // 统计数据
  103. $model = clone $query;
  104. $counts = [
  105. 'count' => $model->count('a.id'),
  106. 'total' => $model->sum(DB::raw('abs(money)'))
  107. ];
  108. // 分页查询
  109. $list = $query->select(['a.*', 'b.mobile', 'b.nickname'])
  110. ->orderBy('a.create_time', 'desc')
  111. ->orderBy('a.id', 'desc')
  112. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  113. $list = $list ? $list->toArray() : [];
  114. // 格式化数据
  115. if ($list && isset($list['data'])) {
  116. foreach ($list['data'] as &$item) {
  117. $item['create_time_text'] = date('Y-m-d H:i:s', (int)$item['create_time']);
  118. $item['update_time_text'] = date('Y-m-d H:i:s', (int)$item['update_time']);
  119. }
  120. }
  121. return [
  122. 'msg' => '操作成功',
  123. 'code' => 0,
  124. 'pageSize' => $pageSize,
  125. 'total' => isset($list['total']) ? $list['total'] : 0,
  126. 'counts' => $counts,
  127. 'data' => isset($list['data']) ? $list['data'] : [],
  128. 'count' => isset($list['total']) ? $list['total'] : 0
  129. ];
  130. }
  131. /**
  132. * 查询
  133. * @param $params
  134. * @return \Illuminate\Database\Eloquent\Builder
  135. */
  136. public function getQuery($params)
  137. {
  138. $where = ['a.mark' => 1];
  139. $status = isset($params['status']) ? $params['status'] : 0;
  140. $type = isset($params['type']) ? $params['type'] : 0;
  141. if ($status > 0) {
  142. $where['a.status'] = $status;
  143. }
  144. if ($type > 0) {
  145. $where['a.type'] = $type;
  146. }
  147. return $this->model->with(['member'])->from("account_logs as a")
  148. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  149. ->where($where)
  150. ->where(function ($query) use ($params) {
  151. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  152. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  153. if ($userId) {
  154. $query->where('a.user_id', $userId);
  155. }
  156. $storeId = isset($params['store_id']) ? $params['store_id'] : 0;
  157. if ($storeId) {
  158. $query->where('a.store_id', $storeId);
  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. $storeId = isset($params['store_id']) ? $params['store_id'] : 0;
  217. if ($storeId) {
  218. $query->where('a.store_id', $storeId);
  219. }
  220. // 订单号搜索
  221. if (!empty($orderNo)) {
  222. $query->where('a.source_order_no', 'like', "%{$orderNo}%");
  223. }
  224. // 关键词搜索(用户昵称/手机号)
  225. if (!empty($keyword)) {
  226. $query->where(function ($q) use ($keyword) {
  227. $q->where('b.nickname', 'like', "%{$keyword}%")
  228. ->orWhere('b.mobile', 'like', "%{$keyword}%");
  229. });
  230. }
  231. // 日期筛选
  232. if (!empty($date) && is_array($date)) {
  233. $start = $date[0] ?? '';
  234. $end = $date[1] ?? '';
  235. if ($start) {
  236. $query->where('a.create_time', '>=', strtotime($start));
  237. }
  238. if ($end && $start < $end) {
  239. $query->where('a.create_time', '<=', strtotime($end));
  240. }
  241. }
  242. // 统计数据
  243. $counts = [
  244. 'count' => $query->count('a.id'),
  245. 'total' => $query->sum('a.money')
  246. ];
  247. $total = $counts['count'];
  248. $list = $query->select(['a.*', 'b.mobile', 'b.nickname'])
  249. ->orderBy('a.create_time', 'desc')
  250. ->orderBy('a.id', 'desc')
  251. ->offset(($page - 1) * $limit)
  252. ->limit($limit)
  253. ->get()
  254. ->toArray();
  255. // 格式化数据
  256. foreach ($list as &$item) {
  257. $item['create_time_text'] = date('Y-m-d H:i:s', (int)$item['create_time']);
  258. }
  259. return [
  260. 'code' => 0,
  261. 'msg' => '获取成功',
  262. 'data' => $list,
  263. 'count' => $total,
  264. 'counts' => $counts
  265. ];
  266. }
  267. /**
  268. * 获取详情
  269. */
  270. public function getInfo($id = null)
  271. {
  272. if ($id === null) {
  273. $id = request()->input('id');
  274. }
  275. $info = AccountLogModel::from('account_logs as a')
  276. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  277. ->where('a.id', $id)
  278. ->where('a.mark', 1)
  279. ->select(['a.*', 'b.mobile', 'b.nickname'])
  280. ->first();
  281. if (!$info) {
  282. return ['code' => 1, 'msg' => '记录不存在'];
  283. }
  284. $info = $info->toArray();
  285. $info['create_time_text'] = date('Y-m-d H:i:s', (int)$info['create_time']);
  286. $info['update_time_text'] = date('Y-m-d H:i:s', (int)$info['update_time']);
  287. return [
  288. 'code' => 0,
  289. 'msg' => '获取成功',
  290. 'data' => $info
  291. ];
  292. }
  293. /**
  294. * 添加账户明细
  295. */
  296. public function add()
  297. {
  298. $params = request()->all();
  299. $data = [
  300. 'user_id' => (int)($params['user_id'] ?? 0),
  301. 'source_order_no' => $params['source_order_no'] ?? '',
  302. 'type' => (int)($params['type'] ?? 1),
  303. 'money' => (float)($params['money'] ?? 0),
  304. 'after_money' => (float)($params['after_money'] ?? 0),
  305. 'remark' => $params['remark'] ?? '',
  306. 'status' => (int)($params['status'] ?? 1),
  307. 'create_time' => time(),
  308. 'update_time' => time(),
  309. 'mark' => 1
  310. ];
  311. $result = AccountLogModel::insert($data);
  312. if ($result) {
  313. return ['code' => 0, 'msg' => '添加成功'];
  314. }
  315. return ['code' => 1, 'msg' => '添加失败'];
  316. }
  317. /**
  318. * 编辑账户明细
  319. */
  320. public function edit()
  321. {
  322. $params = request()->all();
  323. $id = $params['id'] ?? 0;
  324. $log = AccountLogModel::where('id', $id)
  325. ->where('mark', 1)
  326. ->first();
  327. if (!$log) {
  328. return ['code' => 1, 'msg' => '记录不存在'];
  329. }
  330. if (isset($params['user_id'])) {
  331. $log->user_id = (int)$params['user_id'];
  332. }
  333. if (isset($params['source_order_no'])) {
  334. $log->source_order_no = $params['source_order_no'];
  335. }
  336. if (isset($params['type'])) {
  337. $log->type = (int)$params['type'];
  338. }
  339. if (isset($params['money'])) {
  340. $log->money = (float)$params['money'];
  341. }
  342. if (isset($params['after_money'])) {
  343. $log->before_money = (float)$params['after_money'];
  344. }
  345. if (isset($params['remark'])) {
  346. $log->remark = $params['remark'];
  347. }
  348. if (isset($params['status'])) {
  349. $log->status = (int)$params['status'];
  350. }
  351. $log->update_time = time();
  352. $log->save();
  353. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => '修改账户明细', 'content' => json_encode($params, 256), 'module' => 'admin']);
  354. ActionLogModel::record();
  355. return ['code' => 0, 'msg' => '修改成功'];
  356. }
  357. /**
  358. * 设置状态
  359. */
  360. public function status()
  361. {
  362. $params = request()->all();
  363. $id = $params['id'] ?? 0;
  364. $status = $params['status'] ?? 1;
  365. $log = AccountLogModel::where('id', $id)
  366. ->where('mark', 1)
  367. ->first();
  368. if (!$log) {
  369. return ['code' => 1, 'msg' => '记录不存在'];
  370. }
  371. $log->status = $status;
  372. $log->update_time = time();
  373. $log->save();
  374. return ['code' => 0, 'msg' => '设置成功'];
  375. }
  376. /**
  377. * 删除
  378. * @return array
  379. */
  380. public function delete()
  381. {
  382. $id = request()->input('id');
  383. if (is_array($id)) {
  384. // 批量删除
  385. $count = AccountLogModel::whereIn('id', $id)
  386. ->where('mark', 1)
  387. ->update(['mark' => 0, 'update_time' => time()]);
  388. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "批量删除账户明细", 'content' => json_encode($id, 256), 'module' => 'admin']);
  389. ActionLogModel::record();
  390. return ['code' => 0, 'msg' => "成功删除{$count}条记录"];
  391. } else {
  392. // 单个删除
  393. $log = AccountLogModel::where('id', $id)
  394. ->where('mark', 1)
  395. ->first();
  396. if (!$log) {
  397. return ['code' => 1, 'msg' => '记录不存在'];
  398. }
  399. $log->mark = 0;
  400. $log->update_time = time();
  401. $log->save();
  402. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除账户明细", 'content' => json_encode(['id' => $id], 256), 'module' => 'admin']);
  403. ActionLogModel::record();
  404. return ['code' => 0, 'msg' => '删除成功'];
  405. }
  406. }
  407. }