global class TotalPriceWebService {
    WebService static String canAddMc(String recordId) {
        List<MaintanceContractPackEstimate__c> mcpeList = [Select Id  from MaintanceContractPackEstimate__c Where MaintanceContractPack__c = :recordId and Decide__c =true limit 1];
        if(mcpeList.size()>0) {
            return '2'; //有decide的报价组合之后，不能添加小合同
        } else {
            List<MaintanceContractPackEstimate__c> mcpeStatusList = 
            [Select Id  from MaintanceContractPackEstimate__c Where MaintanceContractPack__c = :recordId and ( Process_Status__c='申请中' OR  Process_Status__c='批准' )  limit 1];
            if(mcpeStatusList.size()>0) { //有不是草案中的报价组合，提示重新做报价
                return '3';
            }
            
        }
        return '1';
    } 
    /*WebService static String updateColunm(String mcpid ){
        //当前维修合同
       List<Maintenance_Contract__c> mcList = [Select Id,Submit_contract_to_Financial_dept__c,HP_Dealer_print__c,Contract_print_completed__c,OCM_Print_request__c,GuidePrice_DownF__c,GuidePrice_UpF__c,Request_quotation_AmountF__c,
                                                Contract_Start_Date__c,Contract_End_Date__c,Limit_Price_Amount__c,Contract_Amount__c,Asset_Repair_Sum_PriceF__c from Maintenance_Contract__c 
                                                    where MaintanceContractPack__c = :mcpid];
         MaintanceContractPack__c mcp = new MaintanceContractPack__c();
         mcp.id = mcpid;
         Decimal GuidePrice_DownF = 0;
         Decimal GuidePrice_UpF = 0;
         Decimal FieldRequest_quotation_Amount = 0;
         Decimal Limit_Price_Amount = 0;
         Decimal Asset_Repair_Sum_PriceF = 0;
         Decimal Contract_Amount = 0;
         Date Contract_Start_Date ;
         Date Contract_End_Date ;
         Date OCM_Print_request ;
         Date Contract_print_completed ;
         Date HP_Dealer_print ;
         Date Submit_contract_to_Financial_dept ;
         for ( Maintenance_Contract__c mc :mcList) {
            //增加上限金额汇总
            if (mc.Limit_Price_Amount__c !=null) {
                Limit_Price_Amount += mc.Limit_Price_Amount__c;
            }
            if (mc.Limit_Price_Amount__c !=null) {
                Asset_Repair_Sum_PriceF += mc.Asset_Repair_Sum_PriceF__c;
            }
             if (mc.Limit_Price_Amount__c !=null) {
                Contract_Amount += mc.Contract_Amount__c;
            }
            if (mc.GuidePrice_DownF__c !=null) {
                GuidePrice_DownF += mc.GuidePrice_DownF__c;
            }
            if (mc.GuidePrice_UpF__c !=null) {
                GuidePrice_UpF += mc.GuidePrice_UpF__c;
            }    
            if (mc.Request_quotation_AmountF__c!=null) {
                FieldRequest_quotation_Amount += mc.Request_quotation_AmountF__c; 
            }
               
            if (Contract_Start_Date == null&&mc.Contract_Start_Date__c!=null) {
                Contract_Start_Date = mc.Contract_Start_Date__c;             
            }            
             // 取小合同最最早开始的合同开始日
             if (Contract_Start_Date > mc.Contract_Start_Date__c && Contract_Start_Date !=null && mc.Contract_Start_Date__c != null) {
                 Contract_Start_Date = mc.Contract_Start_Date__c;
             }
             if (mc.Contract_End_Date__c!=null) {
                Contract_End_Date = mc.Contract_End_Date__c;   
             }
             if (mc.OCM_Print_request__c!=null) {
                OCM_Print_request = mc.OCM_Print_request__c;
             }
             if (mc.Contract_print_completed__c!=null) {
                 Contract_print_completed = mc.Contract_print_completed__c; 
             }
            if (mc.HP_Dealer_print__c!=null) {
                  HP_Dealer_print = mc.HP_Dealer_print__c;
            }
            if (mc.Submit_contract_to_Financial_dept__c!=null) {
                Submit_contract_to_Financial_dept = mc.Submit_contract_to_Financial_dept__c;  
            } 
          }
               mcp.GuidePrice_DownF__c = GuidePrice_DownF; 
               mcp.GuidePrice_UpF__c = GuidePrice_UpF; 
               mcp.FieldRequest_quotation_Amount__c = FieldRequest_quotation_Amount;
               mcp.Contract_Amount__c = Contract_Amount; 
               mcp.Asset_Repair_Sum_PriceF__c = Asset_Repair_Sum_PriceF; 
               mcp.Limit_Price_Amount__c = Limit_Price_Amount; 
               if (Contract_Start_Date!=null) {
                   mcp.Contract_Start_Date__c = Contract_Start_Date;                           
               }                           
               if (Contract_End_Date!=null) {
                  mcp.Contract_End_Date__c = Contract_End_Date;                             
               }
               if (OCM_Print_request!=null) {
                  mcp.OCM_Print_request__c = OCM_Print_request;                             
               }
               if (Contract_print_completed!=null) {
                    mcp.Contract_print_completed__c = Contract_print_completed;                          
               }
               if (HP_Dealer_print!=null) {
                  mcp.HP_Dealer_print__c = HP_Dealer_print;                             
               } 
               if (Submit_contract_to_Financial_dept!=null) {
                 mcp.Submit_contract_to_Financial_dept__c = Submit_contract_to_Financial_dept;                              
               }

         try{
            update mcp;
            return '1';
         }catch(exception e){
            return e.getMessage();
         }
    }
    //服务合同打包-跳转到提交待审批
    // WebService static Map<String,Maintenance_Contract_Estimate__c> toMcpAprroval(String mcpid ){

    //     Maintenance_Contract_Estimate__c mce = new Maintenance_Contract_Estimate__c();
    //     Map<String,Maintenance_Contract_Estimate__c> maps = new Map<String,Maintenance_Contract_Estimate__c>();
    //     maps.put('1',mce);

    //     retrun maps;
    // }

    //服务合同打包-提交待审批
    WebService static String mcpAprroval(String mcpid ){
        Decimal consumptionRateForecast;
        Decimal GuidePrice_DownF = 0;
        Decimal GuidePrice_UpF = 0;
        Decimal FieldRequest_quotation_Amount = 0;
        Decimal Limit_Price_Amount = 0;
        Decimal Asset_Repair_Sum_PriceF = 0;
        Decimal Contract_Amount = 0;

        Decimal totalPriceNumerator = 0;
        Decimal totalPriceDenominator = 0;
        Decimal monthlyRepairCostSUM = 0;
        Decimal contractRange = 0;
        Decimal costrateForecast = 0; 
        String estimatePriceRange;
        String contractPriceType; 
        

        //当前维修合同(是否存在报价decide的情况)
        List<Maintenance_Contract__c> mcList = [Select Id,Name,Decided_Estimation__c,Submit_contract_to_Financial_dept__c,HP_Dealer_print__c,Contract_print_completed__c,OCM_Print_request__c,GuidePrice_DownF__c,GuidePrice_UpF__c,Request_quotation_AmountF__c,
                                                Contract_Start_Date__c,Contract_End_Date__c,Limit_Price_Amount__c,Contract_Amount__c,Asset_Repair_Sum_PriceF__c from Maintenance_Contract__c 
                                                    where MaintanceContractPack__c = :mcpid];
        for(Maintenance_Contract__c mc :mcList) {
            if(mc.Decided_Estimation__c !=null) {
                return mc.Name+':报价已经decide，请先取消decide,再提交审批！';
            }
        }                                            
        //合同报价 
        MaintanceContractPack__c mcpUpdate = new MaintanceContractPack__c();
        mcpUpdate.Id = mcpid;
        

        
        String querySoql = 'Select Id,Name,Contract_Range__c,Request_quotation_Amount__c,Monthly_Repair_Cost_SUM__c, from Maintenance_Contract_Estimate__c where Id in(Select Estimation_Id__c from Maintenance_Contract__c  where MaintanceContractPack__c = :mcpid)';
        System.debug('-------querySoql' + querySoql);
        List<Maintenance_Contract_Estimate__c> mceList = Database.query(querySoql);
        List<Maintenance_Contract_Estimate__c> updateMceList = new List<Maintenance_Contract_Estimate__c>();

        for(Maintenance_Contract_Estimate__c mce :mceList) {
           
            if (mce.Monthly_Repair_Cost_SUM__c !=null) {
                monthlyRepairCostSUM = mce.Monthly_Repair_Cost_SUM__c;
            }
            if (mce.Contract_Range__c !=null) {
                contractRange = mce.Contract_Range__c;
            }
            if (mce.Request_quotation_Amount__c !=null && mce.Request_quotation_Amount__c!=0) {
               // 计算分子
                totalPriceNumerator += (monthlyRepairCostSUM*contractRange*30);
               // 计算分母
                totalPriceDenominator += mce.Request_quotation_Amount__c;
            }

        }
        if (totalPriceDenominator != 0) {
            // 预测消费率
            consumptionRateForecast = ((totalPriceNumerator/totalPriceDenominator)*100);
            mcpUpdate.Consumption_rate_Forecast__c = ((totalPriceNumerator/totalPriceDenominator)*100);
            // 预测成本率
            costrateForecast = (totalPriceNumerator/totalPriceDenominator)*0.75*100;
            mcpUpdate.Cost_rate_Forecast__c = costrateForecast;
        }

        
        for ( Maintenance_Contract__c mc :mcList) {
            if (mc.Limit_Price_Amount__c !=null) {
                Limit_Price_Amount += mc.Limit_Price_Amount__c;
            }
            if (mc.Limit_Price_Amount__c !=null) {
                Asset_Repair_Sum_PriceF += mc.Asset_Repair_Sum_PriceF__c;
            }
             if (mc.Limit_Price_Amount__c !=null) {
                Contract_Amount += mc.Contract_Amount__c;
            }
            if (mc.GuidePrice_DownF__c !=null) {
                GuidePrice_DownF += mc.GuidePrice_DownF__c;
            }
            if (mc.GuidePrice_UpF__c !=null) {
                GuidePrice_UpF += mc.GuidePrice_UpF__c;
            }    
            if (mc.Request_quotation_AmountF__c!=null) {
                FieldRequest_quotation_Amount += mc.Request_quotation_AmountF__c; 
            }
        }
         

        if(FieldRequest_quotation_Amount < GuidePrice_DownF){
            if( costrateForecast > 1) {
                estimatePriceRange = '低于最低价格,预测成本率在100%以上';
            } else {
                estimatePriceRange = '低于最低价格,预测成本率在100%以内';

            }
        } else {
            if( FieldRequest_quotation_Amount >= GuidePrice_DownF && FieldRequest_quotation_Amount <= GuidePrice_UpF){
                estimatePriceRange = '标准价格的范围内';
            } else {
                if(FieldRequest_quotation_Amount <= GuidePrice_UpF * 2){
                    estimatePriceRange = '最高价格的200%以内';
                } else{
                    estimatePriceRange = '最高价格的200%以上';
                }

            }
        } 
        if(estimatePriceRange == '标准价格的范围内') {
            contractPriceType = '标准价格范围内-终批人营业经理';
        } else {
            if(estimatePriceRange.contains('以内')) {
                contractPriceType = '特价范围-批人服务总监';
            } else {
                 contractPriceType = '特价范围-终批人总经理';
            }
        }
       

        mcpUpdate.Estimate_Price_range__c = estimatePriceRange;
        mcpUpdate.ContractPriceType__c = contractPriceType;

        //更新打包合同的cl3 4 5 6 
        User loginUser = [SELECT Id, Name, JingliApprovalManager__c, BuchangApprovalManager__c, ZongjianApprovalManager__c, TongkuoZongjian__c FROM User WHERE Id = :UserInfo.getUserId()];
        mcpUpdate.JingliApprovalManager__c = loginUser.JingliApprovalManager__c == null ? loginUser.Id : loginUser.JingliApprovalManager__c;
        mcpUpdate.BuchangApprovalManager__c = loginUser.BuchangApprovalManager__c == null ? loginUser.Id : loginUser.BuchangApprovalManager__c;
        mcpUpdate.ZongjianApprovalManager__c = loginUser.ZongjianApprovalManager__c == null ? loginUser.Id : loginUser.ZongjianApprovalManager__c;
        // mcpUpdate.Service_Contract_Staff__c = this.contract.Service_Contract_Staff__c;
        mcpUpdate.TKZongjianApprovalManager__c = loginUser.TongkuoZongjian__c == null ? loginUser.Id : loginUser.TongkuoZongjian__c;

        try{
            update mcpUpdate;
            Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
            psr.setObjectId(mcpid);
            Approval.ProcessResult submitResult = Approval.process(psr);
            
            return '1';
           
        }catch(exception e){
            return e.getMessage();

        } 

    }
*/
}