AccountLogService.php 11 KB

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