LotteryOrderRecord.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Lottery\OrderReward as Request;
  3. use App\Models\Custom;
  4. use App\Models\Lottery\OrderRecord as Model;
  5. use App\Models\Lottery\Order as LotteryOrder;
  6. use Illuminate\Support\Facades\DB;
  7. /**
  8. * 抽奖记录
  9. *
  10. * @author 刘相欣
  11. *
  12. */
  13. class LotteryOrderRecord extends Auth{
  14. protected function _initialize(){
  15. parent::_initialize();
  16. $this->assign('breadcrumb1','下单抽奖');
  17. $this->assign('breadcrumb2','抽奖记录');
  18. }
  19. /**
  20. * 列表页
  21. *
  22. * */
  23. public function index(Model $Model,LotteryOrder $LotteryOrder,Custom $Custom){
  24. // 接受参数
  25. $lotteryId = request('lottery_id',0);
  26. $customCode = request('custom_code','');
  27. $rewardName = request('reward_name','');
  28. $startTime = request('start_time','');
  29. $endTime = request('end_time','');
  30. $status = request('status');
  31. // 查询条件
  32. $map = [];
  33. // 编码转ID
  34. $customUid = $customCode ? $Custom->codeToId($customCode) : 0;
  35. if( $customUid ) $map[] = ['lottery_order_record.custom_uid','=',$customUid];
  36. if( $lotteryId ) $map[] = ['lottery_order_record.lottery_id','=',$lotteryId];
  37. if( $rewardName ) $map[] = ['lottery_order_record.reward_name','=',$rewardName];
  38. if( $startTime ) $map[] = ['lottery_order_record.insert_time','>=',strtotime($startTime)];
  39. if( $endTime ) $map[] = ['lottery_order_record.insert_time','<=',strtotime($endTime)];
  40. if( !is_null($status) ) $map[] = ['lottery_order_record.status','=',$status];
  41. // 查询数据
  42. $list = $Model->query()
  43. ->join('lottery_order','lottery_order.id','=','lottery_order_record.lottery_id')
  44. ->join('custom','custom.uid','=','lottery_order_record.custom_uid')
  45. ->where($map)
  46. ->select(['lottery_order_record.*','custom.weiban_extid','custom.username','lottery_order.name as active_name'])
  47. ->orderByDesc('id')
  48. ->paginate(config('page_num',10));
  49. // 循环处理数据
  50. foreach ($list as $key => $value) {
  51. // 组合数据
  52. $value['state'] = $Model->getRecordState($value['status'],'name');
  53. // id转编号
  54. $value['custom_code'] = $Custom->idToCode($value['custom_uid']);
  55. // 重组
  56. $list[$key] = $value;
  57. }
  58. // 获取列表
  59. $statusList = $Model->getRecordStateList();
  60. // 获取列表
  61. $lotteryList = $LotteryOrder->query()->get(['id','name'])->toArray();
  62. // 分配数据
  63. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  64. $this->assign('list',$list);
  65. $this->assign('statusList',$statusList);
  66. $this->assign('lotteryList',$lotteryList);
  67. // 加载模板
  68. return $this->fetch();
  69. }
  70. /**
  71. * 修改状态
  72. *
  73. * */
  74. public function set_status(Request $request,Model $Model){
  75. // 验证参数
  76. $request->scene('set_status')->validate();
  77. // 设置状态
  78. $id = request('id',0);
  79. $status = request('status',0);
  80. // 查询用户
  81. $oldData = $Model->where(['id'=>$id])->first();
  82. // 如果用户不存在
  83. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  84. // 执行修改
  85. $result = $Model->edit($id,['status'=>$status]);
  86. // 提示新增失败
  87. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  88. // 记录行为
  89. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  90. // 告知结果
  91. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  92. }
  93. /**
  94. * 导出表格
  95. *
  96. * */
  97. public function down_excel(Model $Model,Custom $Custom){
  98. // 接受参数
  99. $lotteryId = request('lottery_id',0);
  100. $customCode = request('custom_code','');
  101. $startTime = request('start_time','');
  102. $endTime = request('end_time','');
  103. $status = request('status');
  104. // 查询条件
  105. $map = [];
  106. // 编码转ID
  107. $customUid = $customCode ? $Custom->codeToId($customCode) : 0;
  108. if( $customUid ) $map[] = ['lottery_order_record.custom_uid','=',$customUid];
  109. if( $lotteryId ) $map[] = ['lottery_order_record.lottery_id','=',$lotteryId];
  110. if( $startTime ) $map[] = ['lottery_order_record.insert_time','>=',strtotime($startTime)];
  111. if( $endTime ) $map[] = ['lottery_order_record.insert_time','<=',strtotime($endTime)];
  112. if( !is_null($status) ) $map[] = ['lottery_order_record.status','=',$status];
  113. // 查询数据
  114. $list = $Model->query()
  115. ->join('lottery_order','lottery_order.id','=','lottery_order_record.lottery_id')
  116. ->join('custom','custom.uid','=','lottery_order_record.custom_uid')
  117. ->where($map)
  118. ->select([
  119. 'lottery_order_record.id',
  120. 'lottery_order.id as active_id',
  121. 'lottery_order.name as active_name',
  122. 'custom.uid as custom_uid',
  123. 'custom.username',
  124. 'lottery_order_record.reward_name',
  125. 'lottery_order_record.status',
  126. 'lottery_order_record.contact_name',
  127. 'lottery_order_record.contact_phone',
  128. 'lottery_order_record.contact_province',
  129. 'lottery_order_record.contact_city',
  130. 'lottery_order_record.contact_area',
  131. 'lottery_order_record.contact_addr',
  132. 'lottery_order_record.insert_time',
  133. 'custom.weiban_extid',
  134. ])
  135. ->orderByDesc('id')->get()->toArray();
  136. // 循环处理数据
  137. foreach ($list as $key => $value) {
  138. // 组合数据
  139. $value['status'] = $Model->getRecordState($value['status'],'name');
  140. $value['custom_uid'] = $Custom->idToCode($value['custom_uid']);
  141. $value['contact_addr'] = $value['contact_phone'] ? $value['contact_province'].'/'.$value['contact_city'].'/'.$value['contact_area'].'/'.$value['contact_addr'].' '.$value['contact_name'].' '.$value['contact_phone'] : '';
  142. $value['username'] = hide_phone($value['username']);
  143. $value['insert_time'] = date('Y-m-d H:i:s',$value['insert_time']);
  144. // 删除数据
  145. unset($value['contact_province'],$value['contact_city'],$value['contact_area'],$value['contact_name'],$value['contact_phone']);
  146. // 重组
  147. $list[$key] = $value;
  148. }
  149. // 去下载
  150. $this->toDown($list);
  151. }
  152. /**
  153. * 去下载
  154. */
  155. private function toDown($data){
  156. // xlsx文件保存路径
  157. $excel = new \Vtiful\Kernel\Excel(['path' =>public_path().'/uploads/']);
  158. $filePath = $excel->fileName('tutorial01.xlsx', 'sheet1')->header(['记录ID','活动ID','活动名称','客户编码','客户昵称','奖品名称','收货信息','中奖时间','微伴ID'])->data($data)->output();
  159. header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  160. header('Content-Disposition: attachment;filename="下单抽奖中奖记录'.date('Y-m-d His').'.xlsx"');
  161. header('Content-Length: ' . filesize($filePath));
  162. header('Content-Transfer-Encoding: binary');
  163. header('Cache-Control: must-revalidate');
  164. header('Cache-Control: max-age=0');
  165. header('Pragma: public');
  166. ob_clean();
  167. flush();
  168. // 输出文件,成功删除文件路径
  169. if ( copy($filePath, 'php://output') ) @unlink($filePath);
  170. }
  171. }