<?php
class CommissionDao{
    public static function insert_touspur_commission($params){
        global $wpdb;
        DBManager::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;
        DBManager::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,branchName,ioType from ".Config::TOSPUR_COMMISSION." tcom
        left join (SELECT tct.id,imageUrl,tct.name,ton.name as branchName from ".Config::TOSPUR_CONSULTANT." tct
        left JOIN ".Config::TOSPUR_ORGANIZATION_TABLE." ton on ton.id = tct.subsidiaryId) tc
        on tcom.consultantId = tc.id where tcom.contractId = %d order by ioType;";
        $result = DBManager::get_results($wpdb->prepare($sql,$contractId));
        return $result;
    }

    public static function search_commission_log($commissionIds){
        $sql = "  SELECT tcl.id,time,paid,ioType from tospur_commission_log tcl
          left join tospur_commission tc on tc.id = tcl.commissionId
          where tcl.commissionId in (".$commissionIds.");";
        $result =DBManager::get_results($sql);
        return $result;
    }

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