public with sharing class MaintenanceContractNotOpenController {
    public MaintenanceContractNotOpenController() {

    }

      
        // select Id,Contract_End_Date__c from Maintenance_Contract__c where Contract_End_Date__c > 2020-12-19  limit 30
        // select Id,Contract_End_Date__c,Renewal_Status__c,Status__c,RecordType_DeveloperName__c   
        //from Maintenance_Contract__c  where Id = 'a0H0l000005w711' and ( Status__c  = '契約満了' or Status__c  = '契約' ) 
        //and Renewal_Status__c = '已完成' and (RecordType_DeveloperName__c= 'VM_Contract' or RecordType_DeveloperName__c = 'NewMaintenance_Contract')
        @AuraEnabled
        public  static string SearchData(String ManagementCodeSearch,String NameSearch,String HospitalSearch,String cmlsearch,String zddseach,String recordtype) {
          String sql = 'select ';
          sql += CommonUtils.GetSqlToPorps(Maintenance_Contract__c.SObjectType);
          string propsStr = ' ,Hospital__r.Name,Department__r.Name,Service_Contract_Staff__r.Alias,Dealer__r.Name,Last_year_service_contract__r.Management_Code__c,Estimation_Id__r.Process_Status__c,Estimation_Id__r.Name  ';
          sql +=propsStr;
          sql += ' from Maintenance_Contract__c ';
          
          Datetime dateTimes =  Datetime.now().addYears(-3);
          string statusOpting = '引合中';
        //   string status2Opting = '契約';
          // string RenewalOpting = '询价中';
          // sql += ' where Contract_End_Date__c  >   ' + dateTimes.format('YYYY-MM-dd');
      
          sql += ' where  Status__c  = :statusOpting ';
          string  OpportunityParam = '关闭';
          sql += ' and Opportunity_Level__c != :OpportunityParam';
          if(ManagementCodeSearch!=null && ManagementCodeSearch!= '')
          {
            ManagementCodeSearch  = '%'+ManagementCodeSearch+'%';
            sql += ' and  Management_Code__c  like :ManagementCodeSearch';
          }
          if(NameSearch!=null && NameSearch!= '')
          {
            NameSearch  = '%'+NameSearch+'%';
             sql += ' and  Name  like :NameSearch';
          }
          if(HospitalSearch!=null && HospitalSearch!= '')
          {
            HospitalSearch  = '%'+HospitalSearch+'%';
            sql += ' and  Hospital__r.Name  like :HospitalSearch';
          }

          if(cmlsearch!=null && cmlsearch!= '')
          {
             sql += ' and  Service_man_province_HP__c  = :cmlsearch';
          }
          if(zddseach!=null && zddseach!= '')
          {
            zddseach  = '%'+zddseach+'%';
             sql += ' and  Service_Contract_Staff__r.Alias  like :zddseach';
          }
          if(recordtype!=null && recordtype!= '')
          {
            recordtype  = '%'+recordtype+'%';
             sql += ' and  RecordType_Name__c  like :recordtype';
          }
          
          // 20210915 gzw 追加排序 start
          sql += ' order by RecordType_DeveloperName__c desc limit 50';
          // 20210915 gzw 追加排序 end
          List<Maintenance_Contract__c> arrays = Database.query(sql);
          return JSON.serialize(arrays); 
        }
      
        //查询失单报告的数据
        @AuraEnabled
        public  static string SearchLostReportData(string [] Ids)
        {
          String sql = 'select ';
          sql += CommonUtils.GetSqlToPorps(Lost_Report__c.SObjectType);
          sql += ' from Lost_Report__c ';
          sql += ' where Maintenance_Contract__c in :ids';
          List<Lost_Report__c> arrays = Database.query(sql);
          return JSON.serialize(arrays);
        }
        
        @AuraEnabled
        public  static string SearchMaintenance_ContractData(string  name)
        {
          String sql2;
          String sql = 'select ';
          sql += CommonUtils.GetSqlToPorps(Maintenance_Contract__c.SObjectType);
          // sql += ' , name';
          sql += ' from Maintenance_Contract__c ';
          if(name!=null && name!= '')
          {
            sql2  = '%'+name+'%';
          }
          sql += ' where Maintenance_Contract_No__c like :sql2 or name like :sql2';
          List<Maintenance_Contract__c> arrays = Database.query(sql);
          return JSON.serialize(arrays);
        }
        
          ///获取具体原因
          @AuraEnabled
          public  static string GetJTYY(){
              String JsonData = CommonUtils.GetSelectedValues(Lost_Report__c.Specific_Reasons__c.getDescribe());
              return JsonData;
          }
      
      
        
          ///获取去向
          @AuraEnabled
          public  static string GetQX(){
              String JsonData = CommonUtils.GetSelectedValues(Lost_Report__c.To_Where__c.getDescribe());
              return JsonData;
          }
      
      
          //保存
          @AuraEnabled
          public  static string SaveData(string JsonData,string Id)
          {
            try{
              Lost_Report__c temp = (Lost_Report__c)JSON.deserialize(JsonData,Lost_Report__c.class); 
              if(Id == null || Id == '')
              {
                insert temp;
              }else{
                update temp;
              }
            }catch(Exception ex)
            {
                return  ex.getMessage();
            }
            return '成功';
          }
      
      //提交
     @AuraEnabled
     public  static string SubmitData(string JsonData,string Id)
     {
       try{
         Lost_Report__c temp = (Lost_Report__c)JSON.deserialize(JsonData,Lost_Report__c.class); 
         temp.Status__c = '草案中';
         if(Id == null || Id == '')
         {
           insert temp;
           temp.Status__c = '提交';
           update temp;
         }else{
           update temp;
           temp.Status__c = '提交';
           update temp;
         }
      }catch(Exception ex)
       {
           return  ex.getMessage();
       }
       return '成功';
     }


      //保存修改
    @AuraEnabled
    public  static string SaveUpdateData(string JsonData)
    {
      try{
        List<Maintenance_Contract__c> mcList = new List<Maintenance_Contract__c>();
        if (JsonData != null &&  JsonData != '' && JsonData !='[]') {
          mcList.addAll((List<Maintenance_Contract__c>)JSON.deserialize(JsonData,List<Maintenance_Contract__c>.class));
        }

        for (Maintenance_Contract__c mcItem : mcList) {
          update mcItem;
        }
      }catch(Exception ex)
      {
          return  ex.getMessage();
      }
      return '成功';

    
    }

      
}