public with sharing class ImprovedForecastTimeController {
    @AuraEnabled
    public static  String GetOppinformationcData(String ParamIdStr) {
        //获取数据
        List<InquiryPredictsDateChange__c> ipdList = Database.query('Select Id,Name,Opportunity__c,Date_InAdvance_Delay__c,CloseDate__c, CloseDate_Old__c, Close_Forecasted_Date__c,Predicted_date_ChangeReason__c,Predicted_Date_Status__c,Close_Forecasted_Date_Old__c, Manager_name__c,Opportunity__r.Sales_manager_departmentID__c,Opportunity__r.Sales_assistant_ID__c,Sales_assistant_name__c From InquiryPredictsDateChange__c Where Opportunity__c = : ParamIdStr order by CreatedDate desc');
        if(ipdList.size() > 0){
            OppInfo opp = new OppInfo(ipdList[0]);
            return JSON.serialize(opp);
        }else{
            Opportunity arrays = Database.query('Select Id,Name,Opportunity_No__c,CloseDate, Close_Forecasted_Date__c,Sales_manager_departmentID__c,Sales_assistant_ID__c From Opportunity Where id = : ParamIdStr');
            System.debug('lt123arrays'+arrays);
            OppInfo opp = new OppInfo(arrays);
            return JSON.serialize(opp);
        }
    }

    public class OppInfo {
        // 询价Id
        public String Opportunity { get; set; }
        // 询价编码
        public String Opportunity_No { get; set; }
        // 询价预测表Id
        public String datechangeId { get; set; }
        // 预测KPI当前值
        public Date Close_Forecasted_Date_Old { get; set; }
        // 预测KPI目标值
        public Date Close_Forecasted_Date { get; set; }
        
        // 20220830  “担当”字段弃用  只用“kpi”字段判断
        // // 预测担当当前值
        // public Date Close_Forecasted_Assume_Date_Old { get; set; }
        // // 预测担当目标值
        // public Date Close_Forecasted_Assume_Date { get; set; }

        // 预测发货当前值
        public Date CloseDate_Old { get; set; }
        // 预测发货目标值
        public Date CloseDate { get; set; }
        // 提前/延后
        public String Date_InAdvance_Delay { get; set; }
        // 原因
        public String Predicted_date_ChangeReason { get; set; }
        // cl6经理
        public String Manager_name { get; set; }
        // 助理
        public String Sales_assistant_name { get; set; }
        // 状态
        public String Predicted_Date_Status { get; set; }

        // 增加一个字段 用来做判断用的
        public Date Close_Forecasted_Check { get; set; }
    
        // 从询价上获得数据
        public OppInfo(Opportunity opp) {
            Opportunity = opp.Id;
            Opportunity_No = opp.Opportunity_No__c;
            Close_Forecasted_Date_Old = opp.Close_Forecasted_Date__c;
            // Close_Forecasted_Assume_Date_Old = opp.Close_Forecasted_Assume_Date__c;
            CloseDate_Old = opp.CloseDate;
            Manager_name = opp.Sales_manager_departmentID__c;
            //Sales_assistant_ID__c替换Sales_assistant_name_text__c
            Sales_assistant_name = opp.Sales_assistant_ID__c;

            //20220830  “担当”字段弃用  只用“kpi”字段判断
            //Close_Forecasted_Check = opp.Close_Forecasted_Assume_Date__c != null ? opp.Close_Forecasted_Assume_Date__c : opp.Close_Forecasted_Date__c;
            Close_Forecasted_Check = opp.Close_Forecasted_Date__c;
        }
        // 从询价预测上获得数据
        public OppInfo(InquiryPredictsDateChange__c datechange) {
            // 把当前已有的值都设置上
            Opportunity = datechange.Opportunity__c;
            Opportunity_No = datechange.Name;
            datechangeId = datechange.Id;

            Close_Forecasted_Date_Old = datechange.Predicted_Date_Status__c == '已批准' ? datechange.Close_Forecasted_Date__c : datechange.Close_Forecasted_Date_Old__c;
            // Close_Forecasted_Assume_Date_Old = datechange.Predicted_Date_Status__c == '已批准' ? datechange.Close_Forecasted_Assume_Date__c : datechange.Close_Forecasted_Assume_Date_Old__c;
            Close_Forecasted_Date = datechange.Close_Forecasted_Date__c;
            // Close_Forecasted_Assume_Date = datechange.Close_Forecasted_Assume_Date__c;
            CloseDate_Old = datechange.Predicted_Date_Status__c == '已批准' ? datechange.CloseDate__c : datechange.CloseDate_Old__c;
            CloseDate = datechange.CloseDate__c;

            if(datechange.Predicted_Date_Status__c != '已批准'){
                // Close_Forecasted_Check = datechange.Close_Forecasted_Assume_Date_Old__c != null ? datechange.Close_Forecasted_Assume_Date_Old__c : datechange.Close_Forecasted_Date_Old__c;
                Close_Forecasted_Check = datechange.Close_Forecasted_Date_Old__c;
            }else {
                // Close_Forecasted_Check = datechange.Close_Forecasted_Assume_Date__c != null ? datechange.Close_Forecasted_Assume_Date__c : datechange.Close_Forecasted_Date__c;
                Close_Forecasted_Check = datechange.Close_Forecasted_Date__c;
            }
            
            //Sales_manager_departmentID__c替换Manager_name_text__c
            Manager_name = datechange.Opportunity__r.Sales_manager_departmentID__c;
            Sales_assistant_name = datechange.Opportunity__r.Sales_assistant_ID__c;

            Date_InAdvance_Delay = datechange.Date_InAdvance_Delay__c;
            Predicted_date_ChangeReason = datechange.Predicted_date_ChangeReason__c;
            Predicted_Date_Status = datechange.Predicted_Date_Status__c;
        }
    }

    // 把页面上的数据赋值到询价预测的对象上
    private static InquiryPredictsDateChange__c mergeInfo(Map<String, Object> oppMap) {
        InquiryPredictsDateChange__c datechange = new InquiryPredictsDateChange__c();

        if (String.isBlank(String.valueOf(oppMap.get('Predicted_Date_Status'))) || !'已批准'.equals(String.valueOf(oppMap.get('Predicted_Date_Status')))) {
            datechange.Id = String.valueOf(oppMap.get('datechangeId'));
        } 
        if (String.isBlank(datechange.Id)){
            datechange.Opportunity__c = String.valueOf(oppMap.get('Opportunity'));
            datechange.Name = String.valueOf(oppMap.get('Opportunity_No'));
        }
        // 当前的日期都从对象上获取 
        if(oppMap.get('Close_Forecasted_Date_Old') != null){
            datechange.Close_Forecasted_Date_Old__c = Date.valueOf(String.valueOf(oppMap.get('Close_Forecasted_Date_Old')));
        }
        // if(oppMap.get('Close_Forecasted_Assume_Date_Old') != null){
        //     datechange.Close_Forecasted_Assume_Date_Old__c = Date.valueOf(String.valueOf(oppMap.get('Close_Forecasted_Assume_Date_Old')));
        // }
        if(oppMap.get('CloseDate_Old') != null){
            datechange.CloseDate_Old__c = Date.valueOf(String.valueOf(oppMap.get('CloseDate_Old')));
        }
        // 新的日期全跟预测担当有关
        datechange.Close_Forecasted_Date__c = Date.valueOf(String.valueOf(oppMap.get('Close_Forecasted_Date')));
        // datechange.Close_Forecasted_Assume_Date__c = Date.valueOf(String.valueOf(oppMap.get('Close_Forecasted_Assume_Date')));
        datechange.CloseDate__c = Date.valueOf(String.valueOf(oppMap.get('CloseDate')));

        datechange.Manager_name__c = String.valueOf(oppMap.get('Manager_name'));
        datechange.Sales_assistant_name__c = String.valueOf(oppMap.get('Sales_assistant_name'));

        datechange.Date_InAdvance_Delay__c = String.valueOf(oppMap.get('Date_InAdvance_Delay'));
        datechange.Predicted_date_ChangeReason__c = String.valueOf(oppMap.get('Predicted_date_ChangeReason'));

        // 状态设置为已提交
        datechange.Predicted_Date_Status__c = '已提交';

        return datechange;
    }

    @AuraEnabled
    public static String getReason(){
        return System.Label.DateChangeReason;
    }

    //保存数据  JSONData 是 json格式的值 
    @AuraEnabled
    public  static string  SaveData(String JsonData){
        try{
            // InquiryPredictsDateChange__c ipd = (InquiryPredictsDateChange__c)JSON.deserialize(JsonData,InquiryPredictsDateChange__c.class);
            // OppInfo opp = (OppInfo)JSON.deserializeUntyped(JsonData);
            Map<String, Object> oppMap = (Map<String, Object>)JSON.deserializeUntyped(JsonData);
            System.debug('lt123JsonData'+JsonData);
            InquiryPredictsDateChange__c ipd = mergeInfo(oppMap);
            // ipd.Predicted_Date_Status__c = '已提交';
            upsert ipd;
        }catch(Exception e)
        {
            if (e.getMessage().contains('EXCEPTION,')) {
                System.debug(LoggingLevel.INFO, '*** e: ' + e);
                String exc = '' + e.getMessage();
                Integer left = exc.indexOf('EXCEPTION,') + 10;
                Integer right = exc.lastIndexOf('」: [') + 1;
                String str = exc.substring(left,right);
                // left = str.indexOf(',') +  1;
                // String newStr = str.substring(left);
                return str;
            }else {
                return e.getMessage();   
            }
        }
        return  '成功';
    }
}