AccountService.php 16 KB

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