WelfareResources.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\library\OneFileUpload;
  4. use app\common\logic\Member as MemberService;
  5. use think\Loader;
  6. use think\Db;
  7. class WelfareResources extends Admin
  8. {
  9. protected $gameList;
  10. public function _initialize()
  11. {
  12. parent::_initialize(); // TODO: Change the autogenerated stub
  13. }
  14. /**
  15. *礼包资源列表
  16. */
  17. public function index()
  18. {
  19. $where = [];
  20. $order = 'create_time desc';
  21. //查询参数
  22. $name = input('name', '', 'trim');
  23. $game_id = input('game_id', '', 'trim');
  24. if (isset($name) && !empty($name)) {
  25. $where['a.name'] = ['like', "%$name%"];
  26. }
  27. $tmpGameList = model('Common/Game')->getAllByCondition('id,name',['is_welfare'=>2]);
  28. $gameList = array();
  29. foreach ($tmpGameList as $game) {
  30. $gameList[$game['id']] = $game;
  31. }
  32. cache('welfarweResources:index',input(),3600);
  33. $resourcesModel = model('WelfareResources');
  34. $list = $resourcesModel->alias('a')
  35. ->where($where)
  36. ->where(function ($query) use ($game_id) {
  37. if (isset($game_id) && !empty($game_id)) {
  38. $query->whereRaw(sprintf('FIND_IN_SET(%s,game_id)', $game_id));
  39. }
  40. })
  41. ->order($order)
  42. ->paginate(10, false, ['query' => input('get.')])->each(function ($item, $key) use($gameList) {
  43. if ($item['type_id'] == 2) {
  44. $item['surplus'] = model('WelfareResourcesData')->where(['welfare_resources_id' => $item['id']])->where('status',1)->count();
  45. } else {
  46. $item['total'] = $item['surplus'] = '--';
  47. }
  48. if($item['game_id']){
  49. $name = [];
  50. foreach (explode(',',$item['game_id']) as $k=>$v){
  51. $name[] = $gameList[$v]['name'];
  52. }
  53. $item['game_name'] = implode(',',$name);
  54. }else{
  55. $item['game_name'] = '';
  56. }
  57. return $item;
  58. });
  59. $tmpSelfGameList = model('Common/Game')->getAllByCondition('id,name', ['game_kind' => 1, 'cooperation_status' => ['neq', 0], 'is_welfare' => 2], 'id desc', 'self');
  60. $selfGameList = [['id' => 0, 'name' => '']];
  61. foreach ($tmpSelfGameList as $game) {
  62. $selfGameList[$game['id']] = $game;
  63. }
  64. $this->assign('game_list', $selfGameList);
  65. $this->assign('list', $list);
  66. $this->assign('page', $list->render());
  67. return $this->fetch();
  68. }
  69. /**
  70. *增
  71. */
  72. public function add()
  73. {
  74. if ($this->request->isPost()) {
  75. $data = $this->request->post();
  76. if(!$data['select']){
  77. $this->error('请选择游戏');
  78. }
  79. $data['game_id'] = $data['select'];
  80. unset($data['select']);
  81. $data['update_time'] = $data['create_time'] = time();
  82. $m = Db::name('cy_welfare_resources')->insertGetId($data);
  83. if (!empty($m)) {
  84. $this->success('添加成功', url('index',cache('welfarweResources:index')));
  85. }
  86. $this->error('添加失败');
  87. }
  88. $gameList = model('Common/Game')->getAllByCondition('id,name',['is_welfare'=>2]);
  89. $this->assign('game_list', $gameList);
  90. return $this->fetch();
  91. }
  92. /**
  93. *删
  94. */
  95. public function delete()
  96. {
  97. $id = $this->request->param('id', '', 'intval');
  98. if (empty($id)) {
  99. $this->error('参数错误!');
  100. }
  101. $resourcesModel = Db::name('cy_welfare_resources');
  102. if (!$resourcesModel->find($id)) {
  103. $this->error('参数错误,不存在该数据');
  104. }
  105. if ($resourcesModel->where('id', '=', $id)->delete()) {
  106. $this->success('删除成功', url('index'));
  107. }
  108. $error = $resourcesModel->getError();
  109. $this->error($error ?: '删除失败');
  110. }
  111. /**
  112. *编辑SDK密钥
  113. */
  114. public function edit()
  115. {
  116. $id = $this->request->param('id', '', 'intval');
  117. $appModel = Db::name('cy_welfare_resources');
  118. if (!$data = $appModel->find($id)) {
  119. $this->error('参数错误,不存在该数据');
  120. }
  121. if (empty($id)) {
  122. $this->error('参数错误!');
  123. }
  124. if ($this->request->isPost()) {
  125. $data = $this->request->post();
  126. if(!$data['select']){
  127. $this->error('请选择游戏');
  128. }
  129. $data['game_id'] = $data['select'];
  130. unset($data['select']);
  131. $data['update_time'] = time();
  132. if (Db::name('cy_welfare_resources')->where(['id' => $id])->update($data)) {
  133. $this->success('修改成功', url('index',cache('welfarweResources:index')));
  134. } else {
  135. $this->error($appModel->getError() ?: '修改失败');
  136. }
  137. }
  138. $gameList = model('Common/Game')->getAllByCondition('id,name',['is_welfare'=>2]);
  139. $this->assign('game_list', $gameList);
  140. $this->assign('data', $data);
  141. return $this->fetch();
  142. }
  143. /**
  144. * 上传txt文件
  145. */
  146. public function uplode()
  147. {
  148. $id = $this->request->param('id', '', 'intval');
  149. if (empty($id)) {
  150. $this->error('参数错误!');
  151. }
  152. $resourcesModel = Db::name('cy_welfare_resources');
  153. if (!$data = $resourcesModel->find($id)) {
  154. $this->error('参数错误,不存在该数据');
  155. }
  156. if ($data['type_id'] != 2) {
  157. $this->error('参数错误,不存在该数据');
  158. }
  159. $resources_data = Db::name('cy_welfare_resources_data')->where(['welfare_resources_id' => $data['id'], 'status' => 1])->column('code', 'id');
  160. $this->assign('code', implode("\r\n", $resources_data));
  161. if ($this->request->isPost()) {
  162. $data = $this->request->post();
  163. $code = $this->request->param('code');
  164. empty($code) ? $this->error('礼包码不能为空') : true;
  165. $codeArray = explode("\r\n", $code);
  166. $codeArray = array_map('filterAndTrimInput', $codeArray);
  167. $codeArray = array_unique(array_filter($codeArray));
  168. Db::startTrans();
  169. try {
  170. model('WelfareResourcesData')->where(['welfare_resources_id' => $id, 'status' => 1])->delete();
  171. $codes = [];
  172. foreach ($codeArray as $v) {
  173. $codes[] = ['code' => $v, 'welfare_resources_id' => $id];
  174. }
  175. $res = model('WelfareResourcesData')->saveAll($codes);
  176. $num = model('WelfareResourcesData')->where(['welfare_resources_id' => $id])->count();
  177. $update['update_time'] = time();
  178. $update['total'] = $num;
  179. if (Db::name('cy_welfare_resources')->where(['id' => $id])->update($update)) {
  180. // 提交事务
  181. Db::commit();
  182. } else {
  183. // 回滚事务
  184. Db::rollback();
  185. $this->error('修改失败');
  186. }
  187. } catch (\Exception $e) {
  188. // 回滚事务
  189. Db::rollback();
  190. $this->error('修改失败' . $e->getMessage());
  191. }
  192. $this->success('修改成功', url('index'));
  193. }
  194. $this->assign('data', $data);
  195. return $this->fetch();
  196. }
  197. }