// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\AccountModel; use App\Services\BaseService; /** * 交易管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class AccountService * @package App\Services\Common */ class AccountService extends BaseService { /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * AccountService constructor. */ public function __construct() { $this->model = new AccountModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * @param $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15) { $where = ['a.mark' => 1]; $status = isset($params['status'])? $params['status'] : 0; $type = isset($params['type'])? $params['type'] : 0; $coinType = isset($params['coin_type'])? $params['coin_type'] : 0; $userId = isset($params['user_id'])? $params['user_id'] : 0; $shopId = isset($params['shop_id'])? $params['shop_id'] : 0; if($status>0){ $where['a.status'] = $status; } if($type>0){ $where['a.type'] = $type; } if($coinType>0){ $where['a.coin_type'] = $coinType; } if($userId>0){ $where['a.user_id'] = $userId; } if($shopId>0){ $where['a.shop_id'] = $shopId; } $list = $this->model->from('account_log as a') ->leftJoin('member as b', 'b.id', '=', 'a.user_id') ->leftJoin('shop as c', 'c.id', '=', 'a.shop_id') ->leftJoin('member as d', 'd.id', '=', 'a.source_uid') ->where($where) ->where(function ($query) use($params){ $keyword = isset($params['keyword'])? $params['keyword'] : ''; if($keyword){ $query->where('b.nickname','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%"); } // 日期 $date = isset($params['date']) ? $params['date'] : []; $start = isset($date[0])? $date[0] : ''; $end = isset($date[1])? $date[1] : ''; $end = $start>=$end? '' : $end; if ($start) { $query->where('a.create_time','>=', strtotime($start)); } if($end){ $query->where('a.create_time','<=', strtotime($end)); } }) ->select(['a.*','b.username','b.mobile','b.nickname','c.name as shop_name','d.nickname as source_username']) ->orderBy('a.create_time','desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list? $list->toArray() :[]; if($list){ foreach($list['data'] as &$item){ $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : ''; $titles = ['','交易','推广佣金','平台充值','平台扣除','佣金转换']; if(in_array($item['type'], [1,2])){ $item['remark'] = $item['remark']? $item['remark'] : $titles[$item['type']]; } if($item['coin_type'] == 2 && $item['type'] == 3){ $item['remark'] = $item['remark']? $item['remark'] : ($item['money']>0?'平台充值':'平台扣除'); } } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } /** * 添加或编辑 * @return array * @since 2020/11/11 * @author laravel开发员 */ public function edit() { $data = request()->all(); return parent::edit($data); // TODO: Change the autogenerated stub } /** * 获取店铺交易统计 * @param $shopId * @param int $type * @param int $coinType * @return mixed */ public function getShopAccountTotal($shopId, $type = 1, $coinType=1) { $where = ['shop_id'=>$shopId,'status'=>1,'mark'=>1]; return $this->model->where($where) ->where(function($query) use($type, $coinType){ if($type){ $query->whereIn('type', is_array($type)? $type : [$type]); } if($coinType){ $query->whereIn('coin_type', is_array($coinType)? $coinType : [$coinType]); } }) ->sum('money'); } /** * 获取交易数 * @param $shopId * @param int $type * @param int $coinType * @return mixed */ public function getShopAccountCount($shopId, $type = 1, $coinType=1) { $where = ['shop_id'=>$shopId,'status'=>1,'mark'=>1]; if($coinType){ $where['coin_type'] = $coinType; } return $this->model->where($where) ->where(function($query) use($type, $coinType){ if($type){ $query->whereIn('type', is_array($type)? $type : [$type]); } if($coinType){ $query->whereIn('coin_type', is_array($coinType)? $coinType : [$coinType]); } }) ->count('id'); } }