global class OpportunityWebService {

    WebService static String check(String oppId) {
        String str = '1';
        List<OpportunityLineItem> oliList = [select id, Product2.If_Exempt_Product__c, Product2.If_Radiation_Product__c, Opportunity.DealerId__c from OpportunityLineItem where opportunityId = :oppId];
        Boolean hasA = false;
        for (OpportunityLineItem oli : oliList) {
            if (oli.Product2.If_Exempt_Product__c == false && oli.Product2.If_Radiation_Product__c == true) {
                hasA = true;
            }
        }
        if (hasA == true) {
            str =  LicenseCheckUtil.LicenseCheck1(oliList[0].Opportunity.DealerId__c);
        }
        return str;
    }

    WebService static String checkOly() {
        return LicenseCheckUtil.LicenseCheckOly();
    }

    WebService static String UpdateStageName(String opportunityId, String targetStageName) {
        Opportunity opp = new Opportunity();
        opp.Id = opportunityId;
        opp.StageName = targetStageName;
        try {
            StaticParameter.StageProgressBarUpdate = true;
            update opp;
        } catch (Exception ex) {
            return ex.getMessage();
        }

        return '1';
    }

    WebService static String UpdateShare(String opportunityId, String userId) {
        try {
            List<OpportunityShare> osList = [select Id, OpportunityId, UserOrGroupId, OpportunityAccessLevel, RowCause from OpportunityShare where OpportunityId = :opportunityId and UserOrGroupId = :userId and RowCause = 'Team' and OpportunityAccessLevel = 'Read'];
            for (OpportunityShare os : osList) {
                os.OpportunityAccessLevel = 'Edit';
            }
            if (osList.size() > 0) {
                update osList;
            }
        } catch (Exception ex) {
            return ex.getMessage();
        }

        return '1';
    }

    WebService static String Upload2Sap(String opportunityId, String quoteNo, String quoteId) {
        add();
        Savepoint sp = Database.setSavepoint();
        Opportunity oppor = [select id, AccountId, Dealer__r.DummyDealer__c, Dealer__r.ParentID, Account.MarketVerticals__c, Trade_Type_D__c from Opportunity where id = :opportunityId];
        if (oppor.Account.MarketVerticals__c == 'Clinical' && oppor.Trade_Type_D__c == 'Taxation') {
            if (oppor.Dealer__r.DummyDealer__c == true) {
                String backStr = LicenseCheckUtil.LicenseCheck(oppor.AccountId);
                if (backStr != '1') {
                    return backStr + ',不能上传报价';
                }
            } else {
                String backStr = LicenseCheckUtil.LicenseCheck(oppor.Dealer__r.ParentID);
                if (backStr != '1') {
                    return backStr + ',不能上传报价';
                }
            }
        }
        try {
            Opportunity opp = new Opportunity();
            opp.Id = opportunityId;
            opp.the_Upload_of_quotation_number__c = quoteNo;
            update opp;

            Quote quo = new Quote();
            quo.Id = quoteId;
            quo.Is_upload__c = true;
            update quo;

        } catch (Exception ex) {
            Database.rollback(sp);
            return ex.getMessage();
        }

        return '1';
    }


    WebService static void batch(String oppId){

        System.debug('$$$$$$$$$$'+oppId);
        List<String> idList = new List<String>();
        idList.add(oppId);
        Database.executeBatch(new OpportunityUpdateSendEmailBatch(idList), 10);
    }

    WebService static String checkUpperLimitOfSales(String oppId, String dealerId) {
        String result = '';
        String olympusAccountId = System.label.Olympus_Id;
        Map<String, Integer> getModelQuantityMap = RadiationUtil.GetOpportunityLineMap(oppId);
        String errorStr = RadiationUtil.GetCertificationDetail(dealerId, getModelQuantityMap);
        if (errorStr != '') {
            return '-1';
        }
        String accountDealerErrorMessage = RadiationUtil.updateCertificationDetails(dealerId, null, getModelQuantityMap, false);
        //String olympusDealerErrorMessage = RadiationUtil.updateCertificationDetails(olympusAccountId, null, getModelQuantityMap, false);
        result = accountDealerErrorMessage ;
        //String flage = LicenseCheckUtil.LicenseCheckOly();
        Boolean temp = result.startsWith('你不能');
        if (temp) {
            return '-1';
        } else {
            return result;
        }
    }


    WebService static String checkUpperLimitOfSalesOly(String oppId, String dealerId) {
        String result = '';
        String olympusAccountId = System.label.Olympus_Id;
        Map<String, Integer> getModelQuantityMap = RadiationUtil.GetOpportunityLineMap(oppId);
        /*String errorStr = RadiationUtil.GetCertificationDetail(oppId,getModelQuantityMap);
        if(errorStr != ''){
           return '-1';
        }
        String accountDealerErrorMessage = RadiationUtil.updateCertificationDetails(dealerId, null, getModelQuantityMap, false);*/
        String olympusDealerErrorMessage = RadiationUtil.updateCertificationDetails(olympusAccountId, null, getModelQuantityMap, false);
        result = olympusDealerErrorMessage == '' ? '' : '仪景通可销售数量不足' ;
        //String flage = LicenseCheckUtil.LicenseCheckOly();
        Boolean temp = result.startsWith('你不能');
        if (temp) {
            return '-1';
        } else {
            return result;
        }
    }

    WebService static String checkOrderStatus(String oppId, String stageName, String creuser, String owner) {
        String temp = '1';
        Order ord = new Order();
        List<Order> ordList = new List<Order>();
        ordList = [select id, SAP_Order__c, ProductSegment__c, Is_TAX__c, SAP_Contract__c, ApproveStatus__c, CreatedById from Order where OpportunityId = : oppId and Status__c = 'Active'];
         String userId = UserInfo.getUserId().substring(0, 15);
         String roleId = UserInfo.getUserRoleId().substring(0, 15);
        if (ordList.size() > 0) {
            ord = ordList[0];
           
            if ( (ord.ApproveStatus__c == 'CancelPass' ||  ord.ApproveStatus__c == 'OrderDraft' || ord.ApproveStatus__c  == 'Draft' || ord.ApproveStatus__c == 'Reject') &&  (creuser == userId || owner == userId)) {
                StaticParameter.StageProgressBarUpdate = true;

                

                if (userId == '005280000037mrQ' || userId == '00528000002OAvH' || userId == '00528000000YSdB' ||
                        roleId == '00E0K000001ywwz' || roleId == '00E0K000001jGiM' || roleId == '00E0K000001jGib' ||
                        roleId == '00E0K000001ywgS' || roleId == '00E28000000knAm' || roleId == '00E28000000knAr' ||
                        roleId == '00E28000000aKzn' || roleId == '00E0K000001jGhn' || roleId == '00E0K000001jGhs' ||
                        roleId == '00E0K000001ywgN' || roleId == '00E0K000001PWuh' || roleId == '00E0K000001PWum' ||
                        roleId == '00E0K000001PWur' || roleId == '00E280000015xus' || roleId == '00E280000015xux' ||
                        roleId == '00E28000000aKzs' || roleId == '00E0K000001jGiC' || roleId == '00E0K000001jGiR' ||
                        roleId == '00E0K000001ywgX' || roleId == '00E28000000knAc' || roleId == '00E28000000knAh' ||
                        roleId == '00E28000000aKzx' || roleId == '00E28000000knAw' || roleId == '00E28000000knB1' ||
                        roleId == '00E28000000aL02' || roleId == '00E28000000kjpX') {

                    Opportunity opp = new Opportunity();
                    opp.Id = oppId;
                    opp.Cancel_Fail_Approve__c = 'Pass';
                    opp.StageName = stageName;

                    try {
                        StaticParameter.StageProgressBarUpdate = true;
                        update opp;
                    } catch (Exception ex) {
                        return ex.getMessage();
                    }
                } else {
                    temp = '0';
                }
            } else {
                temp = '0';
            }
        } else {
            if  ((userId == '005280000037mrQ' || userId == '00528000002OAvH' || userId == '00528000000YSdB' ||
                    roleId == '00E0K000001ywwz' || roleId == '00E0K000001jGiM' || roleId == '00E0K000001jGib' ||
                    roleId == '00E0K000001ywgS' || roleId == '00E28000000knAm' || roleId == '00E28000000knAr' ||
                    roleId == '00E28000000aKzn' || roleId == '00E0K000001jGhn' || roleId == '00E0K000001jGhs' ||
                    roleId == '00E0K000001ywgN' || roleId == '00E0K000001PWuh' || roleId == '00E0K000001PWum' ||
                    roleId == '00E0K000001PWur' || roleId == '00E280000015xus' || roleId == '00E280000015xux' ||
                    roleId == '00E28000000aKzs' || roleId == '00E0K000001jGiC' || roleId == '00E0K000001jGiR' ||
                    roleId == '00E0K000001ywgX' || roleId == '00E28000000knAc' || roleId == '00E28000000knAh' ||
                    roleId == '00E28000000aKzx' || roleId == '00E28000000knAw' || roleId == '00E28000000knB1' ||
                    roleId == '00E28000000aL02' || roleId == '00E28000000kjpX') &&  (creuser == userId || owner == userId)) {

                Opportunity opp = new Opportunity();
                opp.Id = oppId;
                opp.Cancel_Fail_Approve__c = 'Pass';
                opp.StageName = stageName;

                try {
                    StaticParameter.StageProgressBarUpdate = true;
                    update opp;
                } catch (Exception ex) {
                    return ex.getMessage();
                }
            } else {
                temp = '0';
            }
        }
        return temp;

    }



















































































































    private static void add() {
        Integer i = 0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
    }
}