Gift.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. ->field('*,count(gift.id) as gift_number')
  49. ->with(['eggjackpot'])
  50. ->where($where)
  51. ->order($sort, $order)
  52. ->group('gift.Jackpot_id,gift.gift_id')
  53. ->paginate();
  54. $totalMoney = $this->model->with(['eggjackpot'])->where($where)->sum('price');
  55. foreach ($list as $row) {
  56. $row->getRelation('eggjackpot')->visible(['name','type']);
  57. }
  58. $result = array("total" => $list->total(), "rows" => $list->items(), "extend" => ['total_price' => $totalMoney]);
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 添加
  65. */
  66. public function add()
  67. {
  68. if ($this->request->isPost()) {
  69. $gift_id = $this->request->post('gift_id');
  70. $jackpot_id = $this->request->post('jackpot_id');
  71. $number = $this->request->post('number');
  72. $gift_info = Db::name('gift')->where('id',$gift_id)->find();
  73. if(empty($gift_info)){
  74. $this->error('不存在的礼物');
  75. }
  76. $jackpot_info = Db::name('egg_jackpot')->where('id',$jackpot_id)->find();
  77. $data_all = [];
  78. for($i=1;$i<=$number;$i++){
  79. $one = [
  80. 'gift_id' => $gift_id,
  81. 'image' => $gift_info['image'],
  82. 'special' => $gift_info['special'],
  83. 'gift_name' => $gift_info['name'],
  84. 'Jackpot_id' => $jackpot_id,
  85. 'prize_no' => 0,
  86. 'price' => $gift_info['price'],
  87. 'is_use' => 0,
  88. 'starttime' => $jackpot_info['starttime'],
  89. ];
  90. $data_all[] = $one;
  91. }
  92. Db::startTrans();
  93. $rs = Db::name('egg_gift')->insertAll($data_all);
  94. Db::commit();
  95. $this->success('添加完成');
  96. }
  97. return $this->view->fetch();
  98. }
  99. /**
  100. * 编辑
  101. */
  102. public function newedit(){
  103. $gift_id = input('gift_id');
  104. $jackpot_id = input('jackpot_id');
  105. $info = Db::name('egg_gift')->field('*,count(id) as number')->where('gift_id',$gift_id)->where('Jackpot_id',$jackpot_id)->find();
  106. if ($this->request->isPost()) {
  107. $number = $this->request->post('number',0,'intval');
  108. if($number <= 0){
  109. $this->error('请填写正确的数量');
  110. }
  111. $gift_info = Db::name('gift')->where('id',$gift_id)->find();
  112. if(empty($gift_info)){
  113. $this->error('不存在的礼物');
  114. }
  115. $jackpot_info = Db::name('egg_jackpot')->where('id',$jackpot_id)->find();
  116. if(empty($jackpot_info)){
  117. $this->error('不存在的奖池');
  118. }
  119. $data_all = [];
  120. for($i=1;$i<=$number;$i++){
  121. $one = [
  122. 'gift_id' => $gift_id,
  123. 'image' => $gift_info['image'],
  124. 'special' => $gift_info['special'],
  125. 'gift_name' => $gift_info['name'],
  126. 'Jackpot_id' => $jackpot_id,
  127. 'prize_no' => 0,
  128. 'price' => $gift_info['price'],
  129. 'is_use' => 0,
  130. 'starttime' => $jackpot_info['starttime'],
  131. ];
  132. $data_all[] = $one;
  133. }
  134. Db::startTrans();
  135. //全删
  136. $info = Db::name('egg_gift')->where('gift_id',$gift_id)->where('Jackpot_id',$jackpot_id)->delete();
  137. if(!$info){
  138. Db::rollback();
  139. $this->error('操作失败');
  140. }
  141. //重新写入
  142. $rs = Db::name('egg_gift')->insertAll($data_all);
  143. if(!$rs){
  144. Db::rollback();
  145. $this->error('操作失败');
  146. }
  147. Db::commit();
  148. $this->success('修改完成');
  149. }
  150. $this->view->assign('row',$info);
  151. return $this->view->fetch();
  152. }
  153. /**
  154. * 删除
  155. */
  156. public function newdel(){
  157. $gift_id = input('gift_id');
  158. $jackpot_id = input('jackpot_id');
  159. $info = Db::name('egg_gift')->where('gift_id',$gift_id)->where('Jackpot_id',$jackpot_id)->delete();
  160. $this->success('删除完成');
  161. }
  162. }