assign('breadcrumb1','拼团活动'); $this->assign('breadcrumb2','拼团记录'); } /** * 列表页 * * */ public function index(Model $Model, Product $Product, RegimentActive $RegimentActive, Custom $Custom,Orders $Orders){ // 接收参数 $code = request('code',''); $name = request('name',''); $productName = request('product_name',''); $status = request('status',''); $start_time = request('start_time',''); $end_time = request('end_time',''); // 查询条件 $map = []; // 组合条件 if( $name ) $map[] = ['regiment_active.name','=',$name]; if( $code ){ $id = $RegimentActive->codeToId($code); $map[] = ['regiment_active.id','=',$id]; } if( $productName ) $map[] = ['product.name','=',$productName]; if( $status ) $map[] = ['regiment_record.status','=',$status]; if( $start_time ) { $start_time = strtotime($start_time); $map[] = ['regiment_record.insert_time','>=',$start_time]; } if( $end_time ) { $end_time = strtotime($end_time); $map[] = ['regiment_record.insert_time','<=',$end_time]; } // 当前登录的角色数据 $session = session('userRule') ? session('userRule') : ['business_id'=>0,'menu_type'=>0,'data_type'=>0]; // 是否存在对应的商业公司ID if ( $session['business_id'] ) $map[] = ['regiment_active.business_id','=',$session['business_id']]; // 数据类型 $shopIds = ($session['menu_type'] == 1 && $session['data_type'] == 2) ? Business::query()->where('leader_uid',$session['admin_uid'])->pluck('id')->toArray() : []; $select = [ 'regiment_record.*', 'regiment.status as regiment_status', 'regiment.custom_uid as regiment_uid', 'regiment_active.name as active_name', 'product.name as product_name', 'custom.username as username', ]; // 查询数据 $list = $Model->query() ->join('regiment','regiment.id','=','regiment_record.regiment_id') ->join('regiment_active','regiment_active.id','=','regiment_record.active_id') ->join('product','product.id','=','regiment_record.product_id') ->join('custom','custom.uid','=','regiment_record.custom_uid'); if (isset($shopIds)){ $list = $list->whereIn('regiment_active.business_id',$shopIds); } $list = $list->where($map) ->select($select) ->orderByDesc('id') ->paginate(config('page_num',10)); // 循环处理数据 foreach($list as &$v){ $v['product_code'] = $Product->idToCode($v['product_id']); $v['active_code'] = $RegimentActive->idToCode($v['active_id']); $v['custom_code'] = $Custom->idToCode($v['custom_uid']); $v['order_code'] = $Orders->idToCode($v['order_id']); $v['regiment_uid_code'] = $Custom->idToCode($v['regiment_uid']); $userInfo = $Custom->getOne($v['regiment_uid']); $v['regiment_username'] = $userInfo['username']; $v['business_name'] = Business::query()->where([['id','=',$v['business_id']]])->value('name'); } // 分配数据 $this->assign('empty', '~~暂无数据'); $this->assign('list',$list); // 加载模板 return $this->fetch(); } /** * 导出表格 * * */ public function down_excel(Model $Model, Product $Product, RegimentActive $RegimentActive, Custom $Custom,Orders $Orders){ // 接受参数 // 接收参数 $code = request('code',''); $name = request('name',''); $productName = request('product_name',''); $status = request('status',''); $start_time = request('start_time',''); $end_time = request('end_time',''); // 查询条件 $map = []; // 组合条件 if( $name ) $map[] = ['regiment_active.name','=',$name]; if( $code ){ $id = $RegimentActive->codeToId($code); $map[] = ['regiment_active.id','=',$id]; } if( $productName ) $map[] = ['product.name','=',$productName]; if( $status ) $map[] = ['regiment_record.status','=',$status]; if( $start_time ) { $start_time = strtotime($start_time); $map[] = ['regiment_record.insert_time','>=',$start_time]; } if( $end_time ) { $end_time = strtotime($end_time); $map[] = ['regiment_record.insert_time','<=',$end_time]; } $select = [ 'regiment_record.*', 'regiment.status as regiment_status', 'regiment.custom_uid as regiment_uid', 'regiment_active.name as active_name', 'product.name as product_name', 'custom.username as username', ]; // 查询数据 $list = $Model->query() ->join('regiment','regiment.id','=','regiment_record.regiment_id') ->join('regiment_active','regiment_active.id','=','regiment_record.active_id') ->join('product','product.id','=','regiment_record.product_id') ->join('custom','custom.uid','=','regiment_record.custom_uid') ->where($map) ->select($select) ->orderByDesc('id') ->paginate(config('page_num',10)); // 循环处理数据 foreach($list as &$v){ $v['product_code'] = $Product->idToCode($v['product_id']); $v['active_code'] = $RegimentActive->idToCode($v['active_id']); $v['custom_code'] = $Custom->idToCode($v['custom_uid']); $v['order_code'] = $Orders->idToCode($v['order_id']); $userInfo = $Custom->getOne($v['regiment_uid']); $v['regiment_username'] = $userInfo['username']; } try { // 去下载 $this->toDown($list); } catch (\Throwable $th) { echo $th->getMessage(); } } /** * 去下载 */ private function toDown($data){ // 创建新的电子表格对象 $spreadsheet = new Spreadsheet(); // 设置合并单元格的行和列,例如合并A1到B2的单元格 $sheet = $this->setStyle($spreadsheet); // 从第二行写入 $row = 2; // 循环写入 foreach ($data as $key => $value) { $statusName = ''; // 单元格内容写入 $sheet->setCellValue('A'.$row, $value['id']); $sheet->setCellValue('B'.$row, $value['regiment_id']); $sheet->setCellValue('C'.$row, $value['order_code']); $sheet->setCellValue('D'.$row, $value['username']); $sheet->setCellValue('E'.$row, $value['active_name']); $sheet->setCellValue('F'.$row, $value['product_name']); $sheet->setCellValue('G'.$row, $value['regiment_username']); switch ($value['status']) { case '0': $statusName = '拼团中'; break; case '1': $statusName = '取消拼团'; break; case '2': $statusName = '拼团成功'; break; case '3': $statusName = '拼团失败'; break; } $sheet->setCellValue('H'.$row, $statusName); $sheet->setCellValue('I'.$row, date('Y-m-d H:i:s',$value['insert_time'])); $row++; } // // 创建内容 $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); header('Pragma: public'); header('Content-type:application/vnd.ms-excel'); header('Content-Disposition: inline;filename=下载拼团记录.xlsx'); // 输出数据流 return $writer->save('php://output'); } /** * 设置表格样式 * */ private function setStyle(Spreadsheet $spreadsheet){ // 选择当前活动的工作表 $sheet = $spreadsheet->getActiveSheet(); // 宽 $sheet->getColumnDimension('A')->setWidth(15); $sheet->getColumnDimension('B')->setWidth(15); $sheet->getColumnDimension('C')->setWidth(15); $sheet->getColumnDimension('D')->setWidth(15); $sheet->getColumnDimension('E')->setWidth(15); $sheet->getColumnDimension('F')->setWidth(15); $sheet->getColumnDimension('G')->setWidth(15); $sheet->getColumnDimension('H')->setWidth(15); $sheet->getColumnDimension('I')->setWidth(15); // 默认高度 $sheet->getDefaultRowDimension()->setRowHeight(18); // 加粗第一行 $sheet->getStyle('A:S')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER); $sheet->getStyle('A1:S1')->getFont()->setBold(true); $sheet->getStyle('A1:S1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('FF00FF00'); // ARGB颜色代码,例如绿色 // 设置表格标题 $sheet ->setCellValue('A1', '拼团记录ID') ->setCellValue('B1', '团单ID') ->setCellValue('C1', '订单编码') ->setCellValue('D1', '客户昵称') ->setCellValue('E1', '参与活动') ->setCellValue('F1', '产品名称') ->setCellValue('G1', '发起客户昵称') ->setCellValue('H1', '拼团状态') ->setCellValue('I1', '订单生成时间'); // 返回结果 return $sheet; } }