AccountService.php 16 KB

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