function ProductListExport(){
$user_id=$this->request->uid;
$USER=getAdminInfo($user_id);
if(!array_intersect(explode(",",$USER['role_id']),[1,2,3,4])){
return $this->ajaxReturn($this->errorCode, '你没有权限访问,如有需要请联系管理员!');
}
$postField = 'pdid';
$req = $this->request->only(explode(',',$postField),'post',null);
$product_ids =array_column($req['pdid'],'product_id');
$priceheads =array_column($req['pdid'],'pricehead');
$pricehead = array_pop($priceheads);
$pricehead = $pricehead == '' ?'价格':$pricehead;
$product_prices =array_column($req['pdid'],'price','product_id');
if($req['pdid']){
$list=db('product')->alias('a')
->leftjoin('cd_brand b','a.brand_id=b.brand_id')
->leftjoin('cd_product_company_link c','a.product_id=c.product_id and c.company_id=1')
->leftjoin('cd_user d','c.buyer_id=d.user_id')
->field('a.product_id,a.l_name_zh as pdname,a.l_name_en,a.l_model_zh,a.engines,a.frey_no,a.number,b.l_name_zh,d.name')
->where('a.product_id','in',$product_ids)
->order('product_id')
->select()
->toArray();
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load(root_path() . 'public/tmpxls/产品列表导出目录.xls');
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->setTitle('Product catalog');
$count=count($list);
$worksheet->insertNewRowBefore(3,$count-2);
$startline=2;
$path = root_path() . 'public/tmpxls/pic/';
@mkdir($path);
$worksheet->getCell('L1')->setValue($pricehead);
foreach($list as $k=>$v){
$kc = \app\api\service\Crm\ProductService::productstock($v['product_id']);
$line=$k+$startline;
if (file_exists( $path. $v['frey_no'] . '.png') === false) {
$product_img = \app\api\unit\Common::PIC_URL_HTTP . '?f=37/' . \frey\FreyImage::encrypt($v['frey_no']).'&n=0&u=200&c=1';
copy($product_img, $path . $v['frey_no'] . '.png');
}
if(file_exists($path.$v['frey_no'].'.png')===true){
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName($v['frey_no']);
$drawing->setDescription($v['frey_no']);
$drawing->setPath(root_path().'public/tmpxls/pic/'.$v['frey_no'].'.png');
$drawing->setWidth(80);
$drawing->setHeight(80);
$drawing->setCoordinates('A'.$line);
$drawing->setOffsetX(13);
$drawing->setOffsetY(6);
$drawing->setWorksheet($spreadsheet->getActiveSheet());
}
$worksheet->getCell('B'.$line)->setValue($v['pdname']);
$worksheet->getCell('C'.$line)->setValue($v['l_name_en']);
$worksheet->getCell('D'.$line)->setValue($v['l_model_zh']);
$worksheet->getCell('E'.$line)->setValue($v['engines']);
$worksheet->getCell('F'.$line)->setValue($v['l_name_zh']);
$worksheet->getCell('G'.$line)->setValue($v['frey_no']);
$worksheet->getCell('H'.$line)->setValue($v['number']);
$worksheet->getCell('I'.$line)->setValue($kc['kykc']);
$worksheet->getCell('J'.$line)->setValue($kc['ZTQTY']);
$worksheet->getCell('K'.$line)->setValue($v['name']);
$price = $product_prices[$v['product_id']] ??0;
$worksheet->getCell('L'.$line)->setValue($price);
}
$filename = date("YmdHis").'产品目录';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename='.$filename.'.Xlsx');
header('Cache-Control: max-age=0');
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
return $writer->save('php://output');
}else{
return $this->ajaxReturn($this->errorCode, '参数错误!');
}
}
```