liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
global class updateFirstServiceContractWebService{
           
      WebService static String updateFirstContract(String mcid ){
         List<Maintenance_Contract__c> mcList = [select id, Effective_Status__c,Status__c,upload_to_sap_time__c from Maintenance_Contract__c where id = :mcid ];
         if (mcList.size() == 0) {
            return '维修合同不存在,请确认维修合同ID。';
        }
        Maintenance_Contract__c mc = mcList[0];
        List<Maintenance_Contract_Asset__c> mcaList = [select id,Maintenance_Contract__r.Contract_Start_Date__c,Maintenance_Contract__r.Contract_End_Date__c,Maintenance_Contract__r.Maintenance_Contract_No__c,Asset__r.name,Asset__r.id,asset__r.First_Service_number__c,asset__r.First_Service_Start_Day__c,asset__r.First_Service_End_Day__c 
                                                         from Maintenance_Contract_Asset__c where Maintenance_Contract__c = :mc.id];
        List<Asset> assList = new List<Asset>();
        if (mc.Effective_Status__c =='有效' && mc.Status__c =='契約'&&mc.upload_to_sap_time__c==null && mcaList!= null) {
             for (Maintenance_Contract_Asset__c mca:mcaList) {
                  Asset ass = new Asset();
                  ass.id = mca.asset__r.id;
                if (mca.asset__r.First_Service_number__c == null) {
                   ass.First_Service_number__c = mca.Maintenance_Contract__r.Maintenance_Contract_No__c;
                }
                if (mca.asset__r.First_Service_End_Day__c  == null) {
                   ass.First_Service_End_Day__c = mca.Maintenance_Contract__r.Contract_End_Date__c;
                }
                if (mca.asset__r.First_Service_Start_Day__c == null) {
                   ass.First_Service_Start_Day__c = mca.Maintenance_Contract__r.Contract_Start_Date__c;
                }
                assList.add(ass);
             }
        }
        try{
              if (assList.size()>0) {
                update assList;
             }
        }catch(Exception e){
         return e.getMessage();
        }
        return '1';
     }
 
    
}