<?php

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

    }

    public static function setContractId($id,$contractId){
        global $wpdb;
        $result = DBManager::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 =DBManager::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 =DBManager::get_row($wpdb->prepare($sql,$id));
        if($result){
            return $result;
        }else{
            return $wpdb->last_error;
        }
    }

    public static function insert_a_contract_image($params){
        global $wpdb;
        DBManager::insert(Config::A_CONTRACT_IMAGE_TABLE,$params);
        if($wpdb->last_error){
            return $wpdb->last_error;
        }
        return $wpdb->insert_id;
    }

    public static function insert_contract_image($contractId){
        $uploadedfile =  $_FILES['files'];
        if($uploadedfile["name"]) {
            foreach ($uploadedfile["name"] as $key => $value) {
                $uploadParam = array(
                    "name" => $uploadedfile["name"][$key],
                    "type" => $uploadedfile["type"][$key],
                    "tmp_name" => $uploadedfile["tmp_name"][$key],
                    "error" => $uploadedfile["error"][$key],
                    "size" => $uploadedfile["size"][$key]
                );
                if (!function_exists('wp_handle_upload')) {
                    require_once(ABSPATH . 'wp-admin/includes/file.php');
                }
                $overrides = array('test_form' => false);
                $movefile = wp_handle_upload($uploadParam, $overrides);
                $str = preg_replace('#^https?://#', '', $movefile["url"]);
                $length = strpos($str, "/", strpos($str, "/") + 1);
                $url = substr($str, $length);
                if ($movefile && !isset($movefile['error'])) {
                    $uploadFileName = end(explode("/", $url));
                    //上传成功后将图片信息存入tospur_image表
                    $insert_image_array = array(
                        'name' => $uploadFileName,
                        'path' => $url,
                        'creattime' => date("Y-m-d H:i:s"),
                        'alt' => "",
                        'image_type' => 7
                    );
                    $result = InsertDao::insert_tospur_image($insert_image_array);
                    if (!is_numeric($result)) {
                        return $result;
                    }
                    $imgid = $result;
                    $contract_image_array = array(
                        "contract_id" => $contractId,
                        "image_id" => $imgid
                    );
                    $result = ContractDao::insert_a_contract_image($contract_image_array);
                    if (!is_numeric($result)) {
                        return $result;
                    }
                } else {
                    return $movefile['error'];
                }
            }
        }
    }

    public static function searchContractImg($id){
        global $wpdb;
        $sql = "select ti.id,ti.path from a_contract_image aci
                left join tospur_contract tc on tc.id = aci.contract_id
                left join tospur_image ti on aci.image_id = ti.id where aci.contract_id = %d;";
        $result =DBManager::get_results($wpdb->prepare($sql,$id));
        if($wpdb->last_error){
            return $wpdb->last_error;
        }
        return $result;
    }

    public static function searchImgPath($id){
        global $wpdb;
        $sql = "select path from ".Config::TOSPUR_IMAGE_TABLE." where id in ({$id})";
        $results =DBManager::get_results($sql);
        if($wpdb->last_error){
            return $wpdb->last_error;
        }
        return $results;
    }

} 