Award.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\admin\controller;
  3. use think\admin\Controller;
  4. use think\admin\extend\DataExtend;
  5. use think\admin\helper\QueryHelper;
  6. use think\admin\service\AdminService;
  7. use think\admin\service\MenuService;
  8. use think\admin\service\NodeService;
  9. /**
  10. * 奖品订单管理
  11. * Class Menu
  12. * @package app\admin\controller
  13. */
  14. class Award extends Controller
  15. {
  16. /**
  17. * 当前操作数据库
  18. * @var string
  19. */
  20. private $table = 'Order';
  21. /**
  22. * 排行榜前50名
  23. * @auth true
  24. * @menu true
  25. * @throws \think\db\exception\DataNotFoundException
  26. * @throws \think\db\exception\DbException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. */
  29. public function index()
  30. {
  31. $this->_query('UserTaskLog')->layTable(function (){
  32. $this->title = '排行榜前50名';
  33. $this->type = input('type', 'award');
  34. },function (QueryHelper $query){
  35. $query->alias('l')
  36. ->leftJoin('awards_order o', 'o.uid = l.uid')
  37. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  38. ->field(['o.id', 'l.uid', 'u.nickname', 'o.name', 'o.mobile', 'o.address', 'g.name as giftName', 'FROM_UNIXTIME(o.create_at) as createAt'])
  39. ->distinct('l.uid')
  40. ->order('l.number', 'desc')
  41. ->order('l.duration', 'asc')->limit(50);
  42. $query->equal('o.uid')->like('u.nickname');
  43. });
  44. }
  45. /**
  46. * 导出排行榜前50名
  47. * @auth true
  48. * @menu true
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public function award()
  54. {
  55. $data = $this->app->db->name('UserTaskLog')->alias('l')
  56. ->leftJoin('awards_order o', 'o.uid = l.uid')
  57. ->leftJoin('awards_user_info u', 'o.uid = u.uid')
  58. ->leftJoin('awards_gift g', 'o.gift_id = g.id')
  59. ->field(['o.id', 'l.uid', 'u.nickname', 'o.name', 'o.mobile', 'o.address', 'g.name as giftName', 'FROM_UNIXTIME(o.create_at) as createAt'])
  60. ->order('l.number', 'desc')
  61. ->order('l.duration', 'asc')->select();
  62. //实例化
  63. $objExcel = new \PHPExcel();
  64. //设置文档属性
  65. $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
  66. //设置内容
  67. $objActSheet = $objExcel->getActiveSheet();
  68. $letter = explode(',', "A,B,C,D,E,F,G");
  69. $arrHeader = ['微博UID', '微博昵称', '收件人姓名', '联系人电话', '收件地址', '奖品名称','中奖时间'];
  70. //填充表头信息
  71. $lenth = count($arrHeader);
  72. for ($i = 0; $i < $lenth; $i++) {
  73. $objActSheet->setCellValue("$letter[$i]1", "$arrHeader[$i]");
  74. };
  75. //填充表格信息
  76. foreach ($data as $k => $v) {
  77. $k += 2;
  78. //表格内容
  79. $objActSheet->setCellValue('A' . $k, $v['uid']." ");
  80. $objActSheet->setCellValue('B' . $k, $v['nickname']);
  81. $objActSheet->setCellValue('C' . $k, $v['name']);
  82. $objActSheet->setCellValue('D' . $k, $v['mobile']." ");
  83. $objActSheet->setCellValue('E' . $k, $v['address']);
  84. $objActSheet->setCellValue('F' . $k, $v['giftName']);
  85. $objActSheet->setCellValue('G' . $k, $v['createAt']);
  86. }
  87. $outfile = "中奖信息" . date('Ymd') . ".xlsx";
  88. ob_end_clean();
  89. header("Content-Type: application/force-download");
  90. header("Content-Type: application/octet-stream");
  91. header("Content-Type: application/download");
  92. header('Content-Disposition:inline;filename="' . $outfile . '"');
  93. header("Content-Transfer-Encoding: binary");
  94. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  95. header("Pragma: no-cache");
  96. $objWriter->save('php://output');
  97. }
  98. /**
  99. * 中奖信息
  100. * @auth true
  101. * @menu true
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. public function luckDraw()
  107. {
  108. $this->_query($this->table)->layTable(function (){
  109. $this->title = '中奖信息';
  110. $this->type = input('type', 'luckDraw');
  111. },function (QueryHelper $query){
  112. $query->alias('o')
  113. ->leftJoin('awards_user_info u', 'o.uid = u.uid')
  114. ->leftJoin('awards_gift g', 'o.gift_id = g.id')
  115. ->field(['o.id', 'u.uid', 'u.nickname', 'o.name', 'o.mobile', 'o.address', 'g.name as giftName', 'FROM_UNIXTIME(o.create_at) as createAt'])
  116. ->where('o.type', 1)
  117. ->order('o.id desc');
  118. $query->equal('o.uid')->like('u.nickname');
  119. });
  120. }
  121. /**
  122. * 导出中奖信息
  123. * @auth true
  124. * @menu true
  125. * @throws \think\db\exception\DataNotFoundException
  126. * @throws \think\db\exception\DbException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. */
  129. public function drawAward()
  130. {
  131. $data = $this->app->db->name('Order')->alias('o')
  132. ->leftJoin('awards_user_info u', 'o.uid = u.uid')
  133. ->leftJoin('awards_gift g', 'o.gift_id = g.id')
  134. ->field(['o.id', 'o.uid', 'u.nickname', 'o.name', 'o.mobile', 'o.address', 'g.name as giftName', 'FROM_UNIXTIME(o.create_at) as createAt'])
  135. ->where('o.type', 1)
  136. ->order('o.id desc')->select();
  137. //实例化
  138. $objExcel = new \PHPExcel();
  139. //设置文档属性
  140. $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
  141. //设置内容
  142. $objActSheet = $objExcel->getActiveSheet();
  143. $letter = explode(',', "A,B,C,D,E,F,G");
  144. $arrHeader = ['微博UID', '微博昵称', '收件人姓名', '联系人电话', '收件地址', '奖品名称','中奖时间'];
  145. //填充表头信息
  146. $lenth = count($arrHeader);
  147. for ($i = 0; $i < $lenth; $i++) {
  148. $objActSheet->setCellValue("$letter[$i]1", "$arrHeader[$i]");
  149. };
  150. //填充表格信息
  151. foreach ($data as $k => $v) {
  152. $k += 2;
  153. //表格内容
  154. $objActSheet->setCellValue('A' . $k, $v['uid']." ");
  155. $objActSheet->setCellValue('B' . $k, $v['nickname']);
  156. $objActSheet->setCellValue('C' . $k, $v['name']);
  157. $objActSheet->setCellValue('D' . $k, $v['mobile']." ");
  158. $objActSheet->setCellValue('E' . $k, $v['address']);
  159. $objActSheet->setCellValue('F' . $k, $v['giftName']);
  160. $objActSheet->setCellValue('G' . $k, $v['createAt']);
  161. }
  162. $outfile = "中奖信息" . date('Ymd') . ".xlsx";
  163. ob_end_clean();
  164. header("Content-Type: application/force-download");
  165. header("Content-Type: application/octet-stream");
  166. header("Content-Type: application/download");
  167. header('Content-Disposition:inline;filename="' . $outfile . '"');
  168. header("Content-Transfer-Encoding: binary");
  169. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  170. header("Pragma: no-cache");
  171. $objWriter->save('php://output');
  172. }
  173. }