| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- <?php
- // +----------------------------------------------------------------------
- // | saiadmin [ saiadmin快速开发框架 ]
- // +----------------------------------------------------------------------
- // | Author: sai <1430792918@qq.com>
- // +----------------------------------------------------------------------
- namespace plugin\saiadmin\basic;
- use plugin\saiadmin\app\cache\UserInfoCache;
- use plugin\saiadmin\exception\ApiException;
- use support\think\Db;
- /**
- * 逻辑层基础类
- * @package app\service
- * @method static where($data) think-orm的where方法
- * @method static find($id) think-orm的find方法
- * @method static findOrEmpty($id) think-orm的findOrEmpty方法
- * @method static hidden($data) think-orm的hidden方法
- * @method static order($data) think-orm的order方法
- * @method static save($data) think-orm的save方法
- * @method static create($data) think-orm的create方法
- * @method static saveAll($data) think-orm的saveAll方法
- * @method static update($data, $where, $allow = []) think-orm的update方法
- * @method static select() think-orm的select方法
- * @method static count($data) think-orm的count方法
- * @method static max($data) think-orm的max方法
- * @method static min($data) think-orm的min方法
- * @method static sum($data) think-orm的sum方法
- * @method static avg($data) think-orm的avg方法
- */
- class BaseLogic
- {
- /**
- * @var object 模型注入
- */
- protected $model;
- /**
- * @var object 管理员信息
- */
- protected $adminInfo;
- /**
- * 排序字段
- * @var string
- */
- protected string $orderField = '';
- /**
- * 排序方式
- * @var string
- */
- protected string $orderType = 'ASC';
- /**
- * 初始化
- * @param $user
- * @return void
- */
- public function init($user): void
- {
- $this->adminInfo = $user;
- }
- /**
- * 设置排序字段
- * @param $field
- * @return void
- */
- public function setOrderField($field): void
- {
- $this->orderField = $field;
- }
- /**
- * 设置排序方式
- * @param $type
- * @return void
- */
- public function setOrderType($type): void
- {
- $this->orderType = $type;
- }
- /**
- * 数据库事务操作
- * @param callable $closure
- * @param bool $isTran
- * @return mixed
- */
- public function transaction(callable $closure, bool $isTran = true): mixed
- {
- return $isTran ? Db::transaction($closure) : $closure();
- }
- /**
- * 添加数据
- * @param $data
- * @return mixed
- */
- public function add($data): mixed
- {
- $this->model->save($data);
- return $this->model->getKey();
- }
- /**
- * 修改数据
- * @param $id
- * @param $data
- * @return mixed
- */
- public function edit($id, $data): mixed
- {
- $model = $this->model->findOrEmpty($id);
- if ($model->isEmpty()) {
- throw new ApiException('数据不存在');
- }
- return $model->save($data);
- }
- /**
- * 读取数据
- * @param $id
- * @return mixed
- */
- public function read($id): mixed
- {
- $model = $this->model->findOrEmpty($id);
- if ($model->isEmpty()) {
- throw new ApiException('数据不存在');
- }
- return $model;
- }
- /**
- * 删除数据
- * @param $ids
- */
- public function destroy($ids)
- {
- $this->model->destroy($ids);
- }
- /**
- * 搜索器搜索
- * @param array $searchWhere
- * @return mixed
- */
- public function search(array $searchWhere = []): mixed
- {
- $withSearch = array_keys($searchWhere);
- $data = $searchWhere;
- foreach ($withSearch as $k => $v) {
- if ($data[$v] === '') {
- unset($data[$v]);
- unset($withSearch[$k]);
- }
- }
- return $this->model->withSearch($withSearch, $data);
- }
- /**
- * 分页查询数据
- * @param $query
- * @return mixed
- */
- public function getList($query): mixed
- {
- $saiType = request()->input('saiType', 'list');
- $page = request()->input('page', 1);
- $limit = request()->input('limit', 10);
- $orderBy = request()->input('orderBy', '');
- $orderType = request()->input('orderType', $this->orderType);
- if (empty($orderBy)) {
- $orderBy = $this->orderField !== '' ? $this->orderField : $this->model->getPk();
- }
- $query->order($orderBy, $orderType);
- if ($saiType === 'all') {
- return $query->select()->toArray();
- }
- return $query->paginate($limit, false, ['page' => $page])->toArray();
- }
- /**
- * 获取全部数据
- * @param $query
- * @return mixed
- */
- public function getAll($query): mixed
- {
- $orderBy = request()->input('orderBy', '');
- $orderType = request()->input('orderType', $this->orderType);
- if (empty($orderBy)) {
- $orderBy = $this->orderField !== '' ? $this->orderField : $this->model->getPk();
- }
- $query->order($orderBy, $orderType);
- return $query->select()->toArray();
- }
- /**
- * 转换列表列
- * @param $data
- * @param $fields
- * @return mixed
- */
- public function trandformListColumn($data, $fields = ['site', 'agent', 'game', 'auth', 'author', 'media', 'pay_channel', 'game_pay_channel', 'ip'])
- {
- if (in_array('site', $fields)) {
- $agentSiteList = Db::connect('db_advert')->table('agent_site')->field('id,name')->select()->toArray();
- $agentSiteList = array_column($agentSiteList, 'name', 'id');
- }
- if (in_array('agent', $fields)) {
- $agentList = Db::connect('db_advert')->table('agent_list')->field('id,name')->select()->toArray();
- $agentList = array_column($agentList, 'name', 'id');
- }
- if (in_array('game', $fields)) {
- $gameList = Db::connect('db_center')->table('pf_game')->field('id,name,os,ios_appid')->select()->toArray();
- $gameList = array_column($gameList, null, 'id');
- }
- if (in_array('auth', $fields)) {
- $authList = Db::connect('db_system')->table('sa_system_user')->field('id,username')->select()->toArray();
- $authList = array_column($authList, 'username', 'id');
- }
- if (in_array('author', $fields)) {
- $authorList = Db::connect('db_system')->table('sa_system_user')->field('id,username')->select()->toArray();
- $authorList = array_column($authorList, 'username', 'id');
- }
- if (in_array('media', $fields)) {
- $mediaList = Db::connect('db_advert')->table('media_list')->field('id,name')->select()->toArray();
- $mediaList = array_column($mediaList, 'name', 'id');
- }
- if (in_array('pay_channel', $fields)) {
- $payChannelList = Db::connect('db_center')->table('pay_channel')->field('id,name')->where('status', 1)->select()->toArray();
- $payChannelList = array_column($payChannelList, 'name', 'id');
- }
- if (in_array('game_pay_channel', $fields)) {
- $gamePayChannelList = Db::connect('db_center')->table('pay_channel')->field('id,name')->where('status', 1)->select()->toArray();
- $gamePayChannelList = array_column($gamePayChannelList, 'name', 'id');
- }
- foreach ($data as &$value) {
- if (!empty($agentSiteList)) {
- $value['site_name'] = $agentSiteList[$value['site_id']] ?? '';
- }
- if (!empty($agentList)) {
- $value['agent_name'] = $agentList[$value['agent_id']] ?? '';
- }
- if (!empty($gameList)) {
- $value['game_name'] = $gameList[$value['game_id']]['name'] ?? '';
- $value['game_os'] = $gameList[$value['game_id']]['os'] ?? '';
- $value['ios_appid'] = $gameList[$value['game_id']]['ios_appid'] ?? '';
- }
- if (!empty($authList)) {
- $value['auth_name'] = $authList[$value['auth_id']] ?? '';
- }
- if (!empty($authorList)) {
- $value['author_name'] = $authorList[$value['author_id']] ?? '';
- }
- if (!empty($mediaList)) {
- $value['media_name'] = $mediaList[$value['media_id']] ?? '';
- }
- if (!empty($payChannelList)) {
- $value['pay_channel_name'] = $payChannelList[$value['pay_channel_id']] ?? '';
- }
- if (!empty($gamePayChannelList)) {
- $value['alipay_wap_name'] = $gamePayChannelList[$value['alipay_wap']] ?? '-';
- $value['inapp_name'] = $gamePayChannelList[$value['inapp']] ?? '-';
- $value['wechat_wap_name'] = $gamePayChannelList[$value['wechat_wap']] ?? '-';
- $value['wechat_scan_name'] = $gamePayChannelList[$value['wechat_scan']] ?? '-';
- $value['wechat_jsapi_name'] = $gamePayChannelList[$value['wechat_jsapi']] ?? '-';
- }
- if (in_array('ip', $fields)) {
- $value['ip'] = getIpLocation($value['ip']) ?? '';
- }
- }
- unset($value);
- return $data;
- }
- /**
- * 获取上传的导入文件
- * @param $file
- * @return string
- */
- public function getImport($file): string
- {
- $full_dir = runtime_path() . '/resource/';
- if (!is_dir($full_dir)) {
- mkdir($full_dir, 0777, true);
- }
- $ext = $file->getUploadExtension() ?: null;
- $full_path = $full_dir . md5(time()) . '.' . $ext;
- $file->move($full_path);
- return $full_path;
- }
- /**
- * 方法调用
- * @param $name
- * @param $arguments
- * @return mixed
- */
- public function __call($name, $arguments)
- {
- // TODO: Implement __call() method.
- return call_user_func_array([$this->model, $name], $arguments);
- }
- /**
- * 根据权限搜索器
- * @param array $searchWhere
- * @param array $withSearch 搜索器
- * @return mixed
- */
- public function searchByAuth(array $searchWhere = []): mixed
- {
- $withSearch = array_keys($searchWhere);
- $data = $searchWhere;
- foreach ($withSearch as $k => $v) {
- if ($data[$v] === '') {
- unset($data[$v]);
- unset($withSearch[$k]);
- }
- }
- $userAuthCache = new UserInfoCache(getCurrentInfo()['id']);
- $userInfo = $userAuthCache->getUserInfo();
- // Todo 如果角色权限,超过跳过权限限制
- $roleIds = array_column($userInfo['roleList'], 'id');
- if (in_array(1, $roleIds)) {
- return $data;
- }
- // Todo 1、游戏权限
- $authGameList = $userInfo['deptList']['game_list'];
- if ($authGameList != '*') {
- $authGameIds = explode(',', $authGameList);
- if (!empty($data['game_id'])) {
- $inputGameIds = is_array($data['game_id']) ? $data['game_id'] : explode(',', $data['game_id']);
- $data['game_id'] = array_values(array_intersect($inputGameIds, $authGameIds)); // 如果传入了game_id,则取权限交集
- } else {
- // 如果没传入game_id,则取权限
- $data['game_id'] = $authGameIds;
- }
- }
- // Todo 2、广告数据权限
- $userId = $userInfo['id'];
- $deptId = $userInfo['deptList']['id'];
- // 广告权限,自己 or 全部
- $authAdPermission = $userInfo['ad_permission'];
- if ($authAdPermission) {
- if ($authAdPermission == 1) { // Todo 1、仅自己
- $data['auth_id'] = [$userId];
- } elseif ($authAdPermission == 2) { // Todo 2、自己部门
- $underUserIds = Db::connect('db_system')->table('sa_system_user')->where('dept_id', 'in', $deptId)->column('id');
- if (!empty($data['auth_id'])) { // 如果传入了负责人ID,则取交集
- $data['auth_id'] = array_values(array_intersect($data['auth_id'], array_values($underUserIds)));
- } else { // 如果没有传入负责人ID, 则取当前用户以及下面组员
- $data['auth_id'] = array_values($underUserIds);
- }
- }
- }
- // 看指定游戏的自然量
- $authNormalGameList = $userInfo['normal_game_list']; // 可看自然量的游戏
- if ($authNormalGameList != '*' && $authNormalGameList != '-1' && !empty($data['game_id'])) {
- // 则取auth_normal_game_list交集
- $data['normal_game_id'] = array_values(array_intersect(explode(',', $authNormalGameList), $data['game_id']));
- }
- return $data;
- }
- // Todo 公共 whereRaw 子句
- protected function getCommonWhereRaw($params): string
- {
- $eqParams = ["user_name", "media_id", "site_id", "agent_id", "vt", "server_id", "server_name"];
- $inParams = ["game_id", "auth_id"];
- $betweenParams = ["tdate", "reg_date", "pay_date"];
- $timeParams = ["reg_time", "pay_time", "login_time"];
- $DateTimeParams = ["create_time"];
- // Todo And条件
- $whereRaw = " 1=1 ";
- foreach ($params as $key => $value) {
- if (in_array($key, $eqParams)) {
- $whereRaw .= " AND `{$key}`='{$value}'";
- } elseif (in_array($key, $inParams) && !empty($value)) {
- $value = is_string($value) ? explode(',', $value) : $value;
- $whereRaw .= " AND `{$key}` IN ('" . implode("','", $value) . "')";
- } elseif (in_array($key, $betweenParams) && !empty($value)) {
- $whereRaw .= " AND `{$key}` BETWEEN '{$value[0]}' AND '{$value[1]}'";
- } elseif (in_array($key, $DateTimeParams) && !empty($value)) {
- $value[0] = $value[0] . ' 00:00:00';
- $value[1] = $value[1] . ' 23:59:59';
- $whereRaw .= " AND `{$key}` BETWEEN '{$value[0]}' AND '{$value[1]}'";
- } elseif (in_array($key, $timeParams) && !empty($value)) {
- $value[0] = strtotime($value[0] . ' 00:00:00');
- $value[1] = strtotime($value[1] . ' 23:59:59');
- $whereRaw .= " AND `{$key}` BETWEEN '{$value[0]}' AND '{$value[1]}'";
- }
- }
- // Todo 自然量走 Or 条件
- $whereOr = [];
- if (!empty($params['normal_game_id'])) {
- foreach ($params['normal_game_id'] as $gameId) {
- $whereOr[] = "(game_id = {$gameId} AND auth_id=0)";
- }
- }
- $whereOr = $whereOr ? implode(' OR ', $whereOr) : "";
- return $whereRaw . ($whereOr ? " OR {$whereOr}" : "");
- }
- // 合并表查询
- public function generateUnionList($namePrefix, $range, $whereRaw = '', $field = '*', $group = null)
- {
- $sqlParts = [];
- foreach ($range as $ext) {
- $tableName = $namePrefix . '_' . $ext;
- $sqlParts[] = "SELECT * FROM {$tableName} WHERE {$whereRaw}";
- }
- $unionSql = implode(" UNION ALL ", $sqlParts);
- $finalSql = "
- SELECT {$field} FROM ( {$unionSql} ) AS all_total_day
- ";
- if (!empty($group)) {
- $finalSql .= " GROUP BY {$group}";
- }
- return Db::connect('db_data_report')->query($finalSql);
- }
- }
|