MemGameLogic.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. /**
  3. * MemGameLogic.php UTF-8
  4. *
  5. *
  6. * @date : 2018/6/1 17:19
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : luowei <lw@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huoAccountDeal\logic;
  13. use huo\logic\member\MemberLogic;
  14. use huo\model\common\CommonModel;
  15. use huo\model\game\GameModel;
  16. use huo\model\member\MemGameModel;
  17. use huoAccountDeal\controller\MemGameCache;
  18. use huoAccountDeal\model\AccountGoodsModel;
  19. use huolib\constant\AccountConst;
  20. use huolib\constant\CommonConst;
  21. use huolib\constant\GameConst;
  22. use huolib\status\AccountStatus;
  23. use think\Exception;
  24. class MemGameLogic extends CommonModel {
  25. /**
  26. * @param int $mem_id
  27. *
  28. * @return array
  29. */
  30. public function getGames($mem_id = 0) {
  31. if (empty($mem_id)) {
  32. return [];
  33. }
  34. $_map['mem_id'] = $mem_id;
  35. $_app_id_arr = (new MemGameModel())->where($_map)->order('create_time desc')->column("distinct(app_id)");
  36. if (empty($_app_id_arr)) {
  37. return [];
  38. }
  39. return $_app_id_arr;
  40. }
  41. /**
  42. * 获取正在售卖的游戏
  43. *
  44. * @return array
  45. */
  46. public function getSellGames() {
  47. $_ex_time = time() - AccountConst::ACCOUNT_MAX_LOCK_TIME;
  48. $_map = 'status='.AccountConst::STATUS_PULL_ON_SHELVES.' OR status='.AccountConst::STATUS_LOCKED
  49. .' AND lock_time<'.$_ex_time;
  50. $_app_id_arr = (new AccountGoodsModel())->where($_map)->column("distinct(app_id)");
  51. if (empty($_app_id_arr)) {
  52. return [];
  53. }
  54. return $_app_id_arr;
  55. }
  56. /**
  57. * @param int $mem_id 玩家ID
  58. *
  59. * @param $page
  60. *
  61. * @return array
  62. */
  63. public function getMyGameList($mem_id, $page = '1,10') {
  64. $_app_ids = $this->getGames($mem_id);
  65. if (empty($_app_ids)) {
  66. return [
  67. 'count' => 0,
  68. 'list' => null
  69. ];
  70. }
  71. $_map['id'] = ['in', $_app_ids];
  72. $_map['status'] = GameConst::GAME_STATUS_ON;
  73. $_game_model = (new GameModel());
  74. $_count = $_game_model->where($_map)->count();
  75. if ($_count <= 0) {
  76. return [
  77. 'count' => 0,
  78. 'list' => null
  79. ];
  80. }
  81. $_datas = $_game_model->withCount(
  82. ['mg' => function ($query) use ($mem_id) {
  83. $query->where('mem_id', $mem_id);
  84. }]
  85. )->page($page)
  86. ->where($_map)
  87. ->select();
  88. if (is_object($_datas)) {
  89. $_datas = $_datas->toArray();
  90. }
  91. $_rdata = [];
  92. $_list = [];
  93. foreach ($_datas as $_v) {
  94. $_list['game_id'] = $_v['id'];
  95. $_list['game_icon'] = $_v['icon'];
  96. $_list['gamename'] = $_v['name'];
  97. $_list['account_cnt'] = $_v['mg_count'];
  98. $_rdata[] = $_list;
  99. }
  100. if (empty($_rdata)) {
  101. $_rdata = null;
  102. }
  103. return [
  104. 'count' => $_count,
  105. 'list' => $_rdata
  106. ];
  107. }
  108. /**
  109. * 获取正在售卖的商品游戏
  110. *
  111. * @param int $mem_id 玩家ID
  112. * @param string $page 分页
  113. *
  114. * @return array
  115. */
  116. public function getSellGameList($mem_id = 0, $page = '1,10') {
  117. $_sell_app_ids = $this->getSellGames();
  118. if (!empty($mem_id)) {
  119. $_my_app_ids = $this->getGames($mem_id);
  120. $_app_ids = array_intersect($_sell_app_ids, $_my_app_ids);
  121. } else {
  122. $_app_ids = $_sell_app_ids;
  123. }
  124. $_map['id'] = ['in', $_app_ids];
  125. $_map['status'] = GameConst::GAME_STATUS_ON;
  126. $_game_model = (new GameModel());
  127. $_count = $_game_model->where($_map)->count();
  128. if ($_count <= 0) {
  129. return [
  130. 'count' => $_count,
  131. 'list' => null
  132. ];
  133. }
  134. $_datas = $_game_model->withCount(
  135. ['goods' => function ($query) {
  136. $query->where('status', AccountConst::STATUS_PULL_ON_SHELVES);
  137. }]
  138. )->page($page)->where($_map)->select();
  139. if (is_object($_datas)) {
  140. $_datas = $_datas->toArray();
  141. }
  142. $_rdata = [];
  143. $_list = [];
  144. foreach ($_datas as $_v) {
  145. $_list['game_id'] = $_v['id'];
  146. $_list['game_icon'] = $_v['icon'];
  147. $_list['gamename'] = $_v['name'];
  148. $_list['account_cnt'] = $_v['goods_count'];
  149. $_rdata[] = $_list;
  150. }
  151. if (empty($_rdata)) {
  152. $_rdata = null;
  153. }
  154. return [
  155. 'count' => $_count,
  156. 'list' => $_rdata
  157. ];
  158. }
  159. protected $base_field
  160. = [
  161. 'id',
  162. ];
  163. public function getWhere($param = []) {
  164. $_map = [];
  165. if (!empty($param['mem_id'])) {
  166. $_map['mem_id'] = $param['mem_id'];
  167. }
  168. if (!empty($param['app_id'])) {
  169. $_map['app_id'] = $param['app_id'];
  170. }
  171. return $_map;
  172. }
  173. public function getAccountList($where, $page = '1,10', $order = '-is_default') {
  174. $_map = $this->getWhere($where);
  175. $_field = [
  176. 'id' => 'mg_mem_id',
  177. 'nickname' => 'nickname',
  178. 'is_default' => 'is_default',
  179. 'create_time' => 'create_time',
  180. ];
  181. return $this->getList($_map, $_field, $page, $order);
  182. }
  183. public function getList($map, $_field = [], $page, $order) {
  184. $_where = $map;
  185. $_model = new MemGameModel();
  186. $_count = $_model->where($_where)->count();
  187. if (empty($_count)) {
  188. return [
  189. 'count' => 0,
  190. 'list' => []
  191. ];
  192. }
  193. $_field = array_merge($this->base_field, $_field);
  194. $_order = $_model->orderFilter($order);
  195. $_datas = $_model->field($_field)->where($_where)->order($_order)->page($page)->select();
  196. if (is_object($_datas)) {
  197. $_datas = $_datas->toArray();
  198. }
  199. if (empty($_datas)) {
  200. return [
  201. 'count' => $_count,
  202. 'list' => []
  203. ];
  204. }
  205. $_mem_game_cache = MemGameCache::ins();
  206. foreach ($_datas as $key => $data) {
  207. $_mem_game_cache_info = $_mem_game_cache->getInfoById($data['id']);
  208. $data['sum_money'] = isset($_mem_game_cache_info['sum_money'])
  209. ? $_mem_game_cache_info['sum_money'] : 0.00;
  210. /* 添加是否在售卖 */
  211. $data['is_sell'] = (new AccountGoodsModel())->isSell($data['mg_mem_id']);
  212. $_datas[$key] = $data;
  213. }
  214. return [
  215. 'count' => $_count,
  216. 'list' => $_datas
  217. ];
  218. }
  219. /**
  220. * 获取所有小号列表
  221. *
  222. * @param $param
  223. * @param string $page
  224. * @param string $order
  225. *
  226. * @return array
  227. * @throws \think\db\exception\DataNotFoundException
  228. * @throws \think\db\exception\ModelNotFoundException
  229. * @throws \think\exception\DbException
  230. */
  231. public function getAllAccountList($param, $page = '1,10', $order = '-create_time') {
  232. $_rdata = ['count' => 0, 'list' => []];
  233. $_map = [];
  234. if (!empty($param['username'])) {
  235. $_mem_ids = (new MemberLogic())->getIdsByUsername($param['username']);
  236. /*没有则返回空*/
  237. if (empty($_mem_ids)) {
  238. return $_rdata;
  239. }
  240. $_map['mem_id'] = ['in', $_mem_ids];
  241. }
  242. if (!empty($param['status'])) {
  243. $_map['status'] = $param['status'];
  244. }
  245. if (!empty($param['app_id'])) {
  246. $_map['app_id'] = $param['app_id'];
  247. }
  248. if (!empty($param['nickname'])) {
  249. $_map['nickname'] = $param['nickname'];
  250. }
  251. if (!empty($param['start_time']) && !empty($param['end_time'])) {
  252. $_map['create_time'] = ['between', [strtotime($param['start_time']), strtotime($param['end_time'])]];
  253. } else if (!empty($param['start_time'])) {
  254. $_map['create_time'] = ['gt', strtotime($param['start_time'])];
  255. } else if (!empty($param['end_time'])) {
  256. $_map['create_time'] = ['lt', CommonConst::CONST_DAY_SECONDS + strtotime($param['end_time'])];
  257. }
  258. $_field = [
  259. 'id' => 'id',
  260. 'app_id' => 'app_id',
  261. 'mem_id' => 'mem_id',
  262. 'nickname' => 'nickname',
  263. 'create_time' => 'create_time',
  264. 'update_time' => 'update_time',
  265. 'is_default' => 'is_default',
  266. 'status' => 'status',
  267. ];
  268. $_model = new MemGameModel();
  269. $_count = $_model->where($_map)->count();
  270. if (empty($_count)) {
  271. return $_rdata;
  272. }
  273. $_order = $_model->orderFilter($order);
  274. $_datas = $_model->field($_field)->with('mem,game2,pay')->where($_map)->order($_order)->page($page)->select();
  275. if (is_object($_datas)) {
  276. $_datas = $_datas->toArray();
  277. }
  278. if (empty($_datas)) {
  279. return $_rdata;
  280. }
  281. foreach ($_datas as $_key => $_data) {
  282. $_list[$_key]['me_mem_id'] = $_data['id'];
  283. $_list[$_key]['mem'] = !empty($_data['mem']) ? $_data['mem']['username'] : '';
  284. $_list[$_key]['gamename'] = !empty($_data['game2']) ? $_data['game2']['name'] : '';
  285. $_list[$_key]['sum'] = !empty($_data['pay']) ? $_data['pay'][0]['sum'] : 0;
  286. $_list[$_key]['nickname'] = $_data['nickname'];
  287. $_list[$_key]['create_time'] = $_data['create_time'];
  288. $_list[$_key]['update_time'] = $_data['update_time'];
  289. $_list[$_key]['status'] = $_data['status'];
  290. }
  291. return [
  292. 'count' => $_count,
  293. 'list' => $_list
  294. ];
  295. }
  296. /**
  297. * 切换游戏小号
  298. *
  299. * @param $mem_id
  300. * @param $app_id
  301. * @param $account_id
  302. *
  303. * @return bool
  304. */
  305. public function change($mem_id, $app_id, $account_id) {
  306. $_model = new MemGameModel();
  307. $_model->where('mem_id', '=', $mem_id)
  308. ->where('app_id', '=', $app_id)
  309. ->update(['is_default' => MemGameModel::IS_DEFAULT_FALSE]);
  310. $_model->where('id', '=', $account_id)
  311. ->update(['is_default' => MemGameModel::IS_DEFAULT_TRUE]);
  312. return true;
  313. }
  314. /**
  315. * 添加小号
  316. *
  317. * @param $data
  318. *
  319. * @return bool
  320. */
  321. public function add($data) {
  322. $_model = new MemGameModel();
  323. $_data = [
  324. 'mem_id' => isset($data['mem_id']) ? $data['mem_id'] : 0,
  325. 'app_id' => isset($data['app_id']) ? $data['app_id'] : 0,
  326. 'nickname' => isset($data['nickname']) ? $data['nickname'] : '',
  327. 'create_time' => time(),
  328. 'update_time' => time(),
  329. 'is_default' => MemGameModel::IS_DEFAULT_FALSE,
  330. ];
  331. if (false !== $_model->addData($_data)) {
  332. return true;
  333. }
  334. return false;
  335. }
  336. /**
  337. * 检查小号是否可以操作
  338. *
  339. * @param int $mem_id 玩家ID
  340. * @param int $app_id 游戏ID
  341. * @param int $mg_mem_id 小号ID
  342. *
  343. * @return bool|int
  344. */
  345. public function canOperate($mem_id, $app_id, $mg_mem_id) {
  346. $_model = new MemGameModel();
  347. $_account = $_model->getInfoById($mg_mem_id);
  348. if (empty($_account)) {
  349. return AccountStatus::ACCOUNT_NOT_EXISTS;
  350. }
  351. if ($_account['mem_id'] != $mem_id) {
  352. return AccountStatus::ACCOUNT_DOES_NOT_BELONG_MEM;
  353. }
  354. if ($_account['app_id'] != $app_id) {
  355. return AccountStatus::ACCOUNT_DOES_NOT_BELONG_GAME;
  356. }
  357. if ($_account['status'] == MemGameModel::STATUS_LOCKED) {
  358. return AccountStatus::ACCOUNT_LOCKED;
  359. }
  360. return true;
  361. }
  362. /**
  363. * 更新玩家小号信息
  364. *
  365. * @param $mg_mem_id
  366. * @param $mem_game_data
  367. *
  368. * @return bool
  369. */
  370. public function updateMemGame($mg_mem_id, $mem_game_data) {
  371. if (empty($mg_mem_id) || empty($mem_game_data)) {
  372. return false;
  373. }
  374. $_map['id'] = $mg_mem_id;
  375. $_rs = MemGameModel::update($mem_game_data, $_map, true);
  376. if (false === $_rs) {
  377. return false;
  378. }
  379. return true;
  380. }
  381. public function getInfoById($mem_game_id) {
  382. $_map['id'] = $mem_game_id;
  383. try {
  384. $_info = (new MemGameModel())->where($_map)->find();
  385. if (false === $_info) {
  386. return false;
  387. }
  388. if (is_object($_info)) {
  389. return $_info->toArray();
  390. } else {
  391. return $_info;
  392. }
  393. } catch (Exception $_e) {
  394. return false;
  395. }
  396. }
  397. }