<?php

namespace app\model;
use think\Db;
use think\Model;

class Evaluate extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'u_evaluate';

    /**
     * 获得评价列表
     * 
     * @param type $pageNo
     * @param type $pageSize
     * @param type $order_
     * @param type $field
     * @param type $params
     * @param type $agent_id //获得经纪人的评价
     * @return type
     */
    public function getEvaluate($pageNo = 1, $pageSize = 15, $order_ = 'id desc', $field, $params, $agent_id = '') {

        if ($agent_id) {
            $result = $this->field($field)->alias('a')
                    ->join('u_users b','a.user_id = b.id','left')
                    ->where('agents_id',$agent_id)
                    ->where('is_show',0)
                    ->order($order_)
                    ->limit($pageSize)
                    ->page($pageNo)
                    ->select();

        } else {
            $result = $this->field($field)->alias('a')
                    ->join('u_users b', 'a.user_id = b.id', 'left')
                    ->where('is_show', 0)
                    ->order($order_)
                    ->limit($pageSize)
                    ->page($pageNo)
                    ->select();
        }

        $sign    = new EvaluateSign();
        $watch   = new AppointWatchShop();
        foreach ($result as $k=>$v) {
            $v->evaluate_grade = floor($v->evaluate_grade/2);
            $data[$k]   = $v->getData();
            $data[$k]['user_pic'] = CURRENT_URL.'/static/head_portrait/'.$data[$k]['user_pic'];
            if ($v['evaluate_sign']) {
                 $data[$k]['evaluate_sign'] = $sign->field('sign_name,point_obj')
                         ->where('id', 'in', $v['evaluate_sign'])
                         ->where('is_del',0)
                         ->select();
            }

            if ($v->source == 10) {
                $applies_time = $watch->alias('a')
                    ->field('b.receptiontime')
                    ->join('applies b','b.id = a.applies_id','left')
                    ->where('a.applies_id',$v->record_id)
                    ->find();
            } else {

                $applies_time = Db::table('Journalaccounts')->alias('a')
                    ->field('b.receptiontime')
                    ->join('applies b','b.id = a.apply_id','left')
                    ->where('a.apply_id',$v->record_id)
                    ->find();
            }

            $house_address = Db::table('HouseInfos')->field('address')->where('id',$v['house_id'])->find();

            $data[$k]['houserAddress'] = $house_address['address'] ? $house_address['address'] : '';
            $data[$k]['receptiontime'] = $applies_time['receptiontime'];
            $data[$k]['created'] = $v['create_time'] ? $v['create_time']:'';
        }
                
        return $data;
    }

    /**
     * 获取商铺是否评价
     * @param int $source
     * @param $recordId
     * @param $userId
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getIsEvaluate($source ,$recordId , $userId){
        $params["source"] = $source ;
        $params["record_id"] = $recordId;
        $params["user_id"] = $userId;
        return $this->field("id")
            ->where($params)
            ->select();

    }
}
