| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\AccountModel;
- use App\Models\MemberModel;
- use App\Services\BaseService;
- use Illuminate\Support\Facades\DB;
- /**
- * 交易管理-服务类
- * @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');
- }
- /**
- * 调整用户佣金
- * @param $params
- * @return bool
- */
- public function changeBonus($params)
- {
- $userId = isset($params['user_id'])? $params['user_id']:0;
- $money = isset($params['money'])? $params['money']:0;
- $type = isset($params['type'])? $params['type']:0;
- $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])->select(['id','login_shop_id','bonus','bonus_total'])->first();
- $bonus = isset($userInfo['bonus'])? $userInfo['bonus'] : 0;
- $bonusTotal = isset($userInfo['bonus_total'])? $userInfo['bonus_total'] : 0;
- if(empty($userInfo)){
- $this->error = '2201';
- return false;
- }
- // 扣除佣金验证
- if($type == 2 && $money > $bonus){
- $this->error = '2202';
- return false;
- }
- // 处理
- DB::beginTransaction();
- $money = $type==1? $money : -$money;
- $data = ['bonus'=> max(0, $bonus+$money), 'bonus_total'=> max(0, $bonusTotal+$money),'update_time'=>time()];
- if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($data)){
- DB::rollBack();
- $this->error = '2203';
- return false;
- }
- $logData = [
- 'user_id'=> $userId,
- 'shop_id'=> $userInfo['login_shop_id'],
- 'source_uid'=> isset($params['source_uid'])? intval($params['source_uid']) : 0,
- 'type'=> 3,
- 'coin_type'=> 2,
- 'money'=> $money,
- 'balance'=> $bonus,
- 'create_time'=> time(),
- 'remark'=> isset($params['remark']) && $params['remark']? trim($params['remark']) : '佣金调整',
- 'status'=> 1,
- 'mark'=>1
- ];
- if (!AccountModel::insertGetId($logData)) {
- $this->error = 2204;
- DB::rollBack();
- return true;
- }
- DB::commit();
- // 结算统计
- FinanceService::make()->settleBonus($bonus, 2);
- $this->error = 2205;
- return true;
- }
- /**
- * 调整用户积分
- * @param $params
- * @return bool
- */
- public function changeScore($params)
- {
- $userId = isset($params['user_id'])? $params['user_id']:0;
- $money = isset($params['money'])? $params['money']:0;
- $type = isset($params['type'])? $params['type']:0;
- $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])->select(['id','login_shop_id','score'])->first();
- $score = isset($userInfo['score'])? $userInfo['score'] : 0;
- if(empty($userInfo)){
- $this->error = '2201';
- return false;
- }
- // 扣除积分验证
- if($type == 2 && $money > $score){
- $this->error = '2212';
- return false;
- }
- // 处理
- DB::beginTransaction();
- $money = $type==1? $money : -$money;
- $data = ['score'=> max(0, $score+$money),'update_time'=>time()];
- if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($data)){
- DB::rollBack();
- $this->error = '2213';
- return false;
- }
- $logData = [
- 'user_id'=> $userId,
- 'shop_id'=> $userInfo['login_shop_id'],
- 'type'=> 3,
- 'coin_type'=> 3,
- 'money'=> $money,
- 'balance'=> $score,
- 'create_time'=> time(),
- 'remark'=> '积分调整',
- 'status'=> 1,
- 'mark'=>1
- ];
- if (!AccountModel::insertGetId($logData)) {
- $this->error = 2214;
- DB::rollBack();
- return true;
- }
- DB::commit();
- $this->error = 2215;
- return true;
- }
- }
|