AccountService.php 16 KB

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