<?php

namespace app\api\controller;

use app\api\extend\Basic;
use app\model\Evaluate;
use app\model\HouseImgs;
use app\model\HouseInfos;
use app\model\JournalAccounts;

/**
 * Created by PhpStorm.
 * User : zw
 * Date : 2017/12/12
 * Time : 17:24
 * Intro: 我的交易记录
 */
class TradeLog extends Basic
{

    protected $journalAccountsMode;
    protected $dbImg;
    protected $evaluateMode;
    protected $houseInfoModel;

    public function __construct($request = null)
    {
        parent::__construct($request);
        $this->journalAccountsMode = new JournalAccounts();
        $this->dbImg = new HouseImgs();
        $this->houseInfoModel = new HouseInfos();
        $this->evaluateMode = new Evaluate();
    }

    /**
     * 获取交易记录
     * @return \think\Response
     */
    public function getTradeList()
    {
        $params = $this->params;
/*        $params = array(
            "user_id" => "1",
            "phone" => "158****6165",
            "pageNo" => 1,
            "pageSize" => 15,
        );*/

        $pageNo = empty($params['pageNo']) ? 1 : $params['pageNo'];
        $pageSize = empty($params['pageSize']) ? 15 : $params['pageSize'];

        $order_ = 'a.created desc';

        $field = "min(a.id) as id,a.tradecode,a.singntime,b.phpone,b.house_id,b.agent_id";

        $result = $this->journalAccountsMode->getJournalAccountsList($pageNo, $pageSize, $order_, $field, $params);
        if (count($result) <= 0) {
            return $this->response("200", "请求数据为空");
        }
        foreach ($result as $key => $val) {
            $houseInfo = $this->houseInfoModel->getHouseDetail("room_num_left,title", array("id"=>$val["house_id"]));
            $result[$key]["room_num_left"] = $houseInfo["room_num_left"];
            $result[$key]["title"] = $houseInfo["title"];
             $result[$key]["api_path"] = IMG_PATH;
             $imgParam["house_id"] = $val["house_id"];
             $imgParam["imgtype"] = 1; //默认主图
             $result[$key]["images"] = $this->dbImg->getHouseImages($imgParam, 1);
             $evaluateResult = $this->evaluateMode->getIsEvaluate(20, $val["id"], $params["user_id"]);
             $result[$key]["is_evaluate"] = count($evaluateResult) > 0 ? 1 : 0;
        }
        return $this->response("200", "request", $result);

    }

}