<?php
class Tospur_House{

    public static function canApproval(){
        return current_user_can("houseApproval");
    }

    public static function canEdit(){
        return current_user_can("canEdit");
    }

    public static function getCurrentRole(){
        $current_user = wp_get_current_user();
        if ( !($current_user instanceof WP_User) )
            return;
        $roles = $current_user->roles;
        return $roles[0];
    }
    
    public static function list_status_upate($houseId,$postStatus,$arrayParams){
        //首先使用role判断是经理还是职业顾问做不同的操作，接着判断置业顾问是否修改了状态
        $role = Tospur_House::getCurrentRole();
        if($role == "jl"|| $role == "xzzl"){
            //经理
            //通过
            if($postStatus != -2){
                $arrayParams["status"] =$postStatus;
                $arrayParams["approval"] = -2;
            }else{
                //退回
                $arrayParams["approval"] = $postStatus;
            }
        }else if ($role == "zygw"){
            //置业顾问
            //修改了状态
            $currentStatus = SearchDao::getDetailInfo($houseId);
            if($currentStatus["result"]->status != $postStatus){
                $arrayParams["status"] = 0;
                $arrayParams["approval"] = $postStatus;
            }else {
                //没有修改状态
                //没有申请审批
                if($currentStatus["result"]->approval == -2){
                    $arrayParams["status"] = 0;
                    $arrayParams["approval"] = $currentStatus["result"]->status;
                }else{
                    //申请了审批，没有修改状态，approval保持原来的
                    $arrayParams["status"] = 0;
                    $arrayParams["approval"] = $currentStatus["result"]->approval;
                }
            }
        }
        return $arrayParams;
    }

    //获得当前登录置业顾问所在门店的所有置业顾问id
    public static function get_oganization_consultant(){
        global $wpdb;
        $string = null;
        $user_id = get_current_user_id();
        if($user_id){
            $res = DBManager::get_row("select subsidiaryId from ".Config::TOSPUR_CONSULTANT." where id = ".$user_id);
            if($res){
                $results = DBManager::get_results("select id from ".Config::TOSPUR_CONSULTANT." where subsidiaryId = ".$res->subsidiaryId);
                $resArray =array();
                foreach($results as $key=>$value) {
                    $resArray[$key] = $value->id;
                }
                $string = '(' . implode(',', array_map('intval', $resArray)) . ')';
                return $string;
            }
        }
    }
}