canyin-project/ybcy/controllers/channel/CommentController.php
2024-11-01 16:07:54 +08:00

175 lines
6.5 KiB
PHP

<?php
namespace app\controllers\channel;
use Yii;
use app\models\common\Store;
class CommentController extends CommonController{
public $enableCsrfValidation = false;
private $shop_id;
function init(){
parent::init();
$this->shop_id=$this->storeId?:Store::getMainStore($this->wqData['uniacid'])['id'];
$this->shop_id=$this->shop_id?:0;
}
//评价列表
public function actionCommentList(){
$uniacid = $this->wqData['uniacid'];
$result=axios_request();
$storeId=$result['storeId']?:$this->shop_id;
$page=$result['page']?:1;
$star=$result['star'];
$timeType=$result['timeType'];
$keyword=$result['keyword'];
$timeArr=empty($timeType) ? false : $this->timeArr[$timeType];
$num=($page-1)*10;
$limit=10;
$data= (new \yii\db\Query())
->select('a.star,a.id,a.body,b.portrait,b.userName,from_unixtime(a.createdAt)createdAt,a.reply,a.state,a.media,b.id userId')
->from('{{%ybwm_comment}} a')
->leftJoin('{{%ybwm_member}} b','a.userId=b.id')
->where(['a.deleteAt'=>0,'a.storeId'=>$storeId,'a.uniacid'=>$uniacid]);
if($star==1){
$data->andWhere('a.star>3');
}
if($star==2){
$data->andWhere('a.star=3');
}
if($star==3){
$data->andWhere('a.star<3');
}
if($timeArr){
$data->andWhere('a.createdAt>=:startTime AND a.createdAt<=:endTime',[':startTime'=>$timeArr['startTime'],':endTime'=>$timeArr['endTime']]);
}
if($keyword){
$data->andWhere(['or',['like','b.userName',$keyword],['like','a.outTradeNo',$keyword],['like','b.userTel',$keyword]]);
}
// echo ddSql($data->offset($num)
// ->limit($limit));die;
$count=$data->count();
$res=$data->offset($num)->orderBy('id desc')
->limit($limit)
->all();
for($i=0;$i<count($res);$i++){
$res[$i]['media']=json_decode($res[$i]['media'],true);
}
$list['list']=$res;
$starStatistics=(new \yii\db\Query())
->select(['sum(if(star>3,1,0)) as good','sum(if(star=3,1,0)) as middle',
'sum(if(star<3,1,0)) as difference','TRUNCATE(avg(star),1) as average'])
->from('{{%ybwm_comment}}')
->where(['deleteAt'=>0,'state'=>2,'storeId'=>$storeId])->one();
$commentStatistics=(new \yii\db\Query())
->from('{{%ybwm_comment}}')
->where(['deleteAt'=>0,'storeId'=>$storeId]);
$list['average']=$starStatistics['average'];
$list['good']=$starStatistics['good'];
$list['goodStar']=sprintf("%.1f",(clone $commentStatistics)->andWhere('star>3')->average('star')?:0);
$list['middle']=$starStatistics['middle'];
$list['middleStar']=sprintf("%.1f",(clone $commentStatistics)->andWhere('star=3')->average('star')?:0);
$list['difference']=$starStatistics['difference'];
$list['differenceStar']=sprintf("%.1f",(clone $commentStatistics)->andWhere('star<3')->average('star')?:0);
return $this->result(1,'成功',$list,$count);
}
//修改评价
public function actionModifyComment(){
$result=axios_request();
if($result['type']==1){
//通过
$data['state']=2;
}
if($result['type']==2){
//拒绝
$data['state']=3;
}
if($result['type']==3){
//回复
$data['reply']=$result['reply'];
}
if($result['type']==4){
//删除
$data['deleteAt']=time();
}
$data['changeAt']=time();
$res = Yii::$app->db->createCommand()->update('{{%ybwm_comment}}', $data, 'id=:id', ['id' =>$result['id']])->execute();
if($res){
return $this->result(1,'成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
//标签列表
public function actionLabelList(){
$result=axios_request();
$storeId=$result['storeId']?:$this->shop_id;
$page=$result['page']?:1;
$num=($page-1)*10;
$limit=10;
$table=(new \yii\db\Query())
->from('{{%ybwm_core_category}}')
->where(['item'=>14,'deleteAt'=>0,'uniacid'=>$this->wqData['uniacid'],'storeId'=>$storeId]);
$list=$table->offset($num)
->limit($limit)->orderBy('id desc')->all();
$count=$table->count();
return $this->result(1, '成功',$list,$count);
}
//保存标签
public function actionSaveLabel(){
$result=axios_request();
$request = Yii::$app->request;
$uniacid=$this->wqData['uniacid'];
$storeId=$result['storeId']?:$this->shop_id;
if($request->isGet){
$info=(new \yii\db\Query())
->from('{{%ybwm_core_category}}')
->where('id=:id',[':id'=>$result['id']])->one();
return $this->result(1, '成功',$info);
}
if(!$result['name']){
return $this->result(2,'标签名称不能为空!');
}
$data['name']=$result['name'];
$data['sort']=$result['sort'];
$data['item']=14;
$data['storeId']=$storeId;
if($result['id']){
$data['changeAt']=time();
$re = Yii::$app->db->createCommand()->update('{{%ybwm_core_category}}', $data, 'id=:id', ['id' =>$result['id']])->execute();
}else{
$data['uniacid']=$uniacid;
$data['createdAt']=time();
// print_R($data);die;
try{
$re = Yii::$app->db->createCommand()->insert('{{%ybwm_core_category}}', $data)->execute();
}catch (\Exception $e){
print_R($e);die;
}
}
if($re){
return $this->result(1,'成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
//修改标签
public function actionModifyLabel(){
$post=axios_request();
if($post['type']==1){
//显示隐藏
$data['display']=$post['display'];
}
if($post['type']==2){
//删除
$data['deleteAt']=time();
}
$data['changeAt']=time();
$res = Yii::$app->db->createCommand()->update('{{%ybwm_core_category}}', $data, 'id=:id', ['id' =>$post['id']])->execute();
if($res){
return $this->result(1,'成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
}