Helper.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Reports Mysql resource helper model
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Reports\Model\ResourceModel;
  12. class Helper extends \Magento\Framework\DB\Helper implements \Magento\Reports\Model\ResourceModel\HelperInterface
  13. {
  14. /**
  15. * @param \Magento\Framework\App\ResourceConnection $resource
  16. * @param string $modulePrefix
  17. */
  18. public function __construct(\Magento\Framework\App\ResourceConnection $resource, $modulePrefix = 'reports')
  19. {
  20. parent::__construct($resource, $modulePrefix);
  21. }
  22. /**
  23. * Merge Index data
  24. *
  25. * @param string $mainTable
  26. * @param array $data
  27. * @param mixed $matchFields
  28. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  29. * @return string
  30. */
  31. public function mergeVisitorProductIndex($mainTable, $data, $matchFields)
  32. {
  33. $result = $this->getConnection()->insertOnDuplicate($mainTable, $data, array_keys($data));
  34. return $result;
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function updateReportRatingPos($connection, $type, $column, $mainTable, $aggregationTable)
  40. {
  41. $periodSubSelect = $connection->select();
  42. $ratingSubSelect = $connection->select();
  43. $ratingSelect = $connection->select();
  44. switch ($type) {
  45. case 'year':
  46. $periodCol = $connection->getDateFormatSql('t.period', '%Y-01-01');
  47. break;
  48. case 'month':
  49. $periodCol = $connection->getDateFormatSql('t.period', '%Y-%m-01');
  50. break;
  51. default:
  52. $periodCol = 't.period';
  53. break;
  54. }
  55. $columns = [
  56. 'period' => 't.period',
  57. 'store_id' => 't.store_id',
  58. 'product_id' => 't.product_id',
  59. 'product_name' => 't.product_name',
  60. 'product_price' => 't.product_price',
  61. ];
  62. if ($type == 'day') {
  63. $columns['id'] = 't.id'; // to speed-up insert on duplicate key update
  64. }
  65. $cols = array_keys($columns);
  66. $cols['total_qty'] = new \Zend_Db_Expr('SUM(t.' . $column . ')');
  67. $periodSubSelect->from(
  68. ['t' => $mainTable],
  69. $cols
  70. )->group(
  71. ['t.store_id', $periodCol, 't.product_id']
  72. )->order(
  73. ['t.store_id', $periodCol, 'total_qty DESC']
  74. );
  75. $cols = $columns;
  76. $cols[$column] = 't.total_qty';
  77. $cols['rating_pos'] = new \Zend_Db_Expr(
  78. "(@pos := IF(t.`store_id` <> @prevStoreId OR {$periodCol} <> @prevPeriod, 1, @pos+1))"
  79. );
  80. $cols['prevStoreId'] = new \Zend_Db_Expr('(@prevStoreId := t.`store_id`)');
  81. $cols['prevPeriod'] = new \Zend_Db_Expr("(@prevPeriod := {$periodCol})");
  82. $ratingSubSelect->from($periodSubSelect, $cols);
  83. $cols = $columns;
  84. $cols['period'] = $periodCol;
  85. $cols[$column] = 't.' . $column;
  86. $cols['rating_pos'] = 't.rating_pos';
  87. $ratingSelect->from($ratingSubSelect, $cols);
  88. $sql = $ratingSelect->insertFromSelect($aggregationTable, array_keys($cols));
  89. $connection->query("SET @pos = 0, @prevStoreId = -1, @prevPeriod = '0000-00-00'");
  90. $connection->query($sql);
  91. return $this;
  92. }
  93. }