CsController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\admin\classes\AdminRoleClass;
  4. use bizGhs\item\classes\ItemClassClass;
  5. use bizGhs\merchant\classes\ShopClass;
  6. use bizGhs\product\classes\ProductClass;
  7. use bizGhs\product\models\Product;
  8. use bizGhs\product\services\ProductService;
  9. use common\components\stringUtil;
  10. use common\components\util;
  11. use Yii;
  12. class CsController extends PublicController
  13. {
  14. public $sjId = 12568, $shopId = 214;
  15. public function actionItem()
  16. {
  17. $shopId = 214;
  18. if (getenv('YII_ENV', 'local') != 'production') {
  19. $shopId = 36225;
  20. $this->shopId = 36225;
  21. $this->sjId = 12715;
  22. }
  23. $py = Yii::$app->request->get('py', '');
  24. $type = Yii::$app->request->get('type', '');
  25. //默认只显示上架的商品
  26. $status = Yii::$app->request->get('status', 1);
  27. //获取所有当前供应商的分类 (全部、常用)
  28. //根据分类组装分类下的花材信息
  29. $classIds = ItemClassClass::getGhsItemClassAll($this->sjId);
  30. $where['sjId'] = $this->sjId;
  31. if ($py) {
  32. $pyName = strtolower($py);
  33. $where['py'] = $pyName;
  34. }
  35. $where['shopId'] = $shopId;
  36. if (!empty($status)) {
  37. $where['status'] = $status;
  38. }
  39. $where['delStatus'] = 0;
  40. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  41. $itemInfoData = ProductClass::groupProductInfo($itemInfoData);
  42. //常用的itemIds
  43. $oftenItemIds = [];
  44. //$oftenItemIds = ProductClass::getOftenItemIds($shopId);
  45. //获取供应商下所有花材ID (itemIds)
  46. $itemIdsInfo = ProductClass::getGhsItemIds($this->sjId);
  47. //dd($itemInfoData);
  48. //组装数据: [{classId:'','className:'',child:[{itemInfoData}]}]
  49. $data = ProductService::assembleData($classIds, $itemIdsInfo, $itemInfoData, $oftenItemIds, $type);
  50. $formatData = [];
  51. foreach ($data as $key => $class) {
  52. $name = $class['className'] ?? '';
  53. $formatData[] = ['name' => $name, 'price' => ''];
  54. if (isset($class['child']) && !empty($class['child'])) {
  55. foreach ($class['child'] as $cKey => $child) {
  56. $cName = $child['name'] ?? '';
  57. $price = $child['price'] ?? '';
  58. $formatData[] = [
  59. 'name' => $cName,
  60. 'price' => $price,
  61. ];
  62. }
  63. }
  64. }
  65. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  66. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  67. $objPHPExcel = new \PHPExcel();
  68. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  69. ->setLastModifiedBy("Maarten Balliauw")
  70. ->setTitle("Office 2007 XLSX Test Document")
  71. ->setSubject("Office 2007 XLSX Test Document")
  72. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  73. ->setKeywords("office 2007 openxml php")
  74. ->setCategory("Test result file");
  75. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(12);
  76. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(4);
  77. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日')." 价格表");
  78. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.4);
  79. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0);
  80. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.1);
  81. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  82. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  83. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  84. $i = 1;
  85. $letter = [1=>['A','B'],2=>['C','D'],3=>['E','F'],4=>['G','H'],5=>['I','J'],6=>['K','L'],7=>['M','N'],
  86. 8=>['O','P'],9=>['Q','R'],10=>['S','T'],11=>['U','V']];
  87. $lastCell = '';
  88. //最大行
  89. $max = 50;
  90. foreach ($formatData as $key => $product) {
  91. $currentPage = ceil($i/$max);
  92. $x = $i%$max == 0 ? $max : $i%$max;
  93. $before = $letter[$currentPage][0] ?? 'A';
  94. $after = $letter[$currentPage][1] ?? 'B';
  95. if(isset($product['price']) == false || empty($product['price'])){
  96. //没有价格的合并单元格
  97. $objPHPExcel->getActiveSheet()->mergeCells("{$before}{$x}:{$after}{$x}")->setCellValue($before . $x, $product['name']);
  98. //加粗
  99. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setBold(true);
  100. //居中
  101. $objPHPExcel->getActiveSheet()->getStyle($before. $x)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  102. }else{
  103. $objPHPExcel->getActiveSheet()->setCellValue($before. $x, $product['name'])->setCellValue($after . $x, $product['price']);
  104. //字体大小
  105. $objPHPExcel->getActiveSheet()->getStyle($before. $x)->getFont()->setSize(10);
  106. }
  107. $lastCell = $after.$max;
  108. $i++;
  109. }
  110. //设置边框
  111. $objPHPExcel->getActiveSheet()->getStyle('A1:'.$lastCell)->applyFromArray(array('borders' => array('allborders' => array('style' => \PHPExcel_Style_Border::BORDER_THIN),)));
  112. $objPHPExcel->getActiveSheet()->setTitle('价格表');
  113. $objPHPExcel->setActiveSheetIndex(0);
  114. header('Content-Type: application/vnd.ms-excel');
  115. header('Content-Disposition: attachment;filename="0922.xls"');
  116. header('Cache-Control: max-age=0');
  117. header('Cache-Control: max-age=1');
  118. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  119. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  120. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  121. header('Pragma: public'); // HTTP/1.0
  122. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  123. $objWriter->save('php://output');
  124. exit;
  125. }
  126. }