AccountService.php 16 KB

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