AccountService.php 16 KB

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