public with sharing class LexChoiceAssetController {
    public class SSPLine {
        @AuraEnabled
        public Boolean isSelected;
        @AuraEnabled
        public String setProductid ;
        @AuraEnabled
        public String Assetid;
        @AuraEnabled
        public String Accountid;
        @AuraEnabled
        public String Hospital;
         @AuraEnabled
        public String WorkLocation;
        @AuraEnabled
        public Asset theObject;
        @AuraEnabled
        public String Name;      
        @AuraEnabled
        public String SerialNumber;
        @AuraEnabled
        public String HP_Name;
        @AuraEnabled
        public String Department_Name;
        @AuraEnabled
        public String DepartmentClass;
        @AuraEnabled
        public Date Posting_Date;
        @AuraEnabled
        public Date Extend_Gurantee_DateTo;
        @AuraEnabled
        public Date CurrentContract_End_Date;
        @AuraEnabled
        public Date NoPartRiskDate;


        public SSPLine(Asset psl) {
          isSelected = false;
          setProductid = psl.id;
            setProductid = setProductid.substring(0,15);
            Assetid = psl.id;
            if (psl.Account.id !=null) {
                Accountid = psl.Account.id;
            }
            Name = psl.Name;
            NoPartRiskDate = psl.product2.PartSupplyFinishDate__c;
            Hospital = psl.Hospital__r.id;
            if (psl.Account.RecordType.DeveloperName !=null && psl.Account.RecordType.DeveloperName!='') {
                if (psl.Account.RecordType.DeveloperName == 'Department_Class_GI' || 
                    psl.Account.RecordType.DeveloperName == 'Department_Class_ET' ||
                    psl.Account.RecordType.DeveloperName == 'Department_Class_BF' ||
                    psl.Account.RecordType.DeveloperName == 'Department_Class_GS' ||
                    psl.Account.RecordType.DeveloperName == 'Department_Class_URO' ||
                    psl.Account.RecordType.DeveloperName == 'Department_Class_GYN' ||
                    psl.Account.RecordType.DeveloperName == 'Department_Class_ENT' ||
                    psl.Account.RecordType.DeveloperName == 'Department_Class_OTH' ||
                    psl.Account.RecordType.DeveloperName == 'RecordType_8' ) {
                        if (psl.Account.Parent.FSE_SP_Main_Leader__r.Work_Location__c !=null) {
                            WorkLocation = psl.Account.Parent.FSE_SP_Main_Leader__r.Work_Location__c;
                        }
                }
                if (psl.Account.RecordType.DeveloperName == 'Department_ENT' || 
                    psl.Account.RecordType.DeveloperName == 'Department_GI' ||
                    psl.Account.RecordType.DeveloperName == 'Department_BF' ||
                    psl.Account.RecordType.DeveloperName == 'Department_GS' ||
                    psl.Account.RecordType.DeveloperName == 'Department_URO' ||
                    psl.Account.RecordType.DeveloperName == 'Department_GYN' ||
                    psl.Account.RecordType.DeveloperName == 'Department_OTH' ) {
                        if (psl.Account.Parent.Parent.FSE_SP_Main_Leader__r.Work_Location__c !=null) {
                            WorkLocation = psl.Account.Parent.Parent.FSE_SP_Main_Leader__r.Work_Location__c;
                        }
                }
            }
            
            
            SerialNumber= psl.SerialNumber;
            HP_Name = psl.HP_Name__c;
            DepartmentClass = psl.Department_Class__c;
            Department_Name = psl.Department_Name__c;
            Posting_Date = psl.Posting_Date__c;
            Extend_Gurantee_DateTo = psl.Extend_Gurantee_DateTo__c;
            CurrentContract_End_Date = psl.CurrentContract_End_Date__c;
        }
    }
    public class InitData{
        @AuraEnabled
        public String Id;
        @AuraEnabled
        public String AssetCode;
        @AuraEnabled
        public String StatusC;
        @AuraEnabled
        public String OwnerId;
        @AuraEnabled
        public String Repair_Detail;
        @AuraEnabled
        public Date FaultTime;
        @AuraEnabled
        public String RepairApplicantName;
        //2023 08 21 Zhang Heyang PIPL按钮改造 strat
        //@AuraEnabled
        //public String AWSId;      //deloitte-zhj 20231205 PIPL还原
        //2023 08 21 Zhang Heyang PIPL按钮改造 end
        @AuraEnabled
        public String HPname;
        @AuraEnabled
        public String DepName;
        @AuraEnabled
        public String IfDeadHurt;
        @AuraEnabled
        public String ReturnWayType;
        @AuraEnabled
        public String ProblemOccurred;


        @AuraEnabled
        public String ProblemOccurredSelect;
        // hql 20231213 添加贸易合规 start
        @AuraEnabled
        public String accDealerBlacklist;
        // hql 20231213 添加贸易合规 end
        @AuraEnabled
        public String SupportingProducts;
        @AuraEnabled
        public String ReportAdverseEvents;
        @AuraEnabled
        public String WhatProject;
        @AuraEnabled
        public String OperationOrExaminationName;
        @AuraEnabled
        public String BreakORFallOff;
        @AuraEnabled
        public String UseFailProductFinish;
        @AuraEnabled
        public String AfterFailureInformation;
        @AuraEnabled
        public String Delay15Min;
        @AuraEnabled
        public String InformationFrom;
        @AuraEnabled
        public String FailureQInHospital;
        @AuraEnabled
        public String DelayReportReason;
        @AuraEnabled
        public String ProductFailureRelated;
        @AuraEnabled
        public String DeliveryLogisticsNo;
        @AuraEnabled
        public Date engineerSendDate;
        @AuraEnabled
        public String engineerSendDateStr;// lwt 20240301 避免js自动计算时差

//deloitte-zhj 20231129 PIPL还原
        @AuraEnabled
        public DateTime engineerSendDateTime;


        @AuraEnabled
        public Date RepairsReportDate;
        @AuraEnabled
        public Date ReceiverTime;
        @AuraEnabled
        public Date DateReceiptQuestions;
        @AuraEnabled
        public Date PlannedVisitDay;
        @AuraEnabled
        public Date ActualVisitDateFirst;
         @AuraEnabled
        public Date ActualVisitDateSecon;
         @AuraEnabled
        public Date ActualVisitDateThird;
         @AuraEnabled
        public Date ApplicationTime;
        @AuraEnabled
        public Date currentDate;

        //deloitte-zhj 20231206 PIPL还原带上直返地址信息
        @AuraEnabled
        public String ReturnedPersonName;
        @AuraEnabled
        public String ReturnedPersonPhone;
        @AuraEnabled
        public String ReturnedCustomer;
        @AuraEnabled
        public String ReturnedCity;
        @AuraEnabled
        public String ReturnedAddress;
        @AuraEnabled
        public String ReturnedZipCode;
        
        @AuraEnabled
        public String AccountName; // WYL 贸易合规2期 add
        @AuraEnabled
        public String state; // WYL 贸易合规2期 add
     }

     public class AssData{
        @AuraEnabled
        public String canRepair;
        @AuraEnabled
        public String proTradeComplianceStatus;//WYl 贸易合规二期修改  add
        @AuraEnabled
        public String Asset_Model_No;//WYl 贸易合规二期修改  add
     }

     @AuraEnabled
    public static AssData searchAss(String recordId){
        AssData res = new AssData();
        List<Asset> ass = [select id,Product_ID__c from Asset where ID = :recordId];
        List<Product2> p2 = [select id,Name,Can_Repair__c,
                          // WYL 贸易合规二期修改  start
                          ProTradeComplianceStatus__c,Asset_Model_No__c
                           // WYL 贸易合规二期修改  end
                        from Product2 where 
                        ID = :ass[0].Product_ID__c];
        if (p2[0].Can_Repair__c != null) {
            res.canRepair = p2[0].Can_Repair__c;
            //20231218  sx 贸易合规二期修改  start
            res.proTradeComplianceStatus = p2[0].ProTradeComplianceStatus__c;
            res.Asset_Model_No = p2[0].Asset_Model_No__c;
            //20231218  sx 贸易合规二期修改  end
        }
        return res;
    }
    @AuraEnabled
    public static InitData init(String recordId){
        InitData res = new initData();
        try{
            //2023 08 21 Zhang Heyang PIPL按钮改造 strat --- 添加查询中字段AWS_Data_Id__c
//deloitte-zhj 20231122 PIPL还原 去掉AWS_Data_Id__c
            //deloitte-zhj 20231206 PIPL还原带上直返地址信息
            RepairSubOrder__c report =  [SELECT Id,ProblemOccurred__c,ReturnWayType__c,IfDeadHurt__c,SupportingProducts__c,
                                            AirframeCodeEngineer__c,AssetCode__c ,ApplicationTime__c,ActualVisitDateThird__c,ActualVisitDateSecon__c,
                                            ActualVisitDateFirst__c,PlannedVisitDay__c,ReceiverTime__c,RepairsReportDate__c,Status__c,Department__r.Name,
                                            ProblemOccurredSelect__c,ReportAdverseEvents__c,WhatProject__c,OperationOrExaminationName__c,BreakORFallOff__c,
                                            // hql 20231213 添加贸易合规 start
                                            AccBlacklist__c,
                                            // hql 20231213 添加贸易合规 end
                                            // WYL 贸易合规2期 start
                                            Hospital__r.TradeComplianceStatus__c,
                                             // WYL 贸易合规2期 start
                                            UseFailProductFinish__c,AfterFailureInformation__c,Delay15Min__c,InformationFrom__c,FailureQInHospital__c,
                                            DelayReportReason__c,IsProductFaultRelated__c,LogisticsSendDate__c,OrderNumber__c,DateReceiptQuestions__c,
                                            Hospital__r.name,RepairApplicantName__c,OwnerId,ResponseResultDesc__c,FaultTime__c,
                                            ReturnedPersonName__c,ReturnedPersonPhone__c,ReturnedCustomer__c,ReturnedCity__c,ReturnedAddress__c,ReturnedZipCode__c FROM RepairSubOrder__c WHERE Id =: recordId LIMIT 1];
            //2023 08 21 Zhang Heyang PIPL按钮改造 End
            System.debug(LoggingLevel.INFO, '*** opp: ' + report);
            res.Id = report.Id;
            res.StatusC = report.Status__c;
            res.OwnerId = report.OwnerId;
            res.AssetCode = report.AirframeCodeEngineer__c;
            // hql 20231213 添加贸易合规 start
            res.accDealerBlacklist = report.AccBlacklist__c;
            // hql 20231213 添加贸易合规 start
            res.Repair_Detail = report.ResponseResultDesc__c;
            if (report.FaultTime__c!=null) {
                res.FaultTime = report.FaultTime__c.date();
            }
            res.RepairApplicantName = report.RepairApplicantName__c;
            //2023 08 21 Zhang Heyang PIPL按钮改造 strat
            //res.AWSId = report.AWS_Data_Id__c;   //deloitte-zhj 20231122 PIPL还原 去掉AWS_Data_Id__c
            //2023 08 21 Zhang Heyang PIPL按钮改造 end
            if (report.Hospital__r.name!=null) {
                res.HPname = report.Hospital__r.name;
            }
            if (report.Department__r.Name!=null) {
                res.DepName = report.Department__r.Name;
            }
            res.RepairsReportDate = report.RepairsReportDate__c;
            if (report.ReceiverTime__c!=null) {
                res.ReceiverTime = report.ReceiverTime__c.date();
            }
            if (report.PlannedVisitDay__c!=null) {
                res.PlannedVisitDay = report.PlannedVisitDay__c.date();
            }
            res.ActualVisitDateFirst = report.ActualVisitDateFirst__c;
            res.ActualVisitDateSecon = report.ActualVisitDateSecon__c;
            res.ActualVisitDateThird = report.ActualVisitDateThird__c;
            res.SupportingProducts = report.SupportingProducts__c;
            res.ProblemOccurredSelect = report.ProblemOccurredSelect__c;
            res.ReportAdverseEvents = report.ReportAdverseEvents__c;
            res.WhatProject = report.WhatProject__c;
            res.OperationOrExaminationName = report.OperationOrExaminationName__c;
            res.BreakORFallOff = report.BreakORFallOff__c;
            res.UseFailProductFinish = report.UseFailProductFinish__c;
            res.AfterFailureInformation = report.AfterFailureInformation__c;
            res.Delay15Min = report.Delay15Min__c;
            res.InformationFrom = report.InformationFrom__c;
            res.FailureQInHospital = report.FailureQInHospital__c;
            res.DelayReportReason = report.DelayReportReason__c;
            res.ProductFailureRelated = report.IsProductFaultRelated__c;
//deloitte-zhj 20231129 PIPL还原 start
            res.engineerSendDateTime = DateTime.newInstance(report.LogisticsSendDate__c, Time.newInstance(0, 0, 0, 0));
            //deloitte-zhj 20231129 PIPL还原 end
            res.engineerSendDate = report.LogisticsSendDate__c;
            //lwt 20240301 避免js自动计算时差 start
            if(report.LogisticsSendDate__c!=null){
                res.engineerSendDateStr = report.LogisticsSendDate__c.year()+'-'+ report.LogisticsSendDate__c.month()+'-'+report.LogisticsSendDate__c.day();
            }
            //lwt 20240301 避免js自动计算时差 end
            res.DeliveryLogisticsNo = report.OrderNumber__c;
            res.IfDeadHurt = report.IfDeadHurt__c;
            res.ReturnWayType = report.ReturnWayType__c;
            res.ProblemOccurred = report.ProblemOccurred__c;
            res.DateReceiptQuestions = report.DateReceiptQuestions__c;
            res.AccountName =  report.Hospital__r.name; // WYL 贸易合规2期 add
            res.state = report.Hospital__r.TradeComplianceStatus__c; // WYL 贸易合规2期 add
            res.currentDate = Date.today();
            if (report.ApplicationTime__c!=null) {
                  res.ApplicationTime = report.ApplicationTime__c.date();
            }

            //deloitte-zhj 20231206 PIPL还原带上直返地址信息
            res.ReturnedPersonName = report.ReturnedPersonName__c;
            res.ReturnedPersonPhone = report.ReturnedPersonPhone__c;
            res.ReturnedCustomer = report.ReturnedCustomer__c;
            res.ReturnedCity = report.ReturnedCity__c;
            res.ReturnedAddress = report.ReturnedAddress__c;
            res.ReturnedZipCode = report.ReturnedZipCode__c;
            System.debug(LoggingLevel.INFO, '*** res: ' + res);
        }catch(Exception e){
            System.debug(LoggingLevel.INFO, '*** e: ' + e);
        }
        return res;
    }  

    @AuraEnabled
    public static String getNeedParm1(){
        String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        return baseUrl;
    }

  //   @AuraEnabled
  //   public static String init(){
  //       String reportid;
  //       Report r = new Report();
  //       List<Report> rs = new List<Report>();
  //       rs=[Select id,DeveloperName,Name,NamespacePrefix from Report Where DeveloperName='Set_Product_Detail'];
  //       if (rs.size()>0){
  //           //system.debug('DeveloperName=' + rs[0].DeveloperName);
  //           //system.debug('Name=' + rs[0].Name);
  //           //system.debug('NamespacePrefix=' + rs[0].NamespacePrefix);
  //           reportid = rs[0].id;
  //           reportid = reportid.substring(0,15);
  //       }
  //       return reportid;
  // }

  @AuraEnabled
  public static List<SSPLine> getData(String SearchName , String SearchNo , String SearchSN){
    String searchSql = 'Select id,Account.RecordType.DeveloperName, product2.PartSupplyFinishDate__c,Department_Class__c,name,Account.id,Account.Parent.Parent.FSE_SP_Main_Leader__r.Work_Location__c,Account.Parent.FSE_SP_Main_Leader__r.Work_Location__c,Hospital__r.id, SerialNumber,HP_Name__c,Department_Name__c,Posting_Date__c,Extend_Gurantee_DateTo__c,CurrentContract_End_Date__c from Asset ';
    String whereStr = 'Where  id !=null AND Repairing_Count__c = 0  ';
    String whereSql = '';
    String SearchNameTrim;
    String SearchNoTrim;
    String SearchSNTrim;
    if (String.isNotBlank(SearchName)) {
         SearchNameTrim = SearchName.trim();
    }
    if (String.isNotBlank(SearchNo)) {
         SearchNoTrim = SearchNo.trim();
    }
    if (String.isNotBlank(SearchSN)) {
         SearchSNTrim = SearchSN.trim();
    }
    if(SearchNameTrim != null && SearchNameTrim != ''){
      whereSql += 'and HP_Name__c Like ' + '\'%' + SearchNameTrim + '%\' ';
    }

    if(SearchNoTrim != null && SearchNoTrim != ''){
      whereSql += 'and OwnershipMachine_No__c Like' + '\'%' + SearchNoTrim + '\'';
    }

    if(SearchSNTrim != null && SearchSNTrim != ''){
      whereSql += 'and SerialNumber like ' + '\'%' + SearchSNTrim + '%\' ';
    }
    searchSql = searchSql + whereStr + whereSql;
    //查询太久
    // searchSql += ' order by Posting_Date__c limit 30';
    if (whereSql !=null && whereSql !='') {
        searchSql += ' limit 10';
    }else{
        searchSql += ' limit 100';
    }
    


    List<Asset> cl = new List<Asset>();

    cl = Database.query(searchSql);
    List<SSPLine> activities = New List<SSPLine>();

    for(Asset psl : cl){
        SSPLine a = new SSPLine(psl);
        activities.add(a);
        
    }
    return activities;
  }

  @AuraEnabled
  public static List<SSPLine> getDatainit(String SearchName , String SearchNo , String SearchSN){
    String searchSql = 'Select id,Account.RecordType.DeveloperName, product2.PartSupplyFinishDate__c,Department_Class__c,name,Account.id,Account.Parent.Parent.FSE_SP_Main_Leader__r.Work_Location__c,Account.Parent.FSE_SP_Main_Leader__r.Work_Location__c,Hospital__r.id, SerialNumber,HP_Name__c,Department_Name__c,Posting_Date__c,Extend_Gurantee_DateTo__c,CurrentContract_End_Date__c from Asset ';
    String whereStr = 'Where  id !=null AND Repairing_Count__c >= 0  ';
    String whereSql = '';
    String SearchNameTrim;
    String SearchNoTrim;
    String SearchSNTrim;
    if (String.isNotBlank(SearchName)) {
         SearchNameTrim = SearchName.trim();
    }
    if (String.isNotBlank(SearchNo)) {
         SearchNoTrim = SearchNo.trim();
    }
    if (String.isNotBlank(SearchSN)) {
         SearchSNTrim = SearchSN.trim();
    }
    if(SearchNameTrim != null && SearchNameTrim != ''){
      whereSql += 'and HP_Name__c Like ' + '\'%' + SearchNameTrim + '%\' ';
    }

    if(SearchNoTrim != null && SearchNoTrim != ''){
      whereSql += 'and OwnershipMachine_No__c Like' + '\'%' + SearchNoTrim + '\'';
    }

    if(SearchSNTrim != null && SearchSNTrim != ''){
      whereSql += 'and SerialNumber like ' + '\'%' + SearchSNTrim + '%\' ';
    }
    searchSql = searchSql + whereStr + whereSql;
    //查询太久
    // searchSql += ' order by Posting_Date__c limit 30';
    if (whereSql !=null && whereSql !='') {
        searchSql += ' limit 10';
    }else{
        searchSql += ' limit 100';
    }
    


    List<Asset> cl = new List<Asset>();

    cl = Database.query(searchSql);
    List<SSPLine> activities = New List<SSPLine>();

    for(Asset psl : cl){
        SSPLine a = new SSPLine(psl);
        activities.add(a);
        
    }
    return activities;
  }

  @AuraEnabled
  public static List<SSPLine> getDataNone(String SearchName , String SearchNo , String SearchSN){
    String searchSql = 'Select id, Account.RecordType.DeveloperName,Account.Parent.Parent.FSE_SP_Main_Leader__r.Work_Location__c,product2.PartSupplyFinishDate__c,Department_Class__c,name,Account.id,Account.Parent.FSE_SP_Main_Leader__r.Work_Location__c,Hospital__r.id, SerialNumber,HP_Name__c,Department_Name__c,Posting_Date__c,Extend_Gurantee_DateTo__c,CurrentContract_End_Date__c from Asset ';
    String whereStr = 'Where  id !=null AND Repairing_Count__c >= 0  ';
    String whereSql = '';
    String SearchNameTrim;
    String SearchNoTrim;
    String SearchSNTrim;
    if (String.isNotBlank(SearchName)) {
         SearchNameTrim = SearchName.trim();
    }
    if (String.isNotBlank(SearchNo)) {
         SearchNoTrim = SearchNo.trim();
    }
    if (String.isNotBlank(SearchSN)) {
         SearchSNTrim = SearchSN.trim();
    }
    if(SearchNameTrim != null && SearchNameTrim != ''){
      whereSql += 'and HP_Name__c Like ' + '\'%' + SearchNameTrim + '%\' ';
    }

    if(SearchNoTrim != null && SearchNoTrim != ''){
      whereSql += 'and OwnershipMachine_No__c like' + '\'%'+ SearchNoTrim + '\'';
    }

    if(SearchSNTrim != null && SearchSNTrim != ''){
      whereSql += 'and SerialNumber like ' + '\'%' + SearchSNTrim + '%\' ';
    }
    searchSql = searchSql + whereStr + whereSql;
    //查询太久
    // searchSql += ' order by Posting_Date__c limit 30';
    searchSql += ' limit 10';
    


    List<Asset> cl = new List<Asset>();

    cl = Database.query(searchSql);
    List<SSPLine> activities = New List<SSPLine>();

    for(Asset psl : cl){
        SSPLine a = new SSPLine(psl);
        activities.add(a);
        
    }
    return activities;
  }


}