AccountLogService.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 Illuminate\Support\Facades\DB;
  15. /**
  16. * 交易管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class AccountLogService
  20. * @package App\Services\Common
  21. */
  22. class AccountLogService extends BaseService
  23. {
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. * AccountService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new AccountLogModel();
  33. }
  34. /**
  35. * 静态入口
  36. * @return static|null
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = (new static());
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * @param $params
  47. * @param int $pageSize
  48. * @return array
  49. */
  50. public function getDataList($params, $pageSize = 15)
  51. {
  52. $where = ['mark' => 1];
  53. $status = isset($params['status'])? $params['status'] : 0;
  54. $type = isset($params['type'])? $params['type'] : 0;
  55. $coinType = isset($params['coin_type'])? $params['coin_type'] : 0;
  56. $userType = isset($params['user_type'])? $params['user_type'] : 0;
  57. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  58. $merchId = isset($params['merch_id'])? $params['merch_id'] : 0;
  59. if($status>0){
  60. $where['status'] = $status;
  61. }else{
  62. $where['status'] = 1;
  63. }
  64. if($type>0){
  65. $where['type'] = $type;
  66. }
  67. if($coinType>0){
  68. $where['coin_type'] = $coinType;
  69. }
  70. if($userId>0 && $merchId<=0){
  71. $where['user_id'] = $userId;
  72. }
  73. if($merchId>0){
  74. $where['merch_id'] = $merchId;
  75. }
  76. $year = isset($params['year']) && $params['year']? $params['year'] : '';
  77. $month = isset($params['month']) && $params['month']? $params['month'] : '';
  78. $date = $year&&$month? "{$year}-{$month}" : '';
  79. $date = $date? $date : date('Y');
  80. if(!$table = $this->model->getTable($year)){
  81. return false;
  82. }
  83. $model = DB::table($table)->where($where)->where(function($query) use($userType,$year,$month){
  84. if($userType>0){
  85. $query->whereIn('user_type', [0, $userType]);
  86. }
  87. $date = $year&&$month? "{$year}-{$month}" : '';
  88. $date = $date? $date : date('Y-m');
  89. if($month){
  90. $query->where('date', 'like', "{$date}%");
  91. }else{
  92. $query->where('date', 'like', "{$year}%");
  93. }
  94. });
  95. // 统计
  96. $countModel = clone $model;
  97. $countModel1 = clone $model;
  98. $counts = [
  99. 'expend'=> moneyFormat(abs($countModel->where('money','<', 0)->sum('money')), 2),
  100. 'income'=> moneyFormat($countModel1->where('money','>', 0)->sum('money'), 2),
  101. ];
  102. $list = $model->select(['*'])
  103. ->orderBy('create_time','desc')
  104. ->orderBy('id','desc')
  105. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  106. $list = $list? $list->toArray() :[];
  107. if($list){
  108. foreach($list['data'] as &$item){
  109. $item->time_text = $item->create_time? dateFormat($item->create_time,'m月d日 H:i') : date('Y年m月');
  110. $item->create_time = $item->create_time? datetime($item->create_time,'Y-m-d H.i.s') : '';
  111. $titles = ['','服务消费','商城消费','佣金结算','平台调整','余额充值','余额转账','退款','聊天付费','简历付费','代理佣金','账户余额提现','商户余额提现'];
  112. $item->remark = $item->remark? $item->remark : (isset($titles[$item->remark])? $titles[$item->remark]:'其他');
  113. }
  114. unset($item);
  115. }
  116. return [
  117. 'pageSize'=> $pageSize,
  118. 'counts'=> $counts,
  119. 'date'=> $date,
  120. 'total'=>isset($list['total'])? $list['total'] : 0,
  121. 'list'=> isset($list['data'])? $list['data'] : []
  122. ];
  123. }
  124. /**
  125. * @param $params
  126. * @param int $pageSize
  127. * @return array
  128. */
  129. public function getCounts($params, $pageSize = 15)
  130. {
  131. $where = ['mark' => 1];
  132. $status = isset($params['status'])? $params['status'] : 0;
  133. $type = isset($params['type'])? $params['type'] : 0;
  134. $coinType = isset($params['coin_type'])? $params['coin_type'] : 0;
  135. $countType = isset($params['count_type'])? $params['count_type'] : 0;
  136. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  137. $merchId = isset($params['merch_id'])? $params['merch_id'] : 0;
  138. if($status>0){
  139. $where['status'] = $status;
  140. }else{
  141. $where['status'] = 1;
  142. }
  143. if($type>0){
  144. $where['type'] = $type;
  145. }
  146. if($coinType>0){
  147. $where['coin_type'] = $coinType;
  148. }
  149. if($userId>0 && $merchId<=0){
  150. $where['user_id'] = $userId;
  151. }
  152. if($merchId>0){
  153. $where['merch_id'] = $merchId;
  154. }
  155. $year = isset($params['year']) && $params['year']? $params['year'] : '';
  156. $month = isset($params['month']) && $params['month']? $params['month'] : '';
  157. $types = [1=>'服务消费',2=>'商城消费',3=>'佣金结算',4=>'平台调整',5=>'余额充值',6=>'余额转账',7=>'退款',8=>'聊天付费',9=>'简历付费',11=>'账户余额提现',99=>'其他'];
  158. if(!$table = $this->model->getTable($year)){
  159. return false;
  160. }
  161. $userType = isset($params['user_type'])? $params['user_type'] : 0;
  162. $model = new AccountLogModel($table);
  163. $model = $model->where($where)->where(function($query) use($userType,$countType,$year,$month){
  164. if($userType>0){
  165. $query->whereIn('user_type', [0, $userType]);
  166. }
  167. if($countType == 1){
  168. $query->where('money', '<', 0);
  169. }else{
  170. $query->where('money', '>', 0);
  171. }
  172. $date = $year&&$month? "{$year}-{$month}" : '';
  173. $date = $date? $date : date('Y-m');
  174. if($month){
  175. $query->where('date', 'like', "{$date}%");
  176. }else{
  177. $query->where('date', 'like', "{$year}%");
  178. }
  179. });
  180. $model1 = clone $model;
  181. $total = abs(moneyFormat($model->sum('money'),2));
  182. $count = $model1->count('id');
  183. $list = [];
  184. $sums = $model1->select(['type',DB::raw('sum(money) as total')])
  185. ->groupBy('type')
  186. ->get()->keyBy('type');
  187. $sums = $sums? $sums->toArray() : [];
  188. foreach ($types as $k => $name){
  189. $sum = isset($sums[$k]['total'])? abs(moneyFormat($sums[$k]['total'],2)) : 0;
  190. if($sum>0){
  191. $list[] = [
  192. 'id'=> $k,
  193. 'name'=> $name,
  194. 'total'=> $sum,
  195. 'rate'=> round($sum/$total*100,2),
  196. ];
  197. }
  198. }
  199. return [
  200. 'counts'=> ['total'=> $total,'count'=> $count],
  201. 'list'=> $list,
  202. ];
  203. }
  204. }