AccountService.php 14 KB

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