AccountLogService.php 14 KB

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