123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- <?php
- /**
- * MemGameLogic.php UTF-8
- *
- *
- * @date : 2018/6/1 17:19
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : luowei <lw@huosdk.com>
- * @version : HUOSDK 8.0
- */
- namespace huoAccountDeal\logic;
- use huo\logic\member\MemberLogic;
- use huo\model\common\CommonModel;
- use huo\model\game\GameModel;
- use huo\model\member\MemGameModel;
- use huoAccountDeal\controller\MemGameCache;
- use huoAccountDeal\model\AccountGoodsModel;
- use huolib\constant\AccountConst;
- use huolib\constant\CommonConst;
- use huolib\constant\GameConst;
- use huolib\status\AccountStatus;
- use think\Exception;
- class MemGameLogic extends CommonModel {
- /**
- * @param int $mem_id
- *
- * @return array
- */
- public function getGames($mem_id = 0) {
- if (empty($mem_id)) {
- return [];
- }
- $_map['mem_id'] = $mem_id;
- $_app_id_arr = (new MemGameModel())->where($_map)->order('create_time desc')->column("distinct(app_id)");
- if (empty($_app_id_arr)) {
- return [];
- }
- return $_app_id_arr;
- }
- /**
- * 获取正在售卖的游戏
- *
- * @return array
- */
- public function getSellGames() {
- $_ex_time = time() - AccountConst::ACCOUNT_MAX_LOCK_TIME;
- $_map = 'status='.AccountConst::STATUS_PULL_ON_SHELVES.' OR status='.AccountConst::STATUS_LOCKED
- .' AND lock_time<'.$_ex_time;
- $_app_id_arr = (new AccountGoodsModel())->where($_map)->column("distinct(app_id)");
- if (empty($_app_id_arr)) {
- return [];
- }
- return $_app_id_arr;
- }
- /**
- * @param int $mem_id 玩家ID
- *
- * @param $page
- *
- * @return array
- */
- public function getMyGameList($mem_id, $page = '1,10') {
- $_app_ids = $this->getGames($mem_id);
- if (empty($_app_ids)) {
- return [
- 'count' => 0,
- 'list' => null
- ];
- }
- $_map['id'] = ['in', $_app_ids];
- $_map['status'] = GameConst::GAME_STATUS_ON;
- $_game_model = (new GameModel());
- $_count = $_game_model->where($_map)->count();
- if ($_count <= 0) {
- return [
- 'count' => 0,
- 'list' => null
- ];
- }
- $_datas = $_game_model->withCount(
- ['mg' => function ($query) use ($mem_id) {
- $query->where('mem_id', $mem_id);
- }]
- )->page($page)
- ->where($_map)
- ->select();
- if (is_object($_datas)) {
- $_datas = $_datas->toArray();
- }
- $_rdata = [];
- $_list = [];
- foreach ($_datas as $_v) {
- $_list['game_id'] = $_v['id'];
- $_list['game_icon'] = $_v['icon'];
- $_list['gamename'] = $_v['name'];
- $_list['account_cnt'] = $_v['mg_count'];
- $_rdata[] = $_list;
- }
- if (empty($_rdata)) {
- $_rdata = null;
- }
- return [
- 'count' => $_count,
- 'list' => $_rdata
- ];
- }
- /**
- * 获取正在售卖的商品游戏
- *
- * @param int $mem_id 玩家ID
- * @param string $page 分页
- *
- * @return array
- */
- public function getSellGameList($mem_id = 0, $page = '1,10') {
- $_sell_app_ids = $this->getSellGames();
- if (!empty($mem_id)) {
- $_my_app_ids = $this->getGames($mem_id);
- $_app_ids = array_intersect($_sell_app_ids, $_my_app_ids);
- } else {
- $_app_ids = $_sell_app_ids;
- }
- $_map['id'] = ['in', $_app_ids];
- $_map['status'] = GameConst::GAME_STATUS_ON;
- $_game_model = (new GameModel());
- $_count = $_game_model->where($_map)->count();
- if ($_count <= 0) {
- return [
- 'count' => $_count,
- 'list' => null
- ];
- }
- $_datas = $_game_model->withCount(
- ['goods' => function ($query) {
- $query->where('status', AccountConst::STATUS_PULL_ON_SHELVES);
- }]
- )->page($page)->where($_map)->select();
- if (is_object($_datas)) {
- $_datas = $_datas->toArray();
- }
- $_rdata = [];
- $_list = [];
- foreach ($_datas as $_v) {
- $_list['game_id'] = $_v['id'];
- $_list['game_icon'] = $_v['icon'];
- $_list['gamename'] = $_v['name'];
- $_list['account_cnt'] = $_v['goods_count'];
- $_rdata[] = $_list;
- }
- if (empty($_rdata)) {
- $_rdata = null;
- }
- return [
- 'count' => $_count,
- 'list' => $_rdata
- ];
- }
- protected $base_field
- = [
- 'id',
- ];
- public function getWhere($param = []) {
- $_map = [];
- if (!empty($param['mem_id'])) {
- $_map['mem_id'] = $param['mem_id'];
- }
- if (!empty($param['app_id'])) {
- $_map['app_id'] = $param['app_id'];
- }
- return $_map;
- }
- public function getAccountList($where, $page = '1,10', $order = '-is_default') {
- $_map = $this->getWhere($where);
- $_field = [
- 'id' => 'mg_mem_id',
- 'nickname' => 'nickname',
- 'is_default' => 'is_default',
- 'create_time' => 'create_time',
- ];
- return $this->getList($_map, $_field, $page, $order);
- }
- public function getList($map, $_field = [], $page, $order) {
- $_where = $map;
- $_model = new MemGameModel();
- $_count = $_model->where($_where)->count();
- if (empty($_count)) {
- return [
- 'count' => 0,
- 'list' => []
- ];
- }
- $_field = array_merge($this->base_field, $_field);
- $_order = $_model->orderFilter($order);
- $_datas = $_model->field($_field)->where($_where)->order($_order)->page($page)->select();
- if (is_object($_datas)) {
- $_datas = $_datas->toArray();
- }
- if (empty($_datas)) {
- return [
- 'count' => $_count,
- 'list' => []
- ];
- }
- $_mem_game_cache = MemGameCache::ins();
- foreach ($_datas as $key => $data) {
- $_mem_game_cache_info = $_mem_game_cache->getInfoById($data['id']);
- $data['sum_money'] = isset($_mem_game_cache_info['sum_money'])
- ? $_mem_game_cache_info['sum_money'] : 0.00;
- /* 添加是否在售卖 */
- $data['is_sell'] = (new AccountGoodsModel())->isSell($data['mg_mem_id']);
- $_datas[$key] = $data;
- }
- return [
- 'count' => $_count,
- 'list' => $_datas
- ];
- }
- /**
- * 获取所有小号列表
- *
- * @param $param
- * @param string $page
- * @param string $order
- *
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getAllAccountList($param, $page = '1,10', $order = '-create_time') {
- $_rdata = ['count' => 0, 'list' => []];
- $_map = [];
- if (!empty($param['username'])) {
- $_mem_ids = (new MemberLogic())->getIdsByUsername($param['username']);
- /*没有则返回空*/
- if (empty($_mem_ids)) {
- return $_rdata;
- }
- $_map['mem_id'] = ['in', $_mem_ids];
- }
- if (!empty($param['status'])) {
- $_map['status'] = $param['status'];
- }
- if (!empty($param['app_id'])) {
- $_map['app_id'] = $param['app_id'];
- }
- if (!empty($param['nickname'])) {
- $_map['nickname'] = $param['nickname'];
- }
- if (!empty($param['start_time']) && !empty($param['end_time'])) {
- $_map['create_time'] = ['between', [strtotime($param['start_time']), strtotime($param['end_time'])]];
- } else if (!empty($param['start_time'])) {
- $_map['create_time'] = ['gt', strtotime($param['start_time'])];
- } else if (!empty($param['end_time'])) {
- $_map['create_time'] = ['lt', CommonConst::CONST_DAY_SECONDS + strtotime($param['end_time'])];
- }
- $_field = [
- 'id' => 'id',
- 'app_id' => 'app_id',
- 'mem_id' => 'mem_id',
- 'nickname' => 'nickname',
- 'create_time' => 'create_time',
- 'update_time' => 'update_time',
- 'is_default' => 'is_default',
- 'status' => 'status',
- ];
- $_model = new MemGameModel();
- $_count = $_model->where($_map)->count();
- if (empty($_count)) {
- return $_rdata;
- }
- $_order = $_model->orderFilter($order);
- $_datas = $_model->field($_field)->with('mem,game2,pay')->where($_map)->order($_order)->page($page)->select();
- if (is_object($_datas)) {
- $_datas = $_datas->toArray();
- }
- if (empty($_datas)) {
- return $_rdata;
- }
- foreach ($_datas as $_key => $_data) {
- $_list[$_key]['me_mem_id'] = $_data['id'];
- $_list[$_key]['mem'] = !empty($_data['mem']) ? $_data['mem']['username'] : '';
- $_list[$_key]['gamename'] = !empty($_data['game2']) ? $_data['game2']['name'] : '';
- $_list[$_key]['sum'] = !empty($_data['pay']) ? $_data['pay'][0]['sum'] : 0;
- $_list[$_key]['nickname'] = $_data['nickname'];
- $_list[$_key]['create_time'] = $_data['create_time'];
- $_list[$_key]['update_time'] = $_data['update_time'];
- $_list[$_key]['status'] = $_data['status'];
- }
- return [
- 'count' => $_count,
- 'list' => $_list
- ];
- }
- /**
- * 切换游戏小号
- *
- * @param $mem_id
- * @param $app_id
- * @param $account_id
- *
- * @return bool
- */
- public function change($mem_id, $app_id, $account_id) {
- $_model = new MemGameModel();
- $_model->where('mem_id', '=', $mem_id)
- ->where('app_id', '=', $app_id)
- ->update(['is_default' => MemGameModel::IS_DEFAULT_FALSE]);
- $_model->where('id', '=', $account_id)
- ->update(['is_default' => MemGameModel::IS_DEFAULT_TRUE]);
- return true;
- }
- /**
- * 添加小号
- *
- * @param $data
- *
- * @return bool
- */
- public function add($data) {
- $_model = new MemGameModel();
- $_data = [
- 'mem_id' => isset($data['mem_id']) ? $data['mem_id'] : 0,
- 'app_id' => isset($data['app_id']) ? $data['app_id'] : 0,
- 'nickname' => isset($data['nickname']) ? $data['nickname'] : '',
- 'create_time' => time(),
- 'update_time' => time(),
- 'is_default' => MemGameModel::IS_DEFAULT_FALSE,
- ];
- if (false !== $_model->addData($_data)) {
- return true;
- }
- return false;
- }
- /**
- * 检查小号是否可以操作
- *
- * @param int $mem_id 玩家ID
- * @param int $app_id 游戏ID
- * @param int $mg_mem_id 小号ID
- *
- * @return bool|int
- */
- public function canOperate($mem_id, $app_id, $mg_mem_id) {
- $_model = new MemGameModel();
- $_account = $_model->getInfoById($mg_mem_id);
- if (empty($_account)) {
- return AccountStatus::ACCOUNT_NOT_EXISTS;
- }
- if ($_account['mem_id'] != $mem_id) {
- return AccountStatus::ACCOUNT_DOES_NOT_BELONG_MEM;
- }
- if ($_account['app_id'] != $app_id) {
- return AccountStatus::ACCOUNT_DOES_NOT_BELONG_GAME;
- }
- if ($_account['status'] == MemGameModel::STATUS_LOCKED) {
- return AccountStatus::ACCOUNT_LOCKED;
- }
- return true;
- }
- /**
- * 更新玩家小号信息
- *
- * @param $mg_mem_id
- * @param $mem_game_data
- *
- * @return bool
- */
- public function updateMemGame($mg_mem_id, $mem_game_data) {
- if (empty($mg_mem_id) || empty($mem_game_data)) {
- return false;
- }
- $_map['id'] = $mg_mem_id;
- $_rs = MemGameModel::update($mem_game_data, $_map, true);
- if (false === $_rs) {
- return false;
- }
- return true;
- }
- public function getInfoById($mem_game_id) {
- $_map['id'] = $mem_game_id;
- try {
- $_info = (new MemGameModel())->where($_map)->find();
- if (false === $_info) {
- return false;
- }
- if (is_object($_info)) {
- return $_info->toArray();
- } else {
- return $_info;
- }
- } catch (Exception $_e) {
- return false;
- }
- }
- }
|