Gift.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\admin\controller\egg;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 奖池礼物管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Gift extends Backend
  11. {
  12. /**
  13. * Gift模型对象
  14. * @var \app\admin\model\egg\Gift
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\egg\Gift;
  21. $this->view->assign("isUseList", $this->model->getIsUseList());
  22. }
  23. public function import()
  24. {
  25. parent::import();
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //当前是否为关联查询
  38. $this->relationSearch = true;
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if ($this->request->isAjax()) {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  47. $list = $this->model
  48. ->with(['eggjackpot'])
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. $totalMoney = $this->model->with(['eggjackpot'])->where($where)->sum('price');
  53. foreach ($list as $row) {
  54. $row->getRelation('eggjackpot')->visible(['name','type']);
  55. }
  56. $result = array("total" => $list->total(), "rows" => $list->items(), "extend" => ['total_price' => $totalMoney]);
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. /**
  62. * 添加
  63. */
  64. public function add()
  65. {
  66. if ($this->request->isPost()) {
  67. $gift_id = $this->request->post('gift_id');
  68. $jackpot_id = $this->request->post('jackpot_id');
  69. $number = $this->request->post('number');
  70. $gift_info = Db::name('gift')->where('id',$gift_id)->find();
  71. if(empty($gift_info)){
  72. $this->error('不存在的礼物');
  73. }
  74. $data_all = [];
  75. for($i=1;$i<=$number;$i++){
  76. $one = [
  77. 'gift_id' => $gift_id,
  78. 'image' => $gift_info['image'],
  79. 'special' => $gift_info['special'],
  80. 'gift_name' => $gift_info['name'],
  81. 'Jackpot_id' => $jackpot_id,
  82. 'prize_no' => 0,
  83. 'price' => $gift_info['price'],
  84. 'is_use' => 0,
  85. ];
  86. $data_all[] = $one;
  87. }
  88. Db::startTrans();
  89. $rs = Db::name('egg_gift')->insertAll($data_all);
  90. Db::commit();
  91. $this->success('添加完成');
  92. }
  93. return $this->view->fetch();
  94. }
  95. }