AccountLogService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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\RedisService;
  16. use Illuminate\Support\Facades\DB;
  17. /**
  18. * 交易管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * Class AccountLogService
  22. * @package App\Services\Common
  23. */
  24. class AccountLogService extends BaseService
  25. {
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * AccountService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new AccountLogModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * @param $params
  49. * @param int $pageSize
  50. * @return array
  51. */
  52. public function getDataList($params, $pageSize = 15)
  53. {
  54. $where = ['mark' => 1];
  55. $status = isset($params['status']) ? $params['status'] : 0;
  56. $type = isset($params['type']) ? $params['type'] : 0;
  57. $coinType = isset($params['coin_type']) ? $params['coin_type'] : 0;
  58. $userType = isset($params['user_type']) ? $params['user_type'] : 0;
  59. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  60. $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0;
  61. if ($status > 0) {
  62. $where['status'] = $status;
  63. } else {
  64. $where['status'] = 1;
  65. }
  66. if ($type > 0) {
  67. $where['type'] = $type;
  68. }
  69. if ($coinType > 0) {
  70. $where['coin_type'] = $coinType;
  71. }
  72. if ($userId > 0 && $merchId <= 0) {
  73. $where['user_id'] = $userId;
  74. }
  75. if ($merchId > 0) {
  76. $where['merch_id'] = $merchId;
  77. }
  78. $year = isset($params['year']) && $params['year'] ? $params['year'] : '';
  79. $month = isset($params['month']) && $params['month'] ? $params['month'] : '';
  80. $date = $year && $month ? "{$year}-{$month}" : '';
  81. $date = $date ? $date : date('Y');
  82. if (!$table = $this->model->getTable($year)) {
  83. return false;
  84. }
  85. $model = DB::table($table)->where($where)->where(function ($query) use ($userType, $year, $month) {
  86. if ($userType > 0) {
  87. $query->whereIn('user_type', [0, $userType]);
  88. }
  89. $date = $year && $month ? "{$year}-{$month}" : '';
  90. $date = $date ? $date : date('Y-m');
  91. if ($month) {
  92. $query->where('date', 'like', "{$date}%");
  93. } else {
  94. $query->where('date', 'like', "{$year}%");
  95. }
  96. });
  97. // 统计
  98. $countModel = clone $model;
  99. $countModel1 = clone $model;
  100. $counts = [
  101. 'expend' => moneyFormat(abs($countModel->where('money', '<', 0)->sum('money')), 2),
  102. 'income' => moneyFormat($countModel1->where('money', '>', 0)->sum('money'), 2),
  103. ];
  104. $list = $model->select(['*'])
  105. ->orderBy('create_time', 'desc')
  106. ->orderBy('id', 'desc')
  107. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  108. $list = $list ? $list->toArray() : [];
  109. if ($list) {
  110. foreach ($list['data'] as &$item) {
  111. $item->time_text = $item->create_time ? dateFormat($item->create_time, 'm月d日 H:i') : date('Y年m月');
  112. $item->pay_at = $item->create_time ? datetime($item->create_time, 'Y-m-d H:i:s') : '';
  113. $item->actual_money = moneyFormat($item->actual_money, 4);
  114. $item->money = moneyFormat($item->money, 4);
  115. if($item->user_type == 1){
  116. $item->member = MemberService::make()->getCacheInfo(['id'=>$item->user_id], ['id','nickname','username']);
  117. }else if($item->user_type == 2){
  118. $item->member = MerchantService::make()->getCacheInfo(['id'=>$item->user_id], ['id','name','user_id']);
  119. }else if($item->user_type == 3){
  120. $item->member = AcceptorService::make()->getCacheInfo(['id'=>$item->user_id], ['id','relname','user_id']);
  121. }else if($item->user_type == 4){
  122. $item->member = ['id'=>0,'nickname'=>'平台'];
  123. }
  124. }
  125. unset($item);
  126. }
  127. return [
  128. 'pageSize' => $pageSize,
  129. 'counts' => $counts,
  130. 'date' => $date,
  131. 'total' => isset($list['total']) ? $list['total'] : 0,
  132. 'list' => isset($list['data']) ? $list['data'] : []
  133. ];
  134. }
  135. /**
  136. * 分类账户统计
  137. * @param $userId 用户ID
  138. * @param int $coinType 币种类型
  139. * @param int $userType 用户账户类型
  140. * @return \float[][]
  141. */
  142. public function getCountsByType($userId, $coinType = 1, $userType = 1)
  143. {
  144. $datas = [
  145. 'counts' => [
  146. 'today' => $this->getCountDataByDate($userId, $coinType, $userType,1),
  147. 'yestday' => $this->getCountDataByDate($userId, $coinType, $userType,2),
  148. 'total' => $this->getCountDataByDate($userId, $coinType, $userType,0)
  149. ],
  150. 'tables' => [
  151. 'gl_burn' => $this->getCountDataByType($userId, $coinType, $userType,98),
  152. 'level_burn' => $this->getCountDataByType($userId, $coinType, $userType,97),
  153. 'live' => $this->getCountDataByType($userId, $coinType, $userType,1),
  154. 'tip' => $this->getCountDataByType($userId, $coinType, $userType,11),
  155. 'global' => $this->getCountDataByType($userId, $coinType, $userType,15),
  156. 'gl' => $this->getCountDataByType($userId, $coinType, $userType,13),
  157. 'point' => $this->getCountDataByType($userId, $coinType, $userType,14),
  158. 'invite' => $this->getCountDataByType($userId, $coinType, $userType,12),
  159. ]
  160. ];
  161. $datas['tables'] = array_values($datas['tables']);
  162. return $datas;
  163. }
  164. /**
  165. * 统计
  166. * @param $userId
  167. * @param $coinType
  168. * @param $userType
  169. * @param string $field
  170. * @return array|mixed
  171. */
  172. public function getCountDataByType($userId, $coinType, $userType, $type=0, $field='money')
  173. {
  174. $cacheKey = "caches:accounts:count_type_{$userId}_{$coinType}_{$userType}_{$type}";
  175. $data = RedisService::get($cacheKey);
  176. if($data){
  177. return $data;
  178. }
  179. $data = $this->model->where(['user_id'=> $userId,'type'=> $type,'coin_type'=> $coinType,'user_type'=>$userType,'status'=>1,'mark'=>1])
  180. ->where($field,'>',0)
  181. ->sum($field);
  182. $data = $data? moneyFormat($data, 2) : 0.00;
  183. if($data){
  184. RedisService::set($cacheKey, $data, rand(3,5));
  185. }
  186. return $data;
  187. }
  188. /**
  189. * 统计
  190. * @param $userId
  191. * @param $coinType
  192. * @param $userType
  193. * @param string $field
  194. * @return array|mixed
  195. */
  196. public function getCountDataByDate($userId, $coinType, $userType, $dateType=1, $field='money')
  197. {
  198. $cacheKey = "caches:accounts:count_date_{$userId}_{$coinType}_{$userType}_{$dateType}";
  199. $data = RedisService::get($cacheKey);
  200. if($data){
  201. return $data;
  202. }
  203. $data = $this->model->where(['user_id'=> $userId,'coin_type'=> $coinType,'user_type'=>$userType,'status'=>1,'mark'=>1])
  204. ->where(function($query) use($dateType){
  205. // 今日
  206. if($dateType == 1){
  207. $query->where('date','>=', date('Y-m-d'));
  208. }
  209. // 昨日
  210. else if ($dateType == 2){
  211. $query->where('date','<', date('Y-m-d'));
  212. $query->where('date','>=', date('Y-m-d', strtotime('-1 day')));
  213. }
  214. })
  215. ->where($field,'>',0)
  216. ->sum($field);
  217. $data = $data? moneyFormat($data, 2) : 0.00;
  218. if($data){
  219. RedisService::set($cacheKey, $data, rand(3,5));
  220. }
  221. return $data;
  222. }
  223. /**
  224. * @param $params
  225. * @param int $pageSize
  226. * @return array
  227. */
  228. public function getCounts($params, $pageSize = 15)
  229. {
  230. $where = ['mark' => 1];
  231. $status = isset($params['status']) ? $params['status'] : 0;
  232. $type = isset($params['type']) ? $params['type'] : 0;
  233. $coinType = isset($params['coin_type']) ? $params['coin_type'] : 0;
  234. $countType = isset($params['count_type']) ? $params['count_type'] : 0;
  235. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  236. $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0;
  237. if ($status > 0) {
  238. $where['status'] = $status;
  239. } else {
  240. $where['status'] = 1;
  241. }
  242. if ($type > 0) {
  243. $where['type'] = $type;
  244. }
  245. if ($coinType > 0) {
  246. $where['coin_type'] = $coinType;
  247. }
  248. if ($userId > 0 && $merchId <= 0) {
  249. $where['user_id'] = $userId;
  250. }
  251. if ($merchId > 0) {
  252. $where['merch_id'] = $merchId;
  253. }
  254. $year = isset($params['year']) && $params['year'] ? $params['year'] : '';
  255. $month = isset($params['month']) && $params['month'] ? $params['month'] : '';
  256. $types = [1 => '服务消费', 2 => '商城消费', 3 => '佣金结算', 4 => '平台调整', 5 => '余额充值', 6 => '余额转账', 7 => '退款', 8 => '聊天付费', 9 => '简历付费', 11 => '账户余额提现', 99 => '其他'];
  257. if (!$table = $this->model->getTable($year)) {
  258. return false;
  259. }
  260. $userType = isset($params['user_type']) ? $params['user_type'] : 0;
  261. $model = new AccountLogModel($table);
  262. $model = $model->where($where)->where(function ($query) use ($userType, $countType, $year, $month) {
  263. if ($userType > 0) {
  264. $query->whereIn('user_type', [0, $userType]);
  265. }
  266. if ($countType == 1) {
  267. $query->where('money', '<', 0);
  268. } else {
  269. $query->where('money', '>', 0);
  270. }
  271. $date = $year && $month ? "{$year}-{$month}" : '';
  272. $date = $date ? $date : date('Y-m');
  273. if ($month) {
  274. $query->where('date', 'like', "{$date}%");
  275. } else {
  276. $query->where('date', 'like', "{$year}%");
  277. }
  278. });
  279. $model1 = clone $model;
  280. $total = abs(moneyFormat($model->sum('money'), 2));
  281. $count = $model1->count('id');
  282. $list = [];
  283. $sums = $model1->select(['type', DB::raw('sum(money) as total')])
  284. ->groupBy('type')
  285. ->get()->keyBy('type');
  286. $sums = $sums ? $sums->toArray() : [];
  287. foreach ($types as $k => $name) {
  288. $sum = isset($sums[$k]['total']) ? abs(moneyFormat($sums[$k]['total'], 2)) : 0;
  289. if ($sum > 0) {
  290. $list[] = [
  291. 'id' => $k,
  292. 'name' => $name,
  293. 'total' => $sum,
  294. 'rate' => round($sum / $total * 100, 2),
  295. ];
  296. }
  297. }
  298. return [
  299. 'counts' => ['total' => $total, 'count' => $count],
  300. 'list' => $list,
  301. ];
  302. }
  303. }