AccountLogService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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\Api;
  12. use App\Models\AccountCountModel;
  13. use App\Models\AccountLogModel;
  14. use App\Models\MemberModel;
  15. use App\Services\BaseService;
  16. use App\Services\ConfigService;
  17. use App\Services\RedisService;
  18. use Illuminate\Support\Facades\DB;
  19. /**
  20. * 交易管理-服务类
  21. * @author laravel开发员
  22. * @since 2020/11/11
  23. * Class AccountLogService
  24. * @package App\Services\Common
  25. */
  26. class AccountLogService extends BaseService
  27. {
  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. $where = ['mark' => 1];
  57. $status = isset($params['status']) ? $params['status'] : 0;
  58. $type = isset($params['type']) ? $params['type'] : 0;
  59. $coinType = isset($params['coin_type']) ? $params['coin_type'] : 0;
  60. $userType = isset($params['user_type']) ? $params['user_type'] : 0;
  61. $tradeType = isset($params['trade_type']) ? $params['trade_type'] : 0;
  62. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  63. $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0;
  64. if ($status > 0) {
  65. $where['status'] = $status;
  66. } else {
  67. $where['status'] = 1;
  68. }
  69. if ($type > 0) {
  70. $where['type'] = $type;
  71. }
  72. if ($coinType > 0) {
  73. $where['coin_type'] = $coinType;
  74. }
  75. if ($userId > 0 && $merchId <= 0) {
  76. $where['user_id'] = $userId;
  77. }
  78. if ($merchId > 0) {
  79. $where['merch_id'] = $merchId;
  80. }
  81. $year = isset($params['year']) && $params['year'] ? $params['year'] : '';
  82. $month = isset($params['month']) && $params['month'] ? $params['month'] : '';
  83. $date = $year && $month ? "{$year}-{$month}" : '';
  84. $date = $date ? $date : date('Y');
  85. if (!$table = $this->model->getTable($year)) {
  86. return false;
  87. }
  88. $model = DB::table($table)->where($where)->where(function ($query) use ($userType, $tradeType, $year, $month) {
  89. if ($userType > 0) {
  90. $query->whereIn('user_type', [0, $userType]);
  91. }
  92. if($tradeType == 1){
  93. $query->where('actual_money','>',0);
  94. }else if($tradeType==2){
  95. $query->where('actual_money','<',0);
  96. }
  97. $date = $year && $month ? "{$year}-{$month}" : '';
  98. $date = $date ? $date : date('Y-m');
  99. if ($month) {
  100. $query->where('date', 'like', "{$date}%");
  101. } else {
  102. $query->where('date', 'like', "{$year}%");
  103. }
  104. });
  105. // 统计
  106. $countModel = clone $model;
  107. $countModel1 = clone $model;
  108. $counts = [
  109. 'expend' => moneyFormat(abs($countModel->where('money', '<', 0)->sum('money')), 2),
  110. 'income' => moneyFormat($countModel1->where('money', '>', 0)->sum('money'), 2),
  111. ];
  112. $list = $model->select(['*'])
  113. ->orderBy('create_time', 'desc')
  114. ->orderBy('id', 'desc')
  115. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  116. $list = $list ? $list->toArray() : [];
  117. if ($list) {
  118. foreach ($list['data'] as &$item) {
  119. $item->time_text = $item->create_time ? dateFormat($item->create_time, 'm月d日 H:i') : date('Y年m月');
  120. $item->pay_at = $item->create_time ? datetime($item->create_time, 'Y-m-d H:i:s') : '';
  121. $item->actual_money = moneyFormat($item->actual_money, 4);
  122. $item->money = moneyFormat($item->money, 4);
  123. if($item->user_type == 1){
  124. $item->member = MemberService::make()->getCacheInfo(['id'=>$item->user_id], ['id','nickname','username']);
  125. }else if($item->user_type == 2){
  126. $item->member = MerchantService::make()->getCacheInfo(['id'=>$item->user_id], ['id','name','user_id']);
  127. }else if($item->user_type == 3){
  128. $item->member = AcceptorService::make()->getCacheInfo(['id'=>$item->user_id], ['id','relname','user_id']);
  129. }else if($item->user_type == 4){
  130. $item->member = ['id'=>0,'nickname'=>'平台'];
  131. }
  132. }
  133. unset($item);
  134. }
  135. return [
  136. 'pageSize' => $pageSize,
  137. 'counts' => $counts,
  138. 'date' => $date,
  139. 'total' => isset($list['total']) ? $list['total'] : 0,
  140. 'list' => isset($list['data']) ? $list['data'] : []
  141. ];
  142. }
  143. /**
  144. * 积分统计
  145. * @param $userId 用户ID
  146. * @param int $coinType 币种类型
  147. * @return \float[][]
  148. */
  149. public function getScoreCounts($userId=0, int $coinType=4)
  150. {
  151. $counts = [
  152. 'day_total' => $this->getScoreCountByDate(4, $coinType, 23,3),
  153. 'award_total' => $this->getScoreCountByDate(1, $coinType, 9,3),
  154. ];
  155. $xlTotal = ConfigService::make()->getConfigByCode('xd_total', 100000000);
  156. $xlTotal = $xlTotal>0? $xlTotal : 100000000;
  157. $counts['xl_total'] = $xlTotal;
  158. $counts['remain_total'] = moneyFormat($xlTotal - ($counts['day_total'] + $counts['award_total']),2);
  159. $counts['total'] = moneyFormat($counts['day_total'] + $counts['award_total'],2);
  160. return $counts;
  161. }
  162. /**
  163. * 分类账户统计
  164. * @param $userId 用户ID
  165. * @param int $coinType 币种类型
  166. * @param int $userType 用户账户类型
  167. * @return \float[][]
  168. */
  169. public function getCountsByType($userId, $coinType = 1, $userType = 1, $type=1)
  170. {
  171. $datas = [
  172. 'counts' => [
  173. 'today' => $this->getCountDataByDate($userId, $coinType, $userType,1),
  174. 'yestday' => $this->getCountDataByDate($userId, $coinType, $userType,2),
  175. 'total' => $this->getCountDataByDate($userId, $coinType, $userType,0)
  176. ],
  177. ];
  178. if($type == 1){
  179. $datas['tables'] = [
  180. 'gl_burn' => $this->getCountDataByType($userId, $coinType, $userType,98),
  181. 'level_burn' => $this->getCountDataByType($userId, $coinType, $userType,97),
  182. 'live' => $this->getCountDataByType($userId, $coinType, $userType,1),
  183. 'tip' => $this->getCountDataByType($userId, $coinType, $userType,11),
  184. 'global' => $this->getCountDataByType($userId, $coinType, $userType,15),
  185. 'gl' => $this->getCountDataByType($userId, $coinType, $userType,13),
  186. 'point' => $this->getCountDataByType($userId, $coinType, $userType,14),
  187. 'invite' => $this->getCountDataByType($userId, $coinType, $userType,12),
  188. ];
  189. $datas['tables'] = array_values($datas['tables']);
  190. }
  191. return $datas;
  192. }
  193. /**
  194. * 统计
  195. * @param $userId
  196. * @param $coinType
  197. * @param $userType
  198. * @param string $field
  199. * @return array|mixed
  200. */
  201. public function getCountDataByType($userId, $coinType, $userType, $type=0, $field='actual_money')
  202. {
  203. $cacheKey = "caches:accounts:count_type_{$userId}_{$coinType}_{$userType}_{$type}";
  204. $data = RedisService::get($cacheKey);
  205. if($data){
  206. return $data;
  207. }
  208. $data = $this->model->where(['user_id'=> $userId,'type'=> $type,'coin_type'=> $coinType,'user_type'=>$userType,'status'=>1,'mark'=>1])
  209. ->where($field,'>',0)
  210. ->sum($field);
  211. $data = $data? moneyFormat($data, 2) : 0.00;
  212. if($data){
  213. RedisService::set($cacheKey, $data, rand(3,5));
  214. }
  215. return $data;
  216. }
  217. /**
  218. * 统计
  219. * @param $userId
  220. * @param $coinType
  221. * @param $userType
  222. * @param string $field
  223. * @return array|mixed
  224. */
  225. public function getCountDataByDate($userId, $coinType, $userType, $dateType=1, $field='actual_money')
  226. {
  227. $cacheKey = "caches:accounts:count_date_{$userId}_{$coinType}_{$userType}_{$dateType}";
  228. $data = RedisService::get($cacheKey);
  229. if($data){
  230. return moneyFormat($data, 2);
  231. }
  232. $data = $this->model->where(['user_id'=> $userId,'coin_type'=> $coinType,'user_type'=>$userType,'status'=>1,'mark'=>1])
  233. ->where(function($query) use($dateType){
  234. // 今日
  235. if($dateType == 1){
  236. $query->where('date','>=', date('Y-m-d'));
  237. }
  238. // 昨日
  239. else if ($dateType == 2){
  240. $query->where('date','<', date('Y-m-d'));
  241. $query->where('date','>=', date('Y-m-d', strtotime('-1 day')));
  242. }
  243. })
  244. ->where($field,'>',0)
  245. ->sum($field);
  246. $data = $data? moneyFormat($data, 2) : 0.00;
  247. if($data){
  248. RedisService::set($cacheKey, $data, rand(3,5));
  249. }
  250. return $data;
  251. }
  252. /**
  253. * 积分统计
  254. * @param $userId
  255. * @param $coinType
  256. * @param $userType
  257. * @param string $field
  258. * @return array|mixed
  259. */
  260. public function getScoreCountByDate($userType = 1, $coinType, $type=23, $dateType=1, $field='actual_money')
  261. {
  262. $cacheKey = "caches:accounts:score_date_{$userType}_{$coinType}_{$type}_{$dateType}";
  263. $data = RedisService::get($cacheKey);
  264. if($data){
  265. return moneyFormat($data, 2);
  266. }
  267. $where = ['coin_type'=> $coinType,'status'=>1,'mark'=>1];
  268. if($userType){
  269. $where['user_type'] = $userType;
  270. }
  271. if($type>0){
  272. $where['type'] = $type;
  273. }
  274. $data = $this->model->where($where)
  275. ->where(function($query) use($dateType){
  276. // 今日
  277. if($dateType == 1){
  278. $query->where('date','>=', date('Y-m-d'));
  279. }
  280. // 昨日
  281. else if($dateType == 2){
  282. $query->where('date','<', date('Y-m-d'))
  283. ->where('date','>=', date('Y-m-d', strtotime('-1 day')));
  284. }
  285. // 截止昨日
  286. else if ($dateType == 3){
  287. $query->where('date','<', date('Y-m-d'));
  288. }
  289. })
  290. ->where($field,'>',0)
  291. ->sum($field);
  292. $data = $data? moneyFormat($data, 2) : 0.00;
  293. if($data){
  294. RedisService::set($cacheKey, $data, rand(3,5));
  295. }
  296. return $data;
  297. }
  298. /**
  299. * 个人每日业绩
  300. * @param $userId
  301. * @param $date
  302. * @param int $type
  303. * @return array|mixed
  304. */
  305. public function getTotal($userId, $date, $type = 2)
  306. {
  307. $cacheKey = "caches:members:account:total_{$userId}_{$date}_{$type}";
  308. $data = RedisService::get($cacheKey);
  309. if($data){
  310. return $data;
  311. }
  312. $data = AccountCountModel::where(['user_id'=> $userId,'date'=>$date,'type'=> $type])
  313. ->sum('money');
  314. if($data){
  315. RedisService::set($cacheKey, 5, 10);
  316. }
  317. return $data;
  318. }
  319. /**
  320. * 团队每日业绩
  321. * @param $userId
  322. * @param $date
  323. * @param int $type
  324. * @return array|mixed
  325. */
  326. public function getTeamTotal($userId, $date, $type = 2)
  327. {
  328. $cacheKey = "caches:members:account:tram_total_{$userId}_{$date}_{$type}";
  329. $data = RedisService::get($cacheKey);
  330. if($data){
  331. return $data;
  332. }
  333. $data = AccountCountModel::from('account_counts as a')
  334. ->leftJoin('member as b','b.id','=','a.user_id')
  335. ->where(function($query) use($userId){
  336. $query->where('b.id',$userId)->orWhereRaw('FIND_IN_SET(?,b.parents)', $userId);
  337. })
  338. ->where(['a.date'=>$date,'a.type'=> $type])
  339. ->sum('a.money');
  340. if($data){
  341. RedisService::set($cacheKey, $data, rand(5,10));
  342. }
  343. return $data;
  344. }
  345. /**
  346. * @param $params
  347. * @param int $pageSize
  348. * @return array
  349. */
  350. public function getCounts($params, $pageSize = 15)
  351. {
  352. $where = ['mark' => 1];
  353. $status = isset($params['status']) ? $params['status'] : 0;
  354. $type = isset($params['type']) ? $params['type'] : 0;
  355. $coinType = isset($params['coin_type']) ? $params['coin_type'] : 0;
  356. $countType = isset($params['count_type']) ? $params['count_type'] : 0;
  357. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  358. $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0;
  359. if ($status > 0) {
  360. $where['status'] = $status;
  361. } else {
  362. $where['status'] = 1;
  363. }
  364. if ($type > 0) {
  365. $where['type'] = $type;
  366. }
  367. if ($coinType > 0) {
  368. $where['coin_type'] = $coinType;
  369. }
  370. if ($userId > 0 && $merchId <= 0) {
  371. $where['user_id'] = $userId;
  372. }
  373. if ($merchId > 0) {
  374. $where['merch_id'] = $merchId;
  375. }
  376. $year = isset($params['year']) && $params['year'] ? $params['year'] : '';
  377. $month = isset($params['month']) && $params['month'] ? $params['month'] : '';
  378. $types = [1 => '服务消费', 2 => '商城消费', 3 => '佣金结算', 4 => '平台调整', 5 => '余额充值', 6 => '余额转账', 7 => '退款', 8 => '聊天付费', 9 => '简历付费', 11 => '账户余额提现', 99 => '其他'];
  379. if (!$table = $this->model->getTable($year)) {
  380. return false;
  381. }
  382. $userType = isset($params['user_type']) ? $params['user_type'] : 0;
  383. $model = new AccountLogModel($table);
  384. $model = $model->where($where)->where(function ($query) use ($userType, $countType, $year, $month) {
  385. if ($userType > 0) {
  386. $query->whereIn('user_type', [0, $userType]);
  387. }
  388. if ($countType == 1) {
  389. $query->where('money', '<', 0);
  390. } else {
  391. $query->where('money', '>', 0);
  392. }
  393. $date = $year && $month ? "{$year}-{$month}" : '';
  394. $date = $date ? $date : date('Y-m');
  395. if ($month) {
  396. $query->where('date', 'like', "{$date}%");
  397. } else {
  398. $query->where('date', 'like', "{$year}%");
  399. }
  400. });
  401. $model1 = clone $model;
  402. $total = abs(moneyFormat($model->sum('money'), 2));
  403. $count = $model1->count('id');
  404. $list = [];
  405. $sums = $model1->select(['type', DB::raw('sum(money) as total')])
  406. ->groupBy('type')
  407. ->get()->keyBy('type');
  408. $sums = $sums ? $sums->toArray() : [];
  409. foreach ($types as $k => $name) {
  410. $sum = isset($sums[$k]['total']) ? abs(moneyFormat($sums[$k]['total'], 2)) : 0;
  411. if ($sum > 0) {
  412. $list[] = [
  413. 'id' => $k,
  414. 'name' => $name,
  415. 'total' => $sum,
  416. 'rate' => round($sum / $total * 100, 2),
  417. ];
  418. }
  419. }
  420. return [
  421. 'counts' => ['total' => $total, 'count' => $count],
  422. 'list' => $list,
  423. ];
  424. }
  425. /**
  426. * 更新账户统计
  427. * @param $userId 用户ID
  428. * @param $money 金额
  429. * @param int $type 类型
  430. * @return mixed
  431. */
  432. public function saveCount($userId, $money, $type=1)
  433. {
  434. $date = date('Y-m-d');
  435. $id = AccountCountModel::where(['user_id'=> $userId,'date'=> $date,'type'=> $type])->value('id');
  436. if(!$id){
  437. $data = ['user_id'=>$userId,'date'=> $date,'type'=>$type,'money'=>$money,'create_time'=>time(),'update_time'=> time()];
  438. return AccountCountModel::insertGetId($data);
  439. }else{
  440. $data = ['money'=>DB::raw("money + {$money}"),'update_time'=>time()];
  441. return AccountCountModel::where(['id'=> $id])->update($data);
  442. }
  443. }
  444. }