AccountService.php 16 KB

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