<?php
/**
* @version EC=CUBE4
* @copyright 株式会社 翔 kakeru.co.jp
* @author
* 2022年08月18日作成
*
* app/Customize/Service/ReviewService.php
*
*
* レヴュージサービス
*
*
* C= C= C= ┌(;・_・)┘トコトコ
******************************************************/
namespace Customize\Service;
use Lm\Service\Db\SqlService;
use Lm\Engine\EC\Entity\GoodsWithRelated;
class ReviewService{
const REVIEW_GET_MAX = 50;
public function __construct()
{
$this->Sql = new SqlService();
}
/**
* @var SqlService $Sql
*/
protected $Sql;
/**
* 未使用 $main_category_id = null, $category_id = null, $limit = '', $offset = 0
* sex ege 使われていない
*/
public function GetReview($id, &$count = null, $limit = GoodsWithRelated::REVIEW_GET_MAX, $offset = 0){
return GoodsWithRelated::getReviewListById($id, $limit, $offset, $count);
}
protected function getGoodsLinkSelectSql ($data, $count = false)
{
if (is_array($data)) {
$whereGoods = "target_goods_id IN (" . implode(",", $data) . ")";
} else {
$whereGoods = "target_goods_id = " . $data;
}
if ($count > 0) {
$max = $count;
} else {
$max = 14;
}
$Sql = new SqlService();
return $Sql->Table('goods_link_table')
->Select('DISTINCT T.goods_id, T1.goods_canonical_hinban, IfNull(T1.goods_canonical_hinban, T1.goods_id) AS item_id, T.goods_name, T.goods_count, min_gp_price, count_gp_price')
->Join('goods_table','T.goods_id = T1.goods_id','INNER')
->Set('T1.goods_ddate', NULL)
->Set('T1.goods_status',1)
->Where($whereGoods)
->Order('T.goods_count','DESC')
->Limit('0',$max)
->FindAll();
}
// 口コミ平均評価取得
protected function getReviewAverageSql($goods_id=null)
{
$Sql = new SqlService();
if($goods_id){
$whereGoods = "cra_goods_id =".$goods_id;
}
return $Sql->Table('customer_review_average_table')
->Select('cra_goods_id,cra_average_points,cra_target_count')
->Where('cra_target_count >= 1')
->Where($whereGoods)
->FindAll();
}
protected function getReviewAverage($goods_id=null) {
$temp = $this->getReviewAverageSql($goods_id);
$average = array();
foreach((array)$temp as $data){
$average[$data['cra_goods_id']] = $data['cra_average_points'];
}
return $average;
}
protected function getReviewSql($id, $isSecret = true, $main_category_id = null, $category_id = null, $limit = '', $offset = 0)
{
$SELECT = !empty($limit) ? "SELECT SQL_CALC_FOUND_ROWS" : "SELECT";
$sql = "$SELECT
DISTINCT
cr_oh_id
,cr_goods_id
,cr_customer_id
,cr_name
,cr_points
,cr_title
,cr_comment
,cr_staff_comment
,CASE cr_age
WHEN '1' THEN '10代'
WHEN '2' THEN '20代'
WHEN '3' THEN '30代'
WHEN '4' THEN '40代'
WHEN '5' THEN '50代'
WHEN '6' THEN '60代'
ELSE '秘密'
END cr_age
,CASE cr_sex
WHEN '0' THEN '男性'
WHEN '1' THEN '女性'
ELSE '秘密'
END cr_sex
,cr_approval
,app_datetime
,upd_datetime
,add_datetime
,goods_name
,(SELECT min(gp_kataban) FROM goods_price_table where cr_goods_id = gp_goods GROUP BY gp_goods) as gp_kataban
FROM customer_review_table
LEFT JOIN goods_table ON goods_id = cr_goods_id
WHERE cr_approval = '1' AND cr_site_id = 0";
if (!empty($id) && empty($main_category_id)) {
// 商品ページのみに表示
$sql .= " AND cr_goods_id = {$id}
AND cr_main_category_id = 0
AND EXISTS (
SELECT * FROM goods_table WHERE goods_id = {$id} AND goods_ddate IS NULL";
if(!$isSecret){
$sql .= " AND goods_status = 1 ";
}
$sql .= " )";
} else if (!empty($main_category_id) && empty($category_id)) {
// 親カテページのみに表示
$sql .= " AND cr_main_category_id = {$main_category_id}
AND cr_category_id = 0";
} else if (!empty($main_category_id) && !empty($category_id)) {
// 子カテページのみに表示
$sql .= " AND cr_main_category_id = {$main_category_id}
AND cr_category_id = '{$category_id}'";
}
$sql .= " ORDER BY `customer_review_table`.`app_datetime` desc";
if (!empty($limit)) {
// 商品ページのみに表示
$sql .= " LIMIT {$offset}, {$limit}";
}
return $this->Sql->SQL($sql)->FetchAll();
}
public function LM_displayTogetherBuyDetail_new3 ($goods_id, $count = false)
{
// 一緒に購入されている商品
$result = array();
$temp = $this->getGoodsLinkSelectSql($goods_id, $count);
if($temp){
foreach ($temp as $row) {
$goodsId = $row['goods_id'];
$result[$goodsId] = $row;
$average = $this->getReviewAverage($row['goods_id']);
$average = (!empty($average[$row['goods_id']])) ? $average[$row['goods_id']] : 0;
$image_name = (int)($average * 2) / 2 * 10;
$result[$goodsId]['average'] = $average;
$result[$goodsId]['average_img'] = "<img class=\"history-average-img\" alt=\"お客様からの口コミレビュー評価の星の数{$average}\" src=\"/images/review/star_m".$image_name.".gif\">";
$count=0;
if($tempRow = $this->getReviewSql($row['goods_id'])) $count=count($tempRow);
$result[$goodsId]['review_count'] = $count;
$result[$goodsId]['img'] = "/goods.img/". substr($goodsId, -1, 1) . "/" . $goodsId . "/main.jpg";
}
}
return $result;
}
public function getReviewExists($ohId, $goodsId, $customerId)
{
$result = (new SqlService())
->Select('cr_id')
->Table('customer_review_table')
->Set('cr_oh_id', $ohId)
->Set('cr_goods_id', $goodsId)
->Set('cr_customer_id', $customerId)
->Find();
return $result;
}
public function insertReview($values)
{
$sql = new SqlService();
$sql->Table('customer_review_table')
->Insert(false, $values);
}
}