<?php
class CommissionDao{
    public static function insert_touspur_commission($params){
        global $wpdb;
        $wpdb->insert(Config::TOSPUR_COMMISSION,$params);
        if($wpdb->insert_id){
            return $wpdb->insert_id;
        }else{
            return $wpdb->last_error;
        }
    }

    public static function insert_commission_log($params){
        global $wpdb;
        $wpdb->insert(Config::TOSPUR_COMMISSION_LOG,$params);
        if($wpdb->insert_id){
            return $wpdb->insert_id;
        }else{
            return $wpdb->last_error;
        }
    }

    public static function search_commission_consultant($contractId){
        global $wpdb;
        $sql = "select consultantId,imageUrl,name from ".Config::TOSPUR_COMMISSION." tcom
        left join (select id,imageUrl,name from ".Config::TOSPUR_CONSULTANT.") tct
        on tcom.consultantId = tct.id where tcom.contractId = %d";
        $result = $wpdb->get_row($wpdb->prepare($sql,$contractId));
        return $result;
    }

    public static function search_commission_log($commissionId){
        global $wpdb;
        $sql = "select id,time,paid from ".Config::TOSPUR_COMMISSION_LOG."
        where commissionId = %d";
        $result = $wpdb->get_results($wpdb->prepare($sql,$commissionId));
        return $result;
    }

    public static function search_tospur_commission($contractId){
        global $wpdb;
        $sql = "select id,intent,accounts,type from ".Config::TOSPUR_COMMISSION."
        where contractId = %d";
        $result = $wpdb->get_row($wpdb->prepare($sql,$contractId));
        return $result;
    }
}
