DrawRecord.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace app\admin\controller\lottery;
  3. use app\common\controller\Backend;
  4. /**
  5. * 抽奖记录管理
  6. *
  7. * @icon fa fa-list
  8. */
  9. class DrawRecord extends Backend
  10. {
  11. /**
  12. * DrawRecord模型对象
  13. * @var \app\admin\model\lottery\DrawRecord
  14. */
  15. protected $model = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = new \app\admin\model\lottery\DrawRecord;
  20. // 获取枚举值列表
  21. $this->view->assign("triggerTypeList", $this->model->getTriggerTypeList());
  22. }
  23. /**
  24. * 查看
  25. */
  26. public function index()
  27. {
  28. //当前是否为关联查询
  29. $this->relationSearch = true;
  30. //设置过滤方法
  31. $this->request->filter(['strip_tags', 'trim']);
  32. if ($this->request->isAjax()) {
  33. //如果发送的来源是Selectpage,则转发到Selectpage
  34. if ($this->request->request('keyField')) {
  35. return $this->selectpage();
  36. }
  37. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  38. $list = $this->model
  39. ->with(['activity', 'user', 'prize'])
  40. ->where($where)
  41. ->order($sort, $order)
  42. ->paginate($limit);
  43. foreach ($list as $row) {
  44. $row->visible(['id','activity_id','user_id','prize_id','is_win','trigger_type','trigger_amount','draw_time','createtime']);
  45. $row->visible(['activity', 'user', 'prize']);
  46. // 处理关联数据(belongsTo关系)
  47. if ($row->getRelation('activity')) {
  48. $row->getRelation('activity')->visible(['name']);
  49. }
  50. if ($row->getRelation('user')) {
  51. $row->getRelation('user')->visible(['nickname']);
  52. }
  53. if ($row->getRelation('prize')) {
  54. $row->getRelation('prize')->visible(['name', 'type']);
  55. }
  56. }
  57. $result = array("total" => $list->total(), "rows" => $list->items());
  58. return json($result);
  59. }
  60. return $this->view->fetch();
  61. }
  62. /**
  63. * 详情
  64. */
  65. public function detail($ids = null)
  66. {
  67. $row = $this->model->with(['activity', 'user', 'prize', 'winRecord'])->get($ids);
  68. if (!$row) {
  69. $this->error(__('No Results were found'));
  70. }
  71. $this->view->assign("row", $row);
  72. return $this->view->fetch();
  73. }
  74. /**
  75. * 导出记录
  76. */
  77. public function export()
  78. {
  79. // 获取导出参数
  80. $filter = $this->request->get('filter', '');
  81. $where = [];
  82. if ($filter) {
  83. $where = json_decode($filter, true);
  84. }
  85. $list = $this->model->with(['activity', 'user', 'prize'])
  86. ->where($where)
  87. ->order('createtime', 'desc')
  88. ->select();
  89. $header = ['ID', '活动名称', '用户昵称', '奖品名称', '是否中奖', '触发类型', '触发金额', '抽奖时间'];
  90. $data = [];
  91. foreach ($list as $item) {
  92. $data[] = [
  93. $item->id,
  94. $item->activity->name ?? '',
  95. $item->user->nickname ?? '',
  96. $item->prize->name ?? '',
  97. $item->is_win ? '是' : '否',
  98. $item->trigger_type_text,
  99. $item->trigger_amount,
  100. date('Y-m-d H:i:s', $item->draw_time)
  101. ];
  102. }
  103. // 这里可以集成Excel导出功能
  104. $this->success('导出成功', '', $data);
  105. }
  106. }