// +---------------------------------------------------------------------- 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'], 'type'=> 3, 'coin_type'=> 2, 'money'=> $money, 'balance'=> $bonus, 'create_time'=> time(), '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; } }