public class infrastructureProjectRelatedOpController {
    public static List<String>  sortList = new List<String>{'询价','注残','发货','完毕','失单','取消'};
    @AuraEnabled
    //根据报价Id检索
    public static  Infrastructure_Project__c selectInfrastructureProjectById(String InfProId){
        List<Infrastructure_Project__c> InfrastructureProject = [ SELECT Id,Name,OCSM_Hospital__c,OCSM_Hospital__r.Name
                  FROM Infrastructure_Project__c Where Id = :InfProId];
        Infrastructure_Project__c  InfProobj = new Infrastructure_Project__c();
        if(InfrastructureProject.size()>0){
            InfProobj = InfrastructureProject[0];
        }
        return InfProobj;
    }
    @AuraEnabled
    //根据报价Id检索
    public static  List<Opportunity> selectoppByAccountId(String OCSMHospital ){
        Map<String,List<Opportunity>> OppMap = new Map<String,List<Opportunity>>();
        List<Opportunity> OppList = [ SELECT Id,Name,StageName__c,Opportunity_No__c,Opportunity_stage__c from Opportunity Where Hospital__c = :OCSMHospital and Infrastructure_Project__c =null and StageName__c != '目标' order by StageName__c asc,CreatedDate desc];
        for (Opportunity Oppobj : OppList) {
            if(OppMap.containsKey(Oppobj.StageName__c)){
                List<Opportunity> oldopp = OppMap.get(Oppobj.StageName__c);
                oldopp.add(Oppobj);
                OppMap.put(Oppobj.StageName__c,oldopp);
            }else{
                List<Opportunity> newopp = new List<Opportunity>();
                newopp.add(Oppobj);
                OppMap.put(Oppobj.StageName__c,newopp);
            }
        }
        List<Opportunity> sortOppList = new List<Opportunity>();
        for (String sortStr : sortList) {
            if(OppMap.containsKey(sortStr)){
                sortOppList.addAll(OppMap.get(sortStr));
            }
        }
        return sortOppList;
    }

    @AuraEnabled
    public static  List<Opportunity> selectoppByOpportunityNoc(String OCSMHospital,String OpportunityNoc){
        System.debug('aa+'+OpportunityNoc);
        Map<String,List<Opportunity>> OppMap = new Map<String,List<Opportunity>>();
        String searchSql = 'Select Id,Name,StageName__c,Opportunity_No__c,Opportunity_stage__c From Opportunity Where Infrastructure_Project__c =null and Hospital__c = \''+OCSMHospital+'\' and StageName__c != \'目标\' ';
        String whereSql = '';
        if(OpportunityNoc != null && OpportunityNoc != ''){
            whereSql += 'and (Opportunity_No__c Like ' + '\'%' + OpportunityNoc + '%\' or Name Like ' + '\'%' + OpportunityNoc + '%\') ';
        }
        whereSql += ' order by StageName__c asc,CreatedDate desc';
        searchSql = searchSql + whereSql;
        System.debug('searchSql+'+searchSql);
        List<Opportunity> OppList = Database.query(searchSql);
        for (Opportunity Oppobj : OppList) {
            if(OppMap.containsKey(Oppobj.StageName__c)){
                List<Opportunity> oldopp = OppMap.get(Oppobj.StageName__c);
                oldopp.add(Oppobj);
                OppMap.put(Oppobj.StageName__c,oldopp);
            }else{
                List<Opportunity> newopp = new List<Opportunity>();
                newopp.add(Oppobj);
                OppMap.put(Oppobj.StageName__c,newopp);
            }
        }
        List<Opportunity> sortOppList = new List<Opportunity>();
        for (String sortStr : sortList) {
            if(OppMap.containsKey(sortStr)){
                sortOppList.addAll(OppMap.get(sortStr));
            }
        }
        return sortOppList;
    }
    @AuraEnabled
    public static  String  saveOppInfrastructureProject(String saveValuec){
        Savepoint sp = Database.setSavepoint();
        try {
            List<Opportunity> oppList = (List<Opportunity>)JSON.deserialize(saveValuec,List<Opportunity>.class);
            StaticParameter.EscapeOppandStaTrigger = true;
            update oppList;
            StaticParameter.EscapeOppandStaTrigger = false;
            return 'success';
        } catch (Exception e) {
            Database.rollback(sp);
            return ''+e;
        }
    }
}