-
Notifications
You must be signed in to change notification settings - Fork 19
/
RecommendProductRepository.php
195 lines (175 loc) · 4.94 KB
/
RecommendProductRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\Recommend4\Repository;
use Eccube\Entity\Master\ProductStatus;
use Eccube\Repository\AbstractRepository;
use Plugin\Recommend4\Entity\RecommendProduct;
use Symfony\Bridge\Doctrine\RegistryInterface;
/**
* RecommendProductRepository.
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class RecommendProductRepository extends AbstractRepository
{
/**
* CouponRepository constructor.
*
* @param RegistryInterface $registry
*/
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, RecommendProduct::class);
}
/**
* Find list.
*
* @return mixed
*/
public function getRecommendList()
{
$qb = $this->createQueryBuilder('rp')
->innerJoin('rp.Product', 'p');
$qb->where('rp.visible = true');
$qb->addOrderBy('rp.sort_no', 'DESC');
return $qb->getQuery()->getResult();
}
/**
* Get max rank.
*
* @return mixed
*
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function getMaxRank()
{
$qb = $this->createQueryBuilder('rp')
->select('MAX(rp.sort_no) AS max_rank');
return $qb->getQuery()->getSingleScalarResult();
}
/**
* Get recommend product by display status of product.
*
* @return array
*/
public function getRecommendProduct()
{
$query = $this->createQueryBuilder('rp')
->innerJoin('Eccube\Entity\Product', 'p', 'WITH', 'p.id = rp.Product')
->where('p.Status = :Disp')
->andWhere('rp.visible = true')
->orderBy('rp.sort_no', 'DESC')
->setParameter('Disp', ProductStatus::DISPLAY_SHOW)
->getQuery();
return $query->getResult();
}
/**
* Number of recommend.
*
* @return mixed
*
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function countRecommend()
{
$qb = $this->createQueryBuilder('rp');
$qb->select('COUNT(rp)');
return $qb->getQuery()->getSingleScalarResult();
}
/**
* Move rank.
*
* @param array $arrRank
*
* @return array
*
* @throws \Exception
*/
public function moveRecommendRank(array $arrRank)
{
$this->getEntityManager()->beginTransaction();
$arrRankMoved = [];
try {
foreach ($arrRank as $recommendId => $rank) {
/* @var $Recommend RecommendProduct */
$Recommend = $this->find($recommendId);
if ($Recommend->getSortno() == $rank) {
continue;
}
$arrRankMoved[$recommendId] = $rank;
$Recommend->setSortno($rank);
$this->getEntityManager()->persist($Recommend);
}
$this->getEntityManager()->flush();
$this->getEntityManager()->commit();
} catch (\Exception $e) {
$this->getEntityManager()->rollback();
throw $e;
}
return $arrRankMoved;
}
/**
* Save recommend.
*
* @param RecommendProduct $RecommendProduct
*
* @return bool
*
* @throws \Exception
*/
public function saveRecommend(RecommendProduct $RecommendProduct)
{
$this->getEntityManager()->beginTransaction();
try {
$this->getEntityManager()->persist($RecommendProduct);
$this->getEntityManager()->flush($RecommendProduct);
$this->getEntityManager()->commit();
} catch (\Exception $e) {
$this->getEntityManager()->rollback();
throw $e;
}
return true;
}
/**
* Get all id of recommend product.
*
* @return array
*/
public function getRecommendProductIdAll()
{
$query = $this->createQueryBuilder('rp')
->select('IDENTITY(rp.Product) as id')
->where('rp.visible = true')
->getQuery();
$arrReturn = $query->getScalarResult();
return array_map('current', $arrReturn);
}
/**
* おすすめ商品情報を削除する
*
* @param RecommendProduct $RecommendProduct
*
* @return bool
*
* @throws \Exception
*/
public function deleteRecommend(RecommendProduct $RecommendProduct)
{
// おすすめ商品情報を書き換える
$RecommendProduct->setVisible(false);
// おすすめ商品情報を登録する
return $this->saveRecommend($RecommendProduct);
}
}