AccountService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\Common;
  12. use App\Models\AccountModel;
  13. use App\Services\BaseService;
  14. /**
  15. * 交易管理-服务类
  16. * @author laravel开发员
  17. * @since 2020/11/11
  18. * Class AccountService
  19. * @package App\Services\Common
  20. */
  21. class AccountService extends BaseService
  22. {
  23. /**
  24. * 构造函数
  25. * @author laravel开发员
  26. * @since 2020/11/11
  27. * AccountService constructor.
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new AccountModel();
  32. }
  33. /**
  34. * 静态入口
  35. * @return static|null
  36. */
  37. public static function make()
  38. {
  39. if (!self::$instance) {
  40. self::$instance = (new static());
  41. }
  42. return self::$instance;
  43. }
  44. /**
  45. * @param $params
  46. * @param int $pageSize
  47. * @return array
  48. */
  49. public function getDataList($params, $pageSize = 15)
  50. {
  51. $where = ['a.mark' => 1];
  52. $status = isset($params['status'])? $params['status'] : 0;
  53. $type = isset($params['type'])? $params['type'] : 0;
  54. $coinType = isset($params['coin_type'])? $params['coin_type'] : 0;
  55. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  56. $shopId = isset($params['shop_id'])? $params['shop_id'] : 0;
  57. if($status>0){
  58. $where['a.status'] = $status;
  59. }
  60. if($type>0){
  61. $where['a.type'] = $type;
  62. }
  63. if($coinType>0){
  64. $where['a.coin_type'] = $coinType;
  65. }
  66. if($userId>0){
  67. $where['a.user_id'] = $userId;
  68. }
  69. if($shopId>0){
  70. $where['a.shop_id'] = $shopId;
  71. }
  72. $list = $this->model->from('account_log as a')
  73. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  74. ->leftJoin('shop as c', 'c.id', '=', 'a.shop_id')
  75. ->leftJoin('member as d', 'd.id', '=', 'a.source_uid')
  76. ->where($where)
  77. ->where(function ($query) use($params){
  78. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  79. if($keyword){
  80. $query->where('b.nickname','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%");
  81. }
  82. // 日期
  83. $date = isset($params['date']) ? $params['date'] : [];
  84. $start = isset($date[0])? $date[0] : '';
  85. $end = isset($date[1])? $date[1] : '';
  86. $end = $start>=$end? '' : $end;
  87. if ($start) {
  88. $query->where('a.create_time','>=', strtotime($start));
  89. }
  90. if($end){
  91. $query->where('a.create_time','<=', strtotime($end));
  92. }
  93. })
  94. ->select(['a.*','b.username','b.mobile','b.nickname','c.name as shop_name','d.nickname as source_username'])
  95. ->orderBy('a.create_time','desc')
  96. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  97. $list = $list? $list->toArray() :[];
  98. if($list){
  99. foreach($list['data'] as &$item){
  100. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  101. $titles = ['','交易','推广佣金','平台充值','平台扣除','佣金转换'];
  102. if(in_array($item['type'], [1,2])){
  103. $item['remark'] = $item['remark']? $item['remark'] : $titles[$item['type']];
  104. }
  105. if($item['coin_type'] == 2 && $item['type'] == 3){
  106. $item['remark'] = $item['remark']? $item['remark'] : ($item['money']>0?'平台充值':'平台扣除');
  107. }
  108. }
  109. }
  110. return [
  111. 'pageSize'=> $pageSize,
  112. 'total'=>isset($list['total'])? $list['total'] : 0,
  113. 'list'=> isset($list['data'])? $list['data'] : []
  114. ];
  115. }
  116. /**
  117. * 添加或编辑
  118. * @return array
  119. * @since 2020/11/11
  120. * @author laravel开发员
  121. */
  122. public function edit()
  123. {
  124. $data = request()->all();
  125. return parent::edit($data); // TODO: Change the autogenerated stub
  126. }
  127. /**
  128. * 获取店铺交易统计
  129. * @param $shopId
  130. * @param int $type
  131. * @param int $coinType
  132. * @return mixed
  133. */
  134. public function getShopAccountTotal($shopId, $type = 1, $coinType=1)
  135. {
  136. $where = ['shop_id'=>$shopId,'status'=>1,'mark'=>1];
  137. return $this->model->where($where)
  138. ->where(function($query) use($type, $coinType){
  139. if($type){
  140. $query->whereIn('type', is_array($type)? $type : [$type]);
  141. }
  142. if($coinType){
  143. $query->whereIn('coin_type', is_array($coinType)? $coinType : [$coinType]);
  144. }
  145. })
  146. ->sum('money');
  147. }
  148. /**
  149. * 获取交易数
  150. * @param $shopId
  151. * @param int $type
  152. * @param int $coinType
  153. * @return mixed
  154. */
  155. public function getShopAccountCount($shopId, $type = 1, $coinType=1)
  156. {
  157. $where = ['shop_id'=>$shopId,'status'=>1,'mark'=>1];
  158. if($coinType){
  159. $where['coin_type'] = $coinType;
  160. }
  161. return $this->model->where($where)
  162. ->where(function($query) use($type, $coinType){
  163. if($type){
  164. $query->whereIn('type', is_array($type)? $type : [$type]);
  165. }
  166. if($coinType){
  167. $query->whereIn('coin_type', is_array($coinType)? $coinType : [$coinType]);
  168. }
  169. })
  170. ->count('id');
  171. }
  172. }