public with sharing class MaintenanceContractController {
  public MaintenanceContractController() {

  }

  // 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 ';
    sql +=propsStr;
    sql += ' from Maintenance_Contract__c ';
    Datetime dateTimes =  Datetime.now().addYears(-3);
    string statusOpting = '契約満了';
    string status2Opting = '契約';
    string RenewalOpting = '已完成';
    string RenewalOpting2 = '关闭';
    // 20210915 gzw 追加排序 start
    // 20210915 gzw 追加排序 end
    // sql += ' where Contract_End_Date__c  >   ' + dateTimes.format('YYYY-MM-dd');

    sql += ' where  ( Status__c  = :statusOpting  or Status__c  = :status2Opting ) and From_warranty_period__c != null and contractNextYear_new__c = null and serviceContractNextYear_new__c =null ';

    sql += '  and (Renewal_Status__c != :RenewalOpting and Renewal_Status__c != :RenewalOpting2 )';

    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 Status__c,Contract_End_Date__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 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 GetUserById(string Id){
      String sql = 'select ';
      sql += CommonUtils.GetSqlToPorps(user.SObjectType);
      sql += ' from user  where Id = :Id';
      List<user> arrays = Database.query(sql);
      return JSON.serialize(arrays);
    }
    ///查询医院
    @AuraEnabled
    public  static string GetAccountById(string Id){
      String sql = 'select ';
      sql += CommonUtils.GetSqlToPorps(account.SObjectType);
      sql += ' from account  where Id = :Id';
      List<account> arrays = Database.query(sql);
      return JSON.serialize(arrays);
    }

    //保存
    @AuraEnabled
    public  static string SaveData(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;
        }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 '成功';

    
    }

}