AccountService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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\DriverModel;
  14. use App\Models\MemberModel;
  15. use App\Services\BaseService;
  16. use Illuminate\Support\Facades\DB;
  17. /**
  18. * 交易管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * Class AccountService
  22. * @package App\Services\Common
  23. */
  24. class AccountService extends BaseService
  25. {
  26. public static $instance = null;
  27. /**
  28. * 构造函数
  29. * @author laravel开发员
  30. * @since 2020/11/11
  31. * AccountService constructor.
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new AccountLogModel();
  36. }
  37. /**
  38. * 静态入口
  39. * @return static|null
  40. */
  41. public static function make()
  42. {
  43. if (!self::$instance) {
  44. self::$instance = (new static());
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * @param $params
  50. * @param int $pageSize
  51. * @return array
  52. */
  53. public function getDataList($params, $pageSize = 15)
  54. {
  55. $where = ['a.mark' => 1];
  56. $status = isset($params['status'])? $params['status'] : 0;
  57. $type = isset($params['type'])? $params['type'] : 0;
  58. if($status>0){
  59. $where['a.status'] = $status;
  60. }
  61. if($type>0){
  62. $where['a.type'] = $type;
  63. }
  64. $year = isset($params['year']) && $params['year']? $params['year'] : '';
  65. $year = $year? $year : date('Y');
  66. if(!$table = $this->model->getTable($year)){
  67. return false;
  68. }
  69. // 账号
  70. $list = $this->model->with(['member','driver'])->from("{$table} as a")
  71. ->leftJoin('member as b','b.id','=','a.source_id')
  72. ->leftJoin('member as c',function($join){
  73. $join->on('c.id','=','a.user_id')->whereIn('a.user_type',[1,3]);
  74. })
  75. ->leftJoin('driver as d',function($join){
  76. $join->on('d.id','=','a.user_id')->where('a.user_type', 2);
  77. })
  78. ->where($where)
  79. ->where(function ($query) use($params) {
  80. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  81. $userType = isset($params['user_type'])? $params['user_type'] : 0;
  82. if($userType >= 1){
  83. $query->where(['a.user_type'=> $userType]);
  84. }
  85. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  86. if($userId){
  87. $query->where('a.user_id',$userId);
  88. }
  89. if ($keyword) {
  90. $query->where(function($query) use($keyword){
  91. $query->where('c.nickname','like',"%{$keyword}%")
  92. ->orWhere('c.mobile','like',"%{$keyword}%")
  93. ->orWhere('d.realname','like',"%{$keyword}%")
  94. ->orWhere('d.mobile','like',"%{$keyword}%");
  95. });
  96. }
  97. $orderNo = isset($params['order_no'])? trim($params['order_no']) : '';
  98. if($orderNo){
  99. $query->where(function($query) use($orderNo){
  100. $query->where('a.source_order_no','like',"%{$orderNo}%");
  101. });
  102. }
  103. })
  104. ->where(function ($query) use($params){
  105. // 日期
  106. $date = isset($params['date']) ? $params['date'] : [];
  107. $start = isset($date[0])? $date[0] : '';
  108. $end = isset($date[1])? $date[1] : '';
  109. $end = $start>=$end? '' : $end;
  110. if ($start) {
  111. $query->where('a.create_time','>=', strtotime($start));
  112. }
  113. if($end){
  114. $query->where('a.create_time','<=', strtotime($end));
  115. }
  116. $coinType = isset($params['coin_type'])? $params['coin_type'] : 0;
  117. if($coinType && is_array($coinType)){
  118. $query->whereIn('a.coin_type',$coinType);
  119. }else if ($coinType){
  120. $query->where('a.coin_type',$coinType);
  121. }
  122. })
  123. ->select(['a.*','b.mobile as source_mobile','b.nickname as source_username'])
  124. ->orderBy('a.create_time','desc')
  125. ->orderBy('a.id','desc')
  126. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  127. $list = $list? $list->toArray() :[];
  128. if($list){
  129. foreach($list['data'] as &$item){
  130. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  131. $item['source_username'] = $item['source_mobile']? $item['source_username']."({$item['source_mobile']})" : $item['source_username'];
  132. $item['user_type_name'] = $item['user_type'] == 2? '司机': ($item['user_type'] == 4? '平台':'用户');
  133. }
  134. }
  135. return [
  136. 'pageSize'=> $pageSize,
  137. 'total'=>isset($list['total'])? $list['total'] : 0,
  138. 'list'=> isset($list['data'])? $list['data'] : []
  139. ];
  140. }
  141. /**
  142. * @param $params
  143. * @param int $pageSize
  144. * @return array
  145. */
  146. public function getCount($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. $year = isset($params['year']) && $params['year']? $params['year'] : '';
  158. $year = $year? $year : date('Y');
  159. if(!$table = $this->model->getTable($year)){
  160. return false;
  161. }
  162. // 数据
  163. $model = $this->model->with(['member','driver'])->from("{$table} as a")
  164. ->leftJoin('member as b','b.id','=','a.source_id')
  165. ->leftJoin('member as c',function($join){
  166. $join->on('c.id','=','a.user_id')->whereIn('a.user_type',[1,3]);
  167. })
  168. ->leftJoin('driver as d',function($join){
  169. $join->on('d.id','=','a.user_id')->where('a.user_type', 2);
  170. })
  171. ->where($where)
  172. ->where(function ($query) use($params) {
  173. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  174. $userType = isset($params['user_type'])? $params['user_type'] : 0;
  175. if($userType >= 1){
  176. $query->where(['a.user_type'=> $userType]);
  177. }
  178. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  179. if($userId){
  180. $query->where('a.user_id',$userId);
  181. }
  182. if ($keyword) {
  183. $query->where(function($query) use($keyword){
  184. $query->where('c.nickname','like',"%{$keyword}%")
  185. ->orWhere('c.mobile','like',"%{$keyword}%")
  186. ->orWhere('d.realname','like',"%{$keyword}%")
  187. ->orWhere('d.mobile','like',"%{$keyword}%");
  188. });
  189. }
  190. $orderNo = isset($params['order_no'])? trim($params['order_no']) : '';
  191. if($orderNo){
  192. $query->where(function($query) use($orderNo){
  193. $query->where('a.source_order_no','like',"%{$orderNo}%");
  194. });
  195. }
  196. })
  197. ->where(function ($query) use($params){
  198. // 日期
  199. $date = isset($params['date']) ? $params['date'] : [];
  200. $start = isset($date[0])? $date[0] : '';
  201. $end = isset($date[1])? $date[1] : '';
  202. $end = $start>=$end? '' : $end;
  203. if ($start) {
  204. $query->where('a.create_time','>=', strtotime($start));
  205. }
  206. if($end){
  207. $query->where('a.create_time','<=', strtotime($end));
  208. }
  209. $coinType = isset($params['coin_type'])? $params['coin_type'] : 0;
  210. if($coinType && is_array($coinType)){
  211. $query->whereIn('a.coin_type',$coinType);
  212. }else if ($coinType){
  213. $query->where('a.coin_type',$coinType);
  214. }
  215. });
  216. $totalModel = clone $model;
  217. $counts = [
  218. 'count'=> $model->count('a.id'),
  219. 'total'=> $totalModel->sum(DB::raw("abs(lev_a.money)")),
  220. 'income'=> $totalModel->sum(DB::raw("lev_a.money")),
  221. ];
  222. return $counts;
  223. }
  224. /**
  225. * 按日期统计流水金额
  226. * @param string $beginAt 开始时间
  227. * @param string $endAt 结束时间
  228. * @param int[] $status 状态:数组或数值
  229. * @return mixed
  230. */
  231. public function getTotalByTime($beginAt='', $endAt='', $status=1, $type=0)
  232. {
  233. $where = ['mark' => 1];
  234. return $this->model->where($where)->where(function($query) use($beginAt,$endAt,$status,$type){
  235. if($beginAt && $endAt){
  236. $query->whereBetween('create_time', [strtotime($beginAt), strtotime($endAt)]);
  237. }else if($beginAt){
  238. $query->where('create_time','>=', strtotime($beginAt));
  239. }
  240. if($status && is_array($status)){
  241. $query->whereIn('status',$status);
  242. }else if($status){
  243. $query->where('status',$status);
  244. }
  245. // 类型
  246. if($type && is_array($type)){
  247. $query->whereIn('type',$type);
  248. }else if($type){
  249. $query->where('type',$type);
  250. }
  251. })->sum(DB::raw("abs(money)"));
  252. }
  253. /**
  254. * 按日期统计营收金额
  255. * @param string $beginAt 开始时间
  256. * @param string $endAt 结束时间
  257. * @return mixed
  258. */
  259. public function getIncomeTotalByTime($beginAt='', $endAt='')
  260. {
  261. $where = ['mark' => 1,'status'=>1];
  262. return $this->model->where($where)->where(function($query) use($beginAt,$endAt){
  263. if($beginAt && $endAt){
  264. $query->whereBetween('create_time', [strtotime($beginAt), strtotime($endAt)]);
  265. }else if($beginAt){
  266. $query->where('create_time','>=', strtotime($beginAt));
  267. }
  268. // 营收
  269. $query->where(function($query){
  270. $query->where(['type'=>2,'coin_type'=>1])->orWhere(['type'=>1,'user_type'=>4]);
  271. });
  272. })->sum(DB::raw("abs(money)"));
  273. }
  274. /**
  275. * 添加或编辑
  276. * @return array
  277. * @since 2020/11/11
  278. * @author laravel开发员
  279. */
  280. public function edit()
  281. {
  282. $data = request()->all();
  283. return parent::edit($data); // TODO: Change the autogenerated stub
  284. }
  285. /**
  286. * 调整用户余额
  287. * @param $params
  288. * @return bool
  289. */
  290. public function changeBalance($params)
  291. {
  292. $userId = isset($params['user_id'])? $params['user_id']:0;
  293. $userType = isset($params['user_type'])? $params['user_type']:0;
  294. $money = isset($params['money'])? $params['money']:0;
  295. $type = isset($params['type'])? $params['type']:0;
  296. // 司机
  297. if($userType == 2){
  298. $userInfo = DriverModel::where(['id'=> $userId,'mark'=>1])->select(['id','balance'])->first();
  299. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  300. if(empty($userInfo)){
  301. $this->error = '2206';
  302. return false;
  303. }
  304. // 扣除验证
  305. if($type == 2 && $money > $balance){
  306. $this->error = '2202';
  307. return false;
  308. }
  309. // 处理
  310. DB::beginTransaction();
  311. $money = $type==1? $money : -$money;
  312. $data = ['balance'=> DB::raw("balance + {$money}"),'update_time'=>time()];
  313. if(!DriverModel::where(['id'=> $userId,'mark'=>1])->update($data)){
  314. DB::rollBack();
  315. $this->error = '2203';
  316. return false;
  317. }
  318. $logData = [
  319. 'user_id'=> $userId,
  320. 'source_id'=> 0,
  321. 'type'=> 4,
  322. 'coin_type'=> 4,
  323. 'user_type'=> 2,
  324. 'money'=> $money,
  325. 'balance'=> $balance,
  326. 'create_time'=> time(),
  327. 'update_time'=> time(),
  328. 'remark'=> isset($params['remark']) && $params['remark']? trim($params['remark']) : '余额调整',
  329. 'status'=> 1,
  330. 'mark'=>1
  331. ];
  332. if (!AccountLogModel::insertGetId($logData)) {
  333. $this->error = 2204;
  334. DB::rollBack();
  335. return true;
  336. }
  337. DB::commit();
  338. // 结算统计
  339. \App\Services\Api\FinanceService::make()->saveLog(0, $money,2);
  340. $this->error = 2205;
  341. return true;
  342. }else{
  343. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])->select(['id','balance'])->first();
  344. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  345. if(empty($userInfo)){
  346. $this->error = '2201';
  347. return false;
  348. }
  349. // 扣除验证
  350. if($type == 2 && $money > $balance){
  351. $this->error = '2202';
  352. return false;
  353. }
  354. // 处理
  355. DB::beginTransaction();
  356. $money = $type==1? $money : -$money;
  357. $data = ['balance'=> DB::raw("balance + {$money}"),'update_time'=>time()];
  358. if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($data)){
  359. DB::rollBack();
  360. $this->error = '2203';
  361. return false;
  362. }
  363. $logData = [
  364. 'user_id'=> $userId,
  365. 'source_id'=> 0,
  366. 'type'=> 4,
  367. 'coin_type'=> 4,
  368. 'user_type'=> $userType,
  369. 'money'=> $money,
  370. 'balance'=> $balance,
  371. 'create_time'=> time(),
  372. 'update_time'=> time(),
  373. 'remark'=> isset($params['remark']) && $params['remark']? trim($params['remark']) : '余额调整',
  374. 'status'=> 1,
  375. 'mark'=>1
  376. ];
  377. if (!AccountLogModel::insertGetId($logData)) {
  378. $this->error = 2204;
  379. DB::rollBack();
  380. return true;
  381. }
  382. DB::commit();
  383. // 结算统计
  384. \App\Services\Api\FinanceService::make()->saveLog(0, $money,2);
  385. $this->error = 2205;
  386. return true;
  387. }
  388. }
  389. /**
  390. * 调整用户积分
  391. * @param $params
  392. * @return bool
  393. */
  394. public function changeScore($params)
  395. {
  396. $userId = isset($params['user_id'])? $params['user_id']:0;
  397. $userType = isset($params['user_type'])? $params['user_type']:0;
  398. $money = isset($params['money'])? $params['money']:0;
  399. $type = isset($params['type'])? $params['type']:0;
  400. // 司机
  401. if($userType == 2){
  402. $userInfo = DriverModel::where(['id'=> $userId,'mark'=>1])->select(['id','tz_score'])->first();
  403. $score = isset($userInfo['tz_score'])? $userInfo['tz_score'] : 0;
  404. if(empty($userInfo)){
  405. $this->error = '2206';
  406. return false;
  407. }
  408. // 扣除验证
  409. if($type == 2 && $money > $score){
  410. $this->error = '2212';
  411. return false;
  412. }
  413. // 处理
  414. DB::beginTransaction();
  415. $money = $type==1? $money : -$money;
  416. $data = ['tz_score'=> DB::raw("tz_score + {$money}"),'update_time'=>time()];
  417. if(!DriverModel::where(['id'=> $userId,'mark'=>1])->update($data)){
  418. DB::rollBack();
  419. $this->error = '2213';
  420. return false;
  421. }
  422. $logData = [
  423. 'user_id'=> $userId,
  424. 'source_id'=> 0,
  425. 'type'=> 4,
  426. 'coin_type'=> 3,
  427. 'user_type'=> 2,
  428. 'money'=> $money,
  429. 'balance'=> $score,
  430. 'create_time'=> time(),
  431. 'update_time'=> time(),
  432. 'remark'=> isset($params['remark']) && $params['remark']? trim($params['remark']) : '通证积分调整',
  433. 'status'=> 1,
  434. 'mark'=>1
  435. ];
  436. if (!AccountLogModel::insertGetId($logData)) {
  437. $this->error = 2214;
  438. DB::rollBack();
  439. return true;
  440. }
  441. DB::commit();
  442. $this->error = 2215;
  443. return true;
  444. }else{
  445. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])->select(['id','tz_score'])->first();
  446. $score = isset($userInfo['tz_score'])? $userInfo['tz_score'] : 0;
  447. if(empty($userInfo)){
  448. $this->error = '2201';
  449. return false;
  450. }
  451. // 扣除验证
  452. if($type == 2 && $money > $score){
  453. $this->error = '2212';
  454. return false;
  455. }
  456. // 处理
  457. DB::beginTransaction();
  458. $money = $type==1? $money : -$money;
  459. $data = ['tz_score'=> DB::raw("tz_score + {$money}"),'update_time'=>time()];
  460. if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($data)){
  461. DB::rollBack();
  462. $this->error = '2213';
  463. return false;
  464. }
  465. $logData = [
  466. 'user_id'=> $userId,
  467. 'source_id'=> 0,
  468. 'type'=> 4,
  469. 'coin_type'=> 3,
  470. 'user_type'=> $userType,
  471. 'money'=> $money,
  472. 'balance'=> $score,
  473. 'create_time'=> time(),
  474. 'update_time'=> time(),
  475. 'remark'=> isset($params['remark']) && $params['remark']? trim($params['remark']) : '通证积分调整',
  476. 'status'=> 1,
  477. 'mark'=>1
  478. ];
  479. if (!AccountLogModel::insertGetId($logData)) {
  480. $this->error = 2214;
  481. DB::rollBack();
  482. return true;
  483. }
  484. DB::commit();
  485. $this->error = 2215;
  486. return true;
  487. }
  488. }
  489. }