GameDiscountV2.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * 游戏充值折扣比例
  4. */
  5. namespace app\admin\controller;
  6. use app\common\model\Game;
  7. use app\common\model\GamePayDiscount;
  8. use think\Db;
  9. use think\Loader;
  10. class GameDiscountV2 extends Admin
  11. {
  12. protected $gamePayDiscount;
  13. protected function _initialize()
  14. {
  15. parent::_initialize();
  16. $this->gamePayDiscount = new GamePayDiscount;
  17. }
  18. // 查
  19. public function index(){
  20. $query = $this->gamePayDiscount->alias('gpd')
  21. ->join("cy_game ng","gpd.game_id=ng.id", "left")
  22. ->field('gpd.*, ng.name,cooperation_status');
  23. // 游戏筛选
  24. $game_id = input('get.game_id', '');
  25. if (!empty($game_id)) {
  26. $gameIds = explode(',', $game_id);
  27. $query = $query->where('gpd.game_id', 'in', $gameIds);
  28. }
  29. // 折扣比例筛选
  30. $proportion = input('get.proportion', '');
  31. if (!empty($proportion)) {
  32. $proportions = explode(',', $proportion);
  33. $query = $query->where('gpd.proportion', 'in', $proportions);
  34. }
  35. $list = $query->order('id desc')
  36. ->paginate(10, false, ['query' => input('get.')]);
  37. // 获取游戏列表
  38. $gameList = Db::table('cy_game')->order('id desc')->field('name,id')->select();
  39. $gameArr = [];
  40. foreach ($gameList as $game) {
  41. $gameArr[] = [
  42. 'id' => $game['id'],
  43. 'name' => $game['name']
  44. ];
  45. }
  46. // 获取折扣列表(去重)
  47. $proportionList = Db::table('cy_game_pay_discount')
  48. ->field('proportion')
  49. ->group('proportion')
  50. ->order('proportion asc')
  51. ->select();
  52. $proportionArr = [];
  53. foreach ($proportionList as $item) {
  54. $proportionArr[] = [
  55. 'id' => $item['proportion'],
  56. 'name' => $item['proportion']
  57. ];
  58. }
  59. $this->assign('list', $list);
  60. $this->assign('page', $list->render());
  61. $this->assign('gameArr', $gameArr);
  62. $this->assign('proportionArr', $proportionArr);
  63. return $this->fetch();
  64. }
  65. // 增
  66. public function add(){
  67. if ($this->request->isPost()) {
  68. $data = $this->request->post();
  69. $data['proportion'] = trim($data['proportion']);
  70. if (true !== ($res = $this->validate($data, 'GameDiscountV2'))) {
  71. $this->error($res);
  72. }
  73. $appModel = model('Common/GamePayDiscount');
  74. $data['create_time'] = time();
  75. $data['update_time'] = time();
  76. if ($appModel->save($data)) {
  77. $gameName = Game::where('id', $data['game_id'])->value('name');
  78. $this->insertLog($this->current_node, "新增-游戏名:{$gameName},折扣比例:{$data['proportion']}", 15);
  79. $this->success('添加成功', url('index'));
  80. }
  81. $this->error($appModel->getError() ?: '添加失败');
  82. }
  83. $gameList = Db::table('cy_game')->order('id desc')->field('name,id')->select();
  84. $this->assign('game_list', $gameList);
  85. return $this->fetch();
  86. }
  87. // 删
  88. public function delete()
  89. {
  90. $id = $this->request->param('id', '', 'intval');
  91. if (empty($id)) {
  92. $this->error('参数错误!');
  93. }
  94. $appModel = model('Common/GamePayDiscount');
  95. if (!$appModel->find($id)) {
  96. $this->error('参数错误,不存在该数据');
  97. }
  98. $logInfo = GamePayDiscount::alias('gpd')->join('cy_game cg', 'gpd.game_id=cg.id', 'left')->field('gpd.id,cg.name, gpd.proportion')->where('gpd.id', $id)->find();
  99. if ($appModel->where('id', '=', $id)->delete()) {
  100. $this->insertLog($this->current_node, "删除-游戏名:{$logInfo['name']},折扣比例:{$logInfo['proportion']}, 改为:{$logInfo['proportion']}", 15);
  101. $this->success('删除成功', url('index'));
  102. }
  103. $error = $appModel->getError();
  104. $this->error($error ?: '删除失败');
  105. }
  106. // 改
  107. public function edit()
  108. {
  109. $id = $this->request->param('id', '', 'intval');
  110. $appModel = model('Common/GamePayDiscount');
  111. if (!$data = $appModel->find($id)) {
  112. $this->error('参数错误,不存在该数据');
  113. }
  114. if (empty($id)) {
  115. $this->error('参数错误!');
  116. }
  117. $logInfo = GamePayDiscount::alias('gpd')->join('cy_game cg', 'gpd.game_id=cg.id', 'left')->field('gpd.id,cg.name, gpd.proportion')->where('gpd.id', $id)->find();
  118. if ($this->request->isPost()) {
  119. $data = $this->request->post();
  120. $data['proportion'] = trim($data['proportion']);
  121. $data['update_time'] = time();
  122. $appKeyValidate = Loader::validate('GameDiscountV2');
  123. if (!$res = $appKeyValidate->check($data)) {
  124. $this->error($appKeyValidate->getError());
  125. }
  126. if ($appModel->update($data, ['id' => $id]) !== false) {
  127. $this->insertLog($this->current_node, "编辑-游戏名:{$logInfo['name']},折扣比例:{$logInfo['proportion']}, 改为:{$data['proportion']}", 15);
  128. $this->success('修改成功', url('index'));
  129. }
  130. $this->error($appModel->getError() ?: '修改失败');
  131. }
  132. $gameList = Db::table('cy_game')->order('id desc')->field('name,id')->select();
  133. $this->assign('game_list', $gameList);
  134. $this->assign('data', $data);
  135. return $this->fetch();
  136. }
  137. }