AccountLogService.php 17 KB

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