<?php

class ContractDao {
    public static function insert($params){
        global $wpdb;
        $wpdb->insert(Config::TOSPUR_CONTRACT,$params);
        if($wpdb->insert_id){
            return $wpdb->insert_id;
        }
        return $wpdb->last_error;
    }

    public static function setContractId($id,$contractId){
        global $wpdb;
        $result = $wpdb->update(Config::TOSPUR_CONTRACT,array(
            "contractId" => $contractId
        ),array(
            "id" => $id
        ));
        if($result){
            return $result;
        }else{
            return $wpdb->last_error;
        }
    }

    public static function update($id,$params){
        global $wpdb;
        $result = $wpdb->update(Config::TOSPUR_CONTRACT,$params,array(
            "id" => $id
        ));
        if($wpdb->last_error)
            return $wpdb->last_error;
        return $result;
    }

    public static function searchContract($id){
        global $wpdb;
        $sql = "select tc.*,th.address from ".Config::TOSPUR_CONTRACT." tc
        left join ".Config::TOSPUR_HOUSE_TABLE." th on tc.houseId = th.id
        where tc.id = %d;";
        $result = $wpdb->get_row($wpdb->prepare($sql,$id));
        if($result){
            return $result;
        }else{
            return $wpdb->last_error;
        }
    }
} 