AccountLogService.php 12 KB

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