<?php
/**
* @version EC=CUBE4
* @copyright 株式会社 翔 kakeru.co.jp
* @author
* 2022年08月15日作成
*
* app/Customize/Service/MessageSErvice.php
*
*
* メッセージサービス
*
*
* C= C= C= ┌(;・_・)┘トコトコ
******************************************************/
namespace Customize\Service;
use Lm\Service\Db\SqlService;
class MessageService
{
private $lmHelper;
public function __construct(LmHelper $lmHelper)
{
$this->lmHelper = $lmHelper;
}
public function GetMessage($GoodsId = null ,$CategoryId = null){
$Sql = new SqlService();
$Sql->Table('customer_message_table')
->Set('display',1 )
->Set('approval' ,1)
->Order('cm_id','DESC');
if (!is_null($GoodsId)){
$Sub ='oh_id IN (SELECT S1.ol_oh FROM order_list_table AS S1 INNER JOIN goods_table AS S2 ON S1.ol_goods = S2.goods_id WHERE S2.goods_ddate IS NULL AND S1.ol_goods = :GoodsId)';
$Sql->Where($Sub)
->Param(':GoodsId',$GoodsId);
}elseif (!is_null($CategoryId)){
$Sql->Set('cm_id',0 ,'>')
->Set('category','"'.$CategoryId.'"','LIKE');
}
return $Sql->FindAll();
}
public function GetGoodsList($Id){
$Sql = new SqlService();
return $Sql->Table('customer_message_table')
->Select('T.*,T1.*,T2.*')
->Join('order_list_table','oh_id = ol_oh','INNER')
->Join('goods_table', 'T1.ol_goods = goods_id', 'INNER')
->Set('T.cm_id',$Id)
->Order('T.display_datetime','DESC')
->FindAll();
}
public function GetCategoryList(){
$Sql = new SqlService();
return $Sql->Table('customer_message_category_table')
->Set('delete',0)
->FindAll();
}
public function getCustomerMessagesAndCount($goodsId, $limit)
{
$messages = (new SqlService())
->Sql("
SELECT SQL_CALC_FOUND_ROWS
customer_message_table.cm_id,
customer_message_table.oh_id,
customer_message_table.customer_name,
customer_message_table.title,
customer_message_table.comment,
customer_message_table.staff_comment,
customer_message_table.category,
customer_message_table.use,
customer_message_table.file,
customer_message_table.display,
customer_message_table.approval,
customer_message_table.app_datetime,
customer_message_table.upd_datetime,
customer_message_table.add_datetime,
customer_message_table.display_datetime,
CASE WHEN customer_message_table.file IS NULL THEN 0 ELSE 1 END AS file_exits,
customer_table.customer_user_name
FROM customer_message_table
INNER JOIN order_header_table ON order_header_table.oh_id = customer_message_table.oh_id
INNER JOIN customer_table ON oh_customer = customer_table.customer_id
INNER JOIN order_list_table ON customer_message_table.oh_id = ol_oh
INNER JOIN goods_table ON ol_goods = goods_id
WHERE goods_id = :goods_id AND display = '1' AND approval = '1'
GROUP BY customer_message_table.cm_id
ORDER BY file_exits DESC, customer_message_table.display_datetime DESC
LIMIT :limit;
")
->Params(['goods_id' => $goodsId, 'limit' => $limit])
->FetchAll();
if (empty($messages)) {
return [[], 0];
}
$foundMessages = (new SqlService())->Sql('SELECT FOUND_ROWS() AS total')->Fetch();
return [$messages, $foundMessages['total'] ?? 0];
}
public function formatCustomerMessages($messages)
{
return array_map(function ($message) {
$message['display_datetime'] = date('Y年m月d日', strtotime($message['display_datetime']));
$attachments = unserialize($message['file']);
if ($attachments) {
$message['file'] = $this->lmHelper->getConfig('CFIMG_BACK_URL') . '/images/koe_admin/'
. $message['cm_id'] . '/' . reset($attachments);
}
if ($message['comment']) {
$comment = strip_tags(str_replace("<br />", "", $message['comment']));
$message['shorted_comment'] = mb_substr($comment, 0, 216);
$message['extra_comment'] = mb_substr(strip_tags($comment), 216);
}
if ($message['staff_comment']) {
$staffComment = strip_tags($message['staff_comment']);
$message['shorted_staff_comment'] = mb_substr($staffComment, 0, 216);
$message['extra_staff_comment'] = mb_substr(strip_tags($staffComment), 216);
}
return $message;
}, $messages);
}
}