<?php
namespace wstmart\app\model;
use wstmart\common\model\GoodsAppraises as CGoodsAppraises;
use think\Db;
/**
 * ============================================================================
 * 评价类
 */
class GoodsAppraises extends CGoodsAppraises{
	/**
	 *  获取评论
	 */
	public function getAppr(){
		$oId = (int)input('oId');
		$uId = (int)session('WST_USER.userId');
		$gId = (int)input('gId');
		$specId = (int)input('sId');
		$orderGoodsId = (int)input('orderGoodsId');
		$rs = $this->where(['orderId'=>$oId,'userId'=>$uId,'goodsId'=>$gId,'goodsSpecId'=>$specId,'orderGoodsId'=>$orderGoodsId])->find();
		if($rs!==false){
			$rs = !empty($rs)?$rs:['goodsScore'=>'','timeScore'=>'','serviceScore'=>'','content'=>''];
			return jsonReturn('',1,$rs);
		}
		return jsonReturn('获取出错',-1);
	}
	/**
	* 根据商品id取一试图评论
	*/
	public function getOneAppraises($goodsId){
		$goodsId=$goodsId==0?(int)input('goodsId'):$goodsId;
		// 处理匿名
		$anonymous = 1;
		$where = ['ga.goodsId'=>$goodsId,
				  'ga.dataFlag'=>1,
				  'ga.isShow'=>1];		
		$rs  = 	$this->alias('ga')
					 ->field('DISTINCT(ga.id),ga.content,ga.images,ga.shopReply,ga.replyTime,ga.createTime,ga.goodsScore,ga.serviceScore,ga.timeScore,ga.shopId,u.userPhoto,u.loginName,u.userPhone,og.goodsSpecNames')
					 ->join('__USERS__ u','ga.userId=u.userId','left')
					 ->join('__ORDER_GOODS__ og','og.orderId=ga.orderId and og.goodsId=ga.goodsId','inner')
					 ->where($where)
					 ->order('id desc')
					 ->find();
	
		if($rs){
			// 格式化时间
			//$rs['createTime'] = date('Y-m-d',strtotime($rs['createTime']));
			$rs['goodsSpecNames'] = str_replace('@@_@@',',',$rs['goodsSpecNames']);
			// 总评分
			//$rs['avgScore'] = ceil(($rs['goodsScore'] + $rs['serviceScore'] + $rs['timeScore'])/3);
			if($anonymous){
				$start = floor((strlen($rs['userPhone'])/2))-1;
				$rs['userPhone'] = substr_replace($rs['userPhone'],'***',$start,3);
				$rs['loginName'] = $rs['userPhone'];
			}		
		}else{
			$rs = [];
		}
		
		return $rs;		
	}
}